-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
Enable prompts on the Hub #23662
Merged
Merged
Enable prompts on the Hub #23662
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,173 +14,33 @@ | |||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
# See the License for the specific language governing permissions and | ||||||
# limitations under the License. | ||||||
import re | ||||||
|
||||||
# docstyle-ignore | ||||||
RUN_PROMPT_TEMPLATE = """I will ask you to perform a task, your job is to come up with a series of simple commands in Python that will perform the task. | ||||||
To help you, I will give you access to a set of tools that you can use. Each tool is a Python function and has a description explaining the task it performs, the inputs it expects and the outputs it returns. | ||||||
You should first explain which tool you will use to perform the task and for what reason, then write the code in Python. | ||||||
Each instruction in Python should be a simple assignment. You can print intermediate results if it makes sense to do so. | ||||||
|
||||||
Tools: | ||||||
<<all_tools>> | ||||||
|
||||||
|
||||||
Task: "Answer the question in the variable `question` about the image stored in the variable `image`. The question is in French." | ||||||
|
||||||
I will use the following tools: `translator` to translate the question into English and then `image_qa` to answer the question on the input image. | ||||||
|
||||||
Answer: | ||||||
```py | ||||||
translated_question = translator(question=question, src_lang="French", tgt_lang="English") | ||||||
print(f"The translated question is {translated_question}.") | ||||||
answer = image_qa(image=image, question=translated_question) | ||||||
print(f"The answer is {answer}") | ||||||
``` | ||||||
|
||||||
Task: "Identify the oldest person in the `document` and create an image showcasing the result." | ||||||
|
||||||
I will use the following tools: `document_qa` to find the oldest person in the document, then `image_generator` to generate an image according to the answer. | ||||||
|
||||||
Answer: | ||||||
```py | ||||||
answer = document_qa(document, question="What is the oldest person?") | ||||||
print(f"The answer is {answer}.") | ||||||
image = image_generator(answer) | ||||||
``` | ||||||
|
||||||
Task: "Generate an image using the text given in the variable `caption`." | ||||||
|
||||||
I will use the following tool: `image_generator` to generate an image. | ||||||
|
||||||
Answer: | ||||||
```py | ||||||
image = image_generator(prompt=caption) | ||||||
``` | ||||||
|
||||||
Task: "Summarize the text given in the variable `text` and read it out loud." | ||||||
|
||||||
I will use the following tools: `summarizer` to create a summary of the input text, then `text_reader` to read it out loud. | ||||||
|
||||||
Answer: | ||||||
```py | ||||||
summarized_text = summarizer(text) | ||||||
print(f"Summary: {summarized_text}") | ||||||
audio_summary = text_reader(summarized_text) | ||||||
``` | ||||||
|
||||||
Task: "Answer the question in the variable `question` about the text in the variable `text`. Use the answer to generate an image." | ||||||
|
||||||
I will use the following tools: `text_qa` to create the answer, then `image_generator` to generate an image according to the answer. | ||||||
|
||||||
Answer: | ||||||
```py | ||||||
answer = text_qa(text=text, question=question) | ||||||
print(f"The answer is {answer}.") | ||||||
image = image_generator(answer) | ||||||
``` | ||||||
|
||||||
Task: "Caption the following `image`." | ||||||
|
||||||
I will use the following tool: `image_captioner` to generate a caption for the image. | ||||||
|
||||||
Answer: | ||||||
```py | ||||||
caption = image_captioner(image) | ||||||
``` | ||||||
|
||||||
Task: "<<prompt>>" | ||||||
|
||||||
I will use the following""" | ||||||
from ..utils import cached_file | ||||||
|
||||||
|
||||||
# docstyle-ignore | ||||||
CHAT_PROMPT_TEMPLATE = """Below are a series of dialogues between various people and an AI assistant specialized in coding. The AI assistant tries to be helpful, polite, honest, and humble-but-knowledgeable. | ||||||
|
||||||
The job of the AI assistant is to come up with a series of simple commands in Python that will perform the task the human wants to perform. | ||||||
To help with that, the AI assistant has access to a set of tools. Each tool is a Python function and has a description explaining the task it performs, the inputs it expects and the outputs it returns. | ||||||
The AI assistant should first explain the tools it will use to perform the task and for what reason, then write the code in Python. | ||||||
Each instruction in Python should be a simple assignment. The AI assistant can print intermediate results if it makes sense to do so. | ||||||
|
||||||
Tools: | ||||||
<<all_tools>> | ||||||
|
||||||
===== | ||||||
|
||||||
Human: Answer the question in the variable `question` about the image stored in the variable `image`. | ||||||
|
||||||
Assistant: I will use the tool `image_qa` to answer the question on the input image. | ||||||
|
||||||
```py | ||||||
answer = image_qa(text=question, image=image) | ||||||
print(f"The answer is {answer}") | ||||||
``` | ||||||
|
||||||
Human: I tried this code, it worked but didn't give me a good result. The question is in French | ||||||
|
||||||
Assistant: In this case, the question needs to be translated first. I will use the tool `translator` to do this. | ||||||
|
||||||
```py | ||||||
translated_question = translator(question=question, src_lang="French", tgt_lang="English") | ||||||
print(f"The translated question is {translated_question}.") | ||||||
answer = image_qa(text=translated_question, image=image) | ||||||
print(f"The answer is {answer}") | ||||||
``` | ||||||
|
||||||
===== | ||||||
|
||||||
Human: Identify the oldest person in the `document`. | ||||||
|
||||||
Assistant: I will use the tool `document_qa` to find the oldest person in the document. | ||||||
|
||||||
```py | ||||||
answer = document_qa(document, question="What is the oldest person?") | ||||||
print(f"The answer is {answer}.") | ||||||
``` | ||||||
|
||||||
Human: Can you generate an image with the result? | ||||||
|
||||||
Assistant: I will use the tool `image_generator` to do that. | ||||||
|
||||||
```py | ||||||
image = image_generator(answer) | ||||||
``` | ||||||
|
||||||
===== | ||||||
|
||||||
Human: Summarize the text given in the variable `text` and read it out loud. | ||||||
|
||||||
Assistant: I will use the tool `summarizer` to create a summary of the input text, then the tool `text_reader` to read it out loud. | ||||||
|
||||||
```py | ||||||
summarized_text = summarizer(text) | ||||||
print(f"Summary: {summarized_text}") | ||||||
audio_summary = text_reader(text=summary) | ||||||
``` | ||||||
|
||||||
Human: I got the following error: "The variable `summary` is not defined." | ||||||
|
||||||
Assistant: My bad! Let's try this code instead. | ||||||
|
||||||
```py | ||||||
summarized_text = summarizer(text) | ||||||
print(f"Summary: {summarized_text}") | ||||||
audio_summary = text_reader(text=summarized_text) | ||||||
``` | ||||||
CHAT_MESSAGE_PROMPT = """ | ||||||
Human: <<task>> | ||||||
|
||||||
Human: It worked! Can you translate the summary in German? | ||||||
Assistant: """ | ||||||
|
||||||
Assistant: I will use the tool `translator` to translate the text in German. | ||||||
|
||||||
```py | ||||||
translated_summary = translator(summarized_text, src_lang="English", tgt_lang="German") | ||||||
``` | ||||||
DEFAULT_PROMPTS_REPO = "huggingface-tools/default-prompts" | ||||||
PROMPT_FILES = {"chat": "chat_prompt_template.txt", "run": "run_prompt_template.txt"} | ||||||
|
||||||
==== | ||||||
""" | ||||||
|
||||||
def download_prompt(prompt, agent_name, mode="run"): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
to make it clearer that the input can be both text and repo id? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||||||
""" | ||||||
Downloads and caches the prompt from a repo and returns it contents (if necessary) | ||||||
""" | ||||||
if prompt is None: | ||||||
prompt = "huggingface-tools/default-prompts" | ||||||
sgugger marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
# docstyle-ignore | ||||||
CHAT_MESSAGE_PROMPT = """ | ||||||
Human: <<task>> | ||||||
# prompt is considered a repo ID when it does not contain any kind of space | ||||||
if re.search("\\s", prompt) is not None: | ||||||
return prompt | ||||||
|
||||||
Assistant: """ | ||||||
prompt_file = cached_file(prompt, PROMPT_FILES[mode], repo_type="dataset", user_agent={"agent": agent_name}) | ||||||
with open(prompt_file, "r", encoding="utf-8") as f: | ||||||
return f.read() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to add to the doc string that
chat_prompt_template
andrun_prompt_template
can be prompts or repo ids.