Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/models/chat_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
SafetySetting,
StartChatSessionRequest,
StreamGenerateContentResult,
ThinkingConfig,
Tool,
} from '../types/content';
import {ToolConfig} from '../types';
Expand All @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -135,6 +138,7 @@ export class ChatSession {
tools: this.tools,
toolConfig: this.toolConfig,
systemInstruction: this.systemInstruction,
thinkingConfig: this.thinkingConfig,
};

const generateContentResult: GenerateContentResult = await generateContent(
Expand Down Expand Up @@ -220,6 +224,7 @@ export class ChatSession {
tools: this.tools,
toolConfig: this.toolConfig,
systemInstruction: this.systemInstruction,
thinkingConfig: this.thinkingConfig,
};

const streamGenerateContentResultPromise = generateContentStream(
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -347,6 +354,7 @@ export class ChatSessionPreview {
toolConfig: this.toolConfig,
systemInstruction: this.systemInstruction,
cachedContent: this.cachedContent,
thinkingConfig: this.thinkingConfig,
};

const generateContentResult: GenerateContentResult = await generateContent(
Expand Down Expand Up @@ -434,6 +442,7 @@ export class ChatSessionPreview {
toolConfig: this.toolConfig,
systemInstruction: this.systemInstruction,
cachedContent: this.cachedContent,
thinkingConfig: this.thinkingConfig,
};

const streamGenerateContentResultPromise = generateContentStream(
Expand Down
42 changes: 20 additions & 22 deletions src/models/generative_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,16 @@ export class GenerativeModel {
async generateContent(
request: GenerateContentRequest | string
): Promise<GenerateContentResult> {
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,
Expand Down Expand Up @@ -176,17 +175,16 @@ export class GenerativeModel {
async generateContentStream(
request: GenerateContentRequest | string
): Promise<StreamGenerateContentResult> {
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,
Expand Down Expand Up @@ -361,10 +359,10 @@ export class GenerativeModelPreview {
async generateContent(
request: GenerateContentRequest | string
): Promise<GenerateContentResult> {
request = formulateRequestToGenerateContentRequest(request);
const formulatedRequest = {
const formulatedRequest = formulateRequestToGenerateContentRequest(request);
const finalRequest = {
...formulateSystemInstructionIntoGenerateContentRequest(
request,
formulatedRequest,
this.systemInstruction
),
cachedContent: this.cachedContent?.name,
Expand All @@ -373,7 +371,7 @@ export class GenerativeModelPreview {
this.location,
this.resourcePath,
this.fetchToken(),
formulatedRequest,
finalRequest,
this.apiEndpoint,
this.generationConfig,
this.safetySettings,
Expand Down Expand Up @@ -410,10 +408,10 @@ export class GenerativeModelPreview {
async generateContentStream(
request: GenerateContentRequest | string
): Promise<StreamGenerateContentResult> {
request = formulateRequestToGenerateContentRequest(request);
const formulatedRequest = {
const formulatedRequest = formulateRequestToGenerateContentRequest(request);
const finalRequest = {
...formulateSystemInstructionIntoGenerateContentRequest(
request,
formulatedRequest,
this.systemInstruction
),
cachedContent: this.cachedContent?.name,
Expand All @@ -422,7 +420,7 @@ export class GenerativeModelPreview {
this.location,
this.resourcePath,
this.fetchToken(),
formulatedRequest,
finalRequest,
this.apiEndpoint,
this.generationConfig,
this.safetySettings,
Expand Down
13 changes: 13 additions & 0 deletions src/types/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down