{%- macro render_content(content) -%}
{%- if content is string -%}
{{- content -}}
{%- else -%}
{%- set ns = namespace(has_audio=false, text=none) -%}
{%- set keyword_namespace = namespace(items=[]) -%}
{%- for item in content -%}
{%- if item.type == 'audio' or 'audio' in item or 'audio_url' in item -%}
{{- '<|audio_start|><|audio_pad|><|audio_end|>
' -}}
{%- set ns.has_audio = true -%}
{%- elif item.type == 'text' and ns.text is none -%}
{%- set ns.text = item.text -%}
{%- elif item.type == 'keywords' -%}
{%- set keyword_namespace.items = keyword_namespace.items + item.keywords -%}
{%- endif -%}
{%- endfor -%}
{%- if ns.has_audio -%}
{%- if ns.text -%}
{{- '补充信息:' + ns.text + '
' -}}
{%- endif -%}
{%- if keyword_namespace.items -%}
{{- '热词列表:[' + keyword_namespace.items|join(', ') + ']
' -}}
{%- endif -%}
{{- '请将音频转写为文本,每一段需以起始时间戳和说话人编号([S01]、[S02]、[S03]…)开头,正文为对应的语音内容,并在段末标注结束时间戳,以清晰标明该段语音范围。' -}}
{%- elif ns.text -%}
{{- ns.text -}}
{%- endif -%}
{%- endif -%}
{%- endmacro -%}
{%- if tools %}
{{- '<|im_start|>system
' }}
{%- if messages[0].role == 'system' %}
{{- render_content(messages[0].content) + '
' }}
{%- else %}
{{- 'You are a helpful assistant.
' }}
{%- endif %}
{{- "# Tools
You may call one or more functions to assist with the user query.
You are provided with function signatures within XML tags:
" }}
{%- for tool in tools %}
{{- "
" }}
{{- tool | tojson }}
{%- endfor %}
{{- "
For each function call, return a json object with function name and arguments within XML tags:
{\"name\": , \"arguments\": }
<|im_end|>
" }}
{%- else %}
{%- if messages[0].role == 'system' %}
{{- '<|im_start|>system
' + render_content(messages[0].content) + '<|im_end|>
' }}
{%- else %}
{{- '<|im_start|>system
You are a helpful assistant.<|im_end|>
' }}
{%- endif %}
{%- endif %}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
{%- for message in messages[::-1] %}
{%- set index = (messages|length - 1) - loop.index0 %}
{%- set content = render_content(message.content) %}
{%- if ns.multi_step_tool and message.role == "user" and content is string and not(content.startswith('') and content.endswith('')) %}
{%- set ns.multi_step_tool = false %}
{%- set ns.last_query_index = index %}
{%- endif %}
{%- endfor %}
{%- for message in messages %}
{%- set content = render_content(message.content) %}
{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
{{- '<|im_start|>' + message.role + '
' + content + '<|im_end|>
' }}
{%- elif message.role == "assistant" %}
{%- set reasoning_content = '' %}
{%- if message.reasoning_content is string %}
{%- set reasoning_content = message.reasoning_content %}
{%- else %}
{%- if '' in content %}
{%- set reasoning_content = content.split('')[0].rstrip('
').split('')[-1].lstrip('
') %}
{%- set content = content.split('')[-1].lstrip('
') %}
{%- endif %}
{%- endif %}
{{- '<|im_start|>' + message.role + '
' }}
{%- generation %}
{%- if loop.index0 > ns.last_query_index %}
{%- if loop.last or (not loop.last and reasoning_content) %}
{{- '
' + reasoning_content.strip('
') + '
' + content.lstrip('
') }}
{%- else %}
{{- content }}
{%- endif %}
{%- else %}
{{- content }}
{%- endif %}
{%- if message.tool_calls %}
{%- for tool_call in message.tool_calls %}
{%- if (loop.first and content) or (not loop.first) %}
{{- '
' }}
{%- endif %}
{%- if tool_call.function %}
{%- set tool_call = tool_call.function %}
{%- endif %}
{{- '
{"name": "' }}
{{- tool_call.name }}
{{- '", "arguments": ' }}
{%- if tool_call.arguments is string %}
{{- tool_call.arguments }}
{%- else %}
{{- tool_call.arguments | tojson }}
{%- endif %}
{{- '}
' }}
{%- endfor %}
{%- endif %}
{{- '<|im_end|>
' }}
{%- endgeneration %}
{%- elif message.role == "tool" %}
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
{{- '<|im_start|>user' }}
{%- endif %}
{{- '
' }}
{{- content }}
{{- '
' }}
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
{{- '<|im_end|>
' }}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant
' }}
{%- if enable_thinking is defined and enable_thinking is false %}
{{- '
' }}
{%- endif %}
{%- endif %}