Skip to content

Commit 35f4151

Browse files
authored
[Firebase AI] Add support for Thinking budget (#1287)
* [Firebase AI] Add logic for thinking budget * Update readme.md * Update GenerationConfig.cs * Update readme.md
1 parent d76e5fa commit 35f4151

File tree

5 files changed

+261
-166
lines changed

5 files changed

+261
-166
lines changed

docs/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ Release Notes
117117
- Auth: Removed deprecated `FirebaseUser.UpdateEmailAsync`.
118118
- Firebase AI: Add support for image generation via Imagen. For more info, see
119119
https://firebase.google.com/docs/ai-logic/generate-images-imagen
120+
- Firebase AI: Add support for Grounding with Google Search.
121+
- Firebase AI: Add support for defining a Thinking budget.
120122
- Firebase AI: Deprecated `CountTokensResponse.TotalBillableCharacters`, use
121123
`CountTokensResponse.TotalTokens` instead.
122124
- Messaging: Removed deprecated `FirebaseMessage.Dispose`,

firebaseai/src/GenerateContentResponse.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,17 @@ public readonly struct UsageMetadata {
444444
/// </summary>
445445
public int CandidatesTokenCount { get; }
446446
/// <summary>
447+
/// The number of tokens used by the model's internal "thinking" process.
448+
///
449+
/// For models that support thinking (like Gemini 2.5 Pro and Flash), this represents the actual
450+
/// number of tokens consumed for reasoning before the model generated a response. For models
451+
/// that do not support thinking, this value will be `0`.
452+
///
453+
/// When thinking is used, this count will be less than or equal to the `thinkingBudget` set in
454+
/// the `ThinkingConfig`.
455+
/// </summary>
456+
public int ThoughtsTokenCount { get; }
457+
/// <summary>
447458
/// The total number of tokens in both the request and response.
448459
/// </summary>
449460
public int TotalTokenCount { get; }
@@ -463,10 +474,11 @@ public IEnumerable<ModalityTokenCount> CandidatesTokensDetails {
463474
}
464475

465476
// Hidden constructor, users don't need to make this.
466-
private UsageMetadata(int promptTC, int candidatesTC, int totalTC,
477+
private UsageMetadata(int promptTC, int candidatesTC, int thoughtsTC, int totalTC,
467478
List<ModalityTokenCount> promptDetails, List<ModalityTokenCount> candidateDetails) {
468479
PromptTokenCount = promptTC;
469480
CandidatesTokenCount = candidatesTC;
481+
ThoughtsTokenCount = thoughtsTC;
470482
TotalTokenCount = totalTC;
471483
_promptTokensDetails =
472484
new ReadOnlyCollection<ModalityTokenCount>(promptDetails ?? new List<ModalityTokenCount>());
@@ -483,6 +495,7 @@ internal static UsageMetadata FromJson(Dictionary<string, object> jsonDict) {
483495
return new UsageMetadata(
484496
jsonDict.ParseValue<int>("promptTokenCount"),
485497
jsonDict.ParseValue<int>("candidatesTokenCount"),
498+
jsonDict.ParseValue<int>("thoughtsTokenCount"),
486499
jsonDict.ParseValue<int>("totalTokenCount"),
487500
jsonDict.ParseObjectList("promptTokensDetails", ModalityTokenCount.FromJson),
488501
jsonDict.ParseObjectList("candidatesTokensDetails", ModalityTokenCount.FromJson));

0 commit comments

Comments
 (0)