@devries-cameron20 / composio Mirror · read-only / commits / 394d33eaf
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 23, 03:00 UTC

feat: add OpenAI toolset examples using HACKERNEWS_GET_USER

Co-Authored-By: musthaq@composio.dev <musthu.gm@gmail.com>

Devin AI committed May 16, 2025 at 06:39 UTC 394d33eaf82ecc78cd75596917cfcd14f9321fdb
4 files changed +209 -4
examples/openai/package.json
+4
@@ -6,6 +6,9 @@
6 "main": "index.js",
7 "scripts": {
8 "start": "bun src/index.ts",
9 + "chat": "bun src/chat-completions.ts",
10 + "assistant": "bun src/assistants.ts",
11 + "examples": "bun src/examples.ts",
12 "test": "echo \"Error: no test specified\" && exit 1"
13 },
14 "keywords": [],
@@ -14,6 +17,7 @@
17 "packageManager": "pnpm@10.8.0",
18 "dependencies": {
19 "@composio/core": "workspace:*",
20 + "dotenv": "^16.4.1",
21 "openai": "^4.94.0"
22 },
23 "devDependencies": {
examples/openai/src/assistants.ts new
+92
@@ -0,0 +1,92 @@
1 +/**
2 + * OpenAI Assistants with Composio Tool Example
3 + *
4 + * This example demonstrates how to use the Composio OpenAIToolset with OpenAI Assistants API.
5 + * It uses the HACKERNEWS_GET_USER tool to fetch information about a HackerNews user.
6 + */
7 +import { Composio } from '@composio/core';
8 +import { OpenAI } from 'openai';
9 +import dotenv from 'dotenv';
10 +
11 +dotenv.config();
12 +
13 +const openai = new OpenAI({
14 + apiKey: process.env.OPENAI_API_KEY,
15 +});
16 +
17 +const composio = new Composio({
18 + apiKey: process.env.COMPOSIO_API_KEY,
19 +});
20 +
21 +/**
22 + * Main function to demonstrate OpenAI Assistants with Composio tool
23 + */
24 +async function main() {
25 + try {
26 + console.log('🔄 Setting up Composio and fetching tool...');
27 +
28 + const tool = await composio.getToolBySlug('default', 'HACKERNEWS_GET_USER');
29 +
30 + console.log('🔄 Creating OpenAI Assistant with Composio tool...');
31 +
32 + const assistant = await openai.beta.assistants.create({
33 + name: "HackerNews Helper",
34 + instructions: "You are a helpful assistant that can fetch information about HackerNews users.",
35 + model: "gpt-4o",
36 + tools: [tool],
37 + });
38 +
39 + console.log(`✅ Assistant created with ID: ${assistant.id}`);
40 +
41 + const thread = await openai.beta.threads.create();
42 + console.log(`✅ Thread created with ID: ${thread.id}`);
43 +
44 + const query = "Get information about the HackerNews user 'pg'";
45 + console.log(`🔄 Adding message to thread: "${query}"`);
46 +
47 + await openai.beta.threads.messages.create(thread.id, {
48 + role: "user",
49 + content: query,
50 + });
51 +
52 + console.log('🔄 Running the assistant...');
53 + const run = await openai.beta.threads.runs.create(thread.id, {
54 + assistant_id: assistant.id,
55 + });
56 +
57 + console.log('🔄 Waiting for assistant response and handling tool calls...');
58 + const completedRun = await composio.toolset.waitAndHandleAssistantToolCalls(
59 + 'default',
60 + openai,
61 + run,
62 + thread,
63 + {
64 + connectedAccountId: '', // Optional connected account ID
65 + customAuthParams: {}, // Optional custom auth parameters
66 + }
67 + );
68 +
69 + console.log(`✅ Run completed with status: ${completedRun.status}`);
70 +
71 + const messages = await openai.beta.threads.messages.list(thread.id);
72 +
73 + console.log('🤖 Assistant responses:');
74 + for (const message of messages.data) {
75 + if (message.role === 'assistant') {
76 + for (const content of message.content) {
77 + if (content.type === 'text') {
78 + console.log(`- ${content.text.value}`);
79 + }
80 + }
81 + }
82 + }
83 +
84 + await openai.beta.assistants.del(assistant.id);
85 + console.log('🧹 Cleaned up: Assistant deleted');
86 +
87 + } catch (error) {
88 + console.error('❌ Error:', error);
89 + }
90 +}
91 +
92 +main();
examples/openai/src/chat-completions.ts new
+81
@@ -0,0 +1,81 @@
1 +/**
2 + * OpenAI Chat Completions with Composio Tool Example
3 + *
4 + * This example demonstrates how to use the Composio OpenAIToolset with OpenAI chat completions API.
5 + * It uses the HACKERNEWS_GET_USER tool to fetch information about a HackerNews user.
6 + */
7 +import { Composio } from '@composio/core';
8 +import { OpenAI } from 'openai';
9 +import dotenv from 'dotenv';
10 +
11 +dotenv.config();
12 +
13 +const openai = new OpenAI({
14 + apiKey: process.env.OPENAI_API_KEY,
15 +});
16 +
17 +const composio = new Composio({
18 + apiKey: process.env.COMPOSIO_API_KEY,
19 +});
20 +
21 +/**
22 + * Main function to demonstrate OpenAI Chat Completions with Composio tool
23 + */
24 +async function main() {
25 + try {
26 + console.log('🔄 Setting up Composio and fetching tool...');
27 +
28 + const tool = await composio.getToolBySlug('default', 'HACKERNEWS_GET_USER');
29 +
30 + const query = "Find information about the HackerNews user 'pg'";
31 +
32 + console.log(`🔄 Sending query to OpenAI: "${query}"`);
33 +
34 + const response = await openai.chat.completions.create({
35 + model: 'gpt-4o',
36 + messages: [
37 + { role: 'system', content: 'You are a helpful assistant that can use tools to answer questions.' },
38 + { role: 'user', content: query }
39 + ],
40 + tools: [tool],
41 + tool_choice: 'auto',
42 + });
43 +
44 + if (response.choices[0].message.tool_calls) {
45 + console.log('🔧 Assistant is using tool:', response.choices[0].message.tool_calls[0].function.name);
46 +
47 + const toolResult = await composio.toolset.executeToolCall(
48 + 'default',
49 + response.choices[0].message.tool_calls[0],
50 + {
51 + connectedAccountId: '', // Optional connected account ID
52 + customAuthParams: {}, // Optional custom auth parameters
53 + }
54 + );
55 +
56 + console.log('✅ Tool execution result:', JSON.parse(toolResult));
57 +
58 + const finalResponse = await openai.chat.completions.create({
59 + model: 'gpt-4o',
60 + messages: [
61 + { role: 'system', content: 'You are a helpful assistant that can use tools to answer questions.' },
62 + { role: 'user', content: query },
63 + response.choices[0].message,
64 + {
65 + role: 'tool',
66 + tool_call_id: response.choices[0].message.tool_calls[0].id,
67 + content: toolResult,
68 + }
69 + ],
70 + });
71 +
72 + console.log('🤖 Final assistant response:', finalResponse.choices[0].message.content);
73 + } else {
74 + console.log('🤖 Assistant response:', response.choices[0].message.content);
75 + }
76 + } catch (error) {
77 + console.error('❌ Error:', error);
78 + }
79 +}
80 +
81 +main();
examples/openai/src/examples.ts
+32 -4
@@ -1,10 +1,38 @@
1 -import { Composio, ToolListParamsSchema } from '@composio/core';
2 -import { OpenAI } from 'openai';
1 +/**
2 + * Basic Composio Tool Fetching Example
3 + *
4 + * This example demonstrates how to fetch a tool from Composio.
5 + * It retrieves the HACKERNEWS_GET_USER tool and logs its schema.
6 + */
7 +import { Composio } from '@composio/core';
8 +import dotenv from 'dotenv';
9 +
10 +dotenv.config();
11
12 const composio = new Composio({
13 apiKey: process.env.COMPOSIO_API_KEY,
14 });
15
8 -const tool = await composio.getToolBySlug('test-user-id', 'HACKERNEWS_GET_USER');
16 +/**
17 + * Main function to demonstrate fetching a tool from Composio
18 + */
19 +async function main() {
20 + try {
21 + console.log('Fetching HACKERNEWS_GET_USER tool...');
22 + const tool = await composio.getToolBySlug('default', 'HACKERNEWS_GET_USER');
23 +
24 + console.log('Tool details:');
25 + console.log(JSON.stringify(tool, null, 2));
26 +
27 + console.log('\nInput parameters:');
28 + if (tool.function.parameters.properties) {
29 + for (const [key, value] of Object.entries(tool.function.parameters.properties)) {
30 + console.log(`- ${key}: ${JSON.stringify(value)}`);
31 + }
32 + }
33 + } catch (error) {
34 + console.error('Error fetching tool:', error);
35 + }
36 +}
37
10 -console.log(tool);
38 +main();