Implementation:CrewAIInc CrewAI RAG Tool
| Knowledge Sources | |
|---|---|
| Domains | Tools, RAG, Knowledge_Base |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
RagTool is the foundational base class for all knowledge-base-backed search tools in CrewAI, implementing the core RAG (Retrieval-Augmented Generation) pipeline.
Description
RagTool extends BaseTool and provides the central RAG abstraction in the CrewAI tools ecosystem. It defines an abstract Adapter base class with query() and add() methods, and uses a placeholder adapter pattern: on initialization, a model_validator detects the _AdapterPlaceholder and replaces it with a CrewAIRagAdapter configured from RagToolConfig. The _parse_config() method normalizes configuration into provider-specific objects, supporting chromadb (default) and qdrant as vector database backends. A _validate_embedding_config() helper provides clear, provider-specific error messages when ProviderSpec union validation fails. The add() method delegates document ingestion to the adapter, accepting content items with various keyword parameters (data_type, path, url, etc.). The _run() method queries the adapter with configurable similarity threshold and result limit, returning formatted relevant content. The _create_provider_config() static method instantiates the appropriate provider config with optional embedding function injection.
Usage
Use this tool as a general-purpose knowledge base for agents, or extend it (as PDFSearchTool does) to create specialized search tools for specific data types. It supports configurable vector database backends and embedding models.
Code Reference
Source Location
- Repository: CrewAI
- File: lib/crewai-tools/src/crewai_tools/tools/rag/rag_tool.py
- Lines: 1-263
Signature
class Adapter(BaseModel, ABC):
def query(self, question: str, similarity_threshold: float | None = None,
limit: int | None = None) -> str
def add(self, *args: ContentItem, **kwargs: Unpack[AddDocumentParams]) -> None
class RagTool(BaseTool):
name: str = "Knowledge base"
description: str = "A knowledge base that can be used to answer questions."
summarize: bool = False
similarity_threshold: float = 0.6
limit: int = 5
collection_name: str = "rag_tool_collection"
adapter: Adapter = Field(default_factory=_AdapterPlaceholder)
config: RagToolConfig = Field(default_factory=RagToolConfig)
def add(self, *args: ContentItem, **kwargs: Unpack[AddDocumentParams]) -> None
def _run(self, query: str, similarity_threshold: float | None = None,
limit: int | None = None) -> str
Import
from crewai_tools import RagTool
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| query | str | Yes | Question to search the knowledge base with |
| similarity_threshold | float or None | No | Minimum similarity score for results (default 0.6) |
| limit | int or None | No | Maximum number of results to return (default 5) |
Outputs
| Name | Type | Description |
|---|---|---|
| _run() returns | str | Formatted string "Relevant Content:\n{results}" from the adapter query |
Usage Examples
Basic Usage
from crewai_tools import RagTool
# Default ChromaDB backend
tool = RagTool()
tool.add("path/to/document.pdf", data_type="pdf_file")
result = tool._run(query="What are the main conclusions?")
# With Qdrant backend and custom embedding model
tool = RagTool(
config={
"vectordb": {"provider": "qdrant", "config": {"url": "http://localhost:6333"}},
"embedding_model": {"provider": "openai", "config": {"model": "text-embedding-3-large"}}
},
collection_name="my_collection",
similarity_threshold=0.7,
limit=10
)