Add chat template
#1
by
Rocketknight1
HF Staff
- opened
- README.md +14 -5
- tokenizer_config.json +1 -0
README.md
CHANGED
|
@@ -16,6 +16,13 @@ For full details of this model please read our [release blog post](https://mistr
|
|
| 16 |
In order to leverage instruction fine-tuning, your prompt should be surrounded by `[INST]` and `[\INST]` tokens. The very first instruction should begin with a begin of sentence id. The next instructions should not. The assistant generation will be ended by the end-of-sentence token id.
|
| 17 |
|
| 18 |
E.g.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
```python
|
| 21 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
@@ -25,16 +32,18 @@ device = "cuda" # the device to load the model onto
|
|
| 25 |
model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
|
| 27 |
|
| 28 |
-
|
| 29 |
-
"
|
| 30 |
-
"
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
encodeds = tokenizer(
|
| 33 |
|
| 34 |
model_inputs = encodeds.to(device)
|
| 35 |
model.to(device)
|
| 36 |
|
| 37 |
-
generated_ids = model.generate(
|
| 38 |
decoded = tokenizer.batch_decode(generated_ids)
|
| 39 |
print(decoded[0])
|
| 40 |
```
|
|
|
|
| 16 |
In order to leverage instruction fine-tuning, your prompt should be surrounded by `[INST]` and `[\INST]` tokens. The very first instruction should begin with a begin of sentence id. The next instructions should not. The assistant generation will be ended by the end-of-sentence token id.
|
| 17 |
|
| 18 |
E.g.
|
| 19 |
+
```
|
| 20 |
+
text = "<s>[INST] What is your favourite condiment? [/INST]"
|
| 21 |
+
"Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!</s> "
|
| 22 |
+
"[INST] Do you have mayonnaise recipes? [/INST]"
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
This format is available as a [chat template](https://huggingface.co/docs/transformers/main/chat_templating) via the `apply_chat_template()` method:
|
| 26 |
|
| 27 |
```python
|
| 28 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 32 |
model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
|
| 33 |
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
|
| 34 |
|
| 35 |
+
messages = [
|
| 36 |
+
{"role": "user", "content": "What is your favourite condiment?"},
|
| 37 |
+
{"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
|
| 38 |
+
{"role": "user", "content": "Do you have mayonnaise recipes?"}
|
| 39 |
+
]
|
| 40 |
|
| 41 |
+
encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt")
|
| 42 |
|
| 43 |
model_inputs = encodeds.to(device)
|
| 44 |
model.to(device)
|
| 45 |
|
| 46 |
+
generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)
|
| 47 |
decoded = tokenizer.batch_decode(generated_ids)
|
| 48 |
print(decoded[0])
|
| 49 |
```
|
tokenizer_config.json
CHANGED
|
@@ -29,6 +29,7 @@
|
|
| 29 |
},
|
| 30 |
"additional_special_tokens": [],
|
| 31 |
"bos_token": "<s>",
|
|
|
|
| 32 |
"clean_up_tokenization_spaces": false,
|
| 33 |
"eos_token": "</s>",
|
| 34 |
"legacy": true,
|
|
|
|
| 29 |
},
|
| 30 |
"additional_special_tokens": [],
|
| 31 |
"bos_token": "<s>",
|
| 32 |
+
"chat_template": "{{ bos_token }}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if message['role'] == 'user' %}{{ '[INST] ' + message['content'] + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ message['content'] + eos_token + ' ' }}{% else %}{{ raise_exception('Only user and assistant roles are supported!') }}{% endif %}{% endfor %}",
|
| 33 |
"clean_up_tokenization_spaces": false,
|
| 34 |
"eos_token": "</s>",
|
| 35 |
"legacy": true,
|