Gemini 2.5 模型可使用內部「思考程序」,大幅提升推理和多步驟規劃能力,因此非常適合處理程式設計、高等數學和資料分析等複雜工作。
您可以透過思考預算,設定模型可進行的思考量。如果減少延遲或降低成本是首要目標,這項設定就特別重要。此外,請參閱任務難度比較,決定模型可能需要多少思考能力。
使用思考模型
使用思考模型,就像使用任何其他 Gemini 模型一樣 (初始化所選的 Gemini API 提供者、建立 GenerativeModel
執行個體等)。這些模型可用於文字或程式碼生成工作,例如產生結構化輸出內容,或分析多模態輸入內容 (例如圖片、影片、音訊或 PDF)。串流輸出內容時,您甚至可以使用思考模型。
支援的模型
只有 Gemini 2.5 模型支援這項功能。
gemini-2.5-pro
gemini-2.5-flash
gemini-2.5-flash-lite-preview-06-17
使用思考模型的最佳做法
建議您在 Google AI Studio 或 Vertex AI Studio 中測試提示,查看完整的思考過程。找出模型可能出錯的地方,以便修正提示,取得更一致且準確的回覆。
先提供一般提示,說明期望的結果,然後觀察模型初步思考如何決定回覆內容。如果回覆不如預期,請使用下列提示技巧,協助模型生成更完善的回覆:
- 提供逐步說明
- 提供幾個輸入/輸出組合的範例
- 提供輸出內容和回覆的措辭和格式指引
- 提供具體的驗證步驟
除了提示之外,建議您也參考下列做法:
設定系統指令,這就像是「前言」,您可以在模型收到提示或使用者提供的任何其他指令之前,先加入這段文字。您可以根據特定需求和用途,引導模型行為。
設定思考預算,決定模型可思考的程度。如果預算較低,模型就不會「過度思考」回覆內容。如果預算設得較高,模型就能視需要進行更多思考。設定思考預算後,實際回覆可用的總詞元輸出上限也會增加。
(如果使用 Vertex AI Gemini API) 請務必在 Firebase 控制台中啟用 AI 監控,這樣才能監控已啟用思考功能的請求延遲時間。請注意,監控資訊主頁目前不會顯示思考詞元。
控管思考預算
如要控管模型生成回覆時的思考程度,可以指定模型可使用的思考預算詞元數量。
如果需要比預設思考預算更多或更少的權杖,可以手動設定思考預算。如要進一步瞭解工作複雜度和建議預算,請參閱本節稍後內容。以下提供一些高階指引:
- 如果延遲時間很重要,或是要執行較不複雜的工作,請設定較低的思考預算
- 為較複雜的工作設定較高的思考預算
設定思考預算
按一下 Gemini API 供應商,即可在這個頁面查看供應商專屬內容和程式碼。 |
在建立 GenerativeModel
例項時,請在 GenerationConfig
中設定思考預算。這項設定在執行個體的生命週期內都會維持不變。如要為不同要求使用不同的思考預算,請建立以各預算設定的 GenerativeModel
執行個體。
如要瞭解支援的思考預算值,請參閱本節後續內容。
Swift
在建立 GenerativeModel
執行個體的過程中,設定 GenerationConfig
的思考預算。
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
let generationConfig = GenerationConfig(
thinkingConfig: ThinkingConfig(thinkingBudget: 1024)
)
// Specify the config as part of creating the `GenerativeModel` instance
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
modelName: "GEMINI_MODEL_NAME",
generationConfig: generationConfig
)
// ...
Kotlin
建立 GenerativeModel
執行個體時,請在 GenerationConfig
中設定參數值。
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
val generationConfig = generationConfig {
thinkingConfig = thinkingConfig {
thinkingBudget = 1024
}
}
// Specify the config as part of creating the `GenerativeModel` instance
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "GEMINI_MODEL_NAME",
generationConfig,
)
// ...
Java
建立 GenerativeModel
執行個體時,請在 GenerationConfig
中設定參數值。
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
.setThinkingBudget(1024)
.build();
GenerationConfig generationConfig = GenerationConfig.builder()
.setThinkingConfig(thinkingConfig)
.build();
// Specify the config as part of creating the `GenerativeModel` instance
GenerativeModelFutures model = GenerativeModelFutures.from(
FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel(
/* modelName */ "<var>GEMINI_MODEL_NAME</var>",
/* generationConfig */ generationConfig
);
);
// ...
Web
建立 GenerativeModel
執行個體時,請在 GenerationConfig
中設定參數值。
// ...
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
const generationConfig = {
thinkingConfig: {
thinkingBudget: 1024
}
};
// Specify the config as part of creating the `GenerativeModel` instance
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });
// ...
Dart
我們將在下一個版本中,支援為 Flutter 設定思考預算。
Unity
我們將在下一個版本中支援為 Unity 設定思考預算。
支援的思考預算值
下表列出可為每個模型設定的思考預算值,方法是設定模型的 thinkingBudget
。
模型 | 預設值 | 思考預算的可用範圍 |
要 停用思考的值 |
價值: 培養動態思維 |
|
---|---|---|---|---|---|
最小值 | 最大值 | ||||
Gemini 2.5 Pro | 8,192 |
128 |
32,768 |
無法關閉 | -1 |
Gemini 2.5 Flash | 8,192 |
1 |
24,576 |
0 |
-1 |
Gemini 2.5 Flash‑Lite | 0 (預設為停用思考功能) |
512 |
24,576 |
0 (或完全不設定思考預算) |
-1 |
停用思考功能
對於某些較簡單的工作,不需要思考能力,傳統推論就已足夠。或者,如果減少延遲時間是首要考量,您可能不希望模型花費超過必要的時間來生成回覆。
在這些情況下,你可以停用「思考」功能:
- Gemini 2.5 Pro:無法停用思考功能
- Gemini 2.5 Flash:將
thinkingBudget
設為0
個權杖 - Gemini 2.5 Flash-Lite:預設停用思考功能
啟用動態思考
您可以將 thinkingBudget
設為 -1
,讓模型決定思考的時間和程度 (稱為動態思考)。模型可視需要使用任意數量的權杖,但不得超過上述權杖上限值。
工作複雜度
簡單工作 - 可關閉思考功能
不需要複雜推理的簡單要求,例如事實擷取或分類。範例:- 「DeepMind 是在哪裡成立的?」
- 「這封電子郵件是要安排會議,還是只是提供資訊?」
中等難度的工作 - 需要預設預算或一些額外的思考預算
常見要求,需要逐步處理或深入瞭解。範例:- 「請用類比的方式說明光合作用和成長之間的關係。」
- 「比較電動車和油電混合車的異同。」
困難的工作 - 可能需要最高思考預算
真正複雜的挑戰,例如解決複雜的數學問題或編寫程式碼工作。這類工作需要模型充分運用推理和規劃能力,通常要經過許多內部步驟才會提供答案。範例:- 「Solve problem 1 in AIME 2025: Find the sum of all integer bases b > 9 for which 17b is a divisor of 97b.」
- 「編寫網路應用程式的 Python 程式碼,以視覺化方式呈現即時股市資料,包括使用者驗證。盡可能提高效率。"
價格和計算詞元
思考權杖的價格與文字輸出權杖相同。
您可以從回應的 usageMetadata
屬性中的 thoughtsTokenCount
欄位取得思考詞元總數:
Swift
// ...
let response = try await model.generateContent("Why is the sky blue?")
if let usageMetadata = response.usageMetadata {
print("Thoughts Token Count: \(usageMetadata.thoughtsTokenCount)")
}
Kotlin
// ...
val response = model.generateContent("Why is the sky blue?")
response.usageMetadata?.let { usageMetadata ->
println("Thoughts Token Count: ${usageMetadata.thoughtsTokenCount}")
}
Java
// ...
ListenableFuture<GenerateContentResponse> response =
model.generateContent("Why is the sky blue?");
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String usageMetadata = result.getUsageMetadata();
if (usageMetadata != null) {
System.out.println("Thoughts Token Count: " +
usageMetadata.getThoughtsTokenCount());
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
// ...
const response = await model.generateContent("Why is the sky blue?");
if (response?.usageMetadata?.thoughtsTokenCount != null) {
console.log(`Thoughts Token Count: ${response.usageMetadata.thoughtsTokenCount}`);
}
Dart
我們將在下一個版本中支援 Flutter 的思考權杖計數。
Unity
我們將在下一個版本中支援 Unity 的思考權杖計數。
如要進一步瞭解權杖,請參閱計算權杖指南。