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

#18
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
 
@@ -215,8 +217,43 @@ Your output should adhere to the following json schema:
215
  {%- if render_developer_instruction -%}
216
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(developer_instruction) }}<|END_OF_TURN_TOKEN|>
217
  {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  {%- for message in messages %}
219
  {%- set msg_role_downcased = message.role | lower %}
 
 
220
  {%- if msg_role_downcased == 'system' and (not (loop.first and initial_instruction_message)) -%}
221
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(message) }}<|END_OF_TURN_TOKEN|>
222
  {%- elif msg_role_downcased == 'user' -%}
@@ -247,11 +284,11 @@ Your output should adhere to the following json schema:
247
  {%- endif %}
248
  {%- elif msg_role_downcased == 'tool' and message.tool_call_id not in tool_ids_seen.value -%}
249
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
250
- {{ format_tool_message(messages, message) }}
251
  {%- for msg in messages[loop.index0 + 1:] %}
252
 
253
  {%- if msg.role | lower == 'tool' %},
254
- {{ format_tool_message(messages, msg) }}
255
  {%- set tool_ids_seen.value = tool_ids_seen.value + [msg.tool_call_id] %}
256
  {%- else %}
257
  {%- 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
 
 
217
  {%- if render_developer_instruction -%}
218
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(developer_instruction) }}<|END_OF_TURN_TOKEN|>
219
  {%- endif %}
220
+ {#- tool_call_ints: one entry per tool message, in message order: the position, among all tool calls, of the first
221
+ tool call with the message's tool_call_id, or '' when none has it. Tool calls and tool messages are tagged, sorted
222
+ by id together (stable, so the calls of an id come first and keep their order) and paired in one pass. -#}
223
+ {%- set tl = namespace(items=[], calls=0, n=0) -%}
224
+ {%- for m in messages -%}
225
+ {%- if m.tool_calls -%}
226
+ {%- for tc in m.tool_calls -%}
227
+ {%- if tc.id is string -%}
228
+ {%- set tl.items = tl.items + [{'id': tc.id, 'k': -1, 'n': tl.calls}] -%}
229
+ {%- endif -%}
230
+ {%- set tl.calls = tl.calls + 1 -%}
231
+ {%- endfor -%}
232
+ {%- endif -%}
233
+ {%- endfor -%}
234
+ {%- for m in messages -%}
235
+ {%- if m.role | lower == 'tool' -%}
236
+ {%- set tl.items = tl.items + [{'id': m.tool_call_id if m.tool_call_id is string else '', 'k': tl.n}] -%}
237
+ {%- set tl.n = tl.n + 1 -%}
238
+ {%- endif -%}
239
+ {%- endfor -%}
240
+ {%- set pr = namespace(id=none, n='', hits=[]) -%}
241
+ {%- for it in tl.items | sort(attribute='id', case_sensitive=true) -%}
242
+ {%- if it.k < 0 -%}
243
+ {%- if pr.id is none or pr.id != it.id -%}
244
+ {%- set pr.id = it.id -%}
245
+ {%- set pr.n = it.n -%}
246
+ {%- endif -%}
247
+ {%- else -%}
248
+ {%- set pr.hits = pr.hits + [{'k': it.k, 'n': pr.n if pr.id is not none and pr.id == it.id else ''}] -%}
249
+ {%- endif -%}
250
+ {%- endfor -%}
251
+ {%- set tool_call_ints = pr.hits | sort(attribute='k') -%}
252
+ {%- set tl.n = 0 -%}
253
  {%- for message in messages %}
254
  {%- set msg_role_downcased = message.role | lower %}
255
+ {%- set tool_ord = tl.n %}
256
+ {%- if msg_role_downcased == 'tool' %}{%- set tl.n = tl.n + 1 %}{%- endif %}
257
  {%- if msg_role_downcased == 'system' and (not (loop.first and initial_instruction_message)) -%}
258
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(message) }}<|END_OF_TURN_TOKEN|>
259
  {%- elif msg_role_downcased == 'user' -%}
 
284
  {%- endif %}
285
  {%- elif msg_role_downcased == 'tool' and message.tool_call_id not in tool_ids_seen.value -%}
286
  <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
287
+ {{ format_tool_message(messages, message, tool_call_ints[tool_ord].n) }}
288
  {%- for msg in messages[loop.index0 + 1:] %}
289
 
290
  {%- if msg.role | lower == 'tool' %},
291
+ {{ format_tool_message(messages, msg, tool_call_ints[tool_ord + 1 + loop.index0].n) }}
292
  {%- set tool_ids_seen.value = tool_ids_seen.value + [msg.tool_call_id] %}
293
  {%- else %}
294
  {%- break %}