Gemini 2.5模型可以使用内部“思考过程”,显著提升推理和多步规划能力,从而高效处理编码、高等数学和数据分析等复杂任务。
您可以使用思考预算来配置模型可以进行的思考量。如果降低延迟时间或成本是首要任务,此配置尤为重要。此外,您还可以查看任务难度比较,以确定模型可能需要多少思维能力。
使用思维模型
像使用任何其他 Gemini 模型一样使用思维模型(初始化所选的 Gemini API 提供程序、创建 GenerativeModel
实例等)。这些模型可用于文本或代码生成任务,例如生成结构化输出或分析多模态输入(例如图片、视频、音频或 PDF)。
您甚至可以在流式传输输出时使用思维模型。
支持的模型
只有 Gemini 2.5 型号支持此功能。
gemini-2.5-pro
gemini-2.5-flash
gemini-2.5-flash-lite-preview-06-17
使用思维模型的最佳实践
建议您在 Google AI Studio 或 Vertex AI Studio 中测试提示,以便查看完整的思考过程。您可以找出模型可能出错的任何方面,以便改进提示,从而获得更一致、更准确的回答。
首先,使用描述预期结果的通用提示,然后观察模型在确定回答时最初的想法。如果回答不尽如人意,请使用以下任一提示技巧来帮助模型生成更好的回答:
- 提供分步说明
- 提供多个输入-输出对示例
- 提供有关输出和回答应如何措辞和设置格式的指南
- 提供具体的验证步骤
除了提示之外,您还可以考虑使用以下建议:
设置系统指令,类似于您在模型接触到来自提示或最终用户的任何进一步指令之前添加的“序言”。它们可让您根据自己的特定需求和使用情形来控制模型的行为。
设置思考预算,以配置模型可进行的思考量。如果您设置的预算较低,模型就不会“过度思考”其回答。如果您设置了较高的预算,模型就可以在需要时进行更多思考。设置思考预算还可以为实际回答预留更多总 token 输出限额。
(如果使用 Vertex AI Gemini API)请务必在 Firebase 控制台中启用 AI 监控,以便监控已启用思考功能的请求的延迟时间。请注意,思考标记尚未显示在监控信息中心内。
控制思考预算
如需控制模型在生成回答时可进行的思考量,您可以指定允许其使用的思考预算 token 数量。
在您可能需要比默认思考预算更多或更少 token 的情况下,您可以手动设置思考预算。如需详细了解任务复杂程度和建议预算,请参阅本部分后面的内容。以下是一些简要指南:
- 如果延迟时间很重要,或者任务不太复杂,请设置较低的思考预算
- 为更复杂的任务设置较高的思考预算
设置思考预算
点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
在创建 GenerativeModel
实例时,在 GenerationConfig
中设置思考预算。该配置在实例的整个生命周期内保持不变。如果您想为不同的请求使用不同的思考预算,请创建配置了相应预算的 GenerativeModel
实例。
如需了解支持的思考预算值,请参阅本部分后面的内容。
Swift
在创建 GenerativeModel
实例时,在 GenerationConfig
中设置思考预算。
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
let generationConfig = GenerationConfig(
thinkingConfig: ThinkingConfig(thinkingBudget: 1024)
)
// Specify the config as part of creating the `GenerativeModel` instance
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
modelName: "GEMINI_MODEL_NAME",
generationConfig: generationConfig
)
// ...
Kotlin
在创建 GenerativeModel
实例时,设置 GenerationConfig
中参数的值。
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
val generationConfig = generationConfig {
thinkingConfig = thinkingConfig {
thinkingBudget = 1024
}
}
// Specify the config as part of creating the `GenerativeModel` instance
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "GEMINI_MODEL_NAME",
generationConfig,
)
// ...
Java
在创建 GenerativeModel
实例时,设置 GenerationConfig
中参数的值。
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
.setThinkingBudget(1024)
.build();
GenerationConfig generationConfig = GenerationConfig.builder()
.setThinkingConfig(thinkingConfig)
.build();
// Specify the config as part of creating the `GenerativeModel` instance
GenerativeModelFutures model = GenerativeModelFutures.from(
FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel(
/* modelName */ "<var>GEMINI_MODEL_NAME</var>",
/* generationConfig */ generationConfig
);
);
// ...
Web
在创建 GenerativeModel
实例时,设置 GenerationConfig
中参数的值。
// ...
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
const generationConfig = {
thinkingConfig: {
thinkingBudget: 1024
}
};
// Specify the config as part of creating the `GenerativeModel` instance
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });
// ...
Dart
Flutter 的下一个版本将支持设置思考预算。
Unity
Unity 将在下一个版本中支持设置思考预算。
支持的思考预算值
下表列出了您可以为每个模型设置的思考预算值,方法是配置模型的 thinkingBudget
。
型号 | 默认值 | 思考预算的可用范围 |
用于 停用思考的值 |
价值: 培养动态思维 |
|
---|---|---|---|---|---|
最小值 | 最大值 | ||||
Gemini 2.5 Pro | 8,192 |
128 |
32,768 |
无法关闭 | -1 |
Gemini 2.5 Flash | 8,192 |
1 |
24,576 |
0 |
-1 |
Gemini 2.5 Flash‑Lite | 0 (默认情况下,思考处于停用状态) |
512 |
24,576 |
0 (或完全不配置思考预算) |
-1 |
停用思考
对于某些较简单的任务,无需思考能力,传统推理就足够了。或者,如果缩短延迟时间是首要任务,您可能不希望模型花费不必要的时间来生成回答。
在这些情况下,您可以停用(或关闭)思考:
- Gemini 2.5 Pro:思考功能无法停用
- Gemini 2.5 Flash:将
thinkingBudget
设置为0
个 token - Gemini 2.5 Flash-Lite:默认情况下,思考功能处于停用状态
培养动态思维
您可以将 thinkingBudget
设置为 -1
,让模型自行决定思考的时间和程度(称为动态思考)。模型可以使用其认为合适的任意数量的 token,但不得超过上述最大 token 值。
任务复杂程度
简单任务 - 无需思考
不需要复杂推理的简单请求,例如事实检索或分类。示例:- “DeepMind 是在哪里成立的?”
- “这封电子邮件是要求安排会议,还是仅提供信息?”
中等任务 - 需要默认预算或一些额外的思考预算
需要一定程度的逐步处理或更深入理解的常见请求。示例:- “将光合作用与成长进行类比。”
- “比较和对比电动汽车与混合动力汽车。”
困难任务 - 可能需要最大思考预算
真正复杂的挑战,例如解决复杂的数学问题或编码任务。这类任务要求模型充分发挥推理和规划能力,通常需要经过许多内部步骤才能提供答案。示例:- “解决 2025 年 AIME 中的问题 1:求出所有整数底数 b > 9 的总和,使得 17b 是 97b 的除数。”
- “编写一个 Python Web 应用,用于直观呈现实时股市数据,包括用户身份验证。尽可能提高效率。”
价格和思考 token 的计数方式
思考令牌与文本输出令牌使用相同的价格。
您可以从回答的 usageMetadata
属性中的 thoughtsTokenCount
字段获取思考 token 的总数:
Swift
// ...
let response = try await model.generateContent("Why is the sky blue?")
if let usageMetadata = response.usageMetadata {
print("Thoughts Token Count: \(usageMetadata.thoughtsTokenCount)")
}
Kotlin
// ...
val response = model.generateContent("Why is the sky blue?")
response.usageMetadata?.let { usageMetadata ->
println("Thoughts Token Count: ${usageMetadata.thoughtsTokenCount}")
}
Java
// ...
ListenableFuture<GenerateContentResponse> response =
model.generateContent("Why is the sky blue?");
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String usageMetadata = result.getUsageMetadata();
if (usageMetadata != null) {
System.out.println("Thoughts Token Count: " +
usageMetadata.getThoughtsTokenCount());
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
// ...
const response = await model.generateContent("Why is the sky blue?");
if (response?.usageMetadata?.thoughtsTokenCount != null) {
console.log(`Thoughts Token Count: ${response.usageMetadata.thoughtsTokenCount}`);
}
Dart
Flutter 将在下一个版本中支持计算思考词元。
Unity
Unity 将在下一个版本中支持计算思考令牌。
如需详细了解令牌,请参阅令牌计数指南。