DFveloper commited on
Commit
02da1e4
·
verified ·
1 Parent(s): f372e80

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +264 -50
chat_template.jinja CHANGED
@@ -1,4 +1,144 @@
1
- {{ bos_token }}{%- macro strip_thinking(text) -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  {%- set ns = namespace(result='') -%}
3
  {%- for part in text.split('<channel|>') -%}
4
  {%- if '<|channel>' in part -%}
@@ -9,66 +149,140 @@
9
  {%- endfor -%}
10
  {{- ns.result | trim -}}
11
  {%- endmacro -%}
12
- {%- set thinking = enable_thinking is defined and enable_thinking -%}
 
13
  {%- set loop_messages = messages -%}
14
- {%- if messages[0]['role'] in ['system', 'developer'] or thinking -%}
15
- {{ '<|turn>system
16
- ' }}
17
- {%- if thinking -%}
18
- {{ '<|think|>
19
- ' }}
 
 
 
20
  {%- endif -%}
 
21
  {%- if messages[0]['role'] in ['system', 'developer'] -%}
22
- {{ messages[0]['content'] | trim }}
23
  {%- set loop_messages = messages[1:] -%}
24
  {%- endif -%}
25
- {{ '<turn|>
26
- ' }}
27
- {%- endif -%}
28
- {%- for message in loop_messages -%}
29
- {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
30
- {{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
 
 
31
  {%- endif -%}
32
- {%- if (message['role'] == 'assistant') -%}
33
- {%- set role = "model" -%}
34
- {%- else -%}
35
- {%- set role = message['role'] -%}
 
 
 
 
36
  {%- endif -%}
37
- {{ '<|turn>' + role + '
38
- ' }}
39
- {%- if message['content'] is string -%}
40
- {%- if role == "model" -%}
41
- {{ strip_thinking(message['content']) }}
42
- {%- else -%}
43
- {{ message['content'] | trim }}
44
  {%- endif -%}
45
- {%- elif message['content'] is iterable -%}
46
- {%- for item in message['content'] -%}
47
- {%- if item['type'] == 'audio' -%}
48
- {{ '<|audio|>' }}
49
- {%- elif item['type'] == 'image' -%}
50
- {{ '<|image|>' }}
51
- {%- elif item['type'] == 'video' -%}
52
- {{ '<|video|>' }}
53
- {%- elif item['type'] == 'text' -%}
54
- {%- if role == "model" -%}
55
- {{ strip_thinking(item['text']) }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  {%- else -%}
57
- {{ item['text'] | trim }}
58
  {%- endif -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  {%- endif -%}
60
- {%- endfor -%}
61
- {%- else -%}
62
- {{ raise_exception("Invalid content type") }}
63
- {%- endif -%}
64
- {{ '<turn|>
65
- ' }}
66
  {%- endfor -%}
 
67
  {%- if add_generation_prompt -%}
68
- {{'<|turn>model
69
- '}}
70
- {%- if not thinking -%}
71
- {{ '<|channel>thought
72
- <channel|>' }}
 
 
 
 
 
 
73
  {%- endif -%}
74
- {%- endif -%}
 
1
+ {%- macro format_parameters(properties, required) -%}
2
+ {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
3
+ {%- set ns = namespace(found_first=false) -%}
4
+ {%- for key, value in properties | dictsort -%}
5
+ {%- set add_comma = false -%}
6
+ {%- if key not in standard_keys -%}
7
+ {%- if ns.found_first %},{% endif -%}
8
+ {%- set ns.found_first = true -%}
9
+ {{ key }}:{
10
+ {%- if value['description'] -%}
11
+ description:<|"|>{{ value['description'] }}<|"|>
12
+ {%- set add_comma = true -%}
13
+ {%- endif -%}
14
+ {%- if value['nullable'] %}
15
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
16
+ nullable:true
17
+ {%- endif -%}
18
+ {%- if value['type'] | upper == 'STRING' -%}
19
+ {%- if value['enum'] -%}
20
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
21
+ enum:{{ format_argument(value['enum']) }}
22
+ {%- endif -%}
23
+ {%- elif value['type'] | upper == 'OBJECT' -%}
24
+ ,properties:{
25
+ {%- if value['properties'] is defined and value['properties'] is mapping -%}
26
+ {{- format_parameters(value['properties'], value['required'] | default([])) -}}
27
+ {%- elif value is mapping -%}
28
+ {{- format_parameters(value, value['required'] | default([])) -}}
29
+ {%- endif -%}
30
+ }
31
+ {%- if value['required'] -%}
32
+ ,required:[
33
+ {%- for item in value['required'] | default([]) -%}
34
+ <|"|>{{- item -}}<|"|>
35
+ {%- if not loop.last %},{% endif -%}
36
+ {%- endfor -%}
37
+ ]
38
+ {%- endif -%}
39
+ {%- elif value['type'] | upper == 'ARRAY' -%}
40
+ {%- if value['items'] is mapping and value['items'] -%}
41
+ ,items:{
42
+ {%- set ns_items = namespace(found_first=false) -%}
43
+ {%- for item_key, item_value in value['items'] | dictsort -%}
44
+ {%- if item_value is not none -%}
45
+ {%- if ns_items.found_first %},{% endif -%}
46
+ {%- set ns_items.found_first = true -%}
47
+ {%- if item_key == 'properties' -%}
48
+ properties:{
49
+ {%- if item_value is mapping -%}
50
+ {{- format_parameters(item_value, value['items']['required'] | default([])) -}}
51
+ {%- endif -%}
52
+ }
53
+ {%- elif item_key == 'required' -%}
54
+ required:[
55
+ {%- for req_item in item_value -%}
56
+ <|"|>{{- req_item -}}<|"|>
57
+ {%- if not loop.last %},{% endif -%}
58
+ {%- endfor -%}
59
+ ]
60
+ {%- elif item_key == 'type' -%}
61
+ {%- if item_value is string -%}
62
+ type:{{ format_argument(item_value | upper) }}
63
+ {%- else -%}
64
+ type:{{ format_argument(item_value | map('upper') | list) }}
65
+ {%- endif -%}
66
+ {%- else -%}
67
+ {{ item_key }}:{{ format_argument(item_value) }}
68
+ {%- endif -%}
69
+ {%- endif -%}
70
+ {%- endfor -%}
71
+ }
72
+ {%- endif -%}
73
+ {%- endif -%}
74
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
75
+ type:<|"|>{{ value['type'] | upper }}<|"|>}
76
+ {%- endif -%}
77
+ {%- endfor -%}
78
+ {%- endmacro -%}
79
+ {%- macro format_function_declaration(tool_data) -%}
80
+ declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
81
+ {%- set params = tool_data['function']['parameters'] -%}
82
+ {%- if params -%}
83
+ ,parameters:{
84
+ {%- if params['properties'] -%}
85
+ properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
86
+ {%- endif -%}
87
+ {%- if params['required'] -%}
88
+ required:[
89
+ {%- for item in params['required'] -%}
90
+ <|"|>{{- item -}}<|"|>
91
+ {{- ',' if not loop.last -}}
92
+ {%- endfor -%}
93
+ ],
94
+ {%- endif -%}
95
+ {%- if params['type'] -%}
96
+ type:<|"|>{{- params['type'] | upper -}}<|"|>}
97
+ {%- endif -%}
98
+ {%- endif -%}
99
+ {%- if 'response' in tool_data['function'] -%}
100
+ {%- set response_declaration = tool_data['function']['response'] -%}
101
+ ,response:{
102
+ {%- if response_declaration['description'] -%}
103
+ description:<|"|>{{- response_declaration['description'] -}}<|"|>,
104
+ {%- endif -%}
105
+ {%- if response_declaration['type'] | upper == 'OBJECT' -%}
106
+ type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
107
+ {%- endif -%}
108
+ {%- endif -%}
109
+ }
110
+ {%- endmacro -%}
111
+ {%- macro format_argument(argument, escape_keys=True) -%}
112
+ {%- if argument is string -%}
113
+ {{- '<|"|>' + argument + '<|"|>' -}}
114
+ {%- elif argument is boolean -%}
115
+ {{- 'true' if argument else 'false' -}}
116
+ {%- elif argument is mapping -%}
117
+ {{- '{' -}}
118
+ {%- set ns = namespace(found_first=false) -%}
119
+ {%- for key, value in argument | dictsort -%}
120
+ {%- if ns.found_first %},{% endif -%}
121
+ {%- set ns.found_first = true -%}
122
+ {%- if escape_keys -%}
123
+ {{- '<|"|>' + key + '<|"|>' -}}
124
+ {%- else -%}
125
+ {{- key -}}
126
+ {%- endif -%}
127
+ :{{- format_argument(value, escape_keys=escape_keys) -}}
128
+ {%- endfor -%}
129
+ {{- '}' -}}
130
+ {%- elif argument is sequence -%}
131
+ {{- '[' -}}
132
+ {%- for item in argument -%}
133
+ {{- format_argument(item, escape_keys=escape_keys) -}}
134
+ {%- if not loop.last %},{% endif -%}
135
+ {%- endfor -%}
136
+ {{- ']' -}}
137
+ {%- else -%}
138
+ {{- argument -}}
139
+ {%- endif -%}
140
+ {%- endmacro -%}
141
+ {%- macro strip_thinking(text) -%}
142
  {%- set ns = namespace(result='') -%}
143
  {%- for part in text.split('<channel|>') -%}
144
  {%- if '<|channel>' in part -%}
 
149
  {%- endfor -%}
150
  {{- ns.result | trim -}}
151
  {%- endmacro -%}
152
+
153
+ {%- set ns = namespace(prev_message_type=None, last_user_message=-1) -%}
154
  {%- set loop_messages = messages -%}
155
+ {{- bos_token -}}
156
+ {#- Handle System/Tool Definitions Block -#}
157
+ {%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
158
+ {{- '<|turn>system\n' -}}
159
+
160
+ {#- Inject Thinking token at the very top of the FIRST system turn -#}
161
+ {%- if enable_thinking is defined and enable_thinking -%}
162
+ {{- '<|think|>\n' -}}
163
+ {%- set ns.prev_message_type = 'think' -%}
164
  {%- endif -%}
165
+
166
  {%- if messages[0]['role'] in ['system', 'developer'] -%}
167
+ {{- messages[0]['content'] | trim -}}
168
  {%- set loop_messages = messages[1:] -%}
169
  {%- endif -%}
170
+
171
+ {%- if tools -%}
172
+ {%- for tool in tools %}
173
+ {{- '<|tool>' -}}
174
+ {{- format_function_declaration(tool) | trim -}}
175
+ {{- '<tool|>' -}}
176
+ {%- endfor %}
177
+ {%- set ns.prev_message_type = 'tool' -%}
178
  {%- endif -%}
179
+
180
+ {{- '<turn|>\n' -}}
181
+ {%- endif %}
182
+
183
+ {#- Find last user message -#}
184
+ {%- for message in loop_messages -%}
185
+ {%- if message['role'] == 'user' -%}
186
+ {%- set ns.last_user_message = loop.index0 -%}
187
  {%- endif -%}
188
+ {%- endfor -%}
189
+
190
+ {#- Loop through messages -#}
191
+ {%- for message in loop_messages -%}
192
+ {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
193
+ {%- if not (ns.prev_message_type == 'tool_response' and message['tool_calls']) -%}
194
+ {{- '<|turn>' + role + '\n' }}
195
  {%- endif -%}
196
+
197
+ {%- set ns.prev_message_type = None -%}
198
+
199
+ {%- if message['tool_calls'] -%}
200
+ {#- Preserve reasoning between tool calls for model turns that come after the last user turn -#}
201
+ {%- if message['reasoning_content'] and loop.index0 > ns.last_user_message -%}
202
+ {{- '<|channel>thought\n' -}}
203
+ {{- message['reasoning_content'] -}}
204
+ {{- '<channel|>' -}}
205
+ {%- endif -%}
206
+ {%- for tool_call in message['tool_calls'] -%}
207
+ {%- set function = tool_call['function'] -%}
208
+ {{- '<|tool_call>call:' + function['name'] + '{' -}}
209
+ {%- if function['arguments'] is mapping -%}
210
+ {%- set ns_args = namespace(found_first=false) -%}
211
+ {%- for key, value in function['arguments'] | dictsort -%}
212
+ {%- if ns_args.found_first %},{% endif -%}
213
+ {%- set ns_args.found_first = true -%}
214
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
215
+ {%- endfor -%}
216
+ {%- elif function['arguments'] is string -%}
217
+ {{- function['arguments'] -}}
218
+ {%- endif -%}
219
+ {{- '}<tool_call|>' -}}
220
+ {%- endfor -%}
221
+ {%- set ns.prev_message_type = 'tool_call' -%}
222
+ {%- endif -%}
223
+
224
+ {%- if message['tool_responses'] -%}
225
+ {#- Tool Response handling -#}
226
+ {%- for tool_response in message['tool_responses'] -%}
227
+ {{- '<|tool_response>' -}}
228
+ {%- if tool_response['response'] is mapping -%}
229
+ {{- 'response:' + tool_response['name'] | default('unknown') + '{' -}}
230
+ {%- for key, value in tool_response['response'] | dictsort -%}
231
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
232
+ {%- if not loop.last %},{% endif -%}
233
+ {%- endfor -%}
234
+ {{- '}' -}}
235
+ {%- else -%}
236
+ {{- 'response:' + tool_response['name'] | default('unknown') + '{value:' + format_argument(tool_response['response'], escape_keys=False) + '}' -}}
237
+ {%- endif -%}
238
+ {{- '<tool_response|>' -}}
239
+ {%- endfor -%}
240
+ {%- set ns.prev_message_type = 'tool_response' -%}
241
+ {%- endif -%}
242
+
243
+ {%- if message['content'] is string -%}
244
+ {%- if role == 'model' -%}
245
+ {{- strip_thinking(message['content']) -}}
246
  {%- else -%}
247
+ {{- message['content'] | trim -}}
248
  {%- endif -%}
249
+ {%- elif message['content'] is sequence -%}
250
+ {%- for item in message['content'] -%}
251
+ {%- if item['type'] == 'text' -%}
252
+ {%- if role == 'model' -%}
253
+ {{- strip_thinking(item['text']) -}}
254
+ {%- else -%}
255
+ {{- item['text'] | trim -}}
256
+ {%- endif -%}
257
+ {%- elif item['type'] == 'image' -%}
258
+ {{- '<|image|>' -}}
259
+ {%- set ns.prev_message_type = 'image' -%}
260
+ {%- elif item['type'] == 'audio' -%}
261
+ {{- '<|audio|>' -}}
262
+ {%- set ns.prev_message_type = 'audio' -%}
263
+ {%- elif item['type'] == 'video' -%}
264
+ {{- '<|video|>' -}}
265
+ {%- set ns.prev_message_type = 'video' -%}
266
+ {%- endif -%}
267
+ {%- endfor -%}
268
  {%- endif -%}
269
+
270
+ {%- if not (message['tool_responses'] and not message['content']) -%}
271
+ {{- '<turn|>\n' -}}
272
+ {%- endif -%}
 
 
273
  {%- endfor -%}
274
+
275
  {%- if add_generation_prompt -%}
276
+ {%- if ns.prev_message_type != 'tool_response' -%}
277
+ {{- '<|turn>model
278
+ ' -}}
279
+ {%- endif -%}
280
+ {%- if not enable_thinking | default(false) -%}
281
+ {{- '<|channel>thought
282
+ <channel|>' -}}
283
+ {%- endif -%}
284
+ {%- if enable_thinking | default(true) -%}
285
+ {{- '<|channel>thought
286
+ ' -}}
287
  {%- endif -%}
288
+ {%- endif -%}