feat(ai): Add support for Thinking Budget (#9156) · firebase/firebase-js-sdk@6ab4e13 (original) (raw)

10 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1 +---
2 +'firebase': minor
3 +'@firebase/ai': minor
4 +---
5 +
6 +Add support for Thinking Budget.
Original file line number Diff line number Diff line change
@@ -381,6 +381,7 @@ export interface GenerationConfig {
381 381 stopSequences?: string[];
382 382 // (undocumented)
383 383 temperature?: number;
384 + thinkingConfig?: ThinkingConfig;
384 385 // (undocumented)
385 386 topK?: number;
386 387 // (undocumented)
@@ -925,6 +926,11 @@ export interface TextPart {
925 926 text: string;
926 927 }
927 928
929 +// @public
930 +export interface ThinkingConfig {
931 + thinkingBudget?: number;
932 +}
933 +
928 934 // @public
929 935 export type Tool = FunctionDeclarationsTool | GoogleSearchTool;
930 936
@@ -947,6 +953,7 @@ export interface UsageMetadata {
947 953 promptTokenCount: number;
948 954 // (undocumented)
949 955 promptTokensDetails?: ModalityTokenCount[];
956 + thoughtsTokenCount?: number;
950 957 // (undocumented)
951 958 totalTokenCount: number;
952 959 }
Original file line number Diff line number Diff line change
@@ -148,6 +148,8 @@ toc:
148 148 path: /docs/reference/js/ai.stringschema.md
149 149 - title: TextPart
150 150 path: /docs/reference/js/ai.textpart.md
151 + - title: ThinkingConfig
152 +path: /docs/reference/js/ai.thinkingconfig.md
151 153 - title: ToolConfig
152 154 path: /docs/reference/js/ai.toolconfig.md
153 155 - title: UsageMetadata

| Original file line number | Diff line number | Diff line change | | | | | --------------------------------------------------- | ------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | @@ -31,6 +31,7 @@ export interface GenerationConfig | | | | | | | 31 | 31 | | [responseSchema](./ai.generationconfig.md#generationconfigresponseschema) | [TypedSchema](./ai.md#typedschema) \| [SchemaRequest](./ai.schemarequest.md#schemarequest_interface) | Output response schema of the generated candidate text. This value can be a class generated with a [Schema](./ai.schema.md#schema_class) static method like <code>Schema.string()</code> or <code>Schema.object()</code> or it can be a plain JS object matching the [SchemaRequest](./ai.schemarequest.md#schemarequest_interface) interface.
Note: This only applies when the specified <code>responseMIMEType</code> supports a schema; currently this is limited to <code>application/json</code> and <code>text/x.enum</code>. | | | 32 | 32 | | [stopSequences](./ai.generationconfig.md#generationconfigstopsequences) | string\[\] | | | | 33 | 33 | | [temperature](./ai.generationconfig.md#generationconfigtemperature) | number | | | | | 34 | +| [thinkingConfig](./ai.generationconfig.md#generationconfigthinkingconfig) | [ThinkingConfig](./ai.thinkingconfig.md#thinkingconfig_interface) | Configuration for "thinking" behavior of compatible Gemini models. | | | | 34 | 35 | | [topK](./ai.generationconfig.md#generationconfigtopk) | number | | | | 35 | 36 | | [topP](./ai.generationconfig.md#generationconfigtopp) | number | | | | 36 | 37 | | | | | | @@ -117,6 +118,16 @@ stopSequences?: string[]; | | | | | | | 117 | 118 | temperature?: number; | | | | | 118 | 119 | ``` | | | | | 119 | 120 | | | | | | | 121 | +## GenerationConfig.thinkingConfig | | | | | | | 122 | + | | | | | | | 123 | +Configuration for "thinking" behavior of compatible Gemini models. | | | | | | | 124 | + | | | | | | | 125 | +<b>Signature:</b> | | | | | | | 126 | + | | | | | | | 127 | +```typescript | | | | | | | 128 | +thinkingConfig?: ThinkingConfig; | | | | | | | 129 | +``` | | | | | | | 130 | + | | | | | | 120 | 131 | ## GenerationConfig.topK | | | | | 121 | 132 | | | | | | 122 | 133 | <b>Signature:</b> | | | |

| Original file line number | Diff line number | Diff line change | | | | -------------------------------------------- | ------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | @@ -124,6 +124,7 @@ The Firebase AI Web SDK. | | | | | | 124 | 124 | | [Segment](./ai.segment.md#segment_interface) | Represents a specific segment within a [Content](./ai.content.md#content_interface) object, often used to pinpoint the exact location of text or data that grounding information refers to. | | | 125 | 125 | | [StartChatParams](./ai.startchatparams.md#startchatparams_interface) | Params for [GenerativeModel.startChat()](./ai.generativemodel.md#generativemodelstartchat). | | | 126 | 126 | | [TextPart](./ai.textpart.md#textpart_interface) | Content part interface if the part represents a text string. | | | | 127 | +| [ThinkingConfig](./ai.thinkingconfig.md#thinkingconfig_interface) | Configuration for "thinking" behavior of compatible Gemini models.Certain models utilize a thinking process before generating a response. This allows them to reason through complex problems and plan a more coherent and accurate answer. | | | | 127 | 128 | | [ToolConfig](./ai.toolconfig.md#toolconfig_interface) | Tool config. This config is shared for all tools provided in the request. | | | 128 | 129 | | [UsageMetadata](./ai.usagemetadata.md#usagemetadata_interface) | Usage metadata about a [GenerateContentResponse](./ai.generatecontentresponse.md#generatecontentresponse_interface). | | | 129 | 130 | | [VertexAIOptions](./ai.vertexaioptions.md#vertexaioptions_interface) | Options when initializing the Firebase AI SDK. | |

| Original file line number | Diff line number | Diff line change | | | | ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | @@ -0,0 +1,43 @@ | | | | | | | 1 | +Project: /docs/reference/js/_project.yaml | | | | | | 2 | +Book: /docs/reference/_book.yaml | | | | | | 3 | +page_type: reference | | | | | | 4 | + | | | | | | 5 | +{% comment %} | | | | | | 6 | +DO NOT EDIT THIS FILE! | | | | | | 7 | +This is generated by the JS SDK team, and any local changes will be | | | | | | 8 | +overwritten. Changes should be made in the source code at | | | | | | 9 | +https://github.com/firebase/firebase-js-sdk | | | | | | 10 | +{% endcomment %} | | | | | | 11 | + | | | | | | 12 | +# ThinkingConfig interface | | | | | | 13 | +Configuration for "thinking" behavior of compatible Gemini models. | | | | | | 14 | + | | | | | | 15 | +Certain models utilize a thinking process before generating a response. This allows them to reason through complex problems and plan a more coherent and accurate answer. | | | | | | 16 | + | | | | | | 17 | +<b>Signature:</b> | | | | | | 18 | + | | | | | | 19 | +```typescript | | | | | | 20 | +export interface ThinkingConfig | | | | | | 21 | +``` | | | | | | 22 | + | | | | | | 23 | +## Properties | | | | | | 24 | + | | | | | | 25 | +| Property | Type | Description | | | | 26 | +| --- | --- | --- | | | | 27 | +| [thinkingBudget](./ai.thinkingconfig.md#thinkingconfigthinkingbudget) | number | The thinking budget, in tokens.This parameter sets an upper limit on the number of tokens the model can use for its internal "thinking" process. A higher budget may result in higher quality responses for complex tasks but can also increase latency and cost.If you don't specify a budget, the model will determine the appropriate amount of thinking based on the complexity of the prompt.An error will be thrown if you set a thinking budget for a model that does not support this feature or if the specified budget is not within the model's supported range. | | | | 28 | + | | | | | | 29 | +## ThinkingConfig.thinkingBudget | | | | | | 30 | + | | | | | | 31 | +The thinking budget, in tokens. | | | | | | 32 | + | | | | | | 33 | +This parameter sets an upper limit on the number of tokens the model can use for its internal "thinking" process. A higher budget may result in higher quality responses for complex tasks but can also increase latency and cost. | | | | | | 34 | + | | | | | | 35 | +If you don't specify a budget, the model will determine the appropriate amount of thinking based on the complexity of the prompt. | | | | | | 36 | + | | | | | | 37 | +An error will be thrown if you set a thinking budget for a model that does not support this feature or if the specified budget is not within the model's supported range. | | | | | | 38 | + | | | | | | 39 | +<b>Signature:</b> | | | | | | 40 | + | | | | | | 41 | +```typescript | | | | | | 42 | +thinkingBudget?: number; | | | | | | 43 | +``` | | | |

| Original file line number | Diff line number | Diff line change | | | | | ------------------------------------------------ | --------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------ | | | | @@ -26,6 +26,7 @@ export interface UsageMetadata | | | | | | | 26 | 26 | | [candidatesTokensDetails](./ai.usagemetadata.md#usagemetadatacandidatestokensdetails) | [ModalityTokenCount](./ai.modalitytokencount.md#modalitytokencount_interface)\[\] | | | | 27 | 27 | | [promptTokenCount](./ai.usagemetadata.md#usagemetadataprompttokencount) | number | | | | 28 | 28 | | [promptTokensDetails](./ai.usagemetadata.md#usagemetadataprompttokensdetails) | [ModalityTokenCount](./ai.modalitytokencount.md#modalitytokencount_interface)\[\] | | | | | 29 | +| [thoughtsTokenCount](./ai.usagemetadata.md#usagemetadatathoughtstokencount) | number | The number of tokens used by the model's internal "thinking" process. | | | | 29 | 30 | | [totalTokenCount](./ai.usagemetadata.md#usagemetadatatotaltokencount) | number | | | | 30 | 31 | | | | | | 31 | 32 | ## UsageMetadata.candidatesTokenCount | | | | | @@ -60,6 +61,16 @@ promptTokenCount: number; | | | | | | | 60 | 61 | promptTokensDetails?: ModalityTokenCount[]; | | | | | 61 | 62 | ``` | | | | | 62 | 63 | | | | | | | 64 | +## UsageMetadata.thoughtsTokenCount | | | | | | | 65 | + | | | | | | | 66 | +The number of tokens used by the model's internal "thinking" process. | | | | | | | 67 | + | | | | | | | 68 | +<b>Signature:</b> | | | | | | | 69 | + | | | | | | | 70 | +```typescript | | | | | | | 71 | +thoughtsTokenCount?: number; | | | | | | | 72 | +``` | | | | | | | 73 | + | | | | | | 63 | 74 | ## UsageMetadata.totalTokenCount | | | | | 64 | 75 | | | | | | 65 | 76 | <b>Signature:</b> | | | |

Original file line number Diff line number Diff line change
@@ -91,6 +91,10 @@ describe('Generate Content', () => {
91 91 2,
92 92 TOKEN_COUNT_DELTA
93 93 );
94 +expect(response.usageMetadata!.thoughtsTokenCount).to.be.closeTo(
95 +30,
96 +TOKEN_COUNT_DELTA * 2
97 +);
94 98 expect(response.usageMetadata!.totalTokenCount).to.be.closeTo(
95 99 55,
96 100 TOKEN_COUNT_DELTA * 2
Original file line number Diff line number Diff line change
@@ -113,6 +113,10 @@ export interface GenerationConfig {
113 113 * @beta
114 114 */
115 115 responseModalities?: ResponseModality[];
116 +/**
117 + * Configuration for "thinking" behavior of compatible Gemini models.
118 + */
119 +thinkingConfig?: ThinkingConfig;
116 120 }
117 121
118 122 /**
@@ -266,3 +270,28 @@ export interface FunctionCallingConfig {
266 270 mode?: FunctionCallingMode;
267 271 allowedFunctionNames?: string[];
268 272 }
273 +
274 +/**
275 + * Configuration for "thinking" behavior of compatible Gemini models.
276 + *
277 + * Certain models utilize a thinking process before generating a response. This allows them to
278 + * reason through complex problems and plan a more coherent and accurate answer.
279 + *
280 + * @public
281 + */
282 +export interface ThinkingConfig {
283 +/**
284 + * The thinking budget, in tokens.
285 + *
286 + * This parameter sets an upper limit on the number of tokens the model can use for its internal
287 + * "thinking" process. A higher budget may result in higher quality responses for complex tasks
288 + * but can also increase latency and cost.
289 + *
290 + * If you don't specify a budget, the model will determine the appropriate amount
291 + * of thinking based on the complexity of the prompt.
292 + *
293 + * An error will be thrown if you set a thinking budget for a model that does not support this
294 + * feature or if the specified budget is not within the model's supported range.
295 + */
296 +thinkingBudget?: number;
297 +}
Original file line number Diff line number Diff line change
@@ -92,6 +92,10 @@ export interface GenerateContentResponse {
92 92 export interface UsageMetadata {
93 93 promptTokenCount: number;
94 94 candidatesTokenCount: number;
95 +/**
96 + * The number of tokens used by the model's internal "thinking" process.
97 + */
98 +thoughtsTokenCount?: number;
95 99 totalTokenCount: number;
96 100 promptTokensDetails?: ModalityTokenCount[];
97 101 candidatesTokensDetails?: ModalityTokenCount[];