Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update convertSystemMessageToHumanContent logic [local pr] #2

Merged
Merged
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
18 changes: 11 additions & 7 deletions libs/langchain-google-genai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export interface GoogleGenerativeAIChatInput
* - All Gemini 1.5 Flash model versions
* - Gemini 1.0 Pro version gemini-1.0-pro-002
*/
convertSystemMessageToHumanContent?: boolean;
convertSystemMessageToHumanContent?: boolean | undefined;
}

/**
Expand Down Expand Up @@ -572,7 +572,7 @@ export class ChatGoogleGenerativeAI

streamUsage = true;

convertSystemMessageToHumanContent: boolean;
convertSystemMessageToHumanContent: boolean | undefined;

private client: GenerativeModel;

Expand All @@ -589,8 +589,6 @@ export class ChatGoogleGenerativeAI
this.model;
this.model = this.modelName;

this.convertSystemMessageToHumanContent = this.useSystemInstructions();

this.maxOutputTokens = fields?.maxOutputTokens ?? this.maxOutputTokens;

if (this.maxOutputTokens && this.maxOutputTokens < 0) {
Expand Down Expand Up @@ -664,7 +662,13 @@ export class ChatGoogleGenerativeAI
this.streamUsage = fields?.streamUsage ?? this.streamUsage;
}

useSystemInstructions() : boolean {
get useSystemInstruction(): boolean {
return typeof this.convertSystemMessageToHumanContent === "boolean"
? !this.convertSystemMessageToHumanContent
: this.computeUseSystemInstruction;
}

get computeUseSystemInstruction() : boolean {
// This works on models from April 2024 and later
// Vertex AI: gemini-1.5-pro and gemini-1.0-002 and later
// AI Studio: gemini-1.5-pro-latest
Expand Down Expand Up @@ -737,7 +741,7 @@ export class ChatGoogleGenerativeAI
const prompt = convertBaseMessagesToContent(
messages,
this._isMultimodalModel,
this.convertSystemMessageToHumanContent
this.useSystemInstruction
);
let actualPrompt = prompt;
if (prompt[0].role === "system") {
Expand Down Expand Up @@ -808,7 +812,7 @@ export class ChatGoogleGenerativeAI
const prompt = convertBaseMessagesToContent(
messages,
this._isMultimodalModel,
this.convertSystemMessageToHumanContent
this.useSystemInstruction
);
let actualPrompt = prompt;
if (prompt[0].role === "system") {
Expand Down