- JavaScript
Import Required Packages
Initialize Composio Toolset
Set up the AI Model
Create the AI Agent
Set up the Agent Executor
Define the Code Execution Function
Run the Code Execution Agent
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
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.
Import Required Packages
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();
Initialize Composio Toolset
const toolset = new LangchainToolSet({
apiKey: process.env.COMPOSIO_API_KEY
});
const tools = await toolset.getTools({
actions: ["codeinterpreter_create_sandbox", "codeinterpreter_execute_code"]
});
Set up the AI Model
const llm = new ChatOpenAI({
model: "gpt-4o",
apiKey: process.env.OPEN_AI_API_KEY
});
Create the AI Agent
const prompt = await pull("hwchase17/openai-functions-agent");
const agent = await createOpenAIFunctionsAgent({ llm, tools, prompt });
Set up the Agent Executor
const agentExecutor = new AgentExecutor({
agent,
tools,
verbose: true,
});
Define the Code Execution Function
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);
}
Run the Code Execution Agent
const userProblem = "Create a list of prime numbers up to 50";
executeCodeAgent(userProblem).catch(error => console.error("An error occurred:", error));
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));
Was this page helpful?