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(google-common): Gemini logprobs #7472

Merged
merged 7 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 3 additions & 1 deletion docs/core_docs/docs/integrations/chat/google_vertex_ai.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"\n",
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
"| ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | \n",
"| ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | \n",
"\n",
"Note that while logprobs are supported, Gemini has fairly restricted usage of them.\n",
"\n",
"## Setup\n",
"\n",
Expand Down
8 changes: 8 additions & 0 deletions libs/langchain-google-common/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,16 @@ export abstract class ChatGoogleBase<AuthOptions>

topK = 40;

presencePenalty: number;

frequencyPenalty: number;

stopSequences: string[] = [];

logprobs: boolean = false;

topLogprobs: number = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: you can just do topLogprobs = 0 and logprobs = false


safetySettings: GoogleAISafetySetting[] = [];

// May intentionally be undefined, meaning to compute this.
Expand Down
33 changes: 33 additions & 0 deletions libs/langchain-google-common/src/tests/chat_models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,39 @@ describe("Mock ChatGoogle - Gemini", () => {

expect(record.opts.data.tools[0]).toHaveProperty("googleSearch");
});

test("7. logprobs", async () => {
const record: Record<string, any> = {};
const projectId = mockId();
const authOptions: MockClientAuthInfo = {
record,
projectId,
resultFile: "chat-7-mock.json",
};

const model = new ChatGoogle({
authOptions,
modelName: "gemini-1.5-flash-002",
logprobs: true,
topLogprobs: 5,
});
const result = await model.invoke(
"What are some names for a company that makes fancy socks?"
);
// console.log(JSON.stringify(result,null,1));
expect(result.response_metadata).toHaveProperty("logprobs");
expect(result.response_metadata.logprobs).toHaveProperty("content");
const logprobs = result.response_metadata.logprobs.content;
expect(Array.isArray(logprobs)).toBeTruthy();
expect(logprobs).toHaveLength(303);
const first = logprobs[0];
expect(first.token).toEqual("Here");
expect(first.logprob).toEqual(-0.25194553);
expect(first.bytes).toEqual([72, 101, 114, 101]);
expect(first).toHaveProperty("top_logprobs");
expect(Array.isArray(first.top_logprobs)).toBeTruthy();
expect(first.top_logprobs).toHaveLength(5);
});
});

describe("Mock ChatGoogle - Anthropic", () => {
Expand Down
Loading
Loading