思考型

所有 Gemini 模型都採用內部「思考程序」,大幅提升推論和多步驟規劃能力,因此能有效處理程式設計、進階數學和資料分析等複雜工作。

思考模型提供下列設定和選項:

  • 控制思考量
    您可以設定思考等級,控制模型的「思考」量。如果優先考量是降低延遲時間或成本,這項設定就特別重要。此外,請參閱工作難度比較,判斷模型可能需要多少思考能力。

  • 取得思考摘要
    您可以啟用思考摘要,並將其納入生成的回覆中。這些摘要是模型原始思考過程的綜合版本,可讓您深入瞭解模型的內部推論過程。

  • 處理思考簽章
    Firebase AI Logic SDK 會自動為您處理思考簽章,確保模型能存取先前對話的思考脈絡,特別是在使用函式呼叫時。

請務必詳閱使用思考模型的最佳做法和提示指南。



使用思考模型的最佳做法和提示指南

建議您在 Google AI Studio 或 Agent  Studio 測試提示,以便查看完整的思考過程。您可以找出模型可能出錯的部分,並調整提示,取得更一致且準確的回覆。

先輸入一般提示詞,說明所需結果,並觀察模型如何初步思考,決定回覆內容。如果回覆不符合預期,請使用下列任一提示技巧,協助模型生成更優質的回覆:

  • 提供逐步說明
  • 提供幾個輸入/輸出組合的範例
  • 提供輸出內容和回覆的措辭和格式指南
  • 提供具體驗證步驟

除了提示之外,也建議您採用下列做法:

  • 設定系統指令,這就像是「前導提示」,在模型接觸到提示詞或使用者提供的任何其他指令之前,先加入這組指令。您可以根據特定需求和用途,引導模型的行為。

  • 設定思考程度,控制模型可進行的思考量。如果設定較高,模型就能在需要時進行更多思考。如果設定較低的值,模型就不會「過度思考」回覆,也會保留更多總輸出詞元數量上限,用於實際回覆,並有助於降低延遲和成本。

  • 在 Firebase 控制台中啟用 AI monitoring ,即可監控思考詞元數量,以及啟用思考功能 的要求延遲時間。如果啟用思考摘要,控制台會顯示摘要,方便您查看模型的詳細推論,協助偵錯及調整提示。



控管思考量

您可以設定思考等級,控制模型在回覆前可進行的「思考」和推論程度。如果優先考量的是降低延遲時間或成本,這項設定就特別重要。

請務必查看工作難度比較,判斷模型可能需要多少思考能力。以下提供一些概略指引:

  • 如果工作較不複雜,或您優先考量的是縮短延遲時間或降低成本,請設定較低的思考值。
  • 執行複雜的工作時,請調高思考值。

思考程度

如要控制模型生成回覆時的思考程度,可以指定思考程度,限制模型可使用的思考詞元數量。

設定思考程度

按一下您的Gemini API供應商,即可在這個頁面查看供應商專屬內容和程式碼。

在建立 GenerativeModel 執行個體時,於 GenerationConfig 中設定思考程度。設定會在執行個體的生命週期內保留。如要針對不同要求使用不同思考等級,請建立 GenerativeModel 執行個體,並設定各個等級。

請參閱本節後續內容,瞭解思考程度支援的值。

Swift

在建立 GenerativeModel 執行個體時,於 GenerationConfig 中設定思考程度。


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(thinkingLevel: .low)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_3.x_MODEL_NAME",
  generationConfig: generationConfig
)

// ...

Kotlin

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      thinkingLevel = ThinkingLevel.LOW
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_3.x_MODEL_NAME",
  generationConfig,
)

// ...

Java

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setThinkingLevel(ThinkingLevel.LOW)
    .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_3.x_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// ...

