professorf commited on
Commit
c2526fb
1 Parent(s): 93ac92d

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +195 -0
README.md ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ base_model: mistralai/Mistral-7B-v0.3
4
+ extra_gated_description: If you want to learn more about how we process your personal data, please read our <a href="https://mistral.ai/terms/">Privacy Policy</a>.
5
+ ---
6
+ [Note: Quantized by **ProfessorF (Nick V. Flor)** for research reproducibility purposes. In those research papers where I mention this model, this is the version used.]
7
+
8
+ # Model Card for Mistral-7B-Instruct-v0.3
9
+
10
+ The Mistral-7B-Instruct-v0.3 Large Language Model (LLM) is an instruct fine-tuned version of the Mistral-7B-v0.3.
11
+
12
+ Mistral-7B-v0.3 has the following changes compared to [Mistral-7B-v0.2](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2/edit/main/README.md)
13
+ - Extended vocabulary to 32768
14
+ - Supports v3 Tokenizer
15
+ - Supports function calling
16
+
17
+ ## Installation
18
+
19
+ 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.
20
+
21
+ ```
22
+ pip install mistral_inference
23
+ ```
24
+
25
+ ## Download
26
+
27
+ ```py
28
+ from huggingface_hub import snapshot_download
29
+ from pathlib import Path
30
+
31
+ mistral_models_path = Path.home().joinpath('mistral_models', '7B-Instruct-v0.3')
32
+ mistral_models_path.mkdir(parents=True, exist_ok=True)
33
+
34
+ snapshot_download(repo_id="mistralai/Mistral-7B-Instruct-v0.3", allow_patterns=["params.json", "consolidated.safetensors", "tokenizer.model.v3"], local_dir=mistral_models_path)
35
+ ```
36
+
37
+ ### Chat
38
+
39
+ After installing `mistral_inference`, a `mistral-chat` CLI command should be available in your environment. You can chat with the model using
40
+
41
+ ```
42
+ mistral-chat $HOME/mistral_models/7B-Instruct-v0.3 --instruct --max_tokens 256
43
+ ```
44
+
45
+ ### Instruct following
46
+
47
+ ```py
48
+ from mistral_inference.transformer import Transformer
49
+ from mistral_inference.generate import generate
50
+
51
+ from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
52
+ from mistral_common.protocol.instruct.messages import UserMessage
53
+ from mistral_common.protocol.instruct.request import ChatCompletionRequest
54
+
55
+
56
+ tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")
57
+ model = Transformer.from_folder(mistral_models_path)
58
+
59
+ completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
60
+
61
+ tokens = tokenizer.encode_chat_completion(completion_request).tokens
62
+
63
+ out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
64
+ result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
65
+
66
+ print(result)
67
+ ```
68
+
69
+ ### Function calling
70
+
71
+ ```py
72
+ from mistral_common.protocol.instruct.tool_calls import Function, Tool
73
+ from mistral_inference.transformer import Transformer
74
+ from mistral_inference.generate import generate
75
+
76
+ from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
77
+ from mistral_common.protocol.instruct.messages import UserMessage
78
+ from mistral_common.protocol.instruct.request import ChatCompletionRequest
79
+
80
+
81
+ tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")
82
+ model = Transformer.from_folder(mistral_models_path)
83
+
84
+ completion_request = ChatCompletionRequest(
85
+ tools=[
86
+ Tool(
87
+ function=Function(
88
+ name="get_current_weather",
89
+ description="Get the current weather",
90
+ parameters={
91
+ "type": "object",
92
+ "properties": {
93
+ "location": {
94
+ "type": "string",
95
+ "description": "The city and state, e.g. San Francisco, CA",
96
+ },
97
+ "format": {
98
+ "type": "string",
99
+ "enum": ["celsius", "fahrenheit"],
100
+ "description": "The temperature unit to use. Infer this from the users location.",
101
+ },
102
+ },
103
+ "required": ["location", "format"],
104
+ },
105
+ )
106
+ )
107
+ ],
108
+ messages=[
109
+ UserMessage(content="What's the weather like today in Paris?"),
110
+ ],
111
+ )
112
+
113
+ tokens = tokenizer.encode_chat_completion(completion_request).tokens
114
+
115
+ out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
116
+ result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
117
+
118
+ print(result)
119
+ ```
120
+
121
+ ## Generate with `transformers`
122
+
123
+ If you want to use Hugging Face `transformers` to generate text, you can do something like this.
124
+
125
+ ```py
126
+ from transformers import pipeline
127
+
128
+ messages = [
129
+ {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
130
+ {"role": "user", "content": "Who are you?"},
131
+ ]
132
+ chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3")
133
+ chatbot(messages)
134
+ ```
135
+
136
+
137
+ ## Function calling with `transformers`
138
+
139
+ To use this example, you'll need `transformers` version 4.42.0 or higher. Please see the
140
+ [function calling guide](https://huggingface.co/docs/transformers/main/chat_templating#advanced-tool-use--function-calling)
141
+ in the `transformers` docs for more information.
142
+
143
+ ```python
144
+ from transformers import AutoModelForCausalLM, AutoTokenizer
145
+ import torch
146
+
147
+ model_id = "mistralai/Mistral-7B-Instruct-v0.3"
148
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
149
+
150
+ def get_current_weather(location: str, format: str):
151
+ """
152
+ Get the current weather
153
+
154
+ Args:
155
+ location: The city and state, e.g. San Francisco, CA
156
+ format: The temperature unit to use. Infer this from the users location. (choices: ["celsius", "fahrenheit"])
157
+ """
158
+ pass
159
+
160
+ conversation = [{"role": "user", "content": "What's the weather like in Paris?"}]
161
+ tools = [get_current_weather]
162
+
163
+
164
+ # format and tokenize the tool use prompt
165
+ inputs = tokenizer.apply_chat_template(
166
+ conversation,
167
+ tools=tools,
168
+ add_generation_prompt=True,
169
+ return_dict=True,
170
+ return_tensors="pt",
171
+ )
172
+
173
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
174
+
175
+ inputs.to(model.device)
176
+ outputs = model.generate(**inputs, max_new_tokens=1000)
177
+ print(tokenizer.decode(outputs[0], skip_special_tokens=True))
178
+ ```
179
+
180
+ 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
181
+ results to the chat history so that the model can use them in its next generation. For a full tool calling example, please
182
+ see the [function calling guide](https://huggingface.co/docs/transformers/main/chat_templating#advanced-tool-use--function-calling),
183
+ and note that Mistral **does** use tool call IDs, so these must be included in your tool calls and tool results. They should be
184
+ exactly 9 alphanumeric characters.
185
+
186
+
187
+ ## Limitations
188
+
189
+ The Mistral 7B Instruct model is a quick demonstration that the base model can be easily fine-tuned to achieve compelling performance.
190
+ It does not have any moderation mechanisms. We're looking forward to engaging with the community on ways to
191
+ make the model finely respect guardrails, allowing for deployment in environments requiring moderated outputs.
192
+
193
+ ## The Mistral AI Team
194
+
195
+ Albert Jiang, Alexandre Sablayrolles, Alexis Tacnet, Antoine Roux, Arthur Mensch, Audrey Herblin-Stoop, Baptiste Bout, Baudouin de Monicault, Blanche Savary, Bam4d, Caroline Feldman, Devendra Singh Chaplot, Diego de las Casas, Eleonore Arcelin, Emma Bou Hanna, Etienne Metzger, Gianna Lengyel, Guillaume Bour, Guillaume Lample, Harizo Rajaona, Jean-Malo Delignon, Jia Li, Justus Murke, Louis Martin, Louis Ternon, Lucile Saulnier, Lélio Renard Lavaud, Margaret Jennings, Marie Pellat, Marie Torelli, Marie-Anne Lachaux, Nicolas Schuhl, Patrick von Platen, Pierre Stock, Sandeep Subramanian, Sophia Yang, Szymon Antoniak, Teven Le Scao, Thibaut Lavril, Timothée Lacroix, Théophile Gervet, Thomas Wang, Valera Nemychnikova, William El Sayed, William Marshall