Using Composio JS SDK with OpenAI

Demo

1

Get Composio API Key

Follow this Get API Key to get your Composio API Key

2

Install Dependencies

Install dependencies
$npm install composio-core openai
>yarn add composio-core openai
>pnpm add composio-core openai
3

Define method to let user connect their GitHub account

index.mjs
1import { OpenAI } from "openai";
2import { OpenAIToolSet } from "composio-core";
3
4const COMPOSIO_API_KEY = "<your-composio-api-key>"
5const OPEN_AI_API_KEY = "<your-openai-api-key>"
6const toolset = new OpenAIToolSet({apiKey: COMPOSIO_API_KEY,});
7
8const appName = 'github';
9
10async function setupUserConnectionIfNotExists(entityId) {
11 const entity = toolset.client.getEntity(entityId);
12 const connection = await entity.getConnection({ appName: appName });
13
14 if (!connection) {
15 const connection = await entity.initiateConnection({appName: appName});
16 console.log("Log in via: ", connection.redirectUrl);
17 return connection.waitUntilActive(60);
18 }
19
20 return connection;
21}

Learn more about entities here

4

Setup Agent with OpenAI

index.mjs
1async function executeAgent(repo,entityName="default") {
2 const entity = toolset.client.getEntity(entityName)
3 await setupUserConnectionIfNotExists(entity.id);
4
5 const tools = await toolset.getTools({ actions: ["github_issues_create"] }, entity.id);
6 const instruction = `Make an issue with sample title in the repo - ${repo}`
7
8 const client = new OpenAI({ apiKey: OPEN_AI_API_KEY })
9 const response = await client.chat.completions.create({
10 model: "gpt-4-turbo",
11 messages: [{
12 role: "user",
13 content: instruction,
14 }],
15 tools: tools,
16 tool_choice: "auto",
17 })
18
19 console.log(response.choices[0].message.tool_calls);
20 await toolset.handleToolCall(response, entity.id);
21}
5

Invoke Your Agent

index.mjs
1executeAgent("himanshu-dixit/custom-repo-breaking")

Tada 🎉! It was this simple to create a powerful Agent with Composio and OpenAI.

6

Complete code snippet

View the full snippet here

Built with