Summarizer
On-device summaries - key points, a TL;DR, a teaser or a headline - from a session you configure once and reuse across documents.
@inject Bit.Butil.Summarizer summarizerMDN reference
Availability, Create from a
user gesture (the first creation downloads the model), summarize, and dispose the session.
See LanguageModel for the whole story.
Availability answers whether these particular options can be served - the summary type, format, length and languages you intend to ask for.
@inject Bit.Butil.Summarizer summarizer
var availability = await summarizer.Availability(
new SummarizerOptions { Type = "key-points", Length = "short" });Type is what kind of summary (key-points, tldr, teaser, headline), Length how long (short, medium, long), Format markdown or plain-text, and SharedContext what every document in this session is - which is the option that most improves the result.
_session = await summarizer.Create(new SummarizerOptions
{
Type = "key-points",
Length = "short",
Format = "plain-text",
SharedContext = "Release notes for a .NET library, summarised for developers."
});Summarize waits for the whole result; SummarizeStreaming reports it as it is produced, which is what a UI should use for anything longer than a paragraph. The per-call context argument describes this one document, on top of the session's shared context.
<p>_summary</p>
{
private string _summary = "";
private SummarizerSession? _session; // from Create
private async Task Run(string article)
{
var summary = await _session!.Summarize(article, context: "A conference talk abstract.");
// or, streaming - the first words arrive long before the last:
_summary = "";
await _session.SummarizeStreaming(article, chunk =>
{
_summary += chunk;
InvokeAsync(StateHasChanged);
});
}
}API reference
ValueTask<bool> IsSupported()ValueTask<AiAvailability> Availability() / Availability(SummarizerOptions options)ValueTask<SummarizerSession?> Create(SummarizerOptions? options = null, Action<double>? onDownloadProgress = null)ValueTask<string?> Summarize(string input, string? context = null)Task<string> SummarizeStreaming(string input, Action<string>? onChunk = null, string? context = null)ValueTask DisposeAsync()