ZHANGYUXUAN-zR commited on
Commit
690b705
·
verified ·
1 Parent(s): 03eb536

chat template: early break in tool result reordering check

Browse files

Following the optimization in [this SGLang PR](https://github.com/sgl-project/sglang/pull/36957), added four `break` statements so the loop stops early once it detects that the results can't be reordered.

Files changed (1) hide show
  1. chat_template.jinja +10 -6
chat_template.jinja CHANGED
@@ -68,8 +68,8 @@ For each function call, output the function name and arguments within the follow
68
  {{- emit_audio() -}}
69
  {%- endif -%}
70
  {%- endfor -%}
71
- {%- else -%}
72
- {{- content }}
73
  {%- endif -%}
74
  {%- endmacro -%}
75
  {%- macro tool_response(text) -%}
@@ -159,7 +159,7 @@ For each function call, output the function name and arguments within the follow
159
  {%- if tc.function %}
160
  {%- set tc = tc.function %}
161
  {%- endif %}
162
- {{- '<tool_call>' + tc.name -}}
163
  {% set _args = tc.arguments %}{% for k, v in _args.items() %}<arg_key>{{ k }}</arg_key><arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_value>{% endfor %}</tool_call>{% endfor %}
164
  {% endif %}
165
  {%- elif m.role == 'tool' -%}
@@ -183,9 +183,11 @@ For each function call, output the function name and arguments within the follow
183
  {%- set ns_chk.can_sort = false -%}
184
  {%- else -%}
185
  {%- for k in range(block_start, ns_blk.end + 1) -%}
 
186
  {%- set m = messages[k] -%}
187
  {%- if is_list_of_outputs(m) -%}
188
  {%- for entry in m.content -%}
 
189
  {%- set eid = id_of(entry) -%}
190
  {%- if not eid -%}
191
  {%- set ns_chk.can_sort = false -%}
@@ -207,6 +209,7 @@ For each function call, output the function name and arguments within the follow
207
  {%- endif -%}
208
  {%- endfor -%}
209
  {%- for i in range(ns_a.tool_calls | length) -%}
 
210
  {%- set tc_id = id_of(ns_a.tool_calls[i]) -%}
211
  {%- if not tc_id -%}
212
  {%- set ns_chk.can_sort = false -%}
@@ -214,9 +217,10 @@ For each function call, output the function name and arguments within the follow
214
  {%- for j in range(i + 1, ns_a.tool_calls | length) -%}
215
  {%- if id_of(ns_a.tool_calls[j]) == tc_id -%}
216
  {%- set ns_chk.can_sort = false -%}
 
217
  {%- endif -%}
218
  {%- endfor -%}
219
- {%- endfor -%}
220
  {%- endif -%}
221
  {%- if ns_chk.can_sort -%}
222
  {%- for tc in ns_a.tool_calls -%}
@@ -233,8 +237,8 @@ For each function call, output the function name and arguments within the follow
233
  {{- tool_response(visible_text(entry.output)) -}}
234
  {%- endif -%}
235
  {%- endif -%}
236
- {%- endfor -%}
237
- {%- else -%}
238
  {%- set tk_id = id_of(m) -%}
239
  {%- if tk_id == tc_id -%}
240
  {{- render_tool_response(m) -}}
 
68
  {{- emit_audio() -}}
69
  {%- endif -%}
70
  {%- endfor -%}
71
+ {%- elif content is not none -%}
72
+ {{- content -}}
73
  {%- endif -%}
74
  {%- endmacro -%}
75
  {%- macro tool_response(text) -%}
 
159
  {%- if tc.function %}
160
  {%- set tc = tc.function %}
161
  {%- endif %}
162
+ {{- '<tool_call>' ~ tc.name -}}
163
  {% set _args = tc.arguments %}{% for k, v in _args.items() %}<arg_key>{{ k }}</arg_key><arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_value>{% endfor %}</tool_call>{% endfor %}
164
  {% endif %}
165
  {%- elif m.role == 'tool' -%}
 
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 -%}
 
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 -%}
 
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 -%}
 
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) -}}