Gemini 思考

Gemini 2.5 系列模型使用內部「思考過程」來生成回應。這種做法可增進推理能力,並協助他們運用多步驟規劃來解決複雜工作因此,這些模型在程式設計、進階數學、資料分析,以及需要規劃或思考的工作方面特別實用。

本指南將說明如何使用 Gemini API 運用 Gemini 的思考功能。

使用思考模型

具備思考能力的模型可在 Google AI Studio 和 Gemini API 中使用。根據預設,API 和 AI Studio 都會啟用思考功能,因為 2.5 系列模型能自動根據提示決定何時要考慮合適內容。在大多數情況下,保留思考時間是有益的。不過,如果您想關閉思考功能,可以將 thinkingBudget 參數設為 0。

傳送基本要求

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-flash-preview-04-17",
    contents=prompt
)

print(response.text)
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-flash-preview-04-17",
    contents: prompt,
  });

  console.log(response.text);
}

main();
// 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-flash-preview-04-17")
  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())
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-04-17: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 參數可為模型提供產生回應時可用的思考符記數量指引。較多符號通常代表更詳細的思考,而這正是解決複雜任務所需的。thinkingBudget 必須是介於 0 到 24576 之間的整數。將思考預算設為 0,就會無法進行思考

視提示而定,模型可能會溢位或反向溢位權杖預算。

PythonJavaScriptREST
from google import genai
from google.genai import types

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash-preview-04-17",
    contents="Explain the Occam's Razor concept and provide everyday examples of it",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_budget=1024)
    ),
)

print(response.text)
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-flash-preview-04-17",
    contents: "Explain the Occam's Razor concept and provide everyday examples of it",
    config: {
      thinkingConfig: {
        thinkingBudget: 1024,
      },
    },
  });

  console.log(response.text);
}

main();
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-04-17:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
  "contents": [
    {
      "parts": [
        {
          "text": "Explain the Occam\''s Razor concept and provide everyday examples of it"
        }
      ]
    }
  ],
  "generationConfig": {
    "thinkingConfig": {
          "thinkingBudget": 1024
    }
  }
}'

搭配思考模式使用工具

您可以運用思考模型和 Gemini 的任何工具和功能,執行生成文字以外的動作。這讓他們能與外部系統互動、執行程式碼或存取即時資訊,並將結果納入其推論和最終回應中。

  • 搜尋工具可讓模型查詢外部搜尋引擎,尋找訓練資料以外的最新資訊或資訊。這項功能適用於關於近期事件或高度特定主題的問題。

  • 程式碼執行工具可讓模型產生及執行 Python 程式碼,以便執行計算、操控資料,或解決以演算法最為理想的方式問題。模型會接收程式碼的輸出內容,並可在回應中使用該輸出內容。

  • 透過結構化輸出格式,您可以限制 Gemini 以 JSON 回應,這是一種適合自動處理的結構化輸出格式。這特別適合用於將模型的輸出內容整合至應用程式。

  • 函式呼叫會將思考模型連結到外部工具和 API,以便判斷何時要呼叫正確的函式,以及該提供哪些參數。

最佳做法

本節提供一些有效使用思考模型的指南。一如既往,遵循提示指南和最佳做法,將能獲得最佳成效。

偵錯和引導

  • 檢視推理原因:如果思考模型沒有收到預期的回覆,可以仔細分析 Gemini 的推理程序。您可以看到工作如何分解工作並得出結論,然後使用該資訊來更正結果。

  • 提供推理指引:如果希望取得相當冗長的輸出內容,可以在提示中提供指引,以限制模型使用的思考程度。這樣您就能保留更多權杖輸出內容做為回應。

工作複雜度

  • 簡單工作 (思考可能關閉):對於簡單的要求,不需要複雜的推理,例如簡單的事實擷取或分類,不需要思考。例如:
    • 「DeepMind 是在哪裡創立?」
    • 「這封電子郵件是要求開會,還是只是提供資訊?」
  • 中等工作 (預設/部分思考):許多常見要求都需要逐步處理或深入瞭解。Gemini 可靈活運用思考功能,執行以下任務:
    • 光合作用和生長的類比。
    • 比較電動汽車和油電混合車。
  • 困難任務 (最高思考能力):對於真正複雜的挑戰,AI 需要運用完整的推理和規劃能力,通常需要執行許多內部步驟才能提供答案。例如:
    • 解決 AIME 2025 中的問題 1:找出 b > 9 所有整數底數 b > 9 的總和,其中 17b 是 97b 的除數。
    • 為網頁應用程式編寫 Python 程式碼,以便將即時股票市場資料 (包括使用者驗證) 以圖形呈現。盡可能提高效率。

後續步驟