Retrieve-wide then rerank-narrow
FullImplement 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(fromllama_index.core.schema). - Gracefully fall back to plain top‑
nslicing ifbuild_rerankerreturnsNone. - 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