Commit
•
b0693ea
1
Parent(s):
0de8392
Add tool calling support to chat template (#68)
Browse files- Add tool calling support to chat template (851330943332947922eadda733af9ac92b601351)
- Update README.md (33db818d1f447d9785eb0ad3e504abeb015e365b)
Co-authored-by: Matthew Carrigan <Rocketknight1@users.noreply.huggingface.co>
- README.md +50 -0
- tokenizer_config.json +1 -1
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.
|
tokenizer_config.json
CHANGED
@@ -6173,7 +6173,7 @@
|
|
6173 |
}
|
6174 |
},
|
6175 |
"bos_token": "<s>",
|
6176 |
-
"chat_template": "{%- if messages[0][
|
6177 |
"clean_up_tokenization_spaces": false,
|
6178 |
"eos_token": "</s>",
|
6179 |
"legacy": false,
|
|
|
6173 |
}
|
6174 |
},
|
6175 |
"bos_token": "<s>",
|
6176 |
+
"chat_template": "{%- if messages[0][\"role\"] == \"system\" %}\n {%- set system_message = messages[0][\"content\"] %}\n {%- set loop_messages = messages[1:] %}\n{%- else %}\n {%- set loop_messages = messages %}\n{%- endif %}\n{%- if not tools is defined %}\n {%- set tools = none %}\n{%- endif %}\n{%- set user_messages = loop_messages | selectattr(\"role\", \"equalto\", \"user\") | list %}\n\n{%- for message in loop_messages | rejectattr(\"role\", \"equalto\", \"tool\") | rejectattr(\"role\", \"equalto\", \"tool_results\") | selectattr(\"tool_calls\", \"undefined\") %}\n {%- if (message[\"role\"] == \"user\") != (loop.index0 % 2 == 0) %}\n {{- raise_exception(\"After the optional system message, conversation roles must alternate user/assistant/user/assistant/...\") }}\n {%- endif %}\n{%- endfor %}\n\n{{- bos_token }}\n{%- for message in loop_messages %}\n {%- if message[\"role\"] == \"user\" %}\n {%- if tools is not none and (message == user_messages[-1]) %}\n {{- \"[AVAILABLE_TOOLS] [\" }}\n {%- for tool in tools %}\n {%- set tool = tool.function %}\n {{- '{\"type\": \"function\", \"function\": {' }}\n {%- for key, val in tool.items() if key != \"return\" %}\n {%- if val is string %}\n {{- '\"' + key + '\": \"' + val + '\"' }}\n {%- else %}\n {{- '\"' + key + '\": ' + val|tojson }}\n {%- endif %}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- endif %}\n {%- endfor %}\n {{- \"}}\" }}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- else %}\n {{- \"]\" }}\n {%- endif %}\n {%- endfor %}\n {{- \"[/AVAILABLE_TOOLS]\" }}\n {%- endif %}\n {%- if loop.last and system_message is defined %}\n {{- \"[INST] \" + system_message + \"\\n\\n\" + message[\"content\"] + \"[/INST]\" }}\n {%- else %}\n {{- \"[INST] \" + message[\"content\"] + \"[/INST]\" }}\n {%- endif %}\n {%- elif message[\"role\"] == \"tool_calls\" or message.tool_calls is defined %}\n {%- if message.tool_calls is defined %}\n {%- set tool_calls = message.tool_calls %}\n {%- else %}\n {%- set tool_calls = message.content %}\n {%- endif %}\n {{- \"[TOOL_CALLS] [\" }}\n {%- for tool_call in tool_calls %}\n {%- set out = tool_call.function|tojson %}\n {{- out[:-1] }}\n {%- if not tool_call.id is defined or tool_call.id|length != 9 %}\n {{- raise_exception(\"Tool call IDs should be alphanumeric strings with length 9!\") }}\n {%- endif %}\n {{- ', \"id\": \"' + tool_call.id + '\"}' }}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- else %}\n {{- \"]\" + eos_token }}\n {%- endif %}\n {%- endfor %}\n {%- elif message[\"role\"] == \"assistant\" %}\n {{- \" \" + message[\"content\"] + eos_token}}\n {%- elif message[\"role\"] == \"tool_results\" or message[\"role\"] == \"tool\" %}\n {%- if message.content is defined and message.content.content is defined %}\n {%- set content = message.content.content %}\n {%- else %}\n {%- set content = message.content %}\n {%- endif %}\n {{- '[TOOL_RESULTS] {\"content\": ' + content|string + \", \" }}\n {%- if not message.tool_call_id is defined or message.tool_call_id|length != 9 %}\n {{- raise_exception(\"Tool call IDs should be alphanumeric strings with length 9!\") }}\n {%- endif %}\n {{- '\"call_id\": \"' + message.tool_call_id + '\"}[/TOOL_RESULTS]' }}\n {%- else %}\n {{- raise_exception(\"Only user and assistant roles are supported, with the exception of an initial optional system message!\") }}\n {%- endif %}\n{%- endfor %}\n",
|
6177 |
"clean_up_tokenization_spaces": false,
|
6178 |
"eos_token": "</s>",
|
6179 |
"legacy": false,
|