Skip to content

< all problems37 · Level 02, Search

Rerank the Retrieved Chunks

medium · implement · RAG

Embedding search returns candidates that are approximately right. A reranker puts the best ones first.

Implement rerank(llm, question, chunks, k=3) returning the k chunks most relevant to the question, best first.

Ask the model to rank the chunks in one call: number them and ask for the numbers back in order of relevance. Do not score each chunk in its own call: ten chunks is ten calls the slow way and one the fast way, and small models are erratic at calibrated scores but reliable at saying which passage is best.

Then handle the reply:

  1. Parse leniently. The model will return 4, 2, 1, 3 or [4, 2] or a sentence with numbers in it. Pull the numbers out in the order they appear; do not insist on one shape.
  2. Fill the gaps. If the model names only some passages, the rest keep their original relative order after them.
  3. Ignore what you were not given. A number that is not a valid passage index (a hallucinated 9 in a list of four) is skipped, not indexed. A repeated number counts once.
  4. Fall back to the original order when nothing usable comes back. Never throw on a bad reply.

The tests put the relevant chunk last in the input and check it comes out first.