在人工智能迅速发展的今天,大型语言模型(Large Language Models,简称LLM)正逐渐成为各类应用的核心组件。其中,对话模板(Chat Template)作为连接用户与模型的关键桥梁,对确保高质量的交互体验至关重要。本文将深入解析Qwen3使用的对话模板,揭示其如何优雅地处理多轮对话、工具调用等复杂场景。
Qwen3的chat_template
是一个精心设计的Jinja模板,其主要职责是将来自不同角色(用户、系统、助手、工具)的消息组织成结构化格式,尤其适用于Function Calling(函数调用)场景。这种模板不仅确保了信息的有序传递,还为模型提供了明确的上下文理解框架。
chat_template
"chat_template": "{%- if tools %}\n {{- '<|im_start|>system\\n' }}\n {%- if messages[0].role == 'system' %}\n {{- messages[0].content + '\\n\\n' }}\n {%- endif %}\n {{- \"# Tools\\n\\nYou may call one or more functions to assist with the user query.\\n\\nYou are provided with function signatures within XML tags:\\n\" }}\n {%- for tool in tools %}\n {{- \"\\n\" }}\n {{- tool | tojson }}\n {%- endfor %}\n {{- \"\\n \\n\\nFor each function call, return a json object with function name and arguments within XML tags:\\n\\n{\\\"name\\\": , \\\"arguments\\\": }\\n <|im_end|>\\n\" }}\n{%- else %}\n {%- if messages[0].role == 'system' %}\n {{- '<|im_start|>system\\n' + messages[0].content + '<|im_end|>\\n' }}\n {%- endif %}\n{%- endif %}\n{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}\n{%- for message in messages[::-1] %}\n {%- set index = (messages|length - 1) - loop.index0 %}\n {%- if ns.multi_step_tool and message.role == \"user\" and not(message.content.startswith('') and message.content.endswith(' ')) %}\n {%- set ns.multi_step_tool = false %}\n {%- set ns.last_query_index = index %}\n {%- endif %}\n{%- endfor %}\n{%- for message in messages %}\n {%- if (message.role == \"user\") or (message.role == \"system\" and not loop.first) %}\n {{- '<|im_start|>' + message.role + '\\n' + message.content + '<|im_end|>' + '\\n' }}\n {%- elif message.role == \"assistant\" %}\n {%- set content = message.content %}\n {%- set reasoning_content = '' %}\n {%- if message.reasoning_content is defined and message.reasoning_content is not none %}\n {%- set reasoning_content = message.reasoning_content %}\n {%- else %}\n {%- if '' in message.content %}\n {%- set content = message.content.split('')[-1].lstrip('\\n') %}\n {%- set reasoning_content = message.content.split('')[0].rstrip('\\n').split('')[-1].lstrip('\\n') %}\n {%- endif %}\n {%- endif %}\n {%- if loop.index0 > ns.last_query_index %}\n {%- if loop.last or (not loop.last and reasoning_content) %}\n {{- '<|im_start|>' + message.role + '\\n\\n' + reasoning_content.strip('\\n') + '\\n \\n\\n' + content.lstrip('\\n') }}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content }}\n {%- endif %}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content }}\n {%- endif %}\n {%- if message.tool_calls %}\n {%- for tool_call in message.tool_calls %}\n {%- if (loop.first and content) or (not loop.first) %}\n {{- '\\n' }}\n {%- endif %}\n {%- if tool_call.function %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '\\n{\"name\": \"' }}\n {{- tool_call.name }}\n {{- '\", \"arguments\": ' }}\n {%- if tool_call.arguments is string %}\n {{- tool_call.arguments }}\n {%- else %}\n {{- tool_call.arguments | tojson }}\n {%- endif %}\n {{- '}\\n ' }}\n {%- endfor %}\n {%- endif %}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == \"tool\" %}\n {%- if loop.first or (messages[loop.index0 - 1].role != \"tool\") %}\n {{- '<|im_start|>user' }}\n {%- endif %}\n {{- '\\n\\n' }}\n {{- message.content }}\n {{- '\\n ' }}\n {%- if loop.last or (messages[loop.index0 + 1].role != \"tool\") %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n {%- if enable_thinking is defined and enable_thinking is false %}\n {{- '\\n\\n \\n\\n' }}\n {%- endif %}\n{%- endif %}",
{%- if tools %}
{{- '<|im_start|>system\n' }}
{%- if messages[0].role 'system' %}
{{- messages[0].content + '\n\n' }}
{%- endif %}
{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\n..." }}
...
{%- else %}
{%- if messages[0].role 'system' %}
{{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
这段代码首先判断是否存在可用工具。若有,则:
<|im_start|>system
标记开始系统指令...
包装所有工具定义若无工具可用,则仅渲染基础系统提示。这一设计确保了模型在交互伊始就明确自身能力边界,为后续智能响应奠定基础。
{%- 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 %}
{%- if ns.multi_step_tool and message.role == "user" and not(message.content.startswith('') and message.content.endswith(' ')) %}
{%- set ns.multi_step_tool = false %}
{%- set ns.last_query_index = index %}
{%- endif %}
{%- endfor %}
这段代码巧妙地解决了多轮对话中的一个核心问题:如何区分真实用户提问与系统生成的工具响应。通过逆序遍历消息历史,模板能够精确定位最后一个真实用户提问的位置,为后续的思考过程和响应生成提供准确参照。
这种设计特别适合复杂的多轮交互场景,例如当模型需要连续调用多个工具来解决单一问题时。
{%- for message in messages %}
{%- if (message.role "user") or (message.role "system" and not loop.first) %}
{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
{%- elif message.role == "assistant" %}
...
{%- elif message.role == "tool" %}
...
{%- endif %}
{%- endfor %}
在这一环节中,模板按照时间顺序处理所有消息,针对不同角色采用差异化处理策略:
助手消息处理是模板的一大亮点,它支持:
...
标签分离推理过程与最终回复
格式规范这种设计使得模型能够展示清晰的推理链(Chain-of-Thought),既增强了透明度,也提升了回复质量。
<|im_start|>user
工具返回内容
<|im_end|>
工具消息被包装为特定格式,确保模型能够正确识别并处理工具执行结果。连续的工具响应会被智能合并,避免冗余标记影响模型理解。
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n' }}
{%- if enable_thinking is defined and enable_thinking is false %}
{{- '\n\n \n\n' }}
{%- endif %}
{%- endif %}
模板最后部分为响应生成提供引导,支持:
Qwen3对话模板的技术亮点主要体现在:
标签实现推理过程与最终回复的清晰分离这些特性使得Qwen3对话模板特别适合以下应用场景: