Skip to content

Commit 6ab4e13

Browse files
authored
feat(ai): Add support for Thinking Budget (#9156)
1 parent 47b154c commit 6ab4e13

File tree

10 files changed

+118
-0
lines changed

10 files changed

+118
-0
lines changed

.changeset/hip-impalas-divide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'firebase': minor
3+
'@firebase/ai': minor
4+
---
5+
6+
Add support for Thinking Budget.

common/api-review/ai.api.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ export interface GenerationConfig {
381381
stopSequences?: string[];
382382
// (undocumented)
383383
temperature?: number;
384+
thinkingConfig?: ThinkingConfig;
384385
// (undocumented)
385386
topK?: number;
386387
// (undocumented)
@@ -925,6 +926,11 @@ export interface TextPart {
925926
text: string;
926927
}
927928

929+
// @public
930+
export interface ThinkingConfig {
931+
thinkingBudget?: number;
932+
}
933+
928934
// @public
929935
export type Tool = FunctionDeclarationsTool | GoogleSearchTool;
930936

@@ -947,6 +953,7 @@ export interface UsageMetadata {
947953
promptTokenCount: number;
948954
// (undocumented)
949955
promptTokensDetails?: ModalityTokenCount[];
956+
thoughtsTokenCount?: number;
950957
// (undocumented)
951958
totalTokenCount: number;
952959
}

docs-devsite/_toc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ toc:
148148
path: /docs/reference/js/ai.stringschema.md
149149
- title: TextPart
150150
path: /docs/reference/js/ai.textpart.md
151+
- title: ThinkingConfig
152+
path: /docs/reference/js/ai.thinkingconfig.md
151153
- title: ToolConfig
152154
path: /docs/reference/js/ai.toolconfig.md
153155
- title: UsageMetadata

docs-devsite/ai.generationconfig.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface GenerationConfig
3131
| [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. <br/>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>. |
3232
| [stopSequences](./ai.generationconfig.md#generationconfigstopsequences) | string\[\] | |
3333
| [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. |
3435
| [topK](./ai.generationconfig.md#generationconfigtopk) | number | |
3536
| [topP](./ai.generationconfig.md#generationconfigtopp) | number | |
3637

@@ -117,6 +118,16 @@ stopSequences?: string[];
117118
temperature?: number;
118119
```
119120

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+
120131
## GenerationConfig.topK
121132

122133
<b>Signature:</b>

docs-devsite/ai.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ The Firebase AI Web SDK.
124124
| [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. |
125125
| [StartChatParams](./ai.startchatparams.md#startchatparams_interface) | Params for [GenerativeModel.startChat()](./ai.generativemodel.md#generativemodelstartchat)<!-- -->. |
126126
| [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. |
127128
| [ToolConfig](./ai.toolconfig.md#toolconfig_interface) | Tool config. This config is shared for all tools provided in the request. |
128129
| [UsageMetadata](./ai.usagemetadata.md#usagemetadata_interface) | Usage metadata about a [GenerateContentResponse](./ai.generatecontentresponse.md#generatecontentresponse_interface)<!-- -->. |
129130
| [VertexAIOptions](./ai.vertexaioptions.md#vertexaioptions_interface) | Options when initializing the Firebase AI SDK. |

docs-devsite/ai.thinkingconfig.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
```

docs-devsite/ai.usagemetadata.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface UsageMetadata
2626
| [candidatesTokensDetails](./ai.usagemetadata.md#usagemetadatacandidatestokensdetails) | [ModalityTokenCount](./ai.modalitytokencount.md#modalitytokencount_interface)<!-- -->\[\] | |
2727
| [promptTokenCount](./ai.usagemetadata.md#usagemetadataprompttokencount) | number | |
2828
| [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. |
2930
| [totalTokenCount](./ai.usagemetadata.md#usagemetadatatotaltokencount) | number | |
3031

3132
## UsageMetadata.candidatesTokenCount
@@ -60,6 +61,16 @@ promptTokenCount: number;
6061
promptTokensDetails?: ModalityTokenCount[];
6162
```
6263

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+
6374
## UsageMetadata.totalTokenCount
6475

6576
<b>Signature:</b>

packages/ai/integration/generate-content.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ describe('Generate Content', () => {
9191
2,
9292
TOKEN_COUNT_DELTA
9393
);
94+
expect(response.usageMetadata!.thoughtsTokenCount).to.be.closeTo(
95+
30,
96+
TOKEN_COUNT_DELTA * 2
97+
);
9498
expect(response.usageMetadata!.totalTokenCount).to.be.closeTo(
9599
55,
96100
TOKEN_COUNT_DELTA * 2

packages/ai/src/types/requests.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export interface GenerationConfig {
113113
* @beta
114114
*/
115115
responseModalities?: ResponseModality[];
116+
/**
117+
* Configuration for "thinking" behavior of compatible Gemini models.
118+
*/
119+
thinkingConfig?: ThinkingConfig;
116120
}
117121

118122
/**
@@ -266,3 +270,28 @@ export interface FunctionCallingConfig {
266270
mode?: FunctionCallingMode;
267271
allowedFunctionNames?: string[];
268272
}
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+
}

packages/ai/src/types/responses.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ export interface GenerateContentResponse {
9292
export interface UsageMetadata {
9393
promptTokenCount: number;
9494
candidatesTokenCount: number;
95+
/**
96+
* The number of tokens used by the model's internal "thinking" process.
97+
*/
98+
thoughtsTokenCount?: number;
9599
totalTokenCount: number;
96100
promptTokensDetails?: ModalityTokenCount[];
97101
candidatesTokensDetails?: ModalityTokenCount[];

0 commit comments

Comments
 (0)