chat template: speed up tool-result validation and reordering (GLM-5.3-Flash)

#63
Files changed (1) hide show
  1. chat_template.jinja +51 -55
chat_template.jinja CHANGED
@@ -178,73 +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
- {%- if not ns_chk.can_sort -%}{%- break -%}{%- endif -%}
187
- {%- set m = messages[k] -%}
188
- {%- if is_list_of_outputs(m) -%}
189
- {%- for entry in m.content -%}
190
- {%- if not ns_chk.can_sort -%}{%- break -%}{%- endif -%}
191
- {%- set eid = id_of(entry) -%}
192
- {%- if not eid -%}
193
- {%- set ns_chk.can_sort = false -%}
194
- {%- elif has_dup_tool_result_id(block_start, ns_blk.end, eid) -%}
195
- {%- set ns_chk.can_sort = false -%}
196
- {%- elif not tc_id_exists(ns_a.tool_calls, eid) -%}
197
- {%- set ns_chk.can_sort = false -%}
198
- {%- endif -%}
199
- {%- endfor -%}
200
- {%- else -%}
201
- {%- set tk_id = id_of(m) -%}
202
- {%- if not tk_id -%}
203
- {%- set ns_chk.can_sort = false -%}
204
- {%- elif has_dup_tool_result_id(block_start, ns_blk.end, tk_id) -%}
205
- {%- set ns_chk.can_sort = false -%}
206
- {%- elif not tc_id_exists(ns_a.tool_calls, tk_id) -%}
207
- {%- set ns_chk.can_sort = false -%}
208
- {%- endif -%}
209
- {%- endif -%}
210
  {%- endfor -%}
211
- {%- for i in range(ns_a.tool_calls | length) -%}
212
- {%- if not ns_chk.can_sort -%}{%- break -%}{%- endif -%}
213
- {%- set tc_id = id_of(ns_a.tool_calls[i]) -%}
214
- {%- if not tc_id -%}
215
- {%- set ns_chk.can_sort = false -%}
216
- {%- endif -%}
217
- {%- for j in range(i + 1, ns_a.tool_calls | length) -%}
218
- {%- if id_of(ns_a.tool_calls[j]) == tc_id -%}
 
219
  {%- set ns_chk.can_sort = false -%}
220
  {%- break -%}
221
  {%- endif -%}
222
  {%- endfor -%}
223
- {%- endfor -%}
224
  {%- endif -%}
225
  {%- if ns_chk.can_sort -%}
226
- {%- for tc in ns_a.tool_calls -%}
227
- {%- set tc_id = id_of(tc) -%}
228
- {%- for k in range(block_start, ns_blk.end + 1) -%}
229
- {%- set m = messages[k] -%}
230
- {%- if is_list_of_outputs(m) -%}
231
- {%- for entry in m.content -%}
232
- {%- set eid = id_of(entry) -%}
233
- {%- if eid == tc_id -%}
234
- {%- 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" -%}
235
- {{- tool_references_to_response(entry.output) -}}
236
- {%- else -%}
237
- {{- tool_response(visible_text(entry.output)) -}}
238
- {%- endif -%}
239
- {%- endif -%}
240
- {%- endfor -%}
 
 
 
 
 
241
  {%- else -%}
242
- {%- set tk_id = id_of(m) -%}
243
- {%- if tk_id == tc_id -%}
244
- {{- render_tool_response(m) -}}
245
- {%- endif -%}
246
  {%- endif -%}
247
- {%- endfor -%}
 
 
248
  {%- endfor -%}
249
  {%- else -%}
250
  {%- for k in range(block_start, ns_blk.end + 1) -%}
 
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) -%}