Chat template: sync with zai-org/GLM-5.3 (linear tool-result reordering)
#2
by vMaroon - opened
- chat_template.jinja +51 -55
chat_template.jinja
CHANGED
|
@@ -172,73 +172,69 @@ For each function call, output the function name and arguments within the follow
|
|
| 172 |
{%- if block_start > 0 and messages[block_start - 1].role == 'assistant' and messages[block_start - 1].tool_calls -%}
|
| 173 |
{%- set ns_a.tool_calls = messages[block_start - 1].tool_calls -%}
|
| 174 |
{%- endif -%}
|
| 175 |
-
{%- set ns_chk = namespace(can_sort=true) -%}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
{%- if not ns_a.tool_calls -%}
|
| 177 |
{%- set ns_chk.can_sort = false -%}
|
| 178 |
{%- else -%}
|
| 179 |
-
{%- for
|
| 180 |
-
{%-
|
| 181 |
-
{%- set m = messages[k] -%}
|
| 182 |
-
{%- if is_list_of_outputs(m) -%}
|
| 183 |
-
{%- for entry in m.content -%}
|
| 184 |
-
{%- if not ns_chk.can_sort -%}{%- break -%}{%- endif -%}
|
| 185 |
-
{%- set eid = id_of(entry) -%}
|
| 186 |
-
{%- if not eid -%}
|
| 187 |
-
{%- set ns_chk.can_sort = false -%}
|
| 188 |
-
{%- elif has_dup_tool_result_id(block_start, ns_blk.end, eid) -%}
|
| 189 |
-
{%- set ns_chk.can_sort = false -%}
|
| 190 |
-
{%- elif not tc_id_exists(ns_a.tool_calls, eid) -%}
|
| 191 |
-
{%- set ns_chk.can_sort = false -%}
|
| 192 |
-
{%- endif -%}
|
| 193 |
-
{%- endfor -%}
|
| 194 |
-
{%- else -%}
|
| 195 |
-
{%- set tk_id = id_of(m) -%}
|
| 196 |
-
{%- if not tk_id -%}
|
| 197 |
-
{%- set ns_chk.can_sort = false -%}
|
| 198 |
-
{%- elif has_dup_tool_result_id(block_start, ns_blk.end, tk_id) -%}
|
| 199 |
-
{%- set ns_chk.can_sort = false -%}
|
| 200 |
-
{%- elif not tc_id_exists(ns_a.tool_calls, tk_id) -%}
|
| 201 |
-
{%- set ns_chk.can_sort = false -%}
|
| 202 |
-
{%- endif -%}
|
| 203 |
-
{%- endif -%}
|
| 204 |
{%- endfor -%}
|
| 205 |
-
{%-
|
| 206 |
-
{%-
|
| 207 |
-
|
| 208 |
-
{%-
|
| 209 |
-
|
| 210 |
-
{%-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
| 213 |
{%- set ns_chk.can_sort = false -%}
|
| 214 |
{%- break -%}
|
| 215 |
{%- endif -%}
|
| 216 |
{%- endfor -%}
|
| 217 |
-
{%-
|
| 218 |
{%- endif -%}
|
| 219 |
{%- if ns_chk.can_sort -%}
|
| 220 |
-
{%-
|
| 221 |
-
|
| 222 |
-
{%-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
{%- else -%}
|
| 236 |
-
{
|
| 237 |
-
{%- if tk_id == tc_id -%}
|
| 238 |
-
{{- render_tool_response(m) -}}
|
| 239 |
-
{%- endif -%}
|
| 240 |
{%- endif -%}
|
| 241 |
-
{%-
|
|
|
|
|
|
|
| 242 |
{%- endfor -%}
|
| 243 |
{%- else -%}
|
| 244 |
{%- for k in range(block_start, ns_blk.end + 1) -%}
|
|
|
|
| 172 |
{%- if block_start > 0 and messages[block_start - 1].role == 'assistant' and messages[block_start - 1].tool_calls -%}
|
| 173 |
{%- set ns_a.tool_calls = messages[block_start - 1].tool_calls -%}
|
| 174 |
{%- endif -%}
|
| 175 |
+
{%- set ns_chk = namespace(can_sort=true, res=[], mk=[], ek=[], tcs=[]) -%}
|
| 176 |
+
{%- for k in range(block_start, ns_blk.end + 1) -%}
|
| 177 |
+
{%- set m = messages[k] -%}
|
| 178 |
+
{%- if is_list_of_outputs(m) -%}
|
| 179 |
+
{%- for entry in m.content -%}
|
| 180 |
+
{%- set ns_chk.res = ns_chk.res + [id_of(entry)] -%}
|
| 181 |
+
{%- set ns_chk.mk = ns_chk.mk + [k] -%}
|
| 182 |
+
{%- set ns_chk.ek = ns_chk.ek + [loop.index0] -%}
|
| 183 |
+
{%- endfor -%}
|
| 184 |
+
{%- else -%}
|
| 185 |
+
{%- set ns_chk.res = ns_chk.res + [id_of(m)] -%}
|
| 186 |
+
{%- set ns_chk.mk = ns_chk.mk + [k] -%}
|
| 187 |
+
{%- set ns_chk.ek = ns_chk.ek + [-1] -%}
|
| 188 |
+
{%- endif -%}
|
| 189 |
+
{%- endfor -%}
|
| 190 |
{%- if not ns_a.tool_calls -%}
|
| 191 |
{%- set ns_chk.can_sort = false -%}
|
| 192 |
{%- else -%}
|
| 193 |
+
{%- for tc in ns_a.tool_calls -%}
|
| 194 |
+
{%- set ns_chk.tcs = ns_chk.tcs + [id_of(tc)] -%}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
{%- endfor -%}
|
| 196 |
+
{%- if '' in ns_chk.res or '' in ns_chk.tcs -%}
|
| 197 |
+
{%- set ns_chk.can_sort = false -%}
|
| 198 |
+
{%- elif ns_chk.res | unique(case_sensitive=true) | list | length != ns_chk.res | length -%}
|
| 199 |
+
{%- set ns_chk.can_sort = false -%}
|
| 200 |
+
{%- elif ns_chk.tcs | unique(case_sensitive=true) | list | length != ns_chk.tcs | length -%}
|
| 201 |
+
{%- set ns_chk.can_sort = false -%}
|
| 202 |
+
{%- else -%}
|
| 203 |
+
{%- for eid in ns_chk.res -%}
|
| 204 |
+
{%- if eid not in ns_chk.tcs -%}
|
| 205 |
{%- set ns_chk.can_sort = false -%}
|
| 206 |
{%- break -%}
|
| 207 |
{%- endif -%}
|
| 208 |
{%- endfor -%}
|
| 209 |
+
{%- endif -%}
|
| 210 |
{%- endif -%}
|
| 211 |
{%- if ns_chk.can_sort -%}
|
| 212 |
+
{%- set ns_ord = namespace(res=[], tcs=[], j=0, pairs=[]) -%}
|
| 213 |
+
{%- for eid in ns_chk.res -%}
|
| 214 |
+
{%- set ns_ord.res = ns_ord.res + [{'id': eid, 'p': loop.index0}] -%}
|
| 215 |
+
{%- endfor -%}
|
| 216 |
+
{%- for tc_id in ns_chk.tcs -%}
|
| 217 |
+
{%- set ns_ord.tcs = ns_ord.tcs + [{'id': tc_id, 'i': loop.index0}] -%}
|
| 218 |
+
{%- endfor -%}
|
| 219 |
+
{%- set ns_ord.res = ns_ord.res | sort(attribute='id', case_sensitive=true) -%}
|
| 220 |
+
{%- for tc in ns_ord.tcs | sort(attribute='id', case_sensitive=true) -%}
|
| 221 |
+
{%- if ns_ord.j < ns_ord.res | length and ns_ord.res[ns_ord.j].id == tc.id -%}
|
| 222 |
+
{%- set ns_ord.pairs = ns_ord.pairs + [{'i': tc.i, 'p': ns_ord.res[ns_ord.j].p}] -%}
|
| 223 |
+
{%- set ns_ord.j = ns_ord.j + 1 -%}
|
| 224 |
+
{%- endif -%}
|
| 225 |
+
{%- endfor -%}
|
| 226 |
+
{%- for pair in ns_ord.pairs | sort(attribute='i') -%}
|
| 227 |
+
{%- set m = messages[ns_chk.mk[pair.p]] -%}
|
| 228 |
+
{%- if ns_chk.ek[pair.p] >= 0 -%}
|
| 229 |
+
{%- set entry = m.content[ns_chk.ek[pair.p]] -%}
|
| 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 |
+
{%- else -%}
|
| 236 |
+
{{- render_tool_response(m) -}}
|
| 237 |
+
{%- endif -%}
|
| 238 |
{%- endfor -%}
|
| 239 |
{%- else -%}
|
| 240 |
{%- for k in range(block_start, ns_blk.end + 1) -%}
|