diff --git a/libs/langchain-google-genai/src/chat_models.ts b/libs/langchain-google-genai/src/chat_models.ts index 201fb38dd106..0dd0e8570089 100644 --- a/libs/langchain-google-genai/src/chat_models.ts +++ b/libs/langchain-google-genai/src/chat_models.ts @@ -198,6 +198,8 @@ export class ChatGoogleGenerativeAI throw new Error("`topK` must be a positive integer"); } + this.stopSequences = fields?.stopSequences ?? this.stopSequences; + this.apiKey = fields?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY"); if (!this.apiKey) { throw new Error( diff --git a/libs/langchain-google-genai/src/tests/chat_models.int.test.ts b/libs/langchain-google-genai/src/tests/chat_models.int.test.ts index 656973425a92..46dffb92e05f 100644 --- a/libs/langchain-google-genai/src/tests/chat_models.int.test.ts +++ b/libs/langchain-google-genai/src/tests/chat_models.int.test.ts @@ -21,6 +21,20 @@ test("Test Google AI generation", async () => { expect(res).toBeTruthy(); }); +test("Test Google AI generation with a stop sequence", async () => { + const model = new ChatGoogleGenerativeAI({ + stopSequences: ["two", "2"], + }); + const res = await model.invoke([ + ["human", `What are the first three positive whole numbers?`], + ]); + console.log(JSON.stringify(res, null, 2)); + expect(res).toBeTruthy(); + expect(res.additional_kwargs.finishReason).toBe("STOP"); + expect(res.content).not.toContain("2"); + expect(res.content).not.toContain("two"); +}); + test("Test Google AI generation with a system message", async () => { const model = new ChatGoogleGenerativeAI({}); const res = await model.generate([ diff --git a/libs/langchain-google-genai/src/utils.ts b/libs/langchain-google-genai/src/utils.ts index c16cd392538e..d8d7c227cbef 100644 --- a/libs/langchain-google-genai/src/utils.ts +++ b/libs/langchain-google-genai/src/utils.ts @@ -177,7 +177,7 @@ export function mapGenerateContentResultToChatResult( message: new AIMessage({ content: text, name: content === null ? undefined : content.role, - additional_kwargs: {}, + additional_kwargs: generationInfo, }), generationInfo, }; @@ -202,6 +202,8 @@ export function convertResponseContentToChatGenerationChunk( message: new AIMessageChunk({ content: text, name: content === null ? undefined : content.role, + // Each chunk can have unique "generationInfo", and merging strategy is unclear, + // so leave blank for now. additional_kwargs: {}, }), generationInfo,