Skip to content

Commit

Permalink
bugfixes in google Generative AI chat_model (#3657)
Browse files Browse the repository at this point in the history
* adding candidateCount + fixing stopSequences not being set in the request

* Update chat_models.ts

* Small Google fixes

* Fix test

* Format

---------

Co-authored-by: Jacob Lee <[email protected]>
  • Loading branch information
haouarihk and jacoblee93 authored Dec 14, 2023
1 parent f601bef commit 06fbae0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions libs/langchain-google-genai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions libs/langchain-google-genai/src/tests/chat_models.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
4 changes: 3 additions & 1 deletion libs/langchain-google-genai/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export function mapGenerateContentResultToChatResult(
message: new AIMessage({
content: text,
name: content === null ? undefined : content.role,
additional_kwargs: {},
additional_kwargs: generationInfo,
}),
generationInfo,
};
Expand All @@ -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,
Expand Down

2 comments on commit 06fbae0

@vercel
Copy link

@vercel vercel bot commented on 06fbae0 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 06fbae0 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

langchainjs-docs – ./docs/core_docs/

langchainjs-docs-langchain.vercel.app
langchainjs-docs-git-main-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
js.langchain.com

Please sign in to comment.