Tool Calls
Examples for using function calling / tool use with the Gateway.Define and Execute Tools
Copy
Ask AI
import { Gateway } from "@adaline/gateway";
import { OpenAI } from "@adaline/openai";
import { Config } from "@adaline/types";
const gateway = new Gateway();
const openai = new OpenAI();
const gpt4o = openai.chatModel({ modelName: "gpt-4o", apiKey: process.env.OPENAI_API_KEY });
// Define tools with handlers
const tools = [
{
type: "function",
definition: {
schema: {
name: "get_weather",
description: "Get current weather for a city",
parameters: {
type: "object",
properties: {
location: { type: "string", description: "City name" },
},
required: ["location"],
},
},
},
handler: async ({ location }) => {
// Your API call or logic here
return { temperature: 72, condition: "sunny", location };
},
},
];
// Step 1: Send the request — the model may return tool calls
const response = await gateway.completeChat({
model: gpt4o,
config: Config().parse({ temperature: 0 }),
messages: [
{ role: "user", content: [{ modality: "text", value: "What's the weather in San Francisco?" }] },
],
tools,
});
// Step 2: If the model requested tool calls, execute them
if (response.toolCalls && response.toolCalls.length > 0) {
const toolResponses = await gateway.getToolResponses({
tools,
toolCalls: response.toolCalls,
});
// Step 3: Send tool results back to the model
const finalResponse = await gateway.completeChat({
model: gpt4o,
config: Config().parse({ temperature: 0 }),
messages: [
{ role: "user", content: [{ modality: "text", value: "What's the weather in San Francisco?" }] },
response.response,
...toolResponses.responses,
],
tools,
});
console.log(finalResponse.response);
}
Multiple Tools
Copy
Ask AI
const tools = [
{
type: "function",
definition: {
schema: {
name: "search_web",
description: "Search the web for information",
parameters: {
type: "object",
properties: {
query: { type: "string", description: "Search query" },
},
required: ["query"],
},
},
},
handler: async ({ query }) => {
return { results: [`Result for: ${query}`] };
},
},
{
type: "function",
definition: {
schema: {
name: "calculate",
description: "Perform a math calculation",
parameters: {
type: "object",
properties: {
expression: { type: "string", description: "Math expression, e.g. '2 + 2'" },
},
required: ["expression"],
},
},
},
handler: async ({ expression }) => {
return { result: eval(expression) };
},
},
];