llmfan46 commited on
Commit
d728b62
·
verified ·
1 Parent(s): d5ffaa5

Upload chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +437 -0
chat_template.jinja ADDED
@@ -0,0 +1,437 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- macro format_argument(argument, escape_keys=True) -%}
2
+ {%- if argument is none -%}
3
+ {{- 'null' -}}
4
+ {%- elif argument is string -%}
5
+ {{- '<|"|>' + argument + '<|"|>' -}}
6
+ {%- elif argument is boolean -%}
7
+ {{- 'true' if argument else 'false' -}}
8
+ {%- elif argument is mapping -%}
9
+ {{- '{' -}}
10
+ {%- set ns = namespace(found_first=false) -%}
11
+ {%- for key, value in argument | dictsort -%}
12
+ {%- if ns.found_first %},{% endif -%}
13
+ {%- set ns.found_first = true -%}
14
+ {%- if escape_keys -%}
15
+ {{- '<|"|>' + key + '<|"|>' -}}
16
+ {%- else -%}
17
+ {{- key -}}
18
+ {%- endif -%}
19
+ :{{- format_argument(value, escape_keys=escape_keys) -}}
20
+ {%- endfor -%}
21
+ {{- '}' -}}
22
+ {%- elif argument is iterable and argument is not string -%}
23
+ {{- '[' -}}
24
+ {%- for item in argument -%}
25
+ {{- format_argument(item, escape_keys=escape_keys) -}}
26
+ {%- if not loop.last %},{% endif -%}
27
+ {%- endfor -%}
28
+ {{- ']' -}}
29
+ {%- else -%}
30
+ {{- argument -}}
31
+ {%- endif -%}
32
+ {%- endmacro -%}
33
+
34
+ {%- macro format_type_argument(type_value) -%}
35
+ {%- if type_value is none -%}
36
+ <|"|>OBJECT<|"|>
37
+ {%- elif type_value is string -%}
38
+ <|"|>{{ type_value | upper }}<|"|>
39
+ {%- elif type_value is iterable and type_value is not string -%}
40
+ [
41
+ {%- for item in type_value -%}
42
+ {%- if item is string -%}
43
+ <|"|>{{ item | upper }}<|"|>
44
+ {%- else -%}
45
+ {{- format_argument(item) -}}
46
+ {%- endif -%}
47
+ {%- if not loop.last %},{% endif -%}
48
+ {%- endfor -%}
49
+ ]
50
+ {%- else -%}
51
+ {{- format_argument(type_value) -}}
52
+ {%- endif -%}
53
+ {%- endmacro -%}
54
+
55
+ {%- macro format_parameters(properties, required, filter_keys=false) -%}
56
+ {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
57
+ {%- set ns = namespace(found_first=false) -%}
58
+ {%- for key, value in properties | dictsort -%}
59
+ {%- if not filter_keys or key not in standard_keys -%}
60
+ {%- if ns.found_first %},{% endif -%}
61
+ {%- set ns.found_first = true -%}
62
+ {{ key }}:{
63
+ {%- set add_comma = false -%}
64
+ {%- if value is mapping -%}
65
+ {%- set vtype = value['type'] if 'type' in value and value['type'] else none -%}
66
+ {%- set is_string_type = (vtype is string and vtype | upper == 'STRING') or (vtype is iterable and vtype is not string and ('string' in vtype or 'STRING' in vtype)) -%}
67
+ {%- set is_array_type = (vtype is string and vtype | upper == 'ARRAY') or (vtype is iterable and vtype is not string and ('array' in vtype or 'ARRAY' in vtype)) -%}
68
+ {%- set is_object_type = (vtype is string and vtype | upper == 'OBJECT') or (vtype is iterable and vtype is not string and ('object' in vtype or 'OBJECT' in vtype)) -%}
69
+ {%- if 'description' in value and value['description'] -%}
70
+ description:<|"|>{{ value['description'] }}<|"|>
71
+ {%- set add_comma = true -%}
72
+ {%- endif -%}
73
+ {%- if is_string_type -%}
74
+ {%- if 'enum' in value and value['enum'] -%}
75
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
76
+ enum:{{ format_argument(value['enum']) }}
77
+ {%- endif -%}
78
+ {%- elif is_array_type -%}
79
+ {%- if 'items' in value and value['items'] is mapping and value['items'] -%}
80
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
81
+ items:{
82
+ {%- set ns_items = namespace(found_first=false) -%}
83
+ {%- for item_key, item_value in value['items'] | dictsort -%}
84
+ {%- if item_value is not none -%}
85
+ {%- if ns_items.found_first %},{% endif -%}
86
+ {%- set ns_items.found_first = true -%}
87
+ {%- if item_key == 'properties' -%}
88
+ properties:{
89
+ {%- if item_value is mapping -%}
90
+ {{- format_parameters(item_value, value['items']['required'] if 'required' in value['items'] else []) -}}
91
+ {%- endif -%}
92
+ }
93
+ {%- elif item_key == 'required' -%}
94
+ required:[
95
+ {%- for req_item in item_value -%}
96
+ <|"|>{{- req_item -}}<|"|>
97
+ {%- if not loop.last %},{% endif -%}
98
+ {%- endfor -%}
99
+ ]
100
+ {%- elif item_key == 'type' -%}
101
+ type:{{ format_type_argument(item_value) }}
102
+ {%- else -%}
103
+ {{ item_key }}:{{ format_argument(item_value) }}
104
+ {%- endif -%}
105
+ {%- endif -%}
106
+ {%- endfor -%}
107
+ }
108
+ {%- endif -%}
109
+ {%- endif -%}
110
+ {%- if 'nullable' in value and value['nullable'] -%}
111
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
112
+ nullable:true
113
+ {%- endif -%}
114
+ {%- if is_object_type or ('properties' in value and value['properties'] is mapping) -%}
115
+ {%- if 'properties' in value and value['properties'] is mapping -%}
116
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
117
+ properties:{
118
+ {{- format_parameters(value['properties'], value['required'] if 'required' in value else []) -}}
119
+ }
120
+ {%- elif value is mapping -%}
121
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
122
+ properties:{
123
+ {{- format_parameters(value, value['required'] if 'required' in value else [], filter_keys=true) -}}
124
+ }
125
+ {%- endif -%}
126
+ {%- if 'required' in value and value['required'] -%}
127
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
128
+ required:[
129
+ {%- for item in value['required'] -%}
130
+ <|"|>{{- item -}}<|"|>
131
+ {%- if not loop.last %},{% endif -%}
132
+ {%- endfor -%}
133
+ ]
134
+ {%- endif -%}
135
+ {%- endif -%}
136
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
137
+ type:{{ format_type_argument(vtype if vtype else 'object') }}
138
+ {%- else -%}
139
+ type:{{ format_type_argument('object') }}
140
+ {%- endif -%}
141
+ }
142
+ {%- endif -%}
143
+ {%- endfor -%}
144
+ {%- endmacro -%}
145
+
146
+ {%- macro format_function_declaration(tool_data) -%}
147
+ {%- set fn = tool_data['function'] if tool_data is mapping and 'function' in tool_data else tool_data -%}
148
+ {%- set tool_name = fn['name'] if fn is mapping and 'name' in fn and fn['name'] else 'unknown' -%}
149
+ {%- set tool_description = fn['description'] if fn is mapping and 'description' in fn and fn['description'] else '' -%}
150
+ declaration:{{- tool_name -}}{description:<|"|>{{- tool_description -}}<|"|>
151
+ {%- set params = none -%}
152
+ {%- if fn is mapping and 'parameters' in fn and fn['parameters'] -%}
153
+ {%- set params = fn['parameters'] -%}
154
+ {%- elif fn is mapping and 'input_schema' in fn and fn['input_schema'] -%}
155
+ {%- set params = fn['input_schema'] -%}
156
+ {%- elif fn is mapping and 'inputSchema' in fn and fn['inputSchema'] -%}
157
+ {%- set params = fn['inputSchema'] -%}
158
+ {%- endif -%}
159
+ {%- if params and params is mapping -%}
160
+ ,parameters:{
161
+ {%- if 'properties' in params and params['properties'] -%}
162
+ properties:{ {{- format_parameters(params['properties'], params['required'] if 'required' in params else []) -}} },
163
+ {%- endif -%}
164
+ {%- if 'required' in params and params['required'] -%}
165
+ required:[
166
+ {%- for item in params['required'] -%}
167
+ <|"|>{{- item -}}<|"|>
168
+ {{- ',' if not loop.last -}}
169
+ {%- endfor -%}
170
+ ],
171
+ {%- endif -%}
172
+ {%- if 'type' in params and params['type'] -%}
173
+ type:{{- format_type_argument(params['type']) -}}}
174
+ {%- else -%}
175
+ type:{{- format_type_argument('object') -}}}
176
+ {%- endif -%}
177
+ {%- endif -%}
178
+ {%- if fn is mapping and 'response' in fn and fn['response'] is mapping -%}
179
+ {%- set response_declaration = fn['response'] -%}
180
+ ,response:{
181
+ {%- if 'description' in response_declaration and response_declaration['description'] -%}
182
+ description:<|"|>{{- response_declaration['description'] -}}<|"|>,
183
+ {%- endif -%}
184
+ {%- if 'type' in response_declaration and response_declaration['type'] -%}
185
+ type:{{- format_type_argument(response_declaration['type']) -}}}
186
+ {%- else -%}
187
+ type:{{- format_type_argument('object') -}}}
188
+ {%- endif -%}
189
+ {%- endif -%}
190
+ }
191
+ {%- endmacro -%}
192
+
193
+ {%- macro strip_thinking(text) -%}
194
+ {{- text | trim -}}
195
+ {%- endmacro -%}
196
+
197
+ {%- macro format_tool_response_block(tool_name, response) -%}
198
+ {{- '<|tool_response>' -}}
199
+ {%- if response is mapping -%}
200
+ {{- 'response:' + tool_name + '{' -}}
201
+ {%- for key, value in response | dictsort -%}
202
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
203
+ {%- if not loop.last %},{% endif -%}
204
+ {%- endfor -%}
205
+ {{- '}' -}}
206
+ {%- else -%}
207
+ {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
208
+ {%- endif -%}
209
+ {{- '<tool_response|>' -}}
210
+ {%- endmacro -%}
211
+
212
+ {#- ===== SETUP ===== -#}
213
+ {%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}
214
+ {%- set loop_messages = messages -%}
215
+ {%- set enable_thinking = enable_thinking if enable_thinking is defined else false -%}
216
+ {%- set preserve_thinking = preserve_thinking if preserve_thinking is defined else true -%}
217
+ {{- bos_token -}}
218
+ {#- Handle System/Tool Definitions Block -#}
219
+ {%- if enable_thinking or (tools is defined and tools) or (messages and messages[0]['role'] in ['system', 'developer']) -%}
220
+ {{- '<|turn>system\n' -}}
221
+ {#- Inject Thinking token at the very top of the FIRST system turn -#}
222
+ {%- if enable_thinking -%}
223
+ {{- '<|think|>\n' -}}
224
+ {%- set ns.prev_message_type = 'think' -%}
225
+ {%- endif -%}
226
+ {%- if messages and messages[0]['role'] in ['system', 'developer'] -%}
227
+ {%- if messages[0]['content'] is string -%}
228
+ {{- messages[0]['content'] | trim -}}
229
+ {%- elif messages[0]['content'] is iterable and messages[0]['content'] is not string -%}
230
+ {%- for item in messages[0]['content'] -%}
231
+ {%- if item is mapping and 'text' in item -%}{{- item['text'] | trim + ' '-}}{%- endif -%}
232
+ {%- endfor -%}
233
+ {%- endif -%}
234
+ {%- set loop_messages = messages[1:] -%}
235
+ {%- endif -%}
236
+ {%- if tools is defined and tools -%}
237
+ {%- for tool in tools %}
238
+ {{- '<|tool>' -}}
239
+ {{- format_function_declaration(tool) | trim -}}
240
+ {{- '<tool|>' -}}
241
+ {%- endfor %}
242
+ {%- set ns.prev_message_type = 'tool' -%}
243
+ {%- endif -%}
244
+ {{- '<turn|>\n' -}}
245
+ {%- endif %}
246
+
247
+ {#- Pre-scan: find last user message index for reasoning guard -#}
248
+ {%- set ns_turn = namespace(last_user_idx=-1) -%}
249
+ {%- for i in range(loop_messages | length) -%}
250
+ {%- if loop_messages[i]['role'] == 'user' -%}
251
+ {%- set ns_turn.last_user_idx = i -%}
252
+ {%- endif -%}
253
+ {%- endfor -%}
254
+
255
+ {#- Loop through messages -#}
256
+ {%- for message in loop_messages -%}
257
+ {%- if message['role'] != 'tool' -%}
258
+ {%- set ns.prev_message_type = None -%}
259
+ {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
260
+ {%- set message_has_tool_calls = ('tool_calls' in message and message['tool_calls']) -%}
261
+ {#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#}
262
+ {%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%}
263
+ {%- if not continue_same_model_turn -%}
264
+ {{- '<|turn>' + role + '\n' }}
265
+ {%- endif -%}
266
+
267
+ {#- Render reasoning/reasoning_content as thinking channel (tool-call turns only) -#}
268
+ {%- set thinking_ns = namespace(text='') -%}
269
+ {%- if 'reasoning' in message and message['reasoning'] -%}
270
+ {%- set thinking_ns.text = message['reasoning'] -%}
271
+ {%- elif 'reasoning_content' in message and message['reasoning_content'] -%}
272
+ {%- set thinking_ns.text = message['reasoning_content'] -%}
273
+ {%- endif -%}
274
+ {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or preserve_thinking -%}
275
+ {%- if thinking_ns.text and thinking_gate and message_has_tool_calls -%}
276
+ {{- '<|channel>thought\n' + thinking_ns.text + '\n<channel|>' -}}
277
+ {%- endif -%}
278
+
279
+ {%- if message_has_tool_calls -%}
280
+ {%- for tool_call in message['tool_calls'] -%}
281
+ {%- set function = tool_call['function'] if tool_call is mapping and 'function' in tool_call else tool_call -%}
282
+ {%- set function_name = function['name'] if function is mapping and 'name' in function and function['name'] else 'unknown' -%}
283
+ {{- '<|tool_call>call:' + function_name + '{' -}}
284
+ {%- if function is mapping and 'arguments' in function and function['arguments'] is mapping -%}
285
+ {%- set ns_args = namespace(found_first=false) -%}
286
+ {%- for key, value in function['arguments'] | dictsort -%}
287
+ {%- if ns_args.found_first %},{% endif -%}
288
+ {%- set ns_args.found_first = true -%}
289
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
290
+ {%- endfor -%}
291
+ {%- elif function is mapping and 'arguments' in function and function['arguments'] is string -%}
292
+ {{- function['arguments'] -}}
293
+ {%- elif function is mapping and 'arguments' in function and function['arguments'] is not none -%}
294
+ {{- format_argument(function['arguments'], escape_keys=False) -}}
295
+ {%- endif -%}
296
+ {{- '}<tool_call|>' -}}
297
+ {%- endfor -%}
298
+ {%- set ns.prev_message_type = 'tool_call' -%}
299
+ {%- endif -%}
300
+
301
+ {%- set ns_tr_out = namespace(flag=false) -%}
302
+ {%- if 'tool_responses' in message and message['tool_responses'] -%}
303
+ {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
304
+ {%- for tool_response in message['tool_responses'] -%}
305
+ {%- set tr_name = tool_response['name'] if tool_response is mapping and 'name' in tool_response and tool_response['name'] else 'unknown' -%}
306
+ {%- set tr_response = tool_response['response'] if tool_response is mapping and 'response' in tool_response else '' -%}
307
+ {{- format_tool_response_block(tr_name, tr_response) -}}
308
+ {%- set ns_tr_out.flag = true -%}
309
+ {%- set ns.prev_message_type = 'tool_response' -%}
310
+ {%- endfor -%}
311
+ {%- elif message_has_tool_calls -%}
312
+ {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
313
+ {%- set ns_tool_scan = namespace(stopped=false) -%}
314
+ {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
315
+ {%- if ns_tool_scan.stopped -%}
316
+ {%- elif loop_messages[k]['role'] != 'tool' -%}
317
+ {%- set ns_tool_scan.stopped = true -%}
318
+ {%- else -%}
319
+ {%- set follow = loop_messages[k] -%}
320
+ {#- Resolve tool_call_id to function name -#}
321
+ {%- set ns_tname = namespace(name='unknown') -%}
322
+ {%- if 'name' in follow and follow['name'] -%}
323
+ {%- set ns_tname.name = follow['name'] -%}
324
+ {%- endif -%}
325
+ {%- for tc in message['tool_calls'] -%}
326
+ {%- if tc is mapping and 'id' in tc and 'tool_call_id' in follow and tc['id'] == follow['tool_call_id'] -%}
327
+ {%- if 'function' in tc and 'name' in tc['function'] and tc['function']['name'] -%}
328
+ {%- set ns_tname.name = tc['function']['name'] -%}
329
+ {%- elif 'name' in tc and tc['name'] -%}
330
+ {%- set ns_tname.name = tc['name'] -%}
331
+ {%- endif -%}
332
+ {%- endif -%}
333
+ {%- endfor -%}
334
+ {#- Handle content as string, object, or content-parts array -#}
335
+ {%- set tool_body = follow['content'] if 'content' in follow else '' -%}
336
+ {%- if tool_body is string -%}
337
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
338
+ {%- elif tool_body is mapping -%}
339
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
340
+ {%- elif tool_body is iterable and tool_body is not string -%}
341
+ {%- set ns_txt = namespace(s='') -%}
342
+ {%- for part in tool_body -%}
343
+ {%- if part is mapping and 'type' in part and part['type'] == 'text' and 'text' in part -%}
344
+ {%- set ns_txt.s = ns_txt.s + part['text'] -%}
345
+ {%- endif -%}
346
+ {%- endfor -%}
347
+ {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
348
+ {%- for part in tool_body -%}
349
+ {%- if part is mapping and 'type' in part and part['type'] in ['image', 'image_url'] -%}
350
+ {{- '<|image|>' -}}
351
+ {%- elif part is mapping and 'type' in part and part['type'] in ['audio', 'input_audio'] -%}
352
+ {{- '<|audio|>' -}}
353
+ {%- elif part is mapping and 'type' in part and part['type'] == 'video' -%}
354
+ {{- '<|video|>' -}}
355
+ {%- endif -%}
356
+ {%- endfor -%}
357
+ {%- else -%}
358
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
359
+ {%- endif -%}
360
+ {%- set ns_tr_out.flag = true -%}
361
+ {%- set ns.prev_message_type = 'tool_response' -%}
362
+ {%- endif -%}
363
+ {%- endfor -%}
364
+ {%- endif -%}
365
+
366
+ {%- set captured_content -%}
367
+ {%- if 'content' in message and message['content'] is string -%}
368
+ {%- if role == 'model' -%}
369
+ {{- strip_thinking(message['content']) -}}
370
+ {%- else -%}
371
+ {{- message['content'] | trim -}}
372
+ {%- endif -%}
373
+ {%- elif 'content' in message and message['content'] is iterable and message['content'] is not string -%}
374
+ {%- for item in message['content'] -%}
375
+ {%- if item is mapping and 'type' in item and item['type'] == 'text' and 'text' in item -%}
376
+ {%- if role == 'model' -%}
377
+ {{- strip_thinking(item['text']) -}}
378
+ {%- else -%}
379
+ {{- item['text'] | trim -}}
380
+ {%- endif -%}
381
+ {%- elif item is mapping and 'type' in item and item['type'] in ['image', 'image_url'] -%}
382
+ {{- '<|image|>' -}}
383
+ {%- elif item is mapping and 'type' in item and item['type'] in ['audio', 'input_audio'] -%}
384
+ {{- '<|audio|>' -}}
385
+ {%- elif item is mapping and 'type' in item and item['type'] == 'video' -%}
386
+ {{- '<|video|>' -}}
387
+ {%- endif -%}
388
+ {%- endfor -%}
389
+ {%- endif -%}
390
+ {%- endset -%}
391
+
392
+ {{- captured_content -}}
393
+ {%- set has_content = captured_content | trim | length > 0 -%}
394
+
395
+ {#- Forward-scan: find next non-tool message role for continuation detection -#}
396
+ {%- set next_nt = namespace(role=None, found=false) -%}
397
+ {%- for j in range(loop.index0 + 1, loop_messages | length) -%}
398
+ {%- if not next_nt.found -%}
399
+ {%- if loop_messages[j]['role'] != 'tool' -%}
400
+ {%- set next_nt.role = loop_messages[j]['role'] -%}
401
+ {%- set next_nt.found = true -%}
402
+ {%- endif -%}
403
+ {%- endif -%}
404
+ {%- endfor -%}
405
+
406
+ {%- set continues_into_next = (
407
+ role == 'model'
408
+ and next_nt.role == 'assistant'
409
+ and not message_has_tool_calls
410
+ and not ns_tr_out.flag
411
+ ) -%}
412
+
413
+ {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
414
+ {{- '<|tool_response>' -}}
415
+ {%- elif continues_into_next -%}
416
+ {{- '\n' -}}
417
+ {%- elif not (ns_tr_out.flag and not has_content) -%}
418
+ {{- '<turn|>\n' -}}
419
+ {%- endif -%}
420
+
421
+ {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
422
+ {%- set ns.prev_non_tool_role = message['role'] -%}
423
+ {%- endif -%}
424
+ {%- endfor -%}
425
+
426
+ {%- if add_generation_prompt is defined and add_generation_prompt -%}
427
+ {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
428
+ {{- '<|turn>model\n' -}}
429
+ {%- endif -%}
430
+
431
+ {%- if not enable_thinking -%}
432
+ {#- Suppress thinking — but not when awaiting tool responses -#}
433
+ {%- if ns.prev_message_type != 'tool_call' -%}
434
+ {{- '<|channel>thought\n<channel|>' -}}
435
+ {%- endif -%}
436
+ {%- endif -%}
437
+ {%- endif -%}