diff --git a/src/models/chat_session.ts b/src/models/chat_session.ts index 431f9ee4..34e8b55d 100644 --- a/src/models/chat_session.ts +++ b/src/models/chat_session.ts @@ -33,6 +33,7 @@ import { SafetySetting, StartChatSessionRequest, StreamGenerateContentResult, + ThinkingConfig, Tool, } from '../types/content'; import {ToolConfig} from '../types'; @@ -59,6 +60,7 @@ export class ChatSession { private readonly safetySettings?: SafetySetting[]; private readonly tools?: Tool[]; private readonly toolConfig?: ToolConfig; + private readonly thinkingConfig?: ThinkingConfig; private readonly apiEndpoint?: string; private readonly systemInstruction?: Content; @@ -83,6 +85,7 @@ export class ChatSession { this.safetySettings = request.safetySettings; this.tools = request.tools; this.toolConfig = request.toolConfig; + this.thinkingConfig = request.thinkingConfig; this.apiEndpoint = request.apiEndpoint; this.requestOptions = requestOptions ?? {}; if (request.systemInstruction) { @@ -135,6 +138,7 @@ export class ChatSession { tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, + thinkingConfig: this.thinkingConfig, }; const generateContentResult: GenerateContentResult = await generateContent( @@ -220,6 +224,7 @@ export class ChatSession { tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, + thinkingConfig: this.thinkingConfig, }; const streamGenerateContentResultPromise = generateContentStream( @@ -269,6 +274,7 @@ export class ChatSessionPreview { private readonly safetySettings?: SafetySetting[]; private readonly tools?: Tool[]; private readonly toolConfig?: ToolConfig; + private readonly thinkingConfig?: ThinkingConfig; private readonly apiEndpoint?: string; private readonly systemInstruction?: Content; private readonly cachedContent?: string; @@ -294,6 +300,7 @@ export class ChatSessionPreview { this.safetySettings = request.safetySettings; this.tools = request.tools; this.toolConfig = request.toolConfig; + this.thinkingConfig = request.thinkingConfig; this.apiEndpoint = request.apiEndpoint; this.requestOptions = requestOptions ?? {}; this.cachedContent = request.cachedContent; @@ -347,6 +354,7 @@ export class ChatSessionPreview { toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: this.cachedContent, + thinkingConfig: this.thinkingConfig, }; const generateContentResult: GenerateContentResult = await generateContent( @@ -434,6 +442,7 @@ export class ChatSessionPreview { toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: this.cachedContent, + thinkingConfig: this.thinkingConfig, }; const streamGenerateContentResultPromise = generateContentStream( diff --git a/src/models/generative_models.ts b/src/models/generative_models.ts index 1e2820e9..213ca5a4 100644 --- a/src/models/generative_models.ts +++ b/src/models/generative_models.ts @@ -129,17 +129,16 @@ export class GenerativeModel { async generateContent( request: GenerateContentRequest | string ): Promise { - request = formulateRequestToGenerateContentRequest(request); - const formulatedRequest = - formulateSystemInstructionIntoGenerateContentRequest( - request, - this.systemInstruction - ); + const formulatedRequest = formulateRequestToGenerateContentRequest(request); + const finalRequest = formulateSystemInstructionIntoGenerateContentRequest( + formulatedRequest, + this.systemInstruction + ); return generateContent( this.location, this.resourcePath, this.fetchToken(), - formulatedRequest, + finalRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, @@ -176,17 +175,16 @@ export class GenerativeModel { async generateContentStream( request: GenerateContentRequest | string ): Promise { - request = formulateRequestToGenerateContentRequest(request); - const formulatedRequest = - formulateSystemInstructionIntoGenerateContentRequest( - request, - this.systemInstruction - ); + const formulatedRequest = formulateRequestToGenerateContentRequest(request); + const finalRequest = formulateSystemInstructionIntoGenerateContentRequest( + formulatedRequest, + this.systemInstruction + ); return generateContentStream( this.location, this.resourcePath, this.fetchToken(), - formulatedRequest, + finalRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, @@ -361,10 +359,10 @@ export class GenerativeModelPreview { async generateContent( request: GenerateContentRequest | string ): Promise { - request = formulateRequestToGenerateContentRequest(request); - const formulatedRequest = { + const formulatedRequest = formulateRequestToGenerateContentRequest(request); + const finalRequest = { ...formulateSystemInstructionIntoGenerateContentRequest( - request, + formulatedRequest, this.systemInstruction ), cachedContent: this.cachedContent?.name, @@ -373,7 +371,7 @@ export class GenerativeModelPreview { this.location, this.resourcePath, this.fetchToken(), - formulatedRequest, + finalRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, @@ -410,10 +408,10 @@ export class GenerativeModelPreview { async generateContentStream( request: GenerateContentRequest | string ): Promise { - request = formulateRequestToGenerateContentRequest(request); - const formulatedRequest = { + const formulatedRequest = formulateRequestToGenerateContentRequest(request); + const finalRequest = { ...formulateSystemInstructionIntoGenerateContentRequest( - request, + formulatedRequest, this.systemInstruction ), cachedContent: this.cachedContent?.name, @@ -422,7 +420,7 @@ export class GenerativeModelPreview { this.location, this.resourcePath, this.fetchToken(), - formulatedRequest, + finalRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, diff --git a/src/types/content.ts b/src/types/content.ts index a227e47f..1d79f383 100644 --- a/src/types/content.ts +++ b/src/types/content.ts @@ -99,6 +99,17 @@ export declare interface CountTokensResponse { totalBillableCharacters?: number; } +/** + * The configuration for thinking features. + * @public + */ +export declare interface ThinkingConfig { + /** Optional. Indicates whether to include thoughts in the response. If true, thoughts are returned only when available. */ + includeThoughts?: boolean; + /** Optional. Indicates the thinking budget in tokens. This is only applied when enable_thinking is true. */ + thinkingBudget?: number; +} + /** * Params used to call the getGenerativeModel method. */ @@ -233,6 +244,8 @@ export declare interface GenerationConfig { * If set, a compatible responseMimeType must also be set. */ responseSchema?: ResponseSchema; + /** Optional. The configuration for thinking features. */ + thinkingConfig?: ThinkingConfig; } /**