fix(chat_template): update SI and tool call handling (#26)

- fix(chat_template): update SI and tool call handling (291f4c095bb92d9293c405100babf9a6e5afaa52)


Co-authored-by: Douglas Reid <dougreid@users.noreply.huggingface.co>
This commit is contained in:
Ryan Mullins 2026-04-28 17:32:39 +00:00 committed by system
parent b4a601102c
commit 6b7e72c67d

@ -1,9 +1,9 @@
{%- macro format_parameters(properties, required) -%} {%- macro format_parameters(properties, required, filter_keys=false) -%}
{%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%} {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
{%- set ns = namespace(found_first=false) -%} {%- set ns = namespace(found_first=false) -%}
{%- for key, value in properties | dictsort -%} {%- for key, value in properties | dictsort -%}
{%- set add_comma = false -%} {%- set add_comma = false -%}
{%- if key not in standard_keys -%} {%- if not filter_keys or key not in standard_keys -%}
{%- if ns.found_first %},{% endif -%} {%- if ns.found_first %},{% endif -%}
{%- set ns.found_first = true -%} {%- set ns.found_first = true -%}
{{ key }}:{ {{ key }}:{
@ -65,7 +65,7 @@
{%- elif value is mapping -%} {%- elif value is mapping -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
properties:{ properties:{
{{- format_parameters(value, value['required'] | default([])) -}} {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
} }
{%- endif -%} {%- endif -%}
{%- if value['required'] -%} {%- if value['required'] -%}
@ -178,18 +178,21 @@
{#- Handle System/Tool Definitions Block -#} {#- Handle System/Tool Definitions Block -#}
{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%} {%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
{{- '<|turn>system\n' -}} {{- '<|turn>system\n' -}}
{#- Inject Thinking token at the very top of the FIRST system turn -#} {#- Inject Thinking token at the very top of the FIRST system turn -#}
{%- if enable_thinking is defined and enable_thinking -%} {%- if enable_thinking is defined and enable_thinking -%}
{{- '<|think|>\n' -}} {{- '<|think|>\n' -}}
{%- set ns.prev_message_type = 'think' -%} {%- set ns.prev_message_type = 'think' -%}
{%- endif -%} {%- endif -%}
{%- if messages[0]['role'] in ['system', 'developer'] -%} {%- if messages[0]['role'] in ['system', 'developer'] -%}
{%- if messages[0]['content'] is string -%}
{{- messages[0]['content'] | trim -}} {{- messages[0]['content'] | trim -}}
{%- elif messages[0]['content'] is sequence -%}
{%- for item in messages[0]['content'] -%}
{{- item['text'] | trim + ' '-}}
{%- endfor -%}
{%- endif -%}
{%- set loop_messages = messages[1:] -%} {%- set loop_messages = messages[1:] -%}
{%- endif -%} {%- endif -%}
{%- if tools -%} {%- if tools -%}
{%- for tool in tools %} {%- for tool in tools %}
{{- '<|tool>' -}} {{- '<|tool>' -}}
@ -198,7 +201,6 @@
{%- endfor %} {%- endfor %}
{%- set ns.prev_message_type = 'tool' -%} {%- set ns.prev_message_type = 'tool' -%}
{%- endif -%} {%- endif -%}
{{- '<turn|>\n' -}} {{- '<turn|>\n' -}}
{%- endif %} {%- endif %}
@ -302,6 +304,7 @@
{%- endfor -%} {%- endfor -%}
{%- endif -%} {%- endif -%}
{%- set captured_content -%}
{%- if message['content'] is string -%} {%- if message['content'] is string -%}
{%- if role == 'model' -%} {%- if role == 'model' -%}
{{- strip_thinking(message['content']) -}} {{- strip_thinking(message['content']) -}}
@ -328,10 +331,14 @@
{%- endif -%} {%- endif -%}
{%- endfor -%} {%- endfor -%}
{%- endif -%} {%- endif -%}
{%- endset -%}
{{- captured_content -}}
{%- set has_content = captured_content | trim | length > 0 -%}
{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%} {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
{{- '<|tool_response>' -}} {{- '<|tool_response>' -}}
{%- elif not (ns_tr_out.flag and not message.get('content')) -%} {%- elif not (ns_tr_out.flag and not has_content) -%}
{{- '<turn|>\n' -}} {{- '<turn|>\n' -}}
{%- endif -%} {%- endif -%}
{%- endif -%} {%- endif -%}