Class HybridRAGStrategy

java.lang.Object
com.tnsai.intelligence.rag.HybridRAGStrategy
All Implemented Interfaces:
RAGStrategy

public final class HybridRAGStrategy extends Object implements RAGStrategy
RAG strategy that fuses BM25, vector similarity, and — when a GraphRAGStrategy is supplied — knowledge-graph hits via Reciprocal Rank Fusion (RRF).

The strategy name stays hybrid. A missing graph capability does not degrade to VECTOR-only: BM25 and vector still fuse, and graphStream() reports HybridGraphStream.SKIPPED_NO_GRAPH_STORE. Unweighted RRF uses ReciprocalRankFusion.DEFAULT_K (60).

Two-stream construction still delegates to HybridMemoryRetriever so existing lexical/semantic scores stay unchanged. The three-stream constructor ranks each source independently, then fuses the lists. Scores are normalized to 0.0..1.0 against the number of non-empty streams before RAGContext.minScore() applies.

Since:
0.2.4
  • Field Details

  • Constructor Details

    • HybridRAGStrategy

      public HybridRAGStrategy(com.tnsai.memory.advanced.BM25Index bm25Index, com.tnsai.memory.advanced.VectorMemoryStore vectorStore)
      Creates a two-stream hybrid strategy from BM25 and vector stores.

      Uses the default RRF constant (k=60). The graph stream is skipped with HybridGraphStream.SKIPPED_NO_GRAPH_STORE.

      Parameters:
      bm25Index - the BM25 keyword index
      vectorStore - the vector similarity store
    • HybridRAGStrategy

      public HybridRAGStrategy(com.tnsai.memory.advanced.HybridMemoryRetriever hybridRetriever)
      Creates a two-stream hybrid strategy with a pre-configured retriever.
      Parameters:
      hybridRetriever - the hybrid retriever to use
    • HybridRAGStrategy

      public HybridRAGStrategy(com.tnsai.memory.advanced.BM25Index bm25Index, com.tnsai.memory.advanced.VectorMemoryStore vectorStore, GraphRAGStrategy graphStrategy)
      Creates a three-stream hybrid strategy that RRF-fuses BM25, vector, and graph rankings.
      Parameters:
      bm25Index - the BM25 keyword index
      vectorStore - the vector similarity store
      graphStrategy - source-fenced graph traversal used as the third list
      Since:
      0.14.0
  • Method Details

    • graphStream

      public HybridGraphStream graphStream()
      How the optional graph stream was handled for this instance.
      Returns:
      fused or skipped-without-store
      Since:
      0.14.0
    • name

      public String name()
      Description copied from interface: RAGStrategy
      Returns the unique name of this strategy (e.g., "vector", "keyword", "hybrid").

      Names are compared case-insensitively using Locale.ROOT.

      Specified by:
      name in interface RAGStrategy
      Returns:
      the strategy name, never null
    • retrieve

      public List<RetrievedDocument> retrieve(String query, RAGContext context)
      Description copied from interface: RAGStrategy
      Retrieves documents relevant to the given query.

      Every returned document must exactly match all metadata filters in the context. Implementations apply that constraint before limiting the result to RAGContext.maxResults().

      Specified by:
      retrieve in interface RAGStrategy
      Parameters:
      query - the search query (never null)
      context - retrieval parameters (max results, min score, exact metadata filters)
      Returns:
      an unmodifiable list of retrieved documents, sorted by descending relevance