Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
/**
* OpenAI Chat Completions with Composio Tool Example
*
* This example demonstrates how to use the Composio OpenAIProvider with OpenAI chat completions API.
* It uses the HACKERNEWS_GET_USER tool to fetch information about a HackerNews user.
*/
import { Composio } from '@composio/core';
import { OpenAI } from 'openai';
import dotenv from 'dotenv';
dotenv.config();
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const composio = new Composio({
apiKey: process.env.COMPOSIO_API_KEY,
});
/**
* Main function to demonstrate OpenAI Chat Completions with Composio tool
*/
async function main() {
try {
console.log('π Setting up Composio and fetching tool...');
const tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER');
const query = "Find information about the HackerNews user 'pg'";
console.log(`π Sending query to OpenAI: "${query}"`);
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{
role: 'system',
content: 'You are a helpful assistant that can use tools to answer questions.',
},
{ role: 'user', content: query },
],
tools: tools,
tool_choice: 'auto',
});
if (
response.choices[0].message.tool_calls &&
response.choices[0].message.tool_calls[0].type === 'function'
) {
console.log(
'π§ Assistant is using tool:',
response.choices[0].message.tool_calls[0].function.name
);
const toolResult = await composio.provider.executeToolCall(
'default',
response.choices[0].message.tool_calls[0],
{
connectedAccountId: '', // Optional connected account ID
}
);
console.log('β
Tool execution result:', JSON.parse(toolResult));
const finalResponse = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{
role: 'system',
content: 'You are a helpful assistant that can use tools to answer questions.',
},
{ role: 'user', content: query },
response.choices[0].message,
{
role: 'tool',
tool_call_id: response.choices[0].message.tool_calls[0].id,
content: toolResult,
},
],
});
console.log('π€ Final assistant response:', finalResponse.choices[0].message.content);
} else {
console.log('π€ Assistant response:', response.choices[0].message.content);
}
} catch (error) {
console.error('β Error:', error);
process.exitCode = 1;
}
}
main();