Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

feat: add OpenAI toolset examples using HACKERNEWS_GET_USER

Devin AI394d33eaf82e
diff --git a/examples/openai/package.json b/examples/openai/package.json
index 973850d89..48e4debc9 100644
--- a/examples/openai/package.json
+++ b/examples/openai/package.json
@@ -6,6 +6,9 @@
"main": "index.js",
"scripts": {
"start": "bun src/index.ts",
+ "chat": "bun src/chat-completions.ts",
+ "assistant": "bun src/assistants.ts",
+ "examples": "bun src/examples.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
@@ -14,6 +17,7 @@
"packageManager": "[email protected]",
"dependencies": {
"@composio/core": "workspace:*",
+ "dotenv": "^16.4.1",
"openai": "^4.94.0"
},
"devDependencies": {
diff --git a/examples/openai/src/assistants.ts b/examples/openai/src/assistants.ts
new file mode 100644
index 000000000..b7d80d2b4
--- /dev/null
+++ b/examples/openai/src/assistants.ts
@@ -0,0 +1,92 @@
+/**
+ * OpenAI Assistants with Composio Tool Example
+ *
+ * This example demonstrates how to use the Composio OpenAIToolset with OpenAI Assistants 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 Assistants with Composio tool
+ */
+async function main() {
+ try {
+ console.log('πŸ”„ Setting up Composio and fetching tool...');
+
+ const tool = await composio.getToolBySlug('default', 'HACKERNEWS_GET_USER');
+
+ console.log('πŸ”„ Creating OpenAI Assistant with Composio tool...');
+
+ const assistant = await openai.beta.assistants.create({
+ name: "HackerNews Helper",
+ instructions: "You are a helpful assistant that can fetch information about HackerNews users.",
+ model: "gpt-4o",
+ tools: [tool],
+ });
+
+ console.log(`βœ… Assistant created with ID: ${assistant.id}`);
+
+ const thread = await openai.beta.threads.create();
+ console.log(`βœ… Thread created with ID: ${thread.id}`);
+
+ const query = "Get information about the HackerNews user 'pg'";
+ console.log(`πŸ”„ Adding message to thread: "${query}"`);
+
+ await openai.beta.threads.messages.create(thread.id, {
+ role: "user",
+ content: query,
+ });
+
+ console.log('πŸ”„ Running the assistant...');
+ const run = await openai.beta.threads.runs.create(thread.id, {
+ assistant_id: assistant.id,
+ });
+
+ console.log('πŸ”„ Waiting for assistant response and handling tool calls...');
+ const completedRun = await composio.toolset.waitAndHandleAssistantToolCalls(
+ 'default',
+ openai,
+ run,
+ thread,
+ {
+ connectedAccountId: '', // Optional connected account ID
+ customAuthParams: {}, // Optional custom auth parameters
+ }
+ );
+
+ console.log(`βœ… Run completed with status: ${completedRun.status}`);
+
+ const messages = await openai.beta.threads.messages.list(thread.id);
+
+ console.log('πŸ€– Assistant responses:');
+ for (const message of messages.data) {
+ if (message.role === 'assistant') {
+ for (const content of message.content) {
+ if (content.type === 'text') {
+ console.log(`- ${content.text.value}`);
+ }
+ }
+ }
+ }
+
+ await openai.beta.assistants.del(assistant.id);
+ console.log('🧹 Cleaned up: Assistant deleted');
+
+ } catch (error) {
+ console.error('❌ Error:', error);
+ }
+}
+
+main();
diff --git a/examples/openai/src/chat-completions.ts b/examples/openai/src/chat-completions.ts
new file mode 100644
index 000000000..d58ad208c
--- /dev/null
+++ b/examples/openai/src/chat-completions.ts
@@ -0,0 +1,81 @@
+/**
+ * OpenAI Chat Completions with Composio Tool Example
+ *
+ * This example demonstrates how to use the Composio OpenAIToolset 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 tool = await composio.getToolBySlug('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: [tool],
+ tool_choice: 'auto',
+ });
+
+ if (response.choices[0].message.tool_calls) {
+ console.log('πŸ”§ Assistant is using tool:', response.choices[0].message.tool_calls[0].function.name);
+
+ const toolResult = await composio.toolset.executeToolCall(
+ 'default',
+ response.choices[0].message.tool_calls[0],
+ {
+ connectedAccountId: '', // Optional connected account ID
+ customAuthParams: {}, // Optional custom auth parameters
+ }
+ );
+
+ 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);
+ }
+}
+
+main();
diff --git a/examples/openai/src/examples.ts b/examples/openai/src/examples.ts
index 1f6963d1e..df6c59203 100644
--- a/examples/openai/src/examples.ts
+++ b/examples/openai/src/examples.ts
@@ -1,10 +1,38 @@
-import { Composio, ToolListParamsSchema } from '@composio/core';
-import { OpenAI } from 'openai';
+/**
+ * Basic Composio Tool Fetching Example
+ *
+ * This example demonstrates how to fetch a tool from Composio.
+ * It retrieves the HACKERNEWS_GET_USER tool and logs its schema.
+ */
+import { Composio } from '@composio/core';
+import dotenv from 'dotenv';
+
+dotenv.config();
const composio = new Composio({
apiKey: process.env.COMPOSIO_API_KEY,
});
-const tool = await composio.getToolBySlug('test-user-id', 'HACKERNEWS_GET_USER');
+/**
+ * Main function to demonstrate fetching a tool from Composio
+ */
+async function main() {
+ try {
+ console.log('Fetching HACKERNEWS_GET_USER tool...');
+ const tool = await composio.getToolBySlug('default', 'HACKERNEWS_GET_USER');
+
+ console.log('Tool details:');
+ console.log(JSON.stringify(tool, null, 2));
+
+ console.log('\nInput parameters:');
+ if (tool.function.parameters.properties) {
+ for (const [key, value] of Object.entries(tool.function.parameters.properties)) {
+ console.log(`- ${key}: ${JSON.stringify(value)}`);
+ }
+ }
+ } catch (error) {
+ console.error('Error fetching tool:', error);
+ }
+}
-console.log(tool);
+main();