Getting Started
The following example demonstrates how to use the GenAI-powered Document Insights functionality to summarize a PDF 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 Framework and .NET Standard an IEmbeddingsStorage implementation is required for the PartialContextQuestionProcessor.
[C#] Example 1: Using GenAI-powered Document Insights
public async void ProcessPdfWithAI()
{
// Load the PDF document
// string filePath = @"path\to\your\document.pdf";
string filePath = @"C:\Users\dyordano\Downloads\SignedDocument (3).pdf";
PdfFormatProvider formatProvider = new PdfFormatProvider();
RadFixedDocument fixedDocument;
using (FileStream fs = File.OpenRead(filePath))
{
fixedDocument = formatProvider.Import(fs, TimeSpan.FromSeconds(10));
}
// Convert the document to a simple text representation
ISimpleTextDocument plainDoc = fixedDocument.ToSimpleTextDocument();
// 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.