Using Composio JS SDK with Langchain

1

Import Dependencies

Import Langchain and LangchainToolSet
1import { ChatOpenAI } from "@langchain/openai";
2import { createOpenAIFunctionsAgent, AgentExecutor } from "langchain/agents";
3import { pull } from "langchain/hub";
4import { LangchainToolSet } from "composio-core";
2

Define method to let users connect their GitHub account

Define method to let users connect their GitHub account
1const toolset = new LangchainToolSet({ apiKey: process.env.COMPOSIO_API_KEY, });
2
3async function setupUserConnectionIfNotExists(entityId) {
4 const entity = toolset.client.getEntity(entityId);
5 const connection = await entity.getConnection({ appName: "GITHUB" });
6
7 if (!connection) {
8 // If this entity/user hasn't already connected the account
9 const connection = await entity.initiateConnection({appName: appName});
10 console.log("Log in via: ", connection.redirectUrl);
11 return connection.waitUntilActive(60);
12 }
13
14 return connection;
15}

Learn more about Entities here

3

Setup Agent with Langchain

Define your Agent to create issues on Github
1async function executeAgent (entityName){
2 // Create entity and get tools
3 const entity = toolset.client.getEntity(entityName)
4 await setupUserConnectionIfNotExists(entity.id);
5 const tools = await toolset.getTools({ actions: ["github_issues_create"] },entity.id);
6
7 // Create an agent
8 const prompt = await pull("hwchase17/openai-functions-agent");
9 const llm = new ChatOpenAI({
10 model: "gpt-4o",
11 apiKey: process.env.OPEN_AI_API_KEY
12 });
13
14 const agent = await createOpenAIFunctionsAgent({
15 llm,
16 tools: tools,
17 prompt,
18 });
19 const agentExecutor = new AgentExecutor({agent,tools,verbose: true,});
20
21 // Invoke the agent
22 const body = "TITLE: HELLO WORLD, DESCRIPTION: HELLO WORLD for the repo - himanshu-dixit/custom-repo-breaking"
23 const result = await agentExecutor.invoke({
24 input: "Please create another github issue with the summary and description with the following details of another issue:- , " + JSON.stringify(body)
25 });
26
27 console.log(result.output)
28}
4

Invoke your Agent

Invoke the agent
1executeAgent("himanshu")

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

5

Complete code snippet

View the full code snippet here

Built with