Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Sep 6, 2024
1 parent 6d1721d commit 1698535
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
17 changes: 12 additions & 5 deletions libs/langchain-anthropic/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import {
type StructuredOutputMethodOptions,
type BaseLanguageModelInput,
type ToolDefinition,
isOpenAITool,
} from "@langchain/core/language_models/base";
import { zodToJsonSchema } from "zod-to-json-schema";
Expand Down Expand Up @@ -698,16 +697,24 @@ export class ChatAnthropicMessages<
name: tool.function.name,
description: tool.function.description,
input_schema: tool.function.parameters as AnthropicTool.InputSchema,
}
};
}
if (isLangChainTool(tool)) {
return {
name: tool.name,
description: tool.description,
input_schema: zodToJsonSchema(tool.schema) as AnthropicTool.InputSchema,
}
input_schema: zodToJsonSchema(
tool.schema
) as AnthropicTool.InputSchema,
};
}
throw new Error(`Unknown tool type passed to ChatAnthropic: ${JSON.stringify(tool, null, 2)}`);
throw new Error(
`Unknown tool type passed to ChatAnthropic: ${JSON.stringify(
tool,
null,
2
)}`
);
});
}

Expand Down
27 changes: 27 additions & 0 deletions libs/langchain-anthropic/src/tests/chat_models-tools.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,30 @@ test("streaming with structured output", async () => {
}
expect(typeof finalChunk2).toEqual("object");
});

test("Can bound and invoke different tool types", async () => {
const langchainTool = {
name: "get_weather_lc",
description: "Get the weather of a specific location.",
schema: zodSchema,
};
const openaiTool = {
type: "function",
function: {
name: "get_weather_oai",
description: "Get the weather of a specific location.",
parameters: zodToJsonSchema(zodSchema),
},
};
const anthropicTool = {
name: "get_weather_ant",
description: "Get the weather of a specific location.",
input_schema: zodToJsonSchema(zodSchema),
};
const tools = [langchainTool, openaiTool, anthropicTool];
const modelWithTools = model.bindTools(tools);
const result = await modelWithTools.invoke(
"Whats the current weather in san francisco?"
);
expect(result.tool_calls?.length).toBeGreaterThanOrEqual(1);
});

0 comments on commit 1698535

Please sign in to comment.