# SummarizationProcessor

The `SummarizationProcessor` class enables you to generate concise summaries of PDF documents using Large Language Models (LLMs). It inherits from the abstract `AIProcessorBase` class, which provides common functionality for all AI processors. The processor automatically handles large documents by splitting them into smaller chunks when needed, making it suitable for documents of any size.

## Public API

| Property   | Description                                        |
| ---------- | -------------------------------------------------- |
| `Settings` | Gets the settings that are used for summarization. |

| Method                                                | Description                                                                                                                            |
| ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `Task<string> Summarize(SimpleTextDocument document)` | Generates a summary of the provided document. The parameter `document` is a `SimpleTextDocument` containing the text to be summarized. |

| Event                                                                          | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| ------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `EventHandler<SummaryResourcesCalculatedEventArgs> SummaryResourcesCalculated` | Triggered before the actual summarization process begins, providing information about the estimated resource usage. The `SummaryResourcesCalculatedEventArgs` provides properties: `EstimatedCallsRequired` (number of API calls required), `EstimatedTokensRequired` (number of tokens to be processed), and `ShouldContinueExecution` (Boolean flag indicating whether to proceed with summarization, default is `true` for single-call and `false` for multi-call operations). |

> caution **Security Warning:** The output produced by this API is generated by a Large Language Model (LLM). The content is untrusted and may include unexpected or unsafe data. Sanitize or encode all output before displaying it in a user interface, logging, or using it in any security-sensitive context.

## SummarizationProcessorSettings

The `SummarizationProcessorSettings` class defines configuration options for the summarization process. The following read-only properties can be set only through the constructor:

| Property                  | Description                                                                             |
| ------------------------- | --------------------------------------------------------------------------------------- |
| `ModelMaxInputTokenLimit` | The maximum input token limit for the model.                                            |
| `PromptAddition`          | An addition for the prompt used for summarization. Use this for clarification purposes. |

**Configuring SummarizationProcessorSettings**

```C#
int maxTokenCount = 1000;
string promptAddition = "Focus on the key points and main arguments.";

SummarizationProcessorSettings summarizationProcessingSettings = new SummarizationProcessorSettings(maxTokenCount, promptAddition);

using (SummarizationProcessor summarizationProcessor = new SummarizationProcessor(iChatClient, summarizationProcessingSettings))
{
    // Rest of the code...
}
```

## Usage Example

The following example demonstrates how to use the `SummarizationProcessor` to generate a summary of a PDF document. To set up the AI client as shown in this example, see the [AI Provider Setup](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/features/gen-ai-powered-document-insights/prerequisites#ai-provider-setup) section.

### Handling Large Documents

For large documents that exceed the token limit of the model, `SummarizationProcessor` automatically splits the document into smaller chunks and processes them separately:

1. The document is split into chunks that fit within the model token limit.
2. Each chunk is summarized individually.
3. The individual summaries are combined and sent for a final summarization.

This approach allows the processor to handle documents of any size, but it increases the number of API calls required. The `SummaryResourcesCalculated` event provides information about the expected resource usage, allowing you to decide whether to proceed with the operation.

**Using SummarizationProcessor**

```C#
async Task SummarizeDocument()
{
    // 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));
    }

    // Convert the document to a simple text representation
    SimpleTextDocument plainDoc = fixedDocument.ToSimpleTextDocument(TimeSpan.FromSeconds(20));

    // 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 = (IChatClient)chatClient;
    int maxTokenCount = 128000;
    string promptAddition = "Focus on the key points and main arguments.";

    SummarizationProcessorSettings summarizationProcessingSettings = new SummarizationProcessorSettings(maxTokenCount, promptAddition);

    using (SummarizationProcessor summarizationProcessor = new SummarizationProcessor(iChatClient, summarizationProcessingSettings))
    {
        // Subscribe to the SummaryResourcesCalculated event to monitor token usage
        summarizationProcessor.SummaryResourcesCalculated += (sender, e) =>
        {
            Console.WriteLine($"This summarization will require approximately {e.EstimatedTokensRequired} tokens " +
                             $"and {e.EstimatedCallsRequired} API calls.");

            // For large documents, you need to explicitly approve the operation
            // to avoid unexpected API usage and costs
            if (e.EstimatedCallsRequired > 1)
            {
                Console.WriteLine("Document is large and will require multiple API calls.");

                // Set to true to proceed with summarization, or leave as false to cancel
                e.ShouldContinueExecution = false;
            }
        };

        try
        {
            // Generate the summary
            string summary = await summarizationProcessor.Summarize(plainDoc);
            Console.WriteLine("Document Summary:");
            Console.WriteLine(summary);
        }
        catch (OperationCanceledException)
        {
            Console.WriteLine("Summarization was cancelled.");
        }
    }
}
```

## See Also

* [GenAI-powered Document Insights Overview](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/features/gen-ai-powered-document-insights/overview)
* [GenAI-powered Document Insights Prerequisites](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/features/gen-ai-powered-document-insights/prerequisites)
* [PartialContextQuestionProcessor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/features/gen-ai-powered-document-insights/partial-context-question-processor)
* [CompleteContextQuestionProcessor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/features/gen-ai-powered-document-insights/complete-context-question-processor)
