DeepSeek R1 Agentic RAG Setup with Small Agents: A Detailed Summary
Key Concepts:
- Agentic RAG: Combining reasoning LLMs (like DeepSeek R1) with faster, lightweight LLMs to create more intelligent and efficient retrieval-augmented generation workflows.
- Small Agents: A simple Python framework by Hugging Face for building agentic workflows with LLMs.
- Reasoning LLM: A large language model specifically designed for complex reasoning tasks (e.g., DeepSeek R1).
- Primary LLM: A faster, more lightweight LLM that drives the overall conversation and calls upon the reasoning LLM when needed (e.g., Llama, Qwen).
- Tool Calling: The ability of an LLM to invoke external functions or tools to perform specific tasks (e.g., RAG).
- Ollama: A tool for running LLMs locally on your machine.
- Context Limit: The maximum number of tokens an LLM can process in a single input.
- Chroma DB: An open-source embedding database.
1. Overview of Agentic RAG with R1
The video focuses on building a simple yet powerful agentic RAG system using DeepSeek R1 (a reasoning LLM) and Small Agents (a lightweight agent framework). The core idea is to leverage the reasoning capabilities of R1 for in-depth insights while maintaining a nimble conversation flow with a faster primary LLM.
- Problem: Reasoning models like R1 are powerful but slow.
- Solution: Combine R1 with a faster LLM that directs the conversation and uses R1 as a tool for complex reasoning tasks.
- Workflow: The primary LLM drives the conversation, and when it needs to extract in-depth insights from a knowledge base, it calls upon an R1-driven RAG tool.
2. System Architecture
The agentic RAG system consists of the following components:
- Primary LLM (Fast Agent): A fast LLM (e.g., Llama, Qwen) that manages the overall conversation flow. It uses Small Agents to break down problems into steps.
- RAG Tool: A tool that allows the primary LLM to access and reason over a knowledge base.
- Vector Database (Chroma DB): Stores the knowledge base as vector embeddings.
- Embedding Model: Used to embed user queries and knowledge base chunks.
- Reasoning LLM (R1): Extracts insights from the retrieved context using its reasoning capabilities.
Workflow Diagram:
- User query is received by the Primary LLM.
- The Primary LLM decides if it needs to look up knowledge.
- If knowledge lookup is needed, the Primary LLM calls the RAG Tool.
- The RAG Tool retrieves relevant context from the Vector Database.
- The retrieved context and user query are passed to the Reasoning LLM (R1).
- R1 extracts insights from the context.
- The insights are returned to the Primary LLM.
- The Primary LLM continues the conversation with the user.
3. Implementation with Small Agents
Small Agents simplifies the process of building agentic workflows. The video demonstrates how to set up the agentic RAG system using Small Agents and local models.
Steps:
- Install Dependencies:
pip install small-agents- Install Ollama from https://ollama.com/
- Import Libraries: Import necessary classes from Small Agents (e.g.,
Agent,Tool,UI). - Load Environment Variables: Load API keys and model IDs from a
.envfile. - Define
get_modelFunction: This function determines how to load the models, either from Hugging Face or locally using Ollama.- Hugging Face: Uses
HF_API_MODELto load models from the Hugging Face Inference API. - Ollama: Uses
OpenAIServerModelwith a modifiedbase_urlto point to the local Ollama instance.
- Hugging Face: Uses
- Create Reasoning Model (R1):
- Get the model using
get_model. - Create a
CodeAgentinstance with the reasoning model. - Set
max_steps=2to limit the reasoning loop.
- Get the model using
- Create RAG Tool:
- Define an embedding model using
HuggingFaceEmbeddings. - Get an instance of the Vector Database (Chroma DB).
- Use the
@tooldecorator to define therag_with_reasonerfunction.- This function takes the user query as input.
- It performs a similarity search in the Vector Database.
- It formats the context and user query into a prompt for R1.
- It calls R1 using
model.runto extract insights. - It returns the response to the primary LLM.
- Define an embedding model using
- Create Primary LLM (Tool Calling Agent):
- Get the model using
get_model. - Create a
ToolCallingAgentinstance with the primary LLM. - Pass the
rag_with_reasonertool to the agent. - Set
max_steps=3.
- Get the model using
- Create User Interface:
- Use
UI(agent).launch()to create a Gradio UI for interacting with the agent.
- Use
4. Knowledge Base Setup
The video uses Chroma DB as a local vector database for storing the knowledge base.
Steps:
- Load PDFs: Use Langchain's
PyPDFLoaderto load PDF documents from a data directory. - Split into Chunks: Split the documents into smaller chunks using
RecursiveCharacterTextSplitter. - Clear Vector Database: Define a function to delete the existing Chroma DB instance.
- Create Embeddings: Use the same embedding model used in the agent to create embeddings for the chunks.
- Insert into Chroma DB: Insert the chunks and their embeddings into the Chroma DB instance.
- Run the script: Execute the script to load the PDFs and create the vector database.
5. Ollama Setup and Model Configuration
The video provides instructions on how to set up Ollama and configure the context limit for the models.
Steps:
- Install Ollama: Download and install Ollama from https://ollama.com/
- Increase Context Limit:
- Navigate to the Ollama models folder.
- Create a new model file (e.g.,
DeepSeek-R1-7B-8k). - Add the following lines to the file:
FROM deepseek-r1-7b PARAMETER num_ctx 8192 - Run the following command in the terminal:
ollama create DeepSeek-R1-7B-8k -f DeepSeek-R1-7B-8k - This will create a new version of the model with an increased context limit of 8192 tokens.
- Set Environment Variables: Update the
.envfile with the correct Ollama model IDs for the reasoning model and the tool model.
6. Running the Agent
After setting up the agent, knowledge base, and Ollama, you can run the agent using the following command:
python R1_small_agent_rag.py
This will launch a Gradio UI in your browser, allowing you to interact with the agent.
7. Example Conversation
The video demonstrates an example conversation where the user asks the agent to compare and contrast the services offered by companies in the knowledge base. The agent uses the RAG tool to retrieve relevant context, and R1 extracts insights from the context to provide a comprehensive answer.
8. Key Takeaways
- Agentic RAG is a powerful approach for combining the strengths of different LLMs.
- Small Agents provides a simple framework for building agentic workflows.
- DeepSeek R1 is a capable reasoning LLM that can be used to extract in-depth insights from a knowledge base.
- Ollama allows you to run LLMs locally on your machine.
- Increasing the context limit of Ollama models is crucial for many use cases.
9. Conclusion
The video provides a practical guide to building an agentic RAG system with DeepSeek R1 and Small Agents. By combining the reasoning capabilities of R1 with the speed and flexibility of a primary LLM, you can create a more intelligent and efficient system for accessing and reasoning over knowledge bases. The video encourages viewers to explore the possibilities of agentic RAG and to experiment with different LLMs and tools to create even more powerful workflows.
AI summaries can miss context or contain errors. Check important details against the original video.