Web

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
const generationConfig = {
  thinkingConfig: {
    thinkingLevel: ThinkingLevel.LOW
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_3.x_MODEL_NAME", generationConfig });

// ...

Dart

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
final thinkingConfig = ThinkingConfig.withThinkingLevel(ThinkingLevel.low);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_3.x_MODEL_NAME',
  generationConfig: generationConfig,
);

// ...

Unity

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
var thinkingConfig = new ThinkingConfig(thinkingLevel: ThinkingLevel.Low);

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_3.x_MODEL_NAME",
  generationConfig: generationConfig
);

// ...

支援的思考程度值

下表列出您可以設定模型 thinkingLevel,為各模型設定的思考等級值。

MINIMAL LOW MEDIUM HIGH

模型會盡可能減少使用的詞元數量,幾乎不思考

低複雜度任務

模型使用的權杖數量較少,可盡量縮短延遲時間並降低成本

簡單任務和高處理量任務

模型採用平衡做法

中等複雜度工作

模型使用的詞元數量達到上限

需要深入推論的複雜提示

Gemini 4 Argon
gemini-4-argon
1 (預設)
Gemini 3.x Pro
gemini-3.1-pro-preview
(預設)
Gemini 3.x Flash
gemini-3.8-flash
gemini-3.7-flash
gemini-3.6-flash
gemini-3.5-flash

gemini-3.8-flash 或 gemini-3.7-flash 不支援 1
(預設)
Gemini 3.x Flash‑Lite
gemini-3.5-flash-lite
gemini-3.1-flash-lite
(預設)
Gemini Nano Banana 2.1
gemini-nano-banana-2.1
(預設)
Gemini Nano Banana 2
gemini-3.1-flash-image
(預設)
Gemini Nano Banana 2 Lite
gemini-3.1-flash-lite-image
(預設)
Gemini Nano Banana Pro
gemini-3-pro-image
(預設)

1 gemini-4-argon、gemini-3.8-flash 和 gemini-3.7-flash 不支援 MINIMAL 思考程度。如果將思考程度設為 MINIMAL,要求會失敗並傳回 400 錯誤。



思考預算 (Gemini 2.5 模型)

如要控管 Gemini 2.5 模型生成回覆時的思考程度,可以指定思考預算,限制模型可使用的思考詞元數量。



所有思考模型的工作複雜度

  • 簡單任務:不需要太多思考
    不需要複雜推論的簡單要求,例如檢索事實或分類。範例:

    • 「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 程式碼,以視覺化方式呈現即時股市資料,包括使用者驗證。請盡可能提高效率。」



想法重點摘要

思考摘要是模型原始想法的合成版本,可深入瞭解模型的內部推論過程。

在回覆中加入思考摘要有以下好處:

  • 您可以在應用程式的使用者介面中顯示思考摘要,或讓使用者存取。思考摘要會以獨立部分的形式傳回,方便您控管在應用程式中的使用方式。

  • 如果您也在 Firebase 控制台中啟用 AI monitoring,控制台就會顯示思考摘要,方便您查看模型的詳細推論,協助您偵錯及調整提示。

以下是思考摘要的重要注意事項:

  • 思考摘要不受思考預算控制 (預算僅適用於模型的原始思考)。不過,如果停用思考功能,模型就不會傳回思考摘要。

  • 思考摘要會視為模型一般生成的文字回覆,並計為輸出詞元。

啟用思考過程摘要

按一下您的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([Content.text('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([Content.text('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 會管理狀態,並自動處理思考簽章,簡化流程。SDK 會在 Chat 工作階段中,自動將產生的思考簽章傳遞至後續的 sendMessage 或 sendMessageStream 呼叫。



思考詞元的計價和計算方式

思考詞元的定價與文字輸出詞元相同。如果啟用思考摘要,系統會將其視為思考詞元,並據此計費。

您可以在 Firebase 控制台啟用 AI monitoring,監控已啟用思考功能的要求,查看思考詞元數量。

您可以從回應 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}");
}

如要進一步瞭解權杖,請參閱計算權杖指南。