llmfan46 commited on
Commit
8b5c786
·
verified ·
1 Parent(s): 7db8897

Delete chat_template_old.jinja

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