An advanced, agentic Hybrid GraphRAG system designed to extract and retrieve information from documents using a combination of vector search and knowledge graphs. Built for high accuracy and deep, multi-hop reasoning over complex text.
This project implements a dual-retrieval pipeline managed by a stateful agentic loop:
- Vector Retrieval: Uses
ChromaDBto store and retrieve semantic chunks. Embeddings are generated locally using theAlibaba-NLP/gte-Qwen2-1.5B-instructmodel viasentence-transformers. - Graph Retrieval: Uses an LLM to extract entities and relationships, storing them in a local
NetworkXgraph. This enables multi-hop reasoning across connected concepts. - Agentic Loop: Utilizes
LangGraph(specifically a ReAct agent) to dynamically route queries between the Vector Database and the Knowledge Graph. If the initial retrieved context is insufficient, the agent self-reflects, reformulates its query, and loops until a satisfactory answer is formed.
- Python 3.9 or higher
- 32GB RAM recommended for CPU-only environments.
- Clone the repository and navigate to the project directory:
git clone <your-repo-url>
cd DocRag- Create a virtual environment and activate it:
python -m venv venv
# On Windows:
.\venv\Scripts\activate
# On Linux/Mac:
source venv/bin/activate- Install the required dependencies:
pip install -r requirements.txtBy default, the system is configured to use local CPU inference.
If running purely locally, ensure you have Ollama installed and the Qwen 2.5 model pulled:
ollama pull qwen2.5Note: Extracting the knowledge graph purely on a CPU via a local LLM can be extremely time-consuming.
To significantly accelerate the knowledge graph extraction phase, you can use a free-tier API such as Together AI.
Create a .env file in the root directory and add your API key:
TOGETHER_API_KEY=your_api_key_hereWhen this key is present, the system will automatically switch to the remote API for LLM inference while keeping vector embeddings strictly local.
To parse a PDF, chunk the text, generate embeddings, and build the knowledge graph, run the ingestion script:
python ingest.py "path/to/your/document.pdf"This process will create a chroma_db directory for the vectors and a graph_db.graphml file for the knowledge graph.
Once the document is ingested, start the agentic chat interface:
python main.pyType your questions into the prompt. The LangGraph agent will output its reasoning process as it searches the vector database and the knowledge graph to synthesize the final answer. Type exit or quit to terminate the session.
This project is licensed under the MIT License.