π Local RAG Researcher with DeepSeek R1 & Langgraph
π Learn How to Build a Local RAG Researcher β Step-by-Step Guide Inside! π
I built a local adaptive RAG research agent using LangGraph and a local DeepSeek R1 model running on Ollama. This agent act like a deep researcher, designed to gather, analyze, and summarize information based on user instructions.
How It Works
-
Generating Research Queries β The agent takes user input and formulates relevant research questions to find the most useful information.
-
Retrieving Documents β It searches a local Chroma database to pull relevant documents related to the query.
-
Evaluating Relevance β Each document is checked against the original query to ensure it contains meaningful and accurate information.
-
Expanding Search if Needed β If the retrieved documents are not sufficient or relevant, the agent can search the web for additional sources.
-
Summarizing Findings β After gathering all necessary information, the agent processes the data and extracts key insights.
-
Final Report Generation β The summarized findings are sent to a writer agent, which structures the information into a detailed and well-formatted report based on a predefined format.
This system allows for an efficient and adaptive research process, ensuring high-quality and relevant outputs while minimizing unnecessary or low-value data.
Key Features
- Dynamic Search Through Local Documents β Efficiently retrieves relevant information from your internal documents.
- Advanced Insight Extraction β Leverages the reasoning power of DeepSeek R1 model to evaluate, analyze, and extract the most valuable insights from documents.
- Real-Time Web Search β Expands research by accessing online sources using Tavily API when local documents are insufficient.
- Structured Report Generation β Produces well-formatted reports based on your predefined reporting templates.
System Flowchart
This is the detailed flow of the system:
Tech Stack
- Ollama: Runs the DeepSeek R1 model locally.
- LangGraph: Builds AI agents and defines the researcher's workflow.
- ChromaDB: Local vector database for RAG-based retrieval.
- Streamlit: Provides a UI for interacting with the researcher.
- Tavily: For searching the web.
How to Run
Prerequisites
Ensure you have the following installed:
- Python 3.9+
- Ollama
- Tavily API key for web searchs
- Necessary Python libraries (listed in
requirements.txt)
Setup
Clone the Repository
git clone https://github.com/kaymen99/local-rag-researcher-deepseek
cd local-rag-researcher-deepseek
Create and Activate a Virtual Environment
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
Install Required Packages
pip install -r requirements.txt
Set Up Environment Variables
Create a .env file in the root directory and add necessary credentials:
# Tavily API key for SearchTool (optional)
TAVILY_API_KEY="your-api-key"
Running the Application
Step 1: Install and Run Ollama
Follow the official instructions to install Ollama, then pull the DeepSeek R1 model (this project uses the 7b model but you can choose any other models available):
ollama pull deepseek-r1:7b
Step 2: Launch the Streamlit App
Run the following command to start the UI:
streamlit run app.py
Step 3: Visualize in LangGraph Studio (Optional)
Since the researcher is built with LangGraph, you can use LangGraph Studio to inspect the agent's workflow. To do this, run the following commands:
pip install -U "langgraph-cli[inmem]"
langgraph dev
Customization
Modify Report Structures
- Add custom structures inside the
report_structuresfolder. - Select the preferred structure in the UI.
Using an External LLM Provider
By default, the researcher runs locally using the DeepSeek R1 model on Ollama. However, if you prefer to use a cloud-based LLM provider instead (such as Cloud DeepSeek R1, OpenAI GPT-4o, or OpenAI o1), follow these steps:
-
Modify the Code:
- Go to
assistant/graph.py. - Comment the code invoking Ollama model.
- Uncomment the section of code that enables external LLM calls.
invoke_llmuses OpenRouter, which provides access to multiple LLMs. You can choose your preferred model from their list. π- You can also modify the
invoke_llmfunction to use a single LLM provider instead of OpenRouter if you want.
- Go to
-
Set Up API Keys:
-
Obtain OpenRouter API key from here.
-
Add these keys to your
.envfile in the following format:OPENROUTER_API_KEY=your_openai_key
-
π Further Reading & Resources
-
Langchain: Building a fully local "deep researcher" with DeepSeek-R1see
-
Langchain: Building a fully local research assistant from scratch with Ollama see
-
LangGraph Template: Multi-Agent RAG Research see
-
LangGraph Adaptative RAG implementation see
Contributing
Contributions are welcome! Please open an issue or submit a pull request for any changes.
Contact
If you have any questions or suggestions, feel free to contact me at [email protected].