Jais-ASR / chat_template.jinja
EvanD's picture amine-khelif's picture guokan-shang's picture
Squash history
1323995
Raw History Blame Contribute Delete
5.36 kB
{%- if language is defined and language is not none -%}
{{- raise_exception("language is a generation-time pin. Pass it to model.generate(), the pipeline, or the API; use language_prefill here to insert a tag.") -}}
{%- endif -%}
{#- ===================================================================================
Jais-ASR — audio transcription chat template.
Optional flags (keyword arguments to apply_chat_template, or
chat_template_kwargs on a server):
diacritics=True -> fully diacritized transcript
code_switch=True -> keep foreign-language spans verbatim
language_prefill="EG" -> prefill the language tag (see the model card)
An explicit system message always wins over the mode flags.
=================================================================================== -#}
{{- bos_token }}
{#- ---- mode: the system message content is the switch -------------------------- -#}
{%- set _m = namespace(modes='') -%}
{%- if code_switch is defined and code_switch -%}
{%- set _m.modes = '[Code-switch]' -%}
{%- endif -%}
{%- if diacritics is defined and diacritics -%}
{%- set _m.modes = (_m.modes ~ ' [Diacritics]') if _m.modes else '[Diacritics]' -%}
{%- endif -%}
{%- set _has_system = messages and messages[0].role == 'system' -%}
{%- if _has_system -%}
{%- if messages[0].content is string -%}
{%- set system_content = messages[0].content -%}
{%- elif messages[0].content -%}
{%- set _sys = namespace(text='') -%}
{%- for part in messages[0].content -%}
{%- if part.type == 'text' -%}
{%- set _sys.text = _sys.text + part.text -%}
{%- endif -%}
{%- endfor -%}
{%- set system_content = _sys.text -%}
{%- else -%}
{%- set system_content = '' -%}
{%- endif -%}
{%- else -%}
{%- set system_content = _m.modes -%}
{%- endif -%}
{%- if system_content -%}
{{- '<|ifm|im_start|>system\n' + system_content + '<|ifm|im_end|>' }}
{%- endif -%}
{#- ---- turns ------------------------------------------------------------------ -#}
{%- for message in messages -%}
{%- if message.content is string -%}
{%- set content = message.content -%}
{%- elif not message.content -%}
{%- set content = '' -%}
{%- else -%}
{#- content parts: {"type": "audio"} renders the audio placeholder,
which the processor expands -#}
{%- set _ns = namespace(text='') -%}
{%- for part in message.content -%}
{%- if part.type == 'audio' -%}
{%- set _ns.text = _ns.text + '<|ifm|audio|>' -%}
{%- elif part.type == 'text' -%}
{%- set _ns.text = _ns.text + part.text -%}
{%- else -%}
{{- raise_exception("audio-only model: unsupported content part type '" ~ part.type ~ "' (use 'audio' or 'text')") -}}
{%- endif -%}
{%- endfor -%}
{%- set content = _ns.text -%}
{%- endif -%}
{%- if message.role == 'system' -%}
{%- if not loop.first -%}
{{- '<|ifm|im_start|>system\n' + content + '<|ifm|im_end|>' }}
{%- endif -%}
{%- elif message.role == 'user' -%}
{{- '<|ifm|im_start|>user\n' + content + '<|ifm|im_end|>' }}
{%- elif message.role == 'assistant' -%}
{%- set thinking = message.reasoning if message.reasoning is defined and message.reasoning is string else '' -%}
{{- '<|ifm|im_start|>assistant' }}
{%- generation -%}
{{- '\n<ifm|think>\n' + thinking + '</ifm|think>' + content }}
{{- '<|ifm|im_end|>' -}}
{%- endgeneration -%}
{%- else -%}
{{- raise_exception("unsupported role '" ~ message.role ~ "' (use system, user or assistant)") -}}
{%- endif -%}
{%- endfor -%}
{#- ---- generation prompt ------------------------------------------------------- -#}
{%- if add_generation_prompt -%}
{%- if enable_thinking is defined and enable_thinking -%}
{%- if language_prefill is defined and language_prefill -%}
{{- raise_exception("language_prefill= prefill is not supported with enable_thinking=True: the prefill cannot follow an open think block. Drop language_prefill= or disable thinking.") -}}
{%- endif -%}
{{- '<|ifm|im_start|>assistant\n<ifm|think>\n' }}
{%- else -%}
{{- '<|ifm|im_start|>assistant\n<ifm|think>\n</ifm|think>' }}
{#- language_prefill= prefills the decision tag into the prompt: a transcript is then
guaranteed and [IFM-NoTranscript] cannot be produced. Unset = the model
decides. To pin a language while keeping abstention, use language.LanguagePin. -#}
{%- if language_prefill is defined and language_prefill -%}
{%- set _tags = ['msa','en','fr','es','it','EG','SA','MA','IQ','JO','SY','QA','DZ','LB','OM','YE','TN','MR','KW','SD','LY','AE','PS','BH'] -%}
{%- if language_prefill not in _tags -%}
{{- raise_exception("language_prefill must be one of " ~ (_tags | join(', ')) ~ " (got '" ~ language_prefill ~ "'). An ISO code such as 'ar' covers several tags and cannot be prefilled; use the LanguagePin logits processor for that.") -}}
{%- endif -%}
{{- '[' ~ language_prefill ~ ']' }}
{%- endif -%}
{%- endif -%}
{%- endif -%}