llmfan46 commited on
Commit
8b06e54
·
verified ·
1 Parent(s): 3e6c666

Upload chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +222 -0
chat_template.jinja ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ {%- set preserve_thinking = preserve_thinking | default(true) %}
47
+ {%- if not messages %}
48
+ {{- raise_exception('No messages provided.') }}
49
+ {%- endif %}
50
+ {%- if add_generation_prompt is defined and add_generation_prompt and continue_final_message is defined and continue_final_message %}
51
+ {{- raise_exception('add_generation_prompt and continue_final_message cannot both be true.') }}
52
+ {%- endif %}
53
+ {%- if tools and tools is iterable and tools is not mapping %}
54
+ {{- '<|im_start|>system\n' }}
55
+ {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
56
+ {%- for tool in tools %}
57
+ {{- "\n" }}
58
+ {{- tool | tojson }}
59
+ {%- endfor %}
60
+ {{- "\n</tools>" }}
61
+ {{- '\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>' }}
62
+ {%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
63
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
64
+ {%- if '<|think_off|>' in content %}
65
+ {%- set ns_flags.enable_thinking = false %}
66
+ {%- set content = content.replace('<|think_off|>', '') %}
67
+ {%- endif %}
68
+ {%- if '<|think_on|>' in content %}
69
+ {%- set ns_flags.enable_thinking = true %}
70
+ {%- set content = content.replace('<|think_on|>', '') %}
71
+ {%- endif %}
72
+ {%- set content = content.strip() %}
73
+ {%- if content %}
74
+ {{- '\n\n' + content }}
75
+ {%- endif %}
76
+ {%- endif %}
77
+ {{- '<|im_end|>\n' }}
78
+ {%- else %}
79
+ {%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
80
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
81
+ {%- if '<|think_off|>' in content %}
82
+ {%- set ns_flags.enable_thinking = false %}
83
+ {%- set content = content.replace('<|think_off|>', '') %}
84
+ {%- endif %}
85
+ {%- if '<|think_on|>' in content %}
86
+ {%- set ns_flags.enable_thinking = true %}
87
+ {%- set content = content.replace('<|think_on|>', '') %}
88
+ {%- endif %}
89
+ {%- set content = content.strip() %}
90
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
91
+ {%- endif %}
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 content = render_content(message.content, true)|trim %}
109
+ {%- set content = content.replace('<|think_off|>', '').replace('<|think_on|>', '') %}
110
+ {%- set content = content.strip() %}
111
+ {%- if message.role == "system" or message.role == "developer" %}
112
+ {%- if not loop.first %}
113
+ {%- set sys_content = render_content(message.content, false, true)|trim %}
114
+ {%- set sys_content = sys_content.replace('<|think_off|>', '').replace('<|think_on|>', '')|trim %}
115
+ {{- '<|im_start|>system\n' + sys_content + '<|im_end|>' + '\n' }}
116
+ {%- endif %}
117
+ {%- elif message.role == "user" %}
118
+ {{- '<|im_start|>' + message.role + '\n' + (content if content else ' ') + '<|im_end|>' + '\n' }}
119
+ {%- elif message.role == "assistant" %}
120
+ {%- set reasoning_content = '' %}
121
+ {%- if message.reasoning_content is string %}
122
+ {%- set reasoning_content = message.reasoning_content %}
123
+ {%- else %}
124
+ {%- set has_think_tag = false %}
125
+ {%- set think_start_token = '<think>' %}
126
+ {%- set think_end_token = '</think>' %}
127
+ {%- if '</think>' in content %}
128
+ {%- set has_think_tag = true %}
129
+ {%- elif '</thinking>' in content %}
130
+ {%- set has_think_tag = true %}
131
+ {%- set think_start_token = '<thinking>' %}
132
+ {%- set think_end_token = '</thinking>' %}
133
+ {%- elif '<think>' in content %}
134
+ {%- set reasoning_content = content.split('<think>')[-1].lstrip('\n') %}
135
+ {%- set content = '' %}
136
+ {%- elif '<thinking>' in content %}
137
+ {%- set reasoning_content = content.split('<thinking>')[-1].lstrip('\n') %}
138
+ {%- set content = '' %}
139
+ {%- endif %}
140
+ {%- if has_think_tag %}
141
+ {%- set reasoning_content = content.split(think_end_token)[0].rstrip('\n').split(think_start_token)[-1].lstrip('\n') %}
142
+ {%- set content = content.split(think_end_token)[-1].lstrip('\n') %}
143
+ {%- endif %}
144
+ {%- endif %}
145
+ {%- set reasoning_content = reasoning_content|trim %}
146
+ {%- set show_think = false %}
147
+ {%- if loop.index0 > ns.last_query_index and reasoning_content|length > 0 %}
148
+ {%- set show_think = true %}
149
+ {%- elif ns_flags.enable_thinking and preserve_thinking and reasoning_content|length > 0 %}
150
+ {%- set show_think = true %}
151
+ {%- endif %}
152
+ {%- if show_think %}
153
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
154
+ {%- else %}
155
+ {{- '<|im_start|>' + message.role + '\n' + content }}
156
+ {%- endif %}
157
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
158
+ {%- for tool_call in message.tool_calls %}
159
+ {%- if tool_call.function is defined %}
160
+ {%- set tool_call = tool_call.function %}
161
+ {%- endif %}
162
+ {%- if loop.first %}
163
+ {%- if content|trim %}
164
+ {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
165
+ {%- else %}
166
+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
167
+ {%- endif %}
168
+ {%- else %}
169
+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
170
+ {%- endif %}
171
+ {%- if tool_call.arguments is defined and tool_call.arguments is mapping %}
172
+ {%- if tool_call.arguments|length > 0 %}
173
+ {%- for args_name in tool_call.arguments %}
174
+ {%- set args_value = tool_call.arguments[args_name] %}
175
+ {{- '<parameter=' + args_name + '>\n' }}
176
+ {%- set args_value = args_value | string if args_value is string else args_value | tojson %}
177
+ {{- args_value }}
178
+ {{- '\n</parameter>\n' }}
179
+ {%- endfor %}
180
+ {%- endif %}
181
+ {%- elif tool_call.arguments is defined and tool_call.arguments is string %}
182
+ {%- if tool_call.arguments|trim|length > 0 %}
183
+ {#- Note: raw JSON string arguments are emitted as-is and will not match
184
+ the XML parameter format in the tool instructions. Normalize arguments
185
+ to a dict in your serving layer before applying this template. -#}
186
+ {{- tool_call.arguments }}
187
+ {{- '\n' }}
188
+ {%- endif %}
189
+ {%- endif %}
190
+ {{- '</function>\n</tool_call>' }}
191
+ {%- endfor %}
192
+ {%- endif %}
193
+ {%- if not (loop.last and continue_final_message is defined and continue_final_message is true) %}
194
+ {{- '<|im_end|>\n' }}
195
+ {%- endif %}
196
+ {%- elif message.role == "tool" %}
197
+ {%- if not loop.previtem or (loop.previtem.role != "tool" and loop.previtem.role != "assistant") %}
198
+ {{- raise_exception('A tool message must follow an assistant or tool message.') }}
199
+ {%- endif %}
200
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
201
+ {{- '<|im_start|>user' }}
202
+ {%- endif %}
203
+ {{- '\n<tool_response>\n' }}
204
+ {{- content }}
205
+ {{- '\n</tool_response>' }}
206
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
207
+ {{- '<|im_end|>\n' }}
208
+ {%- elif loop.last %}
209
+ {{- '<|im_end|>\n' }}
210
+ {%- endif %}
211
+ {%- else %}
212
+ {{- raise_exception('Unexpected message role.') }}
213
+ {%- endif %}
214
+ {%- endfor %}
215
+ {%- if add_generation_prompt %}
216
+ {{- '<|im_start|>assistant\n' }}
217
+ {%- if ns_flags.enable_thinking is false %}
218
+ {{- '<think>\n\n</think>\n\n' }}
219
+ {%- else %}
220
+ {{- '<think>\n' }}
221
+ {%- endif %}
222
+ {%- endif %}