Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Silverlight | UI for Xamarin | UI for WinUI | UI for ASP.NET Core | UI for .NET MAUI

New to Telerik Document Processing? Download free 30-day trial

CompleteContextQuestionProcessor

The CompleteContextQuestionProcessor class enables you to ask questions about a PDF document and receive answers based on the entire document content. This processor sends the complete document text to the AI model, which is suitable for smaller documents or when you need to ensure that the AI model has access to all the information in the document. This class inherits from the abstract AIProcessorBase class, which provides common functionality for all AI processors.

The CompleteContextQuestionProcessor is ideal for the following scenarios:

  1. Small Documents: When the document is small enough to fit within the token limit of the AI model.
  2. Holistic Understanding: When the question requires understanding the entire document context.
  3. Simplicity: When you don't need the advanced embedding functionality of PartialContextQuestionProcessor.

However, if you're working with larger documents or want to optimize token usage, you should use the PartialContextQuestionProcessor instead.

Public API

Property Description
Settings Gets the settings for the AI question-answering process. Returns CompleteContextProcessorSettings.
Method Description
public Task AnswerQuestion(ISimpleTextDocument document, string question) Answers a question using the provided document. Parameters: document - The document containing the text to process, question - The question to answer. Returns a task that represents the asynchronous operation. The task result contains the answer to the question.

CompleteContextProcessorSettings

The CompleteContextProcessorSettings class provides configuration options for the question-answering process.

Settings Properties

  • ModelMaxInputTokenLimit: Gets or sets the maximum input token limit the model allows.
  • TokenizationEncoding: Gets or sets the tokenization encoding.
  • ModelId: Gets or sets the ID of the model.

Usage Example

The following example demonstrates how to use the CompleteContextQuestionProcessor to ask questions about a PDF document, including working with specific document pages. For setting up the AI client as shown in this example, see the AI Provider Setup section:

[C#] Example 1: Using CompleteContextQuestionProcessor

public async void AskQuestionUsingCompleteContext()
{
    // Load the PDF document
    string filePath = @"path\to\your\document.pdf";
    PdfFormatProvider formatProvider = new PdfFormatProvider();
    RadFixedDocument fixedDocument;

    using (FileStream fs = File.OpenRead(filePath))
    {
        fixedDocument = formatProvider.Import(fs, TimeSpan.FromSeconds(10));
    }

    // Set up the AI client (Azure OpenAI in this example)
    string key = "AZUREOPENAI_KEY";
    string endpoint = "AZUREOPENAI_ENDPOINT";
    string model = "gpt-4o-mini";

    Azure.AI.OpenAI.AzureOpenAIClient azureClient = new AzureOpenAIClient(
        new Uri(endpoint),
        new Azure.AzureKeyCredential(key),
        new Azure.AI.OpenAI.AzureOpenAIClientOptions());
    ChatClient chatClient = azureClient.GetChatClient(model);

    IChatClient iChatClient = new OpenAIChatClient(chatClient);
    int maxTokenCount = 128000;

    // Create the processor
    using (CompleteContextQuestionProcessor processor =
           new CompleteContextQuestionProcessor(iChatClient, maxTokenCount))
    {
        // Customize settings if needed
        processor.Settings.TokenizationEncoding = "cl100k_base";
        processor.Settings.ModelId = "gpt-4o-mini";

        // Example 1: Process full document
        // Convert the document to a simple text representation
        ISimpleTextDocument plainDoc = fixedDocument.ToSimpleTextDocument();

        // Ask a question about the full document
        string question = "What is the main subject of this document?";
        string answer = await processor.AnswerQuestion(plainDoc, question);

        Console.WriteLine($"Question: {question}");
        Console.WriteLine($"Answer: {answer}");

        // Ask another question
        string question2 = "What are the key conclusions drawn in this document?";
        string answer2 = await processor.AnswerQuestion(plainDoc, question2);

        Console.WriteLine($"Question: {question2}");
        Console.WriteLine($"Answer: {answer2}");

        // Example 2: Process specific pages
        // Convert only pages 5-10 to a simple text document (0-based index)
        ISimpleTextDocument partialDoc = fixedDocument.ToSimpleTextDocument(4, 9);

        // Ask a question about the specific pages
        string pageQuestion = "Summarize the content of pages 5-10 of the document.";
        string pageAnswer = await processor.AnswerQuestion(partialDoc, pageQuestion);

        Console.WriteLine($"Question: {pageQuestion}");
        Console.WriteLine($"Answer: {pageAnswer}");
    }
}

See Also

In this article