RAG Tool Agent
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 Agent using Composio.
1
Install the required packages
1 pip 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.
1 import os 2 import textwrap 3 4 from composio_crewai import Action, App, ComposioToolSet 5 from crewai import Agent, Crew, Process, Task 6 from dotenv import load_dotenv 7 from langchain_openai import ChatOpenAI
3
Initialize Language Model and Define Tools
Set up the language model and the RAG tools.
1 load_dotenv() 2 3 # Initialize the language model 4 llm = ChatOpenAI(model="gpt-4o") 5 6 # Set up Composio tools 7 composio_toolset = ComposioToolSet() 8 9 # Get tools for RAG operations 10 rag_tools = composio_toolset.get_tools(apps=[App.RAG])
4
Define the RAG Agent
Create the agent that will work with the RAG system.
1 # Define the RAG Agent 2 rag_agent = Agent( 3 role="RAG Knowledge Base Manager", 4 goal="Manage a knowledge base using RAG tools", 5 backstory="""You are an expert in managing knowledge bases and retrieving information. 6 Your job is to add content to the knowledge base and retrieve answers to user queries. 7 You use RAG (Retrieval-Augmented Generation) tools to efficiently store and retrieve information.""", 8 verbose=True, 9 allow_delegation=False, 10 tools=rag_tools, 11 llm=llm 12 )
5
Add Content to the RAG System
Add sample content to the knowledge base.
1 # Define task for adding content 2 add_content_task = Task( 3 description="""Add the following information to the knowledge base: 4 5 1. Paris is the capital city of France. 6 2. London is the capital city of the United Kingdom. 7 3. Washington D.C. is the capital city of the United States. 8 4. Tokyo is the capital city of Japan. 9 5. Berlin is the capital city of Germany. 10 11 Make sure to confirm that each piece of information is successfully added.""", 12 expected_output="Confirmation that all information was added to the knowledge base", 13 agent=rag_agent 14 )
6
Query the RAG System
Retrieve information from the knowledge base.
1 # Define task for querying 2 query_task = Task( 3 description="""Query the knowledge base to answer the following question: 4 "What is the capital of France?" 5 6 Return the answer with any supporting information from the knowledge base.""", 7 expected_output="The answer to the question based on the knowledge base", 8 agent=rag_agent, 9 context=[add_content_task] # This task depends on content being added first 10 )
7
Execute the Workflow
Run the complete RAG workflow.
1 # Create a crew with the agent and tasks 2 crew = Crew( 3 agents=[rag_agent], 4 tasks=[add_content_task, query_task], 5 verbose=2, 6 process=Process.sequential # Tasks must run in order 7 ) 8 9 # Execute the workflow 10 result = crew.kickoff() 11 print(textwrap.fill(f"Final Result: {result}", width=80))
Complete Code
1 import os 2 import textwrap 3 4 from composio_crewai import Action, App, ComposioToolSet 5 from crewai import Agent, Crew, Process, Task 6 from dotenv import load_dotenv 7 from langchain_openai import ChatOpenAI 8 9 # Load environment variables 10 load_dotenv() 11 12 # Initialize the language model 13 llm = ChatOpenAI(model="gpt-4o") 14 15 # Set up Composio tools 16 composio_toolset = ComposioToolSet() 17 18 # Get tools for RAG operations 19 rag_tools = composio_toolset.get_tools(apps=[App.RAG]) 20 21 # Define the RAG Agent 22 rag_agent = Agent( 23 role="RAG Knowledge Base Manager", 24 goal="Manage a knowledge base using RAG tools", 25 backstory="""You are an expert in managing knowledge bases and retrieving information. 26 Your job is to add content to the knowledge base and retrieve answers to user queries. 27 You use RAG (Retrieval-Augmented Generation) tools to efficiently store and retrieve information.""", 28 verbose=True, 29 allow_delegation=False, 30 tools=rag_tools, 31 llm=llm 32 ) 33 34 # Define task for adding content 35 add_content_task = Task( 36 description="""Add the following information to the knowledge base: 37 38 1. Paris is the capital city of France. 39 2. London is the capital city of the United Kingdom. 40 3. Washington D.C. is the capital city of the United States. 41 4. Tokyo is the capital city of Japan. 42 5. Berlin is the capital city of Germany. 43 44 Make sure to confirm that each piece of information is successfully added.""", 45 expected_output="Confirmation that all information was added to the knowledge base", 46 agent=rag_agent 47 ) 48 49 # Define task for querying 50 query_task = Task( 51 description="""Query the knowledge base to answer the following question: 52 "What is the capital of France?" 53 54 Return the answer with any supporting information from the knowledge base.""", 55 expected_output="The answer to the question based on the knowledge base", 56 agent=rag_agent, 57 context=[add_content_task] # This task depends on content being added first 58 ) 59 60 # Create a crew with the agent and tasks 61 crew = Crew( 62 agents=[rag_agent], 63 tasks=[add_content_task, query_task], 64 verbose=2, 65 process=Process.sequential # Tasks must run in order 66 ) 67 68 # Execute the workflow 69 result = crew.kickoff() 70 print(textwrap.fill(f"Final Result: {result}", width=80))