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

bugfixes in google Generative AI chat_model #3657

Merged
merged 5 commits into from
Dec 14, 2023
Merged
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
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

The type of the stopSequences is Array of strings, I'm not sure we need the next two lines.


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