coder3101 commited on
Commit
b07393d
·
verified ·
1 Parent(s): 46d81c4

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +118 -51
chat_template.jinja CHANGED
@@ -11,34 +11,15 @@
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 -%}
@@ -71,6 +52,32 @@
71
  }
72
  {%- endif -%}
73
  {%- endif -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
75
  type:<|"|>{{ value['type'] | upper }}<|"|>}
76
  {%- endif -%}
@@ -149,25 +156,35 @@
149
  {%- endfor -%}
150
  {{- ns.result | trim -}}
151
  {%- endmacro -%}
152
-
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  {%- set ns = namespace(prev_message_type=None) -%}
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|>' -}}
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>' -}}
@@ -176,16 +193,41 @@
176
  {%- endfor %}
177
  {%- set ns.prev_message_type = 'tool' -%}
178
  {%- endif -%}
179
-
180
  {{- '<turn|>\n' -}}
181
  {%- endif %}
182
-
 
 
 
 
 
 
183
  {#- Loop through messages -#}
184
  {%- for message in loop_messages -%}
 
185
  {%- set ns.prev_message_type = None -%}
186
  {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  {{- '<|turn>' + role + '\n' }}
188
-
 
 
 
 
 
189
  {%- if message['tool_calls'] -%}
190
  {%- for tool_call in message['tool_calls'] -%}
191
  {%- set function = tool_call['function'] -%}
@@ -204,26 +246,50 @@
204
  {%- endfor -%}
205
  {%- set ns.prev_message_type = 'tool_call' -%}
206
  {%- endif -%}
207
-
208
- {%- if message['tool_responses'] -%}
209
- {#- Tool Response handling -#}
210
  {%- for tool_response in message['tool_responses'] -%}
211
- {{- '<|tool_response>' -}}
212
- {%- if tool_response['response'] is mapping -%}
213
- {{- 'response:' + tool_response['name'] | default('unknown') + '{' -}}
214
- {%- for key, value in tool_response['response'] | dictsort -%}
215
- {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
216
- {%- if not loop.last %},{% endif -%}
217
- {%- endfor -%}
218
- {{- '}' -}}
 
 
 
219
  {%- else -%}
220
- {{- 'response:' + tool_response['name'] | default('unknown') + '{value:' + format_argument(tool_response['response'], escape_keys=False) + '}' -}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  {%- endif -%}
222
- {{- '<tool_response|>' -}}
223
  {%- endfor -%}
224
- {%- set ns.prev_message_type = 'tool_response' -%}
225
  {%- endif -%}
226
-
227
  {%- if message['content'] is string -%}
228
  {%- if role == 'model' -%}
229
  {{- strip_thinking(message['content']) -}}
@@ -239,25 +305,26 @@
239
  {{- item['text'] | trim -}}
240
  {%- endif -%}
241
  {%- elif item['type'] == 'image' -%}
242
- {{- '\n\n<|image|>\n\n' -}}
243
  {%- set ns.prev_message_type = 'image' -%}
244
  {%- elif item['type'] == 'audio' -%}
245
  {{- '<|audio|>' -}}
246
  {%- set ns.prev_message_type = 'audio' -%}
247
  {%- elif item['type'] == 'video' -%}
248
- {{- '\n\n<|video|>\n\n' -}}
249
  {%- set ns.prev_message_type = 'video' -%}
250
  {%- endif -%}
251
  {%- endfor -%}
252
  {%- endif -%}
253
-
254
- {%- if not (message['tool_responses'] and not message['content']) -%}
 
255
  {{- '<turn|>\n' -}}
256
  {%- endif -%}
 
257
  {%- endfor -%}
258
-
259
  {%- if add_generation_prompt -%}
260
- {%- if ns.prev_message_type != 'tool_response' -%}
261
  {{- '<|turn>model\n' -}}
262
  {%- endif -%}
263
  {%- endif -%}
 
11
  description:<|"|>{{ value['description'] }}<|"|>
12
  {%- set add_comma = true -%}
13
  {%- endif -%}
 
 
 
 
14
  {%- if value['type'] | upper == 'STRING' -%}
15
  {%- if value['enum'] -%}
16
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
17
  enum:{{ format_argument(value['enum']) }}
18
  {%- endif -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  {%- elif value['type'] | upper == 'ARRAY' -%}
20
  {%- if value['items'] is mapping and value['items'] -%}
21
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
22
+ items:{
23
  {%- set ns_items = namespace(found_first=false) -%}
24
  {%- for item_key, item_value in value['items'] | dictsort -%}
25
  {%- if item_value is not none -%}
 
52
  }
53
  {%- endif -%}
54
  {%- endif -%}
55
+ {%- if value['nullable'] %}
56
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
57
+ nullable:true
58
+ {%- endif -%}
59
+ {%- if value['type'] | upper == 'OBJECT' -%}
60
+ {%- if value['properties'] is defined and value['properties'] is mapping -%}
61
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
62
+ properties:{
63
+ {{- format_parameters(value['properties'], value['required'] | default([])) -}}
64
+ }
65
+ {%- elif value is mapping -%}
66
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
67
+ properties:{
68
+ {{- format_parameters(value, value['required'] | default([])) -}}
69
+ }
70
+ {%- endif -%}
71
+ {%- if value['required'] -%}
72
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
73
+ required:[
74
+ {%- for item in value['required'] | default([]) -%}
75
+ <|"|>{{- item -}}<|"|>
76
+ {%- if not loop.last %},{% endif -%}
77
+ {%- endfor -%}
78
+ ]
79
+ {%- endif -%}
80
+ {%- endif -%}
81
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
82
  type:<|"|>{{ value['type'] | upper }}<|"|>}
83
  {%- endif -%}
 
156
  {%- endfor -%}
157
  {{- ns.result | trim -}}
158
  {%- endmacro -%}
159
+ {%- macro format_tool_response_block(tool_name, response) -%}
160
+ {{- '<|tool_response>' -}}
161
+ {%- if response is mapping -%}
162
+ {{- 'response:' + tool_name + '{' -}}
163
+ {%- for key, value in response | dictsort -%}
164
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
165
+ {%- if not loop.last %},{% endif -%}
166
+ {%- endfor -%}
167
+ {{- '}' -}}
168
+ {%- else -%}
169
+ {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
170
+ {%- endif -%}
171
+ {{- '<tool_response|>' -}}
172
+ {%- endmacro -%}
173
  {%- set ns = namespace(prev_message_type=None) -%}
174
  {%- set loop_messages = messages -%}
175
+ {{- bos_token -}}
176
  {#- Handle System/Tool Definitions Block -#}
177
  {%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
178
  {{- '<|turn>system\n' -}}
 
179
  {#- Inject Thinking token at the very top of the FIRST system turn -#}
180
  {%- if enable_thinking is defined and enable_thinking -%}
181
+ {{- '<|think|>\n' -}}
182
  {%- set ns.prev_message_type = 'think' -%}
183
  {%- endif -%}
 
184
  {%- if messages[0]['role'] in ['system', 'developer'] -%}
185
  {{- messages[0]['content'] | trim -}}
186
  {%- set loop_messages = messages[1:] -%}
187
  {%- endif -%}
 
188
  {%- if tools -%}
189
  {%- for tool in tools %}
190
  {{- '<|tool>' -}}
 
193
  {%- endfor %}
194
  {%- set ns.prev_message_type = 'tool' -%}
195
  {%- endif -%}
 
196
  {{- '<turn|>\n' -}}
197
  {%- endif %}
198
+ {#- Pre-scan: find last user message index for reasoning guard -#}
199
+ {%- set ns_turn = namespace(last_user_idx=-1) -%}
200
+ {%- for i in range(loop_messages | length) -%}
201
+ {%- if loop_messages[i]['role'] == 'user' -%}
202
+ {%- set ns_turn.last_user_idx = i -%}
203
+ {%- endif -%}
204
+ {%- endfor -%}
205
  {#- Loop through messages -#}
206
  {%- for message in loop_messages -%}
207
+ {%- if message['role'] != 'tool' -%}
208
  {%- set ns.prev_message_type = None -%}
209
  {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
210
+ {#- Detect continuation: suppress duplicate <|turn>model when previous non-tool message was also assistant -#}
211
+ {%- set prev_nt = namespace(role=None, found=false) -%}
212
+ {%- if loop.index0 > 0 -%}
213
+ {%- for j in range(loop.index0 - 1, -1, -1) -%}
214
+ {%- if not prev_nt.found -%}
215
+ {%- if loop_messages[j]['role'] != 'tool' -%}
216
+ {%- set prev_nt.role = loop_messages[j]['role'] -%}
217
+ {%- set prev_nt.found = true -%}
218
+ {%- endif -%}
219
+ {%- endif -%}
220
+ {%- endfor -%}
221
+ {%- endif -%}
222
+ {%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant') -%}
223
+ {%- if not continue_same_model_turn -%}
224
  {{- '<|turn>' + role + '\n' }}
225
+ {%- endif -%}
226
+ {#- Render reasoning/reasoning_content as thinking channel -#}
227
+ {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
228
+ {%- if thinking_text and loop.index0 > ns_turn.last_user_idx and message.get('tool_calls') -%}
229
+ {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
230
+ {%- endif -%}
231
  {%- if message['tool_calls'] -%}
232
  {%- for tool_call in message['tool_calls'] -%}
233
  {%- set function = tool_call['function'] -%}
 
246
  {%- endfor -%}
247
  {%- set ns.prev_message_type = 'tool_call' -%}
248
  {%- endif -%}
249
+ {%- set ns_tr_out = namespace(flag=false) -%}
250
+ {%- if message.get('tool_responses') -%}
251
+ {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
252
  {%- for tool_response in message['tool_responses'] -%}
253
+ {{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}}
254
+ {%- set ns_tr_out.flag = true -%}
255
+ {%- set ns.prev_message_type = 'tool_response' -%}
256
+ {%- endfor -%}
257
+ {%- elif message.get('tool_calls') -%}
258
+ {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
259
+ {%- set ns_tool_scan = namespace(stopped=false) -%}
260
+ {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
261
+ {%- if ns_tool_scan.stopped -%}
262
+ {%- elif loop_messages[k]['role'] != 'tool' -%}
263
+ {%- set ns_tool_scan.stopped = true -%}
264
  {%- else -%}
265
+ {%- set follow = loop_messages[k] -%}
266
+ {#- Resolve tool_call_id to function name -#}
267
+ {%- set ns_tname = namespace(name=follow.get('name') | default('unknown')) -%}
268
+ {%- for tc in message['tool_calls'] -%}
269
+ {%- if tc.get('id') == follow.get('tool_call_id') -%}
270
+ {%- set ns_tname.name = tc['function']['name'] -%}
271
+ {%- endif -%}
272
+ {%- endfor -%}
273
+ {#- Handle content as string or content-parts array -#}
274
+ {%- set tool_body = follow.get('content') -%}
275
+ {%- if tool_body is string -%}
276
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
277
+ {%- elif tool_body is sequence and tool_body is not string -%}
278
+ {%- set ns_txt = namespace(s='') -%}
279
+ {%- for part in tool_body -%}
280
+ {%- if part.get('type') == 'text' -%}
281
+ {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
282
+ {%- endif -%}
283
+ {%- endfor -%}
284
+ {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
285
+ {%- else -%}
286
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
287
+ {%- endif -%}
288
+ {%- set ns_tr_out.flag = true -%}
289
+ {%- set ns.prev_message_type = 'tool_response' -%}
290
  {%- endif -%}
 
291
  {%- endfor -%}
 
292
  {%- endif -%}
 
293
  {%- if message['content'] is string -%}
294
  {%- if role == 'model' -%}
295
  {{- strip_thinking(message['content']) -}}
 
305
  {{- item['text'] | trim -}}
306
  {%- endif -%}
307
  {%- elif item['type'] == 'image' -%}
308
+ {{- '<|image|>' -}}
309
  {%- set ns.prev_message_type = 'image' -%}
310
  {%- elif item['type'] == 'audio' -%}
311
  {{- '<|audio|>' -}}
312
  {%- set ns.prev_message_type = 'audio' -%}
313
  {%- elif item['type'] == 'video' -%}
314
+ {{- '<|video|>' -}}
315
  {%- set ns.prev_message_type = 'video' -%}
316
  {%- endif -%}
317
  {%- endfor -%}
318
  {%- endif -%}
319
+ {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
320
+ {{- '<|tool_response>' -}}
321
+ {%- elif not (ns_tr_out.flag and not message.get('content')) -%}
322
  {{- '<turn|>\n' -}}
323
  {%- endif -%}
324
+ {%- endif -%}
325
  {%- endfor -%}
 
326
  {%- if add_generation_prompt -%}
327
+ {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
328
  {{- '<|turn>model\n' -}}
329
  {%- endif -%}
330
  {%- endif -%}