An agentic retrieval-augmented generation engine built from first principles. BPE tokenization, BM25, SVD-based dense embeddings, a learned reranker, a plan → retrieve → reflect loop, grounded answers with citations, and a reproducible evaluation harness — with NumPy as the only runtime dependency.
By S M Nazmuz Sakib. Part of the AI Security Lab family.
No PyTorch, no transformers, no scikit-learn, no embedding API, no API key. Every learned component is trained on the bundled corpus at build time and is small enough to read end-to-end.
Most RAG tutorials wire a vector database to an LLM API. Atlas RAG implements each piece so the behaviour is inspectable:
| Stage | What is actually implemented |
|---|---|
| Tokenization | Byte-pair encoding trained from scratch with deterministic merge selection |
| Lexical retrieval | Okapi BM25 with tunable k1 saturation and b length normalization |
| Dense retrieval | TF-IDF + truncated SVD (LSA) learned from the corpus — no pretrained embeddings |
| Fusion | Reciprocal rank fusion over the lexical and dense rankings |
| Reranking | Logistic regression trained with gradient descent on corpus-generated positive/negative pairs |
| Agent | Softmax router (learned), retrieval, evidence-coverage reflection, query expansion via term co-occurrence |
| Answering | Extractive grounded sentences with [doc-id] citations and abstention when evidence is insufficient |
| Evaluation | Recall@k, MRR, nDCG@k, grounded accuracy, answer accuracy, abstention precision/recall |
graph LR
Q[Question] --> T[BPE tokenizer]
T --> B[BM25]
T --> D[TF-IDF + SVD]
B --> F[Reciprocal rank fusion]
D --> F
F --> R[Logistic reranker]
R --> A[Agent: plan / reflect / refine]
A --> G[Grounded answer + citations]
G --> E[Evaluation harness]
pip install -e .Requires Python 3.10+ and NumPy. That is the entire dependency list.
# Build a persistent index from the bundled 26-document corpus
atlas-rag build --out atlas-index
# Lexical, dense, hybrid or learned retrieval
atlas-rag query "How does BM25 rank documents?" --index atlas-index --mode hybrid
# Agentic grounded answer with the decision trace
atlas-rag query "How does reciprocal rank fusion work?" --mode agent --show-trace
# Reproducible benchmark across all four strategies
atlas-rag compare
# JSON for pipelines
atlas-rag query "What is Laplace smoothing?" --mode agent --jsonfrom atlas_rag import RAGIndex, QueryAgent
from atlas_rag.data import load_corpus
index = RAGIndex.build(load_corpus())
answer = QueryAgent(index).run("What does the k1 parameter control in BM25?")
print(answer.text)
print(answer.citations, answer.confidence, answer.abstained)
for step in answer.trace:
print(step.kind, step.detail)Run with atlas-rag compare (deterministic seed, 20 questions, 3 out-of-domain
questions for abstention). These are the actual numbers from this repository:
| Strategy | R@3 | R@5 | MRR | nDCG@5 | Grounded | Accurate | Abstain P | Abstain R |
|---|---|---|---|---|---|---|---|---|
| bm25 | 0.900 | 0.900 | 0.900 | 0.900 | — | — | — | — |
| dense | 0.975 | 1.000 | 0.975 | 0.971 | — | — | — | — |
| hybrid | 0.900 | 1.000 | 0.925 | 0.935 | — | — | — | — |
| agent | 0.900 | 0.900 | 0.900 | 0.892 | 0.900 | 0.800 | 1.000 | 0.900 |
How to read this: on a small corpus, SVD/LSA dense retrieval is the strongest pure ranker — it beats BM25 on recall and MRR. The agent deliberately trades a little ranking quality for grounding: it answers only when content-word coverage and reranker confidence clear a threshold, and it abstains on all three out-of-domain questions while still answering 9 of 10 in-domain ones. A system that never answers is trivially precise; the pair of abstention numbers is what makes the tradeoff visible.
Coverage is computed over content words (stopwords and punctuation removed), IDF-weighted, with a floor weight for unknown words. This matters: measuring coverage over BPE subwords makes almost every question look "covered" because common subwords appear everywhere. Out-of-domain questions score near zero coverage and are refused with a short explanation instead of a fabricated answer.
- The bundled corpus is a 26-document teaching corpus, not a production knowledge base.
- LSA is a linear latent model; it cannot capture the semantic richness of neural embeddings.
- The reranker and router are trained on synthetic queries derived from the same corpus — they are not general-purpose models.
- Answers are extractive: the system quotes evidence sentences rather than generating novel prose. This is a design choice for faithfulness, not a missing feature.
- Evaluation on 20 questions gives directional, not statistically strong, results.
See SECURITY.md for scope and safe-use notes.
pip install -e ".[dev]"
pytest65 tests cover BPE determinism and round-trips, BM25 parameters, SVD shapes, rank fusion, logistic training and persistence, index save/load equivalence, agent planning/reflection/abstention, all IR metrics and the CLI.
atlas-rag/
├── atlas_rag/
│ ├── tokenizer.py BPE + content words
│ ├── bm25.py lexical retriever
│ ├── lsa.py TF-IDF + SVD embeddings
│ ├── fusion.py reciprocal rank fusion
│ ├── rerank.py learned logistic ranker
│ ├── index.py build / search / persistence
│ ├── agent.py router, reflection, grounded answers
│ ├── evaluate.py metrics and benchmark runner
│ └── cli.py command-line interface
├── data/ bundled corpus and 20 benchmark questions
├── tests/ 65 tests
└── examples/quickstart.py
MIT © S M Nazmuz Sakib (2026).