StableQuant commited on
Commit
b918364
·
verified ·
1 Parent(s): fa46bb3

Delete qwen3_5-6-template_v1.1.5.jinja

Browse files
Files changed (1) hide show
  1. qwen3_5-6-template_v1.1.5.jinja +0 -314
qwen3_5-6-template_v1.1.5.jinja DELETED
@@ -1,314 +0,0 @@
1
- {#- ============================================================================
2
- Qwen Chat Template v1.1 (Stable Agentic Version + JSON Escaping Fix)
3
-
4
- FIXES APPLIED IN v1.1:
5
- 1. Added strict JSON escaping instructions to Section 7 to prevent the LLM
6
- from generating raw newlines and quotes inside JSON tool arguments.
7
-
8
- FIXES APPLIED IN v1.0:
9
- 1. Kept v0.9 Audio support and strict iterable type checks.
10
- 2. REMOVED URL text injection before vision/audio pads.
11
- 3. REMOVED Tool Call ID injection (violates Qwen's native JSON schema).
12
- 4. RE-APPLIED Context Breakout Prevention (tojson on tool responses).
13
- 5. RE-APPLIED Think-Mode Tool Call Prevention (Instruction in Section 7).
14
- ============================================================================ -#}
15
-
16
- {%- macro raise_exception(message) -%}
17
- {{- '\n[ERROR: ' ~ message ~ ']' -}}
18
- {%- endmacro -%}
19
-
20
- {#- ===== SECTION 1A: MACRO render_content ===== -#}
21
- {%- macro render_content(content, count_vision=false, is_system_content=false) -%}
22
- {%- if is_system_content and content is iterable and content is not mapping and content is not string and content is not none -%}
23
- {%- for item in content -%}
24
- {%- if item.type == 'image' or 'image' in item or 'image_url' in item -%}
25
- {{- raise_exception('System message cannot contain images.') -}}
26
- {%- endif -%}
27
- {%- if item.type == 'video' or 'video' in item -%}
28
- {{- raise_exception('System message cannot contain videos.') -}}
29
- {%- endif -%}
30
- {%- endfor -%}
31
- {%- endif -%}
32
-
33
- {%- if content is none or content is defined == false -%}
34
- {{- '' -}}
35
- {%- elif content is string -%}
36
- {{- content -}}
37
- {%- elif content is not mapping and content is iterable and content is not string -%}
38
- {%- for item in content -%}
39
- {%- if item.type == 'image' or 'image' in item or 'image_url' in item -%}
40
- {%- if count_vision -%}{%- set ns.image_count = ns.image_count + 1 -%}{%- endif -%}
41
- {%- if add_vision_id is defined and add_vision_id -%}
42
- {{- 'Picture ' ~ ns.image_count ~ ': ' -}}
43
- {%- endif -%}
44
- {{- '<|vision_start|><|image_pad|><|vision_end|>' -}}
45
- {%- elif item.type == 'audio' or 'audio' in item or 'audio_url' in item -%}
46
- {%- if add_vision_id is defined and add_vision_id -%}
47
- {{- 'Audio ' ~ ns.image_count ~ ': ' -}}
48
- {%- endif -%}
49
- {{- '<|audio_pad|><|audio_end|>' -}}
50
- {%- elif item.type == 'video' or 'video' in item -%}
51
- {%- if count_vision -%}{%- set ns.video_count = ns.video_count + 1 -%}{%- endif -%}
52
- {%- if add_vision_id is defined and add_vision_id -%}
53
- {{- 'Video ' ~ ns.video_count ~ ': ' -}}
54
- {%- endif -%}
55
- {{- '<|vision_start|><|video_pad|><|vision_end|>' -}}
56
- {%- elif item.type == 'text' or 'text' in item -%}
57
- {{- item.text -}}
58
- {%- else -%}
59
- {{- raise_exception('Unexpected content type in message content.') -}}
60
- {%- endif -%}
61
- {%- endfor -%}
62
- {%- elif content is not none and content is defined -%}
63
- {{- raise_exception('Unexpected content type.') -}}
64
- {%- endif -%}
65
- {%- endmacro -%}
66
-
67
- {#- ===== SECTION 1B: MACRO detect_tool_error ===== -#}
68
- {%- macro detect_tool_error(content) -%}
69
- {%- set content = content if content is string else '' -%}
70
- {%- set content_lower = content | lower -%}
71
- {%- set content_length = content | length -%}
72
-
73
- {%- if content_length < 500
74
- and '$ ' not in content
75
- and 'took ' not in content_lower
76
- and ('"error":' in content_lower or 'error:' in content_lower
77
- or 'exception:' in content_lower or 'traceback' in content_lower
78
- or 'command not found' in content_lower or 'invalid syntax' in content_lower
79
- or 'failed to' in content_lower or 'permission denied' in content_lower) -%}
80
- {%- set ns.last_tool_failed = true -%}
81
- {%- set ns.consecutive_failures = ns.consecutive_failures + 1 -%}
82
- {%- else -%}
83
- {%- set ns.last_tool_failed = false -%}
84
- {%- set ns.consecutive_failures = 0 -%}
85
- {%- endif -%}
86
- {%- endmacro -%}
87
-
88
- {#- ===== SECTION 2: NAMESPACE INITIALISATION ===== -#}
89
- {%- set ns = namespace(
90
- enable_thinking=true,
91
- preserve_thinking=true,
92
- image_count=0,
93
- video_count=0,
94
- consecutive_failures=0,
95
- last_tool_failed=false
96
- ) -%}
97
-
98
- {%- if enable_thinking is defined -%}
99
- {%- if enable_thinking -%}
100
- {%- set ns.enable_thinking = true -%}
101
- {%- else -%}
102
- {%- set ns.enable_thinking = false -%}
103
- {%- endif -%}
104
- {%- endif -%}
105
-
106
- {%- if preserve_thinking is defined -%}
107
- {%- if not preserve_thinking -%}
108
- {%- set ns.enable_thinking = false -%}
109
- {%- set ns.preserve_thinking = false -%}
110
- {%- else -%}
111
- {%- set ns.preserve_thinking = true -%}
112
- {%- endif -%}
113
- {%- endif -%}
114
-
115
- {#- ===== SECTION 3: PRE-SCAN ===== -#}
116
- {%- for i in range(messages | length) -%}
117
- {%- set _msg = messages[i] -%}
118
- {%- if _msg.role == 'user' -%}
119
- {%- set _u = _msg.content if _msg.content is string else '' -%}
120
- {%- if _u.rstrip().endswith('/no_think') -%}
121
- {%- set ns.enable_thinking = false -%}
122
- {%- elif _u.rstrip().endswith('/think') -%}
123
- {%- set ns.enable_thinking = true -%}
124
- {%- endif -%}
125
- {%- elif _msg.role == 'system' or _msg.role == 'developer' -%}
126
- {%- set _s = _msg.content if _msg.content is string else '' -%}
127
- {%- if '<|think_off|>' in _s -%}
128
- {%- set ns.enable_thinking = false -%}
129
- {%- elif '<|think_on|>' in _s -%}
130
- {%- set ns.enable_thinking = true -%}
131
- {%- endif -%}
132
- {%- endif -%}
133
- {%- endfor -%}
134
-
135
- {#- ===== SECTION 4: VALIDATE MESSAGES ===== -#}
136
- {%- if not messages -%}
137
- {{- raise_exception('No messages provided.') -}}
138
- {%- endif -%}
139
-
140
- {#- ===== SECTION 5: COLLECT SYSTEM CONTENT ===== -#}
141
- {%- set ns_sys = namespace(content='') -%}
142
- {%- for msg in messages -%}
143
- {%- if msg.role == 'system' or msg.role == 'developer' -%}
144
- {%- set _c = render_content(msg.content | default(''), false, true) | trim -%}
145
- {%- set _c = _c | replace('<|think_off|>', '') | replace('<|think_on|>', '') | trim -%}
146
- {%- if _c -%}
147
- {%- if ns_sys.content == '' -%}
148
- {%- set ns_sys.content = _c -%}
149
- {%- else -%}
150
- {%- set ns_sys.content = ns_sys.content + '\n\n' + _c -%}
151
- {%- endif -%}
152
- {%- endif -%}
153
- {%- endif -%}
154
- {%- endfor -%}
155
-
156
- {#- ===== SECTION 6: BUILD TOOLS LIST ===== -#}
157
- {%- set _has_tools = tools is defined and tools -%}
158
- {%- if _has_tools -%}
159
- {%- set ns_tb = namespace(list=[]) -%}
160
- {%- for tool in tools -%}
161
- {%- if tool.function is defined -%}
162
- {%- set ns_tb.list = ns_tb.list + [tool] -%}
163
- {%- else -%}
164
- {%- set ns_tb.list = ns_tb.list + [{"type": "function", "function": tool}] -%}
165
- {%- endif -%}
166
- {%- endfor -%}
167
- {%- endif -%}
168
-
169
- {#- ===== SECTION 7: OUTPUT SYSTEM TURN ===== -#}
170
- {%- if ns_sys.content or _has_tools -%}
171
- {{- '<|im_start|>system\n' -}}
172
- {%- if ns_sys.content -%}
173
- {{- ns_sys.content -}}
174
- {%- if _has_tools -%}{{- '\n\n' -}}{%- endif -%}
175
- {%- endif -%}
176
- {%- if _has_tools -%}
177
- {{- '# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n' -}}
178
- {%- for tool in ns_tb.list -%}
179
- {{- tool | tojson -}}
180
- {%- if not loop.last -%}{{- '\n' -}}{%- endif -%}
181
- {%- endfor -%}
182
- {#- FIX v1.1: Added JSON strict escaping warnings to critical instructions -#}
183
- {{- '\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{"name": <function-name>, "arguments": <args-json-object>}\n</tool_call>\n\nCRITICAL INSTRUCTIONS:\n1. You MUST NOT output <tool_call> tags while inside <think> tags. Complete your reasoning and output </think> FIRST, then initiate your tool calls.\n2. When passing multi-line code or text containing quotes into a JSON argument, you MUST strictly escape all newlines as \\n and quotes as \\". Failure to escape will invalidate the JSON and crash the system.' -}}
184
- {%- endif -%}
185
- {{- '<|im_end|>\n' -}}
186
- {%- endif -%}
187
-
188
- {#- ===== SECTION 8: MAIN MESSAGE LOOP ===== -#}
189
- {%- for message in messages -%}
190
-
191
- {%- if message.role == 'system' or message.role == 'developer' -%}
192
-
193
- {%- elif message.role == 'user' -%}
194
- {%- set _uc = render_content(message.content | default(''), true, false) -%}
195
- {{- '<|im_start|>user\n' + _uc + '<|im_end|>\n' -}}
196
-
197
- {%- elif message.role == 'assistant' -%}
198
- {%- if message.content is defined and message.content is string -%}
199
- {%- set _ac = message.content -%}
200
- {%- elif message.content is defined and message.content is iterable and message.content is not mapping and message.content is not string -%}
201
- {%- set _ac = render_content(message.content, false, false) -%}
202
- {%- else -%}
203
- {%- set _ac = '' -%}
204
- {%- endif -%}
205
-
206
- {%- if message.reasoning_content is defined and message.reasoning_content is string
207
- and message.reasoning_content | trim
208
- and '<think>' not in _ac -%}
209
- {%- set _ac = '<think>\n' + message.reasoning_content | trim + '\n</think>\n\n' + _ac -%}
210
- {%- endif -%}
211
-
212
- {%- set _tc = message.tool_calls if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls is not string else [] -%}
213
-
214
- {%- if _tc and '<tool_call>' in _ac -%}
215
- {%- set _ac = _ac.split('<tool_call>')[0] | trim -%}
216
- {%- endif -%}
217
-
218
- {%- set _show_think = false -%}
219
- {%- if _tc -%}
220
- {%- set _show_think = true -%}
221
- {%- elif ns.preserve_thinking -%}
222
- {%- set _show_think = true -%}
223
- {%- elif loop.last -%}
224
- {%- set _show_think = ns.enable_thinking -%}
225
- {%- endif -%}
226
-
227
- {%- if not _show_think -%}
228
- {%- set _think_end = '' -%}
229
- {%- if '</think>' in _ac -%}
230
- {%- set _think_end = '</think>' -%}
231
- {%- elif '</thinking>' in _ac -%}
232
- {%- set _think_end = '</thinking>' -%}
233
- {%- elif '</ think>' in _ac -%}
234
- {%- set _think_end = '</ think>' -%}
235
- {%- elif '</think >' in _ac -%}
236
- {%- set _think_end = '</think >' -%}
237
- {%- endif -%}
238
- {%- if _think_end -%}
239
- {%- set _ac = _ac.split(_think_end)[-1].lstrip('\n') -%}
240
- {%- endif -%}
241
- {%- elif not _tc and loop.last and '<think>' not in _ac and not ns.enable_thinking -%}
242
- {%- set _ac = '<think>\n\n</think>\n\n' + _ac -%}
243
- {%- endif -%}
244
-
245
- {{- '<|im_start|>assistant\n' -}}
246
- {%- if _ac -%}
247
- {{- _ac -}}
248
- {%- if _tc -%}{{- '\n' -}}{%- endif -%}
249
- {%- endif -%}
250
-
251
- {%- if _tc -%}
252
- {%- for tc in _tc -%}
253
- {{- '<tool_call>\n' -}}
254
- {{- '{"name": ' -}}{{- tc.function.name | tojson -}}
255
- {%- if tc.function.arguments is string -%}
256
- {{- ', "arguments": ' + tc.function.arguments -}}
257
- {%- else -%}
258
- {{- ', "arguments": ' -}}{{- tc.function.arguments | tojson -}}
259
- {%- endif -%}
260
- {{- '}' -}}
261
- {%- if not loop.last -%}
262
- {{- '\n</tool_call>\n' -}}
263
- {%- else -%}
264
- {{- '\n</tool_call>' -}}
265
- {%- endif -%}
266
- {%- endfor -%}
267
- {%- endif -%}
268
- {{- '<|im_end|>\n' -}}
269
-
270
- {#- 8d: Tool results -#}
271
- {%- elif message.role == 'tool' -%}
272
- {%- set _prev_role = messages[loop.index0 - 1].role if loop.index0 > 0 else '' -%}
273
- {%- set _next_role = messages[loop.index0 + 1].role if not loop.last else '' -%}
274
-
275
- {%- set _tool_content = message.content | default('') -%}
276
- {{- detect_tool_error(_tool_content) -}}
277
-
278
- {%- if _prev_role != 'tool' -%}
279
- {{- '<|im_start|>user\n' -}}
280
- {%- endif -%}
281
- {{- '<tool_response>\n' -}}
282
-
283
- {{- '{"result": ' ~ _tool_content | string | tojson ~ '}' -}}
284
-
285
- {%- if ns.last_tool_failed -%}
286
- {%- if ns.consecutive_failures >= 2 -%}
287
- {{- '\n\n[SYSTEM WARNING: ' ~ ns.consecutive_failures ~ ' consecutive tool errors detected. Your previous approach is incorrect.]' -}}
288
- {%- else -%}
289
- {{- '\n\n[SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with corrected arguments.]' -}}
290
- {%- endif -%}
291
- {%- endif -%}
292
-
293
- {%- if _next_role == 'tool' -%}
294
- {{- '\n</tool_response>\n' -}}
295
- {%- else -%}
296
- {{- '\n</tool_response>' -}}
297
- {{- '<|im_end|>\n' -}}
298
- {%- endif -%}
299
-
300
- {%- else -%}
301
- {{- raise_exception('Unexpected message role: ' + message.role) -}}
302
- {%- endif -%}
303
-
304
- {%- endfor -%}
305
-
306
- {#- ===== SECTION 9: GENERATION PROMPT ===== -#}
307
- {%- if add_generation_prompt -%}
308
- {{- '<|im_start|>assistant\n' -}}
309
- {%- if ns.enable_thinking -%}
310
- {{- '<think>\n' -}}
311
- {%- else -%}
312
- {{- '<think>\n\n</think>\n\n' -}}
313
- {%- endif -%}
314
- {%- endif -%}