nightmedia commited on
Commit
c3c13b9
·
verified ·
1 Parent(s): 28fbbd8

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +258 -167
chat_template.jinja CHANGED
@@ -1,38 +1,53 @@
1
- {%- set _enable_thinking = false %}
2
- {%- set preserve_thinking = true %}
3
  {%- set image_count = namespace(value=0) %}
4
  {%- set video_count = namespace(value=0) %}
 
 
 
 
 
 
 
 
 
 
 
5
  {%- macro render_content(content, do_vision_count, is_system_content=false) %}
6
  {%- if content is string %}
7
  {{- content }}
8
  {%- elif content is iterable and content is not mapping %}
9
  {%- for item in content %}
10
- {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
11
- {%- if is_system_content %}
12
- {{- raise_exception('System message cannot contain images.') }}
13
- {%- endif %}
14
- {%- if do_vision_count %}
15
- {%- set image_count.value = image_count.value + 1 %}
16
- {%- endif %}
17
- {%- if add_vision_id is defined and add_vision_id %}
18
- {{- 'Picture ' ~ image_count.value ~ ': ' }}
19
- {%- endif %}
20
- {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
21
- {%- elif 'video' in item or item.type == 'video' %}
22
- {%- if is_system_content %}
23
- {{- raise_exception('System message cannot contain videos.') }}
24
- {%- endif %}
25
- {%- if do_vision_count %}
26
- {%- set video_count.value = video_count.value + 1 %}
27
- {%- endif %}
28
- {%- if add_vision_id is defined and add_vision_id %}
29
- {{- 'Video ' ~ video_count.value ~ ': ' }}
 
 
 
 
 
 
 
30
  {%- endif %}
31
- {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
32
- {%- elif 'text' in item %}
33
- {{- item.text }}
34
  {%- else %}
35
- {{- raise_exception('Unexpected item type in content.') }}
36
  {%- endif %}
37
  {%- endfor %}
38
  {%- elif content is none or content is undefined %}
@@ -41,194 +56,270 @@
41
  {{- raise_exception('Unexpected content type.') }}
42
  {%- endif %}
43
  {%- endmacro %}
44
- {%- set ns_flags = namespace(enable_thinking=true) %}
45
- {%- if enable_thinking is defined %}
46
- {%- set ns_flags.enable_thinking = enable_thinking %}
47
- {%- endif %}
48
  {%- if not messages %}
49
  {{- raise_exception('No messages provided.') }}
50
  {%- endif %}
51
- {%- if tools and tools is iterable and tools is not mapping %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  {{- '<|im_start|>system\n' }}
53
- {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
54
  {%- for tool in tools %}
55
- {{- "\n" }}
56
  {{- tool | tojson }}
57
  {%- endfor %}
58
- {{- "\n</tools>" }}
59
- {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<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- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
60
- {%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
61
- {%- set content = render_content(messages[0].content, false, true)|trim %}
62
- {%- if '<|think_off|>' in content %}
63
- {%- set ns_flags.enable_thinking = false %}
64
- {%- set content = content.replace('<|think_off|>', '') %}
65
- {%- endif %}
66
- {%- if '<|think_on|>' in content %}
67
- {%- set ns_flags.enable_thinking = true %}
68
- {%- set content = content.replace('<|think_on|>', '') %}
69
- {%- endif %}
70
- {%- if '<|think_forget|>' in content %}
71
- {%- set ns_flags.preserve_thinking = false %}
72
- {%- set content = content.replace('<|think_forget|>', '') %}
73
- {%- endif %}
74
- {%- if '<|think_remember|>' in content %}
75
- {%- set ns_flags.preserve_thinking = true %}
76
- {%- set content = content.replace('<|think_remember|>', '') %}
77
- {%- endif %}
78
- {%- set content = content.strip() %}
79
- {%- if content %}
80
- {{- '\n\n' + content }}
81
- {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  {%- endif %}
83
  {{- '<|im_end|>\n' }}
84
  {%- else %}
85
- {%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
86
- {%- set content = render_content(messages[0].content, false, true)|trim %}
87
- {%- if '<|think_off|>' in content %}
88
- {%- set ns_flags.enable_thinking = false %}
89
- {%- set content = content.replace('<|think_off|>', '') %}
90
- {%- endif %}
91
- {%- if '<|think_on|>' in content %}
92
- {%- set ns_flags.enable_thinking = true %}
93
- {%- set content = content.replace('<|think_on|>', '') %}
94
- {%- endif %}
95
- {%- if '<|think_forget|>' in content %}
96
- {%- set ns_flags.preserve_thinking = false %}
97
- {%- set content = content.replace('<|think_forget|>', '') %}
98
- {%- endif %}
99
- {%- if '<|think_remember|>' in content %}
100
- {%- set ns_flags.preserve_thinking = true %}
101
- {%- set content = content.replace('<|think_remember|>', '') %}
102
- {%- endif %}
103
- {%- set content = content.strip() %}
104
- {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
105
  {%- endif %}
106
  {%- endif %}
107
- {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
108
- {%- for message in messages[::-1] %}
109
- {%- set index = (messages|length - 1) - loop.index0 %}
110
- {%- if ns.multi_step_tool and message.role == "user" %}
111
- {%- set content = render_content(message.content, false)|trim %}
112
- {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
 
113
  {%- set ns.multi_step_tool = false %}
114
  {%- set ns.last_query_index = index %}
115
  {%- endif %}
116
  {%- endif %}
117
  {%- endfor %}
118
  {%- if ns.multi_step_tool %}
119
- {{- raise_exception('No user query found in messages.') }}
120
- {%- endif %}
121
- {%- for message in messages %}
122
- {%- set content = render_content(message.content, true)|trim %}
123
- {%- if '<|think_off|>' in content %}
124
- {%- set ns_flags.enable_thinking = false %}
125
- {%- set content = content.replace('<|think_off|>', '') %}
126
- {%- endif %}
127
- {%- if '<|think_on|>' in content %}
128
- {%- set ns_flags.enable_thinking = true %}
129
- {%- set content = content.replace('<|think_on|>', '') %}
130
- {%- endif %}
131
- {%- if '<|think_forget|>' in content %}
132
- {%- set ns_flags.preserve_thinking = false %}
133
- {%- set content = content.replace('<|think_forget|>', '') %}
134
- {%- endif %}
135
- {%- if '<|think_remember|>' in content %}
136
- {%- set ns_flags.preserve_thinking = true %}
137
- {%- set content = content.replace('<|think_remember|>', '') %}
138
  {%- endif %}
139
- {%- set content = content.strip() %}
140
- {%- if message.role == "system" or message.role == "developer" %}
141
- {%- if not loop.first %}
142
- {{- raise_exception('System message must be at the beginning.') }}
 
 
 
 
 
 
 
 
143
  {%- endif %}
144
- {%- elif message.role == "user" %}
145
- {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
146
- {%- elif message.role == "assistant" %}
 
 
 
 
147
  {%- set reasoning_content = '' %}
148
- {%- if message.reasoning_content is string %}
149
- {%- set reasoning_content = message.reasoning_content %}
 
 
 
 
 
 
 
 
 
 
150
  {%- else %}
151
- {%- set has_think_tag = false %}
152
- {%- set think_end_token = '</think>' %}
153
- {%- if '</think>' in content %}
154
- {%- set has_think_tag = true %}
155
- {%- elif '</thinking>' in content %}
156
- {%- set has_think_tag = true %}
157
- {%- set think_end_token = '</thinking>' %}
158
- {%- elif '<think>' in content %}
159
- {%- set reasoning_content = content.split('<think>')[-1].lstrip('\n') %}
160
- {%- set content = '' %}
 
 
 
161
  {%- endif %}
162
- {%- if has_think_tag %}
163
- {%- set reasoning_content = content.split(think_end_token)[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
164
- {%- set content = content.split(think_end_token)[-1].lstrip('\n') %}
 
 
 
 
 
 
 
 
165
  {%- endif %}
166
  {%- endif %}
167
- {%- set reasoning_content = reasoning_content|trim %}
168
- {%- set show_think = false %}
169
- {%- if loop.index0 > ns.last_query_index %}
170
- {%- set show_think = true %}
171
- {%- elif ns_flags.enable_thinking and (preserve_thinking is undefined or preserve_thinking is true) and reasoning_content|length > 0 %}
172
- {%- set show_think = true %}
173
- {%- endif %}
174
- {%- if show_think %}
175
- {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
176
  {%- else %}
177
- {{- '<|im_start|>' + message.role + '\n' + content }}
178
  {%- endif %}
179
- {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
180
  {%- for tool_call in message.tool_calls %}
181
- {%- if tool_call.function is defined %}
182
- {%- set tool_call = tool_call.function %}
 
 
183
  {%- endif %}
184
- {%- if loop.first %}
185
- {%- if content|trim %}
186
- {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
187
- {%- else %}
188
- {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
189
  {%- endif %}
 
 
 
 
 
 
 
 
 
190
  {%- else %}
191
- {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
192
- {%- endif %}
193
- {%- if tool_call.arguments is defined and tool_call.arguments is mapping %}
194
- {%- if tool_call.arguments|length > 0 %}
195
- {%- for args_name in tool_call.arguments %}
196
- {%- set args_value = tool_call.arguments[args_name] %}
197
- {{- '<parameter=' + args_name + '>\n' }}
198
- {%- set args_value = args_value | string if args_value is string else args_value | tojson %}
199
- {{- args_value }}
200
- {{- '\n</parameter>\n' }}
201
- {%- endfor %}
202
  {%- endif %}
203
- {%- elif tool_call.arguments is defined and tool_call.arguments is string %}
204
- {%- if tool_call.arguments|trim|length > 0 %}
205
- {{- tool_call.arguments }}
206
- {{- '\n' }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  {%- endif %}
 
208
  {%- endif %}
209
- {{- '</function>\n</tool_call>' }}
210
  {%- endfor %}
211
  {%- endif %}
212
  {{- '<|im_end|>\n' }}
213
- {%- elif message.role == "tool" %}
214
- {%- if loop.previtem and loop.previtem.role != "tool" %}
 
 
 
 
 
 
 
215
  {{- '<|im_start|>user' }}
216
  {%- endif %}
217
- {{- '\n<tool_response>\n' }}
218
- {{- content }}
 
 
 
 
 
 
 
219
  {{- '\n</tool_response>' }}
220
- {%- if not loop.last and loop.nextitem.role != "tool" %}
221
- {{- '<|im_end|>\n' }}
222
- {%- elif loop.last %}
223
  {{- '<|im_end|>\n' }}
 
 
 
 
 
224
  {%- endif %}
225
  {%- else %}
226
- {{- raise_exception('Unexpected message role.') }}
227
  {%- endif %}
 
228
  {%- endfor %}
229
  {%- if add_generation_prompt %}
230
  {{- '<|im_start|>assistant\n' }}
231
- {%- if ns_flags.enable_thinking is false %}
 
 
232
  {{- '<think>\n\n</think>\n\n' }}
233
  {%- else %}
234
  {{- '<think>\n' }}
 
1
+ {%- set template_version = "qwen3.6-froggeric-v21.3" %}
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 if add_vision_id is defined else false %}
6
+ {%- set enable_thinking = enable_thinking if enable_thinking is defined else true %}
7
+ {%- set auto_disable_thinking_with_tools = auto_disable_thinking_with_tools if auto_disable_thinking_with_tools is defined else false %}
8
+ {%- set _preserve_thinking = preserve_thinking if preserve_thinking is defined else true %}
9
+ {%- set max_tool_arg_chars = max_tool_arg_chars if max_tool_arg_chars is defined else 0 %}
10
+ {%- set max_tool_response_chars = max_tool_response_chars if max_tool_response_chars is defined else 0 %}
11
+ {%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}
12
+ {%- set ns_state = namespace(thinking=enable_thinking) %}
13
+ {%- if auto_disable_thinking_with_tools and _has_tools %}
14
+ {%- set ns_state.thinking = false %}
15
+ {%- endif %}
16
  {%- macro render_content(content, do_vision_count, is_system_content=false) %}
17
  {%- if content is string %}
18
  {{- content }}
19
  {%- elif content is iterable and content is not mapping %}
20
  {%- for item in content %}
21
+ {%- if item is mapping %}
22
+ {%- if item.type == 'image' or 'image' in item or 'image_url' in item %}
23
+ {%- if is_system_content %}
24
+ {{- raise_exception('System message cannot contain images.') }}
25
+ {%- endif %}
26
+ {%- if do_vision_count %}
27
+ {%- set image_count.value = image_count.value + 1 %}
28
+ {%- endif %}
29
+ {%- if add_vision_id %}
30
+ {{- 'Picture ' ~ image_count.value ~ ': ' }}
31
+ {%- endif %}
32
+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
33
+ {%- elif item.type == 'video' or 'video' in item %}
34
+ {%- if is_system_content %}
35
+ {{- raise_exception('System message cannot contain videos.') }}
36
+ {%- endif %}
37
+ {%- if do_vision_count %}
38
+ {%- set video_count.value = video_count.value + 1 %}
39
+ {%- endif %}
40
+ {%- if add_vision_id %}
41
+ {{- 'Video ' ~ video_count.value ~ ': ' }}
42
+ {%- endif %}
43
+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
44
+ {%- elif 'text' in item %}
45
+ {{- item.text }}
46
+ {%- else %}
47
+ {{- raise_exception('Unexpected item type in content.') }}
48
  {%- endif %}
 
 
 
49
  {%- else %}
50
+ {{- item | string }}
51
  {%- endif %}
52
  {%- endfor %}
53
  {%- elif content is none or content is undefined %}
 
56
  {{- raise_exception('Unexpected content type.') }}
57
  {%- endif %}
58
  {%- endmacro %}
 
 
 
 
59
  {%- if not messages %}
60
  {{- raise_exception('No messages provided.') }}
61
  {%- endif %}
62
+ {%- set _first_role = messages[0].role %}
63
+ {%- if _first_role == 'system' or _first_role == 'developer' %}
64
+ {%- set _sys_msg = messages[0] %}
65
+ {%- set _msgs = messages[1:] %}
66
+ {%- else %}
67
+ {%- set _sys_msg = none %}
68
+ {%- set _msgs = messages %}
69
+ {%- endif %}
70
+ {%- set _sc = '' %}
71
+ {%- if _sys_msg is not none %}
72
+ {%- set _sc = render_content(_sys_msg.content, false, true) | trim %}
73
+ {%- if '<|think_off|>' in _sc %}
74
+ {%- set ns_state.thinking = false %}
75
+ {%- set _sc = _sc.split('<|think_off|>') | join('') | trim %}
76
+ {%- elif '<|think_on|>' in _sc %}
77
+ {%- set ns_state.thinking = true %}
78
+ {%- set _sc = _sc.split('<|think_on|>') | join('') | trim %}
79
+ {%- endif %}
80
+ {%- endif %}
81
+ {%- if _has_tools %}
82
  {{- '<|im_start|>system\n' }}
83
+ {{- '# Tools\n\nYou have access to the following functions:\n\n<tools>' }}
84
  {%- for tool in tools %}
85
+ {{- '\n' }}
86
  {{- tool | tojson }}
87
  {%- endfor %}
88
+ {{- '\n</tools>' }}
89
+ {%- set tool_instructions %}
90
+ If you choose to call a function ONLY reply in the following format with NO suffix:
91
+ {%- if _tool_format == 'json' %}
92
+ <think>
93
+ Brief explanation of tool call
94
+ </think>
95
+ <tool_call>
96
+ {"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "This is the value for the second parameter"}}
97
+ </tool_call>
98
+ {%- else %}
99
+ <think>
100
+ Brief explanation of tool call
101
+ </think>
102
+ <tool_call>
103
+ <function=example_function_name>
104
+ <parameter=example_parameter_1>
105
+ value_1
106
+ </parameter>
107
+ <parameter=example_parameter_2>
108
+ This is the value for the second parameter
109
+ that can span
110
+ multiple lines
111
+ </parameter>
112
+ </function>
113
+ </tool_call>
114
+ {%- endif %}
115
+ <IMPORTANT>
116
+ Reminder:
117
+ - 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.
118
+ - ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.
119
+ {%- if _tool_format == 'json' %}
120
+ - Function calls MUST follow the specified format: a single JSON object with "name" and "arguments" keys inside <tool_call></tool_call> XML tags.
121
+ {%- else %}
122
+ - Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.
123
+ {%- endif %}
124
+ - If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.
125
+ {%- if _tool_format == 'json' %}
126
+ - The <tool_call> tag MUST be at the very beginning of a new line, with NO spaces or indentation before it.
127
+ {%- else %}
128
+ - The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.
129
+ {%- endif %}
130
+ - To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.
131
+ - If you have all necessary data, provide your final answer directly to the user without any tool call.
132
+ </IMPORTANT>
133
+ {%- endset %}
134
+ {{- '\n\n' ~ tool_instructions | trim }}
135
+ {%- if _sc %}
136
+ {{- '\n\n' + _sc }}
137
  {%- endif %}
138
  {{- '<|im_end|>\n' }}
139
  {%- else %}
140
+ {%- if _sc %}
141
+ {{- '<|im_start|>system\n' + _sc + '<|im_end|>\n' }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  {%- endif %}
143
  {%- endif %}
144
+ {%- set _last_idx = _msgs | length - 1 %}
145
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=_last_idx) %}
146
+ {%- for message in _msgs[::-1] %}
147
+ {%- set index = (_msgs | length - 1) - loop.index0 %}
148
+ {%- if ns.multi_step_tool and message.role == 'user' %}
149
+ {%- set _rc = render_content(message.content, false) | trim %}
150
+ {%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}
151
  {%- set ns.multi_step_tool = false %}
152
  {%- set ns.last_query_index = index %}
153
  {%- endif %}
154
  {%- endif %}
155
  {%- endfor %}
156
  {%- if ns.multi_step_tool %}
157
+ {%- if _last_idx > 50 %}
158
+ {%- set ns.last_query_index = _last_idx %}
159
+ {%- else %}
160
+ {%- set ns.last_query_index = 0 %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  {%- endif %}
162
+ {%- endif %}
163
+ {%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}
164
+ {%- for message in _msgs %}
165
+ {%- set is_system = (message.role == "system" or message.role == "developer") %}
166
+ {%- set content = render_content(message.content, true, is_system) | trim %}
167
+ {%- if is_system or message.role == 'user' %}
168
+ {%- if '<|think_off|>' in content %}
169
+ {%- set ns_state.thinking = false %}
170
+ {%- set content = content.split('<|think_off|>') | join('') | trim %}
171
+ {%- elif '<|think_on|>' in content %}
172
+ {%- set ns_state.thinking = true %}
173
+ {%- set content = content.split('<|think_on|>') | join('') | trim %}
174
  {%- endif %}
175
+ {%- endif %}
176
+ {%- if is_system %}
177
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
178
+ {%- elif message.role == 'user' %}
179
+ {%- set ns2.consecutive_failures = 0 %}
180
+ {{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}
181
+ {%- elif message.role == 'assistant' %}
182
  {%- set reasoning_content = '' %}
183
+ {%- if message.reasoning_content is defined and message.reasoning_content is not none %}
184
+ {%- if message.reasoning_content is string %}
185
+ {%- set reasoning_content = message.reasoning_content %}
186
+ {%- else %}
187
+ {%- set reasoning_content = message.reasoning_content | string %}
188
+ {%- endif %}
189
+ {%- elif message.thinking is defined and message.thinking is not none %}
190
+ {%- if message.thinking is string %}
191
+ {%- set reasoning_content = message.thinking %}
192
+ {%- else %}
193
+ {%- set reasoning_content = message.thinking | string %}
194
+ {%- endif %}
195
  {%- else %}
196
+ {%- set _think_end = '' %}
197
+ {%- if content.startswith('</think>') %}
198
+ {%- set _think_end = '</think>' %}
199
+ {%- elif content.startswith('</thinking>') %}
200
+ {%- set _think_end = '</thinking>' %}
201
+ {%- elif '\n</think>' in content %}
202
+ {%- set _think_end = '\n</think>' %}
203
+ {%- elif '\n</thinking>' in content %}
204
+ {%- set _think_end = '\n</thinking>' %}
205
+ {%- elif '\n</ think>' in content %}
206
+ {%- set _think_end = '\n</ think>' %}
207
+ {%- elif '\n</think >' in content %}
208
+ {%- set _think_end = '\n</think >' %}
209
  {%- endif %}
210
+ {%- if _think_end %}
211
+ {%- if 'thinking' in _think_end %}
212
+ {%- set _think_start = '<thinking>' %}
213
+ {%- else %}
214
+ {%- set _think_start = '<think>' %}
215
+ {%- endif %}
216
+ {%- set reasoning_content = content.split(_think_end)[0].rstrip('\n') %}
217
+ {%- if _think_start in reasoning_content %}
218
+ {%- set reasoning_content = reasoning_content.split(_think_start)[-1].lstrip('\n') %}
219
+ {%- endif %}
220
+ {%- set content = content.split(_think_end)[-1].lstrip('\n') %}
221
  {%- endif %}
222
  {%- endif %}
223
+ {%- set reasoning_content = reasoning_content | trim %}
224
+ {%- if (_preserve_thinking or loop.index0 > ns.last_query_index) and reasoning_content %}
225
+ {{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
 
 
 
 
 
 
226
  {%- else %}
227
+ {{- '<|im_start|>assistant\n' + content }}
228
  {%- endif %}
229
+ {%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
230
  {%- for tool_call in message.tool_calls %}
231
+ {%- if tool_call.function is defined and tool_call.function is not none %}
232
+ {%- set tc = tool_call.function %}
233
+ {%- else %}
234
+ {%- set tc = tool_call %}
235
  {%- endif %}
236
+ {%- if _tool_format == 'json' %}
237
+ {%- if not loop.first or content | trim %}
238
+ {{- '\n\n' }}
 
 
239
  {%- endif %}
240
+ {%- set _args = '{}' %}
241
+ {%- if tc.arguments is defined and tc.arguments is not none %}
242
+ {%- if tc.arguments is mapping %}
243
+ {%- set _args = tc.arguments | tojson %}
244
+ {%- elif tc.arguments is string and tc.arguments %}
245
+ {%- set _args = tc.arguments %}
246
+ {%- endif %}
247
+ {%- endif %}
248
+ {{- '<tool_call>\n{"name": ' }}{{- tc.name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }}
249
  {%- else %}
250
+ {%- if loop.first %}
251
+ {%- if content | trim %}
252
+ {{- '\n\n<tool_call>\n<function=' + tc.name + '>\n' }}
253
+ {%- else %}
254
+ {{- '<tool_call>\n<function=' + tc.name + '>\n' }}
255
+ {%- endif %}
256
+ {%- else %}
257
+ {{- '\n\n<tool_call>\n<function=' + tc.name + '>\n' }}
 
 
 
258
  {%- endif %}
259
+ {%- if tc.arguments is defined and tc.arguments is not none %}
260
+ {%- if tc.arguments is mapping %}
261
+ {%- for args_name, args_value in tc.arguments.items() %}
262
+ {{- '<parameter=' + args_name + '>\n' }}
263
+ {%- if args_value is mapping or (args_value is sequence and args_value is not string) %}
264
+ {%- set _av = args_value | tojson %}
265
+ {%- else %}
266
+ {%- set _av = args_value | string %}
267
+ {%- endif %}
268
+ {%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %}
269
+ {{- _av[:max_tool_arg_chars] + '\n[TRUNCATED — original length ' ~ (_av | length | string) ~ ' chars]' }}
270
+ {%- else %}
271
+ {{- _av }}
272
+ {%- endif %}
273
+ {{- '\n</parameter>\n' }}
274
+ {%- endfor %}
275
+ {%- elif tc.arguments is string and tc.arguments %}
276
+ {{- tc.arguments }}
277
+ {%- endif %}
278
  {%- endif %}
279
+ {{- '</function>\n</tool_call>' }}
280
  {%- endif %}
 
281
  {%- endfor %}
282
  {%- endif %}
283
  {{- '<|im_end|>\n' }}
284
+ {%- elif message.role == 'tool' %}
285
+ {%- set _content_lower = content | lower %}
286
+ {%- set _content_head = _content_lower[:80] %}
287
+ {%- if content | length < 500 and '$ ' not in content and 'took ' not in _content_lower and ('"error":' in _content_head or 'error:' in _content_head or 'err!' in _content_head or 'fatal:' in _content_head or 'exception:' in _content_head or 'traceback' in _content_head or 'command not found' in _content_head or 'invalid syntax' in _content_head or 'failed to' in _content_head) %}
288
+ {%- set ns2.consecutive_failures = ns2.consecutive_failures + 1 %}
289
+ {%- else %}
290
+ {%- set ns2.consecutive_failures = 0 %}
291
+ {%- endif %}
292
+ {%- if ns2.prev_role != 'tool' %}
293
  {{- '<|im_start|>user' }}
294
  {%- endif %}
295
+ {%- if max_tool_response_chars > 0 and content | length > max_tool_response_chars %}
296
+ {%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED — original length ' ~ (content | length | string) ~ ' chars]' %}
297
+ {%- endif %}
298
+ {{- '\n<tool_response>\n' + content }}
299
+ {%- if ns2.consecutive_failures >= 2 %}
300
+ {{- '\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.' }}
301
+ {%- elif ns2.consecutive_failures == 1 %}
302
+ {{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with completely corrected arguments.' }}
303
+ {%- endif %}
304
  {{- '\n</tool_response>' }}
305
+ {%- if loop.last %}
 
 
306
  {{- '<|im_end|>\n' }}
307
+ {%- else %}
308
+ {%- set _next_role = _msgs[loop.index0 + 1].role %}
309
+ {%- if _next_role != 'tool' %}
310
+ {{- '<|im_end|>\n' }}
311
+ {%- endif %}
312
  {%- endif %}
313
  {%- else %}
314
+ {{- '<|im_start|>user\n[' + message.role + ']: ' + content + '<|im_end|>\n' }}
315
  {%- endif %}
316
+ {%- set ns2.prev_role = message.role %}
317
  {%- endfor %}
318
  {%- if add_generation_prompt %}
319
  {{- '<|im_start|>assistant\n' }}
320
+ {%- if not ns_state.thinking %}
321
+ {{- '<think>\n\n</think>\n\n' }}
322
+ {%- elif ns2.consecutive_failures >= 2 %}
323
  {{- '<think>\n\n</think>\n\n' }}
324
  {%- else %}
325
  {{- '<think>\n' }}