A minimal GraphRAG implementation in ~600 lines of Python code
Project description
NanoGraphRAG
Minimal GraphRAG implementation in ~600 lines of Python code.
NanoGraphRAG is a lightweight, educational implementation of GraphRAG (Graph-based Retrieval-Augmented Generation). It's designed to help you understand the core principles of GraphRAG in a single afternoon.
Features
- Minimal: ~600 lines of code, easy to read and understand
- Zero Config: Only requires OpenAI API key, no complex setup
- 4 Query Modes: local, global, mix, naive
- Optimized: Batch embeddings, LLM caching, NumPy-accelerated vector search
- Streaming: Real-time response output support
Installation
pip install nanographrag
Or install from source:
git clone https://github.com/shibing624/nanographrag.git
cd nanographrag
pip install -e .
Quick Start
import os
from nanographrag import NanoGraphRAG
# Initialize
graph = NanoGraphRAG(
storage_path="./my_graph",
api_key=os.getenv("OPENAI_API_KEY"),
base_url=os.getenv("OPENAI_BASE_URL"), # Optional
)
# Insert documents
graph.insert("""
Charles Dickens wrote "A Christmas Carol" in 1843.
The story features Ebenezer Scrooge, a miserly old man,
and the ghost of his former business partner Jacob Marley.
""")
# Query
answer = graph.query("What is the relationship between Scrooge and Marley?")
print(answer)
Query Modes
| Mode | Description | Use Case |
|---|---|---|
local |
Entity → Related relations | Specific entity questions |
global |
Relation → Related entities | Relationship questions |
mix |
Entity + Relation + Text chunks | Recommended for most cases |
naive |
Text chunks only (traditional RAG) | Baseline comparison |
# Different query modes
answer = graph.query("Who is Scrooge?", mode="local")
answer = graph.query("Who is Scrooge?", mode="global")
answer = graph.query("Who is Scrooge?", mode="mix") # Recommended
answer = graph.query("Who is Scrooge?", mode="naive")
Streaming Output
for chunk in graph.query("Who is Scrooge?", stream=True):
print(chunk, end="", flush=True)
API Reference
NanoGraphRAG
NanoGraphRAG(
storage_path: str = "./nanograph_data", # Data storage directory
api_key: str = None, # OpenAI API key
base_url: str = None, # OpenAI API base URL
model: str = "gpt-4o-mini", # LLM model
embedding_model: str = "text-embedding-3-small", # Embedding model
chunk_size: int = 1200, # Text chunk size
chunk_overlap: int = 100, # Chunk overlap
enable_cache: bool = True, # Enable LLM response caching
)
Methods
| Method | Description |
|---|---|
insert(text, doc_id=None) |
Insert document and build knowledge graph |
query(question, mode="mix", top_k=10, stream=False) |
Query the knowledge graph |
has_data() |
Check if data exists |
get_stats() |
Get statistics |
list_entities() |
List all entities |
list_relations() |
List all relations |
clear() |
Clear all data |
How It Works
- Insert: Documents are chunked, entities and relations are extracted via LLM, then embedded and stored
- Query: Question is used to search relevant entities/relations/chunks, context is built, LLM generates answer
Comparison with nano-graphrag
| Feature | NanoGraphRAG | nano-graphrag |
|---|---|---|
| Code Size | ~600 lines | ~1100 lines |
| Dependencies | openai, numpy, tiktoken | networkx, nano-vectordb, ... |
| LLM Support | OpenAI only | Multiple (OpenAI, Ollama, etc.) |
| Vector Storage | In-memory + JSON | Multiple backends |
| Async Support | ❌ | ✅ |
| Purpose | Educational | Production |
NanoGraphRAG is designed for learning and understanding GraphRAG principles, not for production use. If you need a production-ready solution, consider nano-graphrag or LightRAG.
Community & Support
- GitHub Issues: Have questions or feature requests? Submit an issue.
- WeChat: Join our developer community! Add WeChat ID
xuming624with note "llm" to join the group chat.
License
Apache License 2.0
Citation
If you find this project useful, please consider giving it a ⭐ on GitHub!
@software{nanographrag,
author = {Xu Ming},
title = {NanoGraphRAG: Minimal GraphRAG Implementation},
year = {2026},
url = {https://github.com/shibing624/nanographrag}
}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file graphrag_lite-0.1.0.tar.gz.
File metadata
- Download URL: graphrag_lite-0.1.0.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ca8085a2ee74842c9c5259beaa0f36d34869150815e9d63340809abec418f2f
|
|
| MD5 |
c05ff6ccf2f6f63a7637f2e7e55e432b
|
|
| BLAKE2b-256 |
0b9b1e34e4381f23fa493107a34a18769032b96d1e4ac97077e14d8104ca28ae
|
File details
Details for the file graphrag_lite-0.1.0-py3-none-any.whl.
File metadata
- Download URL: graphrag_lite-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53a5a61c9529aeb0121838036ebede4b189def61499eda5e9f779ab21f70ed0a
|
|
| MD5 |
1c2ab3c98b19a7bdf7173e4faaa8fd76
|
|
| BLAKE2b-256 |
d4f7c68dc8656c343fbcf75c4a2b54036be43cd72b7e71da98ac41e98aa71172
|