Gemini 正在思考
Gemini 3 和 2.5 系列模型會運用內部「思考過程」,大幅提升推論和多步驟規劃能力,因此非常適合處理複雜工作,例如程式設計、高等數學和資料分析。
本指南說明如何使用 Gemini API,運用 Gemini 的思考能力。
生成有思考過程的內容
使用思考型模型發起要求,與任何其他內容生成要求類似。主要差異在於在 model 欄位中指定支援思考步驟的模型:
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Explain the concept of Occam's Razor and provide a simple, everyday example."
)
print(interaction.steps[-1].content[0].text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
model: "gemini-3-flash-preview",
input: "Explain the concept of Occam's Razor and provide a simple, everyday example."
});
console.log(interaction.steps.at(-1).content[0].text);
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3-flash-preview",
"input": "Explain the concept of Occam'\''s Razor and provide a simple example."
}'
想法摘要
思考摘要可深入瞭解模型的內部推論過程。根據預設,系統只會傳回最終輸出內容。你可以使用 thinking_summaries 啟用想法摘要:
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="What is the sum of the first 50 prime numbers?",
generation_config={
"thinking_summaries": "auto"
}
)
for step in interaction.steps:
if step.type == "thought":
print("Thought summary:")
for content_block in step.summary:
if content_block.type == "text":
print(content_block.text)
print()
elif step.type == "model_output":
for content_block in step.content:
if content_block.type == "text":
print("Answer:")
print(content_block.text)
print()
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
model: "gemini-3-flash-preview",
input: "What is the sum of the first 50 prime numbers?",
generationConfig: {
thinkingSummaries: "auto"
}
});
for (const step of interaction.steps) {
if (step.type === "thought") {
console.log("Thought summary:");
for (const contentBlock of step.summary) {
if (contentBlock.type === "text") console.log(contentBlock.text);
}
} else if (step.type === "model_output") {
for (const contentBlock of step.content) {
if (contentBlock.type === "text") {
console.log("Answer:");
console.log(contentBlock.text);
}
}
}
}
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3-flash-preview",
"input": "What is the sum of the first 50 prime numbers?",
"generationConfig": {
"thinkingSummaries": "auto"
}
}'
Stream with thinking
在生成期間使用串流功能,接收增量想法摘要。 這會傳回生成中的摘要,並以遞增方式更新:
Python
from google import genai
client = genai.Client()
prompt = """
Alice, Bob, and Carol each live in a different house on the same street: red, green, and blue.
Alice does not live in the red house.
Bob does not live in the green house.
Carol does not live in the red or green house.
Which house does each person live in?
"""
thoughts = ""
answer = ""
stream = client.interactions.create(
model="gemini-3-flash-preview",
input=prompt,
generation_config={
"thinking_summaries": "auto"
},
stream=True
)
thoughts = ""
answer = ""
for event in stream:
if event.event_type == "step.delta":
if event.delta.type == "thought":
if not thoughts:
print("Thinking...")
summary_text = getattr(event.delta, 'text', '')
print(f"[Thought] {summary_text}", end="")
thoughts += summary_text
elif event.delta.type == "text" and event.delta.text:
if not answer:
print("\nAnswer:")
print(event.delta.text, end="")
answer += event.delta.text
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const prompt = `Alice, Bob, and Carol each live in a different house on the same
street: red, green, and blue. Alice does not live in the red house.
Bob does not live in the green house.
Carol does not live in the red or green house.
Which house does each person live in?`;
let thoughts = "";
let answer = "";
const stream = await client.interactions.create({
model: "gemini-3-flash-preview",
input: prompt,
generationConfig: {
thinkingSummaries: "auto"
},
stream: true
});
for await (const event of stream) {
if (event.type === "step.delta") {
if (event.delta.type === "thought") {
if (!thoughts) console.log("Thinking...");
process.stdout.write(`[Thought] ${event.delta.text || ""}`);
thoughts += event.delta.text || "";
} else if (event.delta.type === "text" && event.delta.text) {
if (!answer) console.log("\nAnswer:");
process.stdout.write(event.delta.text);
answer += event.delta.text;
}
}
}
控制思考
Gemini 模型預設會進行動態思考,根據要求的複雜程度自動調整推理量。不過,您可以使用設定參數控制這項行為。
思考程度 (Gemini 3)
建議搭配 Gemini 3 模型和後續版本使用 thinking_level 參數,
控制推論行為。
| 思考程度 | Gemini 3.1 Pro | Gemini 3 Pro (已淘汰) | Gemini 3 Flash | 說明 |
|---|---|---|---|---|
minimal |
不支援 | 不支援 | 支援 | 與大多數查詢的「不思考」設定相符。模型可能會以極簡思維處理複雜的程式碼編寫工作。將聊天或高處理量應用程式的延遲時間降到最低。請注意,minimal 無法保證思考功能已關閉。 |
low |
支援 | 支援 | 支援 | 盡量縮短延遲時間並降低成本。最適合用於遵循指令、即時通訊或高總處理量應用程式。 |
medium |
支援 | 不支援 | 支援 | 適合處理大多數工作。 |
high |
支援 (預設、動態) | 支援 (預設、動態) | 支援 (預設、動態) | 盡可能深入推論。模型可能需要較長時間才能輸出第一個 (非思考) 輸出權杖,但輸出內容會經過更仔細的推理。 |
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Provide a list of 3 famous physicists and their key contributions",
generation_config={
"thinking_level": "low"
}
)
print(interaction.steps[-1].content[0].text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
model: "gemini-3-flash-preview",
input: "Provide a list of 3 famous physicists and their key contributions",
generationConfig: {
thinkingLevel: "low"
}
});
console.log(interaction.steps.at(-1).content[0].text);
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3-flash-preview",
"input": "Provide a list of 3 famous physicists and their key contributions",
"generation_config": {
"thinking_level": "low"
}
}'
你無法停用 Gemini 3 Pro 的思考功能。Gemini 3 Flash 也不支援完全關閉思考型功能,但 minimal 設定表示模型可能不會思考 (但仍有可能)。
思想簽名
Gemini API 是無狀態的,因此模型會獨立處理每個 API 要求,且無法存取多輪互動中先前回合的思考脈絡。
為確保多輪互動的思考脈絡一致,Gemini 會傳回思考簽章,這是模型內部思考過程的加密表示法。
- 啟用思考功能,且要求包含函式呼叫 (具體來說是函式宣告) 時,Gemini 2.5 模型會傳回思考簽章。
- Gemini 3 模型可能會傳回所有類型部分的思維簽章。建議您一律將所有簽章傳回,但函式呼叫簽章必須傳回。詳情請參閱「想法簽章」頁面。
- 簽章會與其他部分一起傳回,例如函式呼叫或文字部分。將整個回覆連同所有部分,在後續回合中傳回模型。
- 請勿將簽章與其他部分串連在一起。
- 請勿將有簽名的部分與沒有簽名的部分合併。
定價
開啟思考功能後,回覆價格為輸出詞元和思考詞元的總和。您可以從 total_thought_tokens 欄位取得產生的思考權杖總數。
Python
# ...
print("Thoughts tokens:", interaction.usage.total_thought_tokens)
print("Output tokens:", interaction.usage.total_output_tokens)
JavaScript
// ...
console.log(`Thoughts tokens: ${interaction.usage.totalThoughtTokens}`);
console.log(`Output tokens: ${interaction.usage.totalOutputTokens}`);
思考模型會生成完整想法,提升最終回覆的品質,然後輸出摘要,深入瞭解思考過程。即使 API 只會輸出摘要,但計價依據仍是模型需要產生的完整思考權杖。
如要進一步瞭解權杖,請參閱「權杖計數」指南。
最佳做法
本節將說明如何有效運用思考模型。
偵錯及引導
推論:如果思考型模型未提供預期回覆,請仔細分析 Gemini 的思考摘要。你可以查看 AI 如何分解工作並得出結論,然後根據這些資訊修正結果。
在推論過程中提供指引:如果您希望輸出內容特別長,不妨在提示詞中提供指引,限制模型思考的量。這樣一來,就能為回覆保留更多權杖輸出。
工作複雜度
- 簡單工作 (可關閉思考):對於不需要複雜推論的簡單要求,不需要思考。範例:
- 「DeepMind 是在哪裡成立的?」
- 「這封電子郵件是要求召開會議,還是只是提供資訊?」
- 中等難度的工作 (預設/需要思考):許多常見要求都需要逐步處理或深入瞭解。範例:
- 以光合作用和成長過程做類比。
- 比較電動車和油電混合車的異同。
- 困難工作 (最高思考能力):如要解決真正複雜的挑戰,建議設定高思考預算。這類工作需要模型發揮完整的推理和規劃能力。範例:
- 解決 2025 年 AIME 的問題 1:找出所有整數底數 b > 9 的總和...
- 編寫 Python 程式碼,建立可顯示即時股市資料的網頁應用程式,包括使用者驗證。盡可能提高效率。
支援的模型
| 型號 | 預設思考 | 支援的等級 |
|---|---|---|
| gemini-3.1-pro-preview | 開啟 (高) | 低、中、高 |
| gemini-3-flash-preview | 開啟 (高) | 極低、低、中、高 |
| gemini-3-pro-preview | 開啟 (高) | 低、高 |
| gemini-2.5-pro | 開啟 | 預算:128 到 32768 |
| gemini-2.5-flash | 開啟 | 預算:0 到 24576 |
| gemini-2.5-flash-lite | 關閉 | 預算:0 到 24576 |
Thinking 模型可搭配所有 Gemini 工具和功能。模型可藉此與外部系統互動、執行程式碼或存取即時資訊,並將結果納入推理過程。
後續步驟
- 生成文字:基本文字回覆
- 函式呼叫:連結至工具
- 思想特徵:管理多輪對話中的特徵
- Gemini 3 指南:模型專屬功能