Command Palette

Search for a command to run...

Hectal
Phase 6: Hybrid Search and Re-ranking
RAG and Vector Search · Exercise 3 of 5 PreviousNext
Intermediate

Re-rank a shortlist and survive a failing re-ranker

Write rerank_safe(question, candidates, scorer, final_k). candidates is a list of ids in first-stage order, and scorer(question, candidate) returns a number (higher is better).

Return the best final_k candidates ordered by score, best first. Candidates with equal scores keep their first-stage order. If scorer raises any exception for any candidate, return candidates[:final_k] unchanged.

What Check tests

  1. Candidates are ordered by score
  2. Equal scores keep the first-stage order
  3. A failing scorer falls back to the first-stage order
  4. final_k larger than the list returns everything
  5. The scorer receives the question

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.