# π LEANN: A Low-Storage Vector Index
β‘ Real-time embedding computation for large-scale RAG on consumer hardware
Quick Start β’ Features β’ Benchmarks β’ Documentation β’ Paper
--- ## π What is Leann? **Leann** revolutionizes Retrieval-Augmented Generation (RAG) by eliminating the storage bottleneck of traditional vector databases. Instead of pre-computing and storing billions of embeddings, Leann dynamically computes embeddings at query time using highly optimized graph-based search algorithms. ### π― Why Leann? Traditional RAG systems face a fundamental trade-off: - **πΎ Storage**: Storing embeddings for millions of documents requires massive disk space - **π Freshness**: Pre-computed embeddings become stale when documents change - **π° Cost**: Vector databases are expensive to scale **Leann solves this by:** - β **Zero embedding storage** - Only graph structure is persisted - β **Real-time computation** - Embeddings computed on-demand with ms latency - β **Memory efficient** - Runs on consumer hardware (8GB RAM) - β **Always fresh** - No stale embeddings, ever ## π Quick Start ### Installation ```bash git clone git@github.com:yichuan520030910320/LEANN-RAG.git leann cd leann git submodule update --init --recursive uv sync ``` ### 30-Second Example ```python from leann.api import LeannBuilder, LeannSearcher # 1. Build index (no embeddings stored!) builder = LeannBuilder(backend_name="diskann") builder.add_text("Python is a powerful programming language") builder.add_text("Machine learning transforms industries") builder.add_text("Neural networks process complex data") builder.build_index("knowledge.leann") # 2. Search with real-time embeddings searcher = LeannSearcher("knowledge.leann") results = searcher.search("programming languages", top_k=2) for result in results: print(f"Score: {result['score']:.3f} - {result['text']}") ``` ### Run the Demo ```bash uv run examples/document_search.py ``` **PDF RAG Demo (using LlamaIndex for document parsing and Leann for indexing/search)** This demo showcases how to build a RAG system for PDF documents using Leann. 1. Place your PDF files (and other supported formats like .docx, .pptx, .xlsx) into the `examples/data/` directory. 2. Ensure you have an `OPENAI_API_KEY` set in your environment variables or in a `.env` file for the LLM to function. ```bash uv run examples/main_cli_example.py ``` ## β¨ Features ### π₯ Core Features - **π Multiple Distance Functions**: L2, Cosine, MIPS (Maximum Inner Product Search) - **ποΈ Pluggable Backends**: DiskANN, HNSW/FAISS with unified API - **π Real-time Embeddings**: Dynamic computation using optimized ZMQ servers - **π Scalable Architecture**: Handles millions of documents on consumer hardware - **π― Graph Pruning**: Advanced techniques for memory-efficient search ### π οΈ Technical Highlights - **Zero-copy operations** for maximum performance - **SIMD-optimized** distance computations (AVX2/AVX512) - **Async embedding pipeline** with batched processing - **Memory-mapped indices** for fast startup - **Recompute mode** for highest accuracy scenarios ### π¨ Developer Experience - **Simple Python API** - Get started in minutes - **Extensible backend system** - Easy to add new algorithms - **Comprehensive examples** - From basic usage to production deployment - **Rich debugging tools** - Built-in performance profiling ## π Benchmarks ### Memory Usage Comparison | System | 1M Documents | 10M Documents | 100M Documents | |--------|-------------|---------------|----------------| | Traditional Vector DB | 3.1 GB | 31 GB | 310 GB | | **Leann** | **180 MB** | **1.2 GB** | **8.4 GB** | | **Reduction** | **94.2%** | **96.1%** | **97.3%** | ### Query Performance | Backend | Index Size | Query Time | Recall@10 | |---------|------------|------------|-----------| | DiskANN | 1M docs | 12ms | 0.95 | | DiskANN + Recompute | 1M docs | 145ms | 0.98 | | HNSW | 1M docs | 8ms | 0.93 | *Benchmarks run on AMD Ryzen 7 with 32GB RAM* ## ποΈ Architecture ``` βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ β Query Text βββββΆβ Embedding βββββΆβ Graph-based β β β β Computation β β Search β βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ β β βΌ βΌ ββββββββββββββββ ββββββββββββββββ β ZMQ Server β β Pruned Graph β β (Cached) β β Index β ββββββββββββββββ ββββββββββββββββ ``` ### Key Components 1. **π§ Embedding Engine**: Real-time transformer inference with caching 2. **π Graph Index**: Memory-efficient navigation structures 3. **π Search Coordinator**: Orchestrates embedding + graph search 4. **β‘ Backend Adapters**: Pluggable algorithm implementations ## π Supported Models & Backends ### π€ Embedding Models - **sentence-transformers/all-mpnet-base-v2** (default) - **sentence-transformers/all-MiniLM-L6-v2** (lightweight) - Any HuggingFace sentence-transformer model - Custom model support via API ### π§ Search Backends - **DiskANN**: Microsoft's billion-scale ANN algorithm - **HNSW**: Hierarchical Navigable Small World graphs - **Coming soon**: ScaNN, Faiss-IVF, NGT ### π Distance Functions - **L2**: Euclidean distance for precise similarity - **Cosine**: Angular similarity for normalized vectors - **MIPS**: Maximum Inner Product Search for recommendation systems ## π¬ Paper If you find Leann useful, please cite: **[LEANN: A Low-Storage Vector Index](https://arxiv.org/abs/2506.08276)** ```bibtex @misc{wang2025leannlowstoragevectorindex, title={LEANN: A Low-Storage Vector Index}, author={Yichuan Wang and Shu Liu and Zhifei Li and Yongji Wu and Ziming Mao and Yilong Zhao and Xiao Yan and Zhiying Xu and Yang Zhou and Ion Stoica and Sewon Min and Matei Zaharia and Joseph E. Gonzalez}, year={2025}, eprint={2506.08276}, archivePrefix={arXiv}, primaryClass={cs.DB}, url={https://arxiv.org/abs/2506.08276}, } ``` ## π Use Cases ### πΌ Enterprise RAG ```python # Handle millions of documents with limited resources builder = LeannBuilder( backend_name="diskann", distance_metric="cosine", graph_degree=64, memory_budget="4GB" ) ``` ### π¬ Research & Experimentation ```python # Quick prototyping with different algorithms for backend in ["diskann", "hnsw"]: searcher = LeannSearcher(index_path, backend=backend) evaluate_recall(searcher, queries, ground_truth) ``` ### π Real-time Applications ```python # Sub-second response times chat = LeannChat("knowledge.leann") response = chat.ask("What is quantum computing?") # Returns in <100ms with recompute mode ``` ## π€ Contributing We welcome contributions! Leann is built by the community, for the community. ### Ways to Contribute - π **Bug Reports**: Found an issue? Let us know! - π‘ **Feature Requests**: Have an idea? We'd love to hear it! - π§ **Code Contributions**: PRs welcome for all skill levels - π **Documentation**: Help make Leann more accessible - π§ͺ **Benchmarks**: Share your performance results ### Development Setup ```bash git clone https://github.com/yourname/leann cd leann uv sync --dev uv run pytest tests/ ``` ### Quick Tests ```bash # Sanity check all distance functions uv run python tests/sanity_checks/test_distance_functions.py # Verify L2 implementation uv run python tests/sanity_checks/test_l2_verification.py ``` ## β FAQ ### Common Issues #### NCCL Topology Error **Problem**: You encounter `ncclTopoComputePaths` error during document processing: ``` ncclTopoComputePaths (system=β Star us on GitHub if Leann is useful for your research or applications!
Made with β€οΈ by the Leann team