Update README.md
Browse files
README.md
CHANGED
@@ -23,174 +23,6 @@ Mistral-7B-v0.3 has the following changes compared to [Mistral-7B-v0.2](https://
|
|
23 |
- Supports v3 Tokenizer
|
24 |
- Supports function calling
|
25 |
|
26 |
-
## Installation
|
27 |
-
|
28 |
-
It is recommended to use `mistralai/Mistral-7B-Instruct-v0.3` with [mistral-inference](https://github.com/mistralai/mistral-inference). For HF transformers code snippets, please keep scrolling.
|
29 |
-
|
30 |
-
```
|
31 |
-
pip install mistral_inference
|
32 |
-
```
|
33 |
-
|
34 |
-
## Download
|
35 |
-
|
36 |
-
```py
|
37 |
-
from huggingface_hub import snapshot_download
|
38 |
-
from pathlib import Path
|
39 |
-
|
40 |
-
mistral_models_path = Path.home().joinpath('mistral_models', '7B-Instruct-v0.3')
|
41 |
-
mistral_models_path.mkdir(parents=True, exist_ok=True)
|
42 |
-
|
43 |
-
snapshot_download(repo_id="mistralai/Mistral-7B-Instruct-v0.3", allow_patterns=["params.json", "consolidated.safetensors", "tokenizer.model.v3"], local_dir=mistral_models_path)
|
44 |
-
```
|
45 |
-
|
46 |
-
### Chat
|
47 |
-
|
48 |
-
After installing `mistral_inference`, a `mistral-chat` CLI command should be available in your environment. You can chat with the model using
|
49 |
-
|
50 |
-
```
|
51 |
-
mistral-chat $HOME/mistral_models/7B-Instruct-v0.3 --instruct --max_tokens 256
|
52 |
-
```
|
53 |
-
|
54 |
-
### Instruct following
|
55 |
-
|
56 |
-
```py
|
57 |
-
from mistral_inference.model import Transformer
|
58 |
-
from mistral_inference.generate import generate
|
59 |
-
|
60 |
-
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
61 |
-
from mistral_common.protocol.instruct.messages import UserMessage
|
62 |
-
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
63 |
-
|
64 |
-
|
65 |
-
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")
|
66 |
-
model = Transformer.from_folder(mistral_models_path)
|
67 |
-
|
68 |
-
completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
|
69 |
-
|
70 |
-
tokens = tokenizer.encode_chat_completion(completion_request).tokens
|
71 |
-
|
72 |
-
out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
|
73 |
-
result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
|
74 |
-
|
75 |
-
print(result)
|
76 |
-
```
|
77 |
-
|
78 |
-
### Function calling
|
79 |
-
|
80 |
-
```py
|
81 |
-
from mistral_common.protocol.instruct.tool_calls import Function, Tool
|
82 |
-
from mistral_inference.model import Transformer
|
83 |
-
from mistral_inference.generate import generate
|
84 |
-
|
85 |
-
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
86 |
-
from mistral_common.protocol.instruct.messages import UserMessage
|
87 |
-
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
88 |
-
|
89 |
-
|
90 |
-
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")
|
91 |
-
model = Transformer.from_folder(mistral_models_path)
|
92 |
-
|
93 |
-
completion_request = ChatCompletionRequest(
|
94 |
-
tools=[
|
95 |
-
Tool(
|
96 |
-
function=Function(
|
97 |
-
name="get_current_weather",
|
98 |
-
description="Get the current weather",
|
99 |
-
parameters={
|
100 |
-
"type": "object",
|
101 |
-
"properties": {
|
102 |
-
"location": {
|
103 |
-
"type": "string",
|
104 |
-
"description": "The city and state, e.g. San Francisco, CA",
|
105 |
-
},
|
106 |
-
"format": {
|
107 |
-
"type": "string",
|
108 |
-
"enum": ["celsius", "fahrenheit"],
|
109 |
-
"description": "The temperature unit to use. Infer this from the users location.",
|
110 |
-
},
|
111 |
-
},
|
112 |
-
"required": ["location", "format"],
|
113 |
-
},
|
114 |
-
)
|
115 |
-
)
|
116 |
-
],
|
117 |
-
messages=[
|
118 |
-
UserMessage(content="What's the weather like today in Paris?"),
|
119 |
-
],
|
120 |
-
)
|
121 |
-
|
122 |
-
tokens = tokenizer.encode_chat_completion(completion_request).tokens
|
123 |
-
|
124 |
-
out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
|
125 |
-
result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
|
126 |
-
|
127 |
-
print(result)
|
128 |
-
```
|
129 |
-
|
130 |
-
## Generate with `transformers`
|
131 |
-
|
132 |
-
If you want to use Hugging Face `transformers` to generate text, you can do something like this.
|
133 |
-
|
134 |
-
```py
|
135 |
-
from transformers import pipeline
|
136 |
-
|
137 |
-
messages = [
|
138 |
-
{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
|
139 |
-
{"role": "user", "content": "Who are you?"},
|
140 |
-
]
|
141 |
-
chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3")
|
142 |
-
chatbot(messages)
|
143 |
-
```
|
144 |
-
|
145 |
-
|
146 |
-
## Function calling with `transformers`
|
147 |
-
|
148 |
-
To use this example, you'll need `transformers` version 4.42.0 or higher. Please see the
|
149 |
-
[function calling guide](https://huggingface.co/docs/transformers/main/chat_templating#advanced-tool-use--function-calling)
|
150 |
-
in the `transformers` docs for more information.
|
151 |
-
|
152 |
-
```python
|
153 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
154 |
-
import torch
|
155 |
-
|
156 |
-
model_id = "mistralai/Mistral-7B-Instruct-v0.3"
|
157 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
158 |
-
|
159 |
-
def get_current_weather(location: str, format: str):
|
160 |
-
"""
|
161 |
-
Get the current weather
|
162 |
-
|
163 |
-
Args:
|
164 |
-
location: The city and state, e.g. San Francisco, CA
|
165 |
-
format: The temperature unit to use. Infer this from the users location. (choices: ["celsius", "fahrenheit"])
|
166 |
-
"""
|
167 |
-
pass
|
168 |
-
|
169 |
-
conversation = [{"role": "user", "content": "What's the weather like in Paris?"}]
|
170 |
-
tools = [get_current_weather]
|
171 |
-
|
172 |
-
# render the tool use prompt as a string:
|
173 |
-
tool_use_prompt = tokenizer.apply_chat_template(
|
174 |
-
conversation,
|
175 |
-
tools=tools,
|
176 |
-
tokenize=False,
|
177 |
-
add_generation_prompt=True,
|
178 |
-
)
|
179 |
-
|
180 |
-
inputs = tokenizer(tool_use_prompt, return_tensors="pt")
|
181 |
-
|
182 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
|
183 |
-
|
184 |
-
outputs = model.generate(**inputs, max_new_tokens=1000)
|
185 |
-
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
186 |
-
```
|
187 |
-
|
188 |
-
Note that, for reasons of space, this example does not show a complete cycle of calling a tool and adding the tool call and tool
|
189 |
-
results to the chat history so that the model can use them in its next generation. For a full tool calling example, please
|
190 |
-
see the [function calling guide](https://huggingface.co/docs/transformers/main/chat_templating#advanced-tool-use--function-calling),
|
191 |
-
and note that Mistral **does** use tool call IDs, so these must be included in your tool calls and tool results. They should be
|
192 |
-
exactly 9 alphanumeric characters.
|
193 |
-
|
194 |
|
195 |
## Limitations
|
196 |
|
|
|
23 |
- Supports v3 Tokenizer
|
24 |
- Supports function calling
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
## Limitations
|
28 |
|