Gemini 正在思考
Gemini 3 和 2.5 系列模型採用「思考過程」,大幅提升推論和多步驟規劃能力,因此非常適合處理複雜工作,例如程式設計、高等數學和資料分析。
使用思考型模型時,Gemini 會先在內部進行推論,再做出回應。Interactions API 會透過 thought 步驟顯示這項推理過程,這些專屬步驟會依時間順序顯示在 steps 陣列中,與函式呼叫、使用者輸入內容或模型輸出內容並列。
每個思考步驟都包含兩個欄位:
| 欄位 | 必要 | 說明 |
|---|---|---|
signature |
✅ 是 | 模型內部推論狀態的加密表示法。一律會顯示,即使模型只執行最少的推論作業。 |
summary |
❌ 否 | 總結推論過程的內容陣列 (文字和/或圖片)。視 thinking_summaries 設定、模型是否執行足夠的推論,或內容類型而定,可能為空白 (例如,圖像潛在空間可能沒有文字摘要)。 |
與思考過程的互動
啟動與思考型模型的互動,與其他互動要求類似。在 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 "Api-Revision: 2026-05-20" \
-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?",
generation_config: {
thinking_summaries: "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 "Api-Revision: 2026-05-20" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3-flash-preview",
"input": "What is the sum of the first 50 prime numbers?",
"generation_config": {
"thinking_summaries": "auto"
}
}'
在下列情況下,想法區塊只能包含簽名,不得有摘要:
- 簡單要求,模型未充分推理,無法生成摘要
thinking_summaries: "none",明確停用摘要功能- 部分想法內容類型 (例如圖片) 可能沒有文字摘要
您的程式碼應一律處理 summary 為空或不存在的思考區塊。
串流播放並思考
在生成期間使用串流功能,接收增量想法摘要。 系統會使用伺服器傳送事件 (SSE) 傳送思維方塊,並提供兩種不同的差異類型:
| Delta 類型 | 包含 | 傳送時間 |
|---|---|---|
thought_summary |
文字或圖片摘要內容 | 一或多個增量摘要的差異 |
thought_signature |
加密簽章 | step.stop之前的最後一個增量 |
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
)
for event in stream:
if event.event_type == "step.delta":
if event.delta.type == "thought_summary":
if not thoughts:
print("Thinking...")
summary_text = event.delta.content.get('text', '') if hasattr(event.delta, 'content') else 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,
generation_config: {
thinking_summaries: "auto"
},
stream: true
});
for await (const event of stream) {
if (event.event_type === "step.delta") {
if (event.delta.type === "thought_summary") {
if (!thoughts) console.log("Thinking...");
const text = event.delta.content?.text || "";
process.stdout.write(`[Thought] ${text}`);
thoughts += 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;
}
}
}
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Api-Revision: 2026-05-20" \
-H 'Content-Type: application/json' \
--no-buffer \
-d '{
"model": "gemini-3-flash-preview",
"input": "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?",
"generation_config": {
"thinking_summaries": "auto"
},
"stream": true
}'
串流回應會使用伺服器傳送事件 (SSE),並由步驟和事件組成。請參閱下方的範例。
event: interaction.created
data: {"interaction":{"id":"v1_xxx","status":"in_progress","object":"interaction","model":"gemini-3-flash-preview"},"event_type":"interaction.created"}
event: step.start
data: {"index":0,"step":{"signature":"","summary":[{"text":"**Evaluating the clues**\n\nI'm considering...","type":"text"}],"type":"thought"},"event_type":"step.start"}
event: step.delta
data: {"index":0,"delta":{"signature":"EpoGCpcGAXLI2nx/...","type":"thought_signature"},"event_type":"step.delta"}
event: step.stop
data: {"index":0,"event_type":"step.stop"}
event: step.start
data: {"index":1,"step":{"content":[{"text":"Based on the clues provided, here","type":"text"}],"type":"model_output"},"event_type":"step.start"}
event: step.delta
data: {"index":1,"delta":{"text":" is the answer to your question...","type":"text"},"event_type":"step.delta"}
event: step.stop
data: {"index":1,"event_type":"step.stop"}
event: interaction.completed
data: {"interaction":{"id":"v1_xxx","status":"completed","usage":{"total_tokens":530,"total_input_tokens":62,"total_output_tokens":171,"total_thought_tokens":297}},"event_type":"interaction.completed"}
event: done
data: [DONE]
控制思考
Gemini 模型預設會進行動態思考,根據要求的複雜程度自動調整推理量。您可以使用 thinking_level 參數控制這項行為。
| 型號 | 預設思考 | 支援的等級 |
|---|---|---|
| gemini-3.1-pro-preview | 開啟 (高) | 低、中、高 |
| gemini-3-flash-preview | 開啟 (高) | 極低、低、中、高 |
| gemini-3-pro-preview | 開啟 (高) | 低、高 |
| gemini-2.5-pro | 開啟 | 低、中、高 |
| gemini-2.5-flash | 開啟 | 低、中、高 |
| gemini-2.5-flash-lite | 關閉 | 低、中、高 |
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",
generation_config: {
thinking_level: "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 "Api-Revision: 2026-05-20" \
-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"
}
}'
思想簽名
想法簽章是模型內部推論過程的加密表示法。他們必須在多輪互動中維持推論連續性。
與 generateContent API 相比,Interactions API 可大幅簡化處理思緒簽章的程序。
有狀態模式 (建議)
根據預設,在有狀態模式下使用 Interactions API 時 (設定 store: true 並在後續回合中傳遞 previous_interaction_id),伺服器會自動管理對話狀態,包括所有想法區塊和簽章。在這個模式下,您不需要對簽章採取任何行動。這些作業完全在伺服器端處理。
無狀態模式
如果您自行管理對話狀態 (無狀態模式),並在每個要求中傳遞完整的輸入和輸出記錄:
- 您必須一律重新傳送所有
thought區塊,且內容必須與模型傳送的完全一致。 - 請勿從記錄中移除或修改思維方塊,因為這些方塊包含模型繼續推論所需的簽章。
- 在工作階段中切換模型時,您仍應重新傳送先前模型的思考區塊。後端會管理相容性。
定價
開啟思考功能後,回覆價格為輸出詞元和思考詞元的總和。您可以從 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 只會輸出摘要,但計價依據仍是模型需要產生的完整思考權杖。
如要進一步瞭解權杖,請參閱「權杖計數」指南。
最佳做法
請按照下列指南,有效運用思考模型。
- 檢閱推論:分析想法摘要,瞭解失敗原因並改善提示。
- 控管思考預算:提示模型減少思考,以節省詞元。
- 簡單工作:用於事實擷取或分類,不需經過太多思考 (例如「DeepMind 在哪裡成立?」)。
- 中等難度的工作:使用預設的思考方式比較概念或進行創意推論 (例如比較電動車和油電混合車)。
- 複雜工作:運用最高思考力處理進階程式設計、數學或多步驟規劃工作 (例如:解決美國數學邀請賽 (AIME) 的數學問題)。
後續步驟
- 生成文字:基本文字回覆
- 函式呼叫:連結至工具
- Gemini 3 指南:模型專屬功能