Chat template: sync with zai-org/GLM-5.3-Flash (linear tool-result reordering)

#17
by vMaroon - opened
Files changed (1) hide show
  1. chat_template.jinja +56 -56
chat_template.jinja CHANGED
@@ -68,8 +68,8 @@ For each function call, output the function name and arguments within the follow
68
  {{- emit_audio() -}}
69
  {%- endif -%}
70
  {%- endfor -%}
71
- {%- else -%}
72
- {{- content }}
73
  {%- endif -%}
74
  {%- endmacro -%}
75
  {%- macro tool_response(text) -%}
@@ -159,7 +159,7 @@ For each function call, output the function name and arguments within the follow
159
  {%- if tc.function %}
160
  {%- set tc = tc.function %}
161
  {%- endif %}
162
- {{- '<tool_call>' + tc.name -}}
163
  {% set _args = tc.arguments %}{% for k, v in _args.items() %}<arg_key>{{ k }}</arg_key><arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_value>{% endfor %}</tool_call>{% endfor %}
164
  {% endif %}
165
  {%- elif m.role == 'tool' -%}
@@ -178,69 +178,69 @@ For each function call, output the function name and arguments within the follow
178
  {%- if block_start > 0 and messages[block_start - 1].role == 'assistant' and messages[block_start - 1].tool_calls -%}
179
  {%- set ns_a.tool_calls = messages[block_start - 1].tool_calls -%}
180
  {%- endif -%}
