Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 8.9 — Playbook: Grounded Answer Recipes
RAG and Vector Search · Example 1 of 1

Five grounded-answer recipes

Production-shaped but small. Every printed line is a unit test waiting to be written.

Five small, reusable pieces of the answer path: a prompt renderer, a context packer, a refusal gate, a citation verifier and a number checker, plus the one place a real chat 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 should_answer(scores, threshold) returning True only if the list is non-empty and its maximum is at least the threshold. Print the result for [], [0.38] and [0.47, 0.1] with threshold 0.40.

02

Write verify(answer, n_sources) returning a list of problems (an out-of-range marker gives citation N is not a source, no marker gives no citation). Print the problems for "Cost 25 [1] [2]" with 1 source and for "Cost 25" with 1 source.

Explain it without notes

01

Which recipe protects against which failure?

02

Why does the gate run before the model call?

03

How would you test these recipes?

04

What is the minimum you would log for every answer?

Five grounded-answer recipes
Sign in to run this example in your browser.

Expected output

gate 0.47: answer
gate 0.38: refuse
gate with no hits: refuse
packed with budget 70: 2 of 3 passages
[1] The monthly pass costs 25 dollars and includes unlimited rides of up to 45 minutes.
[2] Bikes returned late are charged 2 dollars for every 15 minutes after the booked end time.
verify '... [1]': ok
verify '... [3]': citation 3 is not a source
verify no marker: no citation
numbers missing: 30