Instructions to use unsloth/gemma-4-12b-it with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Inference
- Local Apps Settings
- Unsloth Desktop
Update chat template with tool-calling and turn-balance fixes
Browse files- chat_template.jinja +66 -40
- tokenizer_config.json +1 -1
chat_template.jinja
CHANGED
|
@@ -116,7 +116,9 @@
|
|
| 116 |
}
|
| 117 |
{%- endmacro -%}
|
| 118 |
{%- macro format_argument(argument, escape_keys=True) -%}
|
| 119 |
-
{%- if argument is
|
|
|
|
|
|
|
| 120 |
{{- '<|"|>' + argument + '<|"|>' -}}
|
| 121 |
{%- elif argument is boolean -%}
|
| 122 |
{{- 'true' if argument else 'false' -}}
|
|
@@ -172,18 +174,21 @@
|
|
| 172 |
{{- '<tool_response|>' -}}
|
| 173 |
{%- endmacro -%}
|
| 174 |
|
| 175 |
-
{
|
|
|
|
| 176 |
{%- set loop_messages = messages -%}
|
|
|
|
|
|
|
| 177 |
{{- bos_token -}}
|
| 178 |
{#- Handle System/Tool Definitions Block -#}
|
| 179 |
-
{%- if
|
| 180 |
{{- '<|turn>system\n' -}}
|
| 181 |
{#- Inject Thinking token at the very top of the FIRST system turn -#}
|
| 182 |
-
{%- if enable_thinking
|
| 183 |
{{- '<|think|>\n' -}}
|
| 184 |
{%- set ns.prev_message_type = 'think' -%}
|
| 185 |
{%- endif -%}
|
| 186 |
-
{%- if messages[0]['role'] in ['system', 'developer'] -%}
|
| 187 |
{%- if messages[0]['content'] is string -%}
|
| 188 |
{{- messages[0]['content'] | trim -}}
|
| 189 |
{%- elif messages[0]['content'] is sequence -%}
|
|
@@ -217,31 +222,22 @@
|
|
| 217 |
{%- if message['role'] != 'tool' -%}
|
| 218 |
{%- set ns.prev_message_type = None -%}
|
| 219 |
{%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
|
| 220 |
-
{#- Detect continuation
|
| 221 |
-
{%- set
|
| 222 |
-
{%- if loop.index0 > 0 -%}
|
| 223 |
-
{%- for j in range(loop.index0 - 1, -1, -1) -%}
|
| 224 |
-
{%- if not prev_nt.found -%}
|
| 225 |
-
{%- if loop_messages[j]['role'] != 'tool' -%}
|
| 226 |
-
{%- set prev_nt.role = loop_messages[j]['role'] -%}
|
| 227 |
-
{%- set prev_nt.found = true -%}
|
| 228 |
-
{%- endif -%}
|
| 229 |
-
{%- endif -%}
|
| 230 |
-
{%- endfor -%}
|
| 231 |
-
{%- endif -%}
|
| 232 |
-
{%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant') -%}
|
| 233 |
{%- if not continue_same_model_turn -%}
|
| 234 |
{{- '<|turn>' + role + '\n' }}
|
|
|
|
| 235 |
{%- endif -%}
|
| 236 |
|
| 237 |
{#- Render reasoning/reasoning_content as thinking channel -#}
|
| 238 |
{%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
|
| 239 |
-
{%-
|
|
|
|
| 240 |
{{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
|
| 241 |
{%- endif -%}
|
| 242 |
|
| 243 |
-
{%- if message
|
| 244 |
-
{%- for tool_call in message
|
| 245 |
{%- set function = tool_call['function'] -%}
|
| 246 |
{{- '<|tool_call>call:' + function['name'] + '{' -}}
|
| 247 |
{%- if function['arguments'] is mapping -%}
|
|
@@ -251,8 +247,18 @@
|
|
| 251 |
{%- set ns_args.found_first = true -%}
|
| 252 |
{{- key -}}:{{- format_argument(value, escape_keys=False) -}}
|
| 253 |
{%- endfor -%}
|
|
|
|
| 254 |
{%- elif function['arguments'] is string -%}
|
| 255 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
{%- endif -%}
|
| 257 |
{{- '}<tool_call|>' -}}
|
| 258 |
{%- endfor -%}
|
|
@@ -262,8 +268,8 @@
|
|
| 262 |
{%- set ns_tr_out = namespace(flag=false) -%}
|
| 263 |
{%- if message.get('tool_responses') -%}
|
| 264 |
{#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
|
| 265 |
-
{%- for tool_response in message
|
| 266 |
-
{{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}}
|
| 267 |
{%- set ns_tr_out.flag = true -%}
|
| 268 |
{%- set ns.prev_message_type = 'tool_response' -%}
|
| 269 |
{%- endfor -%}
|
|
@@ -277,8 +283,8 @@
|
|
| 277 |
{%- else -%}
|
| 278 |
{%- set follow = loop_messages[k] -%}
|
| 279 |
{#- Resolve tool_call_id to function name -#}
|
| 280 |
-
{%- set ns_tname = namespace(name=follow.get('name')
|
| 281 |
-
{%- for tc in message
|
| 282 |
{%- if tc.get('id') == follow.get('tool_call_id') -%}
|
| 283 |
{%- set ns_tname.name = tc['function']['name'] -%}
|
| 284 |
{%- endif -%}
|
|
@@ -296,9 +302,9 @@
|
|
| 296 |
{%- endfor -%}
|
| 297 |
{{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
|
| 298 |
{%- for part in tool_body -%}
|
| 299 |
-
{%- if part.get('type')
|
| 300 |
{{- '<|image|>' -}}
|
| 301 |
-
{%- elif part.get('type')
|
| 302 |
{{- '<|audio|>' -}}
|
| 303 |
{%- elif part.get('type') == 'video' -%}
|
| 304 |
{{- '<|video|>' -}}
|
|
@@ -314,29 +320,26 @@
|
|
| 314 |
{%- endif -%}
|
| 315 |
|
| 316 |
{%- set captured_content -%}
|
| 317 |
-
{%- if message
|
| 318 |
{%- if role == 'model' -%}
|
| 319 |
{{- strip_thinking(message['content']) -}}
|
| 320 |
{%- else -%}
|
| 321 |
{{- message['content'] | trim -}}
|
| 322 |
{%- endif -%}
|
| 323 |
-
{%- elif message
|
| 324 |
{%- for item in message['content'] -%}
|
| 325 |
-
{%- if item
|
| 326 |
{%- if role == 'model' -%}
|
| 327 |
{{- strip_thinking(item['text']) -}}
|
| 328 |
{%- else -%}
|
| 329 |
{{- item['text'] | trim -}}
|
| 330 |
{%- endif -%}
|
| 331 |
-
{%- elif item
|
| 332 |
{{- '<|image|>' -}}
|
| 333 |
-
|
| 334 |
-
{%- elif item['type'] == 'audio' -%}
|
| 335 |
{{- '<|audio|>' -}}
|
| 336 |
-
|
| 337 |
-
{%- elif item['type'] == 'video' -%}
|
| 338 |
{{- '<|video|>' -}}
|
| 339 |
-
{%- set ns.prev_message_type = 'video' -%}
|
| 340 |
{%- endif -%}
|
| 341 |
{%- endfor -%}
|
| 342 |
{%- endif -%}
|
|
@@ -345,19 +348,42 @@
|
|
| 345 |
{{- captured_content -}}
|
| 346 |
{%- set has_content = captured_content | trim | length > 0 -%}
|
| 347 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
|
| 349 |
{{- '<|tool_response>' -}}
|
| 350 |
-
{%- elif
|
|
|
|
| 351 |
{{- '<turn|>\n' -}}
|
| 352 |
{%- endif -%}
|
|
|
|
|
|
|
|
|
|
| 353 |
{%- endif -%}
|
| 354 |
{%- endfor -%}
|
| 355 |
|
| 356 |
{%- if add_generation_prompt -%}
|
| 357 |
{%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
|
| 358 |
{{- '<|turn>model\n' -}}
|
| 359 |
-
{%- if not enable_thinking
|
| 360 |
{{- '<|channel>thought\n<channel|>' -}}
|
| 361 |
{%- endif -%}
|
|
|
|
|
|
|
| 362 |
{%- endif -%}
|
| 363 |
-
{%- 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' -}}
|
|
|
|
| 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 -%}
|
|
|
|
| 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 |
+
|
| 230 |
{%- endif -%}
|
| 231 |
|
| 232 |
{#- Render reasoning/reasoning_content as thinking channel -#}
|
| 233 |
{%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
|
| 234 |
+
{%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or (preserve_thinking and message.get('tool_calls')) -%}
|
| 235 |
+
{%- if thinking_text and thinking_gate -%}
|
| 236 |
{{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
|
| 237 |
{%- endif -%}
|
| 238 |
|
| 239 |
+
{%- if message.get('tool_calls') -%}
|
| 240 |
+
{%- for tool_call in message.get('tool_calls') -%}
|
| 241 |
{%- set function = tool_call['function'] -%}
|
| 242 |
{{- '<|tool_call>call:' + function['name'] + '{' -}}
|
| 243 |
{%- if function['arguments'] is mapping -%}
|
|
|
|
| 247 |
{%- set ns_args.found_first = true -%}
|
| 248 |
{{- key -}}:{{- format_argument(value, escape_keys=False) -}}
|
| 249 |
{%- endfor -%}
|
| 250 |
+
{%- elif function['arguments'] is none -%}
|
| 251 |
{%- elif function['arguments'] is string -%}
|
| 252 |
+
{#- Pre-serialized args (e.g. an OpenAI JSON string). We cannot JSON-parse
|
| 253 |
+
portably in-template, so render non-fatally instead of erroring. Strip an
|
| 254 |
+
outer {...} so it composes with the DSL braces rather than double-wrapping.
|
| 255 |
+
Prefer passing arguments as a mapping for exact Gemma DSL. -#}
|
| 256 |
+
{%- set argstr = function['arguments'] | trim -%}
|
| 257 |
+
{%- if argstr[:1] == '{' and argstr[-1:] == '}' -%}
|
| 258 |
+
{{- argstr[1:-1] -}}
|
| 259 |
+
{%- else -%}
|
| 260 |
+
{{- function['arguments'] -}}
|
| 261 |
+
{%- endif -%}
|
| 262 |
{%- endif -%}
|
| 263 |
{{- '}<tool_call|>' -}}
|
| 264 |
{%- endfor -%}
|
|
|
|
| 268 |
{%- set ns_tr_out = namespace(flag=false) -%}
|
| 269 |
{%- if message.get('tool_responses') -%}
|
| 270 |
{#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
|
| 271 |
+
{%- for tool_response in message.get('tool_responses') -%}
|
| 272 |
+
{{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}
|
| 273 |
{%- set ns_tr_out.flag = true -%}
|
| 274 |
{%- set ns.prev_message_type = 'tool_response' -%}
|
| 275 |
{%- endfor -%}
|
|
|
|
| 283 |
{%- else -%}
|
| 284 |
{%- set follow = loop_messages[k] -%}
|
| 285 |
{#- Resolve tool_call_id to function name -#}
|
| 286 |
+
{%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%}
|
| 287 |
+
{%- for tc in message.get('tool_calls') -%}
|
| 288 |
{%- if tc.get('id') == follow.get('tool_call_id') -%}
|
| 289 |
{%- set ns_tname.name = tc['function']['name'] -%}
|
| 290 |
{%- endif -%}
|
|
|
|
| 302 |
{%- endfor -%}
|
| 303 |
{{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
|
| 304 |
{%- for part in tool_body -%}
|
| 305 |
+
{%- if part.get('type') in ['image', 'image_url'] -%}
|
| 306 |
{{- '<|image|>' -}}
|
| 307 |
+
{%- elif part.get('type') in ['audio', 'input_audio'] -%}
|
| 308 |
{{- '<|audio|>' -}}
|
| 309 |
{%- elif part.get('type') == 'video' -%}
|
| 310 |
{{- '<|video|>' -}}
|
|
|
|
| 320 |
{%- endif -%}
|
| 321 |
|
| 322 |
{%- set captured_content -%}
|
| 323 |
+
{%- if message.get('content') is string -%}
|
| 324 |
{%- if role == 'model' -%}
|
| 325 |
{{- strip_thinking(message['content']) -}}
|
| 326 |
{%- else -%}
|
| 327 |
{{- message['content'] | trim -}}
|
| 328 |
{%- endif -%}
|
| 329 |
+
{%- elif message.get('content') is sequence -%}
|
| 330 |
{%- for item in message['content'] -%}
|
| 331 |
+
{%- if item.get('type') == 'text' -%}
|
| 332 |
{%- if role == 'model' -%}
|
| 333 |
{{- strip_thinking(item['text']) -}}
|
| 334 |
{%- else -%}
|
| 335 |
{{- item['text'] | trim -}}
|
| 336 |
{%- endif -%}
|
| 337 |
+
{%- elif item.get('type') in ['image', 'image_url'] -%}
|
| 338 |
{{- '<|image|>' -}}
|
| 339 |
+
{%- elif item.get('type') in ['audio', 'input_audio'] -%}
|
|
|
|
| 340 |
{{- '<|audio|>' -}}
|
| 341 |
+
{%- elif item.get('type') == 'video' -%}
|
|
|
|
| 342 |
{{- '<|video|>' -}}
|
|
|
|
| 343 |
{%- endif -%}
|
| 344 |
{%- endfor -%}
|
| 345 |
{%- endif -%}
|
|
|
|
| 348 |
{{- captured_content -}}
|
| 349 |
{%- set has_content = captured_content | trim | length > 0 -%}
|
| 350 |
|
| 351 |
+
{#- Forward-scan: find next non-tool message role for continuation detection -#}
|
| 352 |
+
{%- set next_nt = namespace(role=None, found=false) -%}
|
| 353 |
+
{%- for j in range(loop.index0 + 1, loop_messages | length) -%}
|
| 354 |
+
{%- if not next_nt.found -%}
|
| 355 |
+
{%- if loop_messages[j]['role'] != 'tool' -%}
|
| 356 |
+
{%- set next_nt.role = loop_messages[j]['role'] -%}
|
| 357 |
+
{%- set next_nt.found = true -%}
|
| 358 |
+
{%- endif -%}
|
| 359 |
+
{%- endif -%}
|
| 360 |
+
{%- endfor -%}
|
| 361 |
+
|
| 362 |
+
{%- set continues_into_next = (
|
| 363 |
+
role == 'model'
|
| 364 |
+
and next_nt.role == 'assistant'
|
| 365 |
+
and (not message.get('tool_calls') or ns_tr_out.flag)
|
| 366 |
+
) -%}
|
| 367 |
+
|
| 368 |
{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
|
| 369 |
{{- '<|tool_response>' -}}
|
| 370 |
+
{%- elif continues_into_next -%}
|
| 371 |
+
{%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%}
|
| 372 |
{{- '<turn|>\n' -}}
|
| 373 |
{%- endif -%}
|
| 374 |
+
|
| 375 |
+
{#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
|
| 376 |
+
{%- set ns.prev_non_tool_role = message['role'] -%}
|
| 377 |
{%- endif -%}
|
| 378 |
{%- endfor -%}
|
| 379 |
|
| 380 |
{%- if add_generation_prompt -%}
|
| 381 |
{%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
|
| 382 |
{{- '<|turn>model\n' -}}
|
| 383 |
+
{%- if not enable_thinking -%}
|
| 384 |
{{- '<|channel>thought\n<channel|>' -}}
|
| 385 |
{%- endif -%}
|
| 386 |
+
{%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%}
|
| 387 |
+
{{- '<|channel>thought\n' -}}
|
| 388 |
{%- endif -%}
|
| 389 |
+
{%- endif -%}
|
tokenizer_config.json
CHANGED
|
@@ -93,5 +93,5 @@
|
|
| 93 |
"think_token": "<|think|>",
|
| 94 |
"tokenizer_class": "GemmaTokenizer",
|
| 95 |
"unk_token": "<unk>",
|
| 96 |
-
"chat_template": "{%- macro format_parameters(properties, required, filter_keys=false) -%}\n {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in properties | dictsort -%}\n {%- set add_comma = false -%}\n {%- if not filter_keys or key not in standard_keys -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {{ key }}:{\n {%- if value['description'] -%}\n description:<|\"|>{{ value['description'] }}<|\"|>\n {%- set add_comma = true -%}\n {%- endif -%}\n {%- if value['type'] | upper == 'STRING' -%}\n {%- if value['enum'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n enum:{{ format_argument(value['enum']) }}\n {%- endif -%}\n {%- elif value['type'] | upper == 'ARRAY' -%}\n {%- if value['items'] is mapping and value['items'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n items:{\n {%- set ns_items = namespace(found_first=false) -%}\n {%- for item_key, item_value in value['items'] | dictsort -%}\n {%- if item_value is not none -%}\n {%- if ns_items.found_first %},{% endif -%}\n {%- set ns_items.found_first = true -%}\n {%- if item_key == 'properties' -%}\n properties:{\n {%- if item_value is mapping -%}\n {{- format_parameters(item_value, value['items']['required'] | default([])) -}}\n {%- endif -%}\n }\n {%- elif item_key == 'required' -%}\n required:[\n {%- for req_item in item_value -%}\n <|\"|>{{- req_item -}}<|\"|>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- elif item_key == 'type' -%}\n {%- if item_value is string -%}\n type:{{ format_argument(item_value | upper) }}\n {%- else -%}\n type:{{ format_argument(item_value | map('upper') | list) }}\n {%- endif -%}\n {%- else -%}\n {{ item_key }}:{{ format_argument(item_value) }}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n }\n {%- endif -%}\n {%- endif -%}\n {%- if value['nullable'] %}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n nullable:true\n {%- endif -%}\n {%- if value['type'] | upper == 'OBJECT' -%}\n {%- if value['properties'] is defined and value['properties'] is mapping -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n properties:{\n {{- format_parameters(value['properties'], value['required'] | default([])) -}}\n }\n {%- elif value is mapping -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n properties:{\n {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}\n }\n {%- endif -%}\n {%- if value['required'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n required:[\n {%- for item in value['required'] | default([]) -%}\n <|\"|>{{- item -}}<|\"|>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- endif -%}\n {%- endif -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n type:<|\"|>{{ value['type'] | upper }}<|\"|>}\n {%- endif -%}\n {%- endfor -%}\n{%- endmacro -%}\n{%- macro format_function_declaration(tool_data) -%}\n declaration:{{- tool_data['function']['name'] -}}{description:<|\"|>{{- tool_data['function']['description'] -}}<|\"|>\n {%- set params = tool_data['function']['parameters'] -%}\n {%- if params -%}\n ,parameters:{\n {%- if params['properties'] -%}\n properties:{ {{- format_parameters(params['properties'], params['required']) -}} },\n {%- endif -%}\n {%- if params['required'] -%}\n required:[\n {%- for item in params['required'] -%}\n <|\"|>{{- item -}}<|\"|>\n {{- ',' if not loop.last -}}\n {%- endfor -%}\n ],\n {%- endif -%}\n {%- if params['type'] -%}\n type:<|\"|>{{- params['type'] | upper -}}<|\"|>}\n {%- endif -%}\n {%- endif -%}\n {%- if 'response' in tool_data['function'] -%}\n {%- set response_declaration = tool_data['function']['response'] -%}\n ,response:{\n {%- if response_declaration['description'] -%}\n description:<|\"|>{{- response_declaration['description'] -}}<|\"|>,\n {%- endif -%}\n {%- if response_declaration['type'] | upper == 'OBJECT' -%}\n type:<|\"|>{{- response_declaration['type'] | upper -}}<|\"|>}\n {%- endif -%}\n {%- endif -%}\n }\n{%- endmacro -%}\n{%- macro format_argument(argument, escape_keys=True) -%}\n {%- if argument is string -%}\n {{- '<|\"|>' + argument + '<|\"|>' -}}\n {%- elif argument is boolean -%}\n {{- 'true' if argument else 'false' -}}\n {%- elif argument is mapping -%}\n {{- '{' -}}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in argument | dictsort -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {%- if escape_keys -%}\n {{- '<|\"|>' + key + '<|\"|>' -}}\n {%- else -%}\n {{- key -}}\n {%- endif -%}\n :{{- format_argument(value, escape_keys=escape_keys) -}}\n {%- endfor -%}\n {{- '}' -}}\n {%- elif argument is sequence -%}\n {{- '[' -}}\n {%- for item in argument -%}\n {{- format_argument(item, escape_keys=escape_keys) -}}\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n {{- ']' -}}\n {%- else -%}\n {{- argument -}}\n {%- endif -%}\n{%- endmacro -%}\n{%- macro strip_thinking(text) -%}\n {%- set ns = namespace(result='') -%}\n {%- for part in text.split('<channel|>') -%}\n {%- if '<|channel>' in part -%}\n {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}\n {%- else -%}\n {%- set ns.result = ns.result + part -%}\n {%- endif -%}\n {%- endfor -%}\n {{- ns.result | trim -}}\n{%- endmacro -%}\n\n{%- macro format_tool_response_block(tool_name, response) -%}\n {{- '<|tool_response>' -}}\n {%- if response is mapping -%}\n {{- 'response:' + tool_name + '{' -}}\n {%- for key, value in response | dictsort -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n {{- '}' -}}\n {%- else -%}\n {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}\n {%- endif -%}\n {{- '<tool_response|>' -}}\n{%- endmacro -%}\n\n{%- set ns = namespace(prev_message_type=None) -%}\n{%- set loop_messages = messages -%}\n{{- bos_token -}}\n{#- Handle System/Tool Definitions Block -#}\n{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}\n {{- '<|turn>system\\n' -}}\n {#- Inject Thinking token at the very top of the FIRST system turn -#}\n {%- if enable_thinking is defined and enable_thinking -%}\n {{- '<|think|>\\n' -}}\n {%- set ns.prev_message_type = 'think' -%}\n {%- endif -%}\n {%- if messages[0]['role'] in ['system', 'developer'] -%}\n {%- if messages[0]['content'] is string -%}\n {{- messages[0]['content'] | trim -}}\n {%- elif messages[0]['content'] is sequence -%}\n {%- for item in messages[0]['content'] -%}\n {{- item['text'] | trim + ' '-}}\n {%- endfor -%}\n {%- endif -%}\n {%- set loop_messages = messages[1:] -%}\n {%- endif -%}\n {%- if tools -%}\n {%- for tool in tools %}\n {{- '<|tool>' -}}\n {{- format_function_declaration(tool) | trim -}}\n {{- '<tool|>' -}}\n {%- endfor %}\n {%- set ns.prev_message_type = 'tool' -%}\n {%- endif -%}\n {{- '<turn|>\\n' -}}\n{%- endif %}\n\n{#- Pre-scan: find last user message index for reasoning guard -#}\n{%- set ns_turn = namespace(last_user_idx=-1) -%}\n{%- for i in range(loop_messages | length) -%}\n {%- if loop_messages[i]['role'] == 'user' -%}\n {%- set ns_turn.last_user_idx = i -%}\n {%- endif -%}\n{%- endfor -%}\n\n{#- Loop through messages -#}\n{%- for message in loop_messages -%}\n {%- if message['role'] != 'tool' -%}\n {%- set ns.prev_message_type = None -%}\n {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}\n {#- Detect continuation: suppress duplicate <|turn>model when previous non-tool message was also assistant -#}\n {%- set prev_nt = namespace(role=None, found=false) -%}\n {%- if loop.index0 > 0 -%}\n {%- for j in range(loop.index0 - 1, -1, -1) -%}\n {%- if not prev_nt.found -%}\n {%- if loop_messages[j]['role'] != 'tool' -%}\n {%- set prev_nt.role = loop_messages[j]['role'] -%}\n {%- set prev_nt.found = true -%}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant') -%}\n {%- if not continue_same_model_turn -%}\n {{- '<|turn>' + role + '\\n' }}\n {%- endif -%}\n\n {#- Render reasoning/reasoning_content as thinking channel -#}\n {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}\n {%- if thinking_text and loop.index0 > ns_turn.last_user_idx and message.get('tool_calls') -%}\n {{- '<|channel>thought\\n' + thinking_text + '\\n<channel|>' -}}\n {%- endif -%}\n\n {%- if message['tool_calls'] -%}\n {%- for tool_call in message['tool_calls'] -%}\n {%- set function = tool_call['function'] -%}\n {{- '<|tool_call>call:' + function['name'] + '{' -}}\n {%- if function['arguments'] is mapping -%}\n {%- set ns_args = namespace(found_first=false) -%}\n {%- for key, value in function['arguments'] | dictsort -%}\n {%- if ns_args.found_first %},{% endif -%}\n {%- set ns_args.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {%- elif function['arguments'] is string -%}\n {{- function['arguments'] -}}\n {%- endif -%}\n {{- '}<tool_call|>' -}}\n {%- endfor -%}\n {%- set ns.prev_message_type = 'tool_call' -%}\n {%- endif -%}\n\n {%- set ns_tr_out = namespace(flag=false) -%}\n {%- if message.get('tool_responses') -%}\n {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}\n {%- for tool_response in message['tool_responses'] -%}\n {{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}}\n {%- set ns_tr_out.flag = true -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endfor -%}\n {%- elif message.get('tool_calls') -%}\n {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}\n {%- set ns_tool_scan = namespace(stopped=false) -%}\n {%- for k in range(loop.index0 + 1, loop_messages | length) -%}\n {%- if ns_tool_scan.stopped -%}\n {%- elif loop_messages[k]['role'] != 'tool' -%}\n {%- set ns_tool_scan.stopped = true -%}\n {%- else -%}\n {%- set follow = loop_messages[k] -%}\n {#- Resolve tool_call_id to function name -#}\n {%- set ns_tname = namespace(name=follow.get('name') | default('unknown')) -%}\n {%- for tc in message['tool_calls'] -%}\n {%- if tc.get('id') == follow.get('tool_call_id') -%}\n {%- set ns_tname.name = tc['function']['name'] -%}\n {%- endif -%}\n {%- endfor -%}\n {#- Handle content as string or content-parts array -#}\n {%- set tool_body = follow.get('content') -%}\n {%- if tool_body is string -%}\n {{- format_tool_response_block(ns_tname.name, tool_body) -}}\n {%- elif tool_body is sequence and tool_body is not string -%}\n {%- set ns_txt = namespace(s='') -%}\n {%- for part in tool_body -%}\n {%- if part.get('type') == 'text' -%}\n {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}\n {%- endif -%}\n {%- endfor -%}\n {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}\n {%- for part in tool_body -%}\n {%- if part.get('type') == 'image' -%}\n {{- '<|image|>' -}}\n {%- elif part.get('type') == 'audio' -%}\n {{- '<|audio|>' -}}\n {%- elif part.get('type') == 'video' -%}\n {{- '<|video|>' -}}\n {%- endif -%}\n {%- endfor -%}\n {%- else -%}\n {{- format_tool_response_block(ns_tname.name, tool_body) -}}\n {%- endif -%}\n {%- set ns_tr_out.flag = true -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n\n {%- set captured_content -%}\n {%- if message['content'] is string -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(message['content']) -}}\n {%- else -%}\n {{- message['content'] | trim -}}\n {%- endif -%}\n {%- elif message['content'] is sequence -%}\n {%- for item in message['content'] -%}\n {%- if item['type'] == 'text' -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(item['text']) -}}\n {%- else -%}\n {{- item['text'] | trim -}}\n {%- endif -%}\n {%- elif item['type'] == 'image' -%}\n {{- '<|image|>' -}}\n {%- set ns.prev_message_type = 'image' -%}\n {%- elif item['type'] == 'audio' -%}\n {{- '<|audio|>' -}}\n {%- set ns.prev_message_type = 'audio' -%}\n {%- elif item['type'] == 'video' -%}\n {{- '<|video|>' -}}\n {%- set ns.prev_message_type = 'video' -%}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- endset -%}\n\n {{- captured_content -}}\n {%- set has_content = captured_content | trim | length > 0 -%}\n\n {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}\n {{- '<|tool_response>' -}}\n {%- elif not (ns_tr_out.flag and not has_content) -%}\n {{- '<turn|>\\n' -}}\n {%- endif -%}\n {%- endif -%}\n{%- endfor -%}\n\n{%- if add_generation_prompt -%}\n {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}\n {{- '<|turn>model\\n' -}}\n {%- if not enable_thinking | default(false) -%}\n {{- '<|channel>thought\\n<channel|>' -}}\n {%- endif -%}\n {%- endif -%}\n{%- endif -%}"
|
| 97 |
}
|
|
|
|
| 93 |
"think_token": "<|think|>",
|
| 94 |
"tokenizer_class": "GemmaTokenizer",
|
| 95 |
"unk_token": "<unk>",
|
| 96 |
+
"chat_template": "{%- macro format_parameters(properties, required, filter_keys=false) -%}\n {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in properties | dictsort -%}\n {%- set add_comma = false -%}\n {%- if not filter_keys or key not in standard_keys -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {{ key }}:{\n {%- if value['description'] -%}\n description:<|\"|>{{ value['description'] }}<|\"|>\n {%- set add_comma = true -%}\n {%- endif -%}\n {%- if value['type'] | upper == 'STRING' -%}\n {%- if value['enum'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n enum:{{ format_argument(value['enum']) }}\n {%- endif -%}\n {%- elif value['type'] | upper == 'ARRAY' -%}\n {%- if value['items'] is mapping and value['items'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n items:{\n {%- set ns_items = namespace(found_first=false) -%}\n {%- for item_key, item_value in value['items'] | dictsort -%}\n {%- if item_value is not none -%}\n {%- if ns_items.found_first %},{% endif -%}\n {%- set ns_items.found_first = true -%}\n {%- if item_key == 'properties' -%}\n properties:{\n {%- if item_value is mapping -%}\n {{- format_parameters(item_value, value['items']['required'] | default([])) -}}\n {%- endif -%}\n }\n {%- elif item_key == 'required' -%}\n required:[\n {%- for req_item in item_value -%}\n <|\"|>{{- req_item -}}<|\"|>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- elif item_key == 'type' -%}\n {%- if item_value is string -%}\n type:{{ format_argument(item_value | upper) }}\n {%- else -%}\n type:{{ format_argument(item_value | map('upper') | list) }}\n {%- endif -%}\n {%- else -%}\n {{ item_key }}:{{ format_argument(item_value) }}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n }\n {%- endif -%}\n {%- endif -%}\n {%- if value['nullable'] %}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n nullable:true\n {%- endif -%}\n {%- if value['type'] | upper == 'OBJECT' -%}\n {%- if value['properties'] is defined and value['properties'] is mapping -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n properties:{\n {{- format_parameters(value['properties'], value['required'] | default([])) -}}\n }\n {%- elif value is mapping -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n properties:{\n {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}\n }\n {%- endif -%}\n {%- if value['required'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n required:[\n {%- for item in value['required'] | default([]) -%}\n <|\"|>{{- item -}}<|\"|>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- endif -%}\n {%- endif -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n type:<|\"|>{{ value['type'] | upper }}<|\"|>}\n {%- endif -%}\n {%- endfor -%}\n{%- endmacro -%}\n{%- macro format_function_declaration(tool_data) -%}\n declaration:{{- tool_data['function']['name'] -}}{description:<|\"|>{{- tool_data['function']['description'] -}}<|\"|>\n {%- set params = tool_data['function']['parameters'] -%}\n {%- if params -%}\n ,parameters:{\n {%- if params['properties'] -%}\n properties:{ {{- format_parameters(params['properties'], params['required']) -}} },\n {%- endif -%}\n {%- if params['required'] -%}\n required:[\n {%- for item in params['required'] -%}\n <|\"|>{{- item -}}<|\"|>\n {{- ',' if not loop.last -}}\n {%- endfor -%}\n ],\n {%- endif -%}\n {%- if params['type'] -%}\n type:<|\"|>{{- params['type'] | upper -}}<|\"|>}\n {%- endif -%}\n {%- endif -%}\n {%- if 'response' in tool_data['function'] -%}\n {%- set response_declaration = tool_data['function']['response'] -%}\n ,response:{\n {%- if response_declaration['description'] -%}\n description:<|\"|>{{- response_declaration['description'] -}}<|\"|>,\n {%- endif -%}\n {%- if response_declaration['type'] | upper == 'OBJECT' -%}\n type:<|\"|>{{- response_declaration['type'] | upper -}}<|\"|>}\n {%- endif -%}\n {%- endif -%}\n }\n{%- endmacro -%}\n{%- macro format_argument(argument, escape_keys=True) -%}\n {%- if argument is none -%}\n {{- 'null' -}}\n {%- elif argument is string -%}\n {{- '<|\"|>' + argument + '<|\"|>' -}}\n {%- elif argument is boolean -%}\n {{- 'true' if argument else 'false' -}}\n {%- elif argument is mapping -%}\n {{- '{' -}}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in argument | dictsort -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {%- if escape_keys -%}\n {{- '<|\"|>' + key + '<|\"|>' -}}\n {%- else -%}\n {{- key -}}\n {%- endif -%}\n :{{- format_argument(value, escape_keys=escape_keys) -}}\n {%- endfor -%}\n {{- '}' -}}\n {%- elif argument is sequence -%}\n {{- '[' -}}\n {%- for item in argument -%}\n {{- format_argument(item, escape_keys=escape_keys) -}}\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n {{- ']' -}}\n {%- else -%}\n {{- argument -}}\n {%- endif -%}\n{%- endmacro -%}\n{%- macro strip_thinking(text) -%}\n {%- set ns = namespace(result='') -%}\n {%- for part in text.split('<channel|>') -%}\n {%- if '<|channel>' in part -%}\n {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}\n {%- else -%}\n {%- set ns.result = ns.result + part -%}\n {%- endif -%}\n {%- endfor -%}\n {{- ns.result | trim -}}\n{%- endmacro -%}\n\n{%- macro format_tool_response_block(tool_name, response) -%}\n {{- '<|tool_response>' -}}\n {%- if response is mapping -%}\n {{- 'response:' + tool_name + '{' -}}\n {%- for key, value in response | dictsort -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n {{- '}' -}}\n {%- else -%}\n {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}\n {%- endif -%}\n {{- '<tool_response|>' -}}\n{%- endmacro -%}\n\n{#- ===== SETUP ===== -#}\n{%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}\n{%- set loop_messages = messages -%}\n{%- set enable_thinking = enable_thinking | default(false) -%}\n{%- set preserve_thinking = preserve_thinking | default(false) -%}\n{{- bos_token -}}\n{#- Handle System/Tool Definitions Block -#}\n{%- if enable_thinking or tools or (messages and messages[0]['role'] in ['system', 'developer']) -%}\n {{- '<|turn>system\\n' -}}\n {#- Inject Thinking token at the very top of the FIRST system turn -#}\n {%- if enable_thinking -%}\n {{- '<|think|>\\n' -}}\n {%- set ns.prev_message_type = 'think' -%}\n {%- endif -%}\n {%- if messages and messages[0]['role'] in ['system', 'developer'] -%}\n {%- if messages[0]['content'] is string -%}\n {{- messages[0]['content'] | trim -}}\n {%- elif messages[0]['content'] is sequence -%}\n {%- for item in messages[0]['content'] -%}\n {{- item['text'] | trim + ' '-}}\n {%- endfor -%}\n {%- endif -%}\n {%- set loop_messages = messages[1:] -%}\n {%- endif -%}\n {%- if tools -%}\n {%- for tool in tools %}\n {{- '<|tool>' -}}\n {{- format_function_declaration(tool) | trim -}}\n {{- '<tool|>' -}}\n {%- endfor %}\n {%- set ns.prev_message_type = 'tool' -%}\n {%- endif -%}\n {{- '<turn|>\\n' -}}\n{%- endif %}\n\n{#- Pre-scan: find last user message index for reasoning guard -#}\n{%- set ns_turn = namespace(last_user_idx=-1) -%}\n{%- for i in range(loop_messages | length) -%}\n {%- if loop_messages[i]['role'] == 'user' -%}\n {%- set ns_turn.last_user_idx = i -%}\n {%- endif -%}\n{%- endfor -%}\n\n{#- Loop through messages -#}\n{%- for message in loop_messages -%}\n {%- if message['role'] != 'tool' -%}\n {%- set ns.prev_message_type = None -%}\n {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}\n {#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#}\n {%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%}\n {%- if not continue_same_model_turn -%}\n {{- '<|turn>' + role + '\\n' }}\n\n {%- endif -%}\n\n {#- Render reasoning/reasoning_content as thinking channel -#}\n {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}\n {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or (preserve_thinking and message.get('tool_calls')) -%}\n {%- if thinking_text and thinking_gate -%}\n {{- '<|channel>thought\\n' + thinking_text + '\\n<channel|>' -}}\n {%- endif -%}\n\n {%- if message.get('tool_calls') -%}\n {%- for tool_call in message.get('tool_calls') -%}\n {%- set function = tool_call['function'] -%}\n {{- '<|tool_call>call:' + function['name'] + '{' -}}\n {%- if function['arguments'] is mapping -%}\n {%- set ns_args = namespace(found_first=false) -%}\n {%- for key, value in function['arguments'] | dictsort -%}\n {%- if ns_args.found_first %},{% endif -%}\n {%- set ns_args.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {%- elif function['arguments'] is none -%}\n {%- elif function['arguments'] is string -%}\n {#- Pre-serialized args (e.g. an OpenAI JSON string). We cannot JSON-parse\n portably in-template, so render non-fatally instead of erroring. Strip an\n outer {...} so it composes with the DSL braces rather than double-wrapping.\n Prefer passing arguments as a mapping for exact Gemma DSL. -#}\n {%- set argstr = function['arguments'] | trim -%}\n {%- if argstr[:1] == '{' and argstr[-1:] == '}' -%}\n {{- argstr[1:-1] -}}\n {%- else -%}\n {{- function['arguments'] -}}\n {%- endif -%}\n {%- endif -%}\n {{- '}<tool_call|>' -}}\n {%- endfor -%}\n {%- set ns.prev_message_type = 'tool_call' -%}\n {%- endif -%}\n\n {%- set ns_tr_out = namespace(flag=false) -%}\n {%- if message.get('tool_responses') -%}\n {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}\n {%- for tool_response in message.get('tool_responses') -%}\n {{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}\n {%- set ns_tr_out.flag = true -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endfor -%}\n {%- elif message.get('tool_calls') -%}\n {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}\n {%- set ns_tool_scan = namespace(stopped=false) -%}\n {%- for k in range(loop.index0 + 1, loop_messages | length) -%}\n {%- if ns_tool_scan.stopped -%}\n {%- elif loop_messages[k]['role'] != 'tool' -%}\n {%- set ns_tool_scan.stopped = true -%}\n {%- else -%}\n {%- set follow = loop_messages[k] -%}\n {#- Resolve tool_call_id to function name -#}\n {%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%}\n {%- for tc in message.get('tool_calls') -%}\n {%- if tc.get('id') == follow.get('tool_call_id') -%}\n {%- set ns_tname.name = tc['function']['name'] -%}\n {%- endif -%}\n {%- endfor -%}\n {#- Handle content as string or content-parts array -#}\n {%- set tool_body = follow.get('content') -%}\n {%- if tool_body is string -%}\n {{- format_tool_response_block(ns_tname.name, tool_body) -}}\n {%- elif tool_body is sequence and tool_body is not string -%}\n {%- set ns_txt = namespace(s='') -%}\n {%- for part in tool_body -%}\n {%- if part.get('type') == 'text' -%}\n {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}\n {%- endif -%}\n {%- endfor -%}\n {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}\n {%- for part in tool_body -%}\n {%- if part.get('type') in ['image', 'image_url'] -%}\n {{- '<|image|>' -}}\n {%- elif part.get('type') in ['audio', 'input_audio'] -%}\n {{- '<|audio|>' -}}\n {%- elif part.get('type') == 'video' -%}\n {{- '<|video|>' -}}\n {%- endif -%}\n {%- endfor -%}\n {%- else -%}\n {{- format_tool_response_block(ns_tname.name, tool_body) -}}\n {%- endif -%}\n {%- set ns_tr_out.flag = true -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n\n {%- set captured_content -%}\n {%- if message.get('content') is string -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(message['content']) -}}\n {%- else -%}\n {{- message['content'] | trim -}}\n {%- endif -%}\n {%- elif message.get('content') is sequence -%}\n {%- for item in message['content'] -%}\n {%- if item.get('type') == 'text' -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(item['text']) -}}\n {%- else -%}\n {{- item['text'] | trim -}}\n {%- endif -%}\n {%- elif item.get('type') in ['image', 'image_url'] -%}\n {{- '<|image|>' -}}\n {%- elif item.get('type') in ['audio', 'input_audio'] -%}\n {{- '<|audio|>' -}}\n {%- elif item.get('type') == 'video' -%}\n {{- '<|video|>' -}}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- endset -%}\n\n {{- captured_content -}}\n {%- set has_content = captured_content | trim | length > 0 -%}\n\n {#- Forward-scan: find next non-tool message role for continuation detection -#}\n {%- set next_nt = namespace(role=None, found=false) -%}\n {%- for j in range(loop.index0 + 1, loop_messages | length) -%}\n {%- if not next_nt.found -%}\n {%- if loop_messages[j]['role'] != 'tool' -%}\n {%- set next_nt.role = loop_messages[j]['role'] -%}\n {%- set next_nt.found = true -%}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n\n {%- set continues_into_next = (\n role == 'model'\n and next_nt.role == 'assistant'\n and (not message.get('tool_calls') or ns_tr_out.flag)\n ) -%}\n\n {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}\n {{- '<|tool_response>' -}}\n {%- elif continues_into_next -%}\n {%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%}\n {{- '<turn|>\\n' -}}\n {%- endif -%}\n\n {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}\n {%- set ns.prev_non_tool_role = message['role'] -%}\n {%- endif -%}\n{%- endfor -%}\n\n{%- if add_generation_prompt -%}\n {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}\n {{- '<|turn>model\\n' -}}\n {%- if not enable_thinking -%}\n {{- '<|channel>thought\\n<channel|>' -}}\n {%- endif -%}\n {%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%}\n {{- '<|channel>thought\\n' -}}\n {%- endif -%}\n{%- endif -%}\n"
|
| 97 |
}
|