Safetensors
vllm
mistral
Eval Results
juliendenize Rocketknight1 HF Staff commited on
Commit
8f4e4ef
·
1 Parent(s): 9527884

Add tool calling to the chat template and a response template (#43)

Browse files

- Add tool calling to the chat template and a response template (3e25b23cf0eab6e0a2cd50402dc03502527d06bb)
- Update chat_template.jinja (4b30a971eb431514fd7e3157de6e00e792ee5eec)
- Regenerate response_template with mistral-common's version-driven generator (64e11ec0a798bb282b3da95bca10cc6116bb6334)
- Regenerate chat_template.jinja from mistral-common's generator (03d6d281c09db12c58b9463f11ebe9326e61963a)
- Regenerate chat_template.jinja from mistral-common's generator (41ca083239608cdf7f922963743fa3bbd7b94758)
- Fill in the model name in the default system prompt (e508a44dff9e1f3112678bba24cd92b2a6ff77a8)


Co-authored-by: Matthew Carrigan <Rocketknight1@users.noreply.huggingface.co>

Files changed (2) hide show
  1. chat_template.jinja +247 -0
  2. tokenizer_config.json +36 -2
chat_template.jinja ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {#- Default system message if no system prompt is passed. #}
2
+ {#- Dates for the default system prompt, filled as in the model card's load_system_prompt(). #}
3
+ {%- set today = strftime_now("%Y-%m-%d") %}
4
+ {%- set date_parts = namespace(year=today[:4]|int, month=today[5:7]|int, day=today[8:10]|int) %}
5
+ {%- if date_parts.day > 1 %}
6
+ {%- set date_parts.day = date_parts.day - 1 %}
7
+ {%- else %}
8
+ {%- if date_parts.month == 1 %}
9
+ {%- set date_parts.month = 12 %}
10
+ {%- set date_parts.year = date_parts.year - 1 %}
11
+ {%- else %}
12
+ {%- set date_parts.month = date_parts.month - 1 %}
13
+ {%- endif %}
14
+ {%- if date_parts.month == 2 %}
15
+ {%- set leap = date_parts.year % 4 == 0 and (date_parts.year % 100 != 0 or date_parts.year % 400 == 0) %}
16
+ {%- set date_parts.day = 29 if leap else 28 %}
17
+ {%- else %}
18
+ {%- set date_parts.day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][date_parts.month - 1] %}
19
+ {%- endif %}
20
+ {%- endif %}
21
+ {%- set yesterday = date_parts.year|string + '-' + ('0' if date_parts.month < 10 else '') + date_parts.month|string + '-' + ('0' if date_parts.day < 10 else '') + date_parts.day|string %}
22
+ {%- set default_system_message = 'You are Mistral-Small-24B-Instruct-2501, a Large Language Model (LLM) created by Mistral AI, a French startup headquartered in Paris.
23
+
24
+ Your knowledge base was last updated on 2023-10-01.
25
+ The current date is ' ~ today ~ '.
26
+
27
+ When you\'re not sure about some information, you say that you don\'t have the information and don\'t make up anything.
28
+ If the user\'s question is not clear, ambiguous, or does not provide enough context for you to accurately answer the question, you do not try to answer it right away and you rather ask the user to clarify their request (e.g. "What are some good restaurants around me?" => "Where are you?" or "When is the next flight to Tokyo" => "Where do you travel from?").
29
+ You are always very attentive to dates, in particular you try to resolve dates (e.g. "yesterday" is ' ~ yesterday ~ ') and when asked about information at specific dates, you discard information that is at another date.
30
+ You follow these instructions in all languages, and always respond to the user in the language they use or request.' %}
31
+
32
+ {#- Begin of sequence token. #}
33
+ {{- bos_token }}
34
+
35
+
36
+ {#- Reject an empty conversation, as the tokenizer does. #}
37
+ {%- if messages | length == 0 %}
38
+ {{- raise_exception('Conversation must have at least one message') }}
39
+ {%- endif %}
40
+
41
+ {#- A single message must be a user or system message, as the tokenizer requires. #}
42
+ {%- if messages | length == 1 and messages[0]['role'] not in ['user', 'system', 'developer'] %}
43
+ {{- raise_exception('Conversation must start with a user message or system message') }}
44
+ {%- endif %}
45
+
46
+ {#- Handle system prompt if it exists. #}
47
+ {%- set loop_messages = messages %}
48
+ {%- if messages[0]['role'] not in ['system', 'developer'] and default_system_message != '' %}
49
+ {{- '[SYSTEM_PROMPT]' ~ default_system_message ~ '[/SYSTEM_PROMPT]' }}
50
+ {%- endif %}
51
+
52
+
53
+ {#- Tools definition #}
54
+ {%- set available_tools = '' %}
55
+ {%- set has_tools = false %}
56
+ {%- if tools is defined and tools is not none and tools|length > 0 %}
57
+ {%- set has_tools = true %}
58
+ {%- set ns_t = namespace(tools=[]) %}
59
+ {%- for tool in tools %}
60
+ {%- set tool_function = tool['function'] %}
61
+ {%- set description = tool_function['description'] if 'description' in tool_function else none %}
62
+ {%- set parameters = tool_function['parameters'] if 'parameters' in tool_function else none %}
63
+ {%- set ns_t.tools = ns_t.tools + [{'type': tool['type'] if 'type' in tool else 'function', 'function': {'name': tool_function['name'], 'description': description if description is not none else '', 'parameters': parameters if parameters is not none else {}}}] %}
64
+ {%- endfor %}
65
+ {%- set available_tools = '[AVAILABLE_TOOLS]' ~ (ns_t.tools| tojson) ~ '[/AVAILABLE_TOOLS]' %}
66
+ {%- endif %}
67
+
68
+ {#- Macros #}
69
+ {%- macro render_content(content, context_name) -%}
70
+ {%- if content is string -%}
71
+ {{- content -}}
72
+ {%- elif content -%}
73
+ {%- for block in content -%}
74
+ {%- if block['type'] == 'text' -%}
75
+ {{- block['text'] -}}
76
+ {%- else -%}
77
+ {{- raise_exception('Only text chunks are supported in ' ~ context_name ~ '.') -}}
78
+ {%- endif -%}
79
+ {%- endfor -%}
80
+ {%- else -%}
81
+ {{- raise_exception(context_name ~ ' must have non-empty content.') -}}
82
+ {%- endif -%}
83
+ {%- endmacro -%}
84
+
85
+ {#- Aggregate consecutive messages with the same role except system and tool. #}
86
+ {#- A sentinel message is appended so the last group gets flushed inside the loop. #}
87
+ {%- set ns_agg = namespace(messages=[], current_group=[], current_role=none) %}
88
+ {%- for message in loop_messages + [{'role': '__sentinel__'}] %}
89
+ {#- Read the developer role as system, as the tokenizer does. #}
90
+ {%- set message = {'role': 'system', 'content': message['content']} if message['role'] == 'developer' else message %}
91
+ {%- if message['role'] != ns_agg.current_role or message['role'] == 'system' or message['role'] == 'tool' %}
92
+ {%- if ns_agg.current_role == 'tool' %}
93
+ {%- set ns_agg.messages = ns_agg.messages + ns_agg.current_group %}
94
+ {%- elif ns_agg.current_role is not none %}
95
+ {%- set ns_c = namespace(text_parts=[], chunks=[], has_non_text=false, tool_calls=[]) %}
96
+ {%- for msg in ns_agg.current_group %}
97
+ {%- if msg['content'] is string %}
98
+ {%- set ns_c.text_parts = ns_c.text_parts + ([msg['content']] if msg['content'] else []) %}
99
+ {%- elif msg['content'] is not none %}
100
+ {%- for block in msg['content'] %}
101
+ {%- if block['type'] == 'text' %}
102
+ {%- set ns_c.text_parts = ns_c.text_parts + ([block['text']] if block['text'] else []) %}
103
+ {%- else %}
104
+ {%- if ns_c.text_parts | length > 0 %}
105
+ {%- set ns_c.chunks = ns_c.chunks + [{'type': 'text', 'text': ns_c.text_parts | join('\n\n')}] %}
106
+ {%- set ns_c.text_parts = [] %}
107
+ {%- endif %}
108
+ {%- set ns_c.chunks = ns_c.chunks + [block] %}
109
+ {%- set ns_c.has_non_text = true %}
110
+ {%- endif %}
111
+ {%- endfor %}
112
+ {%- endif %}
113
+ {%- if msg['tool_calls'] is defined and msg['tool_calls'] is not none %}
114
+ {%- set ns_c.tool_calls = ns_c.tool_calls + msg['tool_calls'] | list %}
115
+ {%- endif %}
116
+ {%- endfor %}
117
+ {%- if ns_c.has_non_text %}
118
+ {%- if ns_c.text_parts | length > 0 %}
119
+ {%- set ns_c.chunks = ns_c.chunks + [{'type': 'text', 'text': ns_c.text_parts | join('\n\n')}] %}
120
+ {%- endif %}
121
+ {%- set merged_content = ns_c.chunks %}
122
+ {%- else %}
123
+ {%- set merged_content = ns_c.text_parts | join('\n\n') %}
124
+ {%- endif %}
125
+ {%- if ns_c.tool_calls | length > 0 %}
126
+ {%- set ns_agg.messages = ns_agg.messages + [{'role': ns_agg.current_role, 'content': merged_content, 'tool_calls': ns_c.tool_calls}] %}
127
+ {%- else %}
128
+ {%- set ns_agg.messages = ns_agg.messages + [{'role': ns_agg.current_role, 'content': merged_content}] %}
129
+ {%- endif %}
130
+ {%- endif %}
131
+ {%- if message['role'] != '__sentinel__' %}
132
+ {%- set ns_agg.current_group = [message] %}
133
+ {%- set ns_agg.current_role = message['role'] %}
134
+ {%- endif %}
135
+ {%- else %}
136
+ {%- set ns_agg.current_group = ns_agg.current_group + [message] %}
137
+ {%- endif %}
138
+ {%- endfor %}
139
+ {%- set loop_messages = ns_agg.messages %}
140
+
141
+ {#- Validates message ordering. #}
142
+ {%- set ns = namespace(max_idx_user=-1) %}
143
+ {%- if loop_messages | length > 0 and loop_messages[0]['role'] not in ['user', 'system', 'assistant'] %}
144
+ {{- raise_exception('Conversation must start with a user, system or assistant message, got ' ~ loop_messages[0]['role'] ~ '.') }}
145
+ {%- endif %}
146
+ {%- set ns_order = namespace(previous_role=none) %}
147
+ {%- for message in loop_messages %}
148
+ {%- set current_role = message['role'] %}
149
+ {%- if ns_order.previous_role is not none %}
150
+ {%- if ns_order.previous_role == 'system' %}
151
+ {%- if current_role not in ['user', 'assistant', 'system'] %}
152
+ {{- raise_exception('Unexpected role \'' ~ current_role ~ '\' after role \'' ~ ns_order.previous_role ~ '\'') }}
153
+ {%- endif %}
154
+ {%- elif ns_order.previous_role == 'user' %}
155
+ {%- if current_role not in ['assistant', 'system', 'user'] %}
156
+ {{- raise_exception('Unexpected role \'' ~ current_role ~ '\' after role \'' ~ ns_order.previous_role ~ '\'') }}
157
+ {%- endif %}
158
+ {%- elif ns_order.previous_role == 'assistant' %}
159
+ {%- if current_role not in ['assistant', 'user', 'tool'] %}
160
+ {{- raise_exception('Unexpected role \'' ~ current_role ~ '\' after role \'' ~ ns_order.previous_role ~ '\'') }}
161
+ {%- endif %}
162
+ {%- elif ns_order.previous_role == 'tool' %}
163
+ {%- if current_role not in ['assistant', 'tool', 'user'] %}
164
+ {{- raise_exception('Unexpected role \'' ~ current_role ~ '\' after role \'' ~ ns_order.previous_role ~ '\'') }}
165
+ {%- endif %}
166
+ {%- endif %}
167
+ {%- endif %}
168
+ {%- if message.role == 'user' %}
169
+ {%- set ns.max_idx_user = ns.max_idx_user + 1 %}
170
+ {%- endif %}
171
+ {%- set ns_order.previous_role = current_role %}
172
+ {%- endfor %}
173
+
174
+
175
+ {%- set ns.index = 0 %}
176
+ {#- Handle conversation messages. #}
177
+ {%- for message in loop_messages %}
178
+ {#- User messages supports text content. #}
179
+ {%- if message['role'] == 'user' %}
180
+ {%- if (ns.index == ns.max_idx_user) and has_tools %}
181
+ {{- available_tools }}
182
+ {%- endif %}
183
+ {{- '[INST]' -}}
184
+ {{- render_content(content=message['content'], context_name='user message content') -}}
185
+ {{- '[/INST]' }}
186
+
187
+ {#- Assistant messages supports text content. #}
188
+ {%- elif message['role'] == 'assistant' %}
189
+ {%- generation -%}
190
+ {%- if (message['content'] is none or message['content'] == '' or message['content']|length == 0) and (message['tool_calls'] is not defined or message['tool_calls'] is none or message['tool_calls']|length == 0) %}
191
+ {{- raise_exception('Assistant message must have a string or a list of chunks in content or a list of tool calls.') }}
192
+ {%- endif %}
193
+
194
+ {%- if message['content'] %}
195
+ {{- render_content(content=message['content'], context_name='assistant message contents') -}}
196
+ {%- endif %}
197
+
198
+ {%- if message['tool_calls'] is defined and message['tool_calls'] is not none and message['tool_calls']|length > 0 %}
199
+ {{- '[TOOL_CALLS][' }}
200
+ {%- for tool in message['tool_calls'] %}
201
+ {%- set name = tool['function']['name'] %}
202
+ {%- set arguments = tool['function']['arguments'] %}
203
+ {%- set id = tool['id']%}
204
+ {%- if id is not defined or id|length != 9 %}
205
+ {{- raise_exception('Tool call must have an id of 9 characters or numbers.') }}
206
+ {%- endif %}
207
+ {%- if arguments is not string %}
208
+ {%- set arguments = arguments|tojson|safe %}
209
+ {%- elif arguments == '' %}
210
+ {%- set arguments = '{}' %}
211
+ {%- endif %}
212
+ {{- '{"name": "' ~ name ~ '", "arguments": ' ~ arguments ~ ', "id": "' ~ id ~ '"}' }}
213
+ {%- if loop.length > 1 and loop.index < loop.length %}
214
+ {{- ', ' }}
215
+ {%- endif %}
216
+ {%- endfor %}
217
+ {{- ']' }}
218
+ {%- endif %}
219
+
220
+ {{- eos_token }}
221
+ {%- endgeneration -%}
222
+
223
+ {#- Tool messages only supports text content. #}
224
+ {%- elif message['role'] == 'tool' %}
225
+ {%- if message['call_id'] is not undefined and message['call_id']|length == 9 %}
226
+ {%- set tool_id = message['call_id'] %}
227
+ {%- elif message['tool_call_id'] is not undefined and message['tool_call_id']|length == 9 %}
228
+ {%- set tool_id = message['tool_call_id'] %}
229
+ {%- else %}
230
+ {{- raise_exception('Tool message must have a call_id or tool_call_id of 9 characters or numbers.') }}
231
+ {%- endif %}
232
+ {{- '[TOOL_RESULTS]' ~ tool_id ~ '[TOOL_CONTENT]' ~ message['content']|string ~ '[/TOOL_RESULTS]' }}
233
+
234
+ {#- System messages. #}
235
+ {%- elif message['role'] == 'system' %}
236
+ {{- '[SYSTEM_PROMPT]' -}}
237
+ {{- render_content(content=message['content'], context_name='system message contents') -}}
238
+ {{- '[/SYSTEM_PROMPT]' -}}
239
+
240
+ {#- Raise exception for unsupported roles. #}
241
+ {%- else %}
242
+ {{- raise_exception('Only user, assistant, system and tool roles are supported, got ' ~ message['role'] ~ '.') }}
243
+ {%- endif %}
244
+ {%- if message['role'] == 'user' %}
245
+ {%- set ns.index = ns.index + 1 %}
246
+ {%- endif %}
247
+ {%- endfor %}
tokenizer_config.json CHANGED
@@ -9007,7 +9007,6 @@
9007
  "<SPECIAL_999>"
9008
  ],
9009
  "bos_token": "<s>",
9010
- "chat_template": "{%- set today = strftime_now(\"%Y-%m-%d\") %}\n{%- set default_system_message = \"You are Mistral Small 3, a Large Language Model (LLM) created by Mistral AI, a French startup headquartered in Paris.\\nYour knowledge base was last updated on 2023-10-01. The current date is \" + today + \".\\n\\nWhen you're not sure about some information, you say that you don't have the information and don't make up anything.\\nIf the user's question is not clear, ambiguous, or does not provide enough context for you to accurately answer the question, you do not try to answer it right away and you rather ask the user to clarify their request (e.g. \\\"What are some good restaurants around me?\\\" => \\\"Where are you?\\\" or \\\"When is the next flight to Tokyo\\\" => \\\"Where do you travel from?\\\")\" %}\n\n{{- bos_token }}\n\n{%- if messages[0]['role'] == 'system' %}\n {%- set system_message = messages[0]['content'] %}\n {%- set loop_messages = messages[1:] %}\n{%- else %}\n {%- set system_message = default_system_message %}\n {%- set loop_messages = messages %}\n{%- endif %}\n{{- '[SYSTEM_PROMPT]' + system_message + '[/SYSTEM_PROMPT]' }}\n\n{%- for message in loop_messages %}\n {%- if message['role'] == 'user' %}\n {{- '[INST]' + message['content'] + '[/INST]' }}\n {%- elif message['role'] == 'system' %}\n {{- '[SYSTEM_PROMPT]' + message['content'] + '[/SYSTEM_PROMPT]' }}\n {%- elif message['role'] == 'assistant' %}\n {{- message['content'] + eos_token }}\n {%- else %}\n {{- raise_exception('Only user, system and assistant roles are supported!') }}\n {%- endif %}\n{%- endfor %}",
9011
  "clean_up_tokenization_spaces": false,
9012
  "eos_token": "</s>",
9013
  "extra_special_tokens": {},
@@ -9015,5 +9014,40 @@
9015
  "model_max_length": 32768,
9016
  "tokenizer_class": "LlamaTokenizer",
9017
  "unk_token": "<unk>",
9018
- "use_default_system_prompt": false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9019
  }
 
9007
  "<SPECIAL_999>"
9008
  ],
9009
  "bos_token": "<s>",
 
9010
  "clean_up_tokenization_spaces": false,
9011
  "eos_token": "</s>",
9012
  "extra_special_tokens": {},
 
9014
  "model_max_length": 32768,
9015
  "tokenizer_class": "LlamaTokenizer",
9016
  "unk_token": "<unk>",
9017
+ "use_default_system_prompt": false,
9018
+ "response_template": {
9019
+ "defaults": {
9020
+ "role": "assistant"
9021
+ },
9022
+ "start_anchor": [
9023
+ "[/SYSTEM_PROMPT]",
9024
+ "[/INST]",
9025
+ "[/TOOL_RESULTS]"
9026
+ ],
9027
+ "fields": {
9028
+ "tool_calls": {
9029
+ "open": "[TOOL_CALLS]",
9030
+ "close": "</s>",
9031
+ "content": "json",
9032
+ "transform_each": true,
9033
+ "transform": {
9034
+ "id": "{id}",
9035
+ "type": "function",
9036
+ "function": {
9037
+ "name": "{name}",
9038
+ "arguments": "{arguments}"
9039
+ }
9040
+ }
9041
+ },
9042
+ "content": {
9043
+ "close": [
9044
+ "[TOOL_CALLS]",
9045
+ "</s>"
9046
+ ],
9047
+ "content": "text",
9048
+ "repeats": true,
9049
+ "join": ""
9050
+ }
9051
+ }
9052
+ }
9053
  }