import Edgee from 'edgee';
const edgee = new Edgee('your-api-key');
// Define the weather function
async function getWeather(location: string, unit: string = 'celsius') {
// Simulate API call
return {
location,
temperature: 15,
unit,
condition: 'sunny'
};
}
// Step 1: Initial request with tools
const response1 = await edgee.send({
model: 'gpt-4o',
input: {
messages: [
{ role: 'user', content: 'What is the weather in Paris and Tokyo?' }
],
tools: [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city name'
},
unit: {
type: 'string',
enum: ['celsius', 'fahrenheit'],
description: 'Temperature unit'
}
},
required: ['location']
}
}
}
],
tool_choice: 'auto'
}
});
// Step 2: Execute all tool calls
const messages = [
{ role: 'user', content: 'What is the weather in Paris and Tokyo?' },
response1.message! // Include assistant's message
];
if (response1.toolCalls) {
for (const toolCall of response1.toolCalls) {
const args = JSON.parse(toolCall.function.arguments);
const result = await getWeather(args.location, args.unit);
messages.push({
role: 'tool',
tool_call_id: toolCall.id,
content: JSON.stringify(result)
});
}
}
// Step 3: Send results back
const response2 = await edgee.send({
model: 'gpt-4o',
input: {
messages,
tools: [
// Keep tools available for follow-up
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a location',
parameters: {
type: 'object',
properties: {
location: { type: 'string', description: 'The city name' },
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }
},
required: ['location']
}
}
}
]
}
});
console.log(response2.text);