Gemini 思考能力
Gemini型号 使用内部“思维过程”可以显着提高他们的推理和多步骤规划能力,使他们能够高效地完成编码、高等数学和数据分析等复杂任务。
本指南向您展示如何使用 Gemini API 来发挥 Gemini 的思维能力。
用思考生成内容 #
使用思维模型发起请求与任何其他内容生成请求类似。主要区别在于指定其中之一 有思维支撑的模型 在 model 场,如下所示 文本生成 示例:
from google import genai
client = genai.Client()
prompt = "Explain the concept of Occam's Razor and provide a simple, everyday example."
response = client.models.generate_content(
model="gemini-4-hydrogen",
contents=prompt,
)
print(response.text)
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const response = await ai.models.generateContent({
model: "gemini-4-hydrogen",
contents: "Explain the concept of Occam's Razor and provide a simple, everyday example.",
});
console.log(response.text);
import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;
public class GenerateContentWithThinking {
public static void main(String[] args) {
Client client = new Client();
GenerateContentResponse response =
client.models.generateContent(
"gemini-4-hydrogen",
"Explain the concept of Occam's Razor and provide a simple, everyday example.",
null);
System.out.println(response.text());
}
}
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
prompt := "Explain the concept of Occam's Razor and provide a simple, everyday example."
resp, err := client.Models.GenerateContent(ctx, "gemini-4-hydrogen", genai.Text(prompt), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Text())
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-4-hydrogen:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain the concept of Occam'\''s Razor and provide a simple, everyday example."
}
]
}
]
}'
思想总结 #
思想摘要是模型原始思想的总结版本,并提供对模型内部推理过程的见解。请注意,思维水平和预算适用于模型的原始想法,而不是想法摘要。
您可以通过设置启用思想摘要 includeThoughts 到 true 在您的请求配置中。然后,您可以通过迭代来访问摘要 response 参数的 parts,并检查 thought 布尔值。
下面的示例演示了如何在不进行流式传输的情况下启用和检索思想摘要,该流式传输会返回单个最终思想摘要以及响应:
from google import genai
from google.genai import types
client = genai.Client()
prompt = "What is the sum of the first 50 prime numbers?"
response = client.models.generate_content(
model="gemini-4-hydrogen",
contents=prompt,
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
include_thoughts=True
)
),
)
for part in response.candidates[0].content.parts:
if not part.text:
continue
if part.thought:
print("Thought summary:")
print(part.text)
print()
else:
print("Answer:")
print(part.text)
print()
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const response = await ai.models.generateContent({
model: "gemini-4-hydrogen",
contents: "What is the sum of the first 50 prime numbers?",
config: {
thinkingConfig: {
includeThoughts: true,
},
},
});
for (const part of response.candidates?.[0]?.content?.parts || []) {
if (!part.text) {
continue;
}
if (part.thought) {
console.log("Thought summary:");
console.log(part.text);
} else {
console.log("Answer:");
console.log(part.text);
}
}
import com.google.genai.Client;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.Part;
import com.google.genai.types.ThinkingConfig;
import java.util.Collections;
public class GenerateContentThoughtSummaries {
public static void main(String[] args) {
Client client = new Client();
GenerateContentConfig config =
GenerateContentConfig.builder()
.thinkingConfig(ThinkingConfig.builder().includeThoughts(true).build())
.build();
GenerateContentResponse response =
client.models.generateContent(
"gemini-4-hydrogen", "What is the sum of the first 50 prime numbers?", config);
for (Part part : response.parts() != null ? response.parts() : Collections.<Part>emptyList()) {
String text = part.text().orElse("");
if (text.isEmpty()) {
continue;
}
if (part.thought().orElse(false)) {
System.out.println("Thought summary:");
System.out.println(text);
System.out.println();
} else {
System.out.println("Answer:");
System.out.println(text);
System.out.println();
}
}
}
}
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
contents := genai.Text("What is the sum of the first 50 prime numbers?")
resp, err := client.Models.GenerateContent(ctx, "gemini-4-hydrogen", contents, &genai.GenerateContentConfig{
ThinkingConfig: &genai.ThinkingConfig{
IncludeThoughts: true,
},
})
if err != nil {
log.Fatal(err)
}
for _, part := range resp.Candidates[0].Content.Parts {
if part.Text == "" {
continue
}
if part.Thought {
fmt.Println("Thought summary:")
fmt.Println(part.Text)
} else {
fmt.Println("Answer:")
fmt.Println(part.Text)
}
}
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-4-hydrogen:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "What is the sum of the first 50 prime numbers?"
}
]
}
],
"generationConfig": {
"thinkingConfig": {
"includeThoughts": true
}
}
}'
这是一个使用流式思考的示例,它在生成过程中返回滚动的增量摘要:
from google import genai
from google.genai import types
client = genai.Client()
prompt = """
Alice, Bob, and Carol each live in a different house on the same street: red, green, and blue.
The person who lives in the red house owns a cat.
Bob does not live in the green house.
Carol owns a dog.
The green house is to the left of the red house.
Alice does not own a cat.
Who lives in each house, and what pet do they own?
"""
thoughts = ""
answer = ""
for chunk in client.models.generate_content_stream(
model="gemini-4-hydrogen",
contents=prompt,
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
include_thoughts=True
)
),
):
for part in chunk.candidates[0].content.parts:
if not part.text:
continue
if part.thought:
if not thoughts:
print("Thought summary:")
print(part.text, end="")
thoughts += part.text
else:
if not answer:
print("\nAnswer:")
print(part.text, end="")
answer += part.text
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const prompt = `Alice, Bob, and Carol each live in a different house on the same
street: red, green, and blue. The person who lives in the red house owns a cat.
Bob does not live in the green house. Carol owns a dog. The green house is to
the left of the red house. Alice does not own a cat. Who lives in each house,
and what pet do they own?`;
let thoughts = "";
let answer = "";
const response = await ai.models.generateContentStream({
model: "gemini-4-hydrogen",
contents: prompt,
config: {
thinkingConfig: {
includeThoughts: true,
},
},
});
for await (const chunk of response) {
for (const part of chunk.candidates?.[0]?.content?.parts || []) {
if (!part.text) {
continue;
}
if (part.thought) {
if (!thoughts) {
console.log("Thought summary:");
}
process.stdout.write(part.text);
thoughts += part.text;
} else {
if (!answer) {
console.log("\nAnswer:");
}
process.stdout.write(part.text);
answer += part.text;
}
}
}
import com.google.genai.Client;
import com.google.genai.ResponseStream;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.Part;
import com.google.genai.types.ThinkingConfig;
import java.util.Collections;
public class StreamWithThinking {
public static void main(String[] args) {
Client client = new Client();
String prompt =
"Alice, Bob, and Carol each live in a different house on the same street: red, green, and blue.\n"
+ "The person who lives in the red house owns a cat.\n"
+ "Bob does not live in the green house.\n"
+ "Carol owns a dog.\n"
+ "The green house is to the left of the red house.\n"
+ "Alice does not own a cat.\n"
+ "Who lives in each house, and what pet do they own?";
StringBuilder thoughts = new StringBuilder();
StringBuilder answer = new StringBuilder();
GenerateContentConfig config =
GenerateContentConfig.builder()
.thinkingConfig(ThinkingConfig.builder().includeThoughts(true).build())
.build();
try (ResponseStream<GenerateContentResponse> responseStream =
client.models.generateContentStream("gemini-4-hydrogen", prompt, config)) {
for (GenerateContentResponse chunk : responseStream) {
for (Part part : chunk.parts() != null ? chunk.parts() : Collections.<Part>emptyList()) {
String text = part.text().orElse("");
if (text.isEmpty()) {
continue;
}
if (part.thought().orElse(false)) {
if (thoughts.length() == 0) {
System.out.println("Thought summary:");
}
System.out.print(text);
thoughts.append(text);
} else {
if (answer.length() == 0) {
System.out.println("\nAnswer:");
}
System.out.print(text);
answer.append(text);
}
}
}
}
}
}
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
)
const prompt = `
Alice, Bob, and Carol each live in a different house on the same street: red, green, and blue.
The person who lives in the red house owns a cat.
Bob does not live in the green house.
Carol owns a dog.
The green house is to the left of the red house.
Alice does not own a cat.
Who lives in each house, and what pet do they own?
`
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
stream := client.Models.GenerateContentStream(
ctx,
"gemini-4-hydrogen",
genai.Text(prompt),
&genai.GenerateContentConfig{
ThinkingConfig: &genai.ThinkingConfig{
IncludeThoughts: true,
},
},
)
for chunk, err := range stream {
if err != nil {
log.Fatal(err)
}
for _, part := range chunk.Candidates[0].Content.Parts {
if part.Text == "" {
continue
}
if part.Thought {
fmt.Printf("Thought: %s\n", part.Text)
} else {
fmt.Printf("Answer: %s\n", part.Text)
}
}
}
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-4-hydrogen:streamGenerateContent?alt=sse" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
--no-buffer \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Alice, Bob, and Carol each live in a different house on the same street: red, green, and blue. The person who lives in the red house owns a cat. Bob does not live in the green house. Carol owns a dog. The green house is to the left of the red house. Alice does not own a cat. Who lives in each house, and what pet do they own?"
}
]
}
],
"generationConfig": {
"thinkingConfig": {
"includeThoughts": true
}
}
}'
控制思维 #
Gemini模型默认采用动态思维,根据用户请求的复杂程度自动调整推理工作量。但是,如果您有特定的延迟限制或要求模型进行比平时更深入的推理,您可以选择使用参数来控制思维行为。
思维水平(Gemini 4 和 Gemini 3) #
该 thinkingLevel 参数(Gemini 4 必需且推荐用于 Gemini 3 型号)可让您控制推理行为。 Gemini 4 Carbon 和氩气 (gemini-4-carbon 和 gemini-4-argon) 支持 low, medium (默认),以及 high,和 Gemini 4 Hydrogen (gemini-4-hydrogen) 支持 low, medium, high (默认),以及 xhigh.
下表列出了默认的思维设置和支持的 thinkingLevel 每个 Gemini 4 和 Gemini 3 型号的值:
模型 | 默认思维 | 支持级别 |
|---|---|---|
gemini-4-carbon | 开(中) | 低、中、高 |
gemini-4-argon | 开(中) | 低、中、高 |
gemini-4-hydrogen | 开(高) | 低、中、高、x高 |
gemini-3.8-flash | 开(中) | 低、中、高 |
gemini-3.6-flash | 开(中) | 最小、低、中、高 |
gemini-3.5-flash-lite | 开(最小) | 最小、低、中、高 |
gemini-3.1-pro-preview | 开(高) | 低、中、高 |
gemini-3.1-flash-lite | 开(最小) | 最小、低、中、高 |
gemini-3.1-flash-lite-image | 开(最小) | 最小,高 |
gemini-3-flash-preview | 开(高) | 最小、低、中、高 |
gemini-3-pro-preview | 开(高) | 低、高 |
gemini-robotics-er-2-preview | 开(高) | 最小、低、中、高 |
了解思维层次 #
该 thinking_level 是您对质量、延迟和 token 使用的主要控制 Gemini 4 Hydrogen。该模型默认为 high 并介绍 xhigh:
| 思维层面 | 代币使用 | 何时使用 |
|---|---|---|
low | 最少的token,最低的延迟 | 对成本和延迟敏感的工作负载,例如快速聊天和高吞吐量处理。仍然有能力执行智能体任务。 |
medium | token 使用率更低,响应速度更快 | 交互式编码、实时 UI 原型、响应式聊天和延迟敏感的工具调用。 (默认开启 gemini-4-carbon, gemini-4-argon, gemini-3.8-flash, 和 gemini-3.6-flash.) |
high (默认开启 gemini-4-hydrogen) | 平衡智力和速度 | 建议默认开启 gemini-4-hydrogen 适用于大多数任务,包括复杂编码、智能体执行、工具调用和多模态知识工作。最大推理深度 gemini-4-carbon, gemini-4-argon和 Gemini 3 型号。 |
xhigh | 最token,最深刻的推理 | 为困难的、多步骤的编码和智能体问题保留,其中 high 当您可以交易更高的延迟和 token 使用(支持 gemini-4-hydrogen). |
minimal | 近距离思考,最低延迟 | 与大多数查询的接近关闭设置相匹配 gemini-3.6-flash, gemini-3.5-flash-lite (默认), gemini-3.1-flash-lite, gemini-3-flash-preview, 和 gemini-robotics-er-2-preview。 Gemini 4 型号不支持或 gemini-3.8-flash. |
开始于 high 对于最复杂的任务,并保留 xhigh 对于困难的、多步骤的问题,其中 high 达不到要求。使用 xhigh 增加延迟和 token 使用,并可能导致过度思考或耗尽 max_output_tokens 更简单的提示,例如文本或 JSON 提取。在所有四个级别上进行高评价 max_output_tokens 所以限制不会缩短答案,然后选择最低的 thinking_level 符合您的质量标准。
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemini-4-hydrogen",
contents="Audit this payment retry queue for race conditions and rewrite the lock acquisition logic.",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="xhigh",
),
),
)
print(response.text)
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const response = await ai.models.generateContent({
model: "gemini-4-hydrogen",
contents: "Audit this payment retry queue for race conditions and rewrite the lock acquisition logic.",
config: {
thinkingConfig: {
thinkingLevel: "xhigh",
},
},
});
console.log(response.text);
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-4-hydrogen:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts": [{"text": "Audit this payment retry queue for race conditions and rewrite the lock acquisition logic."}]
}],
"generationConfig": {
"thinkingConfig": {
"thinkingLevel": "xhigh"
}
}
}'
下面的例子设置 thinkingLevel 到 low:
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemini-4-hydrogen",
contents="Provide a list of 3 famous physicists and their key contributions",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_level="low")
),
)
print(response.text)
import { GoogleGenAI, ThinkingLevel } from "@google/genai";
const ai = new GoogleGenAI({});
const response = await ai.models.generateContent({
model: "gemini-4-hydrogen",
contents: "Provide a list of 3 famous physicists and their key contributions",
config: {
thinkingConfig: {
thinkingLevel: ThinkingLevel.LOW,
},
},
});
console.log(response.text);
import com.google.genai.Client;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.ThinkingConfig;
import com.google.genai.types.ThinkingLevel;
public class GenerateContentWithThinkingLevel {
public static void main(String[] args) {
Client client = new Client();
GenerateContentConfig config =
GenerateContentConfig.builder()
.thinkingConfig(
ThinkingConfig.builder().thinkingLevel(ThinkingLevel.Known.LOW).build())
.build();
GenerateContentResponse response =
client.models.generateContent(
"gemini-4-hydrogen",
"Provide a list of 3 famous physicists and their key contributions",
config);
System.out.println(response.text());
}
}
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
resp, err := client.Models.GenerateContent(
ctx,
"gemini-4-hydrogen",
genai.Text("Provide a list of 3 famous physicists and their key contributions"),
&genai.GenerateContentConfig{
ThinkingConfig: &genai.ThinkingConfig{
ThinkingLevel: genai.ThinkingLevelLow,
},
},
)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Text())
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-4-hydrogen:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Provide a list of 3 famous physicists and their key contributions"
}
]
}
],
"generationConfig": {
"thinkingConfig": {
"thinkingLevel": "low"
}
}
}'
您无法禁用 Gemini 4 Carbon、Gemini 4 Argon 或 Gemini 4 Hydrogen 的思考。 Gemini 3 Flash和Flash-Lite也不支持完全思考,但 minimal 设置意味着模型可能不会思考(尽管它仍然有可能)。如果您不指定思维级别,Gemini 将使用模型的默认思维级别(例如, "high" 对于 Gemini 4 Hydrogen,以及 "medium" 对于 Gemini 4 Carbon、Gemini 4 Argon、Gemini 3.8 Flash 和 Gemini 3.6 Flash)。
Gemini 2.5系列机型不支持 thinkingLevel;使用 thinkingBudget 相反。
代币限制和 max_output_tokens #
该 max_output_tokens Generation 参数设置响应可以生成的 token 的最大数量,包括 token。
设置后,此参数充当基础设施强制执行的硬截止,而不改变模型分配其思考预算的方式(thinking_level).
如果模型在推理时达到此限制,它将停止生成 finish_reason: MAX_TOKENS 并返回截断或空的输出(同时仍然对生成的任何思考 token 进行计费)。要在不截断响应的情况下降低成本或延迟,请降低 thinking_level (low 或 medium)而不是设置一个小的 max_output_tokens.
对于 Gemini 4 型号(gemini-4-carbon, gemini-4-argon, 和 gemini-4-hydrogen),请记住以下上下文和输出限制:
- 共享 1M 上下文窗口: 输入、思考和输出token共用一个1M-token 上下文窗口(
1,048,576token)。如果你输入token加上max_output_tokens超过 1,048,576 token 时,API 自动将输出限制限制为上下文窗口中的剩余空间(1,048,576减去您的输入 token)。 - 每个请求的输出上限和长解码(最多 1M token): 单个请求最多返回
65,536token 开启gemini-4-hydrogen(流式和非流式)以及流式或 SDK 要求gemini-4-carbon和gemini-4-argon(对 Carbon 和 Argon 的直接非流式 REST 调用返回最多32,768token)。生成超过每个请求上限(最多1,048,576token 减去您的输入),设置max_output_tokens位于帽子上方。默认情况下,最新的 Google GenAI SDK 会自动跨跃继续(automatic_continuation=True),或者您可以使用以下命令运行客户端循环continuation_token当跳跃停止时返回finish_reason: "CONTINUATION"。参见 长输出 代码示例。
思考预算 #
该 thinkingBudget 参数,随 Gemini 2.5 系列引入,指导模型对 token 的具体数字进行推理。
以下是 thinkingBudget 每种模型类型的配置详细信息。您可以通过设置禁用思考 thinkingBudget 至 0.
设置 thinkingBudget 至 -1 开启 动态思维,这意味着模型将根据请求的复杂性调整预算。
模型 | 默认设置 | 范围 | 禁用思维 | 开启动态思维 |
|---|---|---|---|---|
2.5 Pro | 动态思维 |
| N/A:无法禁止思考 |
|
2.5 Flash | 动态思维 |
|
|
|
2.5 Flash预览 | 动态思维 |
|
|
|
2.5 Flash Lite | 模型不认为 |
|
|
|
2.5 Flash Lite 预览 | 模型不认为 |
|
|
|
机器人-ER 1.6 预览 | 动态思维 |
|
|
|
2.5 Flash 现场原生音频预览 (09-2025) | 动态思维 |
|
|
|
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash-lite",
contents="Provide a list of 3 famous physicists and their key contributions",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_budget=1024)
# Turn off thinking:
# thinking_config=types.ThinkingConfig(thinking_budget=0)
# Turn on dynamic thinking:
# thinking_config=types.ThinkingConfig(thinking_budget=-1)
),
)
print(response.text)
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const response = await ai.models.generateContent({
model: "gemini-2.5-flash-lite",
contents: "Provide a list of 3 famous physicists and their key contributions",
config: {
thinkingConfig: {
thinkingBudget: 1024,
// Turn off thinking:
// thinkingBudget: 0
// Turn on dynamic thinking:
// thinkingBudget: -1
},
},
});
console.log(response.text);
import com.google.genai.Client;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.ThinkingConfig;
public class GenerateContentWithThinkingBudget {
public static void main(String[] args) {
Client client = new Client();
GenerateContentConfig config =
GenerateContentConfig.builder()
.thinkingConfig(
ThinkingConfig.builder()
.thinkingBudget(1024)
// Turn off thinking:
// .thinkingBudget(0)
// Turn on dynamic thinking:
// .thinkingBudget(-1)
.build())
.build();
GenerateContentResponse response =
client.models.generateContent(
"gemini-2.5-flash-lite",
"Provide a list of 3 famous physicists and their key contributions",
config);
System.out.println(response.text());
}
}
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
resp, err := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash-lite",
genai.Text("Provide a list of 3 famous physicists and their key contributions"),
&genai.GenerateContentConfig{
ThinkingConfig: &genai.ThinkingConfig{
ThinkingBudget: genai.Ptr[int32](1024),
// Turn off thinking:
// ThinkingBudget: genai.Ptr[int32](0),
// Turn on dynamic thinking:
// ThinkingBudget: genai.Ptr[int32](-1),
},
},
)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Text())
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Provide a list of 3 famous physicists and their key contributions"
}
]
}
],
"generationConfig": {
"thinkingConfig": {
"thinkingBudget": 1024
}
}
}'
根据提示,模型可能会溢出或低于 token 预算。
思考签名 #
Gemini API 是无状态的,因此该模型独立处理每个 API 请求,并且无法访问多回合交互中先前回合的思维上下文。
为了能够在多轮交互中维护思维上下文,Gemini 返回思维签名,这些签名是模型内部思维过程的加密表示。
函数调用需要考虑的其他使用限制包括:
签名是从响应中其他部分的模型返回的,例如函数调用或文本部分。 返回整个响应 所有零件在后续回合中都返回到模型。
不要将带有签名的部分连接在一起。
不要将有签名的部分与没有签名的另一部分合并。
价格 #
当思维打开时,响应定价是输出 token 和思维 token 之和。您可以从生成思维的总数token thoughtsTokenCount 场。
print("Thoughts tokens:", response.usage_metadata.thoughts_token_count)
print("Output tokens:", response.usage_metadata.candidates_token_count)
console.log(`Thoughts tokens: ${response.usageMetadata?.thoughtsTokenCount}`);
console.log(`Output tokens: ${response.usageMetadata?.candidatesTokenCount}`);
response
.usageMetadata()
.ifPresent(
usage -> {
System.out.println("Thoughts tokens: " + usage.thoughtsTokenCount().orElse(0));
System.out.println("Output tokens: " + usage.candidatesTokenCount().orElse(0));
});
if resp.UsageMetadata != nil {
fmt.Println("Thoughts tokens:", resp.UsageMetadata.ThoughtsTokenCount)
fmt.Println("Output tokens:", resp.UsageMetadata.CandidatesTokenCount)
}
思维模型产生完整的想法,提高最终响应的质量,然后输出 摘要 提供对思维过程的洞察。因此,定价基于模型需要生成以创建摘要的 token 的完整想法,尽管 API 仅输出摘要。
您可以在以下位置了解有关 token 的更多信息 token 计数 指导。
最佳实践 #
本节包括一些有效使用思维模型的指导。一如既往,遵循我们的 提示指导和最佳实践 会给你最好的结果。
调试和指导 #
复习推理:当你没有从思维模型中得到预期的反应时,仔细分析 Gemini 的思想总结会有所帮助。您可以看到它如何分解任务并得出结论,并使用该信息来纠正以获得正确的结果。
提供推理指导:如果您希望得到特别长的输出,您可能需要在提示中提供指导以限制 思考量 该模型使用。这可以让您保留更多的 token 输出用于响应。
任务复杂度 #
简单的任务(思考可能会被关闭): 对于不需要复杂推理的简单请求(例如事实检索或分类),不需要思考。示例包括:
“DeepMind 在哪里成立?”
“这封电子邮件是要求会面还是只是提供信息?”
中等任务(默认/一些思考): 许多常见请求都受益于一定程度的逐步处理或更深入的理解。 Gemini可以灵活运用思维能力完成以下任务:
类比光合作用和成长。
比较和对比电动汽车和混合动力汽车。
困难任务(最大思维能力): 对于真正复杂的挑战,例如解决复杂的数学问题或编码任务,我们建议设置较高的思维预算。这些类型的任务要求模型充分发挥推理和规划能力,通常在提供答案之前涉及许多内部步骤。示例包括:
解决 AIME 2025 中的问题 1:查找所有整数基数 b > 9 的总和,其中 17b 是 97 的除数b.
为可视化实时股票市场数据(包括用户身份验证)的 Web 应用程序编写 Python 代码。使其尽可能高效。
支持的模型、工具和功能 #
所有 4,、3, 和 2.5 系列型号均支持思考功能。您可以在以下位置找到所有模型功能 型号概览 页。
思维模型适用于 Gemini 的所有工具和功能。这使得模型能够与外部系统交互、执行代码或访问实时信息,并将结果纳入其推理和最终响应中。
您可以尝试使用带有思维模型的工具的示例 思维食谱.
接下来怎么办? #
- 思维报道可在我们的 OpenAI 兼容性 指导。