Gemini 思考

Gemini 2.5 系列模型在產生回覆時會使用內部「思考過程」。這個過程有助於提升他們的推理能力,並協助他們使用多步驟規劃來解決複雜的工作。因此,這些模型特別擅長程式設計、進階數學、資料分析,以及其他需要規劃或思考的工作。

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

使用思考模型

具備思考能力的模型已在 Google AI Studio 和 Gemini API 中提供。由於 2.5 系列模型可根據提示自動決定何時及如何思考,因此 API 和 AI Studio 的思考功能預設為開啟狀態。在大多數情況下,保留思考時間是有益的。不過,如果您想關閉思考功能,可以將 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:找出所有大於 9 的整數基數 b,其中 17b 是 97b 的除數。
    • 為網頁應用程式編寫 Python 程式碼,以便將即時股票市場資料 (包括使用者驗證) 以圖形呈現。盡可能提高效率。

後續步驟