思考型

所有最新的 Gemini 模型都使用内部“思考过程”,可显著提升其推理和多步规划能力,使其在编码、高等数学和数据分析等复杂任务中表现出色。

思考模型提供以下配置和选项:

  • 控制思考量
    您可以配置模型可以进行的“思考”量。如果降低延迟时间或成本是首要考虑因素,此配置尤为重要。此外,请查看任务难度比较,以确定模型可能需要多强的思考能力。

    您可以使用思考水平 Gemini 3.x 及更高版本的模型)思考预算 Gemini 2.5 模型)来控制此配置。

  • 获取思路总结
    您可以启用思路总结,以便在生成的回答中包含思路总结。这些摘要是模型原始想法的合成版本,可帮助您深入了解模型的内部推理过程。

  • 处理思路签名
    Firebase AI Logic SDK 会自动为您处理思路签名,从而确保模型能够访问之前轮次的思路上下文,尤其是在使用函数调用时。

请务必查看使用思维模型的最佳实践和提示指南



使用思考模型

使用思考模型的方式与使用任何其他 Gemini 模型一样。

如需充分利用思维模型,请查看本页稍后介绍的使用思维模型的最佳实践和提示指南

支持此功能的模型

Aware 2 和 Aware 1 Gemini都支持此功能。

  • gemini-3.1-pro-preview
  • gemini-3.7-flash(以及更旧的 gemini-3.6-flashgemini-3.5-flash
  • gemini-3.5-flash-lite(以及较旧的 gemini-3.1-flash-lite
  • gemini-3-pro-image(又称“Nano Banana Pro”)
  • gemini-3.1-flash-image(又称“Nano Banana 2”)
  • gemini-3.1-flash-lite-image(又称“Nano Banana 2 Lite”)
  • gemini-2.5-pro
  • gemini-2.5-flash
  • gemini-2.5-flash-lite

使用思维模型的最佳实践和提示指南

建议您在 Google AI StudioAgent  Studio 中测试提示,以便查看完整的思考过程。您可以找出模型可能出错的任何方面,以便优化提示,从而获得更一致、更准确的回答。

首先提供一个描述预期结果的一般提示,然后观察模型在确定回答时最初的思考过程。如果回答不尽如人意,请使用以下任一提示技巧来帮助模型生成更好的回答:

  • 提供分步说明
  • 提供多个输入-输出对示例
  • 提供有关输出和回答应如何措辞和设置格式的指导
  • 提供具体的验证步骤

除了提示之外,您还可以考虑使用以下建议:

  • 设置系统指令,该指令类似于一段“序言”,在模型接收到提示或最终用户的任何进一步指令之前添加,它们可让您根据自己的特定需求和使用情形来控制模型的行为。

  • 设置思考等级(或 Gemini 2.5 模型的思考预算),以控制模型的思考量。如果将思考等级设置为高,模型就可以根据需要进行更多思考。如果您将该值设置得较低,模型就不会“过度思考”其回答,并且还会为实际回答预留更多总 token 输出限制,从而有助于减少延迟时间和费用。

  • Firebase 控制台中启用 AI 监控,以监控已启用思考功能的请求的思考词元数和延迟时间。如果您已启用思路总结,它们将显示在控制台中,您可以在其中检查模型的详细推理过程,以便调试和优化提示。



控制思考量

您可以配置模型在返回回答之前可以进行多少“思考”和推理。如果降低延迟时间或成本是首要考虑因素,此配置就显得尤为重要。

请务必查看任务难度比较,以确定模型可能需要多少思维能力。以下是一些简要指南:

  • 如果任务不太复杂,或者您优先考虑缩短延迟时间或降低费用,请设置较低的思考值。
  • 对于更复杂的任务,请设置更高的思考值。

您可以使用思考水平 Gemini 3.x 及更高版本的模型)思考预算 Gemini 2.5 模型)来控制此配置。

思考等级Gemini 3.x 及更高版本的模型)

如需控制 Gemini 3.x 及更高版本的模型在生成回答时可进行的思考量,您可以指定思考级别,以确定模型可使用的思考 token 数量。

设置思考等级

点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。

