Gemini 2.5 模型可使用內部「思考程序」,大幅提升推理和多步驟規劃能力,因此非常適合處理程式設計、高等數學和資料分析等複雜工作。
思考模型提供下列設定和選項:
思考預算:您可以透過思考預算,設定模型可進行的「思考」程度。如果減少延遲時間或降低成本是首要目標,這項設定就特別重要。此外,請參閱任務難度比較,決定模型可能需要多少思考能力。
想法摘要:您可以啟用想法摘要,讓系統在生成回覆時一併提供。這些摘要是模型原始想法的綜合版本,可深入瞭解模型的內部推理過程。
想法簽章:Firebase AI Logic SDK 會自動處理想法簽章,確保模型在呼叫函式時,能存取先前回合的想法脈絡。
請務必詳閱最佳做法和提示指南,瞭解如何使用思考模型。
使用思考模型
使用思考模型,就像使用任何其他 Gemini 模型一樣 (初始化所選的 Gemini API 提供者、建立 GenerativeModel
執行個體等)。這些模型可用於文字或程式碼生成工作,例如產生結構化輸出內容,或分析多模態輸入內容 (例如圖片、影片、音訊或 PDF)。串流輸出內容時,您甚至可以使用思考模型。
支援這項功能的模型
只有 Gemini 2.5 模型支援這項功能。
gemini-2.5-pro
gemini-2.5-flash
gemini-2.5-flash-lite
使用思考模型的最佳做法和提示指南
建議您在 Google AI Studio 或 Vertex AI Studio 中測試提示,查看完整的思考過程。找出模型可能出錯的地方,以便修正提示,取得更一致且準確的回覆。
先從描述期望結果的一般提示開始,然後觀察模型初步思考如何決定回覆內容。如果回覆不如預期,請使用下列提示技巧,協助模型生成更完善的回覆:
- 提供逐步說明
- 提供幾個輸入/輸出組合的範例
- 提供輸出內容和回覆的措辭和格式指引
- 提供具體的驗證步驟
除了提示之外,也建議您參考下列做法:
設定系統指令,這就像是「前言」,您可以在模型收到提示或使用者提供的任何其他指令之前,先加入這段文字。您可以根據特定需求和用途,引導模型行為。
設定思考預算,決定模型可思考的程度。如果預算較低,模型就不會「過度思考」回覆內容。如果預算設得較高,模型就能視需要進行更多思考。設定思考預算後,實際回覆可用的總詞元輸出上限也會增加。
在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 */ "GEMINI_MODEL_NAME",
/* 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
在建立 GenerativeModel
執行個體時,設定 GenerationConfig
中的參數值。
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
final thinkingConfig = ThinkingConfig(thinkingBudget: 1024);
final generationConfig = GenerationConfig(
thinkingConfig: thinkingConfig
);
// Specify the config as part of creating the `GenerativeModel` instance
final model = FirebaseAI.googleAI().generativeModel(
model: 'GEMINI_MODEL_NAME',
config: generationConfig,
);
// ...
Unity
建立 GenerativeModel
執行個體時,請在 GenerationConfig
中設定參數值。
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
var thinkingConfig = new ThinkingConfig(thinkingBudget: 1024);
var generationConfig = new GenerationConfig(
thinkingConfig: thinkingConfig
);
// Specify the config as part of creating the `GenerativeModel` instance
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
modelName: "GEMINI_MODEL_NAME",
generationConfig: generationConfig
);
// ...
支援的思考預算值
下表列出可為每個模型設定的思考預算值,方法是設定模型的 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.」(解決 2025 年 AIME 的第 1 題:找出所有大於 9 的整數底數 b,使 17b 是 97b 的因數,並計算這些底數的總和。)
- 「編寫網路應用程式的 Python 程式碼,以視覺化方式呈現即時股市資料,包括使用者驗證。盡可能提高效率。"
在回覆中納入想法摘要
想法摘要是模型原始想法的綜合版本,可深入瞭解模型的內部推論程序。
在回覆中加入想法摘要的好處如下:
您可以在應用程式的使用者介面中顯示想法摘要,或讓使用者存取這些摘要。系統會在回覆中以獨立部分的形式傳回想法摘要,方便您進一步控管摘要在應用程式中的使用方式。
如果您也在 Firebase 控制台中啟用 AI 監控功能,控制台就會顯示想法摘要,方便您檢查模型的詳細推理過程,進而偵錯及調整提示。
以下是關於想法摘要的幾項重要注意事項:
啟用想法摘要
按一下 Gemini API 供應商,即可在這個頁面查看供應商專屬內容和程式碼。 |
如要在模型設定中將 includeThoughts
設為 true,即可啟用想法摘要。接著,您可以查看回應中的 thoughtSummary
欄位,存取摘要。
以下範例說明如何啟用及擷取回覆中的想法摘要:
Swift
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
let generationConfig = GenerationConfig(
thinkingConfig: ThinkingConfig(includeThoughts: true)
)
// Specify the config as part of creating the `GenerativeModel` instance
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
modelName: "GEMINI_MODEL_NAME",
generationConfig: generationConfig
)
let response = try await model.generateContent("solve x^2 + 4x + 4 = 0")
// Handle the response that includes thought summaries
if let thoughtSummary = response.thoughtSummary {
print("Thought Summary: \(thoughtSummary)")
}
guard let text = response.text else {
fatalError("No text in response.")
}
print("Answer: \(text)")
Kotlin
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
val generationConfig = generationConfig {
thinkingConfig = thinkingConfig {
includeThoughts = true
}
}
// Specify the config as part of creating the `GenerativeModel` instance
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "GEMINI_MODEL_NAME",
generationConfig,
)
val response = model.generateContent("solve x^2 + 4x + 4 = 0")
// Handle the response that includes thought summaries
response.thoughtSummary?.let {
println("Thought Summary: $it")
}
response.text?.let {
println("Answer: $it")
}
Java
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
.setIncludeThoughts(true)
.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 */ "GEMINI_MODEL_NAME",
/* generationConfig */ generationConfig
);
);
// Handle the response that includes thought summaries
ListenableFuture responseFuture = model.generateContent("solve x^2 + 4x + 4 = 0");
Futures.addCallback(responseFuture, new FutureCallback() {
@Override
public void onSuccess(GenerateContentResponse response) {
if (response.getThoughtSummary() != null) {
System.out.println("Thought Summary: " + response.getThoughtSummary());
}
if (response.getText() != null) {
System.out.println("Answer: " + response.getText());
}
}
@Override
public void onFailure(Throwable t) {
// Handle error
}
}, MoreExecutors.directExecutor());
Web
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
const generationConfig = {
thinkingConfig: {
includeThoughts: true
}
};
// Specify the config as part of creating the `GenerativeModel` instance
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });
const result = await model.generateContent("solve x^2 + 4x + 4 = 0");
const response = result.response;
// Handle the response that includes thought summaries
if (response.thoughtSummary()) {
console.log(`Thought Summary: ${response.thoughtSummary()}`);
}
const text = response.text();
console.log(`Answer: ${text}`);
Dart
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
final thinkingConfig = ThinkingConfig(includeThoughts: true);
final generationConfig = GenerationConfig(
thinkingConfig: thinkingConfig
);
// Specify the config as part of creating the `GenerativeModel` instance
final model = FirebaseAI.googleAI().generativeModel(
model: 'GEMINI_MODEL_NAME',
generationConfig: generationConfig,
);
final response = await model.generateContent('solve x^2 + 4x + 4 = 0');
// Handle the response that includes thought summaries
if (response.thoughtSummary != null) {
print('Thought Summary: ${response.thoughtSummary}');
}
if (response.text != null) {
print('Answer: ${response.text}');
}
Unity
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
var thinkingConfig = new ThinkingConfig(includeThoughts: true);
var generationConfig = new GenerationConfig(
thinkingConfig: thinkingConfig
);
// Specify the config as part of creating the `GenerativeModel` instance
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
modelName: "GEMINI_MODEL_NAME",
generationConfig: generationConfig
);
var response = await model.GenerateContentAsync("solve x^2 + 4x + 4 = 0");
// Handle the response that includes thought summaries
if (response.ThoughtSummary != null) {
Debug.Log($"Thought Summary: {response.ThoughtSummary}");
}
if (response.Text != null) {
Debug.Log($"Answer: {response.Text}");
}
串流思維摘要
如果您選擇使用 generateContentStream
串流回覆,也可以查看想法摘要。這會在生成回覆期間傳回滾動式增量摘要。
Swift
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
let generationConfig = GenerationConfig(
thinkingConfig: ThinkingConfig(includeThoughts: true)
)
// Specify the config as part of creating the `GenerativeModel` instance
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
modelName: "GEMINI_MODEL_NAME",
generationConfig: generationConfig
)
let stream = try model.generateContentStream("solve x^2 + 4x + 4 = 0")
// Handle the streamed response that includes thought summaries
var thoughts = ""
var answer = ""
for try await response in stream {
if let thought = response.thoughtSummary {
if thoughts.isEmpty {
print("--- Thoughts Summary ---")
}
print(thought)
thoughts += thought
}
if let text = response.text {
if answer.isEmpty {
print("--- Answer ---")
}
print(text)
answer += text
}
}
Kotlin
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
val generationConfig = generationConfig {
thinkingConfig = thinkingConfig {
includeThoughts = true
}
}
// Specify the config as part of creating the `GenerativeModel` instance
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "GEMINI_MODEL_NAME",
generationConfig,
)
// Handle the streamed response that includes thought summaries
var thoughts = ""
var answer = ""
model.generateContentStream("solve x^2 + 4x + 4 = 0").collect { response ->
response.thoughtSummary?.let {
if (thoughts.isEmpty()) {
println("--- Thoughts Summary ---")
}
print(it)
thoughts += it
}
response.text?.let {
if (answer.isEmpty()) {
println("--- Answer ---")
}
print(it)
answer += it
}
}
Java
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
.setIncludeThoughts(true)
.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 */ "GEMINI_MODEL_NAME",
/* generationConfig */ generationConfig
);
);
// Streaming with Java is complex and depends on the async library used.
// This is a conceptual example using a reactive stream.
Flowable responseStream = model.generateContentStream("solve x^2 + 4x + 4 = 0");
// Handle the streamed response that includes thought summaries
StringBuilder thoughts = new StringBuilder();
StringBuilder answer = new StringBuilder();
responseStream.subscribe(response -> {
if (response.getThoughtSummary() != null) {
if (thoughts.length() == 0) {
System.out.println("--- Thoughts Summary ---");
}
System.out.print(response.getThoughtSummary());
thoughts.append(response.getThoughtSummary());
}
if (response.getText() != null) {
if (answer.length() == 0) {
System.out.println("--- Answer ---");
}
System.out.print(response.getText());
answer.append(response.getText());
}
}, throwable -> {
// Handle error
});
Web
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
const generationConfig = {
thinkingConfig: {
includeThoughts: true
}
};
// Specify the config as part of creating the `GenerativeModel` instance
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });
const result = await model.generateContentStream("solve x^2 + 4x + 4 = 0");
// Handle the streamed response that includes thought summaries
let thoughts = "";
let answer = "";
for await (const chunk of result.stream) {
if (chunk.thoughtSummary()) {
if (thoughts === "") {
console.log("--- Thoughts Summary ---");
}
// In Node.js, process.stdout.write(chunk.thoughtSummary()) could be used
// to avoid extra newlines.
console.log(chunk.thoughtSummary());
thoughts += chunk.thoughtSummary();
}
const text = chunk.text();
if (text) {
if (answer === "") {
console.log("--- Answer ---");
}
// In Node.js, process.stdout.write(text) could be used.
console.log(text);
answer += text;
}
}
Dart
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
final thinkingConfig = ThinkingConfig(includeThoughts: true);
final generationConfig = GenerationConfig(
thinkingConfig: thinkingConfig
);
// Specify the config as part of creating the `GenerativeModel` instance
final model = FirebaseAI.googleAI().generativeModel(
model: 'GEMINI_MODEL_NAME',
generationConfig: generationConfig,
);
final responses = model.generateContentStream('solve x^2 + 4x + 4 = 0');
// Handle the streamed response that includes thought summaries
var thoughts = '';
var answer = '';
await for (final response in responses) {
if (response.thoughtSummary != null) {
if (thoughts.isEmpty) {
print('--- Thoughts Summary ---');
}
thoughts += response.thoughtSummary!;
}
if (response.text != null) {
if (answer.isEmpty) {
print('--- Answer ---');
}
answer += response.text!;
}
}
Unity
在建立 GenerativeModel
執行個體的過程中,啟用 GenerationConfig
中的想法摘要。
// ...
// Set the thinking configuration
// Optionally enable thought summaries in the generated response (default is false)
var thinkingConfig = new ThinkingConfig(includeThoughts: true);
var generationConfig = new GenerationConfig(
thinkingConfig: thinkingConfig
);
// Specify the config as part of creating the `GenerativeModel` instance
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
modelName: "GEMINI_MODEL_NAME",
generationConfig: generationConfig
);
var stream = model.GenerateContentStreamAsync("solve x^2 + 4x + 4 = 0");
// Handle the streamed response that includes thought summaries
var thoughts = "";
var answer = "";
await foreach (var response in stream)
{
if (response.ThoughtSummary != null)
{
if (string.IsNullOrEmpty(thoughts))
{
Debug.Log("--- Thoughts Summary ---");
}
Debug.Log(response.ThoughtSummary);
thoughts += response.ThoughtSummary;
}
if (response.Text != null)
{
if (string.IsNullOrEmpty(answer))
{
Debug.Log("--- Answer ---");
}
Debug.Log(response.Text);
answer += response.Text;
}
}
瞭解想法簽章
在多輪互動中使用「思考」功能時,模型無法存取前幾輪的思考內容。不過,如果您使用函式呼叫,可以運用想法簽章,在不同回合之間維持想法脈絡。「想法簽章」是模型內部思考過程的加密表示法,使用「思考」和函式呼叫時會提供這類簽章。具體來說,系統會在下列情況生成想法簽章:
- 已啟用思考功能並生成想法。
- 要求包含函式宣告。
如要使用思維簽章,請照常呼叫函式。
Firebase AI Logic SDK 會管理狀態並自動處理思想簽章,簡化整個程序。在 Chat
工作階段中,SDK 會自動在後續的 sendMessage
或 sendMessageStream
呼叫之間傳遞任何產生的思緒簽章。
價格和計算詞元
思考權杖的價格與文字輸出權杖相同。啟用想法摘要後,系統會將其視為思考權杖,並據此計價。
您可以在 Firebase 控制台中啟用 AI 監控,監控啟用思考功能的要求所用的思考權杖數量。
您可以從回應的 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
// ...
final response = await model.generateContent(
Content.text("Why is the sky blue?"),
]);
if (response?.usageMetadata case final usageMetadata?) {
print("Thoughts Token Count: ${usageMetadata.thoughtsTokenCount}");
}
Unity
// ...
var response = await model.GenerateContentAsync("Why is the sky blue?");
if (response.UsageMetadata != null)
{
UnityEngine.Debug.Log($"Thoughts Token Count: {response.UsageMetadata?.ThoughtsTokenCount}");
}
如要進一步瞭解權杖,請參閱計算權杖指南。