Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Add ai toolcalling example
haxzie committed
Jun 20, 2025 at 16:25 UTC
0062b9eee438459992cf17ce82ef001d6ae744db
3 files changed
+70
-2
pnpm-lock.yaml
+3
@@ -421,6 +421,9 @@ importers:
421
dotenv:
422
specifier: ^16.4.1
423
version: 16.5.0
424
+ openai:
425
+ specifier: ^4.94.0
426
+ version: 4.104.0(ws@8.18.2)(zod@3.25.62)
427
devDependencies:
428
'@types/bun':
429
specifier: ^1.2.9
ts/examples/tools/package.json
+7
-2
@@ -9,13 +9,18 @@
9
"dev": "bun --watch src/index.ts",
10
"test": "echo \"Error: no test specified\" && exit 1"
11
},
12
- "keywords": ["composio", "example", "tools"],
12
+ "keywords": [
13
+ "composio",
14
+ "example",
15
+ "tools"
16
+ ],
17
"author": "",
18
"license": "ISC",
19
"packageManager": "pnpm@10.8.0",
20
"dependencies": {
21
"@composio/core": "workspace:*",
18
- "dotenv": "^16.4.1"
22
+ "dotenv": "^16.4.1",
23
+ "openai": "^4.94.0"
24
},
25
"devDependencies": {
26
"@types/bun": "^1.2.9",
ts/examples/tools/src/ai.ts
new
+60
@@ -0,0 +1,60 @@
1
+import { Composio } from '@composio/core';
2
+import { OpenAI } from 'openai';
3
+import path from 'path';
4
+
5
+const openai = new OpenAI({
6
+ apiKey: process.env.OPENAI_API_KEY,
7
+});
8
+
9
+/**
10
+ * Initialize Composio
11
+ * OpenAI Provider is automatically installed and initialized
12
+ */
13
+const composio = new Composio({
14
+ apiKey: process.env.COMPOSIO_API_KEY,
15
+});
16
+
17
+/**
18
+ * Get the tools
19
+ * This tool is automatically typed and wrapped with the OpenAI Provider
20
+ */
21
+console.log('🔄 Getting tools...');
22
+const tools = await composio.tools.get('default', 'GOOGLEDRIVE_UPLOAD_FILE');
23
+console.log('✅ Tools fetched successfully...');
24
+console.log(JSON.stringify(tools, null, 2));
25
+/**
26
+ * Define a task for the assistant based on the tools in hand
27
+ */
28
+const fileToUpload = path.join(__dirname, 'image.png');
29
+const task = `Upload the file ${fileToUpload} to Google Drive`;
30
+
31
+/**
32
+ * Define the messages for the assistant
33
+ */
34
+const messages: OpenAI.ChatCompletionMessageParam[] = [
35
+ { role: 'system', content: 'You are a helpful assistant that can help with tasks.' },
36
+ { role: 'user', content: task },
37
+];
38
+
39
+/**
40
+ * Create a chat completion
41
+ */
42
+console.log('🔄 Creating chat completion...');
43
+const response = await openai.chat.completions.create({
44
+ model: 'gpt-4o-mini',
45
+ messages,
46
+ tools: tools,
47
+ tool_choice: 'auto',
48
+});
49
+console.log('✅ Chat completion created successfully...');
50
+/**
51
+ * If the assistant has tool calls, execute them and log the result
52
+ */
53
+console.log('🔄 Calling tool...');
54
+if (response.choices[0].message.tool_calls) {
55
+ console.log(`✅ Calling tool ${response.choices[0].message.tool_calls[0].function.name}`);
56
+ const result = await composio.provider.handleToolCalls('default', response);
57
+ const data = JSON.parse(result[0]);
58
+ console.log(data);
59
+}
60
+console.log('✅ Tool called successfully...');
\ No newline at end of file