Skip to content

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.py and test_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.

  1. Add two settings at the top of rag.py: RRF_K and VECTOR_WEIGHT.
  2. Add an optional weights parameter to rrf(): rrf(rankings, k=RRF_K, weights=None). Each list votes weight / (k + rank), with rank starting at 1. No weights: every list has weight 1, exactly as today.
  3. weights of a different length than rankings raises ValueError.
  4. hybrid_search() gets a vector_weight=VECTOR_WEIGHT parameter and passes [vector_weight, 1] as the weights (the vector list first, the keyword list second).
  5. Decide: the value of VECTOR_WEIGHT.
  6. Decide: the value of RRF_K (60 is the usual default; a smaller k gives the top ranks more say).
  7. 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.

  1. answer() acts on check_citations() before returning, whatever check is. Keep check=True printing as it does.
  2. An answer that is exactly NO_ANSWER, or has no problems, is returned as it is.
  3. Decide: an answer that cites a file that was not retrieved. Either return NO_ANSWER, or return the answer with a last line Warning: cites documents that were not retrieved: <files> (the unknown files, comma separated).
  4. Decide: an answer with no citation at all. Either return NO_ANSWER, or return it as it is.
  5. Leave cited() and check_citations() working as they do (the existing tests check them).
  6. Add model-free tests to test_rag.py (replace door.chat with 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.

  1. Create my-work/rag_eval.py with a list GOLDEN of at least 15 cases: {"q": "Restocking fee on 600 bolts?", "expect": ("returns-policy.md", "Bulk orders")}. expect is 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.
  2. Every expect must exist: a file in docs/ and a section heading in it, spelled exactly.
  3. For each case, run vector, keyword and hybrid search (with tier_filter when the case has a tier) and print hit@3 and MRR per method. Put the run under if __name__ == "__main__": so importing the file does not call the model.
  4. Decide: which hard cases your set must include (see ASSIGNMENTS.md).
  5. Change one thing (chunk size, tokens(), VECTOR_WEIGHT, RRF_K, ...), run again, and keep the change only if it helped.
  6. 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/build passes with model-free tests (filter, citation check, refusal path) using a fake chat.
  • 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.