llmfan46 commited on
Commit
13d8eef
·
verified ·
1 Parent(s): aa4052e

Upload chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +361 -0
chat_template.jinja ADDED
@@ -0,0 +1,361 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- macro format_type_argument(type_value) -%}
2
+ {%- if type_value is string -%}
3
+ {{- '<|"|>' + (type_value | upper) + '<|"|>' -}}
4
+ {%- elif type_value is iterable and type_value is not string -%}
5
+ [
6
+ {%- for item in type_value -%}
7
+ <|"|>{{- item | upper -}}<|"|>
8
+ {%- if not loop.last %},{% endif -%}
9
+ {%- endfor -%}
10
+ ]
11
+ {%- else -%}
12
+ {{- format_argument(type_value) -}}
13
+ {%- endif -%}
14
+ {%- endmacro -%}
15
+ {%- macro format_parameters(properties, required, filter_keys=false) -%}
16
+ {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
17
+ {%- set ns = namespace(found_first=false) -%}
18
+ {%- for key, value in properties | dictsort -%}
19
+ {%- set add_comma = false -%}
20
+ {%- if not filter_keys or key not in standard_keys -%}
21
+ {%- if ns.found_first %},{% endif -%}
22
+ {%- set ns.found_first = true -%}
23
+ {{ key }}:{
24
+ {%- if value['description'] -%}
25
+ description:<|"|>{{ value['description'] }}<|"|>
26
+ {%- set add_comma = true -%}
27
+ {%- endif -%}
28
+ {%- if value['type'] and ('string' in value['type'] or 'STRING' in value['type']) -%}
29
+ {%- if value['enum'] -%}
30
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
31
+ enum:{{ format_argument(value['enum']) }}
32
+ {%- endif -%}
33
+ {%- elif value['type'] and ('array' in value['type'] or 'ARRAY' in value['type']) -%}
34
+ {%- if value['items'] is mapping and value['items'] -%}
35
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
36
+ items:{
37
+ {%- set ns_items = namespace(found_first=false) -%}
38
+ {%- for item_key, item_value in value['items'] | dictsort -%}
39
+ {%- if item_value is not none -%}
40
+ {%- if ns_items.found_first %},{% endif -%}
41
+ {%- set ns_items.found_first = true -%}
42
+ {%- if item_key == 'properties' -%}
43
+ properties:{
44
+ {%- if item_value is mapping -%}
45
+ {{- format_parameters(item_value, value['items']['required'] | default([])) -}}
46
+ {%- endif -%}
47
+ }
48
+ {%- elif item_key == 'required' -%}
49
+ required:[
50
+ {%- for req_item in item_value -%}
51
+ <|"|>{{- req_item -}}<|"|>
52
+ {%- if not loop.last %},{% endif -%}
53
+ {%- endfor -%}
54
+ ]
55
+ {%- elif item_key == 'type' -%}
56
+ type:{{ format_type_argument(item_value) }}
57
+ {%- else -%}
58
+ {{ item_key }}:{{ format_argument(item_value) }}
59
+ {%- endif -%}
60
+ {%- endif -%}
61
+ {%- endfor -%}
62
+ }
63
+ {%- endif -%}
64
+ {%- endif -%}
65
+ {%- if value['nullable'] %}
66
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
67
+ nullable:true
68
+ {%- endif -%}
69
+ {%- if value['type'] and ('object' in value['type'] or 'OBJECT' in value['type']) -%}
70
+ {%- if value['properties'] is defined and value['properties'] is mapping -%}
71
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
72
+ properties:{
73
+ {{- format_parameters(value['properties'], value['required'] | default([])) -}}
74
+ }
75
+ {%- elif value is mapping -%}
76
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
77
+ properties:{
78
+ {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
79
+ }
80
+ {%- endif -%}
81
+ {%- if value['required'] -%}
82
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
83
+ required:[
84
+ {%- for item in value['required'] | default([]) -%}
85
+ <|"|>{{- item -}}<|"|>
86
+ {%- if not loop.last %},{% endif -%}
87
+ {%- endfor -%}
88
+ ]
89
+ {%- endif -%}
90
+ {%- endif -%}
91
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
92
+ type:{{ format_type_argument(value['type']) }}}
93
+ {%- endif -%}
94
+ {%- endfor -%}
95
+ {%- endmacro -%}
96
+ {%- macro format_function_declaration(tool_data) -%}
97
+ declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
98
+ {%- set params = tool_data['function']['parameters'] -%}
99
+ {%- if params -%}
100
+ ,parameters:{
101
+ {%- if params['properties'] -%}
102
+ properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
103
+ {%- endif -%}
104
+ {%- if params['required'] -%}
105
+ required:[
106
+ {%- for item in params['required'] -%}
107
+ <|"|>{{- item -}}<|"|>
108
+ {{- ',' if not loop.last -}}
109
+ {%- endfor -%}
110
+ ],
111
+ {%- endif -%}
112
+ {%- if params['type'] -%}
113
+ type:{{- format_type_argument(params['type']) -}}}
114
+ {%- endif -%}
115
+ {%- endif -%}
116
+ {%- if 'response' in tool_data['function'] -%}
117
+ {%- set response_declaration = tool_data['function']['response'] -%}
118
+ ,response:{
119
+ {%- if response_declaration['description'] -%}
120
+ description:<|"|>{{- response_declaration['description'] -}}<|"|>,
121
+ {%- endif -%}
122
+ {%- if response_declaration['type'] and ('object' in response_declaration['type'] or 'OBJECT' in response_declaration['type']) -%}
123
+ type:{{- format_type_argument(response_declaration['type']) -}}}
124
+ {%- endif -%}
125
+ {%- endif -%}
126
+ }
127
+ {%- endmacro -%}
128
+ {%- macro format_argument(argument, escape_keys=True) -%}
129
+ {%- if argument is string -%}
130
+ {{- '<|"|>' + argument + '<|"|>' -}}
131
+ {%- elif argument is boolean -%}
132
+ {{- 'true' if argument else 'false' -}}
133
+ {%- elif argument is mapping -%}
134
+ {{- '{' -}}
135
+ {%- set ns = namespace(found_first=false) -%}
136
+ {%- for key, value in argument | dictsort -%}
137
+ {%- if ns.found_first %},{% endif -%}
138
+ {%- set ns.found_first = true -%}
139
+ {%- if escape_keys -%}
140
+ {{- '<|"|>' + key + '<|"|>' -}}
141
+ {%- else -%}
142
+ {{- key -}}
143
+ {%- endif -%}
144
+ :{{- format_argument(value, escape_keys=escape_keys) -}}
145
+ {%- endfor -%}
146
+ {{- '}' -}}
147
+ {%- elif argument is sequence -%}
148
+ {{- '[' -}}
149
+ {%- for item in argument -%}
150
+ {{- format_argument(item, escape_keys=escape_keys) -}}
151
+ {%- if not loop.last %},{% endif -%}
152
+ {%- endfor -%}
153
+ {{- ']' -}}
154
+ {%- else -%}
155
+ {{- argument -}}
156
+ {%- endif -%}
157
+ {%- endmacro -%}
158
+ {%- macro strip_thinking(text) -%}
159
+ {%- set ns = namespace(result='') -%}
160
+ {%- for part in text.split('<channel|>') -%}
161
+ {%- if '<|channel>' in part -%}
162
+ {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}
163
+ {%- else -%}
164
+ {%- set ns.result = ns.result + part -%}
165
+ {%- endif -%}
166
+ {%- endfor -%}
167
+ {{- ns.result | trim -}}
168
+ {%- endmacro -%}
169
+
170
+ {%- macro format_tool_response_block(tool_name, response) -%}
171
+ {{- '<|tool_response>' -}}
172
+ {%- if response is mapping -%}
173
+ {{- 'response:' + tool_name + '{' -}}
174
+ {%- for key, value in response | dictsort -%}
175
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
176
+ {%- if not loop.last %},{% endif -%}
177
+ {%- endfor -%}
178
+ {{- '}' -}}
179
+ {%- else -%}
180
+ {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
181
+ {%- endif -%}
182
+ {{- '<tool_response|>' -}}
183
+ {%- endmacro -%}
184
+
185
+ {%- set ns = namespace(prev_message_type=None) -%}
186
+ {%- set loop_messages = messages -%}
187
+ {{- bos_token -}}
188
+ {#- Handle System/Tool Definitions Block -#}
189
+ {%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
190
+ {{- '<|turn>system\n' -}}
191
+ {#- Inject Thinking token at the very top of the FIRST system turn -#}
192
+ {%- if enable_thinking is defined and enable_thinking -%}
193
+ {{- '<|think|>\n' -}}
194
+ {%- set ns.prev_message_type = 'think' -%}
195
+ {%- endif -%}
196
+ {%- if messages[0]['role'] in ['system', 'developer'] -%}
197
+ {%- if messages[0]['content'] is string -%}
198
+ {{- messages[0]['content'] | trim -}}
199
+ {%- elif messages[0]['content'] is sequence -%}
200
+ {%- for item in messages[0]['content'] -%}
201
+ {{- item['text'] | trim + ' '-}}
202
+ {%- endfor -%}
203
+ {%- endif -%}
204
+ {%- set loop_messages = messages[1:] -%}
205
+ {%- endif -%}
206
+ {%- if tools -%}
207
+ {%- for tool in tools %}
208
+ {{- '<|tool>' -}}
209
+ {{- format_function_declaration(tool) | trim -}}
210
+ {{- '<tool|>' -}}
211
+ {%- endfor %}
212
+ {%- set ns.prev_message_type = 'tool' -%}
213
+ {%- endif -%}
214
+ {{- '<turn|>\n' -}}
215
+ {%- endif %}
216
+
217
+ {#- Pre-scan: find last user message index for reasoning guard -#}
218
+ {%- set ns_turn = namespace(last_user_idx=-1) -%}
219
+ {%- for i in range(loop_messages | length) -%}
220
+ {%- if loop_messages[i]['role'] == 'user' -%}
221
+ {%- set ns_turn.last_user_idx = i -%}
222
+ {%- endif -%}
223
+ {%- endfor -%}
224
+
225
+ {#- Loop through messages -#}
226
+ {%- for message in loop_messages -%}
227
+ {%- if message['role'] != 'tool' -%}
228
+ {%- set ns.prev_message_type = None -%}
229
+ {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
230
+ {#- Detect continuation: suppress duplicate <|turn>model when previous non-tool message was also assistant -#}
231
+ {%- set prev_nt = namespace(role=None, found=false) -%}
232
+ {%- if loop.index0 > 0 -%}
233
+ {%- for j in range(loop.index0 - 1, -1, -1) -%}
234
+ {%- if not prev_nt.found -%}
235
+ {%- if loop_messages[j]['role'] != 'tool' -%}
236
+ {%- set prev_nt.role = loop_messages[j]['role'] -%}
237
+ {%- set prev_nt.found = true -%}
238
+ {%- endif -%}
239
+ {%- endif -%}
240
+ {%- endfor -%}
241
+ {%- endif -%}
242
+ {%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant') -%}
243
+ {%- if not continue_same_model_turn -%}
244
+ {{- '<|turn>' + role + '\n' }}
245
+ {%- endif -%}
246
+
247
+ {#- Render reasoning/reasoning_content as thinking channel -#}
248
+ {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
249
+ {%- if thinking_text and loop.index0 > ns_turn.last_user_idx and message.get('tool_calls') -%}
250
+ {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
251
+ {%- endif -%}
252
+
253
+ {%- if message['tool_calls'] -%}
254
+ {%- for tool_call in message['tool_calls'] -%}
255
+ {%- set function = tool_call['function'] -%}
256
+ {{- '<|tool_call>call:' + function['name'] + '{' -}}
257
+ {%- if function['arguments'] is mapping -%}
258
+ {%- set ns_args = namespace(found_first=false) -%}
259
+ {%- for key, value in function['arguments'] | dictsort -%}
260
+ {%- if ns_args.found_first %},{% endif -%}
261
+ {%- set ns_args.found_first = true -%}
262
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
263
+ {%- endfor -%}
264
+ {%- elif function['arguments'] is string -%}
265
+ {{- function['arguments'] -}}
266
+ {%- endif -%}
267
+ {{- '}<tool_call|>' -}}
268
+ {%- endfor -%}
269
+ {%- set ns.prev_message_type = 'tool_call' -%}
270
+ {%- endif -%}
271
+
272
+ {%- set ns_tr_out = namespace(flag=false) -%}
273
+ {%- if message.get('tool_responses') -%}
274
+ {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
275
+ {%- for tool_response in message['tool_responses'] -%}
276
+ {{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}}
277
+ {%- set ns_tr_out.flag = true -%}
278
+ {%- set ns.prev_message_type = 'tool_response' -%}
279
+ {%- endfor -%}
280
+ {%- elif message.get('tool_calls') -%}
281
+ {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
282
+ {%- set ns_tool_scan = namespace(stopped=false) -%}
283
+ {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
284
+ {%- if ns_tool_scan.stopped -%}
285
+ {%- elif loop_messages[k]['role'] != 'tool' -%}
286
+ {%- set ns_tool_scan.stopped = true -%}
287
+ {%- else -%}
288
+ {%- set follow = loop_messages[k] -%}
289
+ {#- Resolve tool_call_id to function name -#}
290
+ {%- set ns_tname = namespace(name=follow.get('name') | default('unknown')) -%}
291
+ {%- for tc in message['tool_calls'] -%}
292
+ {%- if tc.get('id') == follow.get('tool_call_id') -%}
293
+ {%- set ns_tname.name = tc['function']['name'] -%}
294
+ {%- endif -%}
295
+ {%- endfor -%}
296
+ {#- Handle content as string or content-parts array -#}
297
+ {%- set tool_body = follow.get('content') -%}
298
+ {%- if tool_body is string -%}
299
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
300
+ {%- elif tool_body is sequence and tool_body is not string -%}
301
+ {%- set ns_txt = namespace(s='') -%}
302
+ {%- for part in tool_body -%}
303
+ {%- if part.get('type') == 'text' -%}
304
+ {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
305
+ {%- endif -%}
306
+ {%- endfor -%}
307
+ {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
308
+ {%- else -%}
309
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
310
+ {%- endif -%}
311
+ {%- set ns_tr_out.flag = true -%}
312
+ {%- set ns.prev_message_type = 'tool_response' -%}
313
+ {%- endif -%}
314
+ {%- endfor -%}
315
+ {%- endif -%}
316
+
317
+ {%- set captured_content -%}
318
+ {%- if message['content'] is string -%}
319
+ {%- if role == 'model' -%}
320
+ {{- strip_thinking(message['content']) -}}
321
+ {%- else -%}
322
+ {{- message['content'] | trim -}}
323
+ {%- endif -%}
324
+ {%- elif message['content'] is sequence -%}
325
+ {%- for item in message['content'] -%}
326
+ {%- if item['type'] == 'text' -%}
327
+ {%- if role == 'model' -%}
328
+ {{- strip_thinking(item['text']) -}}
329
+ {%- else -%}
330
+ {{- item['text'] | trim -}}
331
+ {%- endif -%}
332
+ {%- elif item['type'] == 'image' -%}
333
+ {{- '<|image|>' -}}
334
+ {%- set ns.prev_message_type = 'image' -%}
335
+ {%- elif item['type'] == 'audio' -%}
336
+ {{- '<|audio|>' -}}
337
+ {%- set ns.prev_message_type = 'audio' -%}
338
+ {%- elif item['type'] == 'video' -%}
339
+ {{- '<|video|>' -}}
340
+ {%- set ns.prev_message_type = 'video' -%}
341
+ {%- endif -%}
342
+ {%- endfor -%}
343
+ {%- endif -%}
344
+ {%- endset -%}
345
+
346
+ {{- captured_content -}}
347
+ {%- set has_content = captured_content | trim | length > 0 -%}
348
+
349
+ {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
350
+ {{- '<|tool_response>' -}}
351
+ {%- elif not (ns_tr_out.flag and not has_content) -%}
352
+ {{- '<turn|>\n' -}}
353
+ {%- endif -%}
354
+ {%- endif -%}
355
+ {%- endfor -%}
356
+
357
+ {%- if add_generation_prompt -%}
358
+ {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
359
+ {{- '<|turn>model\n' -}}
360
+ {%- endif -%}
361
+ {%- endif -%}