Class Summarizer
- Namespace
- LMKit.TextGeneration
- Assembly
- LM-Kit.NET.dll
Provides functionality to generate a summary (title and/or content) from various input sources using a language model.
public sealed class Summarizer
- Inheritance
-
Summarizer
- Inherited Members
Examples
Example: Basic text summarization
using LMKit.Model;
using LMKit.TextGeneration;
using System;
// Load the language model
LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
// Create the summarizer
Summarizer summarizer = new Summarizer(model);
// Summarize text content
string article = "Artificial intelligence has made significant strides in recent years, " +
"with large language models demonstrating remarkable capabilities in understanding " +
"and generating human-like text. These advances have applications across healthcare, " +
"education, and business automation.";
var result = summarizer.Summarize(article);
Console.WriteLine($"Title: {result.Title}");
Console.WriteLine($"Summary: {result.Content}");
Example: Customized summarization
using LMKit.Model;
using LMKit.TextGeneration;
using System;
LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
Summarizer summarizer = new Summarizer(model);
// Configure summarization parameters
summarizer.MaxTitleWords = 5;
summarizer.MaxContentWords = 100;
summarizer.Guidance = "Focus on the main business impact and key metrics.";
summarizer.Intent = SummarizationIntent.Semantic;
string report = "Q3 revenue increased by 25% year-over-year...";
var result = summarizer.Summarize(report);
Console.WriteLine($"Title: {result.Title}");
Console.WriteLine($"Summary: {result.Content}");
Example: Summarize a PDF document
using LMKit.Model;
using LMKit.TextGeneration;
using LMKit.Data;
using System;
LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
Summarizer summarizer = new Summarizer(model);
// Summarize a PDF attachment
var pdfAttachment = new Attachment("annual_report.pdf");
var result = summarizer.Summarize(pdfAttachment);
Console.WriteLine($"Document Title: {result.Title}");
Console.WriteLine($"Summary: {result.Content}");
Remarks
This summarizer supports multiple content types, including plain text, images, PDF documents, HTML files, Microsoft Office formats (DOCX, XLSX, PPTX), and email files (EML, MBOX).
Use Summarize(string, CancellationToken) or SummarizeAsync(string, CancellationToken) for text input, and Summarize(Attachment, CancellationToken) or SummarizeAsync(Attachment, CancellationToken) for file attachments.
For a complete list of supported file formats, see the Attachment class documentation.
Key Features
- Generate titles with configurable word limits via MaxTitleWords
- Generate summaries with configurable word limits via MaxContentWords
- Choose summarization intent: classification or semantic via Intent
- Custom guidance to steer summary generation via Guidance
- Multilingual support with automatic language detection
- Handle large documents with overflow resolution strategies
Constructors
- Summarizer(LM)
Initializes a new instance of the Summarizer class with the specified language model.
Properties
- GenerateContent
Gets or sets a value indicating whether the summarization should include the summarized content body.
- GenerateTitle
Gets or sets a value indicating whether the summarization should include a generated title.
- Guidance
Gets or sets optional guidance text that can influence the summarization process.
- Intent
Specifies the summarization behavior to apply to the input content. Determines whether the system should classify the content type (e.g., legal, technical, conversational) or generate a condensed semantic summary of its meaning.
- MaxContentWords
Gets or sets the maximum number of words allowed in the summarized content. The value is constrained between 0 and 2000 words.
- MaxTitleWords
Gets or sets the maximum number of words allowed in the summarized title. The value is constrained between 0 and 50 words.
- MaximumContextLength
Gets or sets the maximum context length (in tokens) that can be used for the model input. Reducing this value can dramatically increase inference speed on CPUs, but may reduce output quality.
- Model
Gets the underlying language model used for summarization.
- OverflowStrategy
Gets or sets the strategy for handling inputs that exceed the configured MaximumContextLength.
- TargetLanguage
Specifies the preferred language for generated summaries. When set to Undefined (default), the engine attempts to auto-detect the input content language (note: auto-detection may affect performance) and generate the summary accordingly. If explicitly set, the engine makes a best-effort attempt to interpret content and generate the summary in the specified language.
Methods
- Summarize(Attachment, CancellationToken)
Generates a summary from an Attachment synchronously.
- Summarize(ImageBuffer, CancellationToken)
Synchronously generates a summary from the specified image.
- Summarize(string, CancellationToken)
Generates a summary from the provided text content synchronously.
- SummarizeAsync(Attachment, CancellationToken)
Asynchronously generates a summary from an Attachment.
- SummarizeAsync(ImageBuffer, CancellationToken)
Asynchronously generates a summary from the specified image.
- SummarizeAsync(string, CancellationToken)
Asynchronously generates a summary from the provided text content.