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

Getting Started

Minimum Version: Q4 2025

The following example demonstrates how to use the GenAI-powered Document Insights functionality to summarize an Excel 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.

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

        public async void ProcessInputSpreadDocWithAI()
        {
            // Load the XLSX document
            string filePath = @"path\to\your\document.xlx";
            XlsxFormatProvider formatProvider = new XlsxFormatProvider();
            Workbook workbook;

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

            // Convert the document to a simple text representation
            SimpleTextDocument plainDoc = workbook.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;
            int maxNumberOfEmbeddingsSent = 20;
            int embeddingTokenSize = 500;
            string tokenizationEncoding = "cl100k_base";
            string additionalPromp = "Focus on the key points.";

            // 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 content does the document contain?";
                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