Code Execution Agent

Code Execution Agent GitHub Repository

Explore the complete source code for the Code Execution Agent project. This repository contains all the necessary files and scripts to set up and run the Code Execution Agent using Composio.

1

Install the required packages

1pip install composio-crewai langchain-openai

Create a .env file and add your OpenAI API Key.

2

Import base packages

Next, we’ll import the essential libraries for our project.

1import os
2
3from composio_crewai import Action, App, ComposioToolSet
4from crewai import Agent, Crew, Process, Task
5from dotenv import load_dotenv
6from langchain_openai import ChatOpenAI
3

Initialize Composio Toolset

We’ll set up the Composio toolset to access the code interpreter functionality.

1load_dotenv()
2
3# Set up Composio tools
4composio_toolset = ComposioToolSet()
5
6# Get tools from the Code Interpreter app
7tools = composio_toolset.get_tools(apps=[App.CODEINTERPRETER])
4

Set up AI Model

Initialize the language model that will power our agent.

1# Initialize the language model
2llm = ChatOpenAI(model="gpt-4o")
5

Create AI Agent

Define the AI agent that will generate and execute code.

1# Define the Python executor agent
2python_executor_agent = Agent(
3 role="Python Executor",
4 goal="Execute Python code to solve problems",
5 backstory="""You are an expert Python developer specializing in executing code to solve various problems.
6 Your job is to analyze problems, write efficient Python code to solve them, and execute the code to ensure it works correctly.""",
7 verbose=True,
8 allow_delegation=False,
9 tools=tools,
10 llm=llm
11)
6

Define Task and Execution

Create a task for the agent and set up the execution function.

1# Define a task for the agent
2task = Task(
3 description="""Write a Python function to calculate the Fibonacci sequence up to the 10th number.
4 Then, execute the code and return the result.""",
5 expected_output="The Fibonacci sequence and the execution result",
6 agent=python_executor_agent
7)
8
9# Create a crew with the agent and task
10crew = Crew(
11 agents=[python_executor_agent],
12 tasks=[task],
13 verbose=2,
14 process=Process.sequential
15)
16
17# Execute the task
18result = crew.kickoff()
19print(f"Result: {result}")

Complete Code

1import os
2
3from composio_crewai import Action, App, ComposioToolSet
4from crewai import Agent, Crew, Process, Task
5from dotenv import load_dotenv
6from langchain_openai import ChatOpenAI
7
8# Load environment variables
9load_dotenv()
10
11# Set up Composio tools
12composio_toolset = ComposioToolSet()
13
14# Get tools from the Code Interpreter app
15tools = composio_toolset.get_tools(apps=[App.CODEINTERPRETER])
16
17# Initialize the language model
18llm = ChatOpenAI(model="gpt-4o")
19
20# Define the Python executor agent
21python_executor_agent = Agent(
22 role="Python Executor",
23 goal="Execute Python code to solve problems",
24 backstory="""You are an expert Python developer specializing in executing code to solve various problems.
25 Your job is to analyze problems, write efficient Python code to solve them, and execute the code to ensure it works correctly.""",
26 verbose=True,
27 allow_delegation=False,
28 tools=tools,
29 llm=llm
30)
31
32# Define a task for the agent
33task = Task(
34 description="""Write a Python function to calculate the Fibonacci sequence up to the 10th number.
35 Then, execute the code and return the result.""",
36 expected_output="The Fibonacci sequence and the execution result",
37 agent=python_executor_agent
38)
39
40# Create a crew with the agent and task
41crew = Crew(
42 agents=[python_executor_agent],
43 tasks=[task],
44 verbose=2,
45 process=Process.sequential
46)
47
48# Execute the task
49result = crew.kickoff()
50print(f"Result: {result}")
Built with