Skip to content

Commit

Permalink
add support in google common for oai tools
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Jun 12, 2024
1 parent b755752 commit 0f2a402
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
19 changes: 16 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,7 @@ import { AIMessageChunk } from "@langchain/core/messages";
import {
BaseLanguageModelInput,
StructuredOutputMethodOptions,
ToolDefinition,
} from "@langchain/core/language_models/base";
import type { z } from "zod";
import {
Expand Down Expand Up @@ -55,7 +56,8 @@ 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";
import { isOpenAITool } from "@langchain/core/utils/is_openai_tool";

class ChatConnection<AuthOptions> extends AbstractGoogleLLMConnection<
BaseMessage[],
Expand Down Expand Up @@ -153,7 +155,7 @@ export interface ChatGoogleBaseInput<AuthOptions>
GoogleAISafetyParams {}

function convertToGeminiTools(
structuredTools: (StructuredToolInterface | Record<string, unknown>)[]
structuredTools: (StructuredToolInterface | Record<string, unknown> | ToolDefinition)[]
): GeminiTool[] {
return [
{
Expand All @@ -167,6 +169,17 @@ function convertToGeminiTools(
parameters: jsonSchema as GeminiFunctionSchema,
};
}
if (isOpenAITool(structuredTool)) {
let parameters: GeminiFunctionSchema = {
type: "object", // Default to `object` if no `type` field is provided.
...structuredTool.function.parameters,
};
return {
name: structuredTool.function.name,
description: structuredTool.function.description ?? `A function available to call.`,
parameters: jsonSchemaToGeminiParameters(parameters),
};
}
return structuredTool as unknown as GeminiFunctionDeclaration;
}
),
Expand Down Expand Up @@ -290,7 +303,7 @@ 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
16 changes: 16 additions & 0 deletions libs/langchain-google-common/src/utils/zod_to_gemini_parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@ export function zodToGeminiParameters(

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 = zodToJsonSchema(zodObj) as any;
const jsonSchema = removeAdditionalProperties(
schema as GeminiJsonSchemaDirty
);
const { $schema, ...rest } = jsonSchema;

return rest;
}
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ export abstract class ChatModelIntegrationTests<
}]);

const result: AIMessage = await modelWithTools.invoke("What is 1 + 2");
expect(result).toBeInstanceOf(AIMessage);
expect(result.tool_calls).toHaveLength(1);
if (!result.tool_calls) {
throw new Error("result.tool_calls is undefined");
Expand Down

0 comments on commit 0f2a402

Please sign in to comment.