Skip to content
CrewAI

RAG Pipeline with CrewAI

Build a Retrieval-Augmented Generation pipeline that searches documents with vector embeddings and answers questions with citations — using CrewAI.

RAGvector storeembeddingsdocuments

Working Code

CrewAI
from crewai import Agent, Crew, Task
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
@tool
def search_documents(query: str, top_k: int = 5) -> str:
"""Search uploaded documents for relevant content."""
results = vector_store.similarity_search(query, k=top_k)
return "\n\n".join(
f"[{i+1}] {r.metadata.get('source', 'unknown')}: {r.page_content}"
for i, r in enumerate(results)
)
agent = Agent(
role="Specialist",
goal="You are a document Q&A assistant. Search documents before answering. Cite sources using [1], [2] format.",
tools=[search_documents],
llm=ChatOpenAI(model="gpt-4o"),
)
task = Task(
description="What does the refund policy say about digital products?",
expected_output="Detailed response",
agent=agent,
)
crew = Crew(agents=[agent], tasks=[task])
result = crew.kickoff()
print(result.raw)

Step by Step

1

Install dependencies

Install CrewAI and the required tools for this use case.

2

Define your tools

Create the domain-specific tool functions your agent will use to interact with external services.

3

Create the agent and run

Initialize the CrewAI agent with your tools, set the system prompt, and execute a query.

Ready to build with CrewAI?

Generate a production-ready project with CrewAI pre-configured — FastAPI + Next.js, auth, streaming, and more.

Get Started

Ready to build your first production AI agent?

Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.