Practice: RAG¶
Three exercises that build on the labs, then one open build task. Do them in your own copy (a fork) of this repo, commit as you go, and keep short notes.
Read the labs first: rag.py, 05_hybrid_search.py, 06_metadata_filters.py, 09_evaluation.py.
Where your work goes:
- Code changes: in
rag.pyandtest_rag.py. - Your own files:
my-work/in this folder. - Notes:
my-work/rag.md. For each exercise, a few lines on what you changed, why, and what you would do differently in production.
Some rules below say Decide. Make the choice yourself and write it in your notes.
If your team gave you an ASSIGNMENTS.md, it makes those choices for you: follow it.
ex1: Weighted fusion¶
Why: in 09_evaluation.py, hybrid search did worse than vector search alone. Keyword search returns junk for some
questions, and RRF gives that junk the same vote as the vector results.
- Add two settings at the top of
rag.py:RRF_KandVECTOR_WEIGHT. - Add an optional
weightsparameter torrf():rrf(rankings, k=RRF_K, weights=None). Each list votesweight / (k + rank), with rank starting at 1. No weights: every list has weight 1, exactly as today. weightsof a different length thanrankingsraisesValueError.hybrid_search()gets avector_weight=VECTOR_WEIGHTparameter and passes[vector_weight, 1]as the weights (the vector list first, the keyword list second).- Decide: the value of
VECTOR_WEIGHT. - Decide: the value of
RRF_K(60 is the usual default; a smaller k gives the top ranks more say). - Add model-free tests to
test_rag.py.
Done when pytest -q passes, including your new tests.
ex2: Act on the citation check¶
Why: the prompt asks the model to cite its sources, and rag.py already finds what is wrong with the citations:
check_citations() returns [], ["no citation"], or one problem per cited file that was never retrieved.
But answer() only prints those problems (with check=True) and returns the answer anyway. A citation to a file
that was never retrieved is made up, and so may be the fact next to it.
answer()acts oncheck_citations()before returning, whatevercheckis. Keepcheck=Trueprinting as it does.- An answer that is exactly
NO_ANSWER, or has no problems, is returned as it is. - Decide: an answer that cites a file that was not retrieved. Either return
NO_ANSWER, or return the answer with a last lineWarning: cites documents that were not retrieved: <files>(the unknown files, comma separated). - Decide: an answer with no citation at all. Either return
NO_ANSWER, or return it as it is. - Leave
cited()andcheck_citations()working as they do (the existing tests check them). - Add model-free tests to
test_rag.py(replacedoor.chatwith a fake, as the existing tests do).
Done when pytest -q passes, including your new tests.
ex3: Golden set and measurement¶
Why: you only know a change helped if you measure it, and 12 questions is a smoke test, not a benchmark.
- Create
my-work/rag_eval.pywith a listGOLDENof at least 15 cases:{"q": "Restocking fee on 600 bolts?", "expect": ("returns-policy.md", "Bulk orders")}.expectis the file and##section that holds the answer. Add"tier": "gold"or"tier": "silver"when the question is asked for a customer of that tier. - Every
expectmust exist: a file indocs/and a section heading in it, spelled exactly. - For each case, run vector, keyword and hybrid search (with
tier_filterwhen the case has a tier) and print hit@3 and MRR per method. Put the run underif __name__ == "__main__":so importing the file does not call the model. - Decide: which hard cases your set must include (see ASSIGNMENTS.md).
- Change one thing (chunk size,
tokens(),VECTOR_WEIGHT,RRF_K, ...), run again, and keep the change only if it helped. - In your notes: hit@3 (as a percentage) and MRR before and after, the one thing you changed, and two misses.
Done when the script runs and your notes have both sets of numbers.
Names these exercises build on¶
Please keep these names in rag.py as they are: rrf, hybrid_search, vector_search, keyword_search,
answer, generate, chat, NO_ANSWER, Hit, load_docs, chunk_by_heading, tier_filter, reciprocal_rank.
Checks and later topics rely on them.
Build it yourself: seller agreement assistant¶
No starter code and no steps this time: a short brief, like one you'd get at work. Plan for half a day.
Your code goes in my-work/build/; reuse rag.py (load_docs(), chunk_by_heading(), ingest() with your own collection and folder) rather than copying it. Notes go in a "Build" section of my-work/rag.md.
The brief. Sellers on the marketplace keep asking the ops team about their agreement: the payment cycle, commission per category, penalties for late dispatch, return rules. Build a Q&A assistant over the seller policy documents.
It must:
- Answer from at least 6 short Markdown documents that you write yourself in
my-work/build/docs/(fake policies), with##sections and front matter for the seller's tier or region. - Use its own vector collection, so it never mixes with the lab's documents.
- Cite
[file.md]in every answer, with citations checked in code (your ex2 code helps). - Refuse when the documents don't cover the question, and for live-data questions ("what is my payout this week?").
- Filter by the seller's tier or region in the search, not in the prompt.
- Have a golden set of at least 15 questions, including 3 the documents can't answer and 2 in Hinglish.
Decide: the chunk size. What happens when two documents disagree, for example an old and a new commission table?
Done when:
pytest -q my-work/buildpasses with model-free tests (filter, citation check, refusal path) using a fakechat.- A demo answers five questions on qwen3:8b, with citations, including one refusal.
- Your notes have hit@3, MRR and the refusal rate on the unanswerable questions, before and after one change.
Or bring your own problem. Build the same pattern on a problem from your own work instead. Write a five-line brief first (who uses it, what goes in, what comes out, what must never go wrong, how you'll measure it), then meet the same "Done when" list. Use synthetic data only, never real customer data.