juliendenize commited on
Commit
a11f36b
·
verified ·
1 Parent(s): 19037a2

update to match mistral-common

Browse files
Files changed (1) hide show
  1. chat_template.jinja +204 -75
chat_template.jinja CHANGED
@@ -1,110 +1,231 @@
1
  {#- Default system message if no system prompt is passed. #}
2
- {%- set default_system_message = '' %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  {#- Begin of sequence token. #}
5
- {{- '<s>' }}
 
6
 
7
  {#- Handle system prompt if it exists. #}
8
- {#- System prompt supports text content or text chunks. #}
9
- {%- if messages[0]['role'] == 'system' %}
10
- {{- '[SYSTEM_PROMPT]' -}}
11
- {%- if messages[0]['content'] is string %}
12
- {{- messages[0]['content'] -}}
13
- {%- else %}
14
- {%- for block in messages[0]['content'] %}
15
- {%- if block['type'] == 'text' %}
16
- {{- block['text'] }}
17
- {%- else %}
18
- {{- raise_exception('Only text chunks are supported in system message contents.') }}
19
- {%- endif %}
20
- {%- endfor %}
21
- {%- endif %}
22
- {{- '[/SYSTEM_PROMPT]' -}}
23
- {%- set loop_messages = messages[1:] %}
24
- {%- else %}
25
- {%- set loop_messages = messages %}
26
- {%- if default_system_message != '' %}
27
- {{- '[SYSTEM_PROMPT]' + default_system_message + '[/SYSTEM_PROMPT]' }}
28
- {%- endif %}
29
  {%- endif %}
30
 
31
 
32
- {#- Tools definition #}
33
- {%- set tools_definition = '' %}
34
  {%- set has_tools = false %}
35
  {%- if tools is defined and tools is not none and tools|length > 0 %}
36
  {%- set has_tools = true %}
37
- {%- set tools_definition = '[AVAILABLE_TOOLS]' + (tools| tojson) + '[/AVAILABLE_TOOLS]' %}
38
- {{- tools_definition }}
 
 
39
  {%- endif %}
40
-
41
- {#- Model settings definition #}
42
- {%- set reasoning_effort = reasoning_effort if reasoning_effort is defined and reasoning_effort is not none else 'none' %}
43
  {%- if reasoning_effort not in ['none', 'high'] %}
44
  {{- raise_exception('reasoning_effort must be either "none" or "high"') }}
45
  {%- endif %}
46
  {%- set model_settings = '[MODEL_SETTINGS]{"reasoning_effort": "' + reasoning_effort + '"}[/MODEL_SETTINGS]' %}
47
- {{- model_settings }}
48
 
49
- {#- Checks for alternating user/assistant messages. #}
50
- {%- set ns = namespace(index=0) %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  {%- for message in loop_messages %}
52
- {%- if message.role == 'user' or (message.role == 'assistant' and (message.tool_calls is not defined or message.tool_calls is none or message.tool_calls | length == 0)) %}
53
- {%- if (message['role'] == 'user') != (ns.index % 2 == 0) %}
54
- {{- raise_exception('After the optional system message, conversation roles must alternate user and assistant roles except for tool calls and results.') }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  {%- endif %}
56
- {%- set ns.index = ns.index + 1 %}
57
  {%- endif %}
 
58
  {%- endfor %}
59
 
60
  {#- Handle conversation messages. #}
61
  {%- for message in loop_messages %}
62
-
63
- {#- User messages supports text content or text and image chunks. #}
64
  {%- if message['role'] == 'user' %}
65
- {%- if message['content'] is string %}
66
- {{- '[INST]' + message['content'] + '[/INST]' }}
67
- {%- elif message['content'] | length > 0 %}
68
- {{- '[INST]' }}
69
- {%- if message['content'] | length == 2 %}
70
- {%- set blocks = message['content'] | sort(attribute='type') %}
 
 
 
71
  {%- else %}
72
  {%- set blocks = message['content'] %}
73
  {%- endif %}
74
- {%- for block in blocks %}
75
- {%- if block['type'] == 'text' %}
76
- {{- block['text'] }}
77
- {%- elif block['type'] in ['image', 'image_url'] %}
78
- {{- '[IMG]' }}
79
- {%- else %}
80
- {{- raise_exception('Only text, image and image_url chunks are supported in user message content.') }}
81
- {%- endif %}
82
- {%- endfor %}
83
- {{- '[/INST]' }}
84
  {%- else %}
85
- {{- raise_exception('User message must have a string or a list of chunks in content') }}
86
  {%- endif %}
 
 
 
87
 
88
- {#- Assistant messages supports text content or text, image and thinking chunks. #}
89
  {%- elif message['role'] == 'assistant' %}
90
  {%- 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) %}
91
  {{- raise_exception('Assistant message must have a string or a list of chunks in content or a list of tool calls.') }}
92
  {%- endif %}
93
 
94
- {%- if message['content'] is string and message['content'] != '' %}
95
- {{- message['content'] }}
96
- {%- elif message['content'] | length > 0 %}
97
- {%- for block in message['content'] %}
98
- {%- if block['type'] == 'text' %}
99
- {{- block['text'] }}
100
- {%- elif block['type'] == 'thinking' %}
101
- {{- '[THINK]' + block['thinking'] + '[/THINK]' }}
102
- {%- else %}
103
- {{- raise_exception('Only text and thinking chunks are supported in assistant message contents.') }}
104
- {%- endif %}
105
- {%- endfor %}
106
  {%- endif %}
107
-
108
  {%- if message['tool_calls'] is defined and message['tool_calls'] is not none and message['tool_calls']|length > 0 %}
109
  {%- for tool in message['tool_calls'] %}
110
  {{- '[TOOL_CALLS]' }}
@@ -119,14 +240,22 @@
119
  {%- endfor %}
120
  {%- endif %}
121
 
122
- {{- '</s>' }}
123
 
124
- {#- Tool messages only supports text content. #}
125
  {%- elif message['role'] == 'tool' %}
126
- {{- '[TOOL_RESULTS]' + message['content']|string + '[/TOOL_RESULTS]' }}
 
 
 
 
 
 
 
 
127
 
128
  {#- Raise exception for unsupported roles. #}
129
  {%- else %}
130
- {{- raise_exception('Only user, assistant and tool roles are supported, got ' + message['role'] + '.') }}
131
  {%- endif %}
132
- {%- endfor %}
 
1
  {#- Default system message if no system prompt is passed. #}
2
+ {%- set default_system_message = 'You are Mistral-Small-4-119B-2603, a Large Language Model (LLM) created by Mistral AI, a French startup headquartered in Paris.
3
+ You power 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
+ When you\'re not sure about some information or when the user\'s request requires up-to-date or specific data, you must use the available tools to fetch the information. Do not hesitate to use tools whenever they can provide a more accurate or complete response. If no relevant tools are available, then clearly state that you don\'t have the information and avoid making up anything.
8
+ 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?").
9
+ 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.
10
+ You follow these instructions in all languages, and always respond to the user in the language they use or request.
11
+ Next sections describe the capabilities that you have.
12
+
13
+ # WEB BROWSING INSTRUCTIONS
14
+
15
+ You cannot perform any web search or access internet to open URLs, links etc. If it seems like the user is expecting you to do so, you clarify the situation and ask the user to copy paste the text directly in the chat.
16
+
17
+ # MULTI-MODAL INSTRUCTIONS
18
+
19
+ You have the ability to read images, but you cannot generate images. You also cannot read nor transcribe audio files or videos.
20
+
21
+ # TOOL CALLING INSTRUCTIONS
22
+
23
+ You may have access to tools that you can use to fetch information or perform actions. You must use these tools in the following situations:
24
+
25
+ 1. When the request requires up-to-date information.
26
+ 2. When the request requires specific data that you do not have in your knowledge base.
27
+ 3. When the request involves actions that you cannot perform without tools.
28
+
29
+ Always prioritize using tools to provide the most accurate and helpful response. If tools are not available, inform the user that you cannot perform the requested action at the moment.' %}
30
 
31
  {#- Begin of sequence token. #}
32
+ {{- bos_token }}
33
+
34
 
35
  {#- Handle system prompt if it exists. #}
36
+ {%- set loop_messages = messages %}
37
+ {%- if messages[0]['role'] != 'system' and default_system_message != '' %}
38
+ {{- '[SYSTEM_PROMPT]' + default_system_message + '[/SYSTEM_PROMPT]' }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  {%- endif %}
40
 
41
 
42
+ {#- Tools and model settings definition #}
43
+ {%- set available_tools = '' %}
44
  {%- set has_tools = false %}
45
  {%- if tools is defined and tools is not none and tools|length > 0 %}
46
  {%- set has_tools = true %}
47
+ {%- set available_tools = '[AVAILABLE_TOOLS]' + (tools| tojson) + '[/AVAILABLE_TOOLS]' %}
48
+ {%- endif %}
49
+ {%- if reasoning_effort is not defined or reasoning_effort is none %}
50
+ {%- set reasoning_effort = 'none' %}
51
  {%- endif %}
 
 
 
52
  {%- if reasoning_effort not in ['none', 'high'] %}
53
  {{- raise_exception('reasoning_effort must be either "none" or "high"') }}
54
  {%- endif %}
55
  {%- set model_settings = '[MODEL_SETTINGS]{"reasoning_effort": "' + reasoning_effort + '"}[/MODEL_SETTINGS]' %}
 
56
 
57
+ {#- Macros #}
58
+ {%- macro render_content(content, context_name, supported_types_desc, support_thinking, support_images) -%}
59
+ {%- if content is string -%}
60
+ {{- content -}}
61
+ {%- elif content -%}
62
+ {%- for block in content -%}
63
+ {%- if block['type'] == 'text' -%}
64
+ {{- block['text'] -}}
65
+ {%- elif support_thinking and block['type'] == 'thinking' -%}
66
+ {{- '[THINK]' + block['thinking'] -}}
67
+ {%- if block.get('closed', true) -%}{{- '[/THINK]' -}}{%- endif -%}
68
+ {%- elif support_images and block['type'] in ['image', 'image_url'] -%}
69
+ {{- '[IMG]' -}}
70
+ {%- else -%}
71
+ {{- raise_exception('Only ' + supported_types_desc + ' chunks are supported in ' + context_name + '.') -}}
72
+ {%- endif -%}
73
+ {%- endfor -%}
74
+ {%- else -%}
75
+ {{- raise_exception(context_name + ' must have non-empty content.') -}}
76
+ {%- endif -%}
77
+ {%- endmacro -%}
78
+
79
+ {#- Aggregate consecutive messages with the same role except system and tool. #}
80
+ {#- A sentinel message is appended so the last group gets flushed inside the loop. #}
81
+ {%- set ns_agg = namespace(messages=[], current_group=[], current_role=none) %}
82
+ {%- for message in loop_messages + [{'role': '__sentinel__'}] %}
83
+ {%- if message['role'] != ns_agg.current_role or message['role'] == 'system' or message['role'] == 'tool' %}
84
+ {%- if ns_agg.current_role == 'tool' %}
85
+ {%- set ns_agg.messages = ns_agg.messages + ns_agg.current_group %}
86
+ {%- elif ns_agg.current_role is not none %}
87
+ {%- set ns_c = namespace(text_parts=[], chunks=[], has_non_text=false, tool_calls=[]) %}
88
+ {%- for msg in ns_agg.current_group %}
89
+ {#- Convert reasoning / reasoning_content to a leading thinking chunk. #}
90
+ {%- set reasoning = msg.get('reasoning_content', msg.get('reasoning', none)) %}
91
+ {%- if reasoning is not none and reasoning != '' %}
92
+ {%- if msg['content'] is not none and msg['content'] is not string %}
93
+ {%- for block in msg['content'] %}
94
+ {%- if block['type'] == 'thinking' %}
95
+ {{- raise_exception('Message cannot have both thinking chunks in content and a top-level `reasoning` or `reasoning_content` field.') }}
96
+ {%- endif %}
97
+ {%- endfor %}
98
+ {%- endif %}
99
+ {%- set think_chunk = {'type': 'thinking', 'thinking': reasoning} %}
100
+ {%- if msg['content'] is string and msg['content'] != '' %}
101
+ {%- set new_content = [think_chunk, {'type': 'text', 'text': msg['content']}] %}
102
+ {%- elif msg['content'] is not none and msg['content'] is not string and msg['content'] | length > 0 %}
103
+ {%- set new_content = [think_chunk] + msg['content'] | list %}
104
+ {%- else %}
105
+ {%- set new_content = [think_chunk] %}
106
+ {%- endif %}
107
+ {%- if msg['tool_calls'] is defined and msg['tool_calls'] is not none %}
108
+ {%- set msg = {'role': msg['role'], 'content': new_content, 'tool_calls': msg['tool_calls']} %}
109
+ {%- else %}
110
+ {%- set msg = {'role': msg['role'], 'content': new_content} %}
111
+ {%- endif %}
112
+ {%- endif %}
113
+ {%- if msg['content'] is string %}
114
+ {%- set ns_c.text_parts = ns_c.text_parts + [msg['content']] %}
115
+ {%- elif msg['content'] is not none %}
116
+ {%- set ns_msg = namespace(msg_text_parts=[]) %}
117
+ {%- for block in msg['content'] %}
118
+ {%- if block['type'] == 'text' %}
119
+ {%- set ns_msg.msg_text_parts = ns_msg.msg_text_parts + [block['text']] %}
120
+ {%- else %}
121
+ {%- if ns_msg.msg_text_parts | length > 0 %}
122
+ {%- set ns_c.text_parts = ns_c.text_parts + [ns_msg.msg_text_parts | join('')] %}
123
+ {%- set ns_msg.msg_text_parts = [] %}
124
+ {%- endif %}
125
+ {%- if ns_c.text_parts | length > 0 %}
126
+ {%- set ns_c.chunks = ns_c.chunks + [{'type': 'text', 'text': ns_c.text_parts | join('\n\n')}] %}
127
+ {%- set ns_c.text_parts = [] %}
128
+ {%- endif %}
129
+ {%- set ns_c.chunks = ns_c.chunks + [block] %}
130
+ {%- set ns_c.has_non_text = true %}
131
+ {%- endif %}
132
+ {%- endfor %}
133
+ {%- if ns_msg.msg_text_parts | length > 0 %}
134
+ {%- set ns_c.text_parts = ns_c.text_parts + [ns_msg.msg_text_parts | join('')] %}
135
+ {%- endif %}
136
+ {%- endif %}
137
+ {%- if msg['tool_calls'] is defined and msg['tool_calls'] is not none %}
138
+ {%- set ns_c.tool_calls = ns_c.tool_calls + msg['tool_calls'] | list %}
139
+ {%- endif %}
140
+ {%- endfor %}
141
+ {%- if ns_c.has_non_text %}
142
+ {%- if ns_c.text_parts | length > 0 %}
143
+ {%- set ns_c.chunks = ns_c.chunks + [{'type': 'text', 'text': ns_c.text_parts | join('\n\n')}] %}
144
+ {%- endif %}
145
+ {%- set merged_content = ns_c.chunks %}
146
+ {%- else %}
147
+ {%- set merged_content = ns_c.text_parts | join('\n\n') %}
148
+ {%- endif %}
149
+ {%- if ns_c.tool_calls | length > 0 %}
150
+ {%- set ns_agg.messages = ns_agg.messages + [{'role': ns_agg.current_role, 'content': merged_content, 'tool_calls': ns_c.tool_calls}] %}
151
+ {%- else %}
152
+ {%- set ns_agg.messages = ns_agg.messages + [{'role': ns_agg.current_role, 'content': merged_content}] %}
153
+ {%- endif %}
154
+ {%- endif %}
155
+ {%- if message['role'] != '__sentinel__' %}
156
+ {%- set ns_agg.current_group = [message] %}
157
+ {%- set ns_agg.current_role = message['role'] %}
158
+ {%- endif %}
159
+ {%- else %}
160
+ {%- set ns_agg.current_group = ns_agg.current_group + [message] %}
161
+ {%- endif %}
162
+ {%- endfor %}
163
+ {%- set loop_messages = ns_agg.messages %}
164
+
165
+ {#- Validates message ordering. #}
166
+ {%- set ns = namespace(available_tools_and_settings_emitted=false) %}
167
+ {%- if loop_messages | length > 0 and loop_messages[0]['role'] not in ['user', 'system'] %}
168
+ {{- raise_exception('Conversation must start with a user or system message, got ' + loop_messages[0]['role'] + '.') }}
169
+ {%- endif %}
170
+ {%- set ns_order = namespace(previous_role=none) %}
171
  {%- for message in loop_messages %}
172
+ {%- set current_role = message['role'] %}
173
+ {%- if ns_order.previous_role is not none %}
174
+ {%- if ns_order.previous_role == 'system' %}
175
+ {%- if current_role not in ['user', 'assistant', 'system'] %}
176
+ {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
177
+ {%- endif %}
178
+ {%- elif ns_order.previous_role == 'user' %}
179
+ {%- if current_role not in ['assistant', 'system', 'user'] %}
180
+ {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
181
+ {%- endif %}
182
+ {%- elif ns_order.previous_role == 'assistant' %}
183
+ {%- if current_role not in ['assistant', 'user', 'tool'] %}
184
+ {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
185
+ {%- endif %}
186
+ {%- elif ns_order.previous_role == 'tool' %}
187
+ {%- if current_role not in ['assistant', 'tool', 'user'] %}
188
+ {{- raise_exception('Unexpected role \'' + current_role + '\' after role \'' + ns_order.previous_role + '\'') }}
189
+ {%- endif %}
190
  {%- endif %}
 
191
  {%- endif %}
192
+ {%- set ns_order.previous_role = current_role %}
193
  {%- endfor %}
194
 
195
  {#- Handle conversation messages. #}
196
  {%- for message in loop_messages %}
197
+ {#- User messages supports text, image and image_url content. #}
 
198
  {%- if message['role'] == 'user' %}
199
+ {%- if not ns.available_tools_and_settings_emitted %}
200
+ {{- available_tools }}
201
+ {{- model_settings }}
202
+ {%- set ns.available_tools_and_settings_emitted = true %}
203
+ {%- endif %}
204
+ {%- if message['content'] is not string and message['content'] %}
205
+ {#- When content has exactly one image and one text block, put image first. #}
206
+ {%- if message['content'] | length == 2 and message['content'][0]['type'] == 'text' and message['content'][1]['type'] in ['image', 'image_url'] %}
207
+ {%- set blocks = [message['content'][1], message['content'][0]] %}
208
  {%- else %}
209
  {%- set blocks = message['content'] %}
210
  {%- endif %}
211
+ {%- set user_content = blocks %}
 
 
 
 
 
 
 
 
 
212
  {%- else %}
213
+ {%- set user_content = message['content'] %}
214
  {%- endif %}
215
+ {{- '[INST]' -}}
216
+ {{- render_content(user_content, 'user message content', supported_types_desc='text, image and image_url', support_thinking=false, support_images=true) -}}
217
+ {{- '[/INST]' }}
218
 
219
+ {#- Assistant messages supports text and thinking content. #}
220
  {%- elif message['role'] == 'assistant' %}
221
  {%- 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) %}
222
  {{- raise_exception('Assistant message must have a string or a list of chunks in content or a list of tool calls.') }}
223
  {%- endif %}
224
 
225
+ {%- if message['content'] %}
226
+ {{- render_content(message['content'], 'assistant message contents', supported_types_desc='text and thinking', support_thinking=true, support_images=false) -}}
 
 
 
 
 
 
 
 
 
 
227
  {%- endif %}
228
+
229
  {%- if message['tool_calls'] is defined and message['tool_calls'] is not none and message['tool_calls']|length > 0 %}
230
  {%- for tool in message['tool_calls'] %}
231
  {{- '[TOOL_CALLS]' }}
 
240
  {%- endfor %}
241
  {%- endif %}
242
 
243
+ {{- eos_token }}
244
 
245
+ {#- Tool messages (multimodal). #}
246
  {%- elif message['role'] == 'tool' %}
247
+ {{- '[TOOL_RESULTS]' -}}
248
+ {{- render_content(message['content'], 'tool message contents', supported_types_desc='text and image', support_images=true) -}}
249
+ {{- '[/TOOL_RESULTS]' }}
250
+
251
+ {#- System messages. #}
252
+ {%- elif message['role'] == 'system' %}
253
+ {{- '[SYSTEM_PROMPT]' -}}
254
+ {{- render_content(message['content'], 'system message contents', supported_types_desc='text', support_thinking=false, support_images=false) -}}
255
+ {{- '[/SYSTEM_PROMPT]' -}}
256
 
257
  {#- Raise exception for unsupported roles. #}
258
  {%- else %}
259
+ {{- raise_exception('Only user, assistant, system and tool roles are supported, got ' + message['role'] + '.') }}
260
  {%- endif %}
261
+ {%- endfor %}