|
1 | | -# LocalRAG-Kit 🚀 |
| 1 | +<div align="center"> |
2 | 2 |
|
3 | | -> **100% Offline, Privacy-First Local RAG Search & Chat Engine for Documents and Codebases.** |
4 | | -> Zero cloud vector database fees. Zero data leaks. Runs entirely on your local machine. |
| 3 | +# ⚡ LocalRAG-Kit |
| 4 | + |
| 5 | +**100% Offline, Privacy-First Local RAG Search & Chat Engine for Codebases and Documents** |
| 6 | + |
| 7 | +[](https://github.com/RitualDev-Lab/localrag-kit/actions) |
| 8 | +[](https://www.python.org/downloads/) |
| 9 | +[](LICENSE) |
| 10 | +[](tests/) |
| 11 | +[]() |
| 12 | + |
| 13 | +*Zero cloud vector database fees. Zero telemetry. Zero data leaks.* |
| 14 | +*Indexes your repositories, PDFs, and docs into a single portable SQLite database with hybrid BM25 + dense vector search and an interactive local web UI.* |
| 15 | + |
| 16 | +</div> |
5 | 17 |
|
6 | 18 | --- |
7 | 19 |
|
8 | | -## ⚡ Key Highlights |
| 20 | +## ✨ Features |
9 | 21 |
|
10 | | -- **🔒 100% Offline & Private**: No data leaves your machine. Compatible with local LLMs (Ollama) and local embeddings. |
11 | | -- **📁 Multi-Format Ingestion**: Scans and parses Markdown (`.md`), Code (`.py`, `.ts`, `.js`, `.go`, `.rs`, `.java`, etc.), Plain Text (`.txt`), HTML, and PDFs. |
12 | | -- **🧩 Structure-Aware Smart Chunking**: |
13 | | - - **Code**: Splits on function and class declarations while keeping imports and docstrings intact. |
14 | | - - **Markdown**: Heading-aware chunking preserving `#`, `##`, `###` hierarchy and code fences. |
15 | | - - **Text / PDF**: Token-aware sliding window preserving exact start/end line numbers. |
16 | | -- **⚡ Zero-Infra Hybrid Storage**: Pure SQLite database (`.localrag/index.db`) combining Dense Vector Search with SQLite FTS5 (BM25) full-text search. |
17 | | -- **🎯 Reciprocal Rank Fusion (RRF)**: Merges semantic vector similarity with exact keyword lookup for 10x better precision on code symbols and error codes. |
| 22 | +- 🔒 **100% Offline & Air-Gapped**: Runs entirely on your CPU/GPU. No external server dependencies or cloud database subscriptions required. |
| 23 | +- ⚡ **Zero-Infra Hybrid Storage**: Pure SQLite database (`.localrag/index.db`) combining SQLite FTS5 (BM25 keyword search) with dense vector embeddings via binary BLOBs and fast NumPy SIMD batch cosine similarity. |
| 24 | +- 🎯 **Reciprocal Rank Fusion (RRF)**: Merges semantic vector similarity with exact BM25 keyword rankings ($k_{rrf} = 60$). Solves the classic vector search weakness for exact code symbols, variable names, and error codes. |
| 25 | +- 🧩 **Structure-Aware Smart Chunking**: |
| 26 | + - **Code**: Language-aware boundary chunking for Python, JavaScript, TypeScript, Go, Rust, Java, C/C++ that preserves function/class signatures and docstrings. |
| 27 | + - **Markdown**: Heading-aware (`#`, `##`, `###`) chunking that prevents code fences from being broken mid-snippet. |
| 28 | + - **Text / PDF**: Token-aware sliding window with deterministic 1-based start and end line coordinates. |
| 29 | +- 🔄 **Incremental SHA-256 Change Detection**: Re-indexing only processes newly created or modified files. Deleted files are automatically cascaded out of SQLite and FTS5 indices. |
| 30 | +- 🤖 **Flexible Provider Ecosystem**: |
| 31 | + - **Embeddings**: Built-in zero-download `fast` 384-dim feature hasher, local `ollama` (`nomic-embed-text`), or `openai`. |
| 32 | + - **LLMs**: Local `ollama` (`llama3.2`, `qwen2.5`, `mistral`), `openai`, or mock testing agent. |
| 33 | +- 🖥️ **Interactive Web Interface**: Clean, dark-mode glassmorphic Tailwind UI featuring real-time SSE token streaming, inline citation badges, a source inspector slideout drawer, and live index management. |
| 34 | +- 💻 **Feature-Rich CLI**: Built with `rich` for colorful terminal tables, progress bars, interactive query synthesis, and streaming terminal output. |
18 | 35 |
|
19 | 36 | --- |
20 | 37 |
|
21 | | -## 📦 Quickstart |
| 38 | +## 🏛️ System Architecture |
| 39 | + |
| 40 | +```mermaid |
| 41 | +flowchart TB |
| 42 | + subgraph Ingestion ["1. INGESTION & PARSING"] |
| 43 | + A[Local Files & Codebase] --> B[File Harvester & Ignore Filter] |
| 44 | + B --> C{File Type Dispatch} |
| 45 | + C -->|Markdown| D[Markdown Chunking Engine] |
| 46 | + C -->|Code: py, ts, go, rs, etc.| E[Code Boundary Chunker] |
| 47 | + C -->|PDF & Text| F[Sliding Window Chunker] |
| 48 | + end |
| 49 | +
|
| 50 | + subgraph Storage ["2. ZERO-INFRA STORAGE (SQLite)"] |
| 51 | + D & E & F --> G[(.localrag/index.db)] |
| 52 | + G --> H[FTS5 Full-Text Search Engine] |
| 53 | + G --> I[Binary Vector Storage & SHA-256 Hashes] |
| 54 | + end |
| 55 | +
|
| 56 | + subgraph Retrieval ["3. HYBRID RETRIEVAL & CITATION"] |
| 57 | + Q[User Query] --> J[BM25 Keyword Search] |
| 58 | + Q --> K[Vector Cosine Similarity] |
| 59 | + H --> J |
| 60 | + I --> K |
| 61 | + J & K --> L[Reciprocal Rank Fusion - RRF] |
| 62 | + L --> M[Context Synthesizer & Token Budget] |
| 63 | + end |
| 64 | +
|
| 65 | + subgraph Presentation ["4. INTERFACES"] |
| 66 | + M --> N[Local LLM - Ollama / OpenAI] |
| 67 | + N --> O[Rich Terminal CLI] |
| 68 | + N --> P[FastAPI Server & Web UI] |
| 69 | + end |
| 70 | +``` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## 🚀 Quickstart |
22 | 75 |
|
23 | 76 | ### 1. Installation |
| 77 | + |
24 | 78 | ```bash |
| 79 | +# Clone repository |
25 | 80 | git clone https://github.com/RitualDev-Lab/localrag-kit.git |
26 | 81 | cd localrag-kit |
| 82 | + |
| 83 | +# Create virtual environment |
27 | 84 | python -m venv .venv |
28 | | -# On Windows: |
29 | | -.\venv\Scripts\pip install -r requirements.txt |
30 | | -# On Linux/macOS: |
31 | | -source .venv/bin/activate && pip install -r requirements.txt |
| 85 | +source .venv/bin/activate # On Windows: .venv\Scripts\Activate.ps1 |
| 86 | + |
| 87 | +# Install in editable mode |
| 88 | +pip install -e . |
| 89 | +``` |
| 90 | + |
| 91 | +### 2. Index Any Folder or Codebase |
| 92 | + |
| 93 | +```bash |
| 94 | +# Preview files and chunk distribution without writing to disk |
| 95 | +localrag scan ./my-project --preview |
| 96 | + |
| 97 | +# Index files into pure SQLite (uses offline fast hasher by default) |
| 98 | +localrag index ./my-project |
| 99 | + |
| 100 | +# Or index with local Ollama embeddings: |
| 101 | +localrag index ./my-project --embed-provider ollama --embed-model nomic-embed-text |
32 | 102 | ``` |
33 | 103 |
|
34 | | -### 2. Scan & Preview Any Folder |
| 104 | +### 3. Ask Questions from the Terminal |
| 105 | + |
35 | 106 | ```bash |
36 | | -python -m localrag.cli scan /path/to/project --preview |
| 107 | +# Ask with hybrid retrieval and local Ollama |
| 108 | +localrag ask "How does the authentication middleware work?" --mode hybrid --llm ollama |
| 109 | + |
| 110 | +# Search matching chunks with line coordinates |
| 111 | +localrag search "create_app" --mode hybrid --limit 5 |
37 | 112 | ``` |
38 | 113 |
|
| 114 | +### 4. Launch the Web Interface |
| 115 | + |
| 116 | +```bash |
| 117 | +localrag serve ./my-project --port 8000 |
| 118 | +``` |
| 119 | +Open `http://127.0.0.1:8000` in your browser to access the interactive web console. |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +## 🛠️ CLI Command Reference |
| 124 | + |
| 125 | +| Command | Description | Example | |
| 126 | +| :--- | :--- | :--- | |
| 127 | +| `localrag scan` | Inspect eligible files, token estimates, and chunk counts without indexing | `localrag scan . --preview` | |
| 128 | +| `localrag index` | Incremental or full indexing of workspace files into SQLite + FTS5 | `localrag index . --full` | |
| 129 | +| `localrag search` | Perform hybrid RRF, BM25 keyword, or vector similarity search | `localrag search "token" --mode hybrid` | |
| 130 | +| `localrag ask` | Query workspace with streaming grounded LLM answers and citations | `localrag ask "Explain routes.py" --llm ollama` | |
| 131 | +| `localrag stats` | Display index statistics, chunk counts, database size, and providers | `localrag stats .` | |
| 132 | +| `localrag serve` | Launch the FastAPI server and embedded glassmorphic web UI | `localrag serve . --port 8000` | |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## 💻 Python API Usage |
| 137 | + |
| 138 | +LocalRAG-Kit can also be embedded directly into your Python scripts and services: |
| 139 | + |
| 140 | +```python |
| 141 | +from pathlib import Path |
| 142 | +from localrag.storage import SQLiteStore |
| 143 | +from localrag.providers import FastFeatureEmbeddingProvider, OllamaLLMProvider |
| 144 | +from localrag.retrieval import RAGOrchestrator |
| 145 | + |
| 146 | +# Initialize storage and providers |
| 147 | +store = SQLiteStore(".localrag/index.db") |
| 148 | +embedder = FastFeatureEmbeddingProvider() |
| 149 | +llm = OllamaLLMProvider(model="llama3.2") |
| 150 | + |
| 151 | +# Orchestrate grounded retrieval |
| 152 | +orchestrator = RAGOrchestrator( |
| 153 | + store=store, |
| 154 | + embedding_provider=embedder, |
| 155 | + llm_provider=llm, |
| 156 | +) |
| 157 | + |
| 158 | +# Stream response with citations |
| 159 | +stream = orchestrator.stream_answer( |
| 160 | + query="Explain the database schema design", |
| 161 | + mode="hybrid", |
| 162 | + top_k=5, |
| 163 | +) |
| 164 | + |
| 165 | +print(f"Citations: {[c.relative_path for c in stream.citations]}") |
| 166 | +for token in stream.token_iterator: |
| 167 | + print(token, end="", flush=True) |
| 168 | +``` |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## 🌐 REST & SSE Streaming API |
| 173 | + |
| 174 | +When running `localrag serve`, the following API endpoints are exposed: |
| 175 | + |
| 176 | +- `GET /` — Serves the modern Web UI |
| 177 | +- `GET /api/status` — Returns workspace stats, chunk counts, and active providers |
| 178 | +- `GET /api/files` — Lists all indexed files with line numbers and metadata |
| 179 | +- `POST /api/search` — Runs hybrid RRF, BM25, or vector similarity search |
| 180 | +- `POST /api/chat` — SSE streaming endpoint (`data: {"type": "token", "token": "..."}`) |
| 181 | +- `POST /api/reindex` — Triggers an incremental or full re-index |
| 182 | +- `GET /api/chunk/{id}` — Retrieves chunk details and surrounding file context |
| 183 | +- `GET /api/file-content` — Reads raw text of a physical workspace file |
| 184 | + |
39 | 185 | --- |
40 | 186 |
|
41 | | -## 🧪 Running Tests |
| 187 | +## 🧪 Testing |
| 188 | + |
| 189 | +The test suite contains 31 unit and integration tests covering chunking, FTS5 BM25, vector math, change detection, SSE streaming, and web UI serving: |
| 190 | + |
42 | 191 | ```bash |
43 | | -python -m pytest -v |
| 192 | +pytest -v |
44 | 193 | ``` |
45 | 194 |
|
46 | 195 | --- |
47 | 196 |
|
48 | | -## 📜 License |
49 | | -MIT © [RitualDev-Lab](https://github.com/RitualDev-Lab) |
| 197 | +## 📄 License |
| 198 | + |
| 199 | +This project is licensed under the [MIT License](LICENSE). |
0 commit comments