Chat template: map tool results to tool call positions in one pass

#4
by vMaroon - opened
Files changed (1) hide show
  1. chat_template.jinja +43 -6
chat_template.jinja CHANGED
@@ -39,8 +39,10 @@ These instructions serve as your defaults, but they can be overridden in subsequ
39
  "is_error": null
40
  }
41
  ]<|END_TOOL_RESULT|><|END_OF_TURN_TOKEN|>{%- endmacro %}
42
- {%- macro tool_call_id_to_int(messages, tool_call_id) %}
43
- {%- if regen_tool_call_ids -%}
 
 
44
  {%- set counter = namespace(value=0) %}
45
  {%- set tool_call_id_seen = namespace(value=false) %}
46
  {%- for msg in messages %}
@@ -58,9 +60,9 @@ These instructions serve as your defaults, but they can be overridden in subsequ
58
  {{ tool_call_id }}
59
  {%- endif -%}
60
  {%- endmacro %}
61
- {%- macro format_tool_message(messages, tool_msg) -%}
62
  {#- format tool message #}{
63
- "tool_call_id": "{{ tool_call_id_to_int(messages, tool_msg.tool_call_id) }}",
64
  "results": {
65
  {%- if tool_msg.content is mapping or tool_msg.content is string %}
66
 
@@ -211,8 +213,43 @@ Your output should adhere to the following json schema:
211
  {%- if render_developer_instruction -%}
212
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(developer_instruction) }}<|END_OF_TURN_TOKEN|>
213
  {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  {%- for message in messages %}
215
  {%- set msg_role_downcased = message.role | lower %}
 
 
216
  {%- if msg_role_downcased == 'system' and (not (loop.first and initial_instruction_message)) -%}
217
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(message) }}<|END_OF_TURN_TOKEN|>
218
  {%- elif msg_role_downcased == 'user' -%}
@@ -243,11 +280,11 @@ Your output should adhere to the following json schema:
243
  {%- endif %}
244
  {%- elif msg_role_downcased == 'tool' and message.tool_call_id not in tool_ids_seen.value -%}
245
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
246
- {{ format_tool_message(messages, message) }}
247
  {%- for msg in messages[loop.index0 + 1:] %}
248
 
249
  {%- if msg.role | lower == 'tool' %},
250
- {{ format_tool_message(messages, msg) }}
251
  {%- set tool_ids_seen.value = tool_ids_seen.value + [msg.tool_call_id] %}
252
  {%- else %}
253
  {%- break %}
 
39
  "is_error": null
40
  }
41
  ]<|END_TOOL_RESULT|><|END_OF_TURN_TOKEN|>{%- endmacro %}
42
+ {%- macro tool_call_id_to_int(messages, tool_call_id, hit) %}
43
+ {%- if regen_tool_call_ids and tool_call_id is string -%}
44
+ {{ hit }}
45
+ {%- elif regen_tool_call_ids -%}
46
  {%- set counter = namespace(value=0) %}
47
  {%- set tool_call_id_seen = namespace(value=false) %}
48
  {%- for msg in messages %}
 
60
  {{ tool_call_id }}
61
  {%- endif -%}
62
  {%- endmacro %}
63
+ {%- macro format_tool_message(messages, tool_msg, hit) -%}
64
  {#- format tool message #}{
65
+ "tool_call_id": "{{ tool_call_id_to_int(messages, tool_msg.tool_call_id, hit) }}",
66
  "results": {
67
  {%- if tool_msg.content is mapping or tool_msg.content is string %}
68
 
 
213
  {%- if render_developer_instruction -%}
214
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(developer_instruction) }}<|END_OF_TURN_TOKEN|>
215
  {%- endif %}
216
+ {#- tool_call_ints: one entry per tool message, in message order: the position, among all tool calls, of the first
217
+ tool call with the message's tool_call_id, or '' when none has it. Tool calls and tool messages are tagged, sorted
218
+ by id together (stable, so the calls of an id come first and keep their order) and paired in one pass. -#}
219
+ {%- set tl = namespace(items=[], calls=0, n=0) -%}
220
+ {%- for m in messages -%}
221
+ {%- if m.tool_calls -%}
222
+ {%- for tc in m.tool_calls -%}
223
+ {%- if tc.id is string -%}
224
+ {%- set tl.items = tl.items + [{'id': tc.id, 'k': -1, 'n': tl.calls}] -%}
225
+ {%- endif -%}
226
+ {%- set tl.calls = tl.calls + 1 -%}
227
+ {%- endfor -%}
228
+ {%- endif -%}
229
+ {%- endfor -%}
230
+ {%- for m in messages -%}
231
+ {%- if m.role | lower == 'tool' -%}
232
+ {%- set tl.items = tl.items + [{'id': m.tool_call_id if m.tool_call_id is string else '', 'k': tl.n}] -%}
233
+ {%- set tl.n = tl.n + 1 -%}
234
+ {%- endif -%}
235
+ {%- endfor -%}
236
+ {%- set pr = namespace(id=none, n='', hits=[]) -%}
237
+ {%- for it in tl.items | sort(attribute='id', case_sensitive=true) -%}
238
+ {%- if it.k < 0 -%}
239
+ {%- if pr.id is none or pr.id != it.id -%}
240
+ {%- set pr.id = it.id -%}
241
+ {%- set pr.n = it.n -%}
242
+ {%- endif -%}
243
+ {%- else -%}
244
+ {%- set pr.hits = pr.hits + [{'k': it.k, 'n': pr.n if pr.id is not none and pr.id == it.id else ''}] -%}
245
+ {%- endif -%}
246
+ {%- endfor -%}
247
+ {%- set tool_call_ints = pr.hits | sort(attribute='k') -%}
248
+ {%- set tl.n = 0 -%}
249
  {%- for message in messages %}
250
  {%- set msg_role_downcased = message.role | lower %}
251
+ {%- set tool_ord = tl.n %}
252
+ {%- if msg_role_downcased == 'tool' %}{%- set tl.n = tl.n + 1 %}{%- endif %}
253
  {%- if msg_role_downcased == 'system' and (not (loop.first and initial_instruction_message)) -%}
254
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(message) }}<|END_OF_TURN_TOKEN|>
255
  {%- elif msg_role_downcased == 'user' -%}
 
280
  {%- endif %}
281
  {%- elif msg_role_downcased == 'tool' and message.tool_call_id not in tool_ids_seen.value -%}
282
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
283
+ {{ format_tool_message(messages, message, tool_call_ints[tool_ord].n) }}
284
  {%- for msg in messages[loop.index0 + 1:] %}
285
 
286
  {%- if msg.role | lower == 'tool' %},
287
+ {{ format_tool_message(messages, msg, tool_call_ints[tool_ord + 1 + loop.index0].n) }}
288
  {%- set tool_ids_seen.value = tool_ids_seen.value + [msg.tool_call_id] %}
289
  {%- else %}
290
  {%- break %}