Qwen3内置提示词模板解读
提示词模版:tokenizer_config.json
1. 工具调用(Function Calling)支持部分
{%- 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..." }}...
- 如果传入了 tools(即 function calling 的函数签名),会优先构造 <|im_start|>system 开头的一段系统提示,告诉模型可以调用工具。
- 这段提示包含:
- # Tools 开头的说明文字;
- tools 列表,每个工具(函数)都通过 tojson 转换为 JSON;
- 如何使用 <tool_call> 标签返回工具调用的结果。
2. 系统消息处理
{%- if messages[0].role == 'system' %}{{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
{%- endif %}
如果首条消息是 system,则会作为系统设定(system prompt)处理,加上 <|im_start|>system\n … <|im_end|>\n
3. 多轮消息回显处理
{%- for message in messages %}{%- if (message.role == "user") ... %}{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
- 针对用户(user)、助手(assistant)、工具响应(tool)等不同角色进行处理。
- 使用 <|im_start|>role\n…<|im_end|> 包裹每一轮对话。
4. Assistant 角色的特殊处理(含推理内容)
{%- if message.role == "assistant" %}...<think>\n...reasoning_content...\n</think>
- 若助手消息中包含 内容,会将其拆分为“推理部分”和“回复正文”。
- 如果存在 tool_calls,还会附加一段 <tool_call> JSON 标签。
5. 工具响应处理(role = tool)
<tool_response>\n...内容...\n</tool_response>
- 模型回复 <tool_call> 后,会给出 <tool_response>。
- 这部分内容会包在 user role 内部,以 <tool_response> 标签封装,用来模拟用户获得工具调用结果。
6. 混合推理模式开启方法
{%- if add_generation_prompt %}{{- '<|im_start|>assistant\n' }}{%- if enable_thinking is defined and enable_thinking is false %}{{- '<think>\n\n</think>\n\n' }}{%- endif %}
{%- endif %}
- 如果需要生成下一轮回复,会在最后加上 <|im_start|>assistant\n 作为提示。
- 还可以通过设置 enable_thinking=false,强制加上 占位符。