How to test custom OpenAI tools with FakeStreamingChatModel #5830
Replies: 1 comment 2 replies
-
To test custom OpenAI tools with import { test, expect, jest } from "@jest/globals";
import { AIMessage, HumanMessage, ToolMessage } from "@langchain/core/messages";
import { FakeStreamingChatModel } from "@langchain/core/models";
import { InMemoryCache } from "@langchain/core/caches";
test("Test FakeStreamingChatModel tool calling", async () => {
function getCurrentWeather(location: string) {
if (location.toLowerCase().includes("tokyo")) {
return JSON.stringify({ location, temperature: "10", unit: "celsius" });
} else if (location.toLowerCase().includes("san francisco")) {
return JSON.stringify({
location,
temperature: "72",
unit: "fahrenheit",
});
} else {
return JSON.stringify({ location, temperature: "22", unit: "celsius" });
}
}
const chat = new FakeStreamingChatModel({
modelName: "gpt-3.5-turbo-1106",
maxTokens: 128,
}).bind({
tools: [
{
type: "function",
function: {
name: "get_current_weather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
},
required: ["location"],
},
},
},
],
tool_choice: "auto",
});
const res = await chat.invoke([
["human", "What's the weather like in San Francisco, Tokyo, and Paris?"],
]);
console.log(JSON.stringify(res));
expect(res.additional_kwargs.tool_calls?.length).toBeGreaterThan(1);
const toolMessages = res.additional_kwargs.tool_calls!.map(
(toolCall) =>
new ToolMessage({
tool_call_id: toolCall.id,
name: toolCall.function.name,
content: getCurrentWeather(
JSON.parse(toolCall.function.arguments).location
),
})
);
const finalResponse = await chat.invoke([
["human", "What's the weather like in San Francisco, Tokyo, and Paris?"],
res,
...toolMessages,
]);
console.log(finalResponse);
}); This code sets up a test for Additionally, you can refer to the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
I'm trying to invoke a tool on a test input via FakeStreamingChatModel. Is it possible to set the required properties to trigger the tool call? Thanks.
System Info
windows
Beta Was this translation helpful? Give feedback.
All reactions