在创建 GenerativeModel 实例的过程中,在 GenerationConfig 中设置思考等级。touchesEnabled 配置在实例的整个生命周期内保持不变。如果您想针对不同的请求使用不同的思维水平,请创建配置了每个水平的 GenerativeModel 实例。

如需了解思维水平支持的值,请参阅本部分后面的内容。

Swift

在创建 GenerativeModel 实例时,在 GenerationConfig 中设置思考等级。


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(thinkingLevel: .low)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_3.x_MODEL_NAME",
  generationConfig: generationConfig
)

// ...

Kotlin

在创建 GenerativeModel 实例时,设置 GenerationConfig 中参数的值。


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      thinkingLevel = ThinkingLevel.LOW
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_3.x_MODEL_NAME",
  generationConfig,
)

// ...

Java

在创建 GenerativeModel 实例时,设置 GenerationConfig 中参数的值。


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setThinkingLevel(ThinkingLevel.LOW)
    .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 */ "GEMINI_3.x_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// ...

Web

在创建 GenerativeModel 实例时,设置 GenerationConfig 中参数的值。


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
const generationConfig = {
  thinkingConfig: {
    thinkingLevel: ThinkingLevel.LOW
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_3.x_MODEL_NAME", generationConfig });

// ...

Dart

在创建 GenerativeModel 实例时,设置 GenerationConfig 中参数的值。


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
final thinkingConfig = ThinkingConfig.withThinkingLevel(ThinkingLevel.low);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_3.x_MODEL_NAME',
  config: generationConfig,
);

// ...

Unity

在创建 GenerativeModel 实例时,设置 GenerationConfig 中参数的值。


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
var thinkingConfig = new ThinkingConfig(thinkingLevel: ThinkingLevel.Low);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_3.x_MODEL_NAME",
  generationConfig: generationConfig
);

// ...

支持的思考等级值

下表列出了您可以为每个模型设置的思维水平值,方法是配置模型的 thinkingLevel

MINIMAL LOW MEDIUM HIGH

模型使用的 token 数量尽可能少;几乎不进行思考

低复杂度任务

模型使用的令牌数量更少;可最大限度地缩短延迟时间并降低费用

简单任务和高吞吐量任务

模型采用均衡的方法

中等复杂度的任务

模型使用的 token 数量达到上限

需要深度推理的复杂提示

Gemini 3.x Pro
gemini-3.1-pro-preview
(默认)
Gemini 3.x Flash
gemini-3.7-flash
gemini-3.6-flash
gemini-3.5-flash

不受 gemini-3.7-flash 1 支持
(默认)
Gemini 3.x Flash‑Lite
gemini-3.5-flash-lite
gemini-3.1-flash-lite
(默认)
Gemini 3.x Pro Image
gemini-3-pro-image
(“Nano Banana Pro”)
(默认)
Gemini 3.x Flash Image
gemini-3.1-flash-image
("Nano Banana 2")
(默认)
Gemini 3.x Flash‑Lite Image
gemini-3.1-flash-lite-image
("Nano Banana 2 Lite")
(默认)

1 gemini-3.7-flash 支持 MINIMAL 思考等级。如果您将思考等级设为 MINIMAL,请求将失败并显示 400 错误。



思考预算Gemini 2.5 个模型)

如需控制 Gemini 2.5 模型在生成回答时可进行的思考量,您可以指定思考预算,以限制模型可使用的思考 token 数量。

设置思考预算

点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。

