Gemma4Test / chat_template.jinja
ConicCat's picture
Upload folder using huggingface_hub
d819a48 verified
{%- macro strip_thinking(text) -%}
{%- set ns = namespace(result='') -%}
{%- for part in text.split('<channel|>') -%}
{%- if '<|channel>' in part -%}
{%- set ns.result = ns.result + part.split('<|channel>')[0] -%}
{%- else -%}
{%- set ns.result = ns.result + part -%}
{%- endif -%}
{%- endfor -%}
{{- ns.result | trim -}}
{%- endmacro -%}
{%- set loop_messages = messages -%} {{ bos_token }}
{#- Handle System Definitions Block -#} {%- if (enable_thinking is defined and enable_thinking) or messages[0]['role'] in ['system', 'developer'] -%}
{{- '<|turn>system\n' -}}
{#- Inject Thinking token at the very top of the FIRST system turn -#}
{%- if enable_thinking is defined and enable_thinking -%}
{{- '<|think|>' -}}
{%- endif -%}
{%- if messages[0]['role'] in ['system', 'developer'] -%}
{{- messages[0]['content'] | trim -}}
{%- set loop_messages = messages[1:] -%}
{%- endif -%}
{{- '<turn|>\n' -}}
{%- endif %}
{#- Loop through messages -#} {%- for message in loop_messages -%}
{%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
{{- '<|turn>' + role + '\n' -}}
{#- Flag to identify the final SFT turn -#}
{%- set is_final_sft_turn = loop.last and not add_generation_prompt -%}
{%- if message['content'] is string -%}
{%- if role == 'model' -%}
{%- if is_final_sft_turn and '<|channel>thought' not in message['content'] -%}
{{- '<|channel>thought\n<channel|>' -}}
{%- endif -%}
{{- strip_thinking(message['content']) -}}
{%- else -%}
{{- message['content'] | trim -}}
{%- endif -%}
{%- elif message['content'] is sequence -%}
{%- set ns = namespace(has_thinking=false) -%}
{%- for item in message['content'] -%}
{%- if item['type'] == 'text' and '<|channel>thought' in item['text'] -%}
{%- set ns.has_thinking = true -%}
{%- endif -%}
{%- endfor -%}
{%- if role == 'model' and is_final_sft_turn and not ns.has_thinking -%}
{{- '<|channel>thought\n<channel|>' -}}
{%- endif -%}
{%- for item in message['content'] -%}
{%- if item['type'] == 'text' -%}
{%- if role == 'model' -%}
{{- strip_thinking(item['text']) -}}
{%- else -%}
{{- item['text'] | trim -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{{- '<turn|>\n' -}}
{%- endfor -%}
{#- Generation Prompt handled as normal (serves as the final turn when true) -#} {%- if add_generation_prompt -%}
{{- '<|turn>model\n' -}}
{%- if not enable_thinking | default(false) -%}
{{- '<|channel>thought\n<channel|>' -}}
{%- endif -%}
{%- endif -%}