Command Palette

Search for a command to run...

Hectal
Phase 8: Retrieval Basics (RAG)
LLM Engineering · Exercise 6 of 6 Previous
Advanced

Answer with guards

Write answer(question, retrieve, model, min_score=0.3). retrieve(question) returns a list of (score, text) pairs, best first. Keep only the texts whose score is at least min_score.

If none are left, return REFUSAL without calling the model. Otherwise build the prompt with the given build_prompt(question, texts) and call model(prompt). If check_reply(reply, len(texts)) is neither "ok" nor "refusal", return REFUSAL; otherwise return the reply.

What Check tests

  1. A good reply is returned unchanged
  2. The model sees numbered sources and the question
  3. No evidence: refuse and never call the model
  4. Weak hits are dropped, strong ones kept
  5. A reply without a citation is replaced by the refusal
  6. A citation to a missing source is replaced by the refusal
  7. A refusal from the model is passed through

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.