pragmaticcs commited on
Commit
657c849
·
verified ·
1 Parent(s): 9773299

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +71 -90
chat_template.jinja CHANGED
@@ -1,20 +1,20 @@
1
- {%- set template_version = "qwen3.8-froggeric-v22.5.1-jack" %}
2
- {%- set _tool_format = tool_call_format if tool_call_format is defined else 'xml' %}
3
- {%- set image_count = namespace(value=0) %}
4
- {%- set video_count = namespace(value=0) %}
5
- {%- set add_vision_id = (add_vision_id in (true, 'true', 'True', 1, '1', 'on')) if add_vision_id is defined else false %}
6
  {%- set enable_thinking = (enable_thinking not in (false, 'false', 'False', 0, '0', 'off', 'none')) if enable_thinking is defined else true %}
7
  {%- set auto_disable_thinking_with_tools = (auto_disable_thinking_with_tools in (true, 'true', 'True', 1, '1', 'on')) if auto_disable_thinking_with_tools is defined else false %}
 
8
  {%- if preserve_reasoning is defined and preserve_reasoning is not none %}
9
- {%- set _preserve_thinking = (preserve_reasoning not in (false, 'false', 'False', 0, '0', 'off', 'none')) %}
10
  {%- elif preserve_thinking is defined and preserve_thinking is not none %}
11
- {%- set _preserve_thinking = (preserve_thinking not in (false, 'false', 'False', 0, '0', 'off', 'none')) %}
12
  {%- else %}
13
- {%- set _preserve_thinking = true %}
14
  {%- endif %}
 
15
  {%- set max_tool_arg_chars = (max_tool_arg_chars | int) if (max_tool_arg_chars is defined and max_tool_arg_chars is not none) else 0 %}
16
  {%- set max_tool_response_chars = (max_tool_response_chars | int) if (max_tool_response_chars is defined and max_tool_response_chars is not none) else 0 %}
17
  {%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}
 
18
  {%- set _effort_raw = (reasoning_effort | string | lower) if reasoning_effort is defined and reasoning_effort is not none else 'medium' %}
19
  {%- set _initial_thinking = enable_thinking %}
20
  {%- set _initial_effort = 'medium' %}
@@ -24,14 +24,17 @@
24
  {%- elif _effort_raw in ('minimal', 'low') %}
25
  {%- set _initial_effort = 'low' %}
26
  {%- elif _effort_raw in ('high', 'xhigh', 'max', 'ultracode', 'extreme', 'deep', 'long') %}
 
27
  {%- set _initial_effort = 'xhigh' %}
28
  {%- else %}
29
  {%- set _initial_effort = 'medium' %}
30
  {%- endif %}
 
31
  {%- set ns_state = namespace(thinking=_initial_thinking, effort=_initial_effort) %}
32
  {%- if auto_disable_thinking_with_tools and _has_tools %}
33
  {%- set ns_state.thinking = false %}
34
  {%- endif %}
 
35
  {%- for msg in messages %}
36
  {%- if msg.role == 'system' or msg.role == 'developer' or msg.role == 'user' %}
37
  {%- if msg.content is string %}
@@ -80,42 +83,26 @@
80
  {%- endif %}
81
  {%- endif %}
82
  {%- endfor %}
 
83
  {%- set reasoning_instructions = '' %}
84
  {%- if ns_state.thinking %}
85
  {%- if ns_state.effort == 'xhigh' %}
