KeyRAG
Officialby KitiLab
README.md
# KeyRAG — by [KitiLab](https://github.com/KitiLab)
[](#)
[](#)
[](LICENSE)
[](#)
> **Beat 133 MB Transformer RAG stacks on official public BEIR benchmarks — in ~5–14 ms on a laptop CPU, inside one portable `.krag` SQLite file, with zero PyTorch, zero Docker, and zero background servers.**
---
## Verified BEIR Benchmarks (Reproducible in One Command)
Measured locally on an x86_64 Linux laptop (Python 3.15, SQLite 3.54, `sqlite-vec 0.1.9`) using **`minishlab/blink-8M`** quantized to a **7.44 MB `int8`** mmap table + SQLite Porter FTS5 + Adaptive IDF-Weighted Convex Fusion:
| System | Runtime Engine | Model Size | SciFact `nDCG@10` | SciFact `R@10` | NFCorpus `nDCG@10` | Local CPU Latency (`P50 / P95`) |
| :--- | :--- | :---: | :---: | :---: | :---: | :---: |
| **KeyRAG (Adaptive Hybrid)** | **C SQLite + C `sqlite-vec` + Static `int8`** | **7.4 MB** | **72.83%** | **86.79%** | **35.19%** | **5.7–11.5 ms / 8.6–16.0 ms** |
| `BAAI/bge-small-en-v1.5` *(published ref)* | 33M Transformer (PyTorch/ONNX) | 133 MB | 70.80% | 83.0% | 34.20% | ~30–80 ms |
| BM25 Only (KeyRAG Porter FTS5) | C SQLite FTS5 | 0 MB | 69.33% | 82.38% | 32.90% | 1.8–6.2 ms / 4.8–11.5 ms |
| Dense Only (`blink-8M/int8`) | C `sqlite-vec` `int8` | 7.4 MB | 65.64% | 79.97% | 31.28% | 4.2–6.1 ms / 4.6–8.7 ms |
| `all-MiniLM-L6-v2` *(published ref)* | 22M Transformer (PyTorch/ONNX) | 80 MB | 65.29% | 82.29% | 31.83% | ~25–60 ms |
> **Honest Ablation Note (Why We Cut MS-MARCO Cross-Encoders):** We tested `cross-encoder/ms-marco-TinyBERT-L-2-v2` (17 MB) on top of KeyRAG. It **dropped** SciFact `nDCG@10` from **72.60% → 69.96%** (`-2.64%`) and NFCorpus from **35.23% → 34.99%** while slowing queries **40×** (`399 ms P50`). Web-trained MS-MARCO cross-encoders do not transfer to scientific corpora—so KeyRAG stays lean, fast, and purely adaptive-hybrid by default.
<p align="center">
<img src="bench/charts/leaderboard_scifact.svg" alt="KeyRAG SciFact Leaderboard" width="49%"/>
<img src="bench/charts/leaderboard_nfcorpus.svg" alt="KeyRAG NFCorpus Leaderboard" width="49%"/>
</p>
### Native Research-Paper PDF Speed (C++ PDFium)
| Paper (arXiv) | Layout | Parse Time | Throughput | Quality |
| :--- | :--- | :---: | :---: | :---: |
| *Attention Is All You Need* (15 pages) | 1-col NeurIPS | **153.9 ms** | **97.4 pages/sec** | `1.00` |
| *BERT* (16 pages) | **2-col ACL/NAACL** | **122.2 ms** | **130.9 pages/sec** | `1.00` |
| *RAG* (19 pages) | 1-col NeurIPS | **92.7 ms** | **204.9 pages/sec** | `1.00` |
*All 3 papers (50 pages, 283 chunks) parse, chunk, embed (`blink-8M/int8`), and index into a single `.krag` file in **781 ms total**.*
---
## Architecture (Clean, Readable, Native Speed)
```text
AI CLIENTS: Claude Desktop · Cursor · Windsurf · VS Code · REST / OpenAPI · CLI
│ MCP stdio (JSON-RPC 2.0) │ Streamable HTTP (/mcp) │ REST (/v1/*)
▼
┌────────────────────────────── KeyRAG (Python shell) ───────────────────────────────┐
│ 1. Native C++ PDFium Reader (pypdfium2) │
│ 100% lossless · 2-column reading order · font-size titles · section tree │
│ 2. Single-File `.krag` Vault (C SQLite + C sqlite-vec) │
│ Live-only Porter FTS5 + fts5vocab IDF + int8 vectors (auto-IVF at >=15k rows) │
│ 3. Adaptive IDF-Weighted Convex Hybrid Search │
│ Zero-PyTorch mmap `blink-8M/int8` (7.4 MB) + query-adaptive BM25/Dense fusion │
└────────────────────────────── one file: papers.krag ───────────────────────────────┘
```
---
## Quickstart (2 Minutes)
### 1. Install
```bash
git clone https://github.com/KitiLab/KeyRAG.git
cd KeyRAG
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\Activate.ps1
pip install -e .
```
### 2. Export the 7.4 MB `blink-8M` Retrieval Table (One-Time)
```bash
pip install -e ".[export]"
keyrag export-model --model minishlab/blink-8M
```
### 3. Ingest & Search Real Research Papers
```bash
# Grab 3 classic open-access arXiv papers (Attention, 2-column BERT, RAG)
python scripts/fetch_papers.py
# Parse & index all 3 into ONE portable `papers.krag` file (~780 ms total!)
keyrag ingest papers/
# Search with exact verifiable page & section citations (~5 ms)
keyrag search "How does multi-head attention work and what is d_k?"
keyrag search "How does BERT pre-training use masked language modeling?"
keyrag docs
keyrag stats
```
### 4. Plug Into Any AI Client (Universal MCP + OpenAPI)
```bash
# One-command auto-registration for Cursor, Claude Desktop, Windsurf, or VS Code:
keyrag --db papers.krag install --client cursor
keyrag --db papers.krag install --client claude-desktop
# Or start Streamable HTTP MCP (/mcp) + REST (/v1/*) + OpenAPI (/openapi.json):
keyrag --db papers.krag serve --http --port 8765
```
### 5. Reproduce the Public BEIR Benchmarks & Charts
```bash
python bench/run_beir.py --dataset scifact
python bench/run_beir.py --dataset nfcorpus
```
---
## Repository Structure
```text
KeyRAG/
├── keyrag/
│ ├── pdf.py # Native C++ PDFium paper reader (2-col, font titles, sections, hyphen repair)
│ ├── embed.py # Vendored WordPiece + 7.4 MB mmap int8 table (`blink-8M`) + IVF k-means
│ ├── vault.py # Single-file `.krag` SQLite + live Porter FTS5 + fts5vocab + sqlite-vec
│ ├── search.py # Adaptive IDF-Weighted Convex Hybrid search + token_budget packing
│ ├── mcp_server.py # Zero-dependency MCP stdio + Streamable HTTP + REST + OpenAPI server
│ ├── rerank.py # Optional pure-NumPy cross-encoder (for ablations)
│ └── cli.py # `keyrag` CLI
├── bench/ # Reproducible BEIR runner + exact TREC metrics + zero-dep SVG charts
├── scripts/ # `fetch_papers.py` arXiv downloader
├── tests/ # 11 fast unit & integration tests
└── .github/workflows/ # GitHub Actions CI across Linux & macOS
```
---
## License
MIT — Built by **[KitiLab](https://github.com/KitiLab)** (`hello.kitilab@gmail.com`).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues