Command Palette

Search for a command to run...

Hectal
Phase 8: Generation with Sources
RAG and Vector Search · Exercise 3 of 5 PreviousNext
Intermediate

Refuse before the model is called

build_prompt(question, hits) is given. hits is a list of (doc_id, score, text) triples, best first. model is a function that takes the prompt and returns a string.

Write answer(question, hits, model, threshold=0.40). Keep only the hits whose score is at least the threshold. If none remain, return REFUSAL and do not call the model. Otherwise return model(build_prompt(question, kept)).

What Check tests

  1. Scores below the threshold are refused
  2. An empty hit list is refused
  3. A good hit calls the model once and returns its reply
  4. Only hits above the threshold reach the prompt
  5. A score exactly at the threshold is answered

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.