wzlu1 commited on
Commit
9721554
·
verified ·
1 Parent(s): fcf4f70

Upload Qwen36.jinja

Browse files
Files changed (1) hide show
  1. Qwen36.jinja +329 -0
Qwen36.jinja ADDED
@@ -0,0 +1,329 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 %}
54
+ {{- '' }}
55
+ {%- else %}
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
+
92
+ {%- if _tool_format == 'json' %}
93
+ <think>
94
+ Brief explanation of tool call
95
+ </think>
96
+ <tool_call>
97
+ {"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "This is the value for the second parameter"}}
98
+ </tool_call>
99
+ {%- else %}
100
+ <think>
101
+ Brief explanation of tool call
102
+ </think>
103
+ <tool_call>
104
+ <function=example_function_name>
105
+ <parameter=example_parameter_1>
106
+ value_1
107
+ </parameter>
108
+ <parameter=example_parameter_2>
109
+ This is the value for the second parameter
110
+ that can span
111
+ multiple lines
112
+ </parameter>
113
+ </function>
114
+ </tool_call>
115
+ {%- endif %}
116
+
117
+ <IMPORTANT>
118
+ Reminder:
119
+ - 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.
120
+ - ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.
121
+ {%- if _tool_format == 'json' %}
122
+ - Function calls MUST follow the specified format: a single JSON object with "name" and "arguments" keys inside <tool_call></tool_call> XML tags.
123
+ {%- else %}
124
+ - Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.
125
+ {%- endif %}
126
+ - If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.
127
+ {%- if _tool_format == 'json' %}
128
+ - The <tool_call> tag MUST be at the very beginning of a new line, with NO spaces or indentation before it.
129
+ {%- else %}
130
+ - The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.
131
+ {%- endif %}
132
+ - To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.
133
+ - If you have all necessary data, provide your final answer directly to the user without any tool call.
134
+ </IMPORTANT>
135
+ {%- endset %}
136
+ {{- '\n\n' ~ tool_instructions | trim }}
137
+ {%- if _sc %}
138
+ {{- '\n\n' + _sc }}
139
+ {%- endif %}
140
+ {{- '<|im_end|>\n' }}
141
+ {%- else %}
142
+ {%- if _sc %}
143
+ {{- '<|im_start|>system\n' + _sc + '<|im_end|>\n' }}
144
+ {%- endif %}
145
+ {%- endif %}
146
+ {%- set _last_idx = _msgs | length - 1 %}
147
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=_last_idx) %}
148
+ {%- for message in _msgs[::-1] %}
149
+ {%- set index = (_msgs | length - 1) - loop.index0 %}
150
+ {%- if ns.multi_step_tool and message.role == 'user' %}
151
+ {%- set _rc = render_content(message.content, false) | trim %}
152
+ {%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}
153
+ {%- set ns.multi_step_tool = false %}
154
+ {%- set ns.last_query_index = index %}
155
+ {%- endif %}
156
+ {%- endif %}
157
+ {%- endfor %}
158
+ {%- if ns.multi_step_tool %}
159
+ {%- if _last_idx > 50 %}
160
+ {%- set ns.last_query_index = _last_idx %}
161
+ {%- else %}
162
+ {%- set ns.last_query_index = 0 %}
163
+ {%- endif %}
164
+ {%- endif %}
165
+ {%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}
166
+ {%- for message in _msgs %}
167
+ {%- set is_system = (message.role == "system" or message.role == "developer") %}
168
+ {%- set content = render_content(message.content, true, is_system) | trim %}
169
+ {%- if is_system or message.role == 'user' %}
170
+ {%- if '<|think_off|>' in content %}
171
+ {%- set ns_state.thinking = false %}
172
+ {%- set content = content.split('<|think_off|>') | join('') | trim %}
173
+ {%- elif '<|think_on|>' in content %}
174
+ {%- set ns_state.thinking = true %}
175
+ {%- set content = content.split('<|think_on|>') | join('') | trim %}
176
+ {%- endif %}
177
+ {%- endif %}
178
+ {%- if is_system %}
179
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
180
+ {%- elif message.role == 'user' %}
181
+ {%- set ns2.consecutive_failures = 0 %}
182
+ {{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}
183
+ {%- elif message.role == 'assistant' %}
184
+ {%- set reasoning_content = '' %}
185
+ {%- if message.reasoning_content is defined and message.reasoning_content is not none %}
186
+ {%- if message.reasoning_content is string %}
187
+ {%- set reasoning_content = message.reasoning_content %}
188
+ {%- else %}
189
+ {%- set reasoning_content = message.reasoning_content | string %}
190
+ {%- endif %}
191
+ {%- elif message.thinking is defined and message.thinking is not none %}
192
+ {%- if message.thinking is string %}
193
+ {%- set reasoning_content = message.thinking %}
194
+ {%- else %}
195
+ {%- set reasoning_content = message.thinking | string %}
196
+ {%- endif %}
197
+ {%- else %}
198
+ {%- set _think_end = '' %}
199
+ {%- if content.startswith('</think>') %}
200
+ {%- set _think_end = '</think>' %}
201
+ {%- elif content.startswith('</thinking>') %}
202
+ {%- set _think_end = '</thinking>' %}
203
+ {%- elif '\n</think>' in content %}
204
+ {%- set _think_end = '\n</think>' %}
205
+ {%- elif '\n</thinking>' in content %}
206
+ {%- set _think_end = '\n</thinking>' %}
207
+ {%- elif '\n</ think>' in content %}
208
+ {%- set _think_end = '\n</ think>' %}
209
+ {%- elif '\n</think >' in content %}
210
+ {%- set _think_end = '\n</think >' %}
211
+ {%- endif %}
212
+ {%- if _think_end %}
213
+ {%- if 'thinking' in _think_end %}
214
+ {%- set _think_start = '<thinking>' %}
215
+ {%- else %}
216
+ {%- set _think_start = '<think>' %}
217
+ {%- endif %}
218
+ {%- set reasoning_content = content.split(_think_end)[0].rstrip('\n') %}
219
+ {%- if _think_start in reasoning_content %}
220
+ {%- set reasoning_content = reasoning_content.split(_think_start)[-1].lstrip('\n') %}
221
+ {%- endif %}
222
+ {%- set content = content.split(_think_end)[-1].lstrip('\n') %}
223
+ {%- endif %}
224
+ {%- endif %}
225
+ {%- set reasoning_content = reasoning_content | trim %}
226
+ {%- if (_preserve_thinking or loop.index0 > ns.last_query_index) and reasoning_content %}
227
+ {{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
228
+ {%- else %}
229
+ {{- '<|im_start|>assistant\n' + content }}
230
+ {%- endif %}
231
+ {%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
232
+ {%- for tool_call in message.tool_calls %}
233
+ {%- if tool_call.function is defined and tool_call.function is not none %}
234
+ {%- set tc = tool_call.function %}
235
+ {%- else %}
236
+ {%- set tc = tool_call %}
237
+ {%- endif %}
238
+ {%- if _tool_format == 'json' %}
239
+ {%- if not loop.first or content | trim %}
240
+ {{- '\n\n' }}
241
+ {%- endif %}
242
+ {%- set _args = '{}' %}
243
+ {%- if tc.arguments is defined and tc.arguments is not none %}
244
+ {%- if tc.arguments is mapping %}
245
+ {%- set _args = tc.arguments | tojson %}
246
+ {%- elif tc.arguments is string and tc.arguments %}
247
+ {%- set _args = tc.arguments %}
248
+ {%- endif %}
249
+ {%- endif %}
250
+ {{- '<tool_call>\n{"name": ' }}{{- tc.name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }}
251
+ {%- else %}
252
+ {%- if loop.first %}
253
+ {%- if content | trim %}
254
+ {{- '\n\n<tool_call>\n<function=' + tc.name + '>\n' }}
255
+ {%- else %}
256
+ {{- '<tool_call>\n<function=' + tc.name + '>\n' }}
257
+ {%- endif %}
258
+ {%- else %}
259
+ {{- '\n\n<tool_call>\n<function=' + tc.name + '>\n' }}
260
+ {%- endif %}
261
+ {%- if tc.arguments is defined and tc.arguments is not none %}
262
+ {%- if tc.arguments is mapping %}
263
+ {%- for args_name, args_value in tc.arguments.items() %}
264
+ {{- '<parameter=' + args_name + '>\n' }}
265
+ {%- if args_value is mapping or (args_value is sequence and args_value is not string) %}
266
+ {%- set _av = args_value | tojson %}
267
+ {%- else %}
268
+ {%- set _av = args_value | string %}
269
+ {%- endif %}
270
+ {%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %}
271
+ {{- _av[:max_tool_arg_chars] + '\n[TRUNCATED — original length ' ~ (_av | length | string) ~ ' chars]' }}
272
+ {%- else %}
273
+ {{- _av }}
274
+ {%- endif %}
275
+ {{- '\n</parameter>\n' }}
276
+ {%- endfor %}
277
+ {%- elif tc.arguments is string and tc.arguments %}
278
+ {{- tc.arguments }}
279
+ {%- endif %}
280
+ {%- endif %}
281
+ {{- '</function>\n</tool_call>' }}
282
+ {%- endif %}
283
+ {%- endfor %}
284
+ {%- endif %}
285
+ {{- '<|im_end|>\n' }}
286
+ {%- elif message.role == 'tool' %}
287
+ {%- set _content_lower = content | lower %}
288
+ {%- set _content_head = _content_lower[:80] %}
289
+ {%- 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) %}
290
+ {%- set ns2.consecutive_failures = ns2.consecutive_failures + 1 %}
291
+ {%- else %}
292
+ {%- set ns2.consecutive_failures = 0 %}
293
+ {%- endif %}
294
+ {%- if ns2.prev_role != 'tool' %}
295
+ {{- '<|im_start|>user' }}
296
+ {%- endif %}
297
+ {%- if max_tool_response_chars > 0 and content | length > max_tool_response_chars %}
298
+ {%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED — original length ' ~ (content | length | string) ~ ' chars]' %}
299
+ {%- endif %}
300
+ {{- '\n<tool_response>\n' + content }}
301
+ {%- if ns2.consecutive_failures >= 2 %}
302
+ {{- '\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.' }}
303
+ {%- elif ns2.consecutive_failures == 1 %}
304
+ {{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with completely corrected arguments.' }}
305
+ {%- endif %}
306
+ {{- '\n</tool_response>' }}
307
+ {%- if loop.last %}
308
+ {{- '<|im_end|>\n' }}
309
+ {%- else %}
310
+ {%- set _next_role = _msgs[loop.index0 + 1].role %}
311
+ {%- if _next_role != 'tool' %}
312
+ {{- '<|im_end|>\n' }}
313
+ {%- endif %}
314
+ {%- endif %}
315
+ {%- else %}
316
+ {{- '<|im_start|>user\n[' + message.role + ']: ' + content + '<|im_end|>\n' }}
317
+ {%- endif %}
318
+ {%- set ns2.prev_role = message.role %}
319
+ {%- endfor %}
320
+ {%- if add_generation_prompt %}
321
+ {{- '<|im_start|>assistant\n' }}
322
+ {%- if not ns_state.thinking %}
323
+ {{- '<think>\n\n</think>\n\n' }}
324
+ {%- elif ns2.consecutive_failures >= 2 %}
325
+ {{- '<think>\n\n</think>\n\n' }}
326
+ {%- else %}
327
+ {{- '<think>\n' }}
328
+ {%- endif %}
329
+ {%- endif %}