Method QueryPartitionsAsync
QueryPartitionsAsync(string, IEnumerable<PartitionSimilarity>, IConversation, CancellationToken)
Asynchronously generates a response by querying the specified partitions and returns the result with source document references.
public Task<DocumentQueryResult> QueryPartitionsAsync(string question, IEnumerable<PartitionSimilarity> partitions, IConversation conversation, CancellationToken cancellationToken = default)
Parameters
questionstringThe question to ask based on the partition content.
partitionsIEnumerable<PartitionSimilarity>A collection of PartitionSimilarity instances containing the context for answering the question. Typically obtained from FindMatchingPartitionsAsync(string, int, float, bool, bool, CancellationToken).
conversationIConversationThe IConversation to use for generating the response.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task<DocumentQueryResult>
A task that resolves to a DocumentQueryResult containing the generated response and a list of source references indicating which documents and pages were used.
Examples
// Find relevant partitions
var partitions = await docRag.FindMatchingPartitionsAsync("What is the conclusion?", topK: 5);
// Generate response with source tracking
LM chatModel = LM.LoadFromModelID("llama-3.1-8b-instruct");
var conversation = new SingleTurnConversation(chatModel);
var result = await docRag.QueryPartitionsAsync(
"What is the conclusion?",
partitions,
conversation,
cancellationToken);
Console.WriteLine($"Answer: {result.Response.Text}");
Console.WriteLine("Sources:");
foreach (var source in result.SourceReferences)
{
Console.WriteLine($" - {source.DocumentName}, Page {source.PageNumber}");
}
Remarks
This method extends the base QueryPartitionsAsync(string, IEnumerable<PartitionSimilarity>, IConversation, CancellationToken) by returning a DocumentQueryResult that includes source references extracted from partition metadata. These references identify the original document name and page number for each partition used in generating the response.
The PromptTemplate property is used to format the prompt sent to the conversation.
- See Also
QueryPartitionsAsync(string, IEnumerable<PartitionSimilarity>, IConversation, bool, CancellationToken)
Asynchronously generates a response by querying the specified partitions, optionally including page renderings for visual context, and returns the result with source document references.
public Task<DocumentQueryResult> QueryPartitionsAsync(string question, IEnumerable<PartitionSimilarity> partitions, IConversation conversation, bool includePageRenderingsInContext, CancellationToken cancellationToken = default)
Parameters
questionstringThe question to ask based on the partition content.
partitionsIEnumerable<PartitionSimilarity>A collection of PartitionSimilarity instances containing the context for answering the question. Typically obtained from FindMatchingPartitionsAsync(string, int, float, bool, bool, CancellationToken).
conversationIConversationThe IConversation to use for generating the response.
includePageRenderingsInContextbooltrueto include page images alongside text for visual grounding;falseto use text only. This parameter is only effective when the conversation model supports vision capabilities.cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task<DocumentQueryResult>
A task that resolves to a DocumentQueryResult containing the generated response and a list of source references indicating which documents and pages were used.
Examples
// Find relevant partitions
var partitions = await docRag.FindMatchingPartitionsAsync("What does the chart show?", topK: 5);
// Generate response with visual context for better chart/table understanding
LM chatModel = LM.LoadFromModelID("qwen3-vl:4b"); // Vision-capable model
var conversation = new SingleTurnConversation(chatModel);
var result = await docRag.QueryPartitionsAsync(
"What does the chart show?",
partitions,
conversation,
includePageRenderingsInContext: true,
cancellationToken);
Console.WriteLine($"Answer: {result.Response.Text}");
Console.WriteLine("Sources:");
foreach (var source in result.SourceReferences)
{
Console.WriteLine($" - {source.DocumentName}, Page {source.PageNumber}");
}
Remarks
This method extends the base QueryPartitionsAsync(string, IEnumerable<PartitionSimilarity>, IConversation, CancellationToken) by returning a DocumentQueryResult that includes source references extracted from partition metadata. These references identify the original document name and page number for each partition used in generating the response.
When includePageRenderingsInContext is true and the conversation model
supports vision (HasVision), page renderings corresponding to retrieved passages
are injected into the context. This enables the model to visually interpret tables, charts, figures,
and complex layouts that may lose fidelity during text extraction.
Page renderings are deduplicated by document URI and page number to avoid redundant context when multiple partitions reference the same page. If a page rendering fails to load, the error is logged and processing continues with the remaining pages.
The PromptTemplate property is used to format the prompt sent to the conversation.
Exceptions
- ArgumentNullException
Thrown if
question,partitions, orconversationisnull.- OperationCanceledException
Thrown if the operation is canceled.
- See Also