Replies: 1 comment 1 reply
-
@ifenghen Hello! I'm here to help you with any bugs, questions, or contributions you have. Let's work on this together. To define tools for OpenAI's latest Function Calling with the import { ChatOpenAI } from '@langchain/openai';
import { z } from 'zod';
// Define your tools with Zod schemas
const GetWeather = {
name: "GetWeather",
description: "Get the current weather in a given location",
schema: z.object({
location: z.string().describe("The city and state, e.g. San Francisco, CA")
}),
};
const GetPopulation = {
name: "GetPopulation",
description: "Get the current population in a given location",
schema: z.object({
location: z.string().describe("The city and state, e.g. San Francisco, CA")
}),
};
// Instantiate the ChatOpenAI model
const llm = new ChatOpenAI({
model: "gpt-4o",
temperature: 0,
maxTokens: undefined,
timeout: undefined,
maxRetries: 2,
});
// Bind tools with strict schema enforcement
const llmWithTools = llm.bindTools(
[GetWeather, GetPopulation],
{
strict: true // enforce tool args schema is respected
}
);
// Invoke the model with a query
const aiMsg = await llmWithTools.invoke(
"Which city is hotter today and which is bigger: LA or NY?"
);
console.log(aiMsg.tool_calls); This setup ensures that the model's output strictly adheres to the JSON Schema defined for each tool, providing more reliable and predictable results [1][2]. To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
no
Description
no
System Info
I want to use OpenAI's latest Function Calling "strict": true feature in the agent's tools, how should I define tools:https://platform.openai.com/docs/guides/function-calling?example=send-email#function-calling-with-structured-outputs
Beta Was this translation helpful? Give feedback.
All reactions