Update README with HF usage
#3
by
Molbap
HF Staff
- opened
README.md
CHANGED
|
@@ -221,6 +221,54 @@ result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
|
|
| 221 |
print(result)
|
| 222 |
```
|
| 223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
## The Mistral AI Team
|
| 226 |
|
|
|
|
| 221 |
print(result)
|
| 222 |
```
|
| 223 |
|
| 224 |
+
### Usage in Hugging Face Transformers
|
| 225 |
+
|
| 226 |
+
You can also use Hugging Face `transformers` library to run inference using various chat templates, or fine-tune the model.
|
| 227 |
+
Example for inference:
|
| 228 |
+
|
| 229 |
+
```python
|
| 230 |
+
from transformers import LlamaTokenizerFast, MistralForCausalLM
|
| 231 |
+
import torch
|
| 232 |
+
|
| 233 |
+
device = "cuda"
|
| 234 |
+
tokenizer = LlamaTokenizerFast.from_pretrained('mistralai/Mistral-Small-Instruct-2409')
|
| 235 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 236 |
+
|
| 237 |
+
model = MistralForCausalLM.from_pretrained('mistralai/Mistral-Small-Instruct-2409', torch_dtype=torch.bfloat16)
|
| 238 |
+
model = model.to(device)
|
| 239 |
+
|
| 240 |
+
prompt = "How often does the letter r occur in Mistral?"
|
| 241 |
+
|
| 242 |
+
messages = [
|
| 243 |
+
{"role": "user", "content": prompt},
|
| 244 |
+
]
|
| 245 |
+
|
| 246 |
+
model_input = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(device)
|
| 247 |
+
gen = model.generate(model_input, max_new_tokens=150)
|
| 248 |
+
dec = tokenizer.batch_decode(gen)
|
| 249 |
+
print(dec)
|
| 250 |
+
```
|
| 251 |
+
|
| 252 |
+
And you should obtain
|
| 253 |
+
```text
|
| 254 |
+
<s>
|
| 255 |
+
[INST]
|
| 256 |
+
How often does the letter r occur in Mistral?
|
| 257 |
+
[/INST]
|
| 258 |
+
To determine how often the letter "r" occurs in the word "Mistral,"
|
| 259 |
+
we can simply count the instances of "r" in the word.
|
| 260 |
+
The word "Mistral" is broken down as follows:
|
| 261 |
+
- M
|
| 262 |
+
- i
|
| 263 |
+
- s
|
| 264 |
+
- t
|
| 265 |
+
- r
|
| 266 |
+
- a
|
| 267 |
+
- l
|
| 268 |
+
Counting the "r"s, we find that there is only one "r" in "Mistral."
|
| 269 |
+
Therefore, the letter "r" occurs once in the word "Mistral."
|
| 270 |
+
</s>
|
| 271 |
+
```
|
| 272 |
|
| 273 |
## The Mistral AI Team
|
| 274 |
|