froggeric commited on
Commit
86d0257
·
verified ·
1 Parent(s): 1dc4a4a

Upload chat_template-v9.jinja

Browse files
Files changed (1) hide show
  1. qwen3.5/chat_template-v9.jinja +219 -0
qwen3.5/chat_template-v9.jinja ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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|>', '') | trim %}
113
+ {%- elif '<|think_on|>' in content %}
114
+ {%- set ns_flags.enable_thinking = true %}
115
+ {%- set content = content | replace('<|think_on|>', '') | trim %}
116
+ {%- endif %}
117
+ {%- if is_system %}
118
+ {%- if not loop.first and content %}
119
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
120
+ {%- endif %}
121
+ {%- elif message.role == "user" %}
122
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
123
+ {%- elif message.role == "assistant" %}
124
+ {%- set reasoning_content = '' %}
125
+ {%- if message.reasoning_content is string %}
126
+ {%- set reasoning_content = message.reasoning_content %}
127
+ {%- else %}
128
+ {%- if '</think>' in content %}
129
+ {%- set reasoning_content = content.split('</think>')[0].split('<think>')[-1] %}
130
+ {%- set content = content.split('</think>')[-1] %}
131
+ {%- if reasoning_content.endswith('\n') %}
132
+ {%- set reasoning_content = reasoning_content[:-1] %}
133
+ {%- endif %}
134
+ {%- if reasoning_content.startswith('\n') %}
135
+ {%- set reasoning_content = reasoning_content[1:] %}
136
+ {%- endif %}
137
+ {%- if content.startswith('\n') %}
138
+ {%- set content = content[1:] %}
139
+ {%- endif %}
140
+ {%- elif '<think>' in content %}
141
+ {#- Auto-close unclosed think before tool_call (compatibility-safe: no rfind/slice) -#}
142
+ {%- set think_part = content.split('<think>')[-1] %}
143
+ {%- if '<tool_call>' in think_part %}
144
+ {%- set reasoning_content = think_part.split('<tool_call>')[0] %}
145
+ {%- set content = '<tool_call>' ~ think_part.split('<tool_call>')[1:] | join('<tool_call>') %}
146
+ {%- else %}
147
+ {%- set reasoning_content = think_part %}
148
+ {%- set content = '' %}
149
+ {%- endif %}
150
+ {#- Ensure reasoning_content doesn't have leading whitespace like newlines -#}
151
+ {%- if reasoning_content.startswith('\n') %}
152
+ {%- set reasoning_content = reasoning_content[1:] %}
153
+ {%- endif %}
154
+ {%- endif %}
155
+ {%- endif %}
156
+ {%- set reasoning_content = reasoning_content|trim %}
157
+ {%- if loop.index0 > ns.last_query_index %}
158
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
159
+ {%- else %}
160
+ {{- '<|im_start|>' + message.role + '\n' + content }}
161
+ {%- endif %}
162
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
163
+ {%- for tool_call in message.tool_calls %}
164
+ {%- if tool_call.function is defined %}
165
+ {%- set tool_call = tool_call.function %}
166
+ {%- endif %}
167
+ {%- if loop.first %}
168
+ {%- if content|trim %}
169
+ {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
170
+ {%- else %}
171
+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
172
+ {%- endif %}
173
+ {%- else %}
174
+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
175
+ {%- endif %}
176
+ {%- if tool_call.arguments is defined and tool_call.arguments is mapping %}
177
+ {%- if tool_call.arguments|length > 0 %}
178
+ {%- for args_name in tool_call.arguments %}
179
+ {%- set args_value = tool_call.arguments[args_name] %}
180
+ {{- '<parameter=' + args_name + '>\n' }}
181
+ {%- set args_value = args_value | tojson if args_value is mapping or (args_value is iterable and args_value is not string) else args_value | string %}
182
+ {{- args_value }}
183
+ {{- '\n</parameter>\n' }}
184
+ {%- endfor %}
185
+ {%- endif %}
186
+ {%- elif tool_call.arguments is defined and tool_call.arguments is string %}
187
+ {%- if tool_call.arguments|trim|length > 0 %}
188
+ {{- tool_call.arguments }}
189
+ {{- '\n' }}
190
+ {%- endif %}
191
+ {%- endif %}
192
+ {{- '</function>\n</tool_call>' }}
193
+ {%- endfor %}
194
+ {%- endif %}
195
+ {{- '<|im_end|>\n' }}
196
+ {%- elif message.role == "tool" %}
197
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
198
+ {{- '<|im_start|>user' }}
199
+ {%- endif %}
200
+ {{- '\n<tool_response>\n' }}
201
+ {{- content }}
202
+ {{- '\n</tool_response>' }}
203
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
204
+ {{- '<|im_end|>\n' }}
205
+ {%- elif loop.last %}
206
+ {{- '<|im_end|>\n' }}
207
+ {%- endif %}
208
+ {%- else %}
209
+ {{- raise_exception('Unexpected message role.') }}
210
+ {%- endif %}
211
+ {%- endfor %}
212
+ {%- if add_generation_prompt %}
213
+ {{- '<|im_start|>assistant\n' }}
214
+ {%- if ns_flags.enable_thinking is false %}
215
+ {{- '<think>\n\n</think>\n\n' }}
216
+ {%- else %}
217
+ {{- '<think>\n' }}
218
+ {%- endif %}
219
+ {%- endif %}