Building Agentic RAG Application with DeepSeek R1 — A Step-by-Step Guide [Part 1]
Building Agentic RAG Application with LangChain, CrewAI, Ollama & DeepSeek
While Retrieval-Augmented Generation (RAG) dominated 2023, agentic workflows are driving massive progress in 2024 and 2025. The use of AI agents opens up new possibilities for building more powerful, robust, and versatile large language model (LLM)- powered applications.
One possibility is enhancing RAG pipelines with AI agents in agentic RAG pipelines. This article provides a step-by-step guide to building an agentic RAG application using the DeepSeek R1 model.
The first part of this two-part article will introduce you to the Agentic RAG workflow and the main components of our application. Then we will start the implementation by setting up the working environment and installing the LLM locally with Ollama. Then we will define the tools and th agents we will use.
In part 2, we will continue building the workflow by defining the agentic workflow tasks. We will then wrap up the whole agentic RAG workflow, put it into action, and test it with different queries.
Table of Contents:
Agentic RAG Pipeline
Setting Up the Working Environment & Installing DeepSeek LLM
Define Agentic RAG Tools
3.1. Define RAG Tool
3.2. Define Web Search ToolDefine Agents
4.1. Define Router Agent
4.2. Define Retriever Agent
4.3. Define Answer Grader Agent
4.4. Define Hallucination Grader Agent
4.5. Define Answer Generator AgentDefine Agents Tasks [Part 2]
5.1. Define Router Task
5.2. Define Retriever Task
5.3. Define Answer Grading Task
5.4. Define Hallucination Grading Task
5.5. Define Answer Generation TaskDefine Agentic Workflow [Part 2]
Agentic RAG Pipeline in Action [Part 2]
1. Agentic RAG Pipeline
Agentic RAG enhances traditional Retrieval-Augmented Generation by decomposing the process into specialized, autonomous AI agents. This pipeline ensures precision, reduces hallucinations, and optimizes context-aware responses. Below is a breakdown of the components and workflow:
Input Query: The user’s question or request initiates the pipeline.
Router Agent:
Role: Determines the query's intent, domain, and optimal retrieval strategy.
Function: Routes requests to domain-specific databases or external tools (e.g., APIs, vector stores).
Example: A medical query might trigger access to PubMed datasets, while a coding question routes to GitHub repositories.
3. Retrieval Agent:
Role: Fetches contextually relevant documents or data chunks from the routed source.
Function: Employs embedding models and similarity search algorithms (e.g., cosine similarity) to retrieve top-k candidates.
4. Grader Agent:
Role: Evaluate the quality and relevance of retrieved content.
Function: Uses a scoring model to rank snippets by confidence, eliminating low-quality or irrelevant results.
5. Hallucination Grader:
Role: Flags inaccuracies or unsupported claims in generated drafts.
Function: Cross-checks outputs against retrieved evidence using rule-based checks or a secondary LLM validator.
6. Answer Generator Agent:
Role: Synthesizes the final response.
Function: Combines graded evidence and query intent to generate a coherent, citation-backed answer.
DeepSeek R1 serves as the orchestration layer for this agentic pipeline:
Agent Chaining: DeepSeek R1 coordinates handoffs between agents (e.g., Router → Retrieval → Grader).
State Management: Maintains context across agents, enabling iterative refinement (e.g., re-retrieving data if the Hallucination Grader rejects a draft).
Customization: Allows developers to fine-tune individual agents (e.g., training the Router Agent on domain-specific intents).
2. Setting Up the Working Environment & Installing DeepSeek LLM
Before we start defining the tools, agents, and tasks we have, we should first install the packages we will need and also the LLM we will use which in this case will be the DeepSeek R1 model.
We will install the Ollama package to be able to use the model locally and crewai and crewai_tools as this is the framework we will use to build the agentic workflow. We will also install the langchain_community package to the tavily_search tool through it.
!pip install Ollama crewai==0.28.8 crewai_tools==0.1.6 langchain_community==0.0.29 --quiet
Next, we will import the main packages we will use throughout this tutorial.
import os
from crewai_tools import PDFSearchTool
from langchain_community.llms import Ollama
from langchain_community.tools.tavily_search import TavilySearchResults
from crewai_tools import tool
from crewai import Crew
from crewai import Task
from crewai import Agent
To install the LLM and use it locally we will use the Ollama tool. You need to follow these simple steps to be able to set the model and build the agent locally: