所有 Gemini 模型都使用内部“思考过程”,这显著提高了其推理和多步规划能力,使其在编码、高等数学和数据分析等复杂任务中非常有效。
思考模型提供以下配置和选项:
控制思考量
您可以配置思考级别,以控制模型可以执行的“思考”量。如果降低延迟时间或费用是优先事项,则此配置尤为重要。此外,请查看任务难度对比,以确定模型可能需要多强的思考能力。获取思考摘要
您可以启用思考摘要,以将其包含在生成的回答中。这些摘要是模型原始思考过程的综合版本,可让您深入了解模型的内部推理过程。处理思考签名
Firebase AI Logic SDK 会自动为您处理思考签名,从而确保模型能够访问之前轮次的思考上下文,尤其是在使用函数调用时。
请务必查看使用思考模型的最佳实践和提示指南。
思考模型使用最佳实践和提示指南
我们建议你在 Google AI Studio 或 Agent Studio 中测试提示,以便查看完整的思考过程。您可以找出模型可能出现偏差的任何方面,以便优化提示,获得更一致、更准确的回答。
首先提供一个描述预期结果的一般提示,然后观察模型在确定回答时最初的思考过程。如果回答不尽如人意,请使用以下任一提示技巧,帮助模型生成更好的回答:
- 提供分步说明
- 提供多个输入-输出对示例
- 提供有关输出和回答应如何措辞和设置格式的指导
- 提供具体的验证步骤
除了提示之外,您还可以考虑使用以下建议:
设置系统指令,这些指令就像一段“序言”,在模型接收到提示或最终用户的进一步指令之前添加。借助它们,您可以根据自己的具体需求和使用情形来控制模型的行为。
设置思考等级,以控制模型可以进行多少思考。如果您将其设置为高,则模型可以根据需要进行更多思考。如果将该值设置得较低,模型就不会“过度思考”其回答,同时也会为实际回答预留更多总 token 输出限制,从而有助于减少延迟时间和费用。
在 Firebase 控制台中启用 AI 监控,以监控已启用思考功能的请求的思考 Token 数和延迟时间。如果您启用了思考摘要,它们将显示在控制台中,您可以在其中检查模型的详细推理,以帮助您调试和优化提示。
控制思考量
您可以配置思考等级,以控制模型在返回回答之前可以进行多少“思考”和推理。如果缩短延迟时间或降低费用是优先事项,则此配置尤为重要。
请务必查看任务难度比较,以确定模型可能需要多强的思考能力。以下是一些简要指南:
- 如果任务不太复杂,或者您优先考虑缩短延迟时间或降低费用,请设置较低的思考值。
- 为较复杂的任务设置较高的思考值。
思考等级
如需控制模型在生成回答时可以进行多少思考,您可以指定思考级别,以控制允许其使用的思考 token 数量。
设置思考等级
|
点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
在创建 GenerativeModel 实例的过程中,在 GenerationConfig 中设置思考等级。该配置在实例的整个生命周期内保持不变。如果您想针对不同的请求使用不同的思维水平,请创建配置了每个水平的 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 4 Argongemini-4-argon
|
1 | (默认) | ||
Gemini 3.x Progemini-3.1-pro-preview
|
(默认) | |||
Gemini 3.x Flashgemini-3.8-flashgemini-3.7-flashgemini-3.6-flashgemini-3.5-flash
|
不受 gemini-3.8-flashgemini-3.7-flash |
(默认) | ||
Gemini 3.x Flash‑Litegemini-3.5-flash-litegemini-3.1-flash-lite
|
(默认) | |||
Gemini 3.x Pro Imagegemini-3-pro-image("Nano Banana Pro") |
(默认) | |||
Gemini 3.x Flash Imagegemini-3.1-flash-image("Nano Banana 2") |
(默认) | |||
Gemini 3.x Flash‑Lite Imagegemini-3.1-flash-lite-image("Nano Banana 2 Lite") |
(默认) |
1 gemini-4-argon、gemini-3.8-flash 和 gemini-3.7-flash 不支持 MINIMAL 思考等级。如果您将思考等级设置为 MINIMAL,则请求将失败并显示 400 错误。
思考预算(Gemini 2.5 模型)
如需控制 Gemini 2.5 模型在生成回答时可以进行多少思考,您可以指定思考预算,以限制其可以使用的思考 token 数量。
所有思维模型对应的任务复杂程度
简单任务 - 无需过多思考
不需要复杂推理的简单请求,例如事实检索或分类。示例:- “DeepMind 是在哪里成立的?”
- “这封邮件是在请求开会,还是只是提供信息?”
中等任务 - 可能需要进行一些思考
需要一定程度的逐步处理或更深入理解的常见请求。示例:- “将光合作用与成长进行类比。”
- “比较和对比电动汽车和混动车。”
困难任务 - 可能需要最大限度地思考
真正复杂的挑战,例如解决复杂的数学问题或编码任务。这些类型的任务需要模型充分发挥其推理和规划能力,通常在提供答案之前涉及许多内部步骤。示例:- “解决 2025 年 AIME 中的问题 1:求出所有整数底数 b > 9 的总和,使得 17b 是 97b 的除数。”
- “为 Web 应用编写 Python 代码,以可视化实时股市数据,包括用户身份验证。尽可能提高效率。”
思考摘要
思考总结是模型原始思考的合成版本,可提供对模型内部推理过程的洞见。
以下是回答中包含思路总结的一些原因:
您可以在应用的界面中显示总结,也可以让用户访问总结。思考摘要会作为响应中的单独部分返回,以便您更好地控制其在应用中的使用方式。
如果您还在 Firebase 控制台中启用 AI 监控,则控制台中会显示思考摘要,您可以在其中检查模型的详细推理,以帮助您调试和优化提示。
以下是关于思考过程摘要的一些重要注意事项:
想法总结不受思考预算的控制(预算仅适用于模型的原始想法)。不过,如果思考功能处于停用状态,模型将不会返回思考摘要。
思考摘要被视为模型常规生成的文本回答的一部分,并计为输出 token。
启用思考总结
|
点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
您可以在模型配置中将 includeThoughts 设置为 true,以启用思考摘要。然后,您可以通过检查响应中的 thoughtSummary 字段来访问摘要。
以下示例展示了如何启用并检索包含在回答中的思路总结:
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 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
在创建 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,
)
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
在创建 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.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 通过管理状态并自动为您处理思考签名,简化了这一过程。SDK 会在 Chat 会话中的后续 sendMessage 或 sendMessageStream 调用之间自动传递任何生成的思考签名。
价格和思考 token 计数
思考 token 的价格与文本输出 token 的价格相同。如果您启用思考摘要,它们会被视为思考 token,并相应地定价。
您可以在 Firebase 控制台中启用 AI 监控,以监控已启用思考功能的请求的思考 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
// ...
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}");
}
如需详细了解 token,请参阅计算 token 数量指南。