Skip to main content

A minimal GraphRAG implementation in ~600 lines of Python code

Project description

GraphRAG-Lite

GraphRAG-Lite Logo

Minimal GraphRAG implementation in ~600 lines of Python code.

PyPI version Python 3.10+ License: Apache 2.0 Chat Group

中文文档

GraphRAG-Lite 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 graphrag-lite

Or install from source:

git clone https://github.com/shibing624/graphrag-lite.git
cd graphrag-lite
pip install -e .

Quick Start

import os
from graphrag_lite import GraphRAGLite

# Initialize
graph = GraphRAGLite(
    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

GraphRAGLite

GraphRAGLite(
    storage_path: str = "./graphrag_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

GraphRAG-Lite Workflow

  1. Insert: Documents are chunked, entities and relations are extracted via LLM, then embedded and stored
  2. Query: Question is used to search relevant entities/relations/chunks, context is built, LLM generates answer

Comparison with nano-graphrag

Feature GraphRAG-Lite 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

GraphRAG-Lite 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 xuming624 with 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{graphrag-lite,
  author = {Xu Ming},
  title = {GraphRAG-Lite: Minimal GraphRAG Implementation},
  year = {2025},
  url = {https://github.com/shibing624/graphrag-lite}
}

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

graphrag_lite-0.1.1.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graphrag_lite-0.1.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file graphrag_lite-0.1.1.tar.gz.

File metadata

  • Download URL: graphrag_lite-0.1.1.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for graphrag_lite-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8bdfce810c44eefa3b4ffa63a1fa7cb49bb3a3f6b59630013d0c8754af7ed737
MD5 c5044d4aa987a2f2bc8fb73e985bfd90
BLAKE2b-256 f0edce50e1428590461bd3738c2b345e5d3dc3b92d1d12588dd45bf15ec88824

See more details on using hashes here.

File details

Details for the file graphrag_lite-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: graphrag_lite-0.1.1-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

Hashes for graphrag_lite-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 901582b4aee3a3ea142fa8d8617403a7eb4d401c8fd229d0e2431fcfe26ffd3b
MD5 607986778fa863155b56894015057af9
BLAKE2b-256 0d42dfb11b7798c937596d6b8e811fd9d51780c6290c21eaf9b951d39112d13d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page