Skip to content
akagifreeezPublic

About

A small, honest RAG pipeline in Python: ingest -> chunk -> embed (offline TF-IDF or OpenAI) -> cosine retrieval -> Claude answer with citations, plus recall@k eval. Runs offline with no API keys.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

mini-rag

tests

A small, honest Retrieval-Augmented Generation pipeline in Python: ingest documents, chunk them, embed, retrieve the most relevant chunks by cosine similarity, and answer the question with Claude — citing the passages it used.

It runs offline with zero API keys (a local TF-IDF embedder + retrieval-only mode), and upgrades to semantic embeddings (OpenAI) + generation (Claude) by setting two environment variables. Same code path either way.

pip install -r requirements.txt

# Offline — no keys needed (local TF-IDF embedder, retrieval only):
python -m mini_rag.cli "What is the default rate limit?" --retrieve-only

# Full RAG — set ANTHROPIC_API_KEY (and optionally OPENAI_API_KEY for semantic embeddings):
python -m mini_rag.cli "How much does the Pro plan cost?"

# Retrieval quality on a labeled set:
python eval.py

# Unit tests (offline, no keys — just numpy + pytest):
pip install pytest && pytest

What it shows

The pieces a real RAG feature needs, each small and readable:

Stage File Notes
Ingest + chunk mini_rag/ingest.py Paragraph chunks, each prefixed with its document title.
Embed mini_rag/embed.py Two interchangeable backends behind one interface: offline TF-IDF, or OpenAI text-embedding-3-small.
Vector store mini_rag/store.py In-process numpy cosine search. Swap for FAISS / pgvector in production — same add / search interface.
Generate + cite mini_rag/generate.py Claude answers from the retrieved context and cites passages as [1], [2]; instructed to say "don't know" when the answer isn't in context.
Evaluate eval.py recall@k over a labeled question→source set — measuring retrieval before judging the generated text.
Tests tests/ 28 offline pytest cases: chunking, TF-IDF fit/normalization, cosine ranking, additive store, retrieval determinism, a recall@3 regression, and the prompt-injection contract. Run in CI on Python 3.10 & 3.12.
question ──▶ embed ──▶ cosine search ──▶ top-k chunks ──▶ Claude (cite [n]) ──▶ answer
                ▲                                              
          corpus embedded once at index time                  

Provider integration

The job-shaped point: production RAG usually wires more than one AI provider — embeddings from one, generation from another. Here that's OpenAI embeddings + Claude generation, selected at runtime by which keys are present (mini_rag/embed.py → make_embedder()). Switching the embedder is the only change to go from offline demo to semantic retrieval.

Honest scope

This is a small published implementation, not a production-operated system. The corpus is a handful of sample docs, the vector store is in-memory, and there's no incremental indexing, auth, or serving layer. It demonstrates that I can build and reason about a RAG pipeline end-to-end (chunking, embeddings, retrieval, grounded generation with citations, and retrieval evaluation); operating one at scale (managed vector DB, ingestion pipelines, monitoring) is the next step.

The retrieved passages are passed to the model as untrusted data — the system prompt tells it never to follow instructions found inside them (a prompt-level mitigation of indirect prompt injection, a reduction rather than a guarantee). The logic is covered by an offline pytest suite that runs in CI; the tests don't call any paid API.

Requirements

  • Python 3.10+
  • numpy (always). anthropic only for answer generation; openai only for semantic embeddings — both optional.

Built by @akagifreeez as a focused study of RAG. See also hl-read (MCP server) and agent-docsmith (Claude Agent SDK agent).

About

A small, honest RAG pipeline in Python: ingest -> chunk -> embed (offline TF-IDF or OpenAI) -> cosine retrieval -> Claude answer with citations, plus recall@k eval. Runs offline with no API keys.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages