Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 0.8 — Playbook: Is RAG the Right Tool?
RAG and Vector Search · Example 2 of 2

Recipe: paste, retrieve or fine-tune?

The window size and tokens per page are example values; use your model's and your documents'.

Two recipes to start any RAG project with: a decision function that says whether to paste, retrieve or fine-tune, and a baseline harness that measures how often your retriever finds the right passage. Plus where a real embedding call plugs in.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write hit_rate(results, expected, k) where results is a list of ranked id lists and expected a list of ids. Print the hit rate at 1 and at 2 for three made-up questions.

02

Extend recommend with a parameter access_controlled that adds the reason "documents have per-user permissions" and therefore recommends RAG. Print the decision for a 4-page document with permissions.

Explain it without notes

01

What does the baseline harness measure, and why is it written before improving retrieval?

02

What is the difference between hit rate at 1 and hit rate at k, and which matters more for RAG?

03

Why should recommend return reasons as well as a decision?

Recipe: paste, retrieve or fine-tune?
Sign in to run this example in your browser.

Expected output

One 4-page policy, stable
  -> paste the documents into the prompt (small, stable, and no sources needed)
The 60-page handbook, edited weekly, sources required
  -> use RAG (the documents change often; answers must point to their source)
A 2000-page help centre
  -> use RAG (the corpus is about 1000000 tokens, more than half the context window)
Replies are correct but too formal
  -> improve the prompt, then consider fine-tuning (the problem is how the model answers, not what it knows)