Mini RAG End to End

The Whole Pipeline in One Program

Example

Chunk → embed → index → retrieve → prompt → answer. fake_llm is a stand-in for the model: it answers with the context sentence that best overlaps the question, cites it, and says "I don't know" when nothing was retrieved. Everything else is what a real RAG system does.

import hashlib, math, re

DOCS = {
    "returns.md": "Returns are accepted within 7 days of delivery. Items must be unused with tags.",
    "refunds.md": "Refunds reach the original payment method in 5-7 working days. COD refunds go to your bank account.",
    "delivery.md": "Orders above Rs 499 ship free. Metro deliveries take 2-3 days, other cities 4-6 days.",
}
STOP = {"the", "is", "a", "my", "i", "to", "of", "in", "do", "how", "what", "are", "will", "it",
        "for", "with", "your", "you", "can", "many", "days", "be", "go"}

def words(t): return [w for w in re.findall(r"[a-z0-9]+", t.lower()) if w not in STOP]
def embed(t, dim=128):
    v = [0.0] * dim
    for w in words(t):
        h = int(hashlib.md5(w.encode()).hexdigest(), 16); v[h % dim] += 1.0
    n = math.sqrt(sum(x * x for x in v)) or 1.0
    return [x / n for x in v]

index = []                                                # 1) chunk (by sentence) + embed
for src, text in DOCS.items():
    for sent in re.split(r"(?<=\.)\s+", text):
        index.append({"id": len(index) + 1, "src": src, "text": sent, "vec": embed(sent)})

def retrieve(q, k=2, min_score=0.3):                      # 2) retrieve
    qv = embed(q)
    hits = sorted(((sum(a * b for a, b in zip(qv, c["vec"])), c["id"], c) for c in index), key=lambda x: (-x[0], x[1]))
    return [c for s, _, c in hits[:k] if s >= min_score]

def fake_llm(question, chunks):                           # 3) stand-in for the model
    if not chunks:
        return "I don't know. Let me connect you to a human agent."
    best = max(chunks, key=lambda c: len(set(words(question)) & set(words(c["text"]))))
    return f"{best['text']} [{best['id']}]"

for q in ["How many days for a COD refund?", "Do orders ship free?", "Can I pay with crypto?"]:
    chunks = retrieve(q)
    print("Q:", q)
    print("   retrieved:", [(c["id"], c["src"]) for c in chunks])
    print("   A:", fake_llm(q, chunks))

Output

Q: How many days for a COD refund?
   retrieved: [(4, 'refunds.md')]
   A: COD refunds go to your bank account. [4]
Q: Do orders ship free?
   retrieved: [(5, 'delivery.md')]
   A: Orders above Rs 499 ship free. [5]
Q: Can I pay with crypto?
   retrieved: []
   A: I don't know. Let me connect you to a human agent.

Swap in Real Parts

Toy partReal replacement
sentence splitheading/size-based chunker with overlap
hashed embed()Voyage AI or sentence-transformers
Python list indexChroma, Qdrant, pgvector
fake_llmclient.messages.create(...) with the RAG prompt