Skip to content

Commit 48211fa

Browse files
committed
Tests, docs, error
1 parent 2652c67 commit 48211fa

File tree

8 files changed

+127
-15
lines changed

8 files changed

+127
-15
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/ai': minor
3+
---
4+
5+
Added ability to specify thinking levels in `thinkingConfig`.

common/api-review/ai.api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,8 +1371,20 @@ export interface TextPart {
13711371
export interface ThinkingConfig {
13721372
includeThoughts?: boolean;
13731373
thinkingBudget?: number;
1374+
thinkingLevel?: ThinkingLevel;
13741375
}
13751376

1377+
// @public
1378+
export const ThinkingLevel: {
1379+
MINIMAL: string;
1380+
LOW: string;
1381+
MEDIUM: string;
1382+
HIGH: string;
1383+
};
1384+
1385+
// @public
1386+
export type ThinkingLevel = (typeof ThinkingLevel)[keyof typeof ThinkingLevel];
1387+
13761388
// Warning: (ae-incompatible-release-tags) The symbol "Tool" is marked as @public, but its signature references "CodeExecutionTool" which is marked as @beta
13771389
// Warning: (ae-incompatible-release-tags) The symbol "Tool" is marked as @public, but its signature references "URLContextTool" which is marked as @beta
13781390
//

docs-devsite/ai.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ The Firebase AI Web SDK.
176176
| [POSSIBLE\_ROLES](./ai.md#possible_roles) | Possible roles. |
177177
| [ResponseModality](./ai.md#responsemodality) | <b><i>(Public Preview)</i></b> Generation modalities to be returned in generation responses. |
178178
| [SchemaType](./ai.md#schematype) | Contains the list of OpenAPI data types as defined by the [OpenAPI specification](https://swagger.io/docs/specification/data-models/data-types/) |
179+
| [ThinkingLevel](./ai.md#thinkinglevel) | A preset that controls the model's "thinking" process. Use <code>ThinkingLevel.LOW</code> for faster responses on less complex tasks, and <code>ThinkingLevel.HIGH</code> for better reasoning on more complex tasks. |
179180
| [URLRetrievalStatus](./ai.md#urlretrievalstatus) | <b><i>(Public Preview)</i></b> The status of a URL retrieval. |
180181

181182
## Type Aliases
@@ -208,6 +209,7 @@ The Firebase AI Web SDK.
208209
| [ResponseModality](./ai.md#responsemodality) | <b><i>(Public Preview)</i></b> Generation modalities to be returned in generation responses. |
209210
| [Role](./ai.md#role) | Role is the producer of the content. |
210211
| [SchemaType](./ai.md#schematype) | Contains the list of OpenAPI data types as defined by the [OpenAPI specification](https://swagger.io/docs/specification/data-models/data-types/) |
212+
| [ThinkingLevel](./ai.md#thinkinglevel) | A preset that controls the model's "thinking" process. Use <code>ThinkingLevel.LOW</code> for faster responses on less complex tasks, and <code>ThinkingLevel.HIGH</code> for better reasoning on more complex tasks. |
211213
| [Tool](./ai.md#tool) | Defines a tool that model can call to access external knowledge. |
212214
| [TypedSchema](./ai.md#typedschema) | A type that includes all specific Schema types. |
213215
| [URLRetrievalStatus](./ai.md#urlretrievalstatus) | <b><i>(Public Preview)</i></b> The status of a URL retrieval. |
@@ -827,6 +829,21 @@ SchemaType: {
827829
}
828830
```
829831

832+
## ThinkingLevel
833+
834+
A preset that controls the model's "thinking" process. Use `ThinkingLevel.LOW` for faster responses on less complex tasks, and `ThinkingLevel.HIGH` for better reasoning on more complex tasks.
835+
836+
<b>Signature:</b>
837+
838+
```typescript
839+
ThinkingLevel: {
840+
MINIMAL: string;
841+
LOW: string;
842+
MEDIUM: string;
843+
HIGH: string;
844+
}
845+
```
846+
830847
## URLRetrievalStatus
831848

832849
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
@@ -1142,6 +1159,16 @@ Contains the list of OpenAPI data types as defined by the [OpenAPI specification
11421159
export type SchemaType = (typeof SchemaType)[keyof typeof SchemaType];
11431160
```
11441161

1162+
## ThinkingLevel
1163+
1164+
A preset that controls the model's "thinking" process. Use `ThinkingLevel.LOW` for faster responses on less complex tasks, and `ThinkingLevel.HIGH` for better reasoning on more complex tasks.
1165+
1166+
<b>Signature:</b>
1167+
1168+
```typescript
1169+
export type ThinkingLevel = (typeof ThinkingLevel)[keyof typeof ThinkingLevel];
1170+
```
1171+
11451172
## Tool
11461173

11471174
Defines a tool that model can call to access external knowledge.

docs-devsite/ai.thinkingconfig.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export interface ThinkingConfig
2525
| Property | Type | Description |
2626
| --- | --- | --- |
2727
| [includeThoughts](./ai.thinkingconfig.md#thinkingconfigincludethoughts) | boolean | Whether to include "thought summaries" in the model's response. |
28-
| [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+
| [thinkingBudget](./ai.thinkingconfig.md#thinkingconfigthinkingbudget) | number | The thinking budget, in tokens. |
29+
| [thinkingLevel](./ai.thinkingconfig.md#thinkingconfigthinkinglevel) | [ThinkingLevel](./ai.md#thinkinglevel) | If not specified, Gemini will use the model's default dynamic thinking level. |
2930

3031
## ThinkingConfig.includeThoughts
3132

@@ -45,12 +46,34 @@ The thinking budget, in tokens.
4546

4647
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.
4748

48-
If you don't specify a budget, the model will determine the appropriate amount of thinking based on the complexity of the prompt.
49+
The range of supported thinking budget values depends on the model.
50+
51+
<ul> <li>To use the default thinking budget or thinking level for a model, leave this value undefined.</li>
52+
53+
<li>To disable thinking, when supported by the model, set this value to `0`<!-- -->.</li>
54+
55+
<li>To use dynamic thinking, allowing the model to decide on the thinking budget based on the task, set this value to `-1`<!-- -->.</li> </ul>
4956

5057
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.
5158

59+
The model will also error if `thinkingLevel` and `thinkingBudget` are both set.
60+
5261
<b>Signature:</b>
5362

5463
```typescript
5564
thinkingBudget?: number;
5665
```
66+
67+
## ThinkingConfig.thinkingLevel
68+
69+
If not specified, Gemini will use the model's default dynamic thinking level.
70+
71+
Note: The model will error if `thinkingLevel` and `thinkingBudget` are both set.
72+
73+
Important: Gemini 2.5 series models do not support thinking levels; use `thinkingBudget` to set a thinking budget instead.
74+
75+
<b>Signature:</b>
76+
77+
```typescript
78+
thinkingLevel?: ThinkingLevel;
79+
```

packages/ai/src/models/generative-model.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {
2121
AI,
2222
InferenceMode,
2323
AIErrorCode,
24-
ChromeAdapter
24+
ChromeAdapter,
25+
ThinkingLevel
2526
} from '../public-types';
2627
import * as request from '../requests/request';
2728
import { SinonStub, match, restore, stub } from 'sinon';
@@ -53,6 +54,20 @@ const fakeAI: AI = {
5354
};
5455

5556
describe('GenerativeModel', () => {
57+
it('throws if generationConfig is invalid', () => {
58+
expect(
59+
() =>
60+
new GenerativeModel(fakeAI, {
61+
model: 'my-model',
62+
generationConfig: {
63+
thinkingConfig: {
64+
thinkingBudget: 1000,
65+
thinkingLevel: ThinkingLevel.LOW
66+
}
67+
}
68+
})
69+
).to.throw(AIErrorCode.UNSUPPORTED);
70+
});
5671
it('passes params through to generateContent', async () => {
5772
const genModel = new GenerativeModel(
5873
fakeAI,

packages/ai/src/models/generative-model.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ import {
4141
formatGenerateContentInput,
4242
formatSystemInstruction
4343
} from '../requests/request-helpers';
44-
import { AI } from '../public-types';
44+
import { AI, AIErrorCode } from '../public-types';
4545
import { AIModel } from './ai-model';
4646
import { ChromeAdapter } from '../types/chrome-adapter';
47+
import { AIError } from '../errors';
4748

4849
/**
4950
* Class for generative model APIs.
@@ -65,6 +66,7 @@ export class GenerativeModel extends AIModel {
6566
) {
6667
super(ai, modelParams.model);
6768
this.generationConfig = modelParams.generationConfig || {};
69+
validateGenerationConfig(this.generationConfig);
6870
this.safetySettings = modelParams.safetySettings || [];
6971
this.tools = modelParams.tools;
7072
this.toolConfig = modelParams.toolConfig;
@@ -165,3 +167,20 @@ export class GenerativeModel extends AIModel {
165167
);
166168
}
167169
}
170+
171+
/**
172+
* Client-side validation of some common `GenerationConfig` pitfalls, in order
173+
* to save the developer a wasted request.
174+
*/
175+
function validateGenerationConfig(generationConfig: GenerationConfig): void {
176+
if (
177+
// != allows for null and undefined. 0 is considered "set" by the model
178+
generationConfig.thinkingConfig?.thinkingBudget != null &&
179+
generationConfig.thinkingConfig.thinkingLevel
180+
) {
181+
throw new AIError(
182+
AIErrorCode.UNSUPPORTED,
183+
`Cannot set both thinkingBudget and thinkingLevel in a config.`
184+
);
185+
}
186+
}

packages/ai/src/types/enums.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ export type Language = (typeof Language)[keyof typeof Language];
437437
* A preset that controls the model's "thinking" process. Use
438438
* `ThinkingLevel.LOW` for faster responses on less complex tasks, and
439439
* `ThinkingLevel.HIGH` for better reasoning on more complex tasks.
440-
*
440+
*
441441
* @public
442442
*/
443443
export const ThinkingLevel = {
@@ -451,7 +451,7 @@ export const ThinkingLevel = {
451451
* A preset that controls the model's "thinking" process. Use
452452
* `ThinkingLevel.LOW` for faster responses on less complex tasks, and
453453
* `ThinkingLevel.HIGH` for better reasoning on more complex tasks.
454-
*
454+
*
455455
* @public
456456
*/
457457
export type ThinkingLevel = (typeof ThinkingLevel)[keyof typeof ThinkingLevel];

packages/ai/src/types/requests.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -437,28 +437,39 @@ export interface ThinkingConfig {
437437
/**
438438
* The thinking budget, in tokens.
439439
*
440+
* @remarks
440441
* This parameter sets an upper limit on the number of tokens the model can use for its internal
441442
* "thinking" process. A higher budget may result in higher quality responses for complex tasks
442443
* but can also increase latency and cost.
443444
*
444445
* The range of supported thinking budget values depends on the model.
445-
*
446-
* - To use the default thinking budget or thinking level for a model, leave
447-
* this value undefined.
448-
*
449-
* - To disable thinking, when supported by the model, set this value to `0`.
450-
*
451-
* - To use dynamic thinking, allowing the model to decide on the thinking
452-
* budget based on the task, set this value to `-1`.
446+
*
447+
* <ul>
448+
* <li>To use the default thinking budget or thinking level for a model, leave
449+
* this value undefined.</li>
450+
*
451+
* <li>To disable thinking, when supported by the model, set this value
452+
* to `0`.</li>
453+
*
454+
* <li>To use dynamic thinking, allowing the model to decide on the thinking
455+
* budget based on the task, set this value to `-1`.</li>
456+
* </ul>
453457
*
454458
* An error will be thrown if you set a thinking budget for a model that does not support this
455459
* feature or if the specified budget is not within the model's supported range.
460+
*
461+
* The model will also error if `thinkingLevel` and `thinkingBudget` are
462+
* both set.
456463
*/
457464
thinkingBudget?: number;
458465

459466
/**
460467
* If not specified, Gemini will use the model's default dynamic thinking level.
461-
*
468+
*
469+
* @remarks
470+
* Note: The model will error if `thinkingLevel` and `thinkingBudget` are
471+
* both set.
472+
*
462473
* Important: Gemini 2.5 series models do not support thinking levels; use
463474
* `thinkingBudget` to set a thinking budget instead.
464475
*/

0 commit comments

Comments
 (0)