New to Telerik Document ProcessingStart a free 30-day trial

Getting Started

Updated on Feb 19, 2026

The following example demonstrates how to use the GenAI-powered Document Insights functionality to summarize a Word document and ask questions about it:

The following code snippet is valid for Azure Open AI 9.3. The specific IChatClient initialization may be different according to the specific version.

For .NET 8+ (Target OS Windows) with Packages for .NET 8 and .NET 10 for Windows, an IEmbedder implementation is required for the PartialContextQuestionProcessor.

Example 1: Using GenAI-powered Document Insights

c#
public async void ProcessInputFlowDocWithAI()
{
    // Load the Docx document
    string filePath = @"path\to\your\document.docx";
    DocxFormatProvider formatProvider = new DocxFormatProvider();
    RadFlowDocument flowDocument;

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

    // Convert the document to a simple text representation
    SimpleTextDocument plainDoc = flowDocument.ToSimpleTextDocument(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());
    OpenAI.Chat.ChatClient chatClient = azureClient.GetChatClient(model);

    IChatClient iChatClient = (IChatClient)chatClient;
    int maxTokenCount = 128000;
    int maxNumberOfEmbeddingsSent = 20;
    int embeddingTokenSize = 500;
    string tokenizationEncoding = "cl100k_base";
    string additionalPromp = "Focus on the key points and main arguments.";

    // 1. Summarize the document
    SummarizationProcessorSettings summarizationProcessorSettings = new SummarizationProcessorSettings(maxTokenCount, additionalPromp);
    using (SummarizationProcessor summarizationProcessor = new SummarizationProcessor(iChatClient, summarizationProcessorSettings))
    {
        // Handle resources calculation event to control token usage
        summarizationProcessor.SummaryResourcesCalculated += (sender, e) =>
        {
            Console.WriteLine($"Estimated calls required: {e.EstimatedCallsRequired}");
            Console.WriteLine($"Estimated tokens required: {e.EstimatedTokensRequired}");

            // Confirm if the operation should continue
            e.ShouldContinueExecution = true;
        };

        string summary = await summarizationProcessor.Summarize(plainDoc);
        Console.WriteLine("Document Summary:");
        Console.WriteLine(summary);
    }

    // 2. Answer questions using partial context (recommended for efficiency)
    IEmbeddingSettings partialProcessorSettings = EmbeddingSettingsFactory.CreateSettingsForTextDocuments(maxTokenCount, model, tokenizationEncoding, maxNumberOfEmbeddingsSent, embeddingTokenSize);
#if NET8_0_WINDOWS
    using (PartialContextQuestionProcessor partialContextQuestionProcessor = new PartialContextQuestionProcessor(iChatClient, partialProcessorSettings, plainDoc))
    {
        string question = "What are the main findings in the document?";
        string answer = await partialContextQuestionProcessor.AnswerQuestion(question);

        Console.WriteLine($"Question: {question}");
        Console.WriteLine($"Answer: {answer}");
    }
#else
   IEmbedder embeddingsStorage = new CustomOpenAIEmbedder();
    using (PartialContextQuestionProcessor partialContextQuestionProcessor = new PartialContextQuestionProcessor(iChatClient, embeddingsStorage, partialProcessorSettings, plainDoc))
    {
        string question = "What are the main findings in the document?";
        string answer = await partialContextQuestionProcessor.AnswerQuestion(question);

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

    // 3. Answer questions using complete context (for smaller documents)
    CompleteContextProcessorSettings settings = new CompleteContextProcessorSettings(maxTokenCount, model, tokenizationEncoding, false);
    using (CompleteContextQuestionProcessor completeContextQuestionProcessor = new CompleteContextQuestionProcessor(iChatClient, settings))
    {
        string question = "What is the conclusion of the document?";
        string answer = await completeContextQuestionProcessor.AnswerQuestion(plainDoc, question);

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

When you run this code, the AI will process your document, generate a summary, and answer your questions.

See Also

In this article
See Also
Not finding the help you need?
Contact Support