从 Gemma 4 开始,我们引入了新的控制令牌。对于 Gemma 3 及更低版本, 请参阅之前的文档。
以下部分指定了 Gemma 4 使用的控制令牌及其使用场景。请注意,控制令牌在我们的分词器中是保留的,并且是特定于我们的分词器的。
- 用于指示系统指令的令牌:
system - 用于指示用户轮次的令牌:
user - 用于指示模型轮次的令牌:
model - 用于指示对话轮次开始的令牌:
<|turn> - 用于指示对话轮次结束的令牌:
<turn|>
以下是一个对话示例:
<|turn>system
You are a helpful assistant.<turn|>
<|turn>user
Hello.<turn|>
多模态
| 多模态令牌 | 用途 |
|---|---|
<|image> <image|> |
指示图片嵌入 |
<|audio> <audio|> |
指示音频嵌入 |
<|image|> <|audio|> |
特殊占位符令牌 |
我们使用两个特殊占位符令牌(<|image|> 和 <|audio|>)来指定
应插入图片和音频令牌的位置。分词后,这些令牌会被模型内的实际软嵌入替换。
以下是一个对话示例:
prompt = """<|turn>user
Describe this image: <|image|>
And translate these audio:
a. <|audio|>
b. <|audio|><turn|>
<|turn>model"""
音频
使用以下提示结构进行音频处理:
- 音频语音识别 (ASR)
Transcribe the following speech segment in {LANGUAGE} into {LANGUAGE} text.
Follow these specific instructions for formatting the answer:
* Only output the transcription, with no newlines.
* When transcribing numbers, write the digits, i.e. write 1.7 and not one point seven, and write 3 instead of three.
- 自动语音翻译 (AST)
Transcribe the following speech segment in {SOURCE_LANGUAGE}, then translate it into {TARGET_LANGUAGE}.
When formatting the answer, first output the transcription in {SOURCE_LANGUAGE}, then one newline, then output the string '{TARGET_LANGUAGE}: ', then the translation in {TARGET_LANGUAGE}.
智能体和推理控制令牌
为了支持智能体工作流,Gemma 使用专门的控制令牌来区分内部推理(思考)和外部操作(函数调用)。这些令牌允许模型在提供最终响应或与外部工具互动之前处理复杂的逻辑。
函数调用
Gemma 4 经过六个特殊令牌的训练,用于管理“工具使用”生命周期。
| 令牌对 | 用途 |
|---|---|
<|tool> <tool|> |
定义工具 |
<|tool_call> <tool_call|> |
指示模型使用工具的请求。 |
<|tool_response> <tool_response|> |
将工具的执行结果提供给模型。 |
字符串值的定界符:<|"|>
单个令牌 <|"|> 用作 所有字符串值
结构化数据块中 的分隔符。
- 用途 :此令牌可确保字符串内的任何特殊字符(例如
{、}、,或引号)都被视为字面文本,而不是数据结构底层语法的一部分。 - 用法 :函数声明、调用和
响应中的所有字符串字面量都必须使用此令牌括起来(例如
key:<|"|>string value<|"|>)。
思考模式
如需激活思考模式,请在<|think|>控制令牌中添加
系统指令。
| 控制令牌 | 用途 |
|---|---|
<|think|> |
激活思考模式 |
<|channel> <channel|> |
指示模型的内部流程。 |
以下是一个对话示例:
<|turn>system
<|think|><turn|>
<|turn>user
What is the water formula?<turn|>
<|turn>model
<|channel>thought
...
<channel|>The most common interpretation of "the water formula" refers...<turn|>
思考模式旨在在对话级别启用。这应与其他系统指令(例如工具定义)一起整合到单个系统轮次中。
推理和函数调用示例
在智能体轮次中,模型可能会先私下“思考”,然后再决定调用函数。生命周期遵循以下顺序:
- 用户查询:用户提出问题。
- 内部推理:模型在思考通道中私下思考。
- 工具请求:模型停止生成,以请求工具调用。
- 执行和注入:应用执行工具并附加响应。
- 最终响应:模型读取响应并生成最终答案。
以下示例演示了模型使用天气工具:
<|turn>system
<|think|>You are a helpful assistant.<|tool>declaration:get_current_temperature{...}<tool|><turn|>
<|turn>user
What's the temperature in London?<turn|>
<|turn>model
<|channel>thought
...
<channel|><|tool_call>call:get_current_temperature{location:<|"|>London<|"|>}<tool_call|><|tool_response>
您的应用应解析模型的响应以提取函数名称和实参,执行函数,然后将 tool_calls 和 tool_responses 附加到 assistant 角色下的聊天记录中。
<|turn>model
<|tool_call>call:get_current_weather{location:<|"|>London<|"|>}<tool_call|><|tool_response>response:get_current_weather{temperature:15,weather:<|"|>sunny<|"|>}<tool_response|>
最后,Gemma 会读取工具响应并回复用户。
The temperature in London is 15 degrees and it is sunny.<turn|>
以下是此示例的完整 JSON 聊天记录:
[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What's the temperature in London?"
},
{
"role": "assistant",
"tool_calls": [
{
"function": {
"name": "get_current_weather",
"arguments": {
"location": "London"
}
}
}
],
"tool_responses": [
{
"name": "get_current_weather",
"response": {
"temperature": 15,
"weather": "sunny"
}
}
],
"content": "The temperature in London is 15 degrees and it is sunny."
}
]
管理轮次之间的思考上下文
正确管理模型生成的思考对于在多轮对话中保持性能至关重要。
- 标准多轮对话 :您必须先从上一轮中移除(剥离)模型生成的思考,然后再将对话记录传递回模型以进行下一轮。如果您想在对话中停用思考
模式,可以在剥离
之前的思考时移除
<|think|>令牌。 - 函数调用(例外情况):如果单个模型轮次涉及函数 或工具调用,则不得在函数调用之间移除思考。
智能体工作流和长时间运行的任务
由于原始思考在标准轮次之间会被剥离,因此构建长时间运行的智能体的开发者可能希望保留推理上下文,以防止模型进入循环推理循环。
- 总结思考 :一种强烈推荐的推理技术是提取、总结模型的先前思考,并将其作为标准文本反馈到上下文窗口中。
- 格式限制 :由于 Gemma 4 没有经过明确的训练,使其在提示中包含原始思考(上述特定工具调用场景除外),因此模型对于这些注入的思考没有严格或特定的格式要求。您可以灵活地以最适合您的特定智能体架构的方式来格式化总结的推理。
集成说明
- 内部状态 :
<|channel>和<channel|>令牌通常 用于思维链 (CoT) 处理。在面向用户的标准应用中,此内容通常对最终用户隐藏。 - 工具循环 :
tool_call和tool_response令牌有助于在模型与应用环境之间建立“握手”。应用会拦截tool_call,执行底层代码,并在tool_response令牌中将结果反馈给模型。 - 模型行为 :即使明确关闭思考模式,较大的模型(例如 gemma-4-26B-A4B-it、gemma-4-31B-it)有时也可能会生成思考通道。为了在这些极端情况下稳定模型行为,请考虑向提示添加空思考令牌。
提示:使用无思考数据集微调大型模型
使用不包含思考的数据集微调较大的 Gemma 模型时,您可以通过向训练提示添加空通道来获得更好的结果:
<|turn>model
<|channel>thought
<channel|>
提示:使用系统指令实现自适应思考效率
虽然 Gemma 4 中的“思考”正式支持作为 ON 或 OFF 布尔值功能,但该模型具有非常强大的指令遵循能力,可让您动态调整其思考行为。
您可以使用系统指令 (SI) 将模型引导到减少思考模式,而不是依赖于硬编码的框架参数来表示“高”或“低”思考。通过明确指示模型高效思考或以较低的深度思考(我们称之为“LOW”思考指令),您可以实现自适应思考效率。
- 降低成本 :测试表明,应用“LOW”思考系统指令可将生成的思考令牌数量减少约 20%。
- 概念验证 :由于此行为是模型可指导性的副产品,而不是经过专门训练的,因此没有单一的“完美”提示。“LOW”指令是一种概念验证。
- 自定义 :我们强烈建议开发者尝试使用自己的自定义系统指令。您可以微调模型思考过程的深度、长度和样式,以便为您的特定使用场景完美平衡延迟时间、成本和输出质量。