Tool use (a.k.a. function calling) lets the LLM choose to call a function instead of replying directly.
Code
import OpenAI from "openai";
const client = new OpenAI();
const tools = [{
type: "function",
function: {
name: "get_weather",
description: "Get current weather for a city",
parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
},
}];
const r = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "What's the weather in Miami?" }],
tools,
});
// r.choices[0].message.tool_calls -> [{ name: "get_weather", arguments: '{"city":"Miami"}' }]