GenerationConfig 中设置思考预算,作为为 Gemini 2.5 模型创建 GenerativeModel 实例的一部分。该配置在实例的整个生命周期内保持不变。如果您想为不同的请求使用不同的思考预算,请创建配置了每个预算的 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_2.5_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_2.5_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 */ "GEMINI_2.5_MODEL_NAME",
                  /* 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_2.5_MODEL_NAME", generationConfig });

// ...

Dart

在创建 GenerativeModel 实例时,设置 GenerationConfig 中参数的值。


// ...

// Set the thinking configuration.
// Use a thinking budget value appropriate for your model (example value shown here).
final thinkingConfig = ThinkingConfig.withThinkingBudget(1024);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_2.5_MODEL_NAME',
  config: generationConfig,
);

// ...

Unity

在创建 GenerativeModel 实例时,设置 GenerationConfig 中参数的值。


// ...

// Set the thinking configuration.
// Use a thinking budget value appropriate for your model (example value shown here).
var thinkingConfig = new ThinkingConfig(thinkingBudget: 1024);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_2.5_MODEL_NAME",
  generationConfig: generationConfig
);

// ...

支持的思考预算值

下表列出了您可以为每个模型设置的思考预算值,方法是配置模型的 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



所有思维模型对应的任务复杂程度

  • 简单任务 - 无需过多思考
    不需要复杂推理的简单请求,例如事实检索或分类。示例:

    • “DeepMind 是在哪里成立的?”
    • “这封电子邮件是要求安排会议,还是仅提供信息?”
  • 中等任务 - 可能需要进行一些思考
    常见请求,需要一定程度的逐步处理或更深入的理解。示例:

    • “将光合作用和成长进行类比。”
    • “比较和对比电动汽车与混合动力汽车。”
  • 困难任务 - 可能需要进行最大程度的思考
    真正复杂的挑战,例如解决复杂的数学问题或编码任务。这类任务需要模型充分发挥推理和规划能力,通常需要经过许多内部步骤才能提供答案。示例:

    • “解决 2025 年 AIME 中的问题 1:求出所有整数基数 b > 9 的总和,使得 17b 是 97b 的除数。”
    • “编写一个 Python Web 应用的代码,该应用可直观呈现实时股市数据,包括用户身份验证。尽可能提高效率。”



思考摘要

思考总结是模型原始思考的合成版本,可提供对模型内部推理过程的洞见。

以下是回答中包含思路总结的一些原因:

  • 您可以在应用的界面中显示思维总结,也可以让用户访问这些总结。思维总结会作为响应中的单独部分返回,以便您更好地控制如何在应用中使用它。

  • 如果您还Firebase 控制台中启用 AI 监控,系统会在控制台中显示思路总结,您可以在其中检查模型的详细推理过程,以便调试和优化提示。

以下是有关思维总结的一些重要说明:

  • 想法总结不受思考预算的控制(预算适用于模型的原始想法)。不过,如果停用思考功能,模型就不会返回思考总结。

  • 思考总结被视为模型生成的常规文本回答的一部分,并计为输出 token。

启用思考总结

点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。

您可以在模型配置中将 includeThoughts 设置为 true,以启用思路总结。然后,您可以通过检查响应中的 thoughtSummary 字段来访问摘要。

以下示例展示了如何启用并检索包含在响应中的思路总结:

Swift

Aware 实例创建过程中,在 GenerationConfig 中启用思路总结。GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(includeThoughts: true)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
)

let response = try await model.generateContent("solve x^2 + 4x + 4 = 0")

// Handle the response that includes thought summaries.
if let thoughtSummary = response.thoughtSummary {
  print("Thought Summary: \(thoughtSummary)")
}
guard let text = response.text else {
  fatalError("No text in response.")
}
print("Answer: \(text)")

Kotlin

Aware 实例创建过程中,在 GenerationConfig 中启用思路总结。GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      includeThoughts = true
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_MODEL_NAME",
  generationConfig,
)

val response = model.generateContent("solve x^2 + 4x + 4 = 0")

// Handle the response that includes thought summaries.
response.thoughtSummary?.let {
    println("Thought Summary: $it")
}
response.text?.let {
    println("Answer: $it")
}

Java

在创建 GenerativeModel 实例时,在 GenerationConfig 中启用思路总结。


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setIncludeThoughts(true)
    .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 */ "GEMINI_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// Handle the response that includes thought summaries.
ListenableFuture responseFuture = model.generateContent("solve x^2 + 4x + 4 = 0");
Futures.addCallback(responseFuture, new FutureCallback() {
    @Override
    public void onSuccess(GenerateContentResponse response) {
        if (response.getThoughtSummary() != null) {
            System.out.println("Thought Summary: " + response.getThoughtSummary());
        }
        if (response.getText() != null) {
            System.out.println("Answer: " + response.getText());
        }
    }

    @Override
    public void onFailure(Throwable t) {
        // Handle error
    }
}, MoreExecutors.directExecutor());

