MultiHopRetriever<T>
Models & Types · AiDotNet.RetrievalAugmentedGeneration.AdvancedPatterns
An agentic multi-hop retriever that wraps a base IRetriever and an
IGenerator to answer questions requiring several rounds of evidence gathering.
Inheritance: RetrieverBase<T> → MultiHopRetriever<T>
For Beginners
Think of this as a researcher who does not stop at the first search. It searches, reads what it found, decides what is still missing, searches again for that, and keeps going for a few rounds — collecting all the useful documents along the way and never listing the same one twice. It stops as soon as a new round finds nothing new.
How It Works
For each hop (up to maxHops) the retriever fetches documents for the current query, then
asks the generator to propose a follow-up sub-query given the original question and the evidence
gathered so far. Newly retrieved documents are accumulated with de-duplication by document id.
The loop stops early as soon as a hop contributes no new documents, or when the generator returns
an empty / unchanged follow-up query. The final result is the merged set ordered by descending
relevance score.
Because the wrapped IRetriever and IGenerator contracts are
synchronous, the hop loop runs synchronously; RetrieveAsync
exposes the same behaviour with cooperative cancellation between hops.
topK semantics:topK controls how many documents each individual hop retrieves. The
returned collection is the merged, de-duplicated union across all hops and may therefore contain
more than topK documents — this is intentional for multi-hop evidence accumulation.
Constructors
| Constructor | Summary |
|---|---|
MultiHopRetriever(IRetriever<T> baseRetriever, IGenerator<T> generator, int maxHops, int defaultTopK) |
Initializes a new instance of the MultiHopRetriever class. |
Properties
| Property | Summary |
|---|---|
int MaxHops { get; } |
Gets the maximum number of retrieval hops performed. |
Methods
| Method | Summary |
|---|---|
Task<IEnumerable<Document<T>>> RetrieveAsync(string query, CancellationToken cancellationToken) |
Asynchronously performs multi-hop retrieval using the default TopK, honoring cancellation. |
Task<IEnumerable<Document<T>>> RetrieveAsync(string query, int topK, CancellationToken cancellationToken) |
Asynchronously performs multi-hop retrieval, honoring cancellation between hops. |