import OpenAI from "openai";
const API_KEY = "sk-your-key";
const client = new OpenAI({
apiKey: API_KEY,
baseURL: "https://moxus.ai/v1",
});
function getWeather(city) {
// Call a real weather API here in production.
return { temp: "25 C", condition: "sunny", city };
}
const tools = [
{
type: "function",
function: {
name: "get_weather",
description: "Get the current weather for a city",
parameters: {
type: "object",
properties: {
city: { type: "string", description: "City name" },
},
required: ["city"],
},
},
},
];
const messages = [{ role: "user", content: "What is the weather in Beijing today?" }];
const response = await client.chat.completions.create({
model: "gpt-5.4-mini",
messages,
tools,
tool_choice: "auto",
});
const message = response.choices[0].message;
if (message.tool_calls?.length) {
messages.push(message);
for (const call of message.tool_calls) {
if (call.function.name !== "get_weather") {
throw new Error(`Tool is not allowed: ${call.function.name}`);
}
const args = JSON.parse(call.function.arguments);
const result = getWeather(args.city);
messages.push({
role: "tool",
tool_call_id: call.id,
content: JSON.stringify(result),
});
}
const final = await client.chat.completions.create({
model: "gpt-5.4-mini",
messages,
tools,
});
console.log(final.choices[0].message.content);
} else {
console.log(message.content);
}