Back to Practice

Retrieve-wide then rerank-narrow

Full

Implement a Python function that takes a query string and a list of NodeWithScore objects (the wide candidate set from a bi-encoder), builds a FastEmbedRerank cross‑encoder node‑postprocessor using the build_reranker factory, applies it to the nodes to re‑rank by true relevance (reading each query–chunk pair together), and returns the top‑n most relevant nodes. Your function must:

  • Use exactly the symbols from the provided repository source: build_reranker (from .rerank), FastEmbedRerank, postprocess_nodes, QueryBundle (from llama_index.core.schema).
  • Gracefully fall back to plain top‑n slicing if build_reranker returns None.
  • Include a docstring that explains why a cross‑encoder gives better reranking quality than the bi‑encoder used for first‑pass retrieval (mention pairing query and chunk, higher precision, etc.).

Write only the function — no class, no main block.

Your code
Sources
  • roadmap-kg/kg/rerank.py:40-70
  • roadmap-kg/kg/rerank.py:1-37
  • roadmap-kg/kg/rerank.py:67-106