Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | 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

Getting Started

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 9 for Windows, an IEmbeddingsStorage implementation is required for the PartialContextQuestionProcessor.

[C#] Example 1: Using GenAI-powered Document Insights

            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
                ISimpleTextDocument 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 = new OpenAIChatClient(chatClient);
                int maxTokenCount = 128000;

                // 1. Summarize the document
                using (SummarizationProcessor summarizationProcessor = new SummarizationProcessor(iChatClient, maxTokenCount))
                {
                    // 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)
#if NET8_0_WINDOWS
            using (PartialContextQuestionProcessor partialContextQuestionProcessor = new PartialContextQuestionProcessor(iChatClient, maxTokenCount, 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
                IEmbeddingsStorage embeddingsStorage = new OllamaEmbeddingsStorage();
                using (PartialContextQuestionProcessor partialContextQuestionProcessor =
                       new PartialContextQuestionProcessor(iChatClient, embeddingsStorage, maxTokenCount, 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)
                using (CompleteContextQuestionProcessor completeContextQuestionProcessor =
                       new CompleteContextQuestionProcessor(iChatClient, maxTokenCount))
                {
                    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