Gemini 思考

Gemini 2.5 系列模型采用内部“思考过程”,可显著提升其推理能力和多步规划能力,使其能够高效地处理编码、高级数学和数据分析等复杂任务。

本指南介绍了如何使用 Gemini API 处理 Gemini 的思考功能。

准备工作

确保您使用的是受支持的 2.5 系列模型进行思考。 在深入了解 API 之前,不妨先在 AI Studio 中探索以下模型:

通过思考生成内容

使用思考模型发起请求与任何其他内容生成请求类似。主要区别在于,您需要在 model 字段中指定某个支持思考的模型,如以下文本生成示例所示:

Python

from google import genai  client = genai.Client(api_key="GOOGLE_API_KEY") prompt = "Explain the concept of Occam's Razor and provide a simple, everyday example." response = client.models.generate_content(     model="gemini-2.5-pro",     contents=prompt )  print(response.text) 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });  async function main() {   const prompt = "Explain the concept of Occam's Razor and provide a simple, everyday example.";    const response = await ai.models.generateContent({     model: "gemini-2.5-pro",     contents: prompt,   });    console.log(response.text); }  main(); 

Go

// import packages here  func main() {   ctx := context.Background()   client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GOOGLE_API_KEY")))   if err != nil {     log.Fatal(err)   }   defer client.Close()    model := client.GenerativeModel("gemini-2.5-pro")   resp, err := model.GenerateContent(ctx, genai.Text("Explain the concept of Occam's Razor and provide a simple, everyday example."))   if err != nil {     log.Fatal(err)   }   fmt.Println(resp.Text()) } 

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent?key=$GOOGLE_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."          }        ]      }    ]  }'  ``` 

思考预算

thinkingBudget 参数可为模型提供指导,帮助其了解在生成回答时可使用的思考 token 数量。令牌数量越多,推理过程通常越详细,这对处理更复杂的任务很有帮助。将 thinkingBudget 设置为 -1 会启用动态思考,这意味着模型会根据请求的复杂性调整预算。

只有 Gemini 2.5 Flash、2.5 Pro 和 2.5 Flash-Lite 支持 thinkingBudget。根据问题,模型可能会超出或不足令牌预算。

以下是每种模型类型的 thinkingBudget 配置详细信息。

型号 默认设置
(未设置思考预算)
Range 停用思考功能 开启动态思考
2.5 Pro 动态思考:模型决定何时以及思考多少 12832768 不适用:无法停用思考功能 thinkingBudget = -1
2.5 闪光灯 动态思考:模型决定何时以及思考多少 024576 thinkingBudget = 0 thinkingBudget = -1
2.5 Flash Lite 模型不会思考 51224576 thinkingBudget = 0 thinkingBudget = -1

Python

from google import genai from google.genai import types  client = genai.Client()  response = client.models.generate_content(     model="gemini-2.5-pro",     contents="Provide a list of 3 famous physicists and their key contributions",     config=types.GenerateContentConfig(         thinking_config=types.ThinkingConfig(thinking_budget=1024)     ), )  print(response.text) 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });  async function main() {   const response = await ai.models.generateContent({     model: "gemini-2.5-pro",     contents: "Provide a list of 3 famous physicists and their key contributions",     config: {       thinkingConfig: {         thinkingBudget: 1024,       },     },   });    console.log(response.text); }  main(); 

Go

package main  import (   "context"   "fmt"   "google.golang.org/genai"   "os" )  func main() {   ctx := context.Background()   client, _ := genai.NewClient(ctx, &genai.ClientConfig{     APIKey:  os.Getenv("GOOGLE_API_KEY"),     Backend: genai.BackendGeminiAPI,   })    thinkingBudgetVal := int32(1024)    contents := genai.Text("Provide a list of 3 famous physicists and their key contributions")   model := "gemini-2.5-pro"   resp, _ := client.Models.GenerateContent(ctx, model, contents, &genai.GenerateContentConfig{     ThinkingConfig: &genai.ThinkingConfig{       ThinkingBudget: &thinkingBudgetVal,     },   })  fmt.Println(resp.Text()) } 

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent?key=$GOOGLE_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     }   } }' 

思考摘要(实验性)

想法摘要是模型原始想法的综合版本,可提供有关模型内部推理过程的深入分析。请注意,思考预算适用于模型的原始思考,而不适用于思考摘要。

您可以在请求配置中将 includeThoughts 设置为 true,以启用思考摘要。然后,您可以通过迭代 response 参数的 parts 并检查 thought 布尔值来访问摘要。

以下示例演示了如何在不流式传输的情况下启用和检索思考摘要,这会在响应中返回单个最终思考摘要:

Python

from google import genai from google.genai import types  client = genai.Client(api_key="GOOGLE_API_KEY") prompt = "What is the sum of the first 50 prime numbers?" response = client.models.generate_content(   model="gemini-2.5-pro",   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() 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });  async function main() {   const response = await ai.models.generateContent({     model: "gemini-2.5-pro",     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;     }     else if (part.thought) {       console.log("Thoughts summary:");       console.log(part.text);     }     else {       console.log("Answer:");       console.log(part.text);     }   } }  main(); 

Go

package main  import (   "context"   "fmt"   "google.golang.org/genai"   "os" )  func main() {   ctx := context.Background()   client, _ := genai.NewClient(ctx, &genai.ClientConfig{     APIKey:  os.Getenv("GOOGLE_API_KEY"),     Backend: genai.BackendGeminiAPI,   })    contents := genai.Text("What is the sum of the first 50 prime numbers?")   model := "gemini-2.5-pro"   resp, _ := client.Models.GenerateContent(ctx, model, contents, &genai.GenerateContentConfig{     ThinkingConfig: &genai.ThinkingConfig{       IncludeThoughts: true,     },   })    for _, part := range resp.Candidates[0].Content.Parts {     if part.Text != "" {       if part.Thought {         fmt.Println("Thoughts Summary:")         fmt.Println(part.Text)       } else {         fmt.Println("Answer:")         fmt.Println(part.Text)       }     }   } } 

