THe-Plague commited on
Commit
33a98cf
·
verified ·
1 Parent(s): 4c5ff3b

Upload chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +227 -0
chat_template.jinja ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- set image_count = namespace(value=0) -%}
2
+ {%- set video_count = namespace(value=0) -%}
3
+ {%- set ns = namespace(enable_thinking=true, multi_step_tool=true, last_query_index=messages|length - 1) -%}
4
+ {#-
5
+ Patched Qwen3.6 chat template for coding-agent/tool-calling use.
6
+
7
+ This is a conservative drop-in patch for Qwen3.6-family repos. It preserves the
8
+ official Qwen3.6 vision/text rendering and XML-like tool-call syntax, while fixing
9
+ common coding-agent issues around developer messages, thinking toggles, historical
10
+ tool calls, and tool responses.
11
+
12
+ Key changes from the official Qwen3.6 template:
13
+ - Preserve image/video/text content handling from upstream Qwen3.6.
14
+ - Fold consecutive leading system/developer messages into one system block.
15
+ - Support <|think_on|> / <|think_off|> and the enable_thinking flag.
16
+ - Avoid exposing thinking-toggle control tokens to the model.
17
+ - Avoid injecting empty historical <think> blocks unless generation is explicitly think-off.
18
+ - Serialize assistant.tool_calls safely for mapping arguments and JSON-string arguments.
19
+ - Include Function: <name> in tool responses when available.
20
+ -#}
21
+ {%- macro render_content(content, do_vision_count, is_system_content=false) %}
22
+ {%- if content is string %}
23
+ {{- content }}
24
+ {%- elif content is iterable and content is not mapping %}
25
+ {%- for item in content %}
26
+ {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
27
+ {%- if is_system_content %}
28
+ {{- raise_exception('System/developer message cannot contain images.') }}
29
+ {%- endif %}
30
+ {%- if do_vision_count %}
31
+ {%- set image_count.value = image_count.value + 1 %}
32
+ {%- endif %}
33
+ {%- if add_vision_id is defined and add_vision_id %}
34
+ {{- 'Picture ' ~ image_count.value ~ ': ' }}
35
+ {%- endif %}
36
+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
37
+ {%- elif 'video' in item or item.type == 'video' %}
38
+ {%- if is_system_content %}
39
+ {{- raise_exception('System/developer message cannot contain videos.') }}
40
+ {%- endif %}
41
+ {%- if do_vision_count %}
42
+ {%- set video_count.value = video_count.value + 1 %}
43
+ {%- endif %}
44
+ {%- if add_vision_id is defined and add_vision_id %}
45
+ {{- 'Video ' ~ video_count.value ~ ': ' }}
46
+ {%- endif %}
47
+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
48
+ {%- elif 'text' in item %}
49
+ {{- item.text }}
50
+ {%- else %}
51
+ {{- raise_exception('Unexpected item type in content.') }}
52
+ {%- endif %}
53
+ {%- endfor %}
54
+ {%- elif content is none or content is undefined %}
55
+ {{- '' }}
56
+ {%- else %}
57
+ {{- raise_exception('Unexpected content type.') }}
58
+ {%- endif %}
59
+ {%- endmacro %}
60
+
61
+ {%- macro process_thinking_toggles(content) %}
62
+ {%- if '<|think_off|>' in content %}
63
+ {%- set ns.enable_thinking = false %}
64
+ {{- content | replace('<|think_off|>', '') | trim }}
65
+ {%- elif '<|think_on|>' in content %}
66
+ {%- set ns.enable_thinking = true %}
67
+ {{- content | replace('<|think_on|>', '') | trim }}
68
+ {%- else %}
69
+ {{- content | trim }}
70
+ {%- endif %}
71
+ {%- endmacro %}
72
+
73
+ {%- if enable_thinking is defined and enable_thinking is false %}
74
+ {%- set ns.enable_thinking = false %}
75
+ {%- endif %}
76
+
77
+ {%- if not messages %}
78
+ {{- raise_exception('No messages provided.') }}
79
+ {%- endif %}
80
+
81
+ {#- Fold consecutive leading system/developer messages into one system block. -#}
82
+ {%- set leading = namespace(content='', end_index=0) %}
83
+ {%- for message in messages %}
84
+ {%- if loop.index0 == leading.end_index and (message.role == 'system' or message.role == 'developer') %}
85
+ {%- set content = render_content(message.content, false, true) %}
86
+ {%- set content = process_thinking_toggles(content) %}
87
+ {%- if content %}
88
+ {%- if leading.content %}
89
+ {%- set leading.content = leading.content + '\n\n' + content %}
90
+ {%- else %}
91
+ {%- set leading.content = content %}
92
+ {%- endif %}
93
+ {%- endif %}
94
+ {%- set leading.end_index = loop.index0 + 1 %}
95
+ {%- endif %}
96
+ {%- endfor %}
97
+
98
+ {%- if tools and tools is iterable and tools is not mapping %}
99
+ {{- '<|im_start|>system\n' }}
100
+ {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
101
+ {%- for tool in tools %}
102
+ {{- "\n" }}
103
+ {{- tool | tojson }}
104
+ {%- endfor %}
105
+ {{- "\n</tools>" }}
106
+ {{- '\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>' }}
107
+ {%- if leading.content %}
108
+ {{- '\n\n' + leading.content }}
109
+ {%- endif %}
110
+ {{- '<|im_end|>\n' }}
111
+ {%- else %}
112
+ {%- if leading.content %}
113
+ {{- '<|im_start|>system\n' + leading.content + '<|im_end|>\n' }}
114
+ {%- endif %}
115
+ {%- endif %}
116
+
117
+ {#- Find the last real user query. Trailing tool-response user blocks are part of multi-step tool use. -#}
118
+ {%- for message in messages[::-1] %}
119
+ {%- set index = (messages|length - 1) - loop.index0 %}
120
+ {%- if ns.multi_step_tool and message.role == "user" %}
121
+ {%- set content = render_content(message.content, false) | trim %}
122
+ {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
123
+ {%- set ns.multi_step_tool = false %}
124
+ {%- set ns.last_query_index = index %}
125
+ {%- endif %}
126
+ {%- endif %}
127
+ {%- endfor %}
128
+
129
+ {%- if ns.multi_step_tool %}
130
+ {{- raise_exception('No user query found in messages.') }}
131
+ {%- endif %}
132
+
133
+ {%- for message in messages %}
134
+ {%- set content = render_content(message.content, true) | trim %}
135
+
136
+ {%- if loop.index0 < leading.end_index %}
137
+ {#- Already rendered in the leading system block. -#}
138
+ {%- elif message.role == "system" or message.role == "developer" %}
139
+ {{- raise_exception('System/developer messages must be consecutive and at the beginning.') }}
140
+ {%- elif message.role == "user" %}
141
+ {%- set content = process_thinking_toggles(content) %}
142
+ {{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}
143
+ {%- elif message.role == "assistant" %}
144
+ {%- set reasoning_content = '' %}
145
+ {%- if message.reasoning_content is string %}
146
+ {%- set reasoning_content = message.reasoning_content %}
147
+ {%- else %}
148
+ {%- if '</think>' in content %}
149
+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
150
+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}
151
+ {%- elif '</thinking>' in content %}
152
+ {%- set reasoning_content = content.split('</thinking>')[0].rstrip('\n').split('<thinking>')[-1].lstrip('\n') %}
153
+ {%- set content = content.split('</thinking>')[-1].lstrip('\n') %}
154
+ {%- endif %}
155
+ {%- endif %}
156
+ {%- set reasoning_content = reasoning_content | trim %}
157
+
158
+ {{- '<|im_start|>assistant\n' }}
159
+ {%- if reasoning_content and loop.index0 > ns.last_query_index %}
160
+ {{- '<think>\n' + reasoning_content + '\n</think>\n\n' }}
161
+ {%- endif %}
162
+ {{- content }}
163
+
164
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
165
+ {%- for tool_call in message.tool_calls %}
166
+ {%- if tool_call.function is defined %}
167
+ {%- set tool_call = tool_call.function %}
168
+ {%- endif %}
169
+ {%- if loop.first %}
170
+ {%- if content | trim %}
171
+ {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
172
+ {%- else %}
173
+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
174
+ {%- endif %}
175
+ {%- else %}
176
+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
177
+ {%- endif %}
178
+
179
+ {%- if tool_call.arguments is defined %}
180
+ {%- set t_args = tool_call.arguments %}
181
+ {%- if t_args is mapping %}
182
+ {%- for args_name, args_value in t_args | items %}
183
+ {{- '<parameter=' + args_name + '>\n' }}
184
+ {%- set args_value = args_value | string if args_value is string else args_value | tojson %}
185
+ {{- args_value }}
186
+ {{- '\n</parameter>\n' }}
187
+ {%- endfor %}
188
+ {%- elif t_args is string %}
189
+ {{- '<parameter=arguments>\n' }}
190
+ {{- t_args }}
191
+ {{- '\n</parameter>\n' }}
192
+ {%- else %}
193
+ {{- '<parameter=arguments>\n' }}
194
+ {{- t_args | tojson }}
195
+ {{- '\n</parameter>\n' }}
196
+ {%- endif %}
197
+ {%- endif %}
198
+ {{- '</function>\n</tool_call>' }}
199
+ {%- endfor %}
200
+ {%- endif %}
201
+ {{- '<|im_end|>\n' }}
202
+ {%- elif message.role == "tool" %}
203
+ {%- if loop.index0 == 0 or messages[loop.index0 - 1].role != "tool" %}
204
+ {{- '<|im_start|>user' }}
205
+ {%- endif %}
206
+ {{- '\n<tool_response>\n' }}
207
+ {%- if message.name is defined and message.name %}
208
+ {{- 'Function: ' + message.name + '\n' }}
209
+ {%- endif %}
210
+ {{- content }}
211
+ {{- '\n</tool_response>' }}
212
+ {%- if loop.last or messages[loop.index0 + 1].role != "tool" %}
213
+ {{- '<|im_end|>\n' }}
214
+ {%- endif %}
215
+ {%- else %}
216
+ {{- raise_exception('Unexpected message role.') }}
217
+ {%- endif %}
218
+ {%- endfor %}
219
+
220
+ {%- if add_generation_prompt %}
221
+ {{- '<|im_start|>assistant\n' }}
222
+ {%- if ns.enable_thinking %}
223
+ {{- '<think>\n' }}
224
+ {%- else %}
225
+ {{- '<think>\n\n</think>\n\n' }}
226
+ {%- endif %}
227
+ {%- endif %}