<style>
table, th, td {
	border: 1px solid;
}
table th:first-of-type {
	width: 30%;
}
table th:nth-of-type(2) {
	width: 70%;
}
</style>

# SummarizationProcessor

The `SummarizationProcessor` class enables you to generate concise summaries of Word documents using Large Language Models (LLMs). It inherits from the abstract `AIProcessorBase` class, which provides common functionality for all AI processors. It 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 used for summarization. |

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

| Event                                                                          | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `EventHandler<SummaryResourcesCalculatedEventArgs> SummaryResourcesCalculated` | Fires before the summarization process begins and provides information about the estimated resource usage. The `SummaryResourcesCalculatedEventArgs` exposes properties: `EstimatedCallsRequired` (number of API calls required), `EstimatedTokensRequired` (number of tokens to process), 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 must be considered untrusted and may include unexpected or unsafe data. Properly 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. You can use it for clarification purposes. |

**Example 1: Create SummarizationProcessorSettings with a token limit and an additional prompt instruction**

```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 Word 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/radwordsprocessing/editing/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.

**Example 2: Import a DOCX file, configure Azure OpenAI, monitor resource estimates, and summarize the document**

```C#
async Task SummarizeDocument()
{
    // 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(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/radwordsprocessing/editing/gen-ai-powered-document-insights/overview)
* [Prerequisites](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/gen-ai-powered-document-insights/prerequisites)
* [PartialContextQuestionProcessor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/gen-ai-powered-document-insights/partial-context-question-processor)
* [CompleteContextQuestionProcessor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/gen-ai-powered-document-insights/complete-context-question-processor)
