darkc0de commited on
Commit
ce20271
·
verified ·
1 Parent(s): 72123f2

(Trained with Unsloth)

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
chat_template.jinja ADDED
@@ -0,0 +1,454 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- macro format_argument(argument, escape_keys=True) -%}
2
+ {%- if argument is none -%}
3
+ {{- 'null' -}}
4
+ {%- elif argument is string -%}
5
+ {{- '<|"|>' + argument + '<|"|>' -}}
6
+ {%- elif argument is boolean -%}
7
+ {{- 'true' if argument else 'false' -}}
8
+ {%- elif argument is mapping -%}
9
+ {{- '{' -}}
10
+ {%- set ns = namespace(found_first=false) -%}
11
+ {%- for key, value in argument | dictsort -%}
12
+ {%- if ns.found_first %},{% endif -%}
13
+ {%- set ns.found_first = true -%}
14
+ {%- if escape_keys -%}
15
+ {{- '<|"|>' + key + '<|"|>' -}}
16
+ {%- else -%}
17
+ {{- key -}}
18
+ {%- endif -%}
19
+ :{{- format_argument(value, escape_keys=escape_keys) -}}
20
+ {%- endfor -%}
21
+ {{- '}' -}}
22
+ {%- elif argument is iterable and argument is not string -%}
23
+ {{- '[' -}}
24
+ {%- for item in argument -%}
25
+ {{- format_argument(item, escape_keys=escape_keys) -}}
26
+ {%- if not loop.last %},{% endif -%}
27
+ {%- endfor -%}
28
+ {{- ']' -}}
29
+ {%- else -%}
30
+ {{- argument -}}
31
+ {%- endif -%}
32
+ {%- endmacro -%}
33
+
34
+ {%- macro format_type_argument(type_value) -%}
35
+ {%- if type_value is none -%}
36
+ <|"|>OBJECT<|"|>
37
+ {%- elif type_value is string -%}
38
+ <|"|>{{ type_value | upper }}<|"|>
39
+ {%- elif type_value is iterable and type_value is not string -%}
40
+ [
41
+ {%- for item in type_value -%}
42
+ {%- if item is string -%}
43
+ <|"|>{{ item | upper }}<|"|>
44
+ {%- else -%}
45
+ {{- format_argument(item) -}}
46
+ {%- endif -%}
47
+ {%- if not loop.last %},{% endif -%}
48
+ {%- endfor -%}
49
+ ]
50
+ {%- else -%}
51
+ {{- format_argument(type_value) -}}
52
+ {%- endif -%}
53
+ {%- endmacro -%}
54
+
55
+ {%- macro format_parameters(properties, required, filter_keys=false) -%}
56
+ {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
57
+ {%- set ns = namespace(found_first=false) -%}
58
+ {%- for key, value in properties | dictsort -%}
59
+ {%- if not filter_keys or key not in standard_keys -%}
60
+ {%- if ns.found_first %},{% endif -%}
61
+ {%- set ns.found_first = true -%}
62
+ {{ key }}:{
63
+ {%- set add_comma = false -%}
64
+ {%- if value is mapping -%}
65
+ {%- set vtype = value['type'] if 'type' in value and value['type'] else none -%}
66
+ {%- set is_string_type = (vtype is string and vtype | upper == 'STRING') or (vtype is iterable and vtype is not string and ('string' in vtype or 'STRING' in vtype)) -%}
67
+ {%- set is_array_type = (vtype is string and vtype | upper == 'ARRAY') or (vtype is iterable and vtype is not string and ('array' in vtype or 'ARRAY' in vtype)) -%}
68
+ {%- set is_object_type = (vtype is string and vtype | upper == 'OBJECT') or (vtype is iterable and vtype is not string and ('object' in vtype or 'OBJECT' in vtype)) -%}
69
+ {%- if 'description' in value and value['description'] -%}
70
+ description:<|"|>{{ value['description'] }}<|"|>
71
+ {%- set add_comma = true -%}
72
+ {%- endif -%}
73
+ {%- if is_string_type -%}
74
+ {%- if 'enum' in value and value['enum'] -%}
75
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
76
+ enum:{{ format_argument(value['enum']) }}
77
+ {%- endif -%}
78
+ {%- elif is_array_type -%}
79
+ {%- if 'items' in value and value['items'] is mapping and value['items'] -%}
80
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
81
+ items:{
82
+ {%- set ns_items = namespace(found_first=false) -%}
83
+ {%- for item_key, item_value in value['items'] | dictsort -%}
84
+ {%- if item_value is not none -%}
85
+ {%- if ns_items.found_first %},{% endif -%}
86
+ {%- set ns_items.found_first = true -%}
87
+ {%- if item_key == 'properties' -%}
88
+ properties:{
89
+ {%- if item_value is mapping -%}
90
+ {{- format_parameters(item_value, value['items']['required'] if 'required' in value['items'] else []) -}}
91
+ {%- endif -%}
92
+ }
93
+ {%- elif item_key == 'required' -%}
94
+ required:[
95
+ {%- for req_item in item_value -%}
96
+ <|"|>{{- req_item -}}<|"|>
97
+ {%- if not loop.last %},{% endif -%}
98
+ {%- endfor -%}
99
+ ]
100
+ {%- elif item_key == 'type' -%}
101
+ type:{{ format_type_argument(item_value) }}
102
+ {%- else -%}
103
+ {{ item_key }}:{{ format_argument(item_value) }}
104
+ {%- endif -%}
105
+ {%- endif -%}
106
+ {%- endfor -%}
107
+ }
108
+ {%- endif -%}
109
+ {%- endif -%}
110
+ {%- if 'nullable' in value and value['nullable'] -%}
111
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
112
+ nullable:true
113
+ {%- endif -%}
114
+ {%- if is_object_type or ('properties' in value and value['properties'] is mapping) -%}
115
+ {%- if 'properties' in value and value['properties'] is mapping -%}
116
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
117
+ properties:{
118
+ {{- format_parameters(value['properties'], value['required'] if 'required' in value else []) -}}
119
+ }
120
+ {%- elif value is mapping -%}
121
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
122
+ properties:{
123
+ {{- format_parameters(value, value['required'] if 'required' in value else [], filter_keys=true) -}}
124
+ }
125
+ {%- endif -%}
126
+ {%- if 'required' in value and value['required'] -%}
127
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
128
+ required:[
129
+ {%- for item in value['required'] -%}
130
+ <|"|>{{- item -}}<|"|>
131
+ {%- if not loop.last %},{% endif -%}
132
+ {%- endfor -%}
133
+ ]
134
+ {%- endif -%}
135
+ {%- endif -%}
136
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
137
+ type:{{ format_type_argument(vtype if vtype else 'object') }}
138
+ {%- else -%}
139
+ type:{{ format_type_argument('object') }}
140
+ {%- endif -%}
141
+ }
142
+ {%- endif -%}
143
+ {%- endfor -%}
144
+ {%- endmacro -%}
145
+
146
+ {%- macro format_function_declaration(tool_data) -%}
147
+ {%- set fn = tool_data['function'] if tool_data is mapping and 'function' in tool_data else tool_data -%}
148
+ {%- set tool_name = fn['name'] if fn is mapping and 'name' in fn and fn['name'] else 'unknown' -%}
149
+ {%- set tool_description = fn['description'] if fn is mapping and 'description' in fn and fn['description'] else '' -%}
150
+ declaration:{{- tool_name -}}{description:<|"|>{{- tool_description -}}<|"|>
151
+ {%- set params = none -%}
152
+ {%- if fn is mapping and 'parameters' in fn and fn['parameters'] -%}
153
+ {%- set params = fn['parameters'] -%}
154
+ {%- elif fn is mapping and 'input_schema' in fn and fn['input_schema'] -%}
155
+ {%- set params = fn['input_schema'] -%}
156
+ {%- elif fn is mapping and 'inputSchema' in fn and fn['inputSchema'] -%}
157
+ {%- set params = fn['inputSchema'] -%}
158
+ {%- endif -%}
159
+ {%- if params and params is mapping -%}
160
+ ,parameters:{
161
+ {%- if 'properties' in params and params['properties'] -%}
162
+ properties:{ {{- format_parameters(params['properties'], params['required'] if 'required' in params else []) -}} },
163
+ {%- endif -%}
164
+ {%- if 'required' in params and params['required'] -%}
165
+ required:[
166
+ {%- for item in params['required'] -%}
167
+ <|"|>{{- item -}}<|"|>
168
+ {{- ',' if not loop.last -}}
169
+ {%- endfor -%}
170
+ ],
171
+ {%- endif -%}
172
+ {%- if 'type' in params and params['type'] -%}
173
+ type:{{- format_type_argument(params['type']) -}}}
174
+ {%- else -%}
175
+ type:{{- format_type_argument('object') -}}}
176
+ {%- endif -%}
177
+ {%- endif -%}
178
+ {%- if fn is mapping and 'response' in fn and fn['response'] is mapping -%}
179
+ {%- set response_declaration = fn['response'] -%}
180
+ ,response:{
181
+ {%- if 'description' in response_declaration and response_declaration['description'] -%}
182
+ description:<|"|>{{- response_declaration['description'] -}}<|"|>,
183
+ {%- endif -%}
184
+ {%- if 'type' in response_declaration and response_declaration['type'] -%}
185
+ type:{{- format_type_argument(response_declaration['type']) -}}}
186
+ {%- else -%}
187
+ type:{{- format_type_argument('object') -}}}
188
+ {%- endif -%}
189
+ {%- endif -%}
190
+ }
191
+ {%- endmacro -%}
192
+
193
+ {%- macro strip_thinking(text) -%}
194
+ {{- text | trim -}}
195
+ {%- endmacro -%}
196
+
197
+ {%- macro format_tool_response_block(tool_name, response) -%}
198
+ {{- '<|tool_response>' -}}
199
+ {%- if response is mapping -%}
200
+ {{- 'response:' + tool_name + '{' -}}
201
+ {%- for key, value in response | dictsort -%}
202
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
203
+ {%- if not loop.last %},{% endif -%}
204
+ {%- endfor -%}
205
+ {{- '}' -}}
206
+ {%- else -%}
207
+ {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
208
+ {%- endif -%}
209
+ {{- '<tool_response|>' -}}
210
+ {%- endmacro -%}
211
+
212
+ {#- ===== SETUP ===== -#}
213
+ {%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}
214
+ {%- set loop_messages = messages -%}
215
+ {%- set enable_thinking = enable_thinking if enable_thinking is defined else false -%}
216
+ {%- set preserve_thinking = preserve_thinking if preserve_thinking is defined else true -%}
217
+ {{- bos_token -}}
218
+ {#- Handle System/Tool Definitions Block -#}
219
+ {%- if enable_thinking or (tools is defined and tools) or (messages and messages[0]['role'] in ['system', 'developer']) -%}
220
+ {{- '<|turn>system\n' -}}
221
+ {#- Inject Thinking token at the very top of the FIRST system turn -#}
222
+ {%- if enable_thinking -%}
223
+ {{- '<|think|>\n' -}}
224
+ {%- set ns.prev_message_type = 'think' -%}
225
+ {%- endif -%}
226
+ {%- if messages and messages[0]['role'] in ['system', 'developer'] -%}
227
+ {%- if messages[0]['content'] is string -%}
228
+ {{- messages[0]['content'] | trim -}}
229
+ {%- elif messages[0]['content'] is iterable and messages[0]['content'] is not string -%}
230
+ {%- for item in messages[0]['content'] -%}
231
+ {%- if item is mapping and 'text' in item -%}{{- item['text'] | trim + ' '-}}{%- endif -%}
232
+ {%- endfor -%}
233
+ {%- endif -%}
234
+ {%- set loop_messages = messages[1:] -%}
235
+ {%- endif -%}
236
+ {%- if tools is defined and tools -%}
237
+ {%- for tool in tools %}
238
+ {{- '<|tool>' -}}
239
+ {{- format_function_declaration(tool) | trim -}}
240
+ {{- '<tool|>' -}}
241
+ {%- endfor %}
242
+ {%- set ns.prev_message_type = 'tool' -%}
243
+ {%- endif -%}
244
+ {{- '<turn|>\n' -}}
245
+ {%- endif %}
246
+
247
+ {#- Pre-scan: find last user message index for reasoning guard -#}
248
+ {%- set ns_turn = namespace(last_user_idx=-1) -%}
249
+ {%- for i in range(loop_messages | length) -%}
250
+ {%- if loop_messages[i]['role'] == 'user' -%}
251
+ {%- set ns_turn.last_user_idx = i -%}
252
+ {%- endif -%}
253
+ {%- endfor -%}
254
+
255
+ {#- Loop through messages -#}
256
+ {%- for message in loop_messages -%}
257
+ {%- if message['role'] != 'tool' -%}
258
+ {%- set ns.prev_message_type = None -%}
259
+ {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
260
+ {%- set message_has_tool_calls = ('tool_calls' in message and message['tool_calls']) -%}
261
+ {%- set message_has_tool_responses = ('tool_responses' in message and message['tool_responses']) -%}
262
+ {#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#}
263
+ {%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%}
264
+ {%- if not continue_same_model_turn -%}
265
+ {{- '<|turn>' + role + '\n' }}
266
+ {%- endif -%}
267
+
268
+ {#- Gemma 4 31B no-thinking SFT alignment: ordinary completed assistant turns
269
+ get an EMPTY thought channel before visible content. This preserves the
270
+ native thought->final boundary without adding reasoning traces. -#}
271
+ {%- set has_explicit_thinking = (
272
+ ('reasoning' in message and message['reasoning'])
273
+ or ('reasoning_content' in message and message['reasoning_content'])
274
+ ) -%}
275
+ {%- if role == 'model'
276
+ and not enable_thinking
277
+ and not continue_same_model_turn
278
+ and not has_explicit_thinking
279
+ and not message_has_tool_calls
280
+ and not message_has_tool_responses -%}
281
+ {{- '<|channel>thought\n<channel|>' -}}
282
+ {%- endif -%}
283
+
284
+ {#- Render reasoning/reasoning_content as thinking channel (tool-call turns only) -#}
285
+ {%- set thinking_ns = namespace(text='') -%}
286
+ {%- if 'reasoning' in message and message['reasoning'] -%}
287
+ {%- set thinking_ns.text = message['reasoning'] -%}
288
+ {%- elif 'reasoning_content' in message and message['reasoning_content'] -%}
289
+ {%- set thinking_ns.text = message['reasoning_content'] -%}
290
+ {%- endif -%}
291
+ {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or preserve_thinking -%}
292
+ {%- if thinking_ns.text and thinking_gate and message_has_tool_calls -%}
293
+ {{- '<|channel>thought\n' + thinking_ns.text + '\n<channel|>' -}}
294
+ {%- endif -%}
295
+
296
+ {%- if message_has_tool_calls -%}
297
+ {%- for tool_call in message['tool_calls'] -%}
298
+ {%- set function = tool_call['function'] if tool_call is mapping and 'function' in tool_call else tool_call -%}
299
+ {%- set function_name = function['name'] if function is mapping and 'name' in function and function['name'] else 'unknown' -%}
300
+ {{- '<|tool_call>call:' + function_name + '{' -}}
301
+ {%- if function is mapping and 'arguments' in function and function['arguments'] is mapping -%}
302
+ {%- set ns_args = namespace(found_first=false) -%}
303
+ {%- for key, value in function['arguments'] | dictsort -%}
304
+ {%- if ns_args.found_first %},{% endif -%}
305
+ {%- set ns_args.found_first = true -%}
306
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
307
+ {%- endfor -%}
308
+ {%- elif function is mapping and 'arguments' in function and function['arguments'] is string -%}
309
+ {{- function['arguments'] -}}
310
+ {%- elif function is mapping and 'arguments' in function and function['arguments'] is not none -%}
311
+ {{- format_argument(function['arguments'], escape_keys=False) -}}
312
+ {%- endif -%}
313
+ {{- '}<tool_call|>' -}}
314
+ {%- endfor -%}
315
+ {%- set ns.prev_message_type = 'tool_call' -%}
316
+ {%- endif -%}
317
+
318
+ {%- set ns_tr_out = namespace(flag=false) -%}
319
+ {%- if 'tool_responses' in message and message['tool_responses'] -%}
320
+ {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
321
+ {%- for tool_response in message['tool_responses'] -%}
322
+ {%- set tr_name = tool_response['name'] if tool_response is mapping and 'name' in tool_response and tool_response['name'] else 'unknown' -%}
323
+ {%- set tr_response = tool_response['response'] if tool_response is mapping and 'response' in tool_response else '' -%}
324
+ {{- format_tool_response_block(tr_name, tr_response) -}}
325
+ {%- set ns_tr_out.flag = true -%}
326
+ {%- set ns.prev_message_type = 'tool_response' -%}
327
+ {%- endfor -%}
328
+ {%- elif message_has_tool_calls -%}
329
+ {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
330
+ {%- set ns_tool_scan = namespace(stopped=false) -%}
331
+ {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
332
+ {%- if ns_tool_scan.stopped -%}
333
+ {%- elif loop_messages[k]['role'] != 'tool' -%}
334
+ {%- set ns_tool_scan.stopped = true -%}
335
+ {%- else -%}
336
+ {%- set follow = loop_messages[k] -%}
337
+ {#- Resolve tool_call_id to function name -#}
338
+ {%- set ns_tname = namespace(name='unknown') -%}
339
+ {%- if 'name' in follow and follow['name'] -%}
340
+ {%- set ns_tname.name = follow['name'] -%}
341
+ {%- endif -%}
342
+ {%- for tc in message['tool_calls'] -%}
343
+ {%- if tc is mapping and 'id' in tc and 'tool_call_id' in follow and tc['id'] == follow['tool_call_id'] -%}
344
+ {%- if 'function' in tc and 'name' in tc['function'] and tc['function']['name'] -%}
345
+ {%- set ns_tname.name = tc['function']['name'] -%}
346
+ {%- elif 'name' in tc and tc['name'] -%}
347
+ {%- set ns_tname.name = tc['name'] -%}
348
+ {%- endif -%}
349
+ {%- endif -%}
350
+ {%- endfor -%}
351
+ {#- Handle content as string, object, or content-parts array -#}
352
+ {%- set tool_body = follow['content'] if 'content' in follow else '' -%}
353
+ {%- if tool_body is string -%}
354
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
355
+ {%- elif tool_body is mapping -%}
356
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
357
+ {%- elif tool_body is iterable and tool_body is not string -%}
358
+ {%- set ns_txt = namespace(s='') -%}
359
+ {%- for part in tool_body -%}
360
+ {%- if part is mapping and 'type' in part and part['type'] == 'text' and 'text' in part -%}
361
+ {%- set ns_txt.s = ns_txt.s + part['text'] -%}
362
+ {%- endif -%}
363
+ {%- endfor -%}
364
+ {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
365
+ {%- for part in tool_body -%}
366
+ {%- if part is mapping and 'type' in part and part['type'] in ['image', 'image_url'] -%}
367
+ {{- '<|image|>' -}}
368
+ {%- elif part is mapping and 'type' in part and part['type'] in ['audio', 'input_audio'] -%}
369
+ {{- '<|audio|>' -}}
370
+ {%- elif part is mapping and 'type' in part and part['type'] == 'video' -%}
371
+ {{- '<|video|>' -}}
372
+ {%- endif -%}
373
+ {%- endfor -%}
374
+ {%- else -%}
375
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
376
+ {%- endif -%}
377
+ {%- set ns_tr_out.flag = true -%}
378
+ {%- set ns.prev_message_type = 'tool_response' -%}
379
+ {%- endif -%}
380
+ {%- endfor -%}
381
+ {%- endif -%}
382
+
383
+ {%- set captured_content -%}
384
+ {%- if 'content' in message and message['content'] is string -%}
385
+ {%- if role == 'model' -%}
386
+ {{- strip_thinking(message['content']) -}}
387
+ {%- else -%}
388
+ {{- message['content'] | trim -}}
389
+ {%- endif -%}
390
+ {%- elif 'content' in message and message['content'] is iterable and message['content'] is not string -%}
391
+ {%- for item in message['content'] -%}
392
+ {%- if item is mapping and 'type' in item and item['type'] == 'text' and 'text' in item -%}
393
+ {%- if role == 'model' -%}
394
+ {{- strip_thinking(item['text']) -}}
395
+ {%- else -%}
396
+ {{- item['text'] | trim -}}
397
+ {%- endif -%}
398
+ {%- elif item is mapping and 'type' in item and item['type'] in ['image', 'image_url'] -%}
399
+ {{- '<|image|>' -}}
400
+ {%- elif item is mapping and 'type' in item and item['type'] in ['audio', 'input_audio'] -%}
401
+ {{- '<|audio|>' -}}
402
+ {%- elif item is mapping and 'type' in item and item['type'] == 'video' -%}
403
+ {{- '<|video|>' -}}
404
+ {%- endif -%}
405
+ {%- endfor -%}
406
+ {%- endif -%}
407
+ {%- endset -%}
408
+
409
+ {{- captured_content -}}
410
+ {%- set has_content = captured_content | trim | length > 0 -%}
411
+
412
+ {#- Forward-scan: find next non-tool message role for continuation detection -#}
413
+ {%- set next_nt = namespace(role=None, found=false) -%}
414
+ {%- for j in range(loop.index0 + 1, loop_messages | length) -%}
415
+ {%- if not next_nt.found -%}
416
+ {%- if loop_messages[j]['role'] != 'tool' -%}
417
+ {%- set next_nt.role = loop_messages[j]['role'] -%}
418
+ {%- set next_nt.found = true -%}
419
+ {%- endif -%}
420
+ {%- endif -%}
421
+ {%- endfor -%}
422
+
423
+ {%- set continues_into_next = (
424
+ role == 'model'
425
+ and next_nt.role == 'assistant'
426
+ and not message_has_tool_calls
427
+ and not ns_tr_out.flag
428
+ ) -%}
429
+
430
+ {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
431
+ {{- '<|tool_response>' -}}
432
+ {%- elif continues_into_next -%}
433
+ {{- '\n' -}}
434
+ {%- elif not (ns_tr_out.flag and not has_content) -%}
435
+ {{- '<turn|>\n' -}}
436
+ {%- endif -%}
437
+
438
+ {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
439
+ {%- set ns.prev_non_tool_role = message['role'] -%}
440
+ {%- endif -%}
441
+ {%- endfor -%}
442
+
443
+ {%- if add_generation_prompt is defined and add_generation_prompt -%}
444
+ {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
445
+ {{- '<|turn>model\n' -}}
446
+ {%- endif -%}
447
+
448
+ {%- if not enable_thinking -%}
449
+ {#- Suppress thinking — but not when awaiting tool responses -#}
450
+ {%- if ns.prev_message_type != 'tool_call' -%}
451
+ {{- '<|channel>thought\n<channel|>' -}}
452
+ {%- endif -%}
453
+ {%- endif -%}
454
+ {%- endif -%}
config.json ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Gemma4ForConditionalGeneration"
4
+ ],
5
+ "audio_config": null,
6
+ "audio_token_id": 258881,
7
+ "boa_token_id": 256000,
8
+ "boi_token_id": 255999,
9
+ "torch_dtype": "bfloat16",
10
+ "eoa_token_id": 258883,
11
+ "eoa_token_index": 258883,
12
+ "eoi_token_id": 258882,
13
+ "eos_token_id": 106,
14
+ "image_token_id": 258880,
15
+ "initializer_range": 0.02,
16
+ "model_name": "darkc0de/gemma-4-31B-it-updated-heretic",
17
+ "model_type": "gemma4",
18
+ "pad_token_id": 0,
19
+ "text_config": {
20
+ "attention_bias": false,
21
+ "attention_dropout": 0.0,
22
+ "attention_k_eq_v": true,
23
+ "bos_token_id": 2,
24
+ "torch_dtype": "bfloat16",
25
+ "enable_moe_block": false,
26
+ "eos_token_id": 1,
27
+ "expert_intermediate_size": null,
28
+ "final_logit_softcapping": 30.0,
29
+ "global_head_dim": 512,
30
+ "head_dim": 256,
31
+ "hidden_activation": "gelu_pytorch_tanh",
32
+ "hidden_size": 5376,
33
+ "hidden_size_per_layer_input": 0,
34
+ "initializer_range": 0.02,
35
+ "intermediate_size": 21504,
36
+ "layer_types": [
37
+ "sliding_attention",
38
+ "sliding_attention",
39
+ "sliding_attention",
40
+ "sliding_attention",
41
+ "sliding_attention",
42
+ "full_attention",
43
+ "sliding_attention",
44
+ "sliding_attention",
45
+ "sliding_attention",
46
+ "sliding_attention",
47
+ "sliding_attention",
48
+ "full_attention",
49
+ "sliding_attention",
50
+ "sliding_attention",
51
+ "sliding_attention",
52
+ "sliding_attention",
53
+ "sliding_attention",
54
+ "full_attention",
55
+ "sliding_attention",
56
+ "sliding_attention",
57
+ "sliding_attention",
58
+ "sliding_attention",
59
+ "sliding_attention",
60
+ "full_attention",
61
+ "sliding_attention",
62
+ "sliding_attention",
63
+ "sliding_attention",
64
+ "sliding_attention",
65
+ "sliding_attention",
66
+ "full_attention",
67
+ "sliding_attention",
68
+ "sliding_attention",
69
+ "sliding_attention",
70
+ "sliding_attention",
71
+ "sliding_attention",
72
+ "full_attention",
73
+ "sliding_attention",
74
+ "sliding_attention",
75
+ "sliding_attention",
76
+ "sliding_attention",
77
+ "sliding_attention",
78
+ "full_attention",
79
+ "sliding_attention",
80
+ "sliding_attention",
81
+ "sliding_attention",
82
+ "sliding_attention",
83
+ "sliding_attention",
84
+ "full_attention",
85
+ "sliding_attention",
86
+ "sliding_attention",
87
+ "sliding_attention",
88
+ "sliding_attention",
89
+ "sliding_attention",
90
+ "full_attention",
91
+ "sliding_attention",
92
+ "sliding_attention",
93
+ "sliding_attention",
94
+ "sliding_attention",
95
+ "sliding_attention",
96
+ "full_attention"
97
+ ],
98
+ "max_position_embeddings": 262144,
99
+ "model_type": "gemma4_text",
100
+ "moe_intermediate_size": null,
101
+ "num_attention_heads": 32,
102
+ "num_experts": null,
103
+ "num_global_key_value_heads": 4,
104
+ "num_hidden_layers": 60,
105
+ "num_key_value_heads": 16,
106
+ "num_kv_shared_layers": 0,
107
+ "pad_token_id": 0,
108
+ "rms_norm_eps": 1e-06,
109
+ "rope_parameters": {
110
+ "full_attention": {
111
+ "partial_rotary_factor": 0.25,
112
+ "rope_theta": 1000000.0,
113
+ "rope_type": "proportional"
114
+ },
115
+ "sliding_attention": {
116
+ "rope_theta": 10000.0,
117
+ "rope_type": "default"
118
+ }
119
+ },
120
+ "sliding_window": 1024,
121
+ "tie_word_embeddings": true,
122
+ "top_k_experts": null,
123
+ "use_bidirectional_attention": "vision",
124
+ "use_cache": false,
125
+ "use_double_wide_mlp": false,
126
+ "vocab_size": 262144,
127
+ "vocab_size_per_layer_input": 262144
128
+ },
129
+ "tie_word_embeddings": true,
130
+ "unsloth_fixed": true,
131
+ "unsloth_version": "2026.8.18",
132
+ "video_token_id": 258884,
133
+ "vision_config": {
134
+ "_name_or_path": "",
135
+ "architectures": null,
136
+ "attention_bias": false,
137
+ "attention_dropout": 0.0,
138
+ "chunk_size_feed_forward": 0,
139
+ "default_output_length": 280,
140
+ "torch_dtype": "bfloat16",
141
+ "global_head_dim": 72,
142
+ "head_dim": 72,
143
+ "hidden_activation": "gelu_pytorch_tanh",
144
+ "hidden_size": 1152,
145
+ "id2label": {
146
+ "0": "LABEL_0",
147
+ "1": "LABEL_1"
148
+ },
149
+ "initializer_range": 0.02,
150
+ "intermediate_size": 4304,
151
+ "is_encoder_decoder": false,
152
+ "label2id": {
153
+ "LABEL_0": 0,
154
+ "LABEL_1": 1
155
+ },
156
+ "max_position_embeddings": 131072,
157
+ "model_type": "gemma4_vision",
158
+ "num_attention_heads": 16,
159
+ "num_hidden_layers": 27,
160
+ "num_key_value_heads": 16,
161
+ "output_attentions": false,
162
+ "output_hidden_states": false,
163
+ "patch_size": 16,
164
+ "pooling_kernel_size": 3,
165
+ "position_embedding_size": 10240,
166
+ "problem_type": null,
167
+ "return_dict": true,
168
+ "rms_norm_eps": 1e-06,
169
+ "rope_parameters": {
170
+ "rope_theta": 100.0,
171
+ "rope_type": "default"
172
+ },
173
+ "standardize": true,
174
+ "use_clipped_linears": false
175
+ },
176
+ "vision_soft_tokens_per_image": 280
177
+ }
generation_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 2,
3
+ "do_sample": true,
4
+ "eos_token_id": [
5
+ 1,
6
+ 106,
7
+ 50
8
+ ],
9
+ "pad_token_id": 0,
10
+ "temperature": 1.0,
11
+ "top_k": 64,
12
+ "top_p": 0.95,
13
+ "transformers_version": "5.5.0"
14
+ }
processor_config.json ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "audio_ms_per_token": 40,
3
+ "audio_seq_length": 750,
4
+ "feature_extractor": {
5
+ "dither": 0.0,
6
+ "feature_extractor_type": "Gemma4AudioFeatureExtractor",
7
+ "feature_size": 128,
8
+ "fft_length": 512,
9
+ "fft_overdrive": false,
10
+ "frame_length": 320,
11
+ "hop_length": 160,
12
+ "input_scale_factor": 1.0,
13
+ "max_frequency": 8000.0,
14
+ "mel_floor": 0.001,
15
+ "min_frequency": 0.0,
16
+ "padding_side": "right",
17
+ "padding_value": 0.0,
18
+ "per_bin_mean": null,
19
+ "per_bin_stddev": null,
20
+ "preemphasis": 0.0,
21
+ "preemphasis_htk_flavor": true,
22
+ "return_attention_mask": true,
23
+ "sampling_rate": 16000
24
+ },
25
+ "image_processor": {
26
+ "do_convert_rgb": true,
27
+ "do_normalize": false,
28
+ "do_rescale": true,
29
+ "do_resize": true,
30
+ "image_mean": [
31
+ 0.0,
32
+ 0.0,
33
+ 0.0
34
+ ],
35
+ "image_processor_type": "Gemma4ImageProcessor",
36
+ "image_seq_length": 280,
37
+ "image_std": [
38
+ 1.0,
39
+ 1.0,
40
+ 1.0
41
+ ],
42
+ "max_soft_tokens": 280,
43
+ "patch_size": 16,
44
+ "pooling_kernel_size": 3,
45
+ "resample": 3,
46
+ "rescale_factor": 0.00392156862745098
47
+ },
48
+ "image_seq_length": 280,
49
+ "processor_class": "Gemma4Processor",
50
+ "video_processor": {
51
+ "do_convert_rgb": true,
52
+ "do_normalize": true,
53
+ "do_rescale": true,
54
+ "do_resize": true,
55
+ "do_sample_frames": true,
56
+ "image_mean": [
57
+ 0.0,
58
+ 0.0,
59
+ 0.0
60
+ ],
61
+ "image_std": [
62
+ 1.0,
63
+ 1.0,
64
+ 1.0
65
+ ],
66
+ "max_soft_tokens": 70,
67
+ "num_frames": 32,
68
+ "patch_size": 16,
69
+ "pooling_kernel_size": 3,
70
+ "resample": 3,
71
+ "rescale_factor": 0.00392156862745098,
72
+ "return_metadata": false,
73
+ "video_processor_type": "Gemma4VideoProcessor"
74
+ }
75
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f
3
+ size 32169626
tokenizer_config.json ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": "<turn|>",
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": true,
21
+ "local_files_only": false,
22
+ "mask_token": "<mask>",
23
+ "max_length": null,
24
+ "model_max_length": 262144,
25
+ "model_specific_special_tokens": {
26
+ "audio_token": "<|audio|>",
27
+ "boa_token": "<|audio>",
28
+ "boi_token": "<|image>",
29
+ "eoa_token": "<audio|>",
30
+ "eoc_token": "<channel|>",
31
+ "eoi_token": "<image|>",
32
+ "eot_token": "<turn|>",
33
+ "escape_token": "<|\"|>",
34
+ "etc_token": "<tool_call|>",
35
+ "etd_token": "<tool|>",
36
+ "etr_token": "<tool_response|>",
37
+ "image_token": "<|image|>",
38
+ "soc_token": "<|channel>",
39
+ "sot_token": "<|turn>",
40
+ "stc_token": "<|tool_call>",
41
+ "std_token": "<|tool>",
42
+ "str_token": "<|tool_response>",
43
+ "think_token": "<|think|>"
44
+ },
45
+ "pad_to_multiple_of": null,
46
+ "pad_token": "<pad>",
47
+ "pad_token_type_id": 0,
48
+ "padding_side": "left",
49
+ "processor_class": "Gemma4Processor",
50
+ "response_schema": {
51
+ "properties": {
52
+ "content": {
53
+ "type": "string"
54
+ },
55
+ "role": {
56
+ "const": "assistant"
57
+ },
58
+ "thinking": {
59
+ "type": "string"
60
+ },
61
+ "tool_calls": {
62
+ "items": {
63
+ "properties": {
64
+ "function": {
65
+ "properties": {
66
+ "arguments": {
67
+ "additionalProperties": {},
68
+ "type": "object",
69
+ "x-parser": "gemma4-tool-call"
70
+ },
71
+ "name": {
72
+ "type": "string"
73
+ }
74
+ },
75
+ "type": "object",
76
+ "x-regex": "call\\:(?P<name>\\w+)(?P<arguments>\\{.*\\})"
77
+ },
78
+ "type": {
79
+ "const": "function"
80
+ }
81
+ },
82
+ "type": "object"
83
+ },
84
+ "type": "array",
85
+ "x-regex-iterator": "<\\|tool_call>(.*?)<tool_call\\|>"
86
+ }
87
+ },
88
+ "type": "object",
89
+ "x-regex": "(\\<\\|channel\\>thought\\n(?P<thinking>.*?)\\<channel\\|\\>)?(?P<tool_calls>\\<\\|tool_call\\>.*\\<tool_call\\|\\>)?(?P<content>(?:(?!\\<turn\\|\\>)(?!\\<\\|tool_response\\>).)+)?(?:\\<turn\\|\\>|\\<\\|tool_response\\>)?"
90
+ },
91
+ "soc_token": "<|channel>",
92
+ "sot_token": "<|turn>",
93
+ "stc_token": "<|tool_call>",
94
+ "std_token": "<|tool>",
95
+ "str_token": "<|tool_response>",
96
+ "think_token": "<|think|>",
97
+ "tokenizer_class": "GemmaTokenizer",
98
+ "unk_token": "<unk>",
99
+ "added_tokens_decoder": {
100
+ "0": {
101
+ "content": "<pad>",
102
+ "single_word": false,
103
+ "lstrip": false,
104
+ "rstrip": false,
105
+ "normalized": false,
106
+ "special": true
107
+ },
108
+ "1": {
109
+ "content": "<eos>",
110
+ "single_word": false,
111
+ "lstrip": false,
112
+ "rstrip": false,
113
+ "normalized": false,
114
+ "special": true
115
+ },
116
+ "2": {
117
+ "content": "<bos>",
118
+ "single_word": false,
119
+ "lstrip": false,
120
+ "rstrip": false,
121
+ "normalized": false,
122
+ "special": true
123
+ },
124
+ "3": {
125
+ "content": "<unk>",
126
+ "single_word": false,
127
+ "lstrip": false,
128
+ "rstrip": false,
129
+ "normalized": false,
130
+ "special": true
131
+ },
132
+ "4": {
133
+ "content": "<mask>",
134
+ "single_word": false,
135
+ "lstrip": false,
136
+ "rstrip": false,
137
+ "normalized": false,
138
+ "special": true
139
+ },
140
+ "46": {
141
+ "content": "<|tool>",
142
+ "single_word": false,
143
+ "lstrip": false,
144
+ "rstrip": false,
145
+ "normalized": false,
146
+ "special": true
147
+ },
148
+ "47": {
149
+ "content": "<tool|>",
150
+ "single_word": false,
151
+ "lstrip": false,
152
+ "rstrip": false,
153
+ "normalized": false,
154
+ "special": true
155
+ },
156
+ "48": {
157
+ "content": "<|tool_call>",
158
+ "single_word": false,
159
+ "lstrip": false,
160
+ "rstrip": false,
161
+ "normalized": false,
162
+ "special": true
163
+ },
164
+ "49": {
165
+ "content": "<tool_call|>",
166
+ "single_word": false,
167
+ "lstrip": false,
168
+ "rstrip": false,
169
+ "normalized": false,
170
+ "special": true
171
+ },
172
+ "50": {
173
+ "content": "<|tool_response>",
174
+ "single_word": false,
175
+ "lstrip": false,
176
+ "rstrip": false,
177
+ "normalized": false,
178
+ "special": true
179
+ },
180
+ "51": {
181
+ "content": "<tool_response|>",
182
+ "single_word": false,
183
+ "lstrip": false,
184
+ "rstrip": false,
185
+ "normalized": false,
186
+ "special": true
187
+ },
188
+ "52": {
189
+ "content": "<|\"|>",
190
+ "single_word": false,
191
+ "lstrip": false,
192
+ "rstrip": false,
193
+ "normalized": false,
194
+ "special": true
195
+ },
196
+ "98": {
197
+ "content": "<|think|>",
198
+ "single_word": false,
199
+ "lstrip": false,
200
+ "rstrip": false,
201
+ "normalized": false,
202
+ "special": true
203
+ },
204
+ "100": {
205
+ "content": "<|channel>",
206
+ "single_word": false,
207
+ "lstrip": false,
208
+ "rstrip": false,
209
+ "normalized": false,
210
+ "special": true
211
+ },
212
+ "101": {
213
+ "content": "<channel|>",
214
+ "single_word": false,
215
+ "lstrip": false,
216
+ "rstrip": false,
217
+ "normalized": false,
218
+ "special": true
219
+ },
220
+ "105": {
221
+ "content": "<|turn>",
222
+ "single_word": false,
223
+ "lstrip": false,
224
+ "rstrip": false,
225
+ "normalized": false,
226
+ "special": true
227
+ },
228
+ "106": {
229
+ "content": "<turn|>",
230
+ "single_word": false,
231
+ "lstrip": false,
232
+ "rstrip": false,
233
+ "normalized": false,
234
+ "special": true
235
+ },
236
+ "255999": {
237
+ "content": "<|image>",
238
+ "single_word": false,
239
+ "lstrip": false,
240
+ "rstrip": false,
241
+ "normalized": false,
242
+ "special": true
243
+ },
244
+ "256000": {
245
+ "content": "<|audio>",
246
+ "single_word": false,
247
+ "lstrip": false,
248
+ "rstrip": false,
249
+ "normalized": false,
250
+ "special": true
251
+ },
252
+ "258880": {
253
+ "content": "<|image|>",
254
+ "single_word": false,
255
+ "lstrip": false,
256
+ "rstrip": false,
257
+ "normalized": false,
258
+ "special": true
259
+ },
260
+ "258881": {
261
+ "content": "<|audio|>",
262
+ "single_word": false,
263
+ "lstrip": false,
264
+ "rstrip": false,
265
+ "normalized": false,
266
+ "special": true
267
+ },
268
+ "258882": {
269
+ "content": "<image|>",
270
+ "single_word": false,
271
+ "lstrip": false,
272
+ "rstrip": false,
273
+ "normalized": false,
274
+ "special": true
275
+ },
276
+ "258883": {
277
+ "content": "<audio|>",
278
+ "single_word": false,
279
+ "lstrip": false,
280
+ "rstrip": false,
281
+ "normalized": false,
282
+ "special": true
283
+ },
284
+ "258884": {
285
+ "content": "<|video|>",
286
+ "single_word": false,
287
+ "lstrip": false,
288
+ "rstrip": false,
289
+ "normalized": false,
290
+ "special": true
291
+ }
292
+ },
293
+ "chat_template": "{%- macro format_argument(argument, escape_keys=True) -%}\n {%- if argument is none -%}\n {{- 'null' -}}\n {%- elif 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 and argument is not string -%}\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\n{%- macro format_type_argument(type_value) -%}\n {%- if type_value is none -%}\n <|\"|>OBJECT<|\"|>\n {%- elif 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 {%- if item is string -%}\n <|\"|>{{ item | upper }}<|\"|>\n {%- else -%}\n {{- format_argument(item) -}}\n {%- endif -%}\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 {%- 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 {%- set add_comma = false -%}\n {%- if value is mapping -%}\n {%- set vtype = value['type'] if 'type' in value and value['type'] else none -%}\n {%- set is_string_type = (vtype is string and vtype | upper == 'STRING') or (vtype is iterable and vtype is not string and ('string' in vtype or 'STRING' in vtype)) -%}\n {%- set is_array_type = (vtype is string and vtype | upper == 'ARRAY') or (vtype is iterable and vtype is not string and ('array' in vtype or 'ARRAY' in vtype)) -%}\n {%- set is_object_type = (vtype is string and vtype | upper == 'OBJECT') or (vtype is iterable and vtype is not string and ('object' in vtype or 'OBJECT' in vtype)) -%}\n {%- if 'description' in value and value['description'] -%}\n description:<|\"|>{{ value['description'] }}<|\"|>\n {%- set add_comma = true -%}\n {%- endif -%}\n {%- if is_string_type -%}\n {%- if 'enum' in value and value['enum'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n enum:{{ format_argument(value['enum']) }}\n {%- endif -%}\n {%- elif is_array_type -%}\n {%- if 'items' in value and 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'] if 'required' in value['items'] else []) -}}\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 'nullable' in value and value['nullable'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n nullable:true\n {%- endif -%}\n {%- if is_object_type or ('properties' in value and value['properties'] is mapping) -%}\n {%- if 'properties' in value and value['properties'] is mapping -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n properties:{\n {{- format_parameters(value['properties'], value['required'] if 'required' in value else []) -}}\n }\n {%- elif value is mapping -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n properties:{\n {{- format_parameters(value, value['required'] if 'required' in value else [], filter_keys=true) -}}\n }\n {%- endif -%}\n {%- if 'required' in value and value['required'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n required:[\n {%- for item in value['required'] -%}\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(vtype if vtype else 'object') }}\n {%- else -%}\n type:{{ format_type_argument('object') }}\n {%- endif -%}\n }\n {%- endif -%}\n {%- endfor -%}\n{%- endmacro -%}\n\n{%- macro format_function_declaration(tool_data) -%}\n {%- set fn = tool_data['function'] if tool_data is mapping and 'function' in tool_data else tool_data -%}\n {%- set tool_name = fn['name'] if fn is mapping and 'name' in fn and fn['name'] else 'unknown' -%}\n {%- set tool_description = fn['description'] if fn is mapping and 'description' in fn and fn['description'] else '' -%}\n declaration:{{- tool_name -}}{description:<|\"|>{{- tool_description -}}<|\"|>\n {%- set params = none -%}\n {%- if fn is mapping and 'parameters' in fn and fn['parameters'] -%}\n {%- set params = fn['parameters'] -%}\n {%- elif fn is mapping and 'input_schema' in fn and fn['input_schema'] -%}\n {%- set params = fn['input_schema'] -%}\n {%- elif fn is mapping and 'inputSchema' in fn and fn['inputSchema'] -%}\n {%- set params = fn['inputSchema'] -%}\n {%- endif -%}\n {%- if params and params is mapping -%}\n ,parameters:{\n {%- if 'properties' in params and params['properties'] -%}\n properties:{ {{- format_parameters(params['properties'], params['required'] if 'required' in params else []) -}} },\n {%- endif -%}\n {%- if 'required' in params and params['required'] -%}\n required:[\n {%- for item in params['required'] -%}\n <|\"|>{{- item -}}<|\"|>\n {{- ',' if not loop.last -}}\n {%- endfor -%}\n ],\n {%- endif -%}\n {%- if 'type' in params and params['type'] -%}\n type:{{- format_type_argument(params['type']) -}}}\n {%- else -%}\n type:{{- format_type_argument('object') -}}}\n {%- endif -%}\n {%- endif -%}\n {%- if fn is mapping and 'response' in fn and fn['response'] is mapping -%}\n {%- set response_declaration = fn['response'] -%}\n ,response:{\n {%- if 'description' in response_declaration and response_declaration['description'] -%}\n description:<|\"|>{{- response_declaration['description'] -}}<|\"|>,\n {%- endif -%}\n {%- if 'type' in response_declaration and response_declaration['type'] -%}\n type:{{- format_type_argument(response_declaration['type']) -}}}\n {%- else -%}\n type:{{- format_type_argument('object') -}}}\n {%- endif -%}\n {%- endif -%}\n }\n{%- endmacro -%}\n\n{%- macro strip_thinking(text) -%}\n {{- text | 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{#- ===== SETUP ===== -#}\n{%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}\n{%- set loop_messages = messages -%}\n{%- set enable_thinking = enable_thinking if enable_thinking is defined else false -%}\n{%- set preserve_thinking = preserve_thinking if preserve_thinking is defined else true -%}\n{{- bos_token -}}\n{#- Handle System/Tool Definitions Block -#}\n{%- if enable_thinking or (tools is defined and tools) or (messages and 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 -%}\n {{- '<|think|>\\n' -}}\n {%- set ns.prev_message_type = 'think' -%}\n {%- endif -%}\n {%- if messages and 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 and messages[0]['content'] is not string -%}\n {%- for item in messages[0]['content'] -%}\n {%- if item is mapping and 'text' in item -%}{{- item['text'] | trim + ' '-}}{%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- set loop_messages = messages[1:] -%}\n {%- endif -%}\n {%- if tools is defined and 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 {%- set message_has_tool_calls = ('tool_calls' in message and message['tool_calls']) -%}\n {%- set message_has_tool_responses = ('tool_responses' in message and message['tool_responses']) -%}\n {#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#}\n {%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%}\n {%- if not continue_same_model_turn -%}\n {{- '<|turn>' + role + '\\n' }}\n {%- endif -%}\n\n {#- Gemma 4 31B no-thinking SFT alignment: ordinary completed assistant turns\n get an EMPTY thought channel before visible content. This preserves the\n native thought->final boundary without adding reasoning traces. -#}\n {%- set has_explicit_thinking = (\n ('reasoning' in message and message['reasoning'])\n or ('reasoning_content' in message and message['reasoning_content'])\n ) -%}\n {%- if role == 'model'\n and not enable_thinking\n and not continue_same_model_turn\n and not has_explicit_thinking\n and not message_has_tool_calls\n and not message_has_tool_responses -%}\n {{- '<|channel>thought\\n<channel|>' -}}\n {%- endif -%}\n\n {#- Render reasoning/reasoning_content as thinking channel (tool-call turns only) -#}\n {%- set thinking_ns = namespace(text='') -%}\n {%- if 'reasoning' in message and message['reasoning'] -%}\n {%- set thinking_ns.text = message['reasoning'] -%}\n {%- elif 'reasoning_content' in message and message['reasoning_content'] -%}\n {%- set thinking_ns.text = message['reasoning_content'] -%}\n {%- endif -%}\n {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or preserve_thinking -%}\n {%- if thinking_ns.text and thinking_gate and message_has_tool_calls -%}\n {{- '<|channel>thought\\n' + thinking_ns.text + '\\n<channel|>' -}}\n {%- endif -%}\n\n {%- if message_has_tool_calls -%}\n {%- for tool_call in message['tool_calls'] -%}\n {%- set function = tool_call['function'] if tool_call is mapping and 'function' in tool_call else tool_call -%}\n {%- set function_name = function['name'] if function is mapping and 'name' in function and function['name'] else 'unknown' -%}\n {{- '<|tool_call>call:' + function_name + '{' -}}\n {%- if function is mapping and 'arguments' in function and 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 is mapping and 'arguments' in function and function['arguments'] is string -%}\n {{- function['arguments'] -}}\n {%- elif function is mapping and 'arguments' in function and function['arguments'] is not none -%}\n {{- format_argument(function['arguments'], escape_keys=False) -}}\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 'tool_responses' in message and message['tool_responses'] -%}\n {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}\n {%- for tool_response in message['tool_responses'] -%}\n {%- set tr_name = tool_response['name'] if tool_response is mapping and 'name' in tool_response and tool_response['name'] else 'unknown' -%}\n {%- set tr_response = tool_response['response'] if tool_response is mapping and 'response' in tool_response else '' -%}\n {{- format_tool_response_block(tr_name, tr_response) -}}\n {%- set ns_tr_out.flag = true -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endfor -%}\n {%- elif message_has_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='unknown') -%}\n {%- if 'name' in follow and follow['name'] -%}\n {%- set ns_tname.name = follow['name'] -%}\n {%- endif -%}\n {%- for tc in message['tool_calls'] -%}\n {%- if tc is mapping and 'id' in tc and 'tool_call_id' in follow and tc['id'] == follow['tool_call_id'] -%}\n {%- if 'function' in tc and 'name' in tc['function'] and tc['function']['name'] -%}\n {%- set ns_tname.name = tc['function']['name'] -%}\n {%- elif 'name' in tc and tc['name'] -%}\n {%- set ns_tname.name = tc['name'] -%}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n {#- Handle content as string, object, or content-parts array -#}\n {%- set tool_body = follow['content'] if 'content' in follow else '' -%}\n {%- if tool_body is string -%}\n {{- format_tool_response_block(ns_tname.name, tool_body) -}}\n {%- elif tool_body is mapping -%}\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 is mapping and 'type' in part and part['type'] == 'text' and 'text' in part -%}\n {%- set ns_txt.s = ns_txt.s + part['text'] -%}\n {%- endif -%}\n {%- endfor -%}\n {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}\n {%- for part in tool_body -%}\n {%- if part is mapping and 'type' in part and part['type'] in ['image', 'image_url'] -%}\n {{- '<|image|>' -}}\n {%- elif part is mapping and 'type' in part and part['type'] in ['audio', 'input_audio'] -%}\n {{- '<|audio|>' -}}\n {%- elif part is mapping and 'type' in part and part['type'] == 'video' -%}\n {{- '<|video|>' -}}\n {%- endif -%}\n {%- endfor -%}\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 'content' in message and message['content'] is string -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(message['content']) -}}\n {%- else -%}\n {{- message['content'] | trim -}}\n {%- endif -%}\n {%- elif 'content' in message and message['content'] is iterable and message['content'] is not string -%}\n {%- for item in message['content'] -%}\n {%- if item is mapping and 'type' in item and item['type'] == 'text' and 'text' in item -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(item['text']) -}}\n {%- else -%}\n {{- item['text'] | trim -}}\n {%- endif -%}\n {%- elif item is mapping and 'type' in item and item['type'] in ['image', 'image_url'] -%}\n {{- '<|image|>' -}}\n {%- elif item is mapping and 'type' in item and item['type'] in ['audio', 'input_audio'] -%}\n {{- '<|audio|>' -}}\n {%- elif item is mapping and 'type' in item and item['type'] == 'video' -%}\n {{- '<|video|>' -}}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- endset -%}\n\n {{- captured_content -}}\n {%- set has_content = captured_content | trim | length > 0 -%}\n\n {#- Forward-scan: find next non-tool message role for continuation detection -#}\n {%- set next_nt = namespace(role=None, found=false) -%}\n {%- for j in range(loop.index0 + 1, loop_messages | length) -%}\n {%- if not next_nt.found -%}\n {%- if loop_messages[j]['role'] != 'tool' -%}\n {%- set next_nt.role = loop_messages[j]['role'] -%}\n {%- set next_nt.found = true -%}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n\n {%- set continues_into_next = (\n role == 'model'\n and next_nt.role == 'assistant'\n and not message_has_tool_calls\n and not ns_tr_out.flag\n ) -%}\n\n {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}\n {{- '<|tool_response>' -}}\n {%- elif continues_into_next -%}\n {{- '\\n' -}}\n {%- elif not (ns_tr_out.flag and not has_content) -%}\n {{- '<turn|>\\n' -}}\n {%- endif -%}\n\n {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}\n {%- set ns.prev_non_tool_role = message['role'] -%}\n {%- endif -%}\n{%- endfor -%}\n\n{%- if add_generation_prompt is defined and add_generation_prompt -%}\n {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}\n {{- '<|turn>model\\n' -}}\n {%- endif -%}\n\n {%- if not enable_thinking -%}\n {#- Suppress thinking — but not when awaiting tool responses -#}\n {%- if ns.prev_message_type != 'tool_call' -%}\n {{- '<|channel>thought\\n<channel|>' -}}\n {%- endif -%}\n {%- endif -%}\n{%- endif -%}\n"
294
+ }