froggeric commited on
Commit
781cc51
·
verified ·
1 Parent(s): 81ec3f0

Upload chat_template-v8.jinja

Browse files
Files changed (1) hide show
  1. qwen3.6/chat_template-v8.jinja +229 -0
qwen3.6/chat_template-v8.jinja ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ {%- if tools and tools is iterable and tools is not mapping %}
50
+ {{- '<|im_start|>system\n' }}
51
+ {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
52
+ {%- for tool in tools %}
53
+ {{- "\n" }}
54
+ {{- tool | tojson }}
55
+ {%- endfor %}
56
+ {{- "\n</tools>" }}
57
+ {{- '\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>' }}
58
+ {%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
59
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
60
+ {%- if '<|think_off|>' in content %}
61
+ {%- set ns_flags.enable_thinking = false %}
62
+ {%- set content = content | replace('<|think_off|>', '') %}
63
+ {%- endif %}
64
+ {%- if '<|think_on|>' in content %}
65
+ {%- set ns_flags.enable_thinking = true %}
66
+ {%- set content = content | replace('<|think_on|>', '') %}
67
+ {%- endif %}
68
+ {%- set content = content | trim %}
69
+ {%- if content %}
70
+ {{- '\n\n' + content }}
71
+ {%- endif %}
72
+ {%- endif %}
73
+ {{- '<|im_end|>\n' }}
74
+ {%- else %}
75
+ {%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
76
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
77
+ {%- if '<|think_off|>' in content %}
78
+ {%- set ns_flags.enable_thinking = false %}
79
+ {%- set content = content | replace('<|think_off|>', '') %}
80
+ {%- endif %}
81
+ {%- if '<|think_on|>' in content %}
82
+ {%- set ns_flags.enable_thinking = true %}
83
+ {%- set content = content | replace('<|think_on|>', '') %}
84
+ {%- endif %}
85
+ {%- set content = content | trim %}
86
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
87
+ {%- endif %}
88
+ {%- endif %}
89
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
90
+ {%- for message in messages[::-1] %}
91
+ {%- set index = (messages|length - 1) - loop.index0 %}
92
+ {%- if ns.multi_step_tool and message.role == "user" %}
93
+ {%- set content = render_content(message.content, false)|trim %}
94
+ {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
95
+ {%- set ns.multi_step_tool = false %}
96
+ {%- set ns.last_query_index = index %}
97
+ {%- endif %}
98
+ {%- endif %}
99
+ {%- endfor %}
100
+ {%- if ns.multi_step_tool %}
101
+ {%- set ns.last_query_index = messages|length - 1 %}
102
+ {%- endif %}
103
+ {%- for message in messages %}
104
+ {%- set is_system = (message.role == "system" or message.role == "developer") %}
105
+ {%- set content = render_content(message.content, true, is_system)|trim %}
106
+ {%- if '<|think_off|>' in content %}
107
+ {%- set ns_flags.enable_thinking = false %}
108
+ {%- set content = content | replace('<|think_off|>', '') %}
109
+ {%- endif %}
110
+ {%- if '<|think_on|>' in content %}
111
+ {%- set ns_flags.enable_thinking = true %}
112
+ {%- set content = content | replace('<|think_on|>', '') %}
113
+ {%- endif %}
114
+ {%- set content = content | trim %}
115
+ {%- if is_system %}
116
+ {%- if not loop.first and content %}
117
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
118
+ {%- endif %}
119
+ {%- elif message.role == "user" %}
120
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
121
+ {%- elif message.role == "assistant" %}
122
+ {%- set reasoning_content = '' %}
123
+ {%- if message.reasoning_content is string %}
124
+ {%- set reasoning_content = message.reasoning_content %}
125
+ {%- else %}
126
+ {%- set think_end_token = '' %}
127
+ {%- if '</think>' in content %}
128
+ {%- set think_end_token = '</think>' %}
129
+ {%- elif '</thinking>' in content %}
130
+ {%- set think_end_token = '</thinking>' %}
131
+ {%- elif '<think>' in content %}
132
+ {#- Auto-close unclosed think before tool_call (compatibility-safe: no rfind/slice) -#}
133
+ {%- set think_part = content.split('<think>')[-1] %}
134
+ {%- if '<tool_call>' in think_part %}
135
+ {%- set reasoning_content = think_part.split('<tool_call>')[0] %}
136
+ {%- set content = '<tool_call>' ~ think_part.split('<tool_call>')[1:] | join('<tool_call>') %}
137
+ {%- else %}
138
+ {%- set reasoning_content = think_part %}
139
+ {%- set content = '' %}
140
+ {%- endif %}
141
+ {#- Ensure reasoning_content doesn't have leading whitespace like newlines -#}
142
+ {%- if reasoning_content.startswith('\n') %}
143
+ {%- set reasoning_content = reasoning_content[1:] %}
144
+ {%- endif %}
145
+ {%- endif %}
146
+ {%- if think_end_token %}
147
+ {%- set reasoning_content = content.split(think_end_token)[0].split('<think>')[-1] %}
148
+ {%- set content = content.split(think_end_token)[-1] %}
149
+ {%- if reasoning_content.endswith('\n') %}
150
+ {%- set reasoning_content = reasoning_content[:-1] %}
151
+ {%- endif %}
152
+ {%- if reasoning_content.startswith('\n') %}
153
+ {%- set reasoning_content = reasoning_content[1:] %}
154
+ {%- endif %}
155
+ {%- if content.startswith('\n') %}
156
+ {%- set content = content[1:] %}
157
+ {%- endif %}
158
+ {%- endif %}
159
+ {%- endif %}
160
+ {%- set reasoning_content = reasoning_content|trim %}
161
+ {%- set show_think = false %}
162
+ {%- if loop.index0 > ns.last_query_index %}
163
+ {%- set show_think = true %}
164
+ {%- elif ns_flags.enable_thinking and (preserve_thinking is undefined or preserve_thinking is true) and reasoning_content|length > 0 %}
165
+ {%- set show_think = true %}
166
+ {%- endif %}
167
+ {%- if show_think %}
168
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
169
+ {%- else %}
170
+ {{- '<|im_start|>' + message.role + '\n' + content }}
171
+ {%- endif %}
172
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
173
+ {%- for tool_call in message.tool_calls %}
174
+ {%- if tool_call.function is defined %}
175
+ {%- set tool_call = tool_call.function %}
176
+ {%- endif %}
177
+ {%- if loop.first %}
178
+ {%- if content|trim %}
179
+ {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
180
+ {%- else %}
181
+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
182
+ {%- endif %}
183
+ {%- else %}
184
+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
185
+ {%- endif %}
186
+ {%- if tool_call.arguments is defined and tool_call.arguments is mapping %}
187
+ {%- if tool_call.arguments|length > 0 %}
188
+ {%- for args_name in tool_call.arguments %}
189
+ {%- set args_value = tool_call.arguments[args_name] %}
190
+ {{- '<parameter=' + args_name + '>\n' }}
191
+ {%- set args_value = args_value | string if args_value is string else args_value | tojson %}
192
+ {{- args_value }}
193
+ {{- '\n</parameter>\n' }}
194
+ {%- endfor %}
195
+ {%- endif %}
196
+ {%- elif tool_call.arguments is defined and tool_call.arguments is string %}
197
+ {%- if tool_call.arguments|trim|length > 0 %}
198
+ {{- tool_call.arguments }}
199
+ {{- '\n' }}
200
+ {%- endif %}
201
+ {%- endif %}
202
+ {{- '</function>\n</tool_call>' }}
203
+ {%- endfor %}
204
+ {%- endif %}
205
+ {{- '<|im_end|>\n' }}
206
+ {%- elif message.role == "tool" %}
207
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
208
+ {{- '<|im_start|>user' }}
209
+ {%- endif %}
210
+ {{- '\n<tool_response>\n' }}
211
+ {{- content }}
212
+ {{- '\n</tool_response>' }}
213
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
214
+ {{- '<|im_end|>\n' }}
215
+ {%- elif loop.last %}
216
+ {{- '<|im_end|>\n' }}
217
+ {%- endif %}
218
+ {%- else %}
219
+ {{- raise_exception('Unexpected message role.') }}
220
+ {%- endif %}
221
+ {%- endfor %}
222
+ {%- if add_generation_prompt %}
223
+ {{- '<|im_start|>assistant\n' }}
224
+ {%- if ns_flags.enable_thinking is false %}
225
+ {{- '<think>\n\n</think>\n\n' }}
226
+ {%- else %}
227
+ {{- '<think>\n' }}
228
+ {%- endif %}
229
+ {%- endif %}