Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support chat generator as input of TextGenerationPipeline #35551

Merged
merged 6 commits into from
Jan 8, 2025

Conversation

lhoestq
Copy link
Member

@lhoestq lhoestq commented Jan 7, 2025

Pipelines generally accept a generator input like pipe(input for input in inputs). In this case the output is an iterator that yields results one by one as they arrive from the pipeline.

TextGenerationPipeline does accept a generator of strings pipe(text for text in texts). Hoewever it doesn't accept a generator of chats pipe(chat for chat in chats) at the moment. This PR fixes this issue and adds support for pipe(chat for chat in chats).

In particular this PR enables this use case with a nice tqdm bar :)

>>> import pandas as pd
>>> from tqdm import tqdm
>>> df = pd.DataFrame({"text": ["Nvidia announces new RTX graphics cards"] * 10})
>>> prompt = "What is the main topic of this text ? REPLY IN LESS THAN 3 WORDS. Text: '{}'"
>>> df["main_topic"] = [
...     y[0]["generated_text"][1]["content"]
...     for y in pipe(([{"role": "user", "content": prompt.format(x)}] for x in tqdm(df["text"])), do_sample=False)
... ]
100%|███████████████████████████████████████████████████████████████████| 10/10 [00:05<00:00,  1.96it/s]
>>> df.head()
                                      text                    main_topic
0  Nvidia announces new RTX graphics cards  New Graphics Cards Announced
1  Nvidia announces new RTX graphics cards  New Graphics Cards Announced
2  Nvidia announces new RTX graphics cards  New Graphics Cards Announced
3  Nvidia announces new RTX graphics cards  New Graphics Cards Announced
4  Nvidia announces new RTX graphics cards  New Graphics Cards Announced

currently it raises

File ~/hf/transformers/src/transformers/pipelines/text_generation.py:310, in TextGenerationPipeline.preprocess(self, prompt_text, prefix, handle_long_generation, add_special_tokens, truncation, padding, max_length, continue_final_message, **generate_kwargs)
    301     inputs = self.tokenizer.apply_chat_template(
    302         prompt_text.messages,
    303         add_generation_prompt=not continue_final_message,
   (...)
    307         **tokenizer_kwargs,
    308     )
    309 else:
--> 310     inputs = self.tokenizer(prefix + prompt_text, return_tensors=self.framework, **tokenizer_kwargs)
    312 inputs["prompt_text"] = prompt_text
    314 if handle_long_generation == "hole":

TypeError: can only concatenate str (not "list") to str

@lhoestq lhoestq force-pushed the chat-generator-input-for-text-gen-pipe branch from 8b6df5a to 6f7c1cd Compare January 7, 2025 17:29
@lhoestq lhoestq marked this pull request as ready for review January 7, 2025 18:17
@lhoestq lhoestq requested a review from Rocketknight1 January 7, 2025 18:17
Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks good! Can you add a small test (or modify an existing test) in tests/pipelines/test_pipelines_text_generation.py to cover this case?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link

@ruidazeng ruidazeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome actually!

@lhoestq lhoestq merged commit 3fde88b into main Jan 8, 2025
26 checks passed
@lhoestq lhoestq deleted the chat-generator-input-for-text-gen-pipe branch January 8, 2025 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants