llmfan46 commited on
Commit
145586c
·
verified ·
1 Parent(s): d9238c4

Upload 3 files

Browse files
Files changed (3) hide show
  1. chat_template.jinja +365 -0
  2. tokenizer.json +3 -0
  3. tokenizer_config.json +293 -0
chat_template.jinja ADDED
@@ -0,0 +1,365 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 -%}
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a2619fe11b50dbed06ac443c51d757b354d0b62d64baa514404d4e84e6713519
3
+ size 32169780
tokenizer_config.json ADDED
@@ -0,0 +1,293 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "audio_token": "<|audio|>",
3
+ "backend": "tokenizers",
4
+ "boa_token": "<|audio>",
5
+ "boi_token": "<|image>",
6
+ "bos_token": "<bos>",
7
+ "eoa_token": "<audio|>",
8
+ "eoc_token": "<channel|>",
9
+ "eoi_token": "<image|>",
10
+ "eos_token": "<eos>",
11
+ "eot_token": "<turn|>",
12
+ "escape_token": "<|\"|>",
13
+ "etc_token": "<tool_call|>",
14
+ "etd_token": "<tool|>",
15
+ "etr_token": "<tool_response|>",
16
+ "extra_special_tokens": [
17
+ "<|video|>"
18
+ ],
19
+ "image_token": "<|image|>",
20
+ "is_local": false,
21
+ "mask_token": "<mask>",
22
+ "max_length": null,
23
+ "model_max_length": 1000000000000000019884624838656,
24
+ "model_specific_special_tokens": {
25
+ "audio_token": "<|audio|>",
26
+ "boa_token": "<|audio>",
27
+ "boi_token": "<|image>",
28
+ "eoa_token": "<audio|>",
29
+ "eoc_token": "<channel|>",
30
+ "eoi_token": "<image|>",
31
+ "eot_token": "<turn|>",
32
+ "escape_token": "<|\"|>",
33
+ "etc_token": "<tool_call|>",
34
+ "etd_token": "<tool|>",
35
+ "etr_token": "<tool_response|>",
36
+ "image_token": "<|image|>",
37
+ "soc_token": "<|channel>",
38
+ "sot_token": "<|turn>",
39
+ "stc_token": "<|tool_call>",
40
+ "std_token": "<|tool>",
41
+ "str_token": "<|tool_response>",
42
+ "think_token": "<|think|>"
43
+ },
44
+ "pad_to_multiple_of": null,
45
+ "pad_token": "<pad>",
46
+ "pad_token_type_id": 0,
47
+ "padding_side": "left",
48
+ "processor_class": "Gemma4Processor",
49
+ "response_schema": {
50
+ "properties": {
51
+ "content": {
52
+ "type": "string"
53
+ },
54
+ "role": {
55
+ "const": "assistant"
56
+ },
57
+ "thinking": {
58
+ "type": "string"
59
+ },
60
+ "tool_calls": {
61
+ "items": {
62
+ "properties": {
63
+ "function": {
64
+ "properties": {
65
+ "arguments": {
66
+ "additionalProperties": {},
67
+ "type": "object",
68
+ "x-parser": "gemma4-tool-call"
69
+ },
70
+ "name": {
71
+ "type": "string"
72
+ }
73
+ },
74
+ "type": "object",
75
+ "x-regex": "call\\:(?P<name>\\w+)(?P<arguments>\\{.*\\})"
76
+ },
77
+ "type": {
78
+ "const": "function"
79
+ }
80
+ },
81
+ "type": "object"
82
+ },
83
+ "type": "array",
84
+ "x-regex-iterator": "<\\|tool_call>(.*?)<tool_call\\|>"
85
+ }
86
+ },
87
+ "type": "object",
88
+ "x-regex": "(\\<\\|channel\\>thought\\n(?P<thinking>.*?)\\<channel\\|\\>)?(?P<tool_calls>\\<\\|tool_call\\>.*\\<tool_call\\|\\>)?(?P<content>(?:(?!\\<turn\\|\\>)(?!\\<\\|tool_response\\>).)+)?(?:\\<turn\\|\\>|\\<\\|tool_response\\>)?"
89
+ },
90
+ "soc_token": "<|channel>",
91
+ "sot_token": "<|turn>",
92
+ "stc_token": "<|tool_call>",
93
+ "std_token": "<|tool>",
94
+ "str_token": "<|tool_response>",
95
+ "think_token": "<|think|>",
96
+ "tokenizer_class": "GemmaTokenizer",
97
+ "unk_token": "<unk>",
98
+ "added_tokens_decoder": {
99
+ "0": {
100
+ "content": "<pad>",
101
+ "single_word": false,
102
+ "lstrip": false,
103
+ "rstrip": false,
104
+ "normalized": false,
105
+ "special": true
106
+ },
107
+ "1": {
108
+ "content": "<eos>",
109
+ "single_word": false,
110
+ "lstrip": false,
111
+ "rstrip": false,
112
+ "normalized": false,
113
+ "special": true
114
+ },
115
+ "2": {
116
+ "content": "<bos>",
117
+ "single_word": false,
118
+ "lstrip": false,
119
+ "rstrip": false,
120
+ "normalized": false,
121
+ "special": true
122
+ },
123
+ "3": {
124
+ "content": "<unk>",
125
+ "single_word": false,
126
+ "lstrip": false,
127
+ "rstrip": false,
128
+ "normalized": false,
129
+ "special": true
130
+ },
131
+ "4": {
132
+ "content": "<mask>",
133
+ "single_word": false,
134
+ "lstrip": false,
135
+ "rstrip": false,
136
+ "normalized": false,
137
+ "special": true
138
+ },
139
+ "46": {
140
+ "content": "<|tool>",
141
+ "single_word": false,
142
+ "lstrip": false,
143
+ "rstrip": false,
144
+ "normalized": false,
145
+ "special": true
146
+ },
147
+ "47": {
148
+ "content": "<tool|>",
149
+ "single_word": false,
150
+ "lstrip": false,
151
+ "rstrip": false,
152
+ "normalized": false,
153
+ "special": true
154
+ },
155
+ "48": {
156
+ "content": "<|tool_call>",
157
+ "single_word": false,
158
+ "lstrip": false,
159
+ "rstrip": false,
160
+ "normalized": false,
161
+ "special": true
162
+ },
163
+ "49": {
164
+ "content": "<tool_call|>",
165
+ "single_word": false,
166
+ "lstrip": false,
167
+ "rstrip": false,
168
+ "normalized": false,
169
+ "special": true
170
+ },
171
+ "50": {
172
+ "content": "<|tool_response>",
173
+ "single_word": false,
174
+ "lstrip": false,
175
+ "rstrip": false,
176
+ "normalized": false,
177
+ "special": true
178
+ },
179
+ "51": {
180
+ "content": "<tool_response|>",
181
+ "single_word": false,
182
+ "lstrip": false,
183
+ "rstrip": false,
184
+ "normalized": false,
185
+ "special": true
186
+ },
187
+ "52": {
188
+ "content": "<|\"|>",
189
+ "single_word": false,
190
+ "lstrip": false,
191
+ "rstrip": false,
192
+ "normalized": false,
193
+ "special": true
194
+ },
195
+ "98": {
196
+ "content": "<|think|>",
197
+ "single_word": false,
198
+ "lstrip": false,
199
+ "rstrip": false,
200
+ "normalized": false,
201
+ "special": true
202
+ },
203
+ "100": {
204
+ "content": "<|channel>",
205
+ "single_word": false,
206
+ "lstrip": false,
207
+ "rstrip": false,
208
+ "normalized": false,
209
+ "special": true
210
+ },
211
+ "101": {
212
+ "content": "<channel|>",
213
+ "single_word": false,
214
+ "lstrip": false,
215
+ "rstrip": false,
216
+ "normalized": false,
217
+ "special": true
218
+ },
219
+ "105": {
220
+ "content": "<|turn>",
221
+ "single_word": false,
222
+ "lstrip": false,
223
+ "rstrip": false,
224
+ "normalized": false,
225
+ "special": true
226
+ },
227
+ "106": {
228
+ "content": "<turn|>",
229
+ "single_word": false,
230
+ "lstrip": false,
231
+ "rstrip": false,
232
+ "normalized": false,
233
+ "special": true
234
+ },
235
+ "255999": {
236
+ "content": "<|image>",
237
+ "single_word": false,
238
+ "lstrip": false,
239
+ "rstrip": false,
240
+ "normalized": false,
241
+ "special": true
242
+ },
243
+ "256000": {
244
+ "content": "<|audio>",
245
+ "single_word": false,
246
+ "lstrip": false,
247
+ "rstrip": false,
248
+ "normalized": false,
249
+ "special": true
250
+ },
251
+ "258880": {
252
+ "content": "<|image|>",
253
+ "single_word": false,
254
+ "lstrip": false,
255
+ "rstrip": false,
256
+ "normalized": false,
257
+ "special": true
258
+ },
259
+ "258881": {
260
+ "content": "<|audio|>",
261
+ "single_word": false,
262
+ "lstrip": false,
263
+ "rstrip": false,
264
+ "normalized": false,
265
+ "special": true
266
+ },
267
+ "258882": {
268
+ "content": "<image|>",
269
+ "single_word": false,
270
+ "lstrip": false,
271
+ "rstrip": false,
272
+ "normalized": false,
273
+ "special": true
274
+ },
275
+ "258883": {
276
+ "content": "<audio|>",
277
+ "single_word": false,
278
+ "lstrip": false,
279
+ "rstrip": false,
280
+ "normalized": false,
281
+ "special": true
282
+ },
283
+ "258884": {
284
+ "content": "<|video|>",
285
+ "single_word": false,
286
+ "lstrip": false,
287
+ "rstrip": false,
288
+ "normalized": false,
289
+ "special": true
290
+ }
291
+ },
292
+ "chat_template": "{%- macro format_type_argument(type_value) -%}\n {%- if type_value is string -%}\n <|\"|>{{ type_value | upper }}<|\"|>\n {%- elif type_value is iterable and type_value is not string -%}\n [\n {%- for item in type_value -%}\n <|\"|>{{ item | upper }}<|\"|>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- else -%}\n {{- format_argument(type_value) -}}\n {%- endif -%}\n{%- endmacro -%}\n\n{%- macro format_parameters(properties, required, filter_keys=false) -%}\n {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in properties | dictsort -%}\n {%- set add_comma = false -%}\n {%- if not filter_keys or key not in standard_keys -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {{ key }}:{\n {%- if value['description'] -%}\n description:<|\"|>{{ value['description'] }}<|\"|>\n {%- set add_comma = true -%}\n {%- endif -%}\n {%- if value['type'] and ('string' in value['type'] or 'STRING' in value['type']) -%}\n {%- if value['enum'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n enum:{{ format_argument(value['enum']) }}\n {%- endif -%}\n {%- elif value['type'] and ('array' in value['type'] or 'ARRAY' in value['type']) -%}\n {%- if value['items'] is mapping and value['items'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n items:{\n {%- set ns_items = namespace(found_first=false) -%}\n {%- for item_key, item_value in value['items'] | dictsort -%}\n {%- if item_value is not none -%}\n {%- if ns_items.found_first %},{% endif -%}\n {%- set ns_items.found_first = true -%}\n {%- if item_key == 'properties' -%}\n properties:{\n {%- if item_value is mapping -%}\n {{- format_parameters(item_value, value['items']['required'] | default([])) -}}\n {%- endif -%}\n }\n {%- elif item_key == 'required' -%}\n required:[\n {%- for req_item in item_value -%}\n <|\"|>{{- req_item -}}<|\"|>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- elif item_key == 'type' -%}\n type:{{ format_type_argument(item_value) }}\n {%- else -%}\n {{ item_key }}:{{ format_argument(item_value) }}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n }\n {%- endif -%}\n {%- endif -%}\n {%- if value['nullable'] %}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n nullable:true\n {%- endif -%}\n {%- if value['type'] and ('object' in value['type'] or 'OBJECT' in value['type']) -%}\n {%- if value['properties'] is defined and value['properties'] is mapping -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n properties:{\n {{- format_parameters(value['properties'], value['required'] | default([])) -}}\n }\n {%- elif value is mapping -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n properties:{\n {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}\n }\n {%- endif -%}\n {%- if value['required'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n required:[\n {%- for item in value['required'] | default([]) -%}\n <|\"|>{{- item -}}<|\"|>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- endif -%}\n {%- endif -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n type:{{ format_type_argument(value['type']) }}}\n {%- endif -%}\n {%- endfor -%}\n{%- endmacro -%}\n{%- macro format_function_declaration(tool_data) -%}\n declaration:{{- tool_data['function']['name'] -}}{description:<|\"|>{{- tool_data['function']['description'] -}}<|\"|>\n {%- set params = tool_data['function']['parameters'] -%}\n {%- if params -%}\n ,parameters:{\n {%- if params['properties'] -%}\n properties:{ {{- format_parameters(params['properties'], params['required']) -}} },\n {%- endif -%}\n {%- if params['required'] -%}\n required:[\n {%- for item in params['required'] -%}\n <|\"|>{{- item -}}<|\"|>\n {{- ',' if not loop.last -}}\n {%- endfor -%}\n ],\n {%- endif -%}\n {%- if params['type'] -%}\n type:{{- format_type_argument(params['type']) -}}}\n {%- endif -%}\n {%- endif -%}\n {%- if 'response' in tool_data['function'] -%}\n {%- set response_declaration = tool_data['function']['response'] -%}\n ,response:{\n {%- if response_declaration['description'] -%}\n description:<|\"|>{{- response_declaration['description'] -}}<|\"|>,\n {%- endif -%}\n {%- if response_declaration['type'] and ('object' in response_declaration['type'] or 'OBJECT' in response_declaration['type']) -%}\n type:{{- format_type_argument(response_declaration['type']) -}}}\n {%- endif -%}\n {%- endif -%}\n }\n{%- endmacro -%}\n{%- macro format_argument(argument, escape_keys=True) -%}\n {%- if argument is string -%}\n {{- '<|\"|>' + argument + '<|\"|>' -}}\n {%- elif argument is boolean -%}\n {{- 'true' if argument else 'false' -}}\n {%- elif argument is mapping -%}\n {{- '{' -}}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in argument | dictsort -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {%- if escape_keys -%}\n {{- '<|\"|>' + key + '<|\"|>' -}}\n {%- else -%}\n {{- key -}}\n {%- endif -%}\n :{{- format_argument(value, escape_keys=escape_keys) -}}\n {%- endfor -%}\n {{- '}' -}}\n {%- elif argument is iterable -%}\n {{- '[' -}}\n {%- for item in argument -%}\n {{- format_argument(item, escape_keys=escape_keys) -}}\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n {{- ']' -}}\n {%- else -%}\n {{- argument -}}\n {%- endif -%}\n{%- endmacro -%}\n{%- macro strip_thinking(text) -%}\n {%- set ns = namespace(result='') -%}\n {%- for part in text.split('<channel|>') -%}\n {%- if '<|channel>' in part -%}\n {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}\n {%- else -%}\n {%- set ns.result = ns.result + part -%}\n {%- endif -%}\n {%- endfor -%}\n {{- ns.result | trim -}}\n{%- endmacro -%}\n\n{%- macro format_tool_response_block(tool_name, response) -%}\n {{- '<|tool_response>' -}}\n {%- if response is mapping -%}\n {{- 'response:' + tool_name + '{' -}}\n {%- for key, value in response | dictsort -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n {{- '}' -}}\n {%- else -%}\n {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}\n {%- endif -%}\n {{- '<tool_response|>' -}}\n{%- endmacro -%}\n\n{%- set ns = namespace(prev_message_type=None) -%}\n{%- set loop_messages = messages -%}\n{{- bos_token -}}\n{#- Handle System/Tool Definitions Block -#}\n{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}\n {{- '<|turn>system\\n' -}}\n {#- Inject Thinking token at the very top of the FIRST system turn -#}\n {%- if enable_thinking is defined and enable_thinking -%}\n {{- '<|think|>\\n' -}}\n {%- set ns.prev_message_type = 'think' -%}\n {%- endif -%}\n {%- if messages[0]['role'] in ['system', 'developer'] -%}\n {%- if messages[0]['content'] is string -%}\n {{- messages[0]['content'] | trim -}}\n {%- elif messages[0]['content'] is iterable -%}\n {%- for item in messages[0]['content'] -%}\n {{- item['text'] | trim + ' '-}}\n {%- endfor -%}\n {%- endif -%}\n {%- set loop_messages = messages[1:] -%}\n {%- endif -%}\n {%- if tools -%}\n {%- for tool in tools %}\n {{- '<|tool>' -}}\n {{- format_function_declaration(tool) | trim -}}\n {{- '<tool|>' -}}\n {%- endfor %}\n {%- set ns.prev_message_type = 'tool' -%}\n {%- endif -%}\n {{- '<turn|>\\n' -}}\n{%- endif %}\n\n{#- Pre-scan: find last user message index for reasoning guard -#}\n{%- set ns_turn = namespace(last_user_idx=-1) -%}\n{%- for i in range(loop_messages | length) -%}\n {%- if loop_messages[i]['role'] == 'user' -%}\n {%- set ns_turn.last_user_idx = i -%}\n {%- endif -%}\n{%- endfor -%}\n\n{#- Loop through messages -#}\n{%- for message in loop_messages -%}\n {%- if message['role'] != 'tool' -%}\n {%- set ns.prev_message_type = None -%}\n {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}\n {#- Detect continuation: suppress duplicate <|turn>model when previous non-tool message was also assistant -#}\n {%- set prev_nt = namespace(role=None, found=false) -%}\n {%- if loop.index0 > 0 -%}\n {%- for j in range(loop.index0 - 1, -1, -1) -%}\n {%- if not prev_nt.found -%}\n {%- if loop_messages[j]['role'] != 'tool' -%}\n {%- set prev_nt.role = loop_messages[j]['role'] -%}\n {%- set prev_nt.found = true -%}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant') -%}\n {%- if not continue_same_model_turn -%}\n {{- '<|turn>' + role + '\\n' }}\n {%- endif -%}\n\n {#- Render reasoning/reasoning_content as thinking channel -#}\n {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}\n {%- if thinking_text and loop.index0 > ns_turn.last_user_idx and message.get('tool_calls') -%}\n {{- '<|channel>thought\\n' + thinking_text + '\\n<channel|>' -}}\n {%- endif -%}\n\n {%- if message['tool_calls'] -%}\n {%- for tool_call in message['tool_calls'] -%}\n {%- set function = tool_call['function'] -%}\n {{- '<|tool_call>call:' + function['name'] + '{' -}}\n {%- if function['arguments'] is mapping -%}\n {%- set ns_args = namespace(found_first=false) -%}\n {%- for key, value in function['arguments'] | dictsort -%}\n {%- if ns_args.found_first %},{% endif -%}\n {%- set ns_args.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {%- elif function['arguments'] is string -%}\n {{- function['arguments'] -}}\n {%- endif -%}\n {{- '}<tool_call|>' -}}\n {%- endfor -%}\n {%- set ns.prev_message_type = 'tool_call' -%}\n {%- endif -%}\n\n {%- set ns_tr_out = namespace(flag=false) -%}\n {%- if message.get('tool_responses') -%}\n {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}\n {%- for tool_response in message['tool_responses'] -%}\n {{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}}\n {%- set ns_tr_out.flag = true -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endfor -%}\n {%- elif message.get('tool_calls') -%}\n {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}\n {%- set ns_tool_scan = namespace(stopped=false) -%}\n {%- for k in range(loop.index0 + 1, loop_messages | length) -%}\n {%- if ns_tool_scan.stopped -%}\n {%- elif loop_messages[k]['role'] != 'tool' -%}\n {%- set ns_tool_scan.stopped = true -%}\n {%- else -%}\n {%- set follow = loop_messages[k] -%}\n {#- Resolve tool_call_id to function name -#}\n {%- set ns_tname = namespace(name=follow.get('name') | default('unknown')) -%}\n {%- for tc in message['tool_calls'] -%}\n {%- if tc.get('id') == follow.get('tool_call_id') -%}\n {%- set ns_tname.name = tc['function']['name'] -%}\n {%- endif -%}\n {%- endfor -%}\n {#- Handle content as string or content-parts array -#}\n {%- set tool_body = follow.get('content') -%}\n {%- if tool_body is string -%}\n {{- format_tool_response_block(ns_tname.name, tool_body) -}}\n {%- elif tool_body is iterable and tool_body is not string -%}\n {%- set ns_txt = namespace(s='') -%}\n {%- for part in tool_body -%}\n {%- if part.get('type') == 'text' -%}\n {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}\n {%- endif -%}\n {%- endfor -%}\n {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}\n {%- else -%}\n {{- format_tool_response_block(ns_tname.name, tool_body) -}}\n {%- endif -%}\n {%- set ns_tr_out.flag = true -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n\n {%- set captured_content -%}\n {%- if message['content'] is string -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(message['content']) -}}\n {%- else -%}\n {{- message['content'] | trim -}}\n {%- endif -%}\n {%- elif message['content'] is iterable -%}\n {%- for item in message['content'] -%}\n {%- if item['type'] == 'text' -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(item['text']) -}}\n {%- else -%}\n {{- item['text'] | trim -}}\n {%- endif -%}\n {%- elif item['type'] == 'image' -%}\n {{- '<|image|>' -}}\n {%- set ns.prev_message_type = 'image' -%}\n {%- elif item['type'] == 'audio' -%}\n {{- '<|audio|>' -}}\n {%- set ns.prev_message_type = 'audio' -%}\n {%- elif item['type'] == 'video' -%}\n {{- '<|video|>' -}}\n {%- set ns.prev_message_type = 'video' -%}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- endset -%}\n\n {{- captured_content -}}\n {%- set has_content = captured_content | trim | length > 0 -%}\n\n {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}\n {{- '<|tool_response>' -}}\n {%- elif not (ns_tr_out.flag and not has_content) -%}\n {{- '<turn|>\\n' -}}\n {%- endif -%}\n {%- endif -%}\n{%- endfor -%}\n\n{%- if add_generation_prompt -%}\n {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}\n {{- '<|turn>model\\n' -}}\n {%- if not enable_thinking | default(false) -%}\n {{- '<|channel>thought\\n<channel|>' -}}\n {%- endif -%}\n {%- endif -%}\n{%- endif -%}"
293
+ }