Rocketknight1 HF Staff commited on
Commit
33db818
·
verified ·
1 Parent(s): 8513309

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +50 -0
README.md CHANGED
@@ -132,6 +132,56 @@ chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3"
132
  chatbot(messages)
133
  ```
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  ## Limitations
136
 
137
  The Mistral 7B Instruct model is a quick demonstration that the base model can be easily fine-tuned to achieve compelling performance.
 
132
  chatbot(messages)
133
  ```
134
 
135
+
136
+ ## Function calling with `transformers`
137
+
138
+ To use this example, you'll need `transformers` version 4.42.0 or higher. Please see the
139
+ [function calling guide](https://huggingface.co/docs/transformers/main/chat_templating#advanced-tool-use--function-calling)
140
+ in the `transformers` docs for more information.
141
+
142
+ ```python
143
+ from transformers import AutoModelForCausalLM, AutoTokenizer
144
+ import torch
145
+
146
+ model_id = "mistralai/Mistral-7B-Instruct-v0.3"
147
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
148
+
149
+ def get_current_weather(location: str, format: str):
150
+ """
151
+ Get the current weather
152
+
153
+ Args:
154
+ location: The city and state, e.g. San Francisco, CA
155
+ format: The temperature unit to use. Infer this from the users location. (choices: ["celsius", "fahrenheit"])
156
+ """
157
+ pass
158
+
159
+ conversation = [{"role": "user", "content": "What's the weather like in Paris?"}]
160
+ tools = [get_current_weather]
161
+
162
+ # render the tool use prompt as a string:
163
+ tool_use_prompt = tokenizer.apply_chat_template(
164
+ conversation,
165
+ tools=tools,
166
+ tokenize=False,
167
+ add_generation_prompt=True,
168
+ )
169
+
170
+ inputs = tokenizer(tool_use_prompt, return_tensors="pt")
171
+
172
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
173
+
174
+ outputs = model.generate(**inputs, max_new_tokens=1000)
175
+ print(tokenizer.decode(outputs[0], skip_special_tokens=True))
176
+ ```
177
+
178
+ 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
179
+ results to the chat history so that the model can use them in its next generation. For a full tool calling example, please
180
+ see the [function calling guide](https://huggingface.co/docs/transformers/main/chat_templating#advanced-tool-use--function-calling),
181
+ and note that Mistral **does** use tool call IDs, so these must be included in your tool calls and tool results. They should be
182
+ exactly 9 alphanumeric characters.
183
+
184
+
185
  ## Limitations
186
 
187
  The Mistral 7B Instruct model is a quick demonstration that the base model can be easily fine-tuned to achieve compelling performance.