-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathchat_template.jinja
More file actions
325 lines (304 loc) · 16.3 KB
/
chat_template.jinja
File metadata and controls
325 lines (304 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
{#- ============================================================= #}
{#- Qwen3.5 Barubary Chat Template v1 #}
{#- Target: Qwen3.5-* #}
{#- #}
{#- A Jinja chat template for Qwen3.5 on llama.cpp, #}
{#- Open WebUI, vLLM, and any OAI-compatible endpoint. #}
{#- #}
{#- Fixes (1-21) over the official Qwen3.5 chat template: #}
{#- 1. add_vision_id / enable_thinking safe defaults #}
{#- 2. Precomputed _last_idx for namespace() constructor #}
{#- 3. Developer role handled (Claude Code / Codex / OpenCode) #}
{#- 4. System/developer split before main loop #}
{#- 5. item.type checked before 'in item' key test #}
{#- 6. arguments.items() replaces bare key iteration #}
{#- 7. | safe filter removed (llama.cpp compat) #}
{#- 8. tojson/string explicit if/else (no chained filters) #}
{#- 9. String arguments pass-through for OAI-compat proxies #}
{#- 10. tc alias avoids shadowing tool_call loop var #}
{#- 11. ns2 namespace replaces loop.previtem / loop.nextitem #}
{#- 12. enable_thinking applied to in-context assistant turns #}
{#- 13. reasoning_content is defined + not none guard #}
{#- 14. loop.index0 > (not >=) for assistant thinking scope #}
{#- 15. Parallel tool calls: \n\n delimiter between blocks + #}
{#- system prompt instruction for clean separation #}
{#- 16. Long tool args/responses: configurable truncation guard #}
{#- via max_tool_arg_chars / max_tool_response_chars #}
{#- 17. Deep agent loops: graceful fallback to index 0 instead #}
{#- of raise_exception when no real user query found #}
{#- 18. Streaming compat: clean newline boundaries on all XML #}
{#- tag openings so streaming parsers can detect blocks #}
{#- 19. Auto-disable thinking when tools active — prevents the #}
{#- known <tool_call>-leaks-into-<think> block bug #}
{#- (configurable via auto_disable_thinking_with_tools) #}
{#- 20. Unknown roles: graceful fallback mapped to user role #}
{#- instead of hard crash (supports planner/critic/etc.) #}
{#- 21. Flattened nesting depth; _has_tools precomputed; all #}
{#- conditionals simplified for llama.cpp minja stability #}
{#- #}
{#- Config variables (pass via --chat-template-kwargs): #}
{#- enable_thinking (bool, default true) #}
{#- Controls whether the model enters <think> mode. #}
{#- Set false for tool-heavy / speed-critical sessions. #}
{#- auto_disable_thinking_with_tools (bool, default true) #}
{#- When true AND tools are provided, thinking is auto-off. #}
{#- Prevents <tool_call> from leaking into <think> blocks. #}
{#- Set false if you explicitly want thinking + tools. #}
{#- add_vision_id (bool, default false) #}
{#- Prefix images/videos with "Picture N:" / "Video N:". #}
{#- max_tool_arg_chars (int, default 0 = unlimited) #}
{#- Truncate tool argument values exceeding this char count. #}
{#- max_tool_response_chars (int, default 0 = unlimited) #}
{#- Truncate tool response content exceeding this char count. #}
{#- ============================================================= #}
{%- set template_version = "qwen3.5-barubary-attuned-v1" %}
{#- ── Config with safe defaults ─────────────────────────────── #}
{%- set image_count = namespace(value=0) %}
{%- set video_count = namespace(value=0) %}
{%- set add_vision_id = add_vision_id if add_vision_id is defined else false %}
{%- set enable_thinking = enable_thinking if enable_thinking is defined else true %}
{%- set auto_disable_thinking_with_tools = auto_disable_thinking_with_tools if auto_disable_thinking_with_tools is defined else false %}
{%- set max_tool_arg_chars = max_tool_arg_chars if max_tool_arg_chars is defined else 0 %}
{%- set max_tool_response_chars = max_tool_response_chars if max_tool_response_chars is defined else 0 %}
{#- FIX21: precompute _has_tools ─────────────────────────────── #}
{%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}
{#- FIX19: auto-disable thinking when tools active ─────────── #}
{%- set _thinking = enable_thinking %}
{%- if auto_disable_thinking_with_tools and _has_tools %}
{%- set _thinking = false %}
{%- endif %}
{#- ── render_content macro ──────────────────────────────────── #}
{%- macro render_content(content, do_vision_count, is_system_content=false) %}
{%- if content is string %}
{{- content }}
{%- elif content is iterable and content is not mapping %}
{%- for item in content %}
{%- if item is mapping %}
{%- if item.type == 'image' or 'image' in item or 'image_url' in item %}
{%- if is_system_content %}
{{- raise_exception('System message cannot contain images.') }}
{%- endif %}
{%- if do_vision_count %}
{%- set image_count.value = image_count.value + 1 %}
{%- endif %}
{%- if add_vision_id %}
{{- 'Picture ' ~ image_count.value ~ ': ' }}
{%- endif %}
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
{%- elif item.type == 'video' or 'video' in item %}
{%- if is_system_content %}
{{- raise_exception('System message cannot contain videos.') }}
{%- endif %}
{%- if do_vision_count %}
{%- set video_count.value = video_count.value + 1 %}
{%- endif %}
{%- if add_vision_id %}
{{- 'Video ' ~ video_count.value ~ ': ' }}
{%- endif %}
{{- '<|vision_start|><|video_pad|><|vision_end|>' }}
{%- elif 'text' in item %}
{{- item.text }}
{%- else %}
{{- raise_exception('Unexpected item type in content.') }}
{%- endif %}
{%- else %}
{{- item | string }}
{%- endif %}
{%- endfor %}
{%- elif content is none or content is undefined %}
{{- '' }}
{%- else %}
{{- raise_exception('Unexpected content type.') }}
{%- endif %}
{%- endmacro %}
{#- ── Guard ──────────────────────────────────────────────────── #}
{%- if not messages %}
{{- raise_exception('No messages provided.') }}
{%- endif %}
{#- ── Extract leading system/developer message ────────────────── #}
{%- set _first_role = messages[0].role %}
{%- if _first_role == 'system' or _first_role == 'developer' %}
{%- set _sys_msg = messages[0] %}
{%- set _msgs = messages[1:] %}
{%- else %}
{%- set _sys_msg = none %}
{%- set _msgs = messages %}
{%- endif %}
{#- ── Tools block OR plain system block ─────────────────────── #}
{%- if _has_tools %}
{{- '<|im_start|>system\n' }}
{{- '# Tools\n\nYou have access to the following functions:\n\n<tools>' }}
{%- for tool in tools %}
{{- '\n' }}
{{- tool | tojson }}
{%- endfor %}
{{- '\n</tools>' }}
{{- '\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\n' }}
{{- 'that 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' }}
{{- '- When making multiple function calls, emit each <tool_call> block separated by a blank line\n' }}
{{- '</IMPORTANT>' }}
{%- if _sys_msg is not none %}
{%- set _sc = render_content(_sys_msg.content, false, true) | trim %}
{%- if _sc %}
{{- '\n\n' + _sc }}
{%- endif %}
{%- endif %}
{{- '<|im_end|>\n' }}
{%- else %}
{%- if _sys_msg is not none %}
{%- set _sc = render_content(_sys_msg.content, false, true) | trim %}
{{- '<|im_start|>system\n' + _sc + '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{#- ── Compute last real-user-query index ────────────────────── #}
{#- FIX2 #}
{%- set _last_idx = _msgs | length - 1 %}
{%- set ns = namespace(multi_step_tool=true, last_query_index=_last_idx) %}
{%- for message in _msgs[::-1] %}
{%- set index = (_msgs | length - 1) - loop.index0 %}
{%- if ns.multi_step_tool and message.role == 'user' %}
{%- set _rc = render_content(message.content, false) | trim %}
{%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}
{%- set ns.multi_step_tool = false %}
{%- set ns.last_query_index = index %}
{%- endif %}
{%- endif %}
{%- endfor %}
{#- FIX17: deep agent loop fallback ────────────────────────── #}
{%- if ns.multi_step_tool %}
{%- if _last_idx > 50 %}
{%- set ns.last_query_index = _last_idx %}
{%- else %}
{%- set ns.last_query_index = 0 %}
{%- endif %}
{%- endif %}
{#- ── Main render loop ──────────────────────────────────────── #}
{%- set ns2 = namespace(prev_role='') %}
{%- for message in _msgs %}
{%- set content = render_content(message.content, true) | trim %}
{%- if message.role == 'user' %}
{{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}
{%- elif message.role == 'assistant' %}
{#- ── Extract reasoning_content ── #}
{%- set reasoning_content = '' %}
{%- if message.reasoning_content is defined and message.reasoning_content is not none %}
{%- if message.reasoning_content is string %}
{%- set reasoning_content = message.reasoning_content %}
{%- else %}
{%- set reasoning_content = message.reasoning_content | string %}
{%- endif %}
{%- else %}
{%- if '</think>' in content %}
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
{%- endif %}
{%- endif %}
{%- set reasoning_content = reasoning_content | trim %}
{#- ── Render assistant turn ── #}
{%- if loop.index0 > ns.last_query_index %}
{%- if _thinking %}
{{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
{%- else %}
{{- '<|im_start|>assistant\n<think>\n\n</think>\n\n' + content }}
{%- endif %}
{%- else %}
{{- '<|im_start|>assistant\n' + content }}
{%- endif %}
{#- ── Render tool_calls ── #}
{%- if message.tool_calls is defined and message.tool_calls
and message.tool_calls is iterable
and message.tool_calls is not mapping %}
{%- for tool_call in message.tool_calls %}
{#- FIX10 #}
{%- if tool_call.function is defined and tool_call.function is not none %}
{%- set tc = tool_call.function %}
{%- else %}
{%- set tc = tool_call %}
{%- endif %}
{#- FIX15+FIX18 #}
{%- if loop.first %}
{%- if content | trim %}
{{- '\n\n<tool_call>\n<function=' + tc.name + '>\n' }}
{%- else %}
{{- '<tool_call>\n<function=' + tc.name + '>\n' }}
{%- endif %}
{%- else %}
{{- '\n\n<tool_call>\n<function=' + tc.name + '>\n' }}
{%- endif %}
{#- ── Render arguments ── #}
{%- if tc.arguments is defined and tc.arguments is not none %}
{%- if tc.arguments is mapping %}
{#- FIX6 #}
{%- for args_name, args_value in tc.arguments.items() %}
{{- '<parameter=' + args_name + '>\n' }}
{#- FIX7+FIX8 #}
{%- if args_value is mapping or (args_value is sequence and args_value is not string) %}
{%- set _av = args_value | tojson %}
{%- else %}
{%- set _av = args_value | string %}
{%- endif %}
{#- FIX16 #}
{%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %}
{{- _av[:max_tool_arg_chars] + '\n[TRUNCATED — original length ' ~ (_av | length | string) ~ ' chars]' }}
{%- else %}
{{- _av }}
{%- endif %}
{{- '\n</parameter>\n' }}
{%- endfor %}
{%- elif tc.arguments is string and tc.arguments %}
{#- FIX9 #}
{{- tc.arguments }}
{%- endif %}
{%- endif %}
{%- if loop.last %}
{{- '</function>\n</tool_call>\n' }}
{%- else %}
{{- '</function>\n</tool_call>' }}
{%- endif %}
{%- endfor %}
{%- endif %}
{{- '<|im_end|>\n' }}
{%- elif message.role == 'tool' %}
{#- FIX11 #}
{%- if ns2.prev_role != 'tool' %}
{{- '<|im_start|>user' }}
{%- endif %}
{#- FIX16 #}
{%- if max_tool_response_chars > 0 and content | length > max_tool_response_chars %}
{%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED — original length ' ~ (content | length | string) ~ ' chars]' %}
{%- endif %}
{{- '\n<tool_response>\n' + content + '\n</tool_response>' }}
{#- FIX11 #}
{%- if loop.last %}
{{- '<|im_end|>\n' }}
{%- else %}
{%- set _next_role = _msgs[loop.index0 + 1].role %}
{%- if _next_role != 'tool' %}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{%- elif message.role == 'system' or message.role == 'developer' %}
{#- FIX4: skip — already rendered above #}
{%- else %}
{#- FIX20 #}
{{- '<|im_start|>user\n[' + message.role + ']: ' + content + '<|im_end|>\n' }}
{%- endif %}
{%- set ns2.prev_role = message.role %}
{%- endfor %}
{#- ── Generation prompt ──────────────────────────────────────── #}
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n' }}
{%- if not _thinking %}
{{- '<think>\n\n</think>\n\n' }}
{%- else %}
{{- '<think>\n' }}
{%- endif %}
{%- endif %}