import OpenAI from 'openai';
import { StackOneToolSet } from '@stackone/ai';
// Initialize clients
const openai = new OpenAI();
const toolset = new StackOneToolSet();
// Get StackOne tools and convert to OpenAI format
const tools = toolset.getStackOneTools('hris_get_*', accountId);
const openAITools = tools.toOpenAI();
// Create completion with function calling
const completion = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{
role: 'system',
content: 'You are an HR assistant with access to employee data.'
},
{
role: 'user',
content: 'How many employees do we have?'
}
],
tools: openAITools,
tool_choice: 'auto'
});
// Execute any tool calls
const message = completion.choices[0].message;
if (message.tool_calls) {
for (const toolCall of message.tool_calls) {
const tool = tools.getTool(toolCall.function.name);
if (tool) {
const result = await tool.execute(
JSON.parse(toolCall.function.arguments)
);
console.log('Result:', result.data);
}
}
}