Commit ·
4c55b52
1
Parent(s): 7d4c97e
fix(chat_template): update SI and tool call handling (#36)
Browse files- fix(chat_template): update SI and tool call handling (76f20cbad876e343bfe8ccd5cbee029cfc962d3f)
Co-authored-by: Douglas Reid <dougreid@users.noreply.huggingface.co>
- chat_template.jinja +17 -10
chat_template.jinja
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
{%- macro format_parameters(properties, required) -%}
|
| 2 |
{%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
|
| 3 |
{%- set ns = namespace(found_first=false) -%}
|
| 4 |
{%- for key, value in properties | dictsort -%}
|
| 5 |
{%- set add_comma = false -%}
|
| 6 |
-
{%- if key not in standard_keys -%}
|
| 7 |
{%- if ns.found_first %},{% endif -%}
|
| 8 |
{%- set ns.found_first = true -%}
|
| 9 |
{{ key }}:{
|
|
@@ -65,7 +65,7 @@
|
|
| 65 |
{%- elif value is mapping -%}
|
| 66 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 67 |
properties:{
|
| 68 |
-
{{- format_parameters(value, value['required'] | default([])) -}}
|
| 69 |
}
|
| 70 |
{%- endif -%}
|
| 71 |
{%- if value['required'] -%}
|
|
@@ -178,18 +178,21 @@
|
|
| 178 |
{#- Handle System/Tool Definitions Block -#}
|
| 179 |
{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
|
| 180 |
{{- '<|turn>system\n' -}}
|
| 181 |
-
|
| 182 |
{#- Inject Thinking token at the very top of the FIRST system turn -#}
|
| 183 |
{%- if enable_thinking is defined and enable_thinking -%}
|
| 184 |
{{- '<|think|>\n' -}}
|
| 185 |
{%- set ns.prev_message_type = 'think' -%}
|
| 186 |
{%- endif -%}
|
| 187 |
-
|
| 188 |
{%- if messages[0]['role'] in ['system', 'developer'] -%}
|
| 189 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
{%- set loop_messages = messages[1:] -%}
|
| 191 |
{%- endif -%}
|
| 192 |
-
|
| 193 |
{%- if tools -%}
|
| 194 |
{%- for tool in tools %}
|
| 195 |
{{- '<|tool>' -}}
|
|
@@ -198,7 +201,6 @@
|
|
| 198 |
{%- endfor %}
|
| 199 |
{%- set ns.prev_message_type = 'tool' -%}
|
| 200 |
{%- endif -%}
|
| 201 |
-
|
| 202 |
{{- '<turn|>\n' -}}
|
| 203 |
{%- endif %}
|
| 204 |
|
|
@@ -302,6 +304,7 @@
|
|
| 302 |
{%- endfor -%}
|
| 303 |
{%- endif -%}
|
| 304 |
|
|
|
|
| 305 |
{%- if message['content'] is string -%}
|
| 306 |
{%- if role == 'model' -%}
|
| 307 |
{{- strip_thinking(message['content']) -}}
|
|
@@ -328,10 +331,14 @@
|
|
| 328 |
{%- endif -%}
|
| 329 |
{%- endfor -%}
|
| 330 |
{%- endif -%}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
|
| 333 |
{{- '<|tool_response>' -}}
|
| 334 |
-
{%- elif not (ns_tr_out.flag and not
|
| 335 |
{{- '<turn|>\n' -}}
|
| 336 |
{%- endif -%}
|
| 337 |
{%- endif -%}
|
|
@@ -344,4 +351,4 @@
|
|
| 344 |
{{- '<|channel>thought\n<channel|>' -}}
|
| 345 |
{%- endif -%}
|
| 346 |
{%- endif -%}
|
| 347 |
-
{%- endif -%}
|
|
|
|
| 1 |
+
{%- macro format_parameters(properties, required, filter_keys=false) -%}
|
| 2 |
{%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
|
| 3 |
{%- set ns = namespace(found_first=false) -%}
|
| 4 |
{%- for key, value in properties | dictsort -%}
|
| 5 |
{%- set add_comma = false -%}
|
| 6 |
+
{%- if not filter_keys or key not in standard_keys -%}
|
| 7 |
{%- if ns.found_first %},{% endif -%}
|
| 8 |
{%- set ns.found_first = true -%}
|
| 9 |
{{ key }}:{
|
|
|
|
| 65 |
{%- elif value is mapping -%}
|
| 66 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 67 |
properties:{
|
| 68 |
+
{{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
|
| 69 |
}
|
| 70 |
{%- endif -%}
|
| 71 |
{%- if value['required'] -%}
|
|
|
|
| 178 |
{#- Handle System/Tool Definitions Block -#}
|
| 179 |
{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
|
| 180 |
{{- '<|turn>system\n' -}}
|
|
|
|
| 181 |
{#- Inject Thinking token at the very top of the FIRST system turn -#}
|
| 182 |
{%- if enable_thinking is defined and enable_thinking -%}
|
| 183 |
{{- '<|think|>\n' -}}
|
| 184 |
{%- set ns.prev_message_type = 'think' -%}
|
| 185 |
{%- endif -%}
|
|
|
|
| 186 |
{%- if messages[0]['role'] in ['system', 'developer'] -%}
|
| 187 |
+
{%- if messages[0]['content'] is string -%}
|
| 188 |
+
{{- messages[0]['content'] | trim -}}
|
| 189 |
+
{%- elif messages[0]['content'] is sequence -%}
|
| 190 |
+
{%- for item in messages[0]['content'] -%}
|
| 191 |
+
{{- item['text'] | trim + ' '-}}
|
| 192 |
+
{%- endfor -%}
|
| 193 |
+
{%- endif -%}
|
| 194 |
{%- set loop_messages = messages[1:] -%}
|
| 195 |
{%- endif -%}
|
|
|
|
| 196 |
{%- if tools -%}
|
| 197 |
{%- for tool in tools %}
|
| 198 |
{{- '<|tool>' -}}
|
|
|
|
| 201 |
{%- endfor %}
|
| 202 |
{%- set ns.prev_message_type = 'tool' -%}
|
| 203 |
{%- endif -%}
|
|
|
|
| 204 |
{{- '<turn|>\n' -}}
|
| 205 |
{%- endif %}
|
| 206 |
|
|
|
|
| 304 |
{%- endfor -%}
|
| 305 |
{%- endif -%}
|
| 306 |
|
| 307 |
+
{%- set captured_content -%}
|
| 308 |
{%- if message['content'] is string -%}
|
| 309 |
{%- if role == 'model' -%}
|
| 310 |
{{- strip_thinking(message['content']) -}}
|
|
|
|
| 331 |
{%- endif -%}
|
| 332 |
{%- endfor -%}
|
| 333 |
{%- endif -%}
|
| 334 |
+
{%- endset -%}
|
| 335 |
+
|
| 336 |
+
{{- captured_content -}}
|
| 337 |
+
{%- set has_content = captured_content | trim | length > 0 -%}
|
| 338 |
|
| 339 |
{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
|
| 340 |
{{- '<|tool_response>' -}}
|
| 341 |
+
{%- elif not (ns_tr_out.flag and not has_content) -%}
|
| 342 |
{{- '<turn|>\n' -}}
|
| 343 |
{%- endif -%}
|
| 344 |
{%- endif -%}
|
|
|
|
| 351 |
{{- '<|channel>thought\n<channel|>' -}}
|
| 352 |
{%- endif -%}
|
| 353 |
{%- endif -%}
|
| 354 |
+
{%- endif -%}
|