Class HybridGraphRetriever<T>
- Namespace
- AiDotNet.RetrievalAugmentedGeneration.Graph
- Assembly
- AiDotNet.dll
Hybrid retriever that combines vector similarity search with graph traversal for enhanced RAG.
public class HybridGraphRetriever<T>
Type Parameters
TThe numeric type used for vector operations.
- Inheritance
-
HybridGraphRetriever<T>
- Inherited Members
Remarks
This retriever uses a two-stage approach: 1. Vector similarity search to find initial candidate nodes 2. Graph traversal to expand context with related nodes
For Beginners: Traditional RAG uses only vector similarity:
Query: "What is photosynthesis?" Traditional RAG:
- Find documents similar to the query
- Return top-k matches
- Misses related context!
Hybrid Graph RAG:
- Find initial matches via vector similarity
- Walk the graph to find related concepts
- Example: photosynthesis → chlorophyll → plants → carbon dioxide
- Provides richer, more complete context
Real-world analogy:
- Traditional: Search "Paris" → get Paris documents
- Hybrid: Search "Paris" → get Paris + France + Eiffel Tower + Seine River
- Graph connections provide context vectors can't capture!
Constructors
HybridGraphRetriever(KnowledgeGraph<T>, IDocumentStore<T>)
Initializes a new instance of the HybridGraphRetriever<T> class.
public HybridGraphRetriever(KnowledgeGraph<T> graph, IDocumentStore<T> documentStore)
Parameters
graphKnowledgeGraph<T>The knowledge graph containing entity relationships.
documentStoreIDocumentStore<T>The document store for similarity search.
Methods
Retrieve(Vector<T>, int, int, int)
Retrieves relevant nodes using hybrid vector + graph approach.
public List<RetrievalResult<T>> Retrieve(Vector<T> queryEmbedding, int topK = 5, int expansionDepth = 1, int maxResults = 10)
Parameters
queryEmbeddingVector<T>The query embedding vector.
topKintNumber of initial candidates to retrieve via vector search.
expansionDepthintHow many hops to traverse in the graph (0 = no expansion).
maxResultsintMaximum total results to return after expansion.
Returns
- List<RetrievalResult<T>>
List of retrieved nodes with their relevance scores.
RetrieveAsync(Vector<T>, int, int, int)
Retrieves relevant nodes asynchronously using hybrid approach.
public Task<List<RetrievalResult<T>>> RetrieveAsync(Vector<T> queryEmbedding, int topK = 5, int expansionDepth = 1, int maxResults = 10)
Parameters
Returns
- Task<List<RetrievalResult<T>>>
RetrieveWithRelationships(Vector<T>, int, Dictionary<string, double>?, int)
Retrieves nodes with relationship-aware scoring.
public List<RetrievalResult<T>> RetrieveWithRelationships(Vector<T> queryEmbedding, int topK = 5, Dictionary<string, double>? relationshipWeights = null, int maxResults = 10)
Parameters
queryEmbeddingVector<T>The query embedding vector.
topKintNumber of initial candidates.
relationshipWeightsDictionary<string, double>Weights for different relationship types.
maxResultsintMaximum results to return.
Returns
- List<RetrievalResult<T>>
List of retrieved nodes with relationship-aware scores.