juliendenize commited on
Commit
22b2b86
·
verified ·
1 Parent(s): ed0c856

update to match mistral-common

Browse files
Files changed (1) hide show
  1. chat_template.jinja +67 -67
chat_template.jinja CHANGED
@@ -1,23 +1,14 @@
1
- {#- Default date variables. To improve UX pass the correct ones to the Jinja render. #}
2
- {%- if today is not defined %}
3
- {%- set today = '29-04-2026' %}
4
- {%- endif %}
5
- {%- if yesterday is not defined %}
6
- {%- set yesterday = '28-04-2026' %}
7
- {%- endif %}
8
-
9
  {#- Default system message if no system prompt is passed. #}
10
- {%- set default_system_message -%}
11
- You are Mistral Medium 3.5, a Large Language Model (LLM) created by Mistral AI, a French startup headquartered in Paris.
12
  You are an intelligent conversational assistant powering an AI assistant called Le Chat.
13
  Your knowledge base was last updated on Friday, November 1, 2024.
14
- The current date is {{ today }}.
15
 
16
  # GENERAL GUIDELINES
17
 
18
- - Accurately answer the user's question.
19
- - For uncertain information or when the user's request requires up-to-date or specific data, use the available tools to fetch the information.
20
- - Be very attentive to dates, always try to resolve dates (e.g. "yesterday" is {{ yesterday }}) and when asked about information at specific dates, discard information that is at another date.
21
 
22
  # WEB BROWSING INSTRUCTIONS
23
 
@@ -37,11 +28,10 @@ You may have access to tools that you can use to fetch information or perform ac
37
  2. When the request requires specific data that you do not have in your knowledge base.
38
  3. When the request involves actions that you cannot perform without tools.
39
 
40
- Always prioritize using tools to provide the most accurate and helpful response.
41
- {%- endset %}
42
 
43
  {#- Begin of sequence token. #}
44
- {{- '<s>' }}
45
 
46
 
47
  {#- Handle system prompt if it exists. #}
@@ -66,6 +56,28 @@ Always prioritize using tools to provide the most accurate and helpful response.
66
  {%- endif %}
67
  {%- set model_settings = '[MODEL_SETTINGS]{"reasoning_effort": "' + reasoning_effort + '"}[/MODEL_SETTINGS]' %}
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  {#- Aggregate consecutive messages with the same role except system and tool. #}
70
  {#- A sentinel message is appended so the last group gets flushed inside the loop. #}
71
  {%- set ns_agg = namespace(messages=[], current_group=[], current_role=none) %}
@@ -79,6 +91,13 @@ Always prioritize using tools to provide the most accurate and helpful response.
79
  {#- Convert reasoning / reasoning_content to a leading thinking chunk. #}
80
  {%- set reasoning = msg.get('reasoning_content', msg.get('reasoning', none)) %}
81
  {%- if reasoning is not none and reasoning != '' %}
 
 
 
 
 
 
 
82
  {%- set think_chunk = {'type': 'thinking', 'thinking': reasoning} %}
83
  {%- if msg['content'] is string and msg['content'] != '' %}
84
  {%- set new_content = [think_chunk, {'type': 'text', 'text': msg['content']}] %}
@@ -96,10 +115,15 @@ Always prioritize using tools to provide the most accurate and helpful response.
96
  {%- if msg['content'] is string %}
97
  {%- set ns_c.text_parts = ns_c.text_parts + [msg['content']] %}
98
  {%- elif msg['content'] is not none %}
 
99
  {%- for block in msg['content'] %}
100
  {%- if block['type'] == 'text' %}
101
- {%- set ns_c.text_parts = ns_c.text_parts + [block['text']] %}
102
  {%- else %}
 
 
 
 
103
  {%- if ns_c.text_parts | length > 0 %}
104
  {%- set ns_c.chunks = ns_c.chunks + [{'type': 'text', 'text': ns_c.text_parts | join('\n\n')}] %}
105
  {%- set ns_c.text_parts = [] %}
@@ -108,6 +132,9 @@ Always prioritize using tools to provide the most accurate and helpful response.
108
  {%- set ns_c.has_non_text = true %}
109
  {%- endif %}
110
  {%- endfor %}
 
 
 
111
  {%- endif %}
112
  {%- if msg['tool_calls'] is defined and msg['tool_calls'] is not none %}
113
  {%- set ns_c.tool_calls = ns_c.tool_calls + msg['tool_calls'] | list %}
@@ -139,7 +166,7 @@ Always prioritize using tools to provide the most accurate and helpful response.
139
 
140
  {#- Validates message ordering. #}
141
  {%- set ns = namespace(available_tools_and_settings_emitted=false) %}
142
- {%- if loop_messages | length > 0 and loop_messages[0]['role'] != 'user' and loop_messages[0]['role'] != 'system' %}
143
  {{- raise_exception('Conversation must start with a user or system message, got ' + loop_messages[0]['role'] + '.') }}
144
  {%- endif %}
145
  {%- set ns_order = namespace(previous_role=none) %}
@@ -147,19 +174,19 @@ Always prioritize using tools to provide the most accurate and helpful response.
147
  {%- set current_role = message['role'] %}
148
  {%- if ns_order.previous_role is not none %}
149
  {%- if ns_order.previous_role == 'system' %}
150
- {%- if current_role != 'user' and current_role != 'assistant' and current_role != 'system' %}
151
  {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
152
  {%- endif %}
153
  {%- elif ns_order.previous_role == 'user' %}
154
- {%- if current_role != 'assistant' and current_role != 'system' and current_role != 'user' %}
155
  {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
156
  {%- endif %}
157
  {%- elif ns_order.previous_role == 'assistant' %}
158
- {%- if current_role != 'assistant' and current_role != 'user' and current_role != 'tool' %}
159
  {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
160
  {%- endif %}
161
  {%- elif ns_order.previous_role == 'tool' %}
162
- {%- if current_role != 'assistant' and current_role != 'tool' and current_role != 'user' %}
163
  {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
164
  {%- endif %}
165
  {%- endif %}
@@ -176,28 +203,20 @@ Always prioritize using tools to provide the most accurate and helpful response.
176
  {{- model_settings }}
177
  {%- set ns.available_tools_and_settings_emitted = true %}
178
  {%- endif %}
179
- {%- if message['content'] is string %}
180
- {{- '[INST]' + message['content'] + '[/INST]' }}
181
- {%- elif message['content'] | length > 0 %}
182
- {{- '[INST]' }}
183
- {%- if message['content'] | length == 2 %}
184
- {%- set blocks = message['content'] | sort(attribute='type') %}
185
  {%- else %}
186
  {%- set blocks = message['content'] %}
187
  {%- endif %}
188
- {%- for block in blocks %}
189
- {%- if block['type'] == 'text' %}
190
- {{- block['text'] }}
191
- {%- elif block['type'] in ['image', 'image_url'] %}
192
- {{- '[IMG]' }}
193
- {%- else %}
194
- {{- raise_exception('Only text, image and image_url chunks are supported in user message content.') }}
195
- {%- endif %}
196
- {%- endfor %}
197
- {{- '[/INST]' }}
198
  {%- else %}
199
- {{- raise_exception('User message must have a string or a list of chunks in content') }}
200
  {%- endif %}
 
 
 
201
 
202
  {#- Assistant messages supports text and thinking content. #}
203
  {%- elif message['role'] == 'assistant' %}
@@ -205,19 +224,8 @@ Always prioritize using tools to provide the most accurate and helpful response.
205
  {{- raise_exception('Assistant message must have a string or a list of chunks in content or a list of tool calls.') }}
206
  {%- endif %}
207
 
208
- {%- if message['content'] is string and message['content'] != '' %}
209
- {{- message['content'] }}
210
- {%- elif message['content'] | length > 0 %}
211
- {%- for block in message['content'] %}
212
- {%- if block['type'] == 'text' %}
213
- {{- block['text'] }}
214
- {%- elif block['type'] == 'thinking' %}
215
- {{- '[THINK]' + block['thinking'] }}
216
- {%- if block.get('closed', true) %}{{- '[/THINK]' }}{%- endif %}
217
- {%- else %}
218
- {{- raise_exception('Only text and thinking chunks are supported in assistant message contents.') }}
219
- {%- endif %}
220
- {%- endfor %}
221
  {%- endif %}
222
 
223
  {%- if message['tool_calls'] is defined and message['tool_calls'] is not none and message['tool_calls']|length > 0 %}
@@ -234,26 +242,18 @@ Always prioritize using tools to provide the most accurate and helpful response.
234
  {%- endfor %}
235
  {%- endif %}
236
 
237
- {{- '</s>' }}
238
 
239
- {#- Tool messages only supports text content. #}
240
  {%- elif message['role'] == 'tool' %}
241
- {{- '[TOOL_RESULTS]' + message['content']|string + '[/TOOL_RESULTS]' }}
 
 
242
 
243
  {#- System messages. #}
244
  {%- elif message['role'] == 'system' %}
245
  {{- '[SYSTEM_PROMPT]' -}}
246
- {%- if message['content'] is string %}
247
- {{- message['content'] -}}
248
- {%- else %}
249
- {%- for block in message['content'] %}
250
- {%- if block['type'] == 'text' %}
251
- {{- block['text'] }}
252
- {%- else %}
253
- {{- raise_exception('Only text chunks are supported in system message contents.') }}
254
- {%- endif %}
255
- {%- endfor %}
256
- {%- endif %}
257
  {{- '[/SYSTEM_PROMPT]' -}}
258
 
259
  {#- Raise exception for unsupported roles. #}
 
 
 
 
 
 
 
 
 
1
  {#- Default system message if no system prompt is passed. #}
2
+ {%- set default_system_message = 'You are Mistral Medium 3.5, a Large Language Model (LLM) created by Mistral AI, a French startup headquartered in Paris.
 
3
  You are an intelligent conversational assistant powering an AI assistant called Le Chat.
4
  Your knowledge base was last updated on Friday, November 1, 2024.
5
+ The current date is {today}.
6
 
7
  # GENERAL GUIDELINES
8
 
9
+ - Accurately answer the user\'s question.
10
+ - For uncertain information or when the user\'s request requires up-to-date or specific data, use the available tools to fetch the information.
11
+ - Be very attentive to dates, always try to resolve dates (e.g. "yesterday" is {yesterday}) and when asked about information at specific dates, discard information that is at another date.
12
 
13
  # WEB BROWSING INSTRUCTIONS
14
 
 
28
  2. When the request requires specific data that you do not have in your knowledge base.
29
  3. When the request involves actions that you cannot perform without tools.
30
 
31
+ Always prioritize using tools to provide the most accurate and helpful response.' %}
 
32
 
33
  {#- Begin of sequence token. #}
34
+ {{- bos_token }}
35
 
36
 
37
  {#- Handle system prompt if it exists. #}
 
56
  {%- endif %}
57
  {%- set model_settings = '[MODEL_SETTINGS]{"reasoning_effort": "' + reasoning_effort + '"}[/MODEL_SETTINGS]' %}
58
 
59
+ {#- Macros #}
60
+ {%- macro render_content(content, context_name, supported_types_desc, support_thinking, support_images) -%}
61
+ {%- if content is string -%}
62
+ {{- content -}}
63
+ {%- elif content -%}
64
+ {%- for block in content -%}
65
+ {%- if block['type'] == 'text' -%}
66
+ {{- block['text'] -}}
67
+ {%- elif support_thinking and block['type'] == 'thinking' -%}
68
+ {{- '[THINK]' + block['thinking'] -}}
69
+ {%- if block.get('closed', true) -%}{{- '[/THINK]' -}}{%- endif -%}
70
+ {%- elif support_images and block['type'] in ['image', 'image_url'] -%}
71
+ {{- '[IMG]' -}}
72
+ {%- else -%}
73
+ {{- raise_exception('Only ' + supported_types_desc + ' chunks are supported in ' + context_name + '.') -}}
74
+ {%- endif -%}
75
+ {%- endfor -%}
76
+ {%- else -%}
77
+ {{- raise_exception(context_name + ' must have non-empty content.') -}}
78
+ {%- endif -%}
79
+ {%- endmacro -%}
80
+
81
  {#- Aggregate consecutive messages with the same role except system and tool. #}
82
  {#- A sentinel message is appended so the last group gets flushed inside the loop. #}
83
  {%- set ns_agg = namespace(messages=[], current_group=[], current_role=none) %}
 
91
  {#- Convert reasoning / reasoning_content to a leading thinking chunk. #}
92
  {%- set reasoning = msg.get('reasoning_content', msg.get('reasoning', none)) %}
93
  {%- if reasoning is not none and reasoning != '' %}
94
+ {%- if msg['content'] is not none and msg['content'] is not string %}
95
+ {%- for block in msg['content'] %}
96
+ {%- if block['type'] == 'thinking' %}
97
+ {{- raise_exception('Message cannot have both thinking chunks in content and a top-level `reasoning` or `reasoning_content` field.') }}
98
+ {%- endif %}
99
+ {%- endfor %}
100
+ {%- endif %}
101
  {%- set think_chunk = {'type': 'thinking', 'thinking': reasoning} %}
102
  {%- if msg['content'] is string and msg['content'] != '' %}
103
  {%- set new_content = [think_chunk, {'type': 'text', 'text': msg['content']}] %}
 
115
  {%- if msg['content'] is string %}
116
  {%- set ns_c.text_parts = ns_c.text_parts + [msg['content']] %}
117
  {%- elif msg['content'] is not none %}
118
+ {%- set ns_msg = namespace(msg_text_parts=[]) %}
119
  {%- for block in msg['content'] %}
120
  {%- if block['type'] == 'text' %}
121
+ {%- set ns_msg.msg_text_parts = ns_msg.msg_text_parts + [block['text']] %}
122
  {%- else %}
123
+ {%- if ns_msg.msg_text_parts | length > 0 %}
124
+ {%- set ns_c.text_parts = ns_c.text_parts + [ns_msg.msg_text_parts | join('')] %}
125
+ {%- set ns_msg.msg_text_parts = [] %}
126
+ {%- endif %}
127
  {%- if ns_c.text_parts | length > 0 %}
128
  {%- set ns_c.chunks = ns_c.chunks + [{'type': 'text', 'text': ns_c.text_parts | join('\n\n')}] %}
129
  {%- set ns_c.text_parts = [] %}
 
132
  {%- set ns_c.has_non_text = true %}
133
  {%- endif %}
134
  {%- endfor %}
135
+ {%- if ns_msg.msg_text_parts | length > 0 %}
136
+ {%- set ns_c.text_parts = ns_c.text_parts + [ns_msg.msg_text_parts | join('')] %}
137
+ {%- endif %}
138
  {%- endif %}
139
  {%- if msg['tool_calls'] is defined and msg['tool_calls'] is not none %}
140
  {%- set ns_c.tool_calls = ns_c.tool_calls + msg['tool_calls'] | list %}
 
166
 
167
  {#- Validates message ordering. #}
168
  {%- set ns = namespace(available_tools_and_settings_emitted=false) %}
169
+ {%- if loop_messages | length > 0 and loop_messages[0]['role'] not in ['user', 'system'] %}
170
  {{- raise_exception('Conversation must start with a user or system message, got ' + loop_messages[0]['role'] + '.') }}
171
  {%- endif %}
172
  {%- set ns_order = namespace(previous_role=none) %}
 
174
  {%- set current_role = message['role'] %}
175
  {%- if ns_order.previous_role is not none %}
176
  {%- if ns_order.previous_role == 'system' %}
177
+ {%- if current_role not in ['user', 'assistant', 'system'] %}
178
  {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
179
  {%- endif %}
180
  {%- elif ns_order.previous_role == 'user' %}
181
+ {%- if current_role not in ['assistant', 'system', 'user'] %}
182
  {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
183
  {%- endif %}
184
  {%- elif ns_order.previous_role == 'assistant' %}
185
+ {%- if current_role not in ['assistant', 'user', 'tool'] %}
186
  {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
187
  {%- endif %}
188
  {%- elif ns_order.previous_role == 'tool' %}
189
+ {%- if current_role not in ['assistant', 'tool', 'user'] %}
190
  {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
191
  {%- endif %}
192
  {%- endif %}
 
203
  {{- model_settings }}
204
  {%- set ns.available_tools_and_settings_emitted = true %}
205
  {%- endif %}
206
+ {%- if message['content'] is not string and message['content'] %}
207
+ {#- When content has exactly one image and one text block, put image first. #}
208
+ {%- if message['content'] | length == 2 and message['content'][0]['type'] == 'text' and message['content'][1]['type'] in ['image', 'image_url'] %}
209
+ {%- set blocks = [message['content'][1], message['content'][0]] %}
 
 
210
  {%- else %}
211
  {%- set blocks = message['content'] %}
212
  {%- endif %}
213
+ {%- set user_content = blocks %}
 
 
 
 
 
 
 
 
 
214
  {%- else %}
215
+ {%- set user_content = message['content'] %}
216
  {%- endif %}
217
+ {{- '[INST]' -}}
218
+ {{- render_content(user_content, 'user message content', supported_types_desc='text, image and image_url', support_thinking=false, support_images=true) -}}
219
+ {{- '[/INST]' }}
220
 
221
  {#- Assistant messages supports text and thinking content. #}
222
  {%- elif message['role'] == 'assistant' %}
 
224
  {{- raise_exception('Assistant message must have a string or a list of chunks in content or a list of tool calls.') }}
225
  {%- endif %}
226
 
227
+ {%- if message['content'] %}
228
+ {{- render_content(message['content'], 'assistant message contents', supported_types_desc='text and thinking', support_thinking=true, support_images=false) -}}
 
 
 
 
 
 
 
 
 
 
 
229
  {%- endif %}
230
 
231
  {%- if message['tool_calls'] is defined and message['tool_calls'] is not none and message['tool_calls']|length > 0 %}
 
242
  {%- endfor %}
243
  {%- endif %}
244
 
245
+ {{- eos_token }}
246
 
247
+ {#- Tool messages (multimodal). #}
248
  {%- elif message['role'] == 'tool' %}
249
+ {{- '[TOOL_RESULTS]' -}}
250
+ {{- render_content(message['content'], 'tool message contents', supported_types_desc='text and image', support_images=true) -}}
251
+ {{- '[/TOOL_RESULTS]' }}
252
 
253
  {#- System messages. #}
254
  {%- elif message['role'] == 'system' %}
255
  {{- '[SYSTEM_PROMPT]' -}}
256
+ {{- render_content(message['content'], 'system message contents', supported_types_desc='text', support_thinking=false, support_images=false) -}}
 
 
 
 
 
 
 
 
 
 
257
  {{- '[/SYSTEM_PROMPT]' -}}
258
 
259
  {#- Raise exception for unsupported roles. #}