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

Imports and Environment Setup

In your Python script, import the necessary libraries and set up your environment variables.

import os
import dotenv
from textwrap import dedent
from composio_langchain import Action, App, ComposioToolSet
from langchain_openai import ChatOpenAI
from composio.local_tools import ragtool

# Load environment variables
dotenv.load_dotenv()
2

Initialize Language Model and Define Tools

Initialize the language model with OpenAI API key and model name, and set up the necessary tools for the agents.

# Initialize the LLM with the OpenAI GPT-4o model and API key
llm = ChatOpenAI(model="gpt-4o")

# Initialize the Composio ToolSet
toolset = ComposioToolSet()

# Get the RAG tool from the Composio ToolSet
tools = toolset.get_tools(apps=[App.RAGTOOL])
3

Define the RAG Agent

Define the RAG agent with its role, goal, backstory, and tools.

# Define the RAG Agent
rag_agent = Agent(
    role="RAG Agent",
    goal=dedent(
        """\
        Add relevant content to the RAG tool to enrich its knowledge base.
        Formulate a query to retrieve information from the RAG tool based on user input.
        After retrieval and addition of content, evaluate whether the goal given by the user input is achieved. If yes, stop execution."""
    ),
    verbose=True,
    memory=True,
    backstory=dedent(
        """\
        You are an expert in understanding user requirements, forming accurate queries,
        and enriching the knowledge base with relevant content."""
    ),
    llm=llm,
    allow_delegation=False,
    tools=tools,
)
4

Define Tasks for Adding Content

Create tasks to add content to the RAG tool for enriching its knowledge base.

from composio_langchain import Task  # Ensure Task is imported

# User-provided description of the data to be added
additional_content_list = [
    "Paris is the capital of France. It is known for its art, fashion, and culture.",
    "Berlin is the capital of Germany. It is famous for its history and vibrant culture.",
    "Tokyo is the capital of Japan. It is known for its technology and cuisine.",
    "Canberra is the capital of Australia. It is known for its modern architecture and museums.",
    # Add more data as needed
]

# Define the task for adding content to the RAG tool
add_content_tasks = [
    Task(
        description=dedent(
            f"""\
            Add the following content to the RAG tool to enrich its knowledge base: {content}"""
        ),
        expected_output="Content was added to the RAG tool",
        tools=tools,
        agent=rag_agent,
        allow_delegation=False,
    )
    for content in additional_content_list
]
5

Define and Execute Query Task

Create and execute the task for querying the RAG tool based on user input.

# User-provided query
user_query = "What is the capital of France?"

# Define the task for executing the RAG tool query
query_task = Task(
    description=dedent(
        f"""\
        Formulate a query based on this input: {user_query}.
        Retrieve relevant information using the RAG tool and return the results."""
    ),
    expected_output="Results of the RAG tool query were returned. Stop once the goal is achieved.",
    tools=tools,
    agent=rag_agent,
    allow_delegation=False,
)
6

Create Crew and Execute Process

Define the crew with the agent and tasks, then execute the process.

# Define the crew with the agent and tasks
crew = Crew(
    agents=[rag_agent],
    tasks=[add_content_tasks ,query_task],
    process=Process.sequential,
)

# Kickoff the process and print the result
result = crew.kickoff()
print(result)

Putting it All Together

import os
import dotenv
from textwrap import dedent
from composio_langchain import Action, App, ComposioToolSet
from crewai import Agent, Crew, Process, Task
from langchain_openai import ChatOpenAI
from composio.tools.local import ragtool

# Load environment variables from .env file
dotenv.load_dotenv()

# Initialize the ComposioToolSet
toolset = ComposioToolSet()

# Get the RAG tool from the Composio ToolSet
tools = toolset.get_tools(apps=[App.RAGTOOL])

# Initialize the ChatOpenAI model with GPT-4 and API key from environment variables
llm = ChatOpenAI(model="gpt-4o")

# User-provided description of the data to be added and the query
additional_content_list = [
    "Paris is the capital of France. It is known for its art, fashion, and culture.",
    "Berlin is the capital of Germany. It is famous for its history and vibrant culture.",
    "Tokyo is the capital of Japan. It is known for its technology and cuisine.",
    "Canberra is the capital of Australia. It is known for its modern architecture and museums.",
    # Add more data as needed
]

user_query = "What is the capital of France?"  # Edit the query for the action you want it to perform

# Define the RAG Agent
rag_agent = Agent(
    role="RAG Agent",
    goal=dedent(
        """\
        Add relevant content to the RAG tool to enrich its knowledge base.
        Formulate a query to retrieve information from the RAG tool based on user input.
        After retrieval and addition of content, evaluate whether the goal given by the user input is achieved. If yes, stop execution."""
    ),
    verbose=True,
    memory=True,
    backstory=dedent(
        """\
        You are an expert in understanding user requirements, forming accurate queries,
        and enriching the knowledge base with relevant content."""
    ),
    llm=llm,
    allow_delegation=False,
    tools=tools,
)

# Define the task for adding content to the RAG tool


total_content = ""
for content in additional_content_list:
  total_content +=content

add_content_tasks = Task(
        description=(
            f"""
            Add the following content to the RAG tool to enrich its knowledge base: {total_content}"""
        ),
        expected_output="Content was added to the RAG tool",
        tools=tools,
        agent=rag_agent,
        allow_delegation=False,
    )
# Define the task for executing the RAG tool query
query_task = Task(
    description=dedent(
        f"""\
        Formulate a query based on this input: {user_query}.
        Retrieve relevant information using the RAG tool and return the results."""
    ),
    expected_output="Results of the RAG tool query were returned. Stop once the goal is achieved.",
    tools=tools,
    agent=rag_agent,
    allow_delegation=False,
)

# Define the crew with the agent and tasks
crew = Crew(
    agents=[rag_agent],
    tasks=[add_content_tasks,query_task],
    process=Process.sequential,
)

# Kickoff the process and print the result
result = crew.kickoff()
print(result)