Skip to main content

RAG Tool

RagTool

Description

The RagTool is designed to answer questions by leveraging the power of Retrieval-Augmented Generation (RAG) through CrewAI's native RAG system. It provides a dynamic knowledge base that can be queried to retrieve relevant information from various data sources. This tool is particularly useful for applications that require access to a vast array of information and need to provide contextually relevant answers.

Example

The following example demonstrates how to initialize the tool and use it with different data sources:

from crewai_tools import RagTool

# Create a RAG tool with default settings
rag_tool = RagTool()

# Add content from a file
rag_tool.add(data_type="file", path="path/to/your/document.pdf")

# Add content from a web page
rag_tool.add(data_type="web_page", url="https://example.com")

# Define an agent with the RagTool
@agent
def knowledge_expert(self) -> Agent:
    '''
    This agent uses the RagTool to answer questions about the knowledge base.
    '''
    return Agent(
        config=self.agents_config["knowledge_expert"],
        allow_delegation=False,
        tools=[rag_tool]
    )

Supported Data Sources

The RagTool can be used with a wide variety of data sources, including:

  • 📰 PDF files
  • 📊 CSV files
  • 📃 JSON files
  • 📝 Text
  • 📁 Directories/Folders
  • 🌐 HTML Web pages
  • 📽️ YouTube Channels
  • 📺 YouTube Videos
  • 📚 Documentation websites
  • 📝 MDX files
  • 📄 DOCX files
  • 🧾 XML files
  • 📬 Gmail
  • 📝 GitHub repositories
  • 🐘 PostgreSQL databases
  • 🐬 MySQL databases
  • 🤖 Slack conversations
  • 💬 Discord messages
  • 🗨️ Discourse forums
  • 📝 Substack newsletters
  • 🐝 Beehiiv content
  • 💾 Dropbox files
  • 🖼️ Images
  • ⚙️ Custom data sources

Parameters

The RagTool accepts the following parameters:

  • summarize: Optional. Whether to summarize the retrieved content. Default is False.
  • adapter: Optional. A custom adapter for the knowledge base. If not provided, a CrewAIRagAdapter will be used.
  • config: Optional. Configuration for the underlying CrewAI RAG system. Accepts a RagToolConfig TypedDict with optional embedding_model (ProviderSpec) and vectordb (VectorDbConfig) keys. All configuration values provided programmatically take precedence over environment variables.

Adding Content

You can add content to the knowledge base using the add method:

# Add a PDF file
rag_tool.add(data_type="file", path="path/to/your/document.pdf")

# Add a web page
rag_tool.add(data_type="web_page", url="https://example.com")

# Add a YouTube video
rag_tool.add(data_type="youtube_video", url="https://www.youtube.com/watch?v=VIDEO_ID")

# Add a directory of files
rag_tool.add(data_type="directory", path="path/to/your/directory")

Agent Integration Example

Here's how to integrate the RagTool with a CrewAI agent:

from crewai import Agent
from crewai.project import agent
from crewai_tools import RagTool

# Initialize the tool and add content
rag_tool = RagTool()
rag_tool.add(data_type="web_page", url="https://docs.crewai.com")
rag_tool.add(data_type="file", path="company_data.pdf")

# Define an agent with the RagTool
@agent
def knowledge_expert(self) -> Agent:
    return Agent(
        config=self.agents_config["knowledge_expert"],
        allow_delegation=False,
        tools=[rag_tool]
    )

Advanced Configuration

You can customize the behavior of the RagTool by providing a configuration dictionary:

from crewai_tools import RagTool
from crewai_tools.tools.rag import RagToolConfig, VectorDbConfig, ProviderSpec

# Create a RAG tool with custom configuration

vectordb: VectorDbConfig = {
    "provider": "qdrant",
    "config": {
        "collection_name": "my-collection"
    }
}

embedding_model: ProviderSpec = {
    "provider": "openai",
    "config": {
        "model_name": "text-embedding-3-small"
    }
}

config: RagToolConfig = {
    "vectordb": vectordb,
    "embedding_model": embedding_model
}

rag_tool = RagTool(config=config, summarize=True)

Embedding Model Configuration

The embedding_model parameter accepts a crewai.rag.embeddings.types.ProviderSpec dictionary with the structure:

{
    "provider": "provider-name",  # Required
    "config": {                    # Optional
        # Provider-specific configuration
    }
}

Supported Providers

OpenAI

Cohere

VoyageAI

Ollama

Amazon Bedrock

Azure OpenAI

Google Generative AI

Google Vertex AI

Jina AI

HuggingFace

Instructor

Sentence Transformer

ONNX

OpenCLIP

Text2Vec

Roboflow

WatsonX (IBM)

Custom

Notes

  • All config fields are optional unless marked as Required
  • API keys can typically be provided via environment variables instead of config
  • Default values are shown where applicable

Conclusion

The RagTool provides a powerful way to create and query knowledge bases from various data sources. By leveraging Retrieval-Augmented Generation, it enables agents to access and retrieve relevant information efficiently, enhancing their ability to provide accurate and contextually appropriate responses.