Skip to main content
Python

RAG Agent GitHub Repository

Explore the complete source code for the RAG Agent project. This repository contains all the necessary files and scripts to set up and run the RAG system using CrewAI and Composio.
1

Import Required Packages

Import necessary packages for the Code Execution Agent:
import os
from composio_langchain import Action, App, ComposioToolSet
from crewai import Agent, Crew, Process, Task
2

Initialize Composio Toolset

Set up the Composio toolset and get the required tools:
composio_toolset = ComposioToolSet()
tools = composio_toolset.get_tools(apps=[App.CODEINTERPRETER])
3

Set up the AI Model

Initialize the OpenAI ChatGPT model:
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o")
4

Create the AI Agent

Set up the agent’s prompt and create the OpenAI Functions Agent:
python_executor_agent = Agent(
role="Python Code Executor",
goal="Execute Python code in a Jupyter notebook cell and return the results.",
verbose=True,
memory=True,
backstory="You are an expert in executing Python code and interpreting results in a sandbox environment.",
allow_delegation=False,
tools=tools,
)
5

Set up the Agent Executor

Create the AgentExecutor to run the agent:
python_code = """
    def calculate_sum(a, b):
        return a + b

    result = calculate_sum(5, 3)
    print(result)
"""

execute_code_task = Task(
    description="Execute the following Python code and return the results:\n\n"
    + python_code,
    expected_output="Execution of Python code returned the results.",
    tools=tools,
    agent=python_executor_agent,
    allow_delegation=False,
)

6

Define the Code Execution Function

Create the main function to generate and execute code:
crew = Crew(
    agents=[python_executor_agent],
    tasks=[execute_code_task],
    process=Process.sequential,
)

7

Run the Code Execution Agent

Execute the agent with a sample problem:
result = crew.kickoff()
print(result)

Complete Code

Here’s the complete Python Code:
import os
from composio_langchain import Action, App, ComposioToolSet
from crewai import Agent, Crew, Process, Task

toolset = ComposioToolSet()
tools = toolset.get_tools(apps=[App.CODEINTERPRETER])

python_executor_agent = Agent(
    role="Python Code Executor",
    goal="Execute Python code in a Jupyter notebook cell and return the results.",
    verbose=True,
    memory=True,
    backstory="You are an expert in executing Python code and interpreting results in a sandbox environment.",
    allow_delegation=False,
    tools=tools,
)

python_code = """
def calculate_sum(a, b):
    return a + b

result = calculate_sum(5, 3)
print(result)
"""

execute_code_task = Task(
    description="Execute the following Python code and return the results:\n\n"
    + python_code,
    expected_output="Execution of Python code returned the results.",
    tools=tools,
    agent=python_executor_agent,
    allow_delegation=False,
)

crew = Crew(
    agents=[python_executor_agent],
    tasks=[execute_code_task],
    process=Process.sequential,
)

result = crew.kickoff()
print(result)