CrewAI meets RAG: built-in and custom solutions


Generative AI offers immense possibilities, however to truly be effective in real-world applications, it needs to overcome key hurdles: ensuring factual accuracy and integrating with private, up-to-date company information. A standard Large Language Model (LLM) on its own often isn't enough for tasks demanding specific facts and deep domain knowledge. LLMs generate responses based solely on their training data. If they lack a direct answer, they might still attempt one, sometimes leading to convincing, yet fabricated, outputs (often referred to as hallucinations). For serious applications that demand reliable, grounded results, a smarter approach is essential.
Understanding RAG: The secret to accurate AI
This is where Retrieval Augmented Generation (RAG) comes in. Think of RAG as giving the LLM a set of specific notes to read before it answers a question. This makes its responses accurate and based on real data. The RAG process can be broadly divided into two main phases: Ingestion and Retrieval, both crucial for augmenting the LLM's knowledge.
RAG Ingestion Phase
This initial phase focuses on preparing your knowledge base.
Document Loading: Documents from various sources (PDFs, text files, databases, etc.) are loaded into the system.
Transformation (Chunking): Loaded documents are often too large to be processed all at once. They are broken down into smaller, manageable "chunks" or segments. This process might also involve cleaning or pre-processing the text.
Embedding: Each text chunk is then converted into a numerical representation called an embedding. This transformation is performed by an embedding model (e.g., a Sentence Transformer or a model like OpenAI's text-embedding-3-small
). Embeddings capture the semantic meaning of the text, allowing for comparisons based on context rather than just keywords.
Persistence (Vector Database): These embeddings, along with references back to their original text chunks, are stored in a Vector Database (Vector DB). A Vector DB is optimized for efficiently storing and querying high-dimensional vectors, enabling fast similarity searches.
RAG Retrieval Phase
This phase occurs when a user submits a query to the Generative AI application.
Query Embedding: The user's input query is also converted into an embedding using the same embedding model that was used during the Ingestion phase. This ensures consistency in the vector space.
Similarity Search: The query embedding is then used to perform a similarity search within the Vector DB. The goal is to find the "closest" or most semantically similar text chunks (and their corresponding original content) to the user's query.
Contextual Augmentation: The relevant text chunks retrieved from the Vector DB serve as precise, up-to-date information. This retrieved context is then used to enrich the original user query, forming a new, more informed prompt.
LLM Interrogation: Finally, this augmented prompt (containing both the user's original request and the relevant retrieved context) is fed to the Large Language Model. The LLM then generates a factual and grounded response, drawing directly from the specific knowledge extracted from your knowledge base, significantly reducing hallucinations and increasing reliability.
Why RAG is a Game-Changer
RAG isn't just about accuracy; it solves critical problems:
Precision and Reliability: RAG ensures the LLM's answers are factually correct and you can trace them back to your trusted data. This significantly reduces the chance of hallucinations.
Cost Optimization: Giving the LLM the right context with RAG means you don't need super long, complex prompts. Getting data (like for embeddings) is usually much cheaper than writing huge prompts or paying for more LLM tokens.
Overcoming Token Limits: All LLMs have a maximum amount of text they can handle at once (their context window). RAG smartly gets around this by only giving the most relevant bits of information, instead of trying to stuff an entire knowledge base into the prompt. This lets LLMs work with much bigger and more complex data sets.
CrewAI: Orchestrating Smart Agents for flexible RAG
CrewAI is a powerful Python framework. It lets you create and manage teams of autonomous AI agents. With CrewAI, you can define specialized agents (like a "Researcher" or an "Analyst") with specific roles, goals, and tools. CrewAI is key for RAG because it allows these agents to work together and share tasks. This makes the RAG process strong and efficient. The "Tools" are the crucial link, connecting your agents to all your different knowledge sources. Flexibility is key: CrewAI lets you use both built-in RAG features that come with the framework (like PDFSearchTool and Knowledge
) and custom tools you build yourself. This means you can connect to any specific data source you need, which is essential for real-world projects.
Implementing RAG with CrewAI: Three Practical Approaches
Here's how you can use CrewAI to implement RAG, showing its versatility.
Native RAG Tools
This is how CrewAI handles RAG natively using its built-in tools. The PDFSearchTool
is a prime example, designed for semantic searches directly within PDF content. A CrewAI agent can use this out-of-the-box tool to efficiently find and retrieve specific passages based on a search query within a PDF document. By default, it uses OpenAI for both embeddings (to understand meaning) and summarization, though this can be customized. This automates knowledge extraction from your existing PDF archives directly within the CrewAI ecosystem.
Knowledge for Document-Based RAG
CrewAI offers a native "Knowledge" feature that allows agents and crews to directly access and utilize various external information sources. This acts as a built-in reference library for your agents. You can provide knowledge sources in formats like raw strings, .txt
, .pdf
, .csv
, .xlsx
, and .json
documents by placing them in specified directories. CrewAI handles the storage (using ChromaDB by default) and embedding automatically. This approach empowers agents with direct access to a curated document set, ensuring their responses are grounded in your specific data.
Custom tool that uses Vector Search (FAISS in our case)
FAISS (Facebook AI Similarity Search) is great for very fast searches based on meaning, across huge sets of data (vector embeddings). Here, a custom tool lets CrewAI agents query a FAISS index. They find information that's semantically similar and feed it to the LLM. This provides fast and scalable semantic search for large, changing knowledge bases.
Comparative Table: RAG Implementations with CrewAI
Feature / Aspect | RAG with Native Tool (PDFSearchTool) | RAG with custom tool (FAISS Vector Search) | RAG with Native Feature (CrewAI Knowledge) |
Primary Data Type | Unstructured Text (e.g., PDF documents, scanned docs) | Semi-structured/Structured Text (embeddings) | Varied Document Types (text, PDF, CSV, JSON) |
Data Source Format | .pdf files | Vector embeddings from various data sources (text, images) | Files (.txt , .pdf , .csv , .xlsx , .json ), raw strings |
Query Mechanism | Semantic search within PDF content | Semantic similarity search (vector distance) | Semantic search within provided documents |
Retrieval Focus | Relevant content efficiently from PDFs | Semantic relevance, conceptual similarity | Contextual information from documents provided to agent/crew |
Scalability | Moderate (depends on PDF size/number) | High (optimized for large datasets) | Moderate (depends on volume of documents loaded) |
Update Frequency | Requires re-processing/re-indexing PDFs | Can be updated frequently (adding/updating vectors) | Managed by re-indexing/updating local document sets |
Complexity to Implement | Low (out-of-the-box CrewAI tool) | Moderate to High (embedding generation, index management) | Low (native to CrewAI, simple directory setup) |
Accuracy / Context | Good for direct PDF content retrieval | Excellent for semantic understanding & broad relevance | Good for grounding based on provided document sets |
Typical Use Cases | Legal document review, technical manuals, contracts, reports | Product catalogs, customer support FAQs, large document archives | Agent-specific knowledge, internal policy lookups, small-to-medium datasets |
CrewAI Agent Role Example | "Document Analyst", "Compliance Officer" | "Data Retriever", "Semantic Search Agent" | "Knowledge Expert", "Policy Reviewer", "Information Assistant" |
Key Advantage | Leverages existing PDF repositories with semantic search | Speed and scale for semantic search | Simple, native way to provide context to agents/crews |
Knowing these differences is critical. Each RAG method has unique strengths. Your choice depends on your data and goals. CrewAI's flexibility lets you seamlessly connect these different ways of getting information. This empowers your AI agents to use the best source, leading to truly smart and reliable generative AI applications.
Real-World Example: Demonstrating RAG Flexibility with Multi-Crew Setup
In a real-world project, we implemented a system to analyze PDF documents. This setup, whose code is available in this repository: CrewAI-RAG-Sample, effectively demonstrated CrewAI's versatility.
├── db
│ ├── 455d85ec-a7f9-4c9c-82d8-d3567d9263aa
│ └── chroma.sqlite3
├── Easy_recipes.pdf
├── knowledge
│ └── Easy_recipes.pdf
├── LICENSE
├── main.py
├── Pipfile
├── Pipfile.lock
├── ragcrew
│ ├── config
│ │ ├── agents.yaml
│ │ └── tasks.yaml
│ ├── faiss_rag_crew.py
│ ├── pdf_knowledge_crew.py
│ ├── tool_rag_crew.py
│ └── tools
│ └── custom_tool.py
├── README.md
├── report.md
└── setup.txt
We achieved this by employing three distinct crews, executed one after the other, to reach the same analytical goal. What made each crew unique was the specific RAG approach it utilized, while the core agents and tasks remained consistent. This allowed for a direct comparison of RAG strategies in action.
Each of these three crews was designed with the same set of specialized agents, for instance, a Document Analyst and a Content Reviewer. Similarly, the tasks they performed were identical across all crews: first, Information Retrieval to locate and extract relevant data from the PDF based on a user query; then, Content Refinement to process and summarize the retrieved information for clarity and conciseness; and finally, Output Generation to format and save the final, refined content into a Markdown (.md) file.
The key differentiator lay in how each crew performed its Information Retrieval task:
ToolRagCrew: PDF Analysis via Native PDFSearchTool. This crew leveraged CrewAI's out-of-the-box
PDFSearchTool
for its RAG mechanism. Agents directly queried PDF documents, showcasing a straightforward, native approach to unstructured document RAG.PDFKnowledgeCrew: PDF Analysis via Native CrewAI Knowledge. Here, the crew utilized CrewAI's native "Knowledge" feature. PDF documents were pre-loaded into the agents' knowledge base by placing them in a designated directory, allowing agents to access and integrate this curated information during their tasks.
FAISSRagCrew: PDF Analysis via Custom FAISS Tool. For this crew, a custom tool integrated with FAISS was implemented. This represented a more scalable solution for large datasets, where agents used this custom tool to perform semantic searches on vector embeddings derived from the PDF content.
This multi-crew architecture effectively highlights CrewAI's ability to maintain consistent operational logic (same agents, same tasks) while allowing for flexible and interchangeable RAG strategies. By executing each crew sequentially, we could directly observe the performance and nuances of different RAG implementations for the same problem, providing valuable insights for future deployments.
Conclusion: Your Expertise in Building Reliable AI
Combining CrewAI and these RAG techniques is essential for building robust, accurate, and relevant generative AI applications. My twenty years of experience as a Product Architect are crucial for putting these advanced AI solutions together effectively. This ensures not just technical function, but also scalability, security, and alignment with overall business goals.
Subscribe to my newsletter
Read articles from Roberto directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
