GraphRetriever<T>

Models & Types · AiDotNet.RetrievalAugmentedGeneration.Retrievers

Retrieves documents by identifying entities and their relationships in knowledge graph structures.

Inheritance: RetrieverBase<T> → GraphRetriever<T>

For Beginners

Think of this like a detective searching a case file:

A regular search finds documents by overall similarity. GraphRetriever is smarter—it:

  1. Identifies key people, places, and things in your question (entities)
  2. Looks for documents mentioning those entities
  3. Gives extra points when entities appear close together (showing relationships)

For example, asking “How does John know Mary?” works better than “John Mary relationship” because:

  • It finds documents mentioning both John AND Mary
  • It prioritizes documents where John and Mary appear near each other
  • It understands you’re looking for connections, not just mentions
var myDocStore = new InMemoryDocumentStore<double>(vectorDimension: 768);
var mySentenceTransformer = new StubEmbeddingModel<double>(embeddingDimension: 768);
var content = "Albert Einstein's work on the photoelectric effect helped found quantum physics.";
myDocStore.Add(new VectorDocument<double>(
new Document<double>("einstein-quantum", content),
mySentenceTransformer.Embed(content)));

var graphRetriever = new GraphRetriever<double>(
documentStore: myDocStore,
embeddingModel: mySentenceTransformer,
enableAdvancedEntityExtraction: true
);

var results = graphRetriever.Retrieve("How does Einstein relate to quantum physics?", topK: 5);
// Finds documents with both "Einstein" and "quantum physics" appearing together

Why use GraphRetriever:

  • Better at finding connected facts (multi-hop questions)
  • Understands relationships between entities
  • Ideal for knowledge-intensive queries (science, history, technical domains)
  • Works well with structured or semi-structured data

When NOT to use it:

  • Simple factual lookups (basic vector search is faster)
  • Queries without clear entities (abstract concepts, opinions)
  • Documents lacking entity mentions or relationships

How It Works

GraphRetriever enhances traditional vector search by leveraging entity recognition and relationship scoring. It extracts entities from the query (proper nouns, quoted terms, numbers), then boosts documents that contain these entities and demonstrate relationships between them (co-occurrence within proximity). This approach is particularly effective for queries requiring structured information or multi-hop reasoning across connected facts. The retriever uses a hybrid scoring strategy combining base vector similarity with entity match scores (30% weight) and relationship scores (20% weight), making it superior to plain vector search for graph-structured data.

Constructors

Constructor Summary
GraphRetriever(IDocumentStore<T> documentStore, IEmbeddingModel<T> embeddingModel, bool enableAdvancedEntityExtraction) Initializes a new instance of the GraphRetriever class.