Skip to main content
Hybrid RAG retrieval combines searches from an original question embedding and a reformulated question embedding to retrieve more comprehensive context for Retrieval-Augmented Generation. A single query embedding may miss relevant documents that a reformulated version of the question would surface. Fusing both result sets captures relevant context from multiple perspectives. This code example defines a reusable retrieval function that generates embeddings for both the original and reformulated question, searches with each embedding, and fuses the results using RRF. It then builds a context string from the retrieved documents for use in an LLM prompt.
In production, use an actual embedding model to generate question embeddings and an LLM to reformulate the user’s question. The example below uses random vectors as placeholders to demonstrate the retrieval and fusion pattern.
The hybrid RAG retrieval function (hybrid_rag_retrieval in Python, hybridRagRetrieval in JavaScript) returns a list of fused results, each containing:
  • id: The unique identifier of the matching point
  • score: Fused score from RRF across both query searches
  • payload: Metadata object containing the document text and any additional fields
Hybrid RAG retrieval improves context quality by:
  • Capturing relevant documents that a single query might miss
  • Combining signals from both the original and reformulated question
  • Providing more diverse context to the LLM for answer generation