---
title: "RAG Tool Agent"
sidebarTitle: "RAG Tool Agent"
icon: "database"
description: "This project involves setting up and running a system of agents to add content to a RAG (Retrieval-Augmented Generation) tool, perform queries, and return relevant information. We use Composio to setup this Local Tool and OpenAI GPT-4o to power the agents. Follow this guide to set up and run the project."
---




<Tab title="Python">
    <Card color="#7bee0c" title="RAG Agent GitHub Repository" icon="github" href="https://www.github.com/ComposioHQ/composio/tree/master/python/examples/quickstarters/rag_agent/">
  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.
  <CardBody>
  </CardBody>
</Card>

<Steps>
    <Step title="Imports and Environment Setup">
    In your Python script, import the necessary libraries and set up your environment variables.
    <CodeGroup>
        ```python Import libraries
        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()
        ```
    </CodeGroup>
    </Step>

    <Step title="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.
    <CodeGroup>
        ```python LLM and Tools
        # 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])
        ```
    </CodeGroup>
    </Step>

    <Step title="Define the RAG Agent">
    Define the RAG agent with its role, goal, backstory, and tools.
    <CodeGroup>
        ```python Set up Agents
        # 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,
        )
        ```
    </CodeGroup>
    </Step>

    <Step title="Define Tasks for Adding Content">
    Create tasks to add content to the RAG tool for enriching its knowledge base.
    <CodeGroup>
        ```python Creating Tasks
        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
        ]
        ```
    </CodeGroup>
    </Step>

    <Step title="Define and Execute Query Task">
    Create and execute the task for querying the RAG tool based on user input.
    <CodeGroup>
        ```python Python - Query task
        # 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,
        )
        ```

    </CodeGroup>
    </Step>

    <Step title="Create Crew and Execute Process">
    Define the crew with the agent and tasks, then execute the process.
    <CodeGroup>
        ```python Final step
        # 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)
        ```
    </CodeGroup>
    </Step>

</Steps>
    </Tab>



## Putting it All Together
<CodeGroup>
    ```python Python Final Code
    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)
    ```

</CodeGroup>