181
- {%- set ns_chk = namespace(can_sort=true) -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  {%- if not ns_a.tool_calls -%}
183
  {%- set ns_chk.can_sort = false -%}
184
  {%- else -%}
185
- {%- for k in range(block_start, ns_blk.end + 1) -%}
186
- {%- set m = messages[k] -%}
187
- {%- if is_list_of_outputs(m) -%}
188
- {%- for entry in m.content -%}
189
- {%- set eid = id_of(entry) -%}
190
- {%- if not eid -%}
191
- {%- set ns_chk.can_sort = false -%}
192
- {%- elif has_dup_tool_result_id(block_start, ns_blk.end, eid) -%}
193
- {%- set ns_chk.can_sort = false -%}
194
- {%- elif not tc_id_exists(ns_a.tool_calls, eid) -%}
195
- {%- set ns_chk.can_sort = false -%}
196
- {%- endif -%}
197
- {%- endfor -%}
198
- {%- else -%}
199
- {%- set tk_id = id_of(m) -%}
200
- {%- if not tk_id -%}
201
- {%- set ns_chk.can_sort = false -%}
202
- {%- elif has_dup_tool_result_id(block_start, ns_blk.end, tk_id) -%}
203
- {%- set ns_chk.can_sort = false -%}
204
- {%- elif not tc_id_exists(ns_a.tool_calls, tk_id) -%}
205
- {%- set ns_chk.can_sort = false -%}
206
- {%- endif -%}
207
- {%- endif -%}
208
  {%- endfor -%}
209
- {%- for i in range(ns_a.tool_calls | length) -%}
210
- {%- set tc_id = id_of(ns_a.tool_calls[i]) -%}
211
- {%- if not tc_id -%}
212
- {%- set ns_chk.can_sort = false -%}
213
- {%- endif -%}
214
- {%- for j in range(i + 1, ns_a.tool_calls | length) -%}
215
- {%- if id_of(ns_a.tool_calls[j]) == tc_id -%}
 
 
216
  {%- set ns_chk.can_sort = false -%}
 
217
  {%- endif -%}
218
  {%- endfor -%}
219
- {%- endfor -%}
220
  {%- endif -%}
221
  {%- if ns_chk.can_sort -%}
222
- {%- for tc in ns_a.tool_calls -%}
223
- {%- set tc_id = id_of(tc) -%}
224
- {%- for k in range(block_start, ns_blk.end + 1) -%}
225
- {%- set m = messages[k] -%}
226
- {%- if is_list_of_outputs(m) -%}
227
- {%- for entry in m.content -%}
228
- {%- set eid = id_of(entry) -%}
229
- {%- if eid == tc_id -%}
230
- {%- if entry.output is iterable and entry.output is not string and entry.output is not mapping and entry.output and entry.output.0.type == "tool_reference" -%}
231
- {{- tool_references_to_response(entry.output) -}}
232
- {%- else -%}
233
- {{- tool_response(visible_text(entry.output)) -}}
234
- {%- endif -%}
235
- {%- endif -%}
236
- {%- endfor -%}
237
- {%- else -%}
238
- {%- set tk_id = id_of(m) -%}
239
- {%- if tk_id == tc_id -%}
240
- {{- render_tool_response(m) -}}
241
- {%- endif -%}
 
 
242
  {%- endif -%}
243
- {%- endfor -%}
 
 
244
  {%- endfor -%}
245
  {%- else -%}
246
  {%- for k in range(block_start, ns_blk.end + 1) -%}
 
68
  {{- emit_audio() -}}
69
  {%- endif -%}
70
  {%- endfor -%}
71
+ {%- elif content is not none -%}
72
+ {{- content -}}
73
  {%- endif -%}
74
  {%- endmacro -%}
75
  {%- macro tool_response(text) -%}
 
159
  {%- if tc.function %}
160
  {%- set tc = tc.function %}
161
  {%- endif %}
162
+ {{- '<tool_call>' ~ tc.name -}}
163
  {% set _args = tc.arguments %}{% for k, v in _args.items() %}<arg_key>{{ k }}</arg_key><arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_value>{% endfor %}</tool_call>{% endfor %}
164
  {% endif %}
165
  {%- elif m.role == 'tool' -%}
 
178
  {%- if block_start > 0 and messages[block_start - 1].role == 'assistant' and messages[block_start - 1].tool_calls -%}
179
  {%- set ns_a.tool_calls = messages[block_start - 1].tool_calls -%}
180
  {%- endif -%}
181
+ {%- set ns_chk = namespace(can_sort=true, res=[], mk=[], ek=[], tcs=[]) -%}
182
+ {%- for k in range(block_start, ns_blk.end + 1) -%}
183
+ {%- set m = messages[k] -%}
184
+ {%- if is_list_of_outputs(m) -%}
185
+ {%- for entry in m.content -%}
186
+ {%- set ns_chk.res = ns_chk.res + [id_of(entry)] -%}
187
+ {%- set ns_chk.mk = ns_chk.mk + [k] -%}
188
+ {%- set ns_chk.ek = ns_chk.ek + [loop.index0] -%}
189
+ {%- endfor -%}
190
+ {%- else -%}
191
+ {%- set ns_chk.res = ns_chk.res + [id_of(m)] -%}
192
+ {%- set ns_chk.mk = ns_chk.mk + [k] -%}
193
+ {%- set ns_chk.ek = ns_chk.ek + [-1] -%}
194
+ {%- endif -%}
195
+ {%- endfor -%}
196
  {%- if not ns_a.tool_calls -%}
197
  {%- set ns_chk.can_sort = false -%}
198
  {%- else -%}
199
+ {%- for tc in ns_a.tool_calls -%}
200
+ {%- set ns_chk.tcs = ns_chk.tcs + [id_of(tc)] -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  {%- endfor -%}
202
+ {%- if '' in ns_chk.res or '' in ns_chk.tcs -%}
203
+ {%- set ns_chk.can_sort = false -%}
204
+ {%- elif ns_chk.res | unique(case_sensitive=true) | list | length != ns_chk.res | length -%}
205
+ {%- set ns_chk.can_sort = false -%}
206
+ {%- elif ns_chk.tcs | unique(case_sensitive=true) | list | length != ns_chk.tcs | length -%}
207
+ {%- set ns_chk.can_sort = false -%}
208
+ {%- else -%}
209
+ {%- for eid in ns_chk.res -%}
210
+ {%- if eid not in ns_chk.tcs -%}
211
  {%- set ns_chk.can_sort = false -%}
212
+ {%- break -%}
213
  {%- endif -%}
214
  {%- endfor -%}
215
+ {%- endif -%}
216
  {%- endif -%}
217
  {%- if ns_chk.can_sort -%}
218
+ {%- set ns_ord = namespace(res=[], tcs=[], j=0, pairs=[]) -%}
219
+ {%- for eid in ns_chk.res -%}
220
+ {%- set ns_ord.res = ns_ord.res + [{'id': eid, 'p': loop.index0}] -%}
221
+ {%- endfor -%}
222
+ {%- for tc_id in ns_chk.tcs -%}
223
+ {%- set ns_ord.tcs = ns_ord.tcs + [{'id': tc_id, 'i': loop.index0}] -%}
224
+ {%- endfor -%}
225
+ {%- set ns_ord.res = ns_ord.res | sort(attribute='id', case_sensitive=true) -%}
226
+ {%- for tc in ns_ord.tcs | sort(attribute='id', case_sensitive=true) -%}
227
+ {%- if ns_ord.j < ns_ord.res | length and ns_ord.res[ns_ord.j].id == tc.id -%}
228
+ {%- set ns_ord.pairs = ns_ord.pairs + [{'i': tc.i, 'p': ns_ord.res[ns_ord.j].p}] -%}
229
+ {%- set ns_ord.j = ns_ord.j + 1 -%}
230
+ {%- endif -%}
231
+ {%- endfor -%}
232
+ {%- for pair in ns_ord.pairs | sort(attribute='i') -%}
233
+ {%- set m = messages[ns_chk.mk[pair.p]] -%}
234
+ {%- if ns_chk.ek[pair.p] >= 0 -%}
235
+ {%- set entry = m.content[ns_chk.ek[pair.p]] -%}
236
+ {%- if entry.output is iterable and entry.output is not string and entry.output is not mapping and entry.output and entry.output.0.type == "tool_reference" -%}
237
+ {{- tool_references_to_response(entry.output) -}}
238
+ {%- else -%}
239
+ {{- tool_response(visible_text(entry.output)) -}}
240
  {%- endif -%}
241
+ {%- else -%}
242
+ {{- render_tool_response(m) -}}
243
+ {%- endif -%}
244
  {%- endfor -%}
245
  {%- else -%}
246
  {%- for k in range(block_start, ns_blk.end + 1) -%}