Back to Practice

store_nodes() for BM25 hybrid

Full

Write a Python function build_hybrid_retriever(dense_retriever, index) that returns a fused retriever combining the given dense retriever with a BM25 sparse retriever built over the full corpus. The corpus nodes must be obtained by calling store_nodes(index) (which handles the case where index.docstore.docs is empty on Qdrant by scrolling nodes from the vector store). If store_nodes returns an empty list, return the dense_retriever unchanged.

  • Use BM25Retriever.from_defaults(nodes=..., similarity_top_k=20) for the sparse arm.
  • Use QueryFusionRetriever with mode "reciprocal_rerank", similarity_top_k=20, num_queries=1, and set llm=None to avoid LLM calls.
  • Import the necessary classes from llama_index.core.retrievers and llama_index.retrievers.bm25.
  • Return the fused retriever.

Assume all required dependencies are installed and the function does not need error handling beyond the empty corpus check.

Your code
Sources
  • roadmap-kg/kg/rerank.py:188-225
  • roadmap-kg/kg/rerank.py:151-186
  • roadmap-kg/kg/memory_common.py:975-990