main
jinja 387 lines 18.2 KB
Raw
1 {%- macro format_parameters(properties, required, filter_keys=false) -%}
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 not filter_keys or 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['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 -%}
26 {%- if ns_items.found_first %},{% endif -%}
27 {%- set ns_items.found_first = true -%}
28 {%- if item_key == 'properties' -%}
29 properties:{
30 {%- if item_value is mapping -%}
31 {{- format_parameters(item_value, value['items']['required'] | default([])) -}}
32 {%- endif -%}
33 }
34 {%- elif item_key == 'required' -%}
35 required:[
36 {%- for req_item in item_value -%}
37 <|"|>{{- req_item -}}<|"|>
38 {%- if not loop.last %},{% endif -%}
39 {%- endfor -%}
40 ]
41 {%- elif item_key == 'type' -%}
42 {%- if item_value is string -%}
43 type:{{ format_argument(item_value | upper) }}
44 {%- else -%}
45 type:{{ format_argument(item_value | map('upper') | list) }}
46 {%- endif -%}
47 {%- else -%}
48 {{ item_key }}:{{ format_argument(item_value) }}
49 {%- endif -%}
50 {%- endif -%}
51 {%- endfor -%}
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([]), filter_keys=true) -}}
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 -%}
84 {%- endfor -%}
85 {%- endmacro -%}
86 {%- macro format_function_declaration(tool_data) -%}
87 declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
88 {%- set params = tool_data['function']['parameters'] -%}
89 {%- if params -%}
90 ,parameters:{
91 {%- if params['properties'] -%}
92 properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
93 {%- endif -%}
94 {%- if params['required'] -%}
95 required:[
96 {%- for item in params['required'] -%}
97 <|"|>{{- item -}}<|"|>
98 {{- ',' if not loop.last -}}
99 {%- endfor -%}
100 ],
101 {%- endif -%}
102 {%- if params['type'] -%}
103 type:<|"|>{{- params['type'] | upper -}}<|"|>}
104 {%- endif -%}
105 {%- endif -%}
106 {%- if 'response' in tool_data['function'] -%}
107 {%- set response_declaration = tool_data['function']['response'] -%}
108 ,response:{
109 {%- if response_declaration['description'] -%}
110 description:<|"|>{{- response_declaration['description'] -}}<|"|>,
111 {%- endif -%}
112 {%- if response_declaration['type'] | upper == 'OBJECT' -%}
113 type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
114 {%- endif -%}
115 {%- endif -%}
116 }
117 {%- endmacro -%}
118 {%- macro format_argument(argument, escape_keys=True) -%}
119 {%- if argument is none -%}
120 {{- 'null' -}}
121 {%- elif argument is string -%}
122 {{- '<|"|>' + argument + '<|"|>' -}}
123 {%- elif argument is boolean -%}
124 {{- 'true' if argument else 'false' -}}
125 {%- elif argument is mapping -%}
126 {{- '{' -}}
127 {%- set ns = namespace(found_first=false) -%}
128 {%- for key, value in argument | dictsort -%}
129 {%- if ns.found_first %},{% endif -%}
130 {%- set ns.found_first = true -%}
131 {%- if escape_keys -%}
132 {{- '<|"|>' + key + '<|"|>' -}}
133 {%- else -%}
134 {{- key -}}
135 {%- endif -%}
136 :{{- format_argument(value, escape_keys=escape_keys) -}}
137 {%- endfor -%}
138 {{- '}' -}}
139 {%- elif argument is sequence -%}
140 {{- '[' -}}
141 {%- for item in argument -%}
142 {{- format_argument(item, escape_keys=escape_keys) -}}
143 {%- if not loop.last %},{% endif -%}
144 {%- endfor -%}
145 {{- ']' -}}
146 {%- else -%}
147 {{- argument -}}
148 {%- endif -%}
149 {%- endmacro -%}
150 {%- macro strip_thinking(text) -%}
151 {%- set ns = namespace(result='') -%}
152 {%- for part in text.split('<channel|>') -%}
153 {%- if '<|channel>' in part -%}
154 {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}
155 {%- else -%}
156 {%- set ns.result = ns.result + part -%}
157 {%- endif -%}
158 {%- endfor -%}
159 {{- ns.result | trim -}}
160 {%- endmacro -%}
161
162 {%- macro format_tool_response_block(tool_name, response) -%}
163 {{- '<|tool_response>' -}}
164 {%- if response is mapping -%}
165 {{- 'response:' + tool_name + '{' -}}
166 {%- for key, value in response | dictsort -%}
167 {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
168 {%- if not loop.last %},{% endif -%}
169 {%- endfor -%}
170 {{- '}' -}}
171 {%- else -%}
172 {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
173 {%- endif -%}
174 {{- '<tool_response|>' -}}
175 {%- endmacro -%}
176
177 {#- ===== SETUP ===== -#}
178 {%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}
179 {%- set loop_messages = messages -%}
180 {%- set enable_thinking = enable_thinking | default(false) -%}
181 {%- set preserve_thinking = preserve_thinking | default(false) -%}
182 {{- bos_token -}}
183 {#- Handle System/Tool Definitions Block -#}
184 {%- if enable_thinking or tools or (messages and messages[0]['role'] in ['system', 'developer']) -%}
185 {{- '<|turn>system\n' -}}
186 {#- Inject Thinking token at the very top of the FIRST system turn -#}
187 {%- if enable_thinking -%}
188 {{- '<|think|>\n' -}}
189 {%- set ns.prev_message_type = 'think' -%}
190 {%- endif -%}
191 {%- if messages and messages[0]['role'] in ['system', 'developer'] -%}
192 {%- if messages[0]['content'] is string -%}
193 {{- messages[0]['content'] | trim -}}
194 {%- elif messages[0]['content'] is sequence -%}
195 {%- for item in messages[0]['content'] -%}
196 {{- item['text'] | trim + ' '-}}
197 {%- endfor -%}
198 {%- endif -%}
199 {%- set loop_messages = messages[1:] -%}
200 {%- endif -%}
201 {%- if tools -%}
202 {%- for tool in tools %}
203 {{- '<|tool>' -}}
204 {{- format_function_declaration(tool) | trim -}}
205 {{- '<tool|>' -}}
206 {%- endfor %}
207 {%- set ns.prev_message_type = 'tool' -%}
208 {%- endif -%}
209 {{- '<turn|>\n' -}}
210 {%- endif %}
211
212 {#- Pre-scan: find last user message index for reasoning guard -#}
213 {%- set ns_turn = namespace(last_user_idx=-1) -%}
214 {%- for i in range(loop_messages | length) -%}
215 {%- if loop_messages[i]['role'] == 'user' -%}
216 {%- set ns_turn.last_user_idx = i -%}
217 {%- endif -%}
218 {%- endfor -%}
219
220 {#- Loop through messages -#}
221 {%- for message in loop_messages -%}
222 {%- if message['role'] != 'tool' -%}
223 {%- set ns.prev_message_type = None -%}
224 {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
225 {#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#}
226 {%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%}
227 {%- if not continue_same_model_turn -%}
228 {{- '<|turn>' + role + '\n' }}
229 {%- if role == 'model' and not enable_thinking and not (message.get('reasoning') or message.get('reasoning_content')) -%}
230 {{- '<|channel>thought\n<channel|>' -}}
231 {%- endif -%}
232 {%- endif -%}
233
234 {#- Render reasoning/reasoning_content as thinking channel -#}
235 {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
236 {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or preserve_thinking -%}
237 {%- if thinking_text and thinking_gate -%}
238 {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
239 {%- endif -%}
240
241 {%- if message.get('tool_calls') -%}
242 {%- for tool_call in message.get('tool_calls') -%}
243 {%- set function = tool_call['function'] -%}
244 {{- '<|tool_call>call:' + function['name'] + '{' -}}
245 {%- if function['arguments'] is mapping -%}
246 {%- set ns_args = namespace(found_first=false) -%}
247 {%- for key, value in function['arguments'] | dictsort -%}
248 {%- if ns_args.found_first %},{% endif -%}
249 {%- set ns_args.found_first = true -%}
250 {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
251 {%- endfor -%}
252 {%- elif function['arguments'] is none -%}
253 {%- else -%}
254 {{- raise_exception(
255 "chat_template: tool_calls[].function.arguments must be a "
256 "JSON object (mapping), not a string. Deserialize arguments "
257 "before passing to the template."
258 ) -}}
259 {%- endif -%}
260 {{- '}<tool_call|>' -}}
261 {%- endfor -%}
262 {%- set ns.prev_message_type = 'tool_call' -%}
263 {%- endif -%}
264
265 {%- set ns_tr_out = namespace(flag=false) -%}
266 {%- if message.get('tool_responses') -%}
267 {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
268 {%- for tool_response in message.get('tool_responses') -%}
269 {{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}
270 {%- set ns_tr_out.flag = true -%}
271 {%- set ns.prev_message_type = 'tool_response' -%}
272 {%- endfor -%}
273 {%- elif message.get('tool_calls') -%}
274 {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
275 {%- set ns_tool_scan = namespace(stopped=false) -%}
276 {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
277 {%- if ns_tool_scan.stopped -%}
278 {%- elif loop_messages[k]['role'] != 'tool' -%}
279 {%- set ns_tool_scan.stopped = true -%}
280 {%- else -%}
281 {%- set follow = loop_messages[k] -%}
282 {#- Resolve tool_call_id to function name -#}
283 {%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%}
284 {%- for tc in message.get('tool_calls') -%}
285 {%- if tc.get('id') == follow.get('tool_call_id') -%}
286 {%- set ns_tname.name = tc['function']['name'] -%}
287 {%- endif -%}
288 {%- endfor -%}
289 {#- Handle content as string or content-parts array -#}
290 {%- set tool_body = follow.get('content') -%}
291 {%- if tool_body is string -%}
292 {{- format_tool_response_block(ns_tname.name, tool_body) -}}
293 {%- elif tool_body is sequence and tool_body is not string -%}
294 {%- set ns_txt = namespace(s='') -%}
295 {%- for part in tool_body -%}
296 {%- if part.get('type') == 'text' -%}
297 {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
298 {%- endif -%}
299 {%- endfor -%}
300 {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
301 {%- for part in tool_body -%}
302 {%- if part.get('type') in ['image', 'image_url'] -%}
303 {{- '<|image|>' -}}
304 {%- elif part.get('type') in ['audio', 'input_audio'] -%}
305 {{- '<|audio|>' -}}
306 {%- elif part.get('type') == 'video' -%}
307 {{- '<|video|>' -}}
308 {%- endif -%}
309 {%- endfor -%}
310 {%- else -%}
311 {{- format_tool_response_block(ns_tname.name, tool_body) -}}
312 {%- endif -%}
313 {%- set ns_tr_out.flag = true -%}
314 {%- set ns.prev_message_type = 'tool_response' -%}
315 {%- endif -%}
316 {%- endfor -%}
317 {%- endif -%}
318
319 {%- set captured_content -%}
320 {%- if message.get('content') is string -%}
321 {%- if role == 'model' -%}
322 {{- strip_thinking(message['content']) -}}
323 {%- else -%}
324 {{- message['content'] | trim -}}
325 {%- endif -%}
326 {%- elif message.get('content') is sequence -%}
327 {%- for item in message['content'] -%}
328 {%- if item.get('type') == 'text' -%}
329 {%- if role == 'model' -%}
330 {{- strip_thinking(item['text']) -}}
331 {%- else -%}
332 {{- item['text'] | trim -}}
333 {%- endif -%}
334 {%- elif item.get('type') in ['image', 'image_url'] -%}
335 {{- '<|image|>' -}}
336 {%- elif item.get('type') in ['audio', 'input_audio'] -%}
337 {{- '<|audio|>' -}}
338 {%- elif item.get('type') == 'video' -%}
339 {{- '<|video|>' -}}
340 {%- endif -%}
341 {%- endfor -%}
342 {%- endif -%}
343 {%- endset -%}
344
345 {{- captured_content -}}
346 {%- set has_content = captured_content | trim | length > 0 -%}
347
348 {#- Forward-scan: find next non-tool message role for continuation detection -#}
349 {%- set next_nt = namespace(role=None, found=false) -%}
350 {%- for j in range(loop.index0 + 1, loop_messages | length) -%}
351 {%- if not next_nt.found -%}
352 {%- if loop_messages[j]['role'] != 'tool' -%}
353 {%- set next_nt.role = loop_messages[j]['role'] -%}
354 {%- set next_nt.found = true -%}
355 {%- endif -%}
356 {%- endif -%}
357 {%- endfor -%}
358
359 {%- set continues_into_next = (
360 role == 'model'
361 and next_nt.role == 'assistant'
362 and (not message.get('tool_calls') or ns_tr_out.flag)
363 ) -%}
364
365 {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
366 {{- '<|tool_response>' -}}
367 {%- elif continues_into_next -%}
368 {{- '\n' -}}
369 {%- elif not (ns_tr_out.flag and not has_content) -%}
370 {{- '<turn|>\n' -}}
371 {%- endif -%}
372
373 {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
374 {%- set ns.prev_non_tool_role = message['role'] -%}
375 {%- endif -%}
376 {%- endfor -%}
377
378 {%- if add_generation_prompt -%}
379 {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
380 {{- '<|turn>model\n' -}}
381 {%- if not enable_thinking -%}
382 {{- '<|channel>thought\n<channel|>' -}}
383 {%- endif -%}
384 {%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%}
385 {{- '<|channel>thought\n' -}}
386 {%- endif -%}
387 {%- endif -%}