Code Execution Agent

The project generates and executes code based on user-defined problems. It utilizes the Composio and connects your AI Agent to E2B's Code Interpreter to facilitate code execution, allowing users to input a problem statement and receive executable code as output. The agent is designed to operate in a sandbox environment, ensuring safe execution and accurate results. Key functionalities include code generation, execution, and result interpretation, making it an invaluable resource for developers and data scientists alike.
1

Import Required Packages

Import necessary packages for the Code Execution Agent:

Import statements
1import dotenv from 'dotenv';
2import { ChatOpenAI } from "@langchain/openai";
3import { createOpenAIFunctionsAgent, AgentExecutor } from "langchain/agents";
4import { pull } from "langchain/hub";
5import { LangchainToolSet } from "composio-core";
6
7dotenv.config();
2

Initialize Composio Toolset

Set up the Composio toolset and get the required tools:

Connect to CodeInterpreter
1const toolset = new LangchainToolSet({
2 apiKey: process.env.COMPOSIO_API_KEY
3});
4
5const tools = await toolset.getTools({
6 actions: ["codeinterpreter_create_sandbox", "codeinterpreter_execute_code"]
7});
3

Set up the AI Model

Initialize the OpenAI ChatGPT model:

Initialise Model
1const llm = new ChatOpenAI({
2 model: "gpt-4o",
3 apiKey: process.env.OPEN_AI_API_KEY
4});
4

Create the AI Agent

Set up the agent’s prompt and create the OpenAI Functions Agent:

Setup Agent
1const prompt = await pull("hwchase17/openai-functions-agent");
2const agent = await createOpenAIFunctionsAgent({ llm, tools, prompt });
5

Set up the Agent Executor

Create the AgentExecutor to run the agent:

Creating Agent Executor
1const agentExecutor = new AgentExecutor({
2 agent,
3 tools,
4 verbose: true,
5});
6

Define the Code Execution Function

Create the main function to generate and execute code:

Main Function
1async function executeCodeAgent(userProblem) {
2 // Generate code
3 console.log("Generating code for the problem...");
4 const codeGenerationResult = await agentExecutor.invoke({
5 input: `Generate Python code to solve the following problem: ${userProblem}.
6 Only provide the code, no explanations.`
7 });
8 const generatedCode = codeGenerationResult.output;
9 console.log("Generated Code:", generatedCode);
10
11 // Execute code
12 console.log("\nExecuting the generated code...");
13 const executionResult = await agentExecutor.invoke({
14 input: `Execute the following Python code:\n${generatedCode}`
15 });
16 console.log("\nExecution Result:", executionResult.output);
17}
7

Run the Code Execution Agent

Execute the agent with a sample problem:

Run the Agent
1const userProblem = "Create a list of prime numbers up to 50";
2executeCodeAgent(userProblem).catch(error => console.error("An error occurred:", error));

Complete Code

Here’s the complete JavaScript code for the Code Execution Agent:

JavaScript Final Code
1import dotenv from 'dotenv';
2import { ChatOpenAI } from "@langchain/openai";
3import { createOpenAIFunctionsAgent, AgentExecutor } from "langchain/agents";
4import { pull } from "langchain/hub";
5import { LangchainToolSet } from "composio-core";
6
7dotenv.config();
8
9async function executeCodeAgent(userProblem) {
10 const toolset = new LangchainToolSet({
11 apiKey: process.env.COMPOSIO_API_KEY
12 });
13
14 const tools = await toolset.getTools({
15 actions: ["codeinterpreter_create_sandbox", "codeinterpreter_execute_code"]
16 });
17
18 const llm = new ChatOpenAI({
19 model: "gpt-4o",
20 apiKey: process.env.OPEN_AI_API_KEY
21 });
22
23 const prompt = await pull("hwchase17/openai-functions-agent");
24 const agent = await createOpenAIFunctionsAgent({ llm, tools, prompt });
25
26 const agentExecutor = new AgentExecutor({
27 agent,
28 tools,
29 verbose: true,
30 });
31
32 console.log("Generating code for the problem...");
33 const codeGenerationResult = await agentExecutor.invoke({
34 input: `Generate Python code to solve the following problem: ${userProblem}.
35 Only provide the code, no explanations.`
36 });
37 const generatedCode = codeGenerationResult.output;
38 console.log("Generated Code:", generatedCode);
39
40 console.log("\nExecuting the generated code...");
41 const executionResult = await agentExecutor.invoke({
42 input: `Execute the following Python code:\n${generatedCode}`
43 });
44 console.log("\nExecution Result:", executionResult.output);
45}
46
47const userProblem = "Create a list of prime numbers up to 50";
48executeCodeAgent(userProblem).catch(error => console.error("An error occurred:", error));