86
- {%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer.' %}
87
  {%- elif ns_state.effort == 'low' %}
88
- {%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.' %}
89
  {%- endif %}
90
  {%- endif %}
91
- {%- macro render_content(content, do_vision_count, is_system_content=false) %}
 
92
  {%- if content is string %}
93
  {{- content }}
94
  {%- elif content is iterable and content is not mapping %}
95
  {%- for item in content %}
96
  {%- if item is mapping %}
97
  {%- if item.type == 'image' or 'image' in item or 'image_url' in item %}
98
- {%- if is_system_content %}
99
- {{- raise_exception('System message cannot contain images.') }}
100
- {%- endif %}
101
- {%- if do_vision_count %}
102
- {%- set image_count.value = image_count.value + 1 %}
103
- {%- endif %}
104
- {%- if add_vision_id %}
105
- {{- 'Picture ' ~ image_count.value ~ ': ' }}
106
- {%- endif %}
107
- {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
108
  {%- elif item.type == 'video' or 'video' in item %}
109
- {%- if is_system_content %}
110
- {{- raise_exception('System message cannot contain videos.') }}
111
- {%- endif %}
112
- {%- if do_vision_count %}
113
- {%- set video_count.value = video_count.value + 1 %}
114
- {%- endif %}
115
- {%- if add_vision_id %}
116
- {{- 'Video ' ~ video_count.value ~ ': ' }}
117
- {%- endif %}
118
- {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
119
  {%- elif 'text' in item %}
120
  {{- (item.text | string) if item.text is not none else '' }}
121
  {%- elif 'content' in item and item.content is string %}
@@ -133,9 +120,11 @@
133
  {{- raise_exception('Unexpected content type.') }}
134
  {%- endif %}
135
  {%- endmacro %}
 
136
  {%- if not messages %}
137
  {{- raise_exception('No messages provided.') }}
138
  {%- endif %}
 
139
  {%- set head = namespace(count=0, seen_non_system=false) %}
140
  {%- for message in messages %}
141
  {%- set _is_sys = (message.role == 'system' or message.role == 'developer') %}
@@ -145,9 +134,10 @@
145
  {%- set head.seen_non_system = true %}
146
  {%- endif %}
147
  {%- endfor %}
 
148
  {%- set sys_state = namespace(content='') %}
149
  {%- for message in messages[:head.count] %}
150
- {%- set _part = render_content(message.content, false, true) | trim %}
151
  {%- if '<|think_off|>' in _part %}{%- set _part = _part.split('<|think_off|>') | join('') | trim %}{%- endif %}
152
  {%- if '<|think_on|>' in _part %}{%- set _part = _part.split('<|think_on|>') | join('') | trim %}{%- endif %}
153
  {%- if '<|think_xhigh|>' in _part %}{%- set _part = _part.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}
@@ -168,8 +158,10 @@
168
  {%- endif %}
169
  {%- endif %}
170
  {%- endfor %}
 
171
  {%- set _sc = sys_state.content %}
172
  {%- set _msgs = messages[head.count:] %}
 
173
  {%- set _terse_on = (terse in (true, 'true', 'True', 1, '1', 'on')) if terse is defined else false %}
174
  {%- if _terse_on %}
175
  {%- if ns_state.thinking %}
@@ -178,9 +170,9 @@
178
  {%- set _terse_lead = 'Answer directly and concisely. Give the answer with only what it needs to be correct and usable.' %}
179
  {%- endif %}
180
  {%- set _terse_core %}
181
- Never: open with preamble or pleasantries; restate the question; add filler transitions; hedge with niceties; or repeat a point you've already made.
182
  Always: keep essential steps, caveats, uncertainties, and specifics — never drop correctness or a needed warning for brevity. Keep the final answer lean. Use the least structure that conveys it (plain prose when short; lists or code only when they earn their place). If genuinely uncertain, say so and explain why — never omit uncertainty for the sake of brevity.
183
- If a user request is genuinely ambiguous, ask a sharp question, don't guess.
184
  {%- endset %}
185
  {%- set _terse = _terse_lead ~ '\n' ~ (_terse_core | trim) %}
186
  {%- if not _sc %}
@@ -189,6 +181,7 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
189
  {%- set _sc = (_sc | trim) ~ '\n\n' ~ (_terse | trim) %}
190
  {%- endif %}
191
  {%- endif %}
 
192
  {%- if _has_tools %}
193
  {{- '<|im_start|>system\n' }}
194
  {%- if reasoning_instructions %}
@@ -201,9 +194,9 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
201
  {%- endfor %}
202
  {{- '\n</tools>' }}
203
  {%- if _tool_format == 'json' %}
204
- {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}{%- if ns_state.thinking %}{{- '<think>\nBrief explanation of tool call\n</think>\n' }}{%- endif %}{{- '<tool_call>\n{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "This is the value for the second parameter"}}\n</tool_call>\n\n<IMPORTANT>\nReminder:\n' }}{%- if ns_state.thinking %}{{- '- You can use the <think></think> block to plan your next tool call OR to synthesize data and formulate your final response to the user.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n' }}{%- endif %}{{- '- Function calls MUST follow the specified format: a single JSON object with "name" and "arguments" keys inside <tool_call></tool_call> XML tags.\n- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY' }}{%- if ns_state.thinking %}{{- ' after thinking' }}{%- endif %}{{- ', with NO conversational text before it.\n- The <tool_call> tag MUST be at the very beginning of a new line, with NO spaces or indentation before it.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}
205
  {%- else %}
206
- {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}{%- if ns_state.thinking %}{{- '<think>\nBrief explanation of tool call\n</think>\n' }}{%- endif %}{{- '<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n' }}{%- if ns_state.thinking %}{{- '- You can use the <think></think> block to plan your next tool call OR to synthesize data and formulate your final response to the user.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n' }}{%- endif %}{{- '- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.\n- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY' }}{%- if ns_state.thinking %}{{- ' after thinking' }}{%- endif %}{{- ', with NO conversational text before it.\n- The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}
207
  {%- endif %}
208
  {%- if _sc %}
209
  {{- '\n\n' + _sc }}
@@ -216,29 +209,23 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
216
  {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}
217
  {%- endif %}
218
  {%- endif %}
219
- {%- set _last_idx = _msgs | length - 1 %}
220
- {%- set ns = namespace(multi_step_tool=true, last_query_index=_last_idx) %}
221
  {%- for message in _msgs[::-1] %}
222
  {%- set index = (_msgs | length - 1) - loop.index0 %}
223
- {%- if ns.multi_step_tool and message.role == 'user' %}
224
- {%- set _rc = render_content(message.content, false) | trim %}
225
  {%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}
226
- {%- set ns.multi_step_tool = false %}
227
  {%- set ns.last_query_index = index %}
228
  {%- endif %}
229
  {%- endif %}
230
  {%- endfor %}
231
- {%- if ns.multi_step_tool %}
232
- {%- if _last_idx > 50 %}
233
- {%- set ns.last_query_index = _last_idx %}
234
- {%- else %}
235
- {%- set ns.last_query_index = 0 %}
236
- {%- endif %}
237
- {%- endif %}
238
  {%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}
239
  {%- for message in _msgs %}
240
  {%- set is_system = (message.role == "system" or message.role == "developer") %}
241
- {%- set content = render_content(message.content, true, is_system) | trim %}
242
  {%- if is_system or message.role == 'user' %}
243
  {%- if '<|think_off|>' in content %}{%- set content = content.split('<|think_off|>') | join('') | trim %}{%- endif %}
244
  {%- if '<|think_on|>' in content %}{%- set content = content.split('<|think_on|>') | join('') | trim %}{%- endif %}
@@ -288,13 +275,9 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
288
  {%- endif %}
289
  {%- if _explicit_reasoning %}
290
  {%- set _lead_end = '' %}
291
- {%- if content.startswith('<think>') and '</think>' in content %}
292
  {%- set _lead_end = '</think>' %}
293
- {%- elif content.startswith('<thinking>') and '</thinking>' in content %}
294
- {%- set _lead_end = '</thinking>' %}
295
- {%- elif content.startswith('</think>') %}
296
- {%- set _lead_end = '</think>' %}
297
- {%- elif content.startswith('</thinking>') %}
298
  {%- set _lead_end = '</thinking>' %}
299
  {%- endif %}
300
  {%- if _lead_end %}
@@ -304,29 +287,13 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
304
  {%- set reasoning_content = _explicit_reasoning %}
305
  {%- else %}
306
  {%- set _think_end = '' %}
307
- {%- if content.startswith('</think>') %}
308
- {%- set _think_end = '</think>' %}
309
- {%- elif content.startswith('</thinking>') %}
310
- {%- set _think_end = '</thinking>' %}
311
- {%- elif '\n</think>' in content %}
312
- {%- set _think_end = '\n</think>' %}
313
- {%- elif '\n</thinking>' in content %}
314
- {%- set _think_end = '\n</thinking>' %}
315
- {%- elif '\n</ think>' in content %}
316
- {%- set _think_end = '\n</ think>' %}
317
- {%- elif '\n</think >' in content %}
318
- {%- set _think_end = '\n</think >' %}
319
- {%- elif content.startswith('<think>') and '</think>' in content %}
320
  {%- set _think_end = '</think>' %}
321
- {%- elif content.startswith('<thinking>') and '</thinking>' in content %}
322
  {%- set _think_end = '</thinking>' %}
323
  {%- endif %}
324
  {%- if _think_end %}
325
- {%- if 'thinking' in _think_end %}
326
- {%- set _think_start = '<thinking>' %}
327
- {%- else %}
328
- {%- set _think_start = '<think>' %}
329
- {%- endif %}
330
  {%- set _parts = content.split(_think_end) %}
331
  {%- set reasoning_content = _parts[0].rstrip('\n') %}
332
  {%- if _think_start in reasoning_content %}
@@ -341,7 +308,7 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
341
  {%- else %}
342
  {{- '<|im_start|>assistant\n' + content }}
343
  {%- endif %}
344
- {%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
345
  {%- for tool_call in message.tool_calls %}
346
  {%- if tool_call.function is defined and tool_call.function is not none %}
347
  {%- set tc = tool_call.function %}
@@ -349,6 +316,14 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
349
  {%- set tc = tool_call %}
350
  {%- endif %}
351
  {%- set tc_name = tc.name if (tc.name is defined and tc.name is not none) else '' %}
 
 
 
 
 
 
 
 
352
  {%- if _tool_format == 'json' %}
353
  {%- if loop.first %}
354
  {%- if content | trim %}
@@ -358,15 +333,20 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
358
  {{- '\n' }}
359
  {%- endif %}
360
  {%- set _args = '{}' %}
361
- {%- if tc.arguments is defined and tc.arguments is not none %}
362
- {%- if tc.arguments is mapping %}
363
- {%- set _args = tc.arguments | tojson %}
364
- {%- elif tc.arguments is string %}
365
- {%- if tc.arguments %}
366
- {%- set _args = tc.arguments %}
 
 
 
 
 
367
  {%- endif %}
368
  {%- else %}
369
- {%- set _args = tc.arguments | tojson %}
370
  {%- endif %}
371
  {%- endif %}
372
  {{- '<tool_call>\n{"name": ' }}{{- tc_name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }}
@@ -380,9 +360,9 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
380
  {%- else %}
381
  {{- '\n<tool_call>\n<function=' + tc_name + '>\n' }}
382
  {%- endif %}
383
- {%- if tc.arguments is defined and tc.arguments is not none %}
384
- {%- if tc.arguments is mapping %}
385
- {%- for args_name, args_value in tc.arguments.items() %}
386
  {{- '<parameter=' + args_name + '>\n' }}
387
  {%- if args_value is string %}
388
  {%- set _av = args_value %}
@@ -397,10 +377,10 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
397
  {{- '\n</parameter>\n' }}
398
  {%- endfor %}
399
  {%- else %}
400
- {%- if tc.arguments is string %}
401
- {%- set _raw_args = tc.arguments %}
402
  {%- else %}
403
- {%- set _raw_args = tc.arguments | tojson %}
404
  {%- endif %}
405
  {%- if _raw_args %}
406
  {%- if max_tool_arg_chars > 0 and _raw_args | length > max_tool_arg_chars %}
@@ -438,7 +418,7 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
438
  {%- endif %}
439
  {{- '\n<tool_response>\n' + content }}
440
  {%- if ns2.consecutive_failures >= 2 %}
441
- {{- '\n\n⚠️ SYSTEM WARNING: ' ~ ns2.consecutive_failures ~ ' consecutive tool errors detected. Your previous approach is incorrect. You MUST use a fundamentally different approach or corrected arguments.' }}
442
  {%- elif ns2.consecutive_failures == 1 %}
443
  {{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with completely corrected arguments.' }}
444
  {%- endif %}
@@ -456,6 +436,7 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
456
  {%- endif %}
457
  {%- set ns2.prev_role = message.role %}
458
  {%- endfor %}
 
459
  {%- set _add_gen = (add_generation_prompt in (true, 'true', 'True', 1, '1', 'on')) if add_generation_prompt is defined else false %}
460
  {%- if _add_gen %}
461
  {{- '<|im_start|>assistant\n' }}
@@ -464,4 +445,4 @@ If a user request is genuinely ambiguous, ask a sharp question, don't guess.
464
  {%- else %}
465
  {{- '<think>\n' }}
466
  {%- endif %}
467
- {%- endif %}
 
1
+ {%- set template_version = "qwen3.5-3.6-kat-qwopus-agentworld-ornith-merged-v2" %}
2
+ {%- set _tool_format = tool_call_format if tool_call_format is defined else 'qwen3_coder' %}
 
 
 
3
  {%- set enable_thinking = (enable_thinking not in (false, 'false', 'False', 0, '0', 'off', 'none')) if enable_thinking is defined else true %}
4
  {%- set auto_disable_thinking_with_tools = (auto_disable_thinking_with_tools in (true, 'true', 'True', 1, '1', 'on')) if auto_disable_thinking_with_tools is defined else false %}
5
+
6
  {%- if preserve_reasoning is defined and preserve_reasoning is not none %}
7
+ {%- set _preserve_thinking = (preserve_reasoning not in (false, 'false', 'False', 0, '0', 'off', 'none')) %}
8
  {%- elif preserve_thinking is defined and preserve_thinking is not none %}
9
+ {%- set _preserve_thinking = (preserve_thinking not in (false, 'false', 'False', 0, '0', 'off', 'none')) %}
10
  {%- else %}
11
+ {%- set _preserve_thinking = true %}
12
  {%- endif %}
13
+
14
  {%- set max_tool_arg_chars = (max_tool_arg_chars | int) if (max_tool_arg_chars is defined and max_tool_arg_chars is not none) else 0 %}
15
  {%- set max_tool_response_chars = (max_tool_response_chars | int) if (max_tool_response_chars is defined and max_tool_response_chars is not none) else 0 %}
16
  {%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}
17
+
18
  {%- set _effort_raw = (reasoning_effort | string | lower) if reasoning_effort is defined and reasoning_effort is not none else 'medium' %}
19
  {%- set _initial_thinking = enable_thinking %}
20
  {%- set _initial_effort = 'medium' %}
 
24
  {%- elif _effort_raw in ('minimal', 'low') %}
25
  {%- set _initial_effort = 'low' %}
26
  {%- elif _effort_raw in ('high', 'xhigh', 'max', 'ultracode', 'extreme', 'deep', 'long') %}
27
+ {%- set _initial_thinking = true %}
28
  {%- set _initial_effort = 'xhigh' %}
29
  {%- else %}
30
  {%- set _initial_effort = 'medium' %}
31
  {%- endif %}
32
+
33
  {%- set ns_state = namespace(thinking=_initial_thinking, effort=_initial_effort) %}
34
  {%- if auto_disable_thinking_with_tools and _has_tools %}
35
  {%- set ns_state.thinking = false %}
36
  {%- endif %}
37
+
38
  {%- for msg in messages %}
39
  {%- if msg.role == 'system' or msg.role == 'developer' or msg.role == 'user' %}
40
  {%- if msg.content is string %}
 
83
  {%- endif %}
84
  {%- endif %}
85
  {%- endfor %}
86
+
87
  {%- set reasoning_instructions = '' %}
88
  {%- if ns_state.thinking %}
89
  {%- if ns_state.effort == 'xhigh' %}
90
+ {%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think rigorously through the task, explore alternative hypotheses, validate edge cases, and prioritize architectural soundness, consistency, and correctness in the final answer.' %}
91
  {%- elif ns_state.effort == 'low' %}
92
+ {%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking concise, focused, and move directly to execution or tool calls without unnecessary elaboration.' %}
93
  {%- endif %}
94
  {%- endif %}
95
+
96
+ {%- macro render_content(content, is_system_content=false) %}
97
  {%- if content is string %}
98
  {{- content }}
99
  {%- elif content is iterable and content is not mapping %}
100
  {%- for item in content %}
101
  {%- if item is mapping %}
102
  {%- if item.type == 'image' or 'image' in item or 'image_url' in item %}
103
+ {{- raise_exception('This model is a pure text CausalLM (vision tower removed). Image inputs are not supported.') }}
 
 
 
 
 
 
 
 
 
104
  {%- elif item.type == 'video' or 'video' in item %}
105
+ {{- raise_exception('This model is a pure text CausalLM (vision tower removed). Video inputs are not supported.') }}
 
 
 
 
 
 
 
 
 
106
  {%- elif 'text' in item %}
107
  {{- (item.text | string) if item.text is not none else '' }}
108
  {%- elif 'content' in item and item.content is string %}
 
120
  {{- raise_exception('Unexpected content type.') }}
121
  {%- endif %}
122
  {%- endmacro %}
123
+
124
  {%- if not messages %}
125
  {{- raise_exception('No messages provided.') }}
126
  {%- endif %}
127
+
128
  {%- set head = namespace(count=0, seen_non_system=false) %}
129
  {%- for message in messages %}
130
  {%- set _is_sys = (message.role == 'system' or message.role == 'developer') %}
 
134
  {%- set head.seen_non_system = true %}
135
  {%- endif %}
136
  {%- endfor %}
137
+
138
  {%- set sys_state = namespace(content='') %}
139
  {%- for message in messages[:head.count] %}
140
+ {%- set _part = render_content(message.content, true) | trim %}
141
  {%- if '<|think_off|>' in _part %}{%- set _part = _part.split('<|think_off|>') | join('') | trim %}{%- endif %}
142
  {%- if '<|think_on|>' in _part %}{%- set _part = _part.split('<|think_on|>') | join('') | trim %}{%- endif %}
143
  {%- if '<|think_xhigh|>' in _part %}{%- set _part = _part.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}
 
158
  {%- endif %}
159
  {%- endif %}
160
  {%- endfor %}
161
+
162
  {%- set _sc = sys_state.content %}
163
  {%- set _msgs = messages[head.count:] %}
164
+
165
  {%- set _terse_on = (terse in (true, 'true', 'True', 1, '1', 'on')) if terse is defined else false %}
166
  {%- if _terse_on %}
167
  {%- if ns_state.thinking %}
 
170
  {%- set _terse_lead = 'Answer directly and concisely. Give the answer with only what it needs to be correct and usable.' %}
171
  {%- endif %}
172
  {%- set _terse_core %}
173
+ Never: open with preamble or pleasantries; restate the question; add filler transitions; hedge with niceties; or repeat a point you have already made.
174
  Always: keep essential steps, caveats, uncertainties, and specifics — never drop correctness or a needed warning for brevity. Keep the final answer lean. Use the least structure that conveys it (plain prose when short; lists or code only when they earn their place). If genuinely uncertain, say so and explain why — never omit uncertainty for the sake of brevity.
175
+ If a user request is genuinely ambiguous, ask a sharp question, do not guess.
176
  {%- endset %}
177
  {%- set _terse = _terse_lead ~ '\n' ~ (_terse_core | trim) %}
178
  {%- if not _sc %}
 
181
  {%- set _sc = (_sc | trim) ~ '\n\n' ~ (_terse | trim) %}
182
  {%- endif %}
183
  {%- endif %}
184
+
185
  {%- if _has_tools %}
186
  {{- '<|im_start|>system\n' }}
187
  {%- if reasoning_instructions %}
 
194
  {%- endfor %}
195
  {{- '\n</tools>' }}
196
  {%- if _tool_format == 'json' %}
197
+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}{%- if ns_state.thinking %}{{- '<think>\nBrief explanation of tool call\n</think>\n' }}{%- endif %}{{- '<tool_call>\n{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "This is the value for the second parameter"}}\n</tool_call>\n\n<IMPORTANT>\nReminder:\n' }}{%- if ns_state.thinking %}{{- '- You can use the <think></think> block to plan your next tool call OR to synthesize data and formulate your final response to the user.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n' }}{%- endif %}{{- '- Function calls MUST follow the specified format: a single JSON object with "name" and "arguments" keys inside <tool_call></tool_call> XML tags.\n- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY' }}{%- if ns_state.thinking %}{{- ' after thinking' }}{%- endif %}{{- ', with NO conversational text before it.\n- The <tool_call> tag MUST be at the very beginning of a new line, with NO spaces or indentation before it.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- Keep tool calls purposeful: fire only the calls needed for the next step, and avoid issuing many parallel or speculative tool calls in a single turn when a few sequential, verified calls would do.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}
198
  {%- else %}
199
+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}{%- if ns_state.thinking %}{{- '<think>\nBrief explanation of tool call\n</think>\n' }}{%- endif %}{{- '<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n' }}{%- if ns_state.thinking %}{{- '- You can use the <think></think> block to plan your next tool call OR to synthesize data and formulate your final response to the user.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n' }}{%- endif %}{{- '- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.\n- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY' }}{%- if ns_state.thinking %}{{- ' after thinking' }}{%- endif %}{{- ', with NO conversational text before it.\n- The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- Keep tool calls purposeful: fire only the calls needed for the next step, and avoid issuing many parallel or speculative tool calls in a single turn when a few sequential, verified calls would do.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}
200
  {%- endif %}
201
  {%- if _sc %}
202
  {{- '\n\n' + _sc }}
 
209
  {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}
210
  {%- endif %}
211
  {%- endif %}
212
+
213
+ {%- set ns = namespace(found_query=false, last_query_index=0) %}
214
  {%- for message in _msgs[::-1] %}
215
  {%- set index = (_msgs | length - 1) - loop.index0 %}
216
+ {%- if not ns.found_query and message.role == 'user' %}
217
+ {%- set _rc = render_content(message.content) | trim %}
218
  {%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}
219
+ {%- set ns.found_query = true %}
220
  {%- set ns.last_query_index = index %}
221
  {%- endif %}
222
  {%- endif %}
223
  {%- endfor %}
224
+
 
 
 
 
 
 
225
  {%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}
226
  {%- for message in _msgs %}
227
  {%- set is_system = (message.role == "system" or message.role == "developer") %}
228
+ {%- set content = render_content(message.content, is_system) | trim %}
229
  {%- if is_system or message.role == 'user' %}
230
  {%- if '<|think_off|>' in content %}{%- set content = content.split('<|think_off|>') | join('') | trim %}{%- endif %}
231
  {%- if '<|think_on|>' in content %}{%- set content = content.split('<|think_on|>') | join('') | trim %}{%- endif %}
 
275
  {%- endif %}
276
  {%- if _explicit_reasoning %}
277
  {%- set _lead_end = '' %}
278
+ {%- if '</think>' in content %}
279
  {%- set _lead_end = '</think>' %}
280
+ {%- elif '</thinking>' in content %}
 
 
 
 
281
  {%- set _lead_end = '</thinking>' %}
282
  {%- endif %}
283
  {%- if _lead_end %}
 
287
  {%- set reasoning_content = _explicit_reasoning %}
288
  {%- else %}
289
  {%- set _think_end = '' %}
290
+ {%- if '</think>' in content %}
 
 
 
 
 
 
 
 
 
 
 
 
291
  {%- set _think_end = '</think>' %}
292
+ {%- elif '</thinking>' in content %}
293
  {%- set _think_end = '</thinking>' %}
294
  {%- endif %}
295
  {%- if _think_end %}
296
+ {%- set _think_start = '<thinking>' if 'thinking' in _think_end else '<think>' %}
 
 
 
 
297
  {%- set _parts = content.split(_think_end) %}
298
  {%- set reasoning_content = _parts[0].rstrip('\n') %}
299
  {%- if _think_start in reasoning_content %}
 
308
  {%- else %}
309
  {{- '<|im_start|>assistant\n' + content }}
310
  {%- endif %}
311
+ {%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping and '<tool_call>' not in content %}
312
  {%- for tool_call in message.tool_calls %}
313
  {%- if tool_call.function is defined and tool_call.function is not none %}
314
  {%- set tc = tool_call.function %}
 
316
  {%- set tc = tool_call %}
317
  {%- endif %}
318
  {%- set tc_name = tc.name if (tc.name is defined and tc.name is not none) else '' %}
319
+ {%- set tc_args_raw = none %}
320
+ {%- if tc.arguments is defined and tc.arguments is not none %}
321
+ {%- set tc_args_raw = tc.arguments %}
322
+ {%- elif tc.parameters is defined and tc.parameters is not none %}
323
+ {%- set tc_args_raw = tc.parameters %}
324
+ {%- elif tc.input is defined and tc.input is not none %}
325
+ {%- set tc_args_raw = tc.input %}
326
+ {%- endif %}
327
  {%- if _tool_format == 'json' %}
328
  {%- if loop.first %}
329
  {%- if content | trim %}
 
333
  {{- '\n' }}
334
  {%- endif %}
335
  {%- set _args = '{}' %}
336
+ {%- if tc_args_raw is not none %}
337
+ {%- if tc_args_raw is mapping %}
338
+ {%- set _args = tc_args_raw | tojson %}
339
+ {%- elif tc_args_raw is string %}
340
+ {%- set _trimmed_args = tc_args_raw | trim %}
341
+ {%- if _trimmed_args | length > 0 %}
342
+ {%- if _trimmed_args | first == '{' and _trimmed_args | last == '}' %}
343
+ {%- set _args = _trimmed_args %}
344
+ {%- else %}
345
+ {%- set _args = tc_args_raw | tojson %}
346
+ {%- endif %}
347
  {%- endif %}
348
  {%- else %}
349
+ {%- set _args = tc_args_raw | tojson %}
350
  {%- endif %}
351
  {%- endif %}
352
  {{- '<tool_call>\n{"name": ' }}{{- tc_name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }}
 
360
  {%- else %}
361
  {{- '\n<tool_call>\n<function=' + tc_name + '>\n' }}
362
  {%- endif %}
363
+ {%- if tc_args_raw is not none %}
364
+ {%- if tc_args_raw is mapping %}
365
+ {%- for args_name, args_value in tc_args_raw.items() %}
366
  {{- '<parameter=' + args_name + '>\n' }}
367
  {%- if args_value is string %}
368
  {%- set _av = args_value %}
 
377
  {{- '\n</parameter>\n' }}
378
  {%- endfor %}
379
  {%- else %}
380
+ {%- if tc_args_raw is string %}
381
+ {%- set _raw_args = tc_args_raw %}
382
  {%- else %}
383
+ {%- set _raw_args = tc_args_raw | tojson %}
384
  {%- endif %}
385
  {%- if _raw_args %}
386
  {%- if max_tool_arg_chars > 0 and _raw_args | length > max_tool_arg_chars %}
 
418
  {%- endif %}
419
  {{- '\n<tool_response>\n' + content }}
420
  {%- if ns2.consecutive_failures >= 2 %}
421
+ {{- '\n\n⚠️ SYSTEM WARNING: ' ~ ns2.consecutive_failures ~ ' consecutive tool errors detected. Your previous approach is incorrect. You MUST diagnose root cause and use a fundamentally different approach or corrected arguments.' }}
422
  {%- elif ns2.consecutive_failures == 1 %}
423
  {{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with completely corrected arguments.' }}
424
  {%- endif %}
 
436
  {%- endif %}
437
  {%- set ns2.prev_role = message.role %}
438
  {%- endfor %}
439
+
440
  {%- set _add_gen = (add_generation_prompt in (true, 'true', 'True', 1, '1', 'on')) if add_generation_prompt is defined else false %}
441
  {%- if _add_gen %}
442
  {{- '<|im_start|>assistant\n' }}
 
445
  {%- else %}
446
  {{- '<think>\n' }}
447
  {%- endif %}
448
+ {%- endif %}