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

google-genai[minor]: Allow openai formatted tools #5854

Merged
merged 7 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 1 addition & 1 deletion libs/langchain-google-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"author": "LangChain",
Copy link

Choose a reason for hiding this comment

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

Hey there! 👋 I noticed the package.json change in the dependencies section, specifically the update to "@langchain/core" from ">=0.2.5 <0.3.0" to ">=0.2.9 <0.3.0". This seems to be a dev dependency change. I'm flagging this for your review. Keep up the great work! 🚀

"license": "MIT",
"dependencies": {
"@langchain/core": ">=0.2.5 <0.3.0",
"@langchain/core": ">=0.2.9 <0.3.0",
"uuid": "^9.0.0",
"zod-to-json-schema": "^3.22.4"
},
Expand Down
30 changes: 27 additions & 3 deletions libs/langchain-google-common/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { AIMessageChunk } from "@langchain/core/messages";
import {
BaseLanguageModelInput,
StructuredOutputMethodOptions,
ToolDefinition,
isOpenAITool,
} from "@langchain/core/language_models/base";
import type { z } from "zod";
import {
Expand Down Expand Up @@ -55,7 +57,10 @@ import type {
GeminiFunctionDeclaration,
GeminiFunctionSchema,
} from "./types.js";
import { zodToGeminiParameters } from "./utils/zod_to_gemini_parameters.js";
import {
jsonSchemaToGeminiParameters,
zodToGeminiParameters,
} from "./utils/zod_to_gemini_parameters.js";

class ChatConnection<AuthOptions> extends AbstractGoogleLLMConnection<
BaseMessage[],
Expand Down Expand Up @@ -154,7 +159,11 @@ export interface ChatGoogleBaseInput<AuthOptions>
Pick<GoogleAIBaseLanguageModelCallOptions, "streamUsage"> {}

function convertToGeminiTools(
structuredTools: (StructuredToolInterface | Record<string, unknown>)[]
structuredTools: (
| StructuredToolInterface
| Record<string, unknown>
| ToolDefinition
)[]
): GeminiTool[] {
return [
{
Expand All @@ -168,6 +177,17 @@ function convertToGeminiTools(
parameters: jsonSchema as GeminiFunctionSchema,
};
}
if (isOpenAITool(structuredTool)) {
return {
name: structuredTool.function.name,
description:
structuredTool.function.description ??
`A function available to call.`,
parameters: jsonSchemaToGeminiParameters(
structuredTool.function.parameters
),
};
}
return structuredTool as unknown as GeminiFunctionDeclaration;
}
),
Expand Down Expand Up @@ -293,7 +313,11 @@ export abstract class ChatGoogleBase<AuthOptions>
}

override bindTools(
tools: (StructuredToolInterface | Record<string, unknown>)[],
tools: (
| StructuredToolInterface
| Record<string, unknown>
| ToolDefinition
)[],
kwargs?: Partial<GoogleAIBaseLanguageModelCallOptions>
): Runnable<
BaseLanguageModelInput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,25 @@ export function zodToGeminiParameters(
// Gemini doesn't accept either the $schema or additionalProperties
// attributes, so we need to explicitly remove them.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// const jsonSchema = zodToJsonSchema(zodObj) as any;
const jsonSchema = removeAdditionalProperties(
zodToJsonSchema(zodObj) as GeminiJsonSchemaDirty
);
const { $schema, ...rest } = jsonSchema;

return rest;
}

export function jsonSchemaToGeminiParameters(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
schema: Record<string, any>
): GeminiFunctionSchema {
// Gemini doesn't accept either the $schema or additionalProperties
// attributes, so we need to explicitly remove them.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const jsonSchema = removeAdditionalProperties(
schema as GeminiJsonSchemaDirty
);
const { $schema, ...rest } = jsonSchema;