Web

Aware 实例创建过程中,在 GenerationConfig 中启用思路总结。GenerativeModel


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
const generationConfig = {
  thinkingConfig: {
    includeThoughts: true
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });

const result = await model.generateContent("solve x^2 + 4x + 4 = 0");
const response = result.response;

// Handle the response that includes thought summaries.
if (response.thoughtSummary()) {
    console.log(`Thought Summary: ${response.thoughtSummary()}`);
}
const text = response.text();
console.log(`Answer: ${text}`);

Dart

在创建 GenerativeModel 实例时,在 GenerationConfig 中启用思维总结。


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
final thinkingConfig = ThinkingConfig(includeThoughts: true);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_MODEL_NAME',
  generationConfig: generationConfig,
);

final response = await model.generateContent('solve x^2 + 4x + 4 = 0');

// Handle the response that includes thought summaries.
if (response.thoughtSummary != null) {
  print('Thought Summary: ${response.thoughtSummary}');
}
if (response.text != null) {
  print('Answer: ${response.text}');
}

Unity

在创建 GenerativeModel 实例时,在 GenerationConfig 中启用思路总结。


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
var thinkingConfig = new ThinkingConfig(includeThoughts: true);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
);

var response = await model.GenerateContentAsync("solve x^2 + 4x + 4 = 0");

// Handle the response that includes thought summaries.
if (response.ThoughtSummary != null) {
    Debug.Log($"Thought Summary: {response.ThoughtSummary}");
}
if (response.Text != null) {
    Debug.Log($"Answer: {response.Text}");
}

流式传输思考总结

如果您选择使用 generateContentStream 对回答进行流式传输,还可以查看思路总结。这将在生成回答期间返回滚动增量摘要。

Swift

在创建 GenerativeModel 实例时,在 GenerationConfig 中启用思路总结。


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(includeThoughts: true)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
)

let stream = try model.generateContentStream("solve x^2 + 4x + 4 = 0")

// Handle the streamed response that includes thought summaries.
var thoughts = ""
var answer = ""
for try await response in stream {
  if let thought = response.thoughtSummary {
    if thoughts.isEmpty {
      print("--- Thoughts Summary ---")
    }
    print(thought)
    thoughts += thought
  }

  if let text = response.text {
    if answer.isEmpty {
      print("--- Answer ---")
    }
    print(text)
    answer += text
  }
}

Kotlin

在创建 GenerativeModel 实例时,在 GenerationConfig 中启用思路总结。


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      includeThoughts = true
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_MODEL_NAME",
  generationConfig,
)

// Handle the streamed response that includes thought summaries.
var thoughts = ""
var answer = ""
model.generateContentStream("solve x^2 + 4x + 4 = 0").collect { response ->
    response.thoughtSummary?.let {
        if (thoughts.isEmpty()) {
            println("--- Thoughts Summary ---")
        }
        print(it)
        thoughts += it
    }
    response.text?.let {
        if (answer.isEmpty()) {
            println("--- Answer ---")
        }
        print(it)
        answer += it
    }
}

Java

在创建 GenerativeModel 实例时,在 GenerationConfig 中启用思路总结。


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setIncludeThoughts(true)
    .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 */ "GEMINI_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// Streaming with Java is complex and depends on the async library used.
// This is a conceptual example using a reactive stream.
Flowable responseStream = model.generateContentStream("solve x^2 + 4x + 4 = 0");

// Handle the streamed response that includes thought summaries
StringBuilder thoughts = new StringBuilder();
StringBuilder answer = new StringBuilder();

responseStream.subscribe(response -> {
    if (response.getThoughtSummary() != null) {
        if (thoughts.length() == 0) {
            System.out.println("--- Thoughts Summary ---");
        }
        System.out.print(response.getThoughtSummary());
        thoughts.append(response.getThoughtSummary());
    }
    if (response.getText() != null) {
        if (answer.length() == 0) {
            System.out.println("--- Answer ---");
        }
        System.out.print(response.getText());
        answer.append(response.getText());
    }
}, throwable -> {
    // Handle error
});

Web

