From c3842e80ef5373bb95494c9edfa2ba6f97c2c2f4 Mon Sep 17 00:00:00 2001 From: Miguel Fernandez Date: Mon, 27 May 2024 11:42:34 +0200 Subject: [PATCH 1/5] Fix: [libs/langchain-google-common] already existing utils test --- libs/langchain-google-common/src/tests/utils.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/langchain-google-common/src/tests/utils.test.ts b/libs/langchain-google-common/src/tests/utils.test.ts index b2d6bcf0b56f..809ed1c1b402 100644 --- a/libs/langchain-google-common/src/tests/utils.test.ts +++ b/libs/langchain-google-common/src/tests/utils.test.ts @@ -18,7 +18,7 @@ test("zodToGeminiParameters can convert zod schema to gemini schema", () => { expect(convertedSchema.type).toBe("object"); expect(convertedSchema.description).toBe("A simple calculator tool"); - expect(convertedSchema).not.toContain("additionalProperties"); + expect((convertedSchema as any).additionalProperties).toBeUndefined(); expect(convertedSchema.properties).toEqual({ operation: { type: "string", From e6ece439079cc621bd3fba6df427a2da32bb8b40 Mon Sep 17 00:00:00 2001 From: Miguel Fernandez Date: Mon, 27 May 2024 11:43:42 +0200 Subject: [PATCH 2/5] Fix: [lib/langchain-google-common] Add failing test for arrays containing additional properties --- .../src/tests/utils.test.ts | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/libs/langchain-google-common/src/tests/utils.test.ts b/libs/langchain-google-common/src/tests/utils.test.ts index 809ed1c1b402..ae3d26667f16 100644 --- a/libs/langchain-google-common/src/tests/utils.test.ts +++ b/libs/langchain-google-common/src/tests/utils.test.ts @@ -45,3 +45,37 @@ test("zodToGeminiParameters can convert zod schema to gemini schema", () => { "childObject", ]); }); + +test("zodToGeminiParameters removes additional properties from arrays", () => { + const zodSchema = z + .object({ + people: z + .object({ + name: z.string().describe("The name of a person"), + }) + .array() + .describe("person elements"), + }) + .describe("A list of people"); + + const convertedSchema = zodToGeminiParameters(zodSchema); + expect(convertedSchema.type).toBe("object"); + expect(convertedSchema.description).toBe("A list of people"); + expect((convertedSchema as any).additionalProperties).toBeUndefined(); + + const peopleSchema = convertedSchema?.properties?.["people"]; + expect(peopleSchema).not.toBeUndefined(); + + if (peopleSchema !== undefined) { + expect(peopleSchema.type).toBe("array"); + expect((peopleSchema as any).additionalProperties).toBeUndefined(); + expect(peopleSchema.description).toBe("person elements"); + } + + const arrayItemsSchema = peopleSchema?.items; + expect(arrayItemsSchema).not.toBeUndefined(); + if (arrayItemsSchema !== undefined) { + expect(arrayItemsSchema.type).toBe("object"); + expect((arrayItemsSchema as any).additionalProperties).toBeUndefined(); + } +}); From 226a86bd76d62e8fc5ecf0ffd638f626234eabc1 Mon Sep 17 00:00:00 2001 From: Miguel Fernandez Date: Mon, 27 May 2024 12:00:41 +0200 Subject: [PATCH 3/5] Fix: [lib/langchain-google-common] Fix failing test --- libs/langchain-google-common/src/types.ts | 1 + .../src/utils/zod_to_gemini_parameters.ts | 3 +++ 2 files changed, 4 insertions(+) diff --git a/libs/langchain-google-common/src/types.ts b/libs/langchain-google-common/src/types.ts index 3c10ee4fd41a..896db07e2757 100644 --- a/libs/langchain-google-common/src/types.ts +++ b/libs/langchain-google-common/src/types.ts @@ -295,6 +295,7 @@ export type GeminiJsonSchema = Record & { }; export interface GeminiJsonSchemaDirty extends GeminiJsonSchema { + items?: GeminiJsonSchemaDirty; properties?: Record; additionalProperties?: boolean; } diff --git a/libs/langchain-google-common/src/utils/zod_to_gemini_parameters.ts b/libs/langchain-google-common/src/utils/zod_to_gemini_parameters.ts index d8dc969c0c4b..039d4e0d7ea9 100644 --- a/libs/langchain-google-common/src/utils/zod_to_gemini_parameters.ts +++ b/libs/langchain-google-common/src/utils/zod_to_gemini_parameters.ts @@ -19,6 +19,9 @@ function removeAdditionalProperties( const keys = Object.keys(updatedSchema.properties); removeProperties(updatedSchema.properties, keys, 0); } + if (Object.hasOwn(updatedSchema, "items") && updatedSchema.items) { + updatedSchema.items = removeAdditionalProperties(updatedSchema.items); + } return updatedSchema; } From 38c9628de6bc6e5f2d0990bf97ea8b5abc784665 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Tue, 28 May 2024 16:55:26 -0700 Subject: [PATCH 4/5] Update int test --- .../src/tests/chat_models.int.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libs/langchain-google-vertexai-web/src/tests/chat_models.int.test.ts b/libs/langchain-google-vertexai-web/src/tests/chat_models.int.test.ts index a9a870ebacb9..e737d61d0bdf 100644 --- a/libs/langchain-google-vertexai-web/src/tests/chat_models.int.test.ts +++ b/libs/langchain-google-vertexai-web/src/tests/chat_models.int.test.ts @@ -18,7 +18,9 @@ import { ChatVertexAI } from "../chat_models.js"; class WeatherTool extends StructuredTool { schema = z.object({ - location: z.string().describe("The name of city to get the weather for."), + locations: z + .array(z.object({ name: z.string() })) + .describe("The name of cities to get the weather for."), }); description = @@ -28,7 +30,7 @@ class WeatherTool extends StructuredTool { async _call(input: z.infer) { console.log(`WeatherTool called with input: ${input}`); - return `The weather in ${input.location} is 25°C`; + return `The weather in ${JSON.stringify(input.locations)} is 25°C`; } } @@ -128,7 +130,7 @@ describe("Google APIKey Chat", () => { test("Tool call", async () => { const chat = new ChatVertexAI().bindTools([new WeatherTool()]); - const res = await chat.invoke("What is the weather in SF"); + const res = await chat.invoke("What is the weather in SF and LA"); console.log(res); expect(res.tool_calls?.length).toEqual(1); expect(res.tool_calls?.[0].args).toEqual( From 41a4b31e2208a80681f6dc2dce6e8dec523b2c9f Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Tue, 28 May 2024 17:06:17 -0700 Subject: [PATCH 5/5] lint --- libs/langchain-google-common/src/tests/utils.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/langchain-google-common/src/tests/utils.test.ts b/libs/langchain-google-common/src/tests/utils.test.ts index ae3d26667f16..84f12dca4dd0 100644 --- a/libs/langchain-google-common/src/tests/utils.test.ts +++ b/libs/langchain-google-common/src/tests/utils.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { expect, test } from "@jest/globals"; import { z } from "zod"; import { zodToGeminiParameters } from "../utils/zod_to_gemini_parameters.js"; @@ -63,7 +64,7 @@ test("zodToGeminiParameters removes additional properties from arrays", () => { expect(convertedSchema.description).toBe("A list of people"); expect((convertedSchema as any).additionalProperties).toBeUndefined(); - const peopleSchema = convertedSchema?.properties?.["people"]; + const peopleSchema = convertedSchema?.properties?.people; expect(peopleSchema).not.toBeUndefined(); if (peopleSchema !== undefined) {