return rest;
}
2 changes: 1 addition & 1 deletion libs/langchain-google-genai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"license": "MIT",
"dependencies": {
"@google/generative-ai": "^0.7.0",
"@langchain/core": ">=0.2.5 <0.3.0",
"@langchain/core": ">=0.2.9 <0.3.0",
"zod-to-json-schema": "^3.22.4"
},
"devDependencies": {
Expand Down
7 changes: 6 additions & 1 deletion libs/langchain-google-genai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
BaseLanguageModelCallOptions,
BaseLanguageModelInput,
StructuredOutputMethodOptions,
ToolDefinition,
} from "@langchain/core/language_models/base";
import { StructuredToolInterface } from "@langchain/core/tools";
import {
Expand Down Expand Up @@ -344,7 +345,11 @@ export class ChatGoogleGenerativeAI
}

override bindTools(
tools: (StructuredToolInterface | Record<string, unknown>)[],
tools: (
| StructuredToolInterface
| Record<string, unknown>
| ToolDefinition
)[],
kwargs?: Partial<GoogleGenerativeAIChatCallOptions>
): Runnable<
BaseLanguageModelInput,
Expand Down
23 changes: 21 additions & 2 deletions libs/langchain-google-genai/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ import {
} from "@langchain/core/outputs";
import { StructuredToolInterface } from "@langchain/core/tools";
import { isStructuredTool } from "@langchain/core/utils/function_calling";
import { ToolDefinition, isOpenAITool } from "@langchain/core/language_models/base";
import { ToolCallChunk } from "@langchain/core/messages/tool";
import { zodToGenerativeAIParameters } from "./zod_to_genai_parameters.js";
import {
jsonSchemaToGeminiParameters,
zodToGenerativeAIParameters,
} from "./zod_to_genai_parameters.js";

export function getMessageAuthor(message: BaseMessage) {
const type = message._getType();
Expand Down Expand Up @@ -311,7 +315,11 @@ export function convertResponseContentToChatGenerationChunk(
}

export function convertToGenerativeAITools(
structuredTools: (StructuredToolInterface | Record<string, unknown>)[]
structuredTools: (
| StructuredToolInterface
| Record<string, unknown>
| ToolDefinition
)[]
): GoogleGenerativeAIFunctionDeclarationsTool[] {
if (
structuredTools.every(
Expand All @@ -336,6 +344,17 @@ export function convertToGenerativeAITools(
parameters: jsonSchema,
};
}
if (isOpenAITool(structuredTool)) {
return {
name: structuredTool.function.name,
description:
structuredTool.function.description ??
`A function available to call.`,
parameters: jsonSchemaToGeminiParameters(
structuredTool.function.parameters
),
};
}
return structuredTool as unknown as GenerativeAIFunctionDeclaration;
}
),
Expand Down
15 changes: 15 additions & 0 deletions libs/langchain-google-genai/src/utils/zod_to_genai_parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,18 @@ export function zodToGenerativeAIParameters(

return rest as GenerativeAIFunctionDeclarationSchema;
}

export function jsonSchemaToGeminiParameters(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
schema: Record<string, any>
): GenerativeAIFunctionDeclarationSchema {
// Gemini doesn't accept either the $schema or additionalProperties
// attributes, so we need to explicitly remove them.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const jsonSchema = removeAdditionalProperties(
schema as GenerativeAIJsonSchemaDirty
);
const { $schema, ...rest } = jsonSchema;

return rest as GenerativeAIFunctionDeclarationSchema;
}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10203,7 +10203,7 @@ __metadata:
resolution: "@langchain/google-common@workspace:libs/langchain-google-common"
dependencies:
"@jest/globals": ^29.5.0
"@langchain/core": ">=0.2.5 <0.3.0"
"@langchain/core": ">=0.2.9 <0.3.0"
"@langchain/scripts": ~0.0.14
"@swc/core": ^1.3.90
"@swc/jest": ^0.2.29
Expand Down Expand Up @@ -10270,7 +10270,7 @@ __metadata:
dependencies:
"@google/generative-ai": ^0.7.0
"@jest/globals": ^29.5.0
"@langchain/core": ">=0.2.5 <0.3.0"
"@langchain/core": ">=0.2.9 <0.3.0"
"@langchain/scripts": ~0.0.14
"@langchain/standard-tests": 0.0.0
"@swc/core": ^1.3.90
Expand Down
Loading