From b1ff373b41ede746c319b9d111a1746e1afa6f9d Mon Sep 17 00:00:00 2001 From: chrisnathan20 Date: Sun, 17 Nov 2024 19:35:05 -0500 Subject: [PATCH 1/7] feat: Add direct system instruction invocation for GenAI --- .../langchain-google-genai/src/chat_models.ts | 52 +++++++++++++++++-- .../src/utils/common.ts | 8 +-- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/libs/langchain-google-genai/src/chat_models.ts b/libs/langchain-google-genai/src/chat_models.ts index 6fc5433babe3..9a26742a01ec 100644 --- a/libs/langchain-google-genai/src/chat_models.ts +++ b/libs/langchain-google-genai/src/chat_models.ts @@ -180,6 +180,15 @@ export interface GoogleGenerativeAIChatInput * @default false */ json?: boolean; + + /** + * Whether or not model supports system instructions. + * The following models support system instructions: + * - All Gemini 1.5 Pro model versions + * - All Gemini 1.5 Flash model versions + * - Gemini 1.0 Pro version gemini-1.0-pro-002 + */ + convertSystemMessageToHumanContent?: boolean; } /** @@ -563,6 +572,8 @@ export class ChatGoogleGenerativeAI streamUsage = true; + convertSystemMessageToHumanContent: boolean; + private client: GenerativeModel; get _isMultimodalModel() { @@ -578,6 +589,8 @@ 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) { @@ -651,6 +664,23 @@ export class ChatGoogleGenerativeAI this.streamUsage = fields?.streamUsage ?? this.streamUsage; } + useSystemInstructions() : 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 + if (this.modelName === "gemini-1.0-pro-001") { + return false; + } else if (this.modelName.startsWith("gemini-pro-vision")) { + return false; + } else if (this.modelName.startsWith("gemini-1.0-pro-vision")) { + return false; + } else if (this.modelName === "gemini-pro") { + // on AI Studio gemini-pro is still pointing at gemini-1.0-pro-001 + return false; + } + return true; + } + getLsParams(options: this["ParsedCallOptions"]): LangSmithParams { return { ls_provider: "google_genai", @@ -706,8 +736,15 @@ export class ChatGoogleGenerativeAI ): Promise { const prompt = convertBaseMessagesToContent( messages, - this._isMultimodalModel + this._isMultimodalModel, + this.convertSystemMessageToHumanContent ); + let actualPrompt = prompt; + if (prompt[0].role === "system") { + const [systemInstruction] = prompt; + this.client.systemInstruction = systemInstruction; + actualPrompt = prompt.slice(1) + } const parameters = this.invocationParams(options); // Handle streaming @@ -734,7 +771,7 @@ export class ChatGoogleGenerativeAI const res = await this.completionWithRetry({ ...parameters, - contents: prompt, + contents: actualPrompt, }); let usageMetadata: UsageMetadata | undefined; @@ -770,12 +807,19 @@ export class ChatGoogleGenerativeAI ): AsyncGenerator { const prompt = convertBaseMessagesToContent( messages, - this._isMultimodalModel + this._isMultimodalModel, + this.convertSystemMessageToHumanContent ); + let actualPrompt = prompt; + if (prompt[0].role === "system") { + const [systemInstruction] = prompt; + this.client.systemInstruction = systemInstruction; + actualPrompt = prompt.slice(1) + } const parameters = this.invocationParams(options); const request = { ...parameters, - contents: prompt, + contents: actualPrompt, }; const stream = await this.caller.callWithOptions( { signal: options?.signal }, diff --git a/libs/langchain-google-genai/src/utils/common.ts b/libs/langchain-google-genai/src/utils/common.ts index 2670760c7115..58ead85a2abf 100644 --- a/libs/langchain-google-genai/src/utils/common.ts +++ b/libs/langchain-google-genai/src/utils/common.ts @@ -61,6 +61,7 @@ export function convertAuthorToRole( case "model": // getMessageAuthor returns message.name. code ex.: return message.name ?? type; return "model"; case "system": + return "system"; case "human": return "user"; case "tool": @@ -179,7 +180,8 @@ export function convertMessageContentToParts( export function convertBaseMessagesToContent( messages: BaseMessage[], - isMultimodalModel: boolean + isMultimodalModel: boolean, + convertSystemMessageToHumanContent: boolean = false ) { return messages.reduce<{ content: Content[]; @@ -223,7 +225,7 @@ export function convertBaseMessagesToContent( }; } let actualRole = role; - if (actualRole === "function") { + if (actualRole === "function" || (actualRole === "system" && !convertSystemMessageToHumanContent)) { // GenerativeAI API will throw an error if the role is not "user" or "model." actualRole = "user"; } @@ -232,7 +234,7 @@ export function convertBaseMessagesToContent( parts, }; return { - mergeWithPreviousContent: author === "system", + mergeWithPreviousContent: author === "system" && !convertSystemMessageToHumanContent, content: [...acc.content, content], }; }, From bd97dc50307340c1eec96b9d94206a8534c7f175 Mon Sep 17 00:00:00 2001 From: Gary Chen Date: Mon, 18 Nov 2024 15:11:08 -0500 Subject: [PATCH 2/7] Update convertSystemMessageToHumanContent logic --- libs/langchain-google-genai/src/chat_models.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/libs/langchain-google-genai/src/chat_models.ts b/libs/langchain-google-genai/src/chat_models.ts index 9a26742a01ec..c7ed6ba7b09a 100644 --- a/libs/langchain-google-genai/src/chat_models.ts +++ b/libs/langchain-google-genai/src/chat_models.ts @@ -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; } /** @@ -572,7 +572,7 @@ export class ChatGoogleGenerativeAI streamUsage = true; - convertSystemMessageToHumanContent: boolean; + convertSystemMessageToHumanContent: boolean | undefined; private client: GenerativeModel; @@ -589,7 +589,7 @@ export class ChatGoogleGenerativeAI this.model; this.model = this.modelName; - this.convertSystemMessageToHumanContent = this.useSystemInstructions(); + this.convertSystemMessageToHumanContent = this.computeUseSystemInstruction(); this.maxOutputTokens = fields?.maxOutputTokens ?? this.maxOutputTokens; @@ -664,7 +664,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 From 6615664a54378d105eedf463824c0c87ce0756eb Mon Sep 17 00:00:00 2001 From: chrisnathan20 Date: Mon, 18 Nov 2024 15:50:22 -0500 Subject: [PATCH 3/7] suggested refactor for convertSystemMessageToHumanContent usage --- libs/langchain-google-genai/src/chat_models.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libs/langchain-google-genai/src/chat_models.ts b/libs/langchain-google-genai/src/chat_models.ts index c7ed6ba7b09a..7bddd2756d87 100644 --- a/libs/langchain-google-genai/src/chat_models.ts +++ b/libs/langchain-google-genai/src/chat_models.ts @@ -589,8 +589,6 @@ export class ChatGoogleGenerativeAI this.model; this.model = this.modelName; - this.convertSystemMessageToHumanContent = this.computeUseSystemInstruction(); - this.maxOutputTokens = fields?.maxOutputTokens ?? this.maxOutputTokens; if (this.maxOutputTokens && this.maxOutputTokens < 0) { @@ -743,7 +741,7 @@ export class ChatGoogleGenerativeAI const prompt = convertBaseMessagesToContent( messages, this._isMultimodalModel, - this.convertSystemMessageToHumanContent + this.useSystemInstruction ); let actualPrompt = prompt; if (prompt[0].role === "system") { @@ -814,7 +812,7 @@ export class ChatGoogleGenerativeAI const prompt = convertBaseMessagesToContent( messages, this._isMultimodalModel, - this.convertSystemMessageToHumanContent + this.useSystemInstruction ); let actualPrompt = prompt; if (prompt[0].role === "system") { From 5f827440579ad6a92aaf4ea8c245b46bf94099e8 Mon Sep 17 00:00:00 2001 From: martinl498 Date: Tue, 19 Nov 2024 18:26:49 -0500 Subject: [PATCH 4/7] Add system message tests for convertBaseMessagesToContent with 3rd parameter set to false --- .../src/tests/chat_models.test.ts | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/libs/langchain-google-genai/src/tests/chat_models.test.ts b/libs/langchain-google-genai/src/tests/chat_models.test.ts index 97015725fc7e..ccc186a96cec 100644 --- a/libs/langchain-google-genai/src/tests/chat_models.test.ts +++ b/libs/langchain-google-genai/src/tests/chat_models.test.ts @@ -253,3 +253,79 @@ test("convertBaseMessagesToContent correctly creates properly formatted content" }, ]); }); + +test("Input has single system message followed by one user message, convert system message is false", async () => { + const messages = [ + new SystemMessage("You are a helpful assistant"), + new HumanMessage("What's the weather like in new york?"), + ]; + const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, false); + + expect(messagesAsGoogleContent).toEqual([ + { + role: "user", + parts: [ + { text: "You are a helpful assistant" }, + { text: "What's the weather like in new york?" }, + ], + }, + ]); +}); + +test("Input has a system message that is not the first message, convert system message is false", async () => { + const messages = [ + new HumanMessage("What's the weather like in new york?"), + new SystemMessage("You are a helpful assistant"), + ]; + expect( + () => { + convertBaseMessagesToContent(messages, false, false); + } + ).toThrow(); +}); + +test("Input has no system message and one user message, convert system message is false", async () => { + const messages = [ + new HumanMessage("What's the weather like in new york?"), + ]; + const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, false); + + expect(messagesAsGoogleContent).toEqual([ + { + role: "user", + parts: [ + { text: "What's the weather like in new york?" }, + ], + }, + ]); +}); + +test("Input has no system message and multiple user message, convert system message is false", async () => { + const messages = [ + new HumanMessage("What's the weather like in new york?"), + new HumanMessage("What's the weather like in toronto?"), + new HumanMessage("What's the weather like in los angeles?"), + ]; + const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, false); + + expect(messagesAsGoogleContent).toEqual([ + { + role: "user", + parts: [ + { text: "What's the weather like in new york?" }, + ], + }, + { + role: "user", + parts: [ + { text: "What's the weather like in toronto?" }, + ], + }, + { + role: "user", + parts: [ + { text: "What's the weather like in los angeles?" }, + ], + }, + ]); +}); \ No newline at end of file From ea59aac6ce8faea65413e9bf4f762616c6599035 Mon Sep 17 00:00:00 2001 From: martinl498 Date: Tue, 19 Nov 2024 21:10:32 -0500 Subject: [PATCH 5/7] Add specific error in throw test, add test with multiple sys messages --- .../src/tests/chat_models.test.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libs/langchain-google-genai/src/tests/chat_models.test.ts b/libs/langchain-google-genai/src/tests/chat_models.test.ts index ccc186a96cec..582e6105a146 100644 --- a/libs/langchain-google-genai/src/tests/chat_models.test.ts +++ b/libs/langchain-google-genai/src/tests/chat_models.test.ts @@ -281,7 +281,19 @@ test("Input has a system message that is not the first message, convert system m () => { convertBaseMessagesToContent(messages, false, false); } - ).toThrow(); + ).toThrow("System message should be the first one"); +}); + +test("Input has multiple system messages, convert system message is false", async () => { + const messages = [ + new SystemMessage("You are a helpful assistant"), + new SystemMessage("You are not a helpful assistant"), + ]; + expect( + () => { + convertBaseMessagesToContent(messages, false, false); + } + ).toThrow("System message should be the first one"); }); test("Input has no system message and one user message, convert system message is false", async () => { From 17bf68d5246ed4088405fce09a9a5049aaa57224 Mon Sep 17 00:00:00 2001 From: Shannon Budiman Date: Tue, 19 Nov 2024 21:26:08 -0500 Subject: [PATCH 6/7] added test cases for third parameter true --- .../src/tests/chat_models.test.ts | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/libs/langchain-google-genai/src/tests/chat_models.test.ts b/libs/langchain-google-genai/src/tests/chat_models.test.ts index 582e6105a146..5f9a3643d01e 100644 --- a/libs/langchain-google-genai/src/tests/chat_models.test.ts +++ b/libs/langchain-google-genai/src/tests/chat_models.test.ts @@ -340,4 +340,98 @@ test("Input has no system message and multiple user message, convert system mess ], }, ]); +}); + +test("Input has single system message followed by one user message, convert system message is true", async () => { + const messages = [ + new SystemMessage("You are a helpful assistant"), + new HumanMessage("What's the weather like in new york?") + ]; + + const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, true); + + expect(messagesAsGoogleContent).toEqual([ + { + role: "system", + parts: [ + { text: "You are a helpful assistant" } + ] + }, + { + role: "user", + parts: [ + { text: "What's the weather like in new york?" } + ], + }, + ]); +}); + +test("Input has single system message that is not the first message, convert system message is true", async () => { + const messages = [ + new HumanMessage("What's the weather like in new york?"), + new SystemMessage("You are a helpful assistant") + ]; + + expect(() => convertBaseMessagesToContent(messages, false, true)).toThrow( + "System message should be the first one" + ); +}); + +test("Input has multiple system message, convert system message is true", async () => { + const messages = [ + new SystemMessage("What's the weather like in new york?"), + new SystemMessage("You are a helpful assistant") + ]; + + expect(() => convertBaseMessagesToContent(messages, false, true)).toThrow( + "System message should be the first one" + ); +}); + +test("Input has no system message and one user message, convert system message is true", async () => { + const messages = [ + new HumanMessage("What's the weather like in new york?") + ]; + + const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, true); + + expect(messagesAsGoogleContent).toEqual([ + { + role: "user", + parts: [ + { text: "What's the weather like in new york?" } + ], + }, + ]); +}); + +test("Input has no system message and multiple user messages, convert system message is true", async () => { + const messages = [ + new HumanMessage("What's the weather like in new york?"), + new HumanMessage("Will it rain today?"), + new HumanMessage("How about next week?") + ]; + + const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, true); + + expect(messagesAsGoogleContent).toEqual([ + { + role: "user", + parts: [ + { text: "What's the weather like in new york?" } + ], + }, + { + role: "user", + parts: [ + { text: "Will it rain today?" } + ], + }, + { + role: "user", + parts: [ + { text: "How about next week?" } + ], + }, + ]); }); \ No newline at end of file From 39f18c185383a0d14b2bddb8d2e5774ee955f0be Mon Sep 17 00:00:00 2001 From: chrisnathan20 Date: Tue, 3 Dec 2024 15:11:42 -0500 Subject: [PATCH 7/7] fixed formatting issues --- .../langchain-google-genai/src/chat_models.ts | 8 +- .../src/tests/chat_models.test.ts | 110 +++++++++--------- .../src/utils/common.ts | 10 +- 3 files changed, 64 insertions(+), 64 deletions(-) diff --git a/libs/langchain-google-genai/src/chat_models.ts b/libs/langchain-google-genai/src/chat_models.ts index 7bddd2756d87..7b22c8a93ce9 100644 --- a/libs/langchain-google-genai/src/chat_models.ts +++ b/libs/langchain-google-genai/src/chat_models.ts @@ -667,8 +667,8 @@ export class ChatGoogleGenerativeAI ? !this.convertSystemMessageToHumanContent : this.computeUseSystemInstruction; } - - get computeUseSystemInstruction() : boolean { + + 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 @@ -747,7 +747,7 @@ export class ChatGoogleGenerativeAI if (prompt[0].role === "system") { const [systemInstruction] = prompt; this.client.systemInstruction = systemInstruction; - actualPrompt = prompt.slice(1) + actualPrompt = prompt.slice(1); } const parameters = this.invocationParams(options); @@ -818,7 +818,7 @@ export class ChatGoogleGenerativeAI if (prompt[0].role === "system") { const [systemInstruction] = prompt; this.client.systemInstruction = systemInstruction; - actualPrompt = prompt.slice(1) + actualPrompt = prompt.slice(1); } const parameters = this.invocationParams(options); const request = { diff --git a/libs/langchain-google-genai/src/tests/chat_models.test.ts b/libs/langchain-google-genai/src/tests/chat_models.test.ts index 5f9a3643d01e..73cc321abd7c 100644 --- a/libs/langchain-google-genai/src/tests/chat_models.test.ts +++ b/libs/langchain-google-genai/src/tests/chat_models.test.ts @@ -259,7 +259,11 @@ test("Input has single system message followed by one user message, convert syst new SystemMessage("You are a helpful assistant"), new HumanMessage("What's the weather like in new york?"), ]; - const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, false); + const messagesAsGoogleContent = convertBaseMessagesToContent( + messages, + false, + false + ); expect(messagesAsGoogleContent).toEqual([ { @@ -277,11 +281,9 @@ test("Input has a system message that is not the first message, convert system m new HumanMessage("What's the weather like in new york?"), new SystemMessage("You are a helpful assistant"), ]; - expect( - () => { - convertBaseMessagesToContent(messages, false, false); - } - ).toThrow("System message should be the first one"); + expect(() => { + convertBaseMessagesToContent(messages, false, false); + }).toThrow("System message should be the first one"); }); test("Input has multiple system messages, convert system message is false", async () => { @@ -289,25 +291,23 @@ test("Input has multiple system messages, convert system message is false", asyn new SystemMessage("You are a helpful assistant"), new SystemMessage("You are not a helpful assistant"), ]; - expect( - () => { - convertBaseMessagesToContent(messages, false, false); - } - ).toThrow("System message should be the first one"); + expect(() => { + convertBaseMessagesToContent(messages, false, false); + }).toThrow("System message should be the first one"); }); test("Input has no system message and one user message, convert system message is false", async () => { - const messages = [ - new HumanMessage("What's the weather like in new york?"), - ]; - const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, false); + const messages = [new HumanMessage("What's the weather like in new york?")]; + const messagesAsGoogleContent = convertBaseMessagesToContent( + messages, + false, + false + ); expect(messagesAsGoogleContent).toEqual([ { role: "user", - parts: [ - { text: "What's the weather like in new york?" }, - ], + parts: [{ text: "What's the weather like in new york?" }], }, ]); }); @@ -318,26 +318,24 @@ test("Input has no system message and multiple user message, convert system mess new HumanMessage("What's the weather like in toronto?"), new HumanMessage("What's the weather like in los angeles?"), ]; - const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, false); + const messagesAsGoogleContent = convertBaseMessagesToContent( + messages, + false, + false + ); expect(messagesAsGoogleContent).toEqual([ { role: "user", - parts: [ - { text: "What's the weather like in new york?" }, - ], + parts: [{ text: "What's the weather like in new york?" }], }, { role: "user", - parts: [ - { text: "What's the weather like in toronto?" }, - ], + parts: [{ text: "What's the weather like in toronto?" }], }, { role: "user", - parts: [ - { text: "What's the weather like in los angeles?" }, - ], + parts: [{ text: "What's the weather like in los angeles?" }], }, ]); }); @@ -345,23 +343,23 @@ test("Input has no system message and multiple user message, convert system mess test("Input has single system message followed by one user message, convert system message is true", async () => { const messages = [ new SystemMessage("You are a helpful assistant"), - new HumanMessage("What's the weather like in new york?") + new HumanMessage("What's the weather like in new york?"), ]; - const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, true); + const messagesAsGoogleContent = convertBaseMessagesToContent( + messages, + false, + true + ); expect(messagesAsGoogleContent).toEqual([ { role: "system", - parts: [ - { text: "You are a helpful assistant" } - ] + parts: [{ text: "You are a helpful assistant" }], }, { role: "user", - parts: [ - { text: "What's the weather like in new york?" } - ], + parts: [{ text: "What's the weather like in new york?" }], }, ]); }); @@ -369,7 +367,7 @@ test("Input has single system message followed by one user message, convert syst test("Input has single system message that is not the first message, convert system message is true", async () => { const messages = [ new HumanMessage("What's the weather like in new york?"), - new SystemMessage("You are a helpful assistant") + new SystemMessage("You are a helpful assistant"), ]; expect(() => convertBaseMessagesToContent(messages, false, true)).toThrow( @@ -380,7 +378,7 @@ test("Input has single system message that is not the first message, convert sys test("Input has multiple system message, convert system message is true", async () => { const messages = [ new SystemMessage("What's the weather like in new york?"), - new SystemMessage("You are a helpful assistant") + new SystemMessage("You are a helpful assistant"), ]; expect(() => convertBaseMessagesToContent(messages, false, true)).toThrow( @@ -389,18 +387,18 @@ test("Input has multiple system message, convert system message is true", async }); test("Input has no system message and one user message, convert system message is true", async () => { - const messages = [ - new HumanMessage("What's the weather like in new york?") - ]; + const messages = [new HumanMessage("What's the weather like in new york?")]; - const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, true); + const messagesAsGoogleContent = convertBaseMessagesToContent( + messages, + false, + true + ); expect(messagesAsGoogleContent).toEqual([ { role: "user", - parts: [ - { text: "What's the weather like in new york?" } - ], + parts: [{ text: "What's the weather like in new york?" }], }, ]); }); @@ -409,29 +407,27 @@ test("Input has no system message and multiple user messages, convert system mes const messages = [ new HumanMessage("What's the weather like in new york?"), new HumanMessage("Will it rain today?"), - new HumanMessage("How about next week?") + new HumanMessage("How about next week?"), ]; - const messagesAsGoogleContent = convertBaseMessagesToContent(messages, false, true); + const messagesAsGoogleContent = convertBaseMessagesToContent( + messages, + false, + true + ); expect(messagesAsGoogleContent).toEqual([ { role: "user", - parts: [ - { text: "What's the weather like in new york?" } - ], + parts: [{ text: "What's the weather like in new york?" }], }, { role: "user", - parts: [ - { text: "Will it rain today?" } - ], + parts: [{ text: "Will it rain today?" }], }, { role: "user", - parts: [ - { text: "How about next week?" } - ], + parts: [{ text: "How about next week?" }], }, ]); -}); \ No newline at end of file +}); diff --git a/libs/langchain-google-genai/src/utils/common.ts b/libs/langchain-google-genai/src/utils/common.ts index 58ead85a2abf..e2e07f3c61df 100644 --- a/libs/langchain-google-genai/src/utils/common.ts +++ b/libs/langchain-google-genai/src/utils/common.ts @@ -181,7 +181,7 @@ export function convertMessageContentToParts( export function convertBaseMessagesToContent( messages: BaseMessage[], isMultimodalModel: boolean, - convertSystemMessageToHumanContent: boolean = false + convertSystemMessageToHumanContent: boolean = false ) { return messages.reduce<{ content: Content[]; @@ -225,7 +225,10 @@ export function convertBaseMessagesToContent( }; } let actualRole = role; - if (actualRole === "function" || (actualRole === "system" && !convertSystemMessageToHumanContent)) { + if ( + actualRole === "function" || + (actualRole === "system" && !convertSystemMessageToHumanContent) + ) { // GenerativeAI API will throw an error if the role is not "user" or "model." actualRole = "user"; } @@ -234,7 +237,8 @@ export function convertBaseMessagesToContent( parts, }; return { - mergeWithPreviousContent: author === "system" && !convertSystemMessageToHumanContent, + mergeWithPreviousContent: + author === "system" && !convertSystemMessageToHumanContent, content: [...acc.content, content], }; },