vMaroon commited on
Commit
ffc0d7c
·
verified ·
1 Parent(s): ad14a0a

chat template: pair tool results by sort-merge instead of list.index

Browse files
Files changed (1) hide show
  1. chat_template.jinja +22 -11
chat_template.jinja CHANGED
@@ -209,20 +209,31 @@ For each function call, output the function name and arguments within the follow
209
  {%- endif -%}
210
  {%- endif -%}
211
  {%- if ns_chk.can_sort -%}
 
 
 
 
212
  {%- for tc_id in ns_chk.tcs -%}
213
- {%- if tc_id in ns_chk.res -%}
214
- {%- set p = ns_chk.res.index(tc_id) -%}
215
- {%- set m = messages[ns_chk.mk[p]] -%}
216
- {%- if ns_chk.ek[p] >= 0 -%}
217
- {%- set entry = m.content[ns_chk.ek[p]] -%}
218
- {%- 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" -%}
219
- {{- tool_references_to_response(entry.output) -}}
220
- {%- else -%}
221
- {{- tool_response(visible_text(entry.output)) -}}
222
- {%- endif -%}
 
 
 
 
 
223
  {%- else -%}
224
- {{- render_tool_response(m) -}}
225
  {%- endif -%}
 
 
226
  {%- endif -%}
227
  {%- endfor -%}
228
  {%- else -%}
 
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 -%}