Retrieve-wide then rerank-narrow
CompletionExercise: 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:
- Retrieve a wide candidate set of nodes (e.g., top_k=20) from a retriever.
- Rerank those nodes using a FastEmbedRerank cross-encoder node-postprocessor, keeping only the top_n=6 most relevant nodes.
- 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_rerankerhelper from the repository (from .rerank import build_reranker). - The
QueryBundleclass fromllama_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
NodeWithScoreobjects. - Build a reranker (with
top_n=6) usingbuild_reranker. - Apply the reranker via
postprocess_nodes, passing the nodes and aQueryBundlecreated 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