在创建 GenerativeModel 实例时,在 GenerationConfig 中启用思路总结。


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
const generationConfig = {
  thinkingConfig: {
    includeThoughts: true
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });

const result = await model.generateContentStream("solve x^2 + 4x + 4 = 0");

// Handle the streamed response that includes thought summaries.
let thoughts = "";
let answer = "";
for await (const chunk of result.stream) {
  if (chunk.thoughtSummary()) {
    if (thoughts === "") {
      console.log("--- Thoughts Summary ---");
    }
    // In Node.js, process.stdout.write(chunk.thoughtSummary()) could be used
    // to avoid extra newlines.
    console.log(chunk.thoughtSummary());
    thoughts += chunk.thoughtSummary();
  }

  const text = chunk.text();
  if (text) {
    if (answer === "") {
      console.log("--- Answer ---");
    }
    // In Node.js, process.stdout.write(text) could be used.
    console.log(text);
    answer += text;
  }
}

Dart

在创建 GenerativeModel 实例时,在 GenerationConfig 中启用思路总结。


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
final thinkingConfig = ThinkingConfig(includeThoughts: true);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_MODEL_NAME',
  generationConfig: generationConfig,
);

final responses = model.generateContentStream('solve x^2 + 4x + 4 = 0');

// Handle the streamed response that includes thought summaries.
var thoughts = '';
var answer = '';
await for (final response in responses) {
  if (response.thoughtSummary != null) {
    if (thoughts.isEmpty) {
      print('--- Thoughts Summary ---');
    }
    thoughts += response.thoughtSummary!;
  }
  if (response.text != null) {
    if (answer.isEmpty) {
      print('--- Answer ---');
    }
    answer += response.text!;
  }
}

Unity

在创建 GenerativeModel 实例时,在 GenerationConfig 中启用思路总结。


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
var thinkingConfig = new ThinkingConfig(includeThoughts: true);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
);

var stream = model.GenerateContentStreamAsync("solve x^2 + 4x + 4 = 0");

// Handle the streamed response that includes thought summaries.
var thoughts = "";
var answer = "";
await foreach (var response in stream)
{
    if (response.ThoughtSummary != null)
    {
        if (string.IsNullOrEmpty(thoughts))
        {
            Debug.Log("--- Thoughts Summary ---");
        }
        Debug.Log(response.ThoughtSummary);
        thoughts += response.ThoughtSummary;
    }
    if (response.Text != null)
    {
        if (string.IsNullOrEmpty(answer))
        {
            Debug.Log("--- Answer ---");
        }
        Debug.Log(response.Text);
        answer += response.Text;
    }
}



思维签名

在多轮互动中使用思考时,模型无法访问之前轮次的思考上下文。不过,如果您使用函数调用,则可以利用思考特征在多个对话轮次中保持思考上下文。思考特征是模型内部思考过程的加密表示形式,在使用思考函数调用时可用。具体来说,在以下情况下,系统会生成思考特征:

  • 已启用思考,并生成了思考。
  • 请求包含函数声明。

如需利用思考签名,请照常使用函数调用。 Firebase AI Logic SDK 可管理状态并自动处理意念签名,从而简化流程。在 Chat 会话中,SDK 会自动在后续的 sendMessagesendMessageStream 调用之间传递任何生成的思维签名。



价格和思考 token 计数

思考令牌的价格与文本输出令牌的价格相同。如果您启用思考总结,则这些总结会被视为思考 token,并按相应价格收费。

您可以Firebase 控制台中启用 AI 监控,以监控已启用思考功能的请求的思考令牌数量。

您可以从回答的 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

// ...

final response = await model.generateContent(
  Content.text("Why is the sky blue?"),
]);

if (response?.usageMetadata case final usageMetadata?) {
  print("Thoughts Token Count: ${usageMetadata.thoughtsTokenCount}");
}

Unity

// ...

var response = await model.GenerateContentAsync("Why is the sky blue?");

if (response.UsageMetadata != null)
{
    UnityEngine.Debug.Log($"Thoughts Token Count: {response.UsageMetadata?.ThoughtsTokenCount}");
}

如需详细了解令牌,请参阅计算令牌指南