Summarizer API
The summarizeStreaming() method of the Summarizer interface generates a new summary as a ReadableStream.
Syntax
summarizeStreaming(input)
summarizeStreaming(input, options)
Parameters
input- : A string representing the text to be summarized.
options- : An object specifying configuration options for the
summarizeStreaming()operation. Possible values include:context- : A string describing the context the input text is being used in, which helps the
Summarizergenerate a more suitable summary.
- : A string describing the context the input text is being used in, which helps the
signal- : An AbortSignal object instance, which allows the
summarizeStreaming()operation to be aborted via the associated AbortController.
- : An AbortSignal object instance, which allows the
- : An object specifying configuration options for the
Return value
A ReadableStream containing the generated summary.
Exceptions
AbortErrorDOMException- : Thrown if the
Summarizerwas previously destroyed (had Summarizer.destroy() called on it, or was aborted via its abortsignalafter creation).
- : Thrown if the
InvalidStateErrorDOMException- : Thrown if the current Document is not active.
NotAllowedErrorDOMException- : Thrown if usage of the Summarizer API is blocked by a Permissions-Policy/summarizer Permissions-Policy.
NotReadableErrorDOMException- : Thrown if the output summary was filtered by the user agent, for example because it was detected to be harmful, inaccurate, or nonsensical.
NotSupportedErrorDOMException- : Thrown if the provided
contextis not in language theSummarizersupports.
- : Thrown if the provided
- QuotaExceededError
- : Thrown if the summarize operation exceeds the available Summarizer.inputQuota.
UnknownErrorDOMException- : Thrown if the
summarizeStreaming()call failed for any other reason, or a reason the user agent did not wish to disclose.
- : Thrown if the
Examples
Basic summarizeStreaming() usage
const summarizer = await Summarizer.create({
sharedContext:
"A general summary to help a user decide if the text is worth reading",
type: "tldr",
length: "short",
});
const stream = summarizer.summarizeStreaming(myTextString);
let summary = "";
for await (const chunk of stream) {
summary += chunk;
}
console.log("Stream complete");
summaryOutput.textContent = summary;
Specifications
Browser compatibility
See also
- Using the Summarizer API
- Web AI demos on chrome.dev
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
