How alizeeshan-07/lightrag works
Project type and purpose
• LightRAG is an open-source Python framework for Retrieval-Augmented Generation (RAG). It provides:
• A core library (“lightrag/…”) that ingests text, chunks it, computes embeddings, builds a knowledge graph (KG), and performs retrieval in four modes (naive, local, global, hybrid).
• Pluggable adapters for LLMs (OpenAI, Hugging Face, Ollama), embedding functions, and storage backends (NetworkX, Neo4j, Oracle).
• A FastAPI-based REST server for remote use, plus numerous example scripts.
File structure
• Top-level: README, Dockerfile, setup.py, requirements.txt, tests, examples, reproduce
• “lightrag/”: core modules
• base.py: foundational classes
• lightrag.py: orchestrates insert/query pipelines
• llm.py: LLM adapter functions
• operate.py: high-level operations
• prompt.py: prompt templates & management
• storage.py: vector/KV store abstractions
• kg/neo4j_impl.py: Neo4j storage implementation
• utils.py: helpers (e.g., xml_to_json)
• “examples/”: demos for ingestion, query, graph viz, API server
• “reproduce/”: step-by-step scripts for dataset prep and evaluation
Main components & relationships
a. Client Layer
• Script or REST client → calls API server or direct library
b. API Server (FastAPI) [optional]
• Receives HTTP requests → calls Core RAG pipeline
c. Core RAG Pipeline
1. Text Ingestion
– Chunker / Document Loader (textract support)
– Entity & Relation Extractor (via LLMs)
2. Embedding Service
– Calls embedding_func → produces vector representations
3. Storage Layer
– Vector store (flat files in working_dir)
– Knowledge Graph store
• Default: NetworkX (GraphML in working_dir)
• Pluggable: Neo4j, Oracle KG
4. Query Engine
– Retrieval strategies (naive/local/global/hybrid)
– Prompt Builder (combines contexts & relations)
– LLM adapter (generation)
d. External Systems
• LLM providers: OpenAI API, Hugging Face Hub, Ollama daemon
• KG backends: Neo4j server, Oracle Database
Architectural patterns & design principles
• Strategy pattern for storage backends (kg="Neo4JStorage" vs default)
• Adapter pattern for LLMs and embedding functions
• Pipeline orchestration in LightRAG class (single entry point)
• Separation of concerns: ingestion, storage, retrieval, generation
Detail and extensibility
• Plugin points: storage (NetworkX vs Neo4j vs Oracle), llm_model_func, embedding_func
• Call out the working_dir as the file system storage root
• FastAPI server as an overlay if used in deployment