Sync chat_template.jinja with upstream (2026-08-26 16:31Z): fixes multimodal image/video/audio token emission
Browse files- chat_template.jinja +59 -12
chat_template.jinja
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
[gMASK]<sop>
|
| 2 |
{%- set effective_reasoning_effort = reasoning_effort if reasoning_effort is defined and reasoning_effort in ['low', 'high'] else 'max' -%}
|
| 3 |
{%- if effective_reasoning_effort is not none -%}<|system|>Reasoning Effort: {{ effective_reasoning_effort | capitalize }}{%- endif -%}
|
|
|
|
| 4 |
{%- if tools -%}
|
| 5 |
{%- macro tool_to_json(tool) -%}
|
| 6 |
{%- set ns_tool = namespace(first=true) -%}
|
|
@@ -47,18 +48,24 @@ You are provided with function signatures within <tools></tools> XML tags:
|
|
| 47 |
|
| 48 |
For each function call, output the function name and arguments within the following XML format:
|
| 49 |
<tool_call>{function-name}<arg_key>{arg-key-1}</arg_key><arg_value>{arg-value-1}</arg_value><arg_key>{arg-key-2}</arg_key><arg_value>{arg-value-2}</arg_value>...</tool_call>{%- endif -%}
|
|
|
|
|
|
|
|
|
|
| 50 |
{%- macro visible_text(content) -%}
|
| 51 |
{%- if content is string -%}
|
| 52 |
-
{{- content }}
|
| 53 |
{%- elif content is iterable and content is not mapping -%}
|
| 54 |
{%- for item in content -%}
|
| 55 |
{%- if item is mapping and item.type == 'text' -%}
|
| 56 |
-
{{- item.text }}
|
| 57 |
{%- elif item is string -%}
|
| 58 |
-
{{- item }}
|
| 59 |
-
{%- elif item is mapping and item.type in ['image', 'image_url'
|
| 60 |
-
{
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
{%- endif -%}
|
| 63 |
{%- endfor -%}
|
| 64 |
{%- else -%}
|
|
@@ -95,6 +102,33 @@ For each function call, output the function name and arguments within the follow
|
|
| 95 |
{%- macro is_list_of_outputs(m) -%}
|
| 96 |
{%- if m.content and m.content.0.output is defined -%}1{%- endif -%}
|
| 97 |
{%- endmacro -%}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
{%- set ns = namespace(last_user_index=-1) -%}
|
| 99 |
{%- for m in messages %}
|
| 100 |
{%- if m.role == 'user' %}
|
|
@@ -112,7 +146,7 @@ For each function call, output the function name and arguments within the follow
|
|
| 112 |
{%- set reasoning_content = content.split('</think>')[0].split('<think>')[-1] %}
|
| 113 |
{%- set content = content.split('</think>')[-1] %}
|
| 114 |
{%- endif %}
|
| 115 |
-
{%- if (
|
| 116 |
{{ '<think>' + reasoning_content + '</think>'}}
|
| 117 |
{%- else -%}
|
| 118 |
{{ '<think></think>' }}
|
|
@@ -155,21 +189,34 @@ For each function call, output the function name and arguments within the follow
|
|
| 155 |
{%- set eid = id_of(entry) -%}
|
| 156 |
{%- if not eid -%}
|
| 157 |
{%- set ns_chk.can_sort = false -%}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
{%- endif -%}
|
| 159 |
{%- endfor -%}
|
| 160 |
{%- else -%}
|
| 161 |
{%- set tk_id = id_of(m) -%}
|
| 162 |
{%- if not tk_id -%}
|
| 163 |
{%- set ns_chk.can_sort = false -%}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
{%- endif -%}
|
| 165 |
{%- endif -%}
|
| 166 |
{%- endfor -%}
|
| 167 |
-
{%- for
|
| 168 |
-
{%- set tc_id = id_of(
|
| 169 |
{%- if not tc_id -%}
|
| 170 |
{%- set ns_chk.can_sort = false -%}
|
| 171 |
{%- endif -%}
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
{%- endif -%}
|
| 174 |
{%- if ns_chk.can_sort -%}
|
| 175 |
{%- for tc in ns_a.tool_calls -%}
|
|
@@ -186,8 +233,8 @@ For each function call, output the function name and arguments within the follow
|
|
| 186 |
{{- tool_response(visible_text(entry.output)) -}}
|
| 187 |
{%- endif -%}
|
| 188 |
{%- endif -%}
|
| 189 |
-
|
| 190 |
-
|
| 191 |
{%- set tk_id = id_of(m) -%}
|
| 192 |
{%- if tk_id == tc_id -%}
|
| 193 |
{{- render_tool_response(m) -}}
|
|
|
|
| 1 |
[gMASK]<sop>
|
| 2 |
{%- set effective_reasoning_effort = reasoning_effort if reasoning_effort is defined and reasoning_effort in ['low', 'high'] else 'max' -%}
|
| 3 |
{%- if effective_reasoning_effort is not none -%}<|system|>Reasoning Effort: {{ effective_reasoning_effort | capitalize }}{%- endif -%}
|
| 4 |
+
{%- set clear_thinking = clear_thinking if clear_thinking is defined else false -%}
|
| 5 |
{%- if tools -%}
|
| 6 |
{%- macro tool_to_json(tool) -%}
|
| 7 |
{%- set ns_tool = namespace(first=true) -%}
|
|
|
|
| 48 |
|
| 49 |
For each function call, output the function name and arguments within the following XML format:
|
| 50 |
<tool_call>{function-name}<arg_key>{arg-key-1}</arg_key><arg_value>{arg-value-1}</arg_value><arg_key>{arg-key-2}</arg_key><arg_value>{arg-value-2}</arg_value>...</tool_call>{%- endif -%}
|
| 51 |
+
{%- macro emit_image() -%}<|begin_of_image|><|image|><|end_of_image|>{%- endmacro -%}
|
| 52 |
+
{%- macro emit_video() -%}<|begin_of_video|><|video|><|end_of_video|>{%- endmacro -%}
|
| 53 |
+
{%- macro emit_audio() -%}<|begin_of_audio|><|end_of_audio|>{%- endmacro -%}
|
| 54 |
{%- macro visible_text(content) -%}
|
| 55 |
{%- if content is string -%}
|
| 56 |
+
{{- content -}}
|
| 57 |
{%- elif content is iterable and content is not mapping -%}
|
| 58 |
{%- for item in content -%}
|
| 59 |
{%- if item is mapping and item.type == 'text' -%}
|
| 60 |
+
{{- item.text -}}
|
| 61 |
{%- elif item is string -%}
|
| 62 |
+
{{- item -}}
|
| 63 |
+
{%- elif item is mapping and item.type in ['image', 'image_url'] -%}
|
| 64 |
+
{{- emit_image() -}}
|
| 65 |
+
{%- elif item is mapping and item.type in ['video', 'video_url'] -%}
|
| 66 |
+
{{- emit_video() -}}
|
| 67 |
+
{%- elif item is mapping and item.type in ['audio', 'audio_url', 'input_audio'] -%}
|
| 68 |
+
{{- emit_audio() -}}
|
| 69 |
{%- endif -%}
|
| 70 |
{%- endfor -%}
|
| 71 |
{%- else -%}
|
|
|
|
| 102 |
{%- macro is_list_of_outputs(m) -%}
|
| 103 |
{%- if m.content and m.content.0.output is defined -%}1{%- endif -%}
|
| 104 |
{%- endmacro -%}
|
| 105 |
+
{%- macro has_dup_tool_result_id(lo, hi, target) -%}
|
| 106 |
+
{%- set ns_cnt = namespace(n=0) -%}
|
| 107 |
+
{%- for k in range(lo, hi + 1) -%}
|
| 108 |
+
{%- set m = messages[k] -%}
|
| 109 |
+
{%- if is_list_of_outputs(m) -%}
|
| 110 |
+
{%- for entry in m.content -%}
|
| 111 |
+
{%- if id_of(entry) == target -%}
|
| 112 |
+
{%- set ns_cnt.n = ns_cnt.n + 1 -%}
|
| 113 |
+
{%- endif -%}
|
| 114 |
+
{%- endfor -%}
|
| 115 |
+
{%- elif id_of(m) == target -%}
|
| 116 |
+
{%- set ns_cnt.n = ns_cnt.n + 1 -%}
|
| 117 |
+
{%- endif -%}
|
| 118 |
+
{%- if ns_cnt.n > 1 -%}{%- break -%}{%- endif -%}
|
| 119 |
+
{%- endfor -%}
|
| 120 |
+
{%- if ns_cnt.n > 1 -%}1{%- endif -%}
|
| 121 |
+
{%- endmacro -%}
|
| 122 |
+
{%- macro tc_id_exists(tcs, target) -%}
|
| 123 |
+
{%- set ns_f = namespace(found=false) -%}
|
| 124 |
+
{%- for tc in tcs -%}
|
| 125 |
+
{%- if id_of(tc) == target -%}
|
| 126 |
+
{%- set ns_f.found = true -%}
|
| 127 |
+
{%- break -%}
|
| 128 |
+
{%- endif -%}
|
| 129 |
+
{%- endfor -%}
|
| 130 |
+
{%- if ns_f.found -%}1{%- endif -%}
|
| 131 |
+
{%- endmacro -%}
|
| 132 |
{%- set ns = namespace(last_user_index=-1) -%}
|
| 133 |
{%- for m in messages %}
|
| 134 |
{%- if m.role == 'user' %}
|
|
|
|
| 146 |
{%- set reasoning_content = content.split('</think>')[0].split('<think>')[-1] %}
|
| 147 |
{%- set content = content.split('</think>')[-1] %}
|
| 148 |
{%- endif %}
|
| 149 |
+
{%- if (not clear_thinking or loop.index0 > ns.last_user_index) and reasoning_content is defined -%}
|
| 150 |
{{ '<think>' + reasoning_content + '</think>'}}
|
| 151 |
{%- else -%}
|
| 152 |
{{ '<think></think>' }}
|
|
|
|
| 189 |
{%- set eid = id_of(entry) -%}
|
| 190 |
{%- if not eid -%}
|
| 191 |
{%- set ns_chk.can_sort = false -%}
|
| 192 |
+
{%- elif has_dup_tool_result_id(block_start, ns_blk.end, eid) -%}
|
| 193 |
+
{%- set ns_chk.can_sort = false -%}
|
| 194 |
+
{%- elif not tc_id_exists(ns_a.tool_calls, eid) -%}
|
| 195 |
+
{%- set ns_chk.can_sort = false -%}
|
| 196 |
{%- endif -%}
|
| 197 |
{%- endfor -%}
|
| 198 |
{%- else -%}
|
| 199 |
{%- set tk_id = id_of(m) -%}
|
| 200 |
{%- if not tk_id -%}
|
| 201 |
{%- set ns_chk.can_sort = false -%}
|
| 202 |
+
{%- elif has_dup_tool_result_id(block_start, ns_blk.end, tk_id) -%}
|
| 203 |
+
{%- set ns_chk.can_sort = false -%}
|
| 204 |
+
{%- elif not tc_id_exists(ns_a.tool_calls, tk_id) -%}
|
| 205 |
+
{%- set ns_chk.can_sort = false -%}
|
| 206 |
{%- endif -%}
|
| 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 -%}
|
| 213 |
{%- endif -%}
|
| 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 |
{{- 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) -}}
|