Skip to content

OpenAI

Use Chronary’s calendar tools with the OpenAI SDK’s function calling. new ChronaryToolkit(...).getTools() returns tool definitions in the chat.completions tools format, and toolkit.execute() runs the calls the model makes.

Terminal window
npm install @chronary/toolkit @chronary/sdk openai
import OpenAI from 'openai';
import { ChronaryToolkit } from '@chronary/toolkit/openai';
const toolkit = new ChronaryToolkit({
apiKey: process.env.CHRONARY_API_KEY!,
});
const tools = toolkit.getTools();
const client = new OpenAI();
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'When is my next calendar event?' }],
tools,
});
console.log(response.choices[0].message.tool_calls);

getTools() returns the tool definitions. When the model answers with tool_calls, run each one with toolkit.execute(name, args) and send the result back as a role: "tool" message, then call the model again until it answers in text:

const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
{ role: 'user', content: 'Book 30 minutes of focus time tomorrow at 9am.' },
];
for (let step = 0; step < 6; step++) {
const { choices } = await client.chat.completions.create({ model: 'gpt-4o', messages, tools });
const message = choices[0].message;
messages.push(message);
if (!message.tool_calls?.length) {
console.log(message.content);
break;
}
for (const call of message.tool_calls) {
if (call.type !== 'function') continue;
const result = await toolkit.execute(call.function.name, call.function.arguments);
messages.push({ role: 'tool', tool_call_id: call.id, content: JSON.stringify(result.result) });
}
}