Skip to main content
1

Import Required Packages

Import necessary packages for the Code Execution Agent:
import dotenv from 'dotenv';
import { ChatOpenAI } from "@langchain/openai";
import { createOpenAIFunctionsAgent, AgentExecutor } from "langchain/agents";
import { pull } from "langchain/hub";
import { LangchainToolSet } from "composio-core";

dotenv.config();
2

Initialize Composio Toolset

Set up the Composio toolset and get the required tools:
const toolset = new LangchainToolSet({ 
    apiKey: process.env.COMPOSIO_API_KEY
});

const tools = await toolset.getTools({ 
    actions: ["codeinterpreter_create_sandbox", "codeinterpreter_execute_code"] 
});
3

Set up the AI Model

Initialize the OpenAI ChatGPT model:
const llm = new ChatOpenAI({ 
    model: "gpt-4o",
    apiKey: process.env.OPEN_AI_API_KEY
});
4

Create the AI Agent

Set up the agent’s prompt and create the OpenAI Functions Agent:
const prompt = await pull("hwchase17/openai-functions-agent");
const agent = await createOpenAIFunctionsAgent({ llm, tools, prompt });
5

Set up the Agent Executor

Create the AgentExecutor to run the agent:
const agentExecutor = new AgentExecutor({ 
    agent, 
    tools, 
    verbose: true,
});
6

Define the Code Execution Function

Create the main function to generate and execute code:
async function executeCodeAgent(userProblem) {
    // Generate code
    console.log("Generating code for the problem...");
    const codeGenerationResult = await agentExecutor.invoke({ 
        input: `Generate Python code to solve the following problem: ${userProblem}. 
                Only provide the code, no explanations.`
    });
    const generatedCode = codeGenerationResult.output;
    console.log("Generated Code:", generatedCode);

    // Execute code
    console.log("\nExecuting the generated code...");
    const executionResult = await agentExecutor.invoke({ 
        input: `Execute the following Python code:\n${generatedCode}`
    });
    console.log("\nExecution Result:", executionResult.output);
}
7

Run the Code Execution Agent

Execute the agent with a sample problem:
const userProblem = "Create a list of prime numbers up to 50";
executeCodeAgent(userProblem).catch(error => console.error("An error occurred:", error));

Complete Code

Here’s the complete JavaScript code for the Code Execution Agent:
import dotenv from 'dotenv';
import { ChatOpenAI } from "@langchain/openai";
import { createOpenAIFunctionsAgent, AgentExecutor } from "langchain/agents";
import { pull } from "langchain/hub";
import { LangchainToolSet } from "composio-core";

dotenv.config();

async function executeCodeAgent(userProblem) {
    const toolset = new LangchainToolSet({ 
        apiKey: process.env.COMPOSIO_API_KEY
    });

    const tools = await toolset.getTools({ 
        actions: ["codeinterpreter_create_sandbox", "codeinterpreter_execute_code"] 
    });

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

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

    const agentExecutor = new AgentExecutor({ 
        agent, 
        tools, 
        verbose: true,
    });

    console.log("Generating code for the problem...");
    const codeGenerationResult = await agentExecutor.invoke({ 
        input: `Generate Python code to solve the following problem: ${userProblem}. 
                Only provide the code, no explanations.`
    });
    const generatedCode = codeGenerationResult.output;
    console.log("Generated Code:", generatedCode);

    console.log("\nExecuting the generated code...");
    const executionResult = await agentExecutor.invoke({ 
        input: `Execute the following Python code:\n${generatedCode}`
    });
    console.log("\nExecution Result:", executionResult.output);
}

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