-
Notifications
You must be signed in to change notification settings - Fork 925
[Llama2] Section on system prompt #1329
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5aa4e0a
[Llama2] Section on system prompt
pcuenca f5d647c
h/t clefourrier
pcuenca 1095a84
Update llama2.md
pcuenca 08cb284
Add prompt templates as suggested by Philipp.
pcuenca 0b28b25
Align prompt templates with spec
pcuenca 7115353
More clarification that the format is important.
pcuenca 48238a3
Link to 70B demo.
pcuenca 5fe7b7f
Fix templates according to spec shared by ArthurZ
pcuenca cf5e892
Mention ArthurZ
pcuenca c294f6c
Suggestion from Omar
pcuenca 4266ea8
Apply suggestions.
pcuenca 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 hidden or 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 |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ We’ve collaborated with Meta to ensure smooth integration into the Hugging Fac | |
| - [With Transformers](#using-transformers) | ||
| - [With Inference Endpoints](#using-text-generation-inference-and-inference-endpoints) | ||
| - [Fine-tuning with PEFT](#fine-tuning-with-peft) | ||
| - [How to Prompt Llama 2](#how-to-prompt-llama-2) | ||
| - [Additional Resources](#additional-resources) | ||
| - [Conclusion](#conclusion) | ||
|
|
||
|
|
@@ -177,12 +178,67 @@ python trl/examples/scripts/sft_trainer.py \ | |
| --gradient_accumulation_steps 2 | ||
| ``` | ||
|
|
||
| ## How to Prompt Llama 2 | ||
osanseviero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| One of the unsung advantages of open-access models is that you have full control over the `system` prompt in chat applications. This is essential to specify the behavior of your chat assistant –and even imbue it with some personality–, but it's unreachable in models served behind APIs. | ||
osanseviero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| We're adding this section just a few days after the initial release of Llama 2, as we've had many questions from the community about how to prompt the models and how to change the system prompt. We hope this helps! | ||
|
|
||
osanseviero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| The prompt template for the first turn looks like this: | ||
|
|
||
| ``` | ||
osanseviero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <s>[INST] <<SYS>> | ||
| {{ system_prompt }} | ||
| <</SYS>> | ||
|
|
||
| {{ user_message }} [/INST] | ||
| ``` | ||
|
|
||
| This template follows the model's training procedure, as described in [the Llama 2 paper](https://huggingface.co/papers/2307.09288). We can use any `system_prompt` we want, but it's crucial that the format matches the one used during training. | ||
|
|
||
| To spell it out in full clarity, this is what is actually sent to the language model when the user enters some text (`There's a llama in my garden 😱 What should I do?`) in [our 13B chat demo](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat) to initiate a chat: | ||
|
|
||
| ```b | ||
| <s>[INST] <<SYS>> | ||
| You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. | ||
|
|
||
| If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. | ||
| <</SYS>> | ||
|
|
||
| There's a llama in my garden 😱 What should I do? [/INST] | ||
| ``` | ||
|
|
||
| As you can see, the instructions between the special `<<SYS>>` tokens provide context for the model so it knows how we expect it to respond. This works because exactly the same format was used during training with a wide variety of system prompts intended for different tasks. | ||
|
|
||
| As the conversation progresses, _all_ the interactions between the human and the "bot" are appended to the previous prompt, enclosed between `[INST]` delimiters. The template used during multi-turn conversations follows this structure (🎩 h/t [Arthur Zucker](https://huggingface.co/ArthurZ) for some final clarifications): | ||
|
|
||
| ```b | ||
| <s>[INST] <<SYS>> | ||
| {{ system_prompt }} | ||
| <</SYS>> | ||
|
|
||
| {{ user_msg_1 }} [/INST] {{ model_answer_1 }} </s>\ | ||
| <s>[INST] {{ user_msg_2 }} [/INST] {{ model_answer_2 }} </s>\ | ||
| <s>[INST] {{ user_msg_3 }} [/INST] | ||
| ``` | ||
|
|
||
| The model is stateless and does not "remember" previous fragments of the conversation, we must always supply it with all the context so the conversation can continue. This is the reason why **context length** is a very important parameter to maximize, as it allows for longer conversations and larger amounts of information to be used. | ||
|
|
||
| ### Ignore previous instructions | ||
|
|
||
| In API-based models, people resort to tricks in an attempt to override the system prompt and change the default model behaviour. As imaginative as these solutions are, this is not necessary in open-access models: anyone can use a different prompt, as long as it follows the format described above. We believe that this will be an important tool for researchers to study the impact of prompts on both desired and unwanted characteristics. For example, when people [are surprised with absurdly cautious generations](https://twitter.com/lauraruis/status/1681612002718887936), you can explore whether maybe [a different prompt would work](https://twitter.com/overlordayn/status/1681631554672513025). (🎩 h/t [Clémentine Fourrier](https://huggingface.co/clefourrier) for the links to this example). | ||
|
|
||
| In our [`13B`](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat) and [`7B`](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat) demos, you can easily explore this feature by disclosing the "Advanced Options" UI and simply writing your desired instructions. You can also duplicate those demos and use them privately for fun or research! | ||
|
Comment on lines
+229
to
+231
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. really cool section! |
||
|
|
||
| ## Additional Resources | ||
|
|
||
| - [Paper Page](https://huggingface.co/papers/2307.09288) | ||
| - [Models on the Hub](https://huggingface.co/meta-llama) | ||
| - [Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard) | ||
| - [Meta Examples and recipes for Llama model](https://github.com/facebookresearch/llama-recipes/tree/main) | ||
| - [Chat demo (7B)](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat) | ||
| - [Chat demo (13B)](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat) | ||
| - [Chat demo (70B) on TGI](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI) | ||
|
|
||
|
|
||
| ## Conclusion | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.