froggeric commited on
Commit
1dc4a4a
·
verified ·
1 Parent(s): 53a6b5b

Upload chat_template-v9.jinja

Browse files
Files changed (1) hide show
  1. qwen3.6/chat_template-v9.jinja +233 -0
qwen3.6/chat_template-v9.jinja ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- set image_count = namespace(value=0) %}
2
+ {%- set video_count = namespace(value=0) %}
3
+ {%- macro render_content(content, do_vision_count, is_system_content=false) %}
4
+ {%- if content is string %}
5
+ {{- content }}
6
+ {%- elif content is iterable and content is not mapping %}
7
+ {%- for item in content %}
8
+ {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
9
+ {%- if is_system_content %}
10
+ {{- raise_exception('System message cannot contain images.') }}
11
+ {%- endif %}
12
+ {%- if do_vision_count %}
13
+ {%- set image_count.value = image_count.value + 1 %}
14
+ {%- endif %}
15
+ {%- if add_vision_id is defined and add_vision_id %}
16
+ {{- 'Picture ' ~ image_count.value ~ ': ' }}
17
+ {%- endif %}
18
+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
19
+ {%- elif 'video' in item or item.type == 'video' %}
20
+ {%- if is_system_content %}
21
+ {{- raise_exception('System message cannot contain videos.') }}
22
+ {%- endif %}
23
+ {%- if do_vision_count %}
24
+ {%- set video_count.value = video_count.value + 1 %}
25
+ {%- endif %}
26
+ {%- if add_vision_id is defined and add_vision_id %}
27
+ {{- 'Video ' ~ video_count.value ~ ': ' }}
28
+ {%- endif %}
29
+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
30
+ {%- elif 'text' in item %}
31
+ {{- item.text }}
32
+ {%- else %}
33
+ {{- raise_exception('Unexpected item type in content.') }}
34
+ {%- endif %}
35
+ {%- endfor %}
36
+ {%- elif content is none or content is undefined %}
37
+ {{- '' }}
38
+ {%- else %}
39
+ {{- raise_exception('Unexpected content type.') }}
40
+ {%- endif %}
41
+ {%- endmacro %}
42
+ {%- set ns_flags = namespace(enable_thinking=true) %}
43
+ {%- if enable_thinking is defined %}
44
+ {%- set ns_flags.enable_thinking = enable_thinking %}
45
+ {%- endif %}
46
+ {%- if not messages %}
47
+ {{- raise_exception('No messages provided.') }}
48
+ {%- endif %}
49
+ {%- set system_content = '' %}
50
+ {%- set has_system = false %}
51
+ {%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
52
+ {%- set has_system = true %}
53
+ {%- set system_content = render_content(messages[0].content, false, true)|trim %}
54
+ {%- if '<|think_off|>' in system_content %}
55
+ {%- set ns_flags.enable_thinking = false %}
56
+ {%- set system_content = system_content | replace('<|think_off|>', '') %}
57
+ {%- endif %}
58
+ {%- if '<|think_on|>' in system_content %}
59
+ {%- set ns_flags.enable_thinking = true %}
60
+ {%- set system_content = system_content | replace('<|think_on|>', '') %}
61
+ {%- endif %}
62
+ {%- set system_content = system_content | trim %}
63
+ {%- endif %}
64
+ {%- if tools and tools is iterable and tools is not mapping %}
65
+ {{- '<|im_start|>system\n' }}
66
+ {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
67
+ {%- for tool in tools %}
68
+ {{- "\n" }}
69
+ {{- tool | tojson }}
70
+ {%- endfor %}
71
+ {{- "\n</tools>" }}
72
+ {%- set ns_scan = namespace(final_enable=ns_flags.enable_thinking) %}
73
+ {%- for message in messages %}
74
+ {%- set content_str = message.content | string %}
75
+ {%- if '<|think_off|>' in content_str %}
76
+ {%- set ns_scan.final_enable = false %}
77
+ {%- elif '<|think_on|>' in content_str %}
78
+ {%- set ns_scan.final_enable = true %}
79
+ {%- endif %}
80
+ {%- endfor %}
81
+ {%- if ns_scan.final_enable %}
82
+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<think>\nBrief explanation of tool call\n</think>\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 MUST provide reasoning for your function call within a <think></think> block BEFORE the <tool_call>\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>' }}
83
+ {%- else %}
84
+ {{- '\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- 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>' }}
85
+ {%- endif %}
86
+ {%- if has_system and system_content %}
87
+ {{- '\n\n' + system_content }}
88
+ {%- endif %}
89
+ {{- '<|im_end|>\n' }}
90
+ {%- elif has_system and system_content %}
91
+ {{- '<|im_start|>system\n' + system_content + '<|im_end|>\n' }}
92
+ {%- endif %}
93
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
94
+ {%- for message in messages[::-1] %}
95
+ {%- set index = (messages|length - 1) - loop.index0 %}
96
+ {%- if ns.multi_step_tool and message.role == "user" %}
97
+ {%- set content = render_content(message.content, false)|trim %}
98
+ {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
99
+ {%- set ns.multi_step_tool = false %}
100
+ {%- set ns.last_query_index = index %}
101
+ {%- endif %}
102
+ {%- endif %}
103
+ {%- endfor %}
104
+ {%- if ns.multi_step_tool %}
105
+ {%- set ns.last_query_index = messages|length - 1 %}
106
+ {%- endif %}
107
+ {%- for message in messages %}
108
+ {%- set is_system = (message.role == "system" or message.role == "developer") %}
109
+ {%- set content = render_content(message.content, true, is_system)|trim %}
110
+ {%- if '<|think_off|>' in content %}
111
+ {%- set ns_flags.enable_thinking = false %}
112
+ {%- set content = content | replace('<|think_off|>', '') %}
113
+ {%- endif %}
114
+ {%- if '<|think_on|>' in content %}
115
+ {%- set ns_flags.enable_thinking = true %}
116
+ {%- set content = content | replace('<|think_on|>', '') %}
117
+ {%- endif %}
118
+ {%- set content = content | trim %}
119
+ {%- if is_system %}
120
+ {%- if not loop.first and content %}
121
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
122
+ {%- endif %}
123
+ {%- elif message.role == "user" %}
124
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
125
+ {%- elif message.role == "assistant" %}
126
+ {%- set reasoning_content = '' %}
127
+ {%- if message.reasoning_content is string %}
128
+ {%- set reasoning_content = message.reasoning_content %}
129
+ {%- else %}
130
+ {%- set think_end_token = '' %}
131
+ {%- if '</think>' in content %}
132
+ {%- set think_end_token = '</think>' %}
133
+ {%- elif '</thinking>' in content %}
134
+ {%- set think_end_token = '</thinking>' %}
135
+ {%- elif '<think>' in content %}
136
+ {#- Auto-close unclosed think before tool_call (compatibility-safe: no rfind/slice) -#}
137
+ {%- set think_part = content.split('<think>')[-1] %}
138
+ {%- if '<tool_call>' in think_part %}
139
+ {%- set reasoning_content = think_part.split('<tool_call>')[0] %}
140
+ {%- set content = '<tool_call>' ~ think_part.split('<tool_call>')[1:] | join('<tool_call>') %}
141
+ {%- else %}
142
+ {%- set reasoning_content = think_part %}
143
+ {%- set content = '' %}
144
+ {%- endif %}
145
+ {#- Ensure reasoning_content doesn't have leading whitespace like newlines -#}
146
+ {%- if reasoning_content.startswith('\n') %}
147
+ {%- set reasoning_content = reasoning_content[1:] %}
148
+ {%- endif %}
149
+ {%- endif %}
150
+ {%- if think_end_token %}
151
+ {%- set reasoning_content = content.split(think_end_token)[0].split('<think>')[-1] %}
152
+ {%- set content = content.split(think_end_token)[-1] %}
153
+ {%- if reasoning_content.endswith('\n') %}
154
+ {%- set reasoning_content = reasoning_content[:-1] %}
155
+ {%- endif %}
156
+ {%- if reasoning_content.startswith('\n') %}
157
+ {%- set reasoning_content = reasoning_content[1:] %}
158
+ {%- endif %}
159
+ {%- if content.startswith('\n') %}
160
+ {%- set content = content[1:] %}
161
+ {%- endif %}
162
+ {%- endif %}
163
+ {%- endif %}
164
+ {%- set reasoning_content = reasoning_content|trim %}
165
+ {%- set show_think = false %}
166
+ {%- if loop.index0 > ns.last_query_index %}
167
+ {%- set show_think = true %}
168
+ {%- elif ns_flags.enable_thinking and (preserve_thinking is undefined or preserve_thinking is true) and reasoning_content|length > 0 %}
169
+ {%- set show_think = true %}
170
+ {%- endif %}
171
+ {%- if show_think %}
172
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
173
+ {%- else %}
174
+ {{- '<|im_start|>' + message.role + '\n' + content }}
175
+ {%- endif %}
176
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
177
+ {%- for tool_call in message.tool_calls %}
178
+ {%- if tool_call.function is defined %}
179
+ {%- set tool_call = tool_call.function %}
180
+ {%- endif %}
181
+ {%- if loop.first %}
182
+ {%- if content|trim %}
183
+ {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
184
+ {%- else %}
185
+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
186
+ {%- endif %}
187
+ {%- else %}
188
+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
189
+ {%- endif %}
190
+ {%- if tool_call.arguments is defined and tool_call.arguments is mapping %}
191
+ {%- if tool_call.arguments|length > 0 %}
192
+ {%- for args_name in tool_call.arguments %}
193
+ {%- set args_value = tool_call.arguments[args_name] %}
194
+ {{- '<parameter=' + args_name + '>\n' }}
195
+ {%- set args_value = args_value | string if args_value is string else args_value | tojson %}
196
+ {{- args_value }}
197
+ {{- '\n</parameter>\n' }}
198
+ {%- endfor %}
199
+ {%- endif %}
200
+ {%- elif tool_call.arguments is defined and tool_call.arguments is string %}
201
+ {%- if tool_call.arguments|trim|length > 0 %}
202
+ {{- tool_call.arguments }}
203
+ {{- '\n' }}
204
+ {%- endif %}
205
+ {%- endif %}
206
+ {{- '</function>\n</tool_call>' }}
207
+ {%- endfor %}
208
+ {%- endif %}
209
+ {{- '<|im_end|>\n' }}
210
+ {%- elif message.role == "tool" %}
211
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
212
+ {{- '<|im_start|>user' }}
213
+ {%- endif %}
214
+ {{- '\n<tool_response>\n' }}
215
+ {{- content }}
216
+ {{- '\n</tool_response>' }}
217
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
218
+ {{- '<|im_end|>\n' }}
219
+ {%- elif loop.last %}
220
+ {{- '<|im_end|>\n' }}
221
+ {%- endif %}
222
+ {%- else %}
223
+ {{- raise_exception('Unexpected message role.') }}
224
+ {%- endif %}
225
+ {%- endfor %}
226
+ {%- if add_generation_prompt %}
227
+ {{- '<|im_start|>assistant\n' }}
228
+ {%- if ns_flags.enable_thinking is false %}
229
+ {{- '<think>\n\n</think>\n\n' }}
230
+ {%- else %}
231
+ {{- '<think>\n' }}
232
+ {%- endif %}
233
+ {%- endif %}