以下示例展示了如何使用“以流式传输的方式思考”方法,在生成过程中返回滚动增量摘要:

Python

from google import genai from google.genai import types  client = genai.Client(api_key="GOOGLE_API_KEY")  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-2.5-pro",     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     elif part.thought:       if not thoughts:         print("Thoughts summary:")       print(part.text)       thoughts += part.text     else:       if not answer:         print("Thoughts summary:")       print(part.text)       answer += part.text 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });  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 = "";  async function main() {   const response = await ai.models.generateContentStream({     model: "gemini-2.5-pro",     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;       } else if (part.thought) {         if (!thoughts) {           console.log("Thoughts summary:");         }         console.log(part.text);         thoughts = thoughts + part.text;       } else {         if (!answer) {           console.log("Answer:");         }         console.log(part.text);         answer = answer + part.text;       }     }   } }  await main(); 

价格

开启思考功能后,回答价格为输出令牌和思考令牌的总和。您可以从 thoughtsTokenCount 字段获取生成的思考令牌的总数。

Python

# ... print("Thoughts tokens:",response.usage_metadata.thoughts_token_count) print("Output tokens:",response.usage_metadata.candidates_token_count) 

JavaScript

// ... console.log(`Thoughts tokens: ${response.usageMetadata.thoughtsTokenCount}`); console.log(`Output tokens: ${response.usageMetadata.candidatesTokenCount}`); 

Go

// ... usageMetadata, err := json.MarshalIndent(response.UsageMetadata, "", "  ") if err != nil {   log.Fatal(err) } fmt.Println("Thoughts tokens:", string(usageMetadata.thoughts_token_count)) fmt.Println("Output tokens:", string(usageMetadata.candidates_token_count)) 

思考模型会生成完整的想法,以提高最终回答的质量,然后输出摘要,以便深入了解思考过程。因此,价格是根据模型生成摘要所需生成的完整思维令牌计算得出的,尽管 API 只会输出摘要。

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

支持的模型

您可以在模型概览页面上找到所有模型功能。

型号 思考摘要 思考预算
Gemini 2.5 Flash ✔️ ✔️
Gemini 2.5 Pro ✔️ ✔️
Gemini 2.5 Flash Lite ✔️ ✔️

最佳做法

本部分包含一些关于高效使用思维模型的指导。 一如既往,遵循我们的提示指南和最佳实践,您将取得理想的成效。

调试和转向

  • 检查推理过程:如果您未从思考模型获得预期的回答,不妨仔细分析 Gemini 的思考摘要。您可以查看它是如何拆解任务并得出结论的,并使用这些信息来修正结果,以获得正确的结果。

  • 在推理中提供指导:如果您希望获得特别长的输出,则可以在问题中提供指导,以限制模型使用的思考量。这样,您就可以为响应预留更多令牌输出。

任务复杂性

  • 简单任务(可以关闭思考):对于不需要复杂推理且直截了当的请求(例如事实检索或分类),无需思考。例如:
    • “DeepMind 的创始地在哪里?”
    • “这封电子邮件是想安排会议,还是只是提供信息?”
  • 中等任务(默认/需要一些思考):许多常见请求都需要进行一定程度的分步处理或更深入的理解。Gemini 可以灵活地使用思考功能来执行以下任务:
    • 将光合作用与成长进行类比。
    • 比较和对比电动汽车与混合动力汽车。
  • 难题(最大限度地发挥思维能力):对于真正复杂的挑战(例如解复杂的数学题或编码任务),我们建议设置较高的思考预算。这类任务要求模型充分发挥推理和规划能力,通常需要完成许多内部步骤才能提供答案。例如:
    • 解 AIME 2025 题目 1:求所有大于 9 且 17b 是 97b 的因子的整数底数 b 的总和。
    • 为可直观呈现实时股市数据(包括用户身份验证)的 Web 应用编写 Python 代码。尽可能提高效率。

利用工具和功能进行思考

思考模型可与 Gemini 的所有工具和功能搭配使用。这样一来,模型就可以与外部系统交互、执行代码或访问实时信息,并将结果纳入其推理和最终回答中。

  • 借助搜索工具,模型可以查询 Google 搜索,以查找最新信息或超出其训练数据范围的信息。这对于有关近期事件或极其具体的主题的问题非常有用。

  • 借助代码执行工具,模型可以生成和运行 Python 代码,以执行计算、处理数据或解决最好通过算法处理的问题。模型会接收代码的输出,并可以在其回答中使用该输出。

  • 借助结构化输出,您可以限制 Gemini 使用 JSON 进行响应。这对于将模型的输出集成到应用中特别有用。

  • 函数调用可将思考模型连接到外部工具和 API,以便推理何时调用正确的函数以及要提供哪些参数。

  • 网址情境可为模型提供网址,作为提示的额外情境。然后,模型可以从网址中检索内容,并使用这些内容来为其回答提供依据并对其进行塑造。

您可以在思考手册中尝试使用工具与思考模型的示例。

后续操作

  • 如需深入了解示例,请参阅以下内容:

    • 在使用工具时思考
    • 边思考边直播
    • 调整思考预算以获得不同的结果

    等内容,请参阅我们的思考手册

  • 我们的 OpenAI 兼容性指南现已推出思考覆盖率。

  • 如需详细了解 Gemini 2.5 Pro、Gemini Flash 2.5 和 Gemini 2.5 Flash-Lite,请访问模型页面