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.

Add ai toolcalling example

haxzie0062b9eee438
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 58d6d4a19..41c6fb878 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -421,6 +421,9 @@ importers:
dotenv:
specifier: ^16.4.1
version: 16.5.0
+ openai:
+ specifier: ^4.94.0
devDependencies:
'@types/bun':
specifier: ^1.2.9
diff --git a/ts/examples/tools/package.json b/ts/examples/tools/package.json
index ad11dcaf5..04baac42f 100644
--- a/ts/examples/tools/package.json
+++ b/ts/examples/tools/package.json
@@ -9,13 +9,18 @@
"dev": "bun --watch src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
- "keywords": ["composio", "example", "tools"],
+ "keywords": [
+ "composio",
+ "example",
+ "tools"
+ ],
"author": "",
"license": "ISC",
"packageManager": "[email protected]",
"dependencies": {
"@composio/core": "workspace:*",
- "dotenv": "^16.4.1"
+ "dotenv": "^16.4.1",
+ "openai": "^4.94.0"
},
"devDependencies": {
"@types/bun": "^1.2.9",
diff --git a/ts/examples/tools/src/ai.ts b/ts/examples/tools/src/ai.ts
new file mode 100644
index 000000000..373e857ec
--- /dev/null
+++ b/ts/examples/tools/src/ai.ts
@@ -0,0 +1,60 @@
+import { Composio } from '@composio/core';
+import { OpenAI } from 'openai';
+import path from 'path';
+
+const openai = new OpenAI({
+ apiKey: process.env.OPENAI_API_KEY,
+});
+
+/**
+ * Initialize Composio
+ * OpenAI Provider is automatically installed and initialized
+ */
+const composio = new Composio({
+ apiKey: process.env.COMPOSIO_API_KEY,
+});
+
+/**
+ * Get the tools
+ * This tool is automatically typed and wrapped with the OpenAI Provider
+ */
+console.log('πŸ”„ Getting tools...');
+const tools = await composio.tools.get('default', 'GOOGLEDRIVE_UPLOAD_FILE');
+console.log('βœ… Tools fetched successfully...');
+console.log(JSON.stringify(tools, null, 2));
+/**
+ * Define a task for the assistant based on the tools in hand
+ */
+const fileToUpload = path.join(__dirname, 'image.png');
+const task = `Upload the file ${fileToUpload} to Google Drive`;
+
+/**
+ * Define the messages for the assistant
+ */
+const messages: OpenAI.ChatCompletionMessageParam[] = [
+ { role: 'system', content: 'You are a helpful assistant that can help with tasks.' },
+ { role: 'user', content: task },
+];
+
+/**
+ * Create a chat completion
+ */
+console.log('πŸ”„ Creating chat completion...');
+const response = await openai.chat.completions.create({
+ model: 'gpt-4o-mini',
+ messages,
+ tools: tools,
+ tool_choice: 'auto',
+});
+console.log('βœ… Chat completion created successfully...');
+/**
+ * If the assistant has tool calls, execute them and log the result
+ */
+console.log('πŸ”„ Calling tool...');
+if (response.choices[0].message.tool_calls) {
+ console.log(`βœ… Calling tool ${response.choices[0].message.tool_calls[0].function.name}`);
+ const result = await composio.provider.handleToolCalls('default', response);
+ const data = JSON.parse(result[0]);
+ console.log(data);
+}
+console.log('βœ… Tool called successfully...');
\ No newline at end of file