Back to Practice

Retrieve-wide then rerank-narrow

Completion

Exercise: Retrieve-wide then Rerank-narrow

In this exercise, you will implement a function that demonstrates the retrieve-wide then rerank-narrow pattern used in the LlamaIndex + Qdrant grounding pipeline. You will:

  1. Retrieve a wide candidate set of nodes (e.g., top_k=20) from a retriever.
  2. Rerank those nodes using a FastEmbedRerank cross-encoder node-postprocessor, keeping only the top_n=6 most relevant nodes.
  3. Explain in a comment why a cross-encoder (which reads each (query, chunk) pair together) ranks better than the bi-encoder used for the initial retrieval.

Provided

  • A retriever object (already configured with similarity_top_k=20).
  • The build_reranker helper from the repository (from .rerank import build_reranker).
  • The QueryBundle class from llama_index.core.schema.

Your Task

Fill in the # TODO: gaps in the starter code below. The function should:

  • Use the retriever to get a list of NodeWithScore objects.
  • Build a reranker (with top_n=6) using build_reranker.
  • Apply the reranker via postprocess_nodes, passing the nodes and a QueryBundle created from the query string.
  • Return the reranked nodes.
  • Add an inline comment after the rerank call explaining why a cross-encoder is more precise for ranking than a bi-encoder.
Your code
Sources
  • roadmap-kg/kg/rerank.py:40-70
  • roadmap-kg/kg/rerank.py:1-37
  • roadmap-kg/kg/rerank.py:67-106