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

Upload chat_template-v8.jinja

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