Update chat template: tool-call author in history, end_sampling after assistant turns, reasoning-effort validation (minimal=0.1, raise on invalid), raise on unknown roles
Browse files- chat_template.jinja +17 -7
chat_template.jinja
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
{#- Keep Python's floating type spelling when
|
| 2 |
{%- macro json_scalar(value) -%}
|
| 3 |
{%- set serialized = value | tojson(ensure_ascii=false, separators=(',', ':')) -%}
|
| 4 |
{%- if value is float and '.' not in serialized and 'e' not in (serialized | lower) -%}
|
|
@@ -47,7 +47,7 @@
|
|
| 47 |
{#-
|
| 48 |
Decode a JSON string under Transformers, then re-encode it like json.dumps.
|
| 49 |
llama.cpp converts string arguments to objects after capability detection;
|
| 50 |
-
the fallback keeps
|
| 51 |
-#}
|
| 52 |
{%- macro json_string(source, state) -%}
|
| 53 |
{%- if lipsum is defined -%}
|
|
@@ -298,23 +298,26 @@
|
|
| 298 |
|
| 299 |
{#-
|
| 300 |
Match Python f"{float(effort):.2f}" followed by trailing-zero removal.
|
| 301 |
-
|
| 302 |
-#}
|
| 303 |
{%- macro reasoning_effort_text(effort) -%}
|
| 304 |
{%- set eff = effort -%}
|
| 305 |
{%- if eff is string -%}
|
| 306 |
{%- set e = eff | trim | lower -%}
|
| 307 |
{%- if e == 'none' -%}{%- set eff = 0.0 -%}
|
|
|
|
| 308 |
{%- elif e == 'low' -%}{%- set eff = 0.2 -%}
|
| 309 |
{%- elif e == 'medium' -%}{%- set eff = 0.7 -%}
|
| 310 |
{%- elif e == 'high' -%}{%- set eff = 0.9 -%}
|
| 311 |
{%- elif e == 'xhigh' -%}{%- set eff = 0.99 -%}
|
| 312 |
{%- elif e == 'max' -%}{%- set eff = 0.99 -%}
|
|
|
|
| 313 |
{%- endif -%}
|
| 314 |
{%- endif -%}
|
| 315 |
{%- set value = eff | float -%}
|
| 316 |
-
{%- if value < 0
|
| 317 |
-
|
|
|
|
| 318 |
{%- if value == value | int and value >= 0 and value <= 1 -%}
|
| 319 |
{{- value | int -}}
|
| 320 |
{%- else -%}
|
|
@@ -432,6 +435,7 @@
|
|
| 432 |
{%- endif -%}
|
| 433 |
{{- emit_message('tool', 'text', tool_content, resolved.name | string) -}}
|
| 434 |
{%- elif role == 'user' or role == 'assistant' or role == 'system' or role == 'developer' -%}
|
|
|
|
| 435 |
{%- if role == 'assistant' and message.get('reasoning_content') is string and message.get('reasoning_content') and
|
| 436 |
((preserve_thinking is not defined) or preserve_thinking is not false or message_index > thinking_state.last_user_index) -%}
|
| 437 |
{{- emit_message('assistant', 'thinking', message.get('reasoning_content')) -}}
|
|
@@ -484,13 +488,19 @@
|
|
| 484 |
{%- set raw_arguments = function.get('arguments') or {} -%}
|
| 485 |
{%- set arguments_json = canonical_arguments(raw_arguments) -%}
|
| 486 |
{%- set invocation = '{"name":' + canonical_json(function.get('name')) + ',"args":' + arguments_json + '}' -%}
|
| 487 |
-
{{- emit_message('assistant', 'invoke_tool_json', invocation) -}}
|
| 488 |
{%- endif -%}
|
| 489 |
{%- endfor -%}
|
| 490 |
{%- endif -%}
|
| 491 |
-
{%-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
{{- '<|content_model_end_sampling|>' -}}
|
| 493 |
{%- endif -%}
|
|
|
|
|
|
|
| 494 |
{%- endif -%}
|
| 495 |
{%- endfor -%}
|
| 496 |
|
|
|
|
| 1 |
+
{#- Keep Python's floating type spelling when the jinja engine compacts 1.0 to 1. -#}
|
| 2 |
{%- macro json_scalar(value) -%}
|
| 3 |
{%- set serialized = value | tojson(ensure_ascii=false, separators=(',', ':')) -%}
|
| 4 |
{%- if value is float and '.' not in serialized and 'e' not in (serialized | lower) -%}
|
|
|
|
| 47 |
{#-
|
| 48 |
Decode a JSON string under Transformers, then re-encode it like json.dumps.
|
| 49 |
llama.cpp converts string arguments to objects after capability detection;
|
| 50 |
+
the fallback keeps engine-only parsing safe without runtime-only filters.
|
| 51 |
-#}
|
| 52 |
{%- macro json_string(source, state) -%}
|
| 53 |
{%- if lipsum is defined -%}
|
|
|
|
| 298 |
|
| 299 |
{#-
|
| 300 |
Match Python f"{float(effort):.2f}" followed by trailing-zero removal.
|
| 301 |
+
llama.cpp's jinja engine has no round filter; the bit table handles binary64 midpoint equality.
|
| 302 |
-#}
|
| 303 |
{%- macro reasoning_effort_text(effort) -%}
|
| 304 |
{%- set eff = effort -%}
|
| 305 |
{%- if eff is string -%}
|
| 306 |
{%- set e = eff | trim | lower -%}
|
| 307 |
{%- if e == 'none' -%}{%- set eff = 0.0 -%}
|
| 308 |
+
{%- elif e == 'minimal' -%}{%- set eff = 0.1 -%}
|
| 309 |
{%- elif e == 'low' -%}{%- set eff = 0.2 -%}
|
| 310 |
{%- elif e == 'medium' -%}{%- set eff = 0.7 -%}
|
| 311 |
{%- elif e == 'high' -%}{%- set eff = 0.9 -%}
|
| 312 |
{%- elif e == 'xhigh' -%}{%- set eff = 0.99 -%}
|
| 313 |
{%- elif e == 'max' -%}{%- set eff = 0.99 -%}
|
| 314 |
+
{%- else -%}{%- set eff = e | float(-1.0) -%}
|
| 315 |
{%- endif -%}
|
| 316 |
{%- endif -%}
|
| 317 |
{%- set value = eff | float -%}
|
| 318 |
+
{%- if value < 0 or value > 0.99 -%}
|
| 319 |
+
{{- raise_exception('Invalid reasoning_effort: ' + (effort | string) + '; expected none/minimal/low/medium/high/xhigh/max or a number in [0.0, 0.99]') -}}
|
| 320 |
+
{%- endif -%}
|
| 321 |
{%- if value == value | int and value >= 0 and value <= 1 -%}
|
| 322 |
{{- value | int -}}
|
| 323 |
{%- else -%}
|
|
|
|
| 435 |
{%- endif -%}
|
| 436 |
{{- emit_message('tool', 'text', tool_content, resolved.name | string) -}}
|
| 437 |
{%- elif role == 'user' or role == 'assistant' or role == 'system' or role == 'developer' -%}
|
| 438 |
+
{%- set turn_out -%}
|
| 439 |
{%- if role == 'assistant' and message.get('reasoning_content') is string and message.get('reasoning_content') and
|
| 440 |
((preserve_thinking is not defined) or preserve_thinking is not false or message_index > thinking_state.last_user_index) -%}
|
| 441 |
{{- emit_message('assistant', 'thinking', message.get('reasoning_content')) -}}
|
|
|
|
| 488 |
{%- set raw_arguments = function.get('arguments') or {} -%}
|
| 489 |
{%- set arguments_json = canonical_arguments(raw_arguments) -%}
|
| 490 |
{%- set invocation = '{"name":' + canonical_json(function.get('name')) + ',"args":' + arguments_json + '}' -%}
|
| 491 |
+
{{- emit_message('assistant', 'invoke_tool_json', invocation, function.get('name')) -}}
|
| 492 |
{%- endif -%}
|
| 493 |
{%- endfor -%}
|
| 494 |
{%- endif -%}
|
| 495 |
+
{%- endset -%}
|
| 496 |
+
{{- turn_out -}}
|
| 497 |
+
{#- Close each historical model turn, but never emit a bare terminator for an
|
| 498 |
+
assistant message that rendered no blocks. -#}
|
| 499 |
+
{%- if role == 'assistant' and turn_out -%}
|
| 500 |
{{- '<|content_model_end_sampling|>' -}}
|
| 501 |
{%- endif -%}
|
| 502 |
+
{%- else -%}
|
| 503 |
+
{{- raise_exception('Unknown message role: ' + (role | string)) -}}
|
| 504 |
{%- endif -%}
|
| 505 |
{%- endfor -%}
|
| 506 |
|