YummyYum commited on
Commit
2fb8c35
·
verified ·
1 Parent(s): 4db12ca

Upload chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +256 -0
chat_template.jinja ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {# ---------- special token variables ---------- #}
2
+ {%- set ns_token = ']<]minimax[>[' -%}
3
+ {%- set bod_token = ']~!b[' -%}
4
+ {%- set bos_token = ']~b]' -%}
5
+ {%- set eos_token = '[e~[' -%}
6
+ {%- set toolcall_begin_token = ns_token ~ '<tool_call>' -%}
7
+ {%- set toolcall_end_token = ns_token ~ '</tool_call>' -%}
8
+ {%- set think_begin_token = '<mm:think>' -%}
9
+ {%- set think_end_token = '</mm:think>' -%}
10
+ {%- set image_token = ']<]image[>[' -%}
11
+ {%- set video_token = ']<]video[>[' -%}
12
+ {#- Thinking mode: "enabled" / "disabled" / "adaptive" / not defined -#}
13
+ {#- Recursive XML renderer for tool_call arguments ======================== -#}
14
+ {#- None values are intentionally skipped in mapping iteration so that
15
+ `<key>null</key>` (which would round-trip to the literal string "null")
16
+ never appears in the rendered tool_call. The convention is: omit the
17
+ field entirely. The top-level `_args` loop applies the same rule.
18
+ The `val is none` branch below is a safety net only — upstream cleaning
19
+ (drop_none_in_tool_arguments) should ensure no None ever reaches here. -#}
20
+ {%- macro to_xml(val, ns) -%}
21
+ {%- if val is mapping -%}
22
+ {%- for k, v in val.items() if v is not none -%}
23
+ {{ ns }}<{{ k }}>{{ to_xml(v, ns) }}{{ ns }}</{{ k }}>
24
+ {%- endfor -%}
25
+ {%- elif val is iterable and val is not string -%}
26
+ {%- for item in val -%}
27
+ {{ ns }}<item>{{ to_xml(item, ns) }}{{ ns }}</item>
28
+ {%- endfor -%}
29
+ {%- elif val is none -%}
30
+ {#- Should be unreachable when upstream cleaning is applied. -#}
31
+ {%- elif val is boolean -%}
32
+ {{ val | tojson }}
33
+ {%- else -%}
34
+ {{ val }}
35
+ {%- endif -%}
36
+ {%- endmacro -%}
37
+ {#- Tool Rendering Functions ============================================== -#}
38
+ {%- macro render_tool_namespace(namespace_name, tool_list) -%}
39
+ {%- for tool in tool_list -%}
40
+ <tool>{{ tool.function | tojson(ensure_ascii=False) }}</tool>
41
+ {% endfor -%}
42
+ {%- endmacro -%}
43
+ {%- macro visible_text(content) -%}
44
+ {%- if content is string -%}
45
+ {{ content }}
46
+ {%- elif content is iterable and content is not mapping -%}
47
+ {%- for item in content -%}
48
+ {%- if item is mapping and item.type == 'text' -%}
49
+ {{- item.text }}
50
+ {%- elif item is mapping and item.type == 'image' -%}
51
+ {{- image_token }}
52
+ {%- elif item is mapping and item.type == 'video' -%}
53
+ {{- video_token}}
54
+ {%- elif item is string -%}
55
+ {{- item }}
56
+ {%- endif -%}
57
+ {%- endfor -%}
58
+ {%- elif content is none -%}
59
+ {{- '' }}
60
+ {%- else -%}
61
+ {{- content }}
62
+ {%- endif -%}
63
+ {%- endmacro -%}
64
+ {#- System Message Construction ============================================ -#}
65
+ {%- macro build_system_message(system_message) -%}
66
+ {%- if system_message and system_message.content -%}
67
+ {{- visible_text(system_message.content) }}
68
+ {#- Handle current_date -#}
69
+ {%- if system_message.current_date -%}
70
+ {{- '\n' ~ 'Current date: ' + system_message.current_date }}
71
+ {%- endif -%}
72
+ {#- Handle current_location -#}
73
+ {%- if system_message.current_location -%}
74
+ {{- '\n' ~ 'Current location: ' + system_message.current_location }}
75
+ {%- endif -%}
76
+ {%- endif -%}
77
+
78
+ {#- Thinking mode instructions -#}
79
+ {{- '\n\n<thinking_instructions>\n' }}
80
+ {{- 'You have a thinking capability that allows you to reason step by step before responding. When thinking is enabled, wrap your reasoning in ' ~ think_begin_token ~ think_end_token ~ ' tags before your response. When thinking is disabled, begin your response directly after the ' ~ think_end_token ~ ' prefix. When thinking is adaptive, decide on your own whether to think for the current turn.\n' }}
81
+ {%- if thinking_mode is defined -%}
82
+ {%- if thinking_mode == "enabled" -%}
83
+ {{- 'Current thinking mode: enabled. You MUST think step by step before every response, including after receiving function/tool results.\n' }}
84
+ {%- elif thinking_mode == "disabled" -%}
85
+ {{- 'Current thinking mode: disabled. Do not output any thinking process.\n' }}
86
+ {%- elif thinking_mode == "adaptive" -%}
87
+ {{- 'Current thinking mode: adaptive. You are encouraged to think for complex decision-making, multi-step reasoning, or when analyzing function/tool results.\n' }}
88
+ {%- endif -%}
89
+ {%- else -%}
90
+ {{- 'Current thinking mode: adaptive. You are encouraged to think for complex decision-making, multi-step reasoning, or when analyzing function/tool results.\n' }}
91
+ {%- endif -%}
92
+ {{- '</thinking_instructions>' }}
93
+ {%- if template_model_id is defined -%}
94
+ {{- '\n\n<model_id>' ~ template_model_id ~ '</model_id>' }}
95
+ {%- endif -%}
96
+ {%- endmacro -%}
97
+ {%- macro build_developer_message(developer_message) -%}
98
+ {%- if developer_message and developer_message.content -%}
99
+ {{- visible_text(developer_message.content) }}
100
+ {%- else -%}
101
+ {%- if model_identity is not defined -%}
102
+ {%- set model_identity = "You are a helpful assistant." -%}
103
+ {%- endif -%}
104
+ {{- model_identity }}
105
+ {%- endif -%}
106
+ {%- endmacro -%}
107
+ {#- Main Template Logic ================================================= -#}
108
+ {#- Role mapping: root -> system sp (high priority), system/developer -> developer sp (low priority) -#}
109
+ {%- set system_message = none -%}
110
+ {%- set developer_message = none -%}
111
+ {%- set conversation_messages = messages -%}
112
+ {%- if messages and messages[0].role == "root" -%}
113
+ {%- set system_message = messages[0] -%}
114
+ {%- set conversation_messages = messages[1:] -%}
115
+ {%- if conversation_messages and conversation_messages[0].role in ["system", "developer"] -%}
116
+ {%- set developer_message = conversation_messages[0] -%}
117
+ {%- set conversation_messages = conversation_messages[1:] -%}
118
+ {%- endif -%}
119
+ {%- elif messages and messages[0].role in ["system", "developer"] -%}
120
+ {%- set developer_message = messages[0] -%}
121
+ {%- set conversation_messages = messages[1:] -%}
122
+ {%- endif -%}
123
+ {#- Render system sp (higher priority, root role only) -#}
124
+ {{- bod_token ~ bos_token ~ 'system' ~ '\n' }}
125
+ {{- build_system_message(system_message) }}
126
+ {{- eos_token ~ '\n' }}
127
+
128
+ {#- Render developer sp (lower priority: system/developer role + tools) -#}
129
+ {{- bos_token ~ 'developer' ~ '\n' }}
130
+ {{- build_developer_message(developer_message) }}
131
+ {%- if tools -%}
132
+ {{- '\n\n' ~ '# Tools' ~ '\n' ~ 'You may call one or more tools to assist with the user query.\nHere are the tools available in JSONSchema format:' ~ '\n' }}
133
+ {{- '\n' ~ '<tools>' ~ '\n' }}
134
+ {{- render_tool_namespace("functions", tools) }}
135
+ {{- '</tools>' ~ '\n\n' }}
136
+ {{- 'To call tools, wrap all invocations in a single ' ~ toolcall_begin_token ~ toolcall_end_token ~ ' block. Parameter values containing nested objects or arrays are recursively expanded into XML elements. Example:\n' }}
137
+ {{- '\n' ~ toolcall_begin_token ~ '\n' }}
138
+ {{- ns_token + '<invoke name="tool-name-1">' }}
139
+ {{- ns_token + '<param-1>value-1' + ns_token + '</param-1>' }}
140
+ {{- ns_token + '<param-2>' }}
141
+ {{- ns_token + '<item>' }}
142
+ {{- ns_token + '<key-a>val-a' + ns_token + '</key-a>' }}
143
+ {{- ns_token + '<key-b>val-b' + ns_token + '</key-b>' }}
144
+ {{- ns_token + '</item>' }}
145
+ {{- ns_token + '</param-2>' }}
146
+ {{- ns_token + '</invoke>\n' }}
147
+ {{- ns_token + '<invoke name="tool-name-2">' }}
148
+ {{- ns_token + '<param-1>value-1' + ns_token + '</param-1>' }}
149
+ {{- ns_token + '</invoke>\n' }}
150
+ {{- toolcall_end_token }}
151
+ {%- endif -%}
152
+ {{- eos_token ~ '\n' }}
153
+
154
+ {#- Render messages -#}
155
+ {%- set last_tool_call = namespace(name=none) -%}
156
+ {%- for message in conversation_messages -%}
157
+ {%- if message.role == 'assistant' -%}
158
+ {{- bos_token ~ 'ai' ~ '\n' }}
159
+
160
+ {%- set reasoning_content = '' %}
161
+ {%- set content = visible_text(message.content) %}
162
+ {%- if message.reasoning_content is string %}
163
+ {%- set reasoning_content = message.reasoning_content %}
164
+ {%- else %}
165
+ {%- if think_end_token in content %}
166
+ {%- set reasoning_content = content.split(think_end_token)[0].strip('\n').split(think_begin_token)[-1].strip('\n') %}
167
+ {%- set content = content.split(think_end_token)[-1].strip('\n') %}
168
+ {%- endif %}
169
+ {%- endif %}
170
+
171
+ {%- if reasoning_content -%}
172
+ {#- Render thinking for every assistant turn (all-turn visible) -#}
173
+ {{- think_begin_token ~ reasoning_content ~ think_end_token }}
174
+ {%- else -%}
175
+ {#- No thinking rendered → prefix with think_end_token -#}
176
+ {{- think_end_token }}
177
+ {%- endif -%}
178
+
179
+ {%- if content -%}
180
+ {{- content }}
181
+ {%- endif -%}
182
+ {%- if message.tool_calls -%}
183
+ {{- toolcall_begin_token ~ '\n' }}
184
+
185
+ {%- for tool_call in message.tool_calls -%}
186
+ {%- if tool_call.function -%}
187
+ {%- set tool_call = tool_call.function -%}
188
+ {%- endif -%}
189
+ {{- ns_token + '<invoke name="' + tool_call.name + '">' }}
190
+ {%- set _args = tool_call.arguments -%}
191
+ {%- for k, v in _args.items() if v is not none %}
192
+ {{- ns_token + '<' + k + '>' -}}
193
+ {{- to_xml(v, ns_token) -}}
194
+ {{- ns_token + '</' + k + '>' }}
195
+ {%- endfor -%}
196
+ {{- ns_token + '</invoke>' ~ '\n' }}
197
+ {%- endfor -%}
198
+
199
+ {{- toolcall_end_token }}
200
+ {%- if message.tool_calls[-1].function -%}
201
+ {%- set last_tool_call.name = message.tool_calls[-1].function.name -%}
202
+ {%- else -%}
203
+ {%- set last_tool_call.name = message.tool_calls[-1].name -%}
204
+ {%- endif -%}
205
+ {%- else -%}
206
+ {%- set last_tool_call.name = none -%}
207
+ {%- endif -%}
208
+ {{- eos_token ~ '\n' }}
209
+
210
+ {%- elif message.role == 'tool' -%}
211
+ {%- if last_tool_call.name is none -%}
212
+ {{- raise_exception("Message has tool role, but there was no previous assistant message with a tool call!") }}
213
+ {%- endif -%}
214
+ {%- if loop.first or (conversation_messages[loop.index0 - 1].role != 'tool') -%}
215
+ {{- bos_token ~ 'tool' }}
216
+ {%- endif -%}
217
+ {{- '\n<response>' }}
218
+ {%- if message.content is string -%}
219
+ {{- message.content }}
220
+ {%- else -%}
221
+ {%- for tr in message.content -%}
222
+ {%- if tr is mapping and tr.type is defined and tr.type == 'image' -%}
223
+ {{- image_token }}
224
+ {%- elif tr is mapping and tr.type is defined and tr.type == 'video' -%}
225
+ {{- video_token }}
226
+ {%- else -%}
227
+ {{- tr.output if tr.output is defined else (tr.text if tr.type == 'text' and tr.text is defined else tr) }}
228
+ {%- endif -%}
229
+ {%- endfor -%}
230
+ {%- endif -%}
231
+ {{- '</response>' }}
232
+ {%- if loop.last or (conversation_messages[loop.index0 + 1].role != 'tool') -%}
233
+ {{- eos_token ~ '\n' -}}
234
+ {%- endif -%}
235
+
236
+ {%- elif message.role == 'user' -%}
237
+ {{- bos_token ~ 'user' ~ '\n' }}
238
+ {{- visible_text(message.content) }}
239
+ {{- eos_token ~ '\n' }}
240
+ {%- endif -%}
241
+ {%- endfor -%}
242
+
243
+ {#- Generation prompt -#}
244
+ {%- if add_generation_prompt -%}
245
+ {{- bos_token ~ 'ai' ~ '\n' }}
246
+ {%- if thinking_mode is defined and thinking_mode == "disabled" -%}
247
+ {{- think_end_token }}
248
+ {%- elif thinking_mode is defined and thinking_mode == "adaptive" -%}
249
+ {#- adaptive: no prefix, let model decide -#}
250
+ {%- elif thinking_mode is defined and thinking_mode == "enabled" -%}
251
+ {#- enabled or not defined: default to think -#}
252
+ {{- think_begin_token }}
253
+ {%- else -%}
254
+ {#- adaptive: no prefix, let model decide -#}
255
+ {%- endif -%}
256
+ {%- endif -%}