Find the closest chunk of your docs, put it in the prompt and answer with the source. "New hires get 15 vacation days a year," from hr-1, score 0.91.
python3 --version.git clone https://github.com/DayanEbrar0X/data-anatomy.ai.git cd data-anatomy.ai
python3 -m venv .venv source .venv/bin/activate
cd ai-engineering/06-rag python3 src/rag.py
from docs import DOCS, embed, cosine
ask = "How many vacation days do new hires get?"
q = embed(ask)
scores = {d: cosine(q, embed(t))
for d, t in DOCS.items()}
best = max(scores, key=scores.get)
prompt = f"{DOCS[best]}\n\nQuestion: {ask}"
print(DOCS[best])
print("source:", best, round(scores[best], 2))Your company's AI never read your HR handbook. It looks things up first. That's RAG. Take eight handbook chunks.
Embed the question. Real systems use a learned model. Score every chunk against it with cosine. Keep the closest and paste it into the prompt.
Print the match. Run it. New hires get 15 vacation days. Source: hr-1.
The model answers from that, and cites it. Look it up, then answer. That's RAG.
Read the lesson on GitHub →