Back to Content

Summarizer

files/en-us/web/api/summarizer/index.md

latest3.8 KB
Original Source

{{APIRef("Summarizer API")}}{{SeeCompatTable}}{{securecontext_header}}

The Summarizer interface of the {{domxref("Summarizer API", "Summarizer API", "", "nocode")}} contains all the functionality for this API, including checking AI model availability, creating a new Summarizer instance, using it to generate a new summary, and more.

{{InheritanceDiagram}}

Instance properties

  • {{domxref("Summarizer.expectedContextLanguages", "expectedContextLanguages")}} {{ReadOnlyInline}} {{Experimental_Inline}}
    • : The languages the context strings should be written in.
  • {{domxref("Summarizer.expectedInputLanguages", "expectedInputLanguages")}} {{ReadOnlyInline}} {{Experimental_Inline}}
    • : The languages the Summarizer should support.
  • {{domxref("Summarizer.format", "format")}} {{ReadOnlyInline}} {{Experimental_Inline}}
    • : The text format summaries will be returned in.
  • {{domxref("Summarizer.inputQuota", "inputQuota")}} {{ReadOnlyInline}} {{Experimental_Inline}}
    • : The input quota available to the browser for generating summaries.
  • {{domxref("Summarizer.length", "length")}} {{ReadOnlyInline}} {{Experimental_Inline}}
    • : The relative length of the generated summaries.
  • {{domxref("Summarizer.outputLanguage", "outputLanguage")}} {{ReadOnlyInline}} {{Experimental_Inline}}
    • : The language the summary should be generated in.
  • {{domxref("Summarizer.sharedContext", "sharedContext")}} {{ReadOnlyInline}} {{Experimental_Inline}}
    • : A text string describing the context the pieces of text to summarize are being used in, which helps the Summarizer generate more suitable summaries.
  • {{domxref("Summarizer.type", "type")}} {{ReadOnlyInline}} {{Experimental_Inline}}
    • : The type of summary that will generated by the Summarizer.

Static methods

  • {{domxref("Summarizer.availability_static", "availability()")}} {{Experimental_Inline}}
    • : Returns an enumerated value that indicates whether the browser AI model supports a given Summarizer configuration.
  • {{domxref("Summarizer.create_static", "create()")}} {{Experimental_Inline}}
    • : Creates a new Summarizer instance from which to generate summaries.

Instance methods

  • {{domxref("Summarizer.destroy", "destroy()")}} {{Experimental_Inline}}
    • : Releases the resources assigned to the Summarizer instance it is called on and stops any further activity on it.
  • {{domxref("Summarizer.measureInputUsage", "measureInputUsage()")}} {{Experimental_Inline}}
    • : Reports how much input quota would be used by a summarize operation for a given text input.
  • {{domxref("Summarizer.summarize", "summarize()")}} {{Experimental_Inline}}
    • : Generates a new summary string.
  • {{domxref("Summarizer.summarizeStreaming", "summarizeStreaming()")}} {{Experimental_Inline}}
    • : Generates a new summary as a {{domxref("ReadableStream")}}.

Examples

See Using the Summarizer API for a complete example.

Creating a Summarizer instance

js
const summarizer = await Summarizer.create({
  sharedContext:
    "A general summary to help a user decide if the text is worth reading",
  type: "tldr",
  length: "short",
  format: "markdown",
  expectedInputLanguages: ["en-US"],
  outputLanguage: "en-US",
});

Generating a summary

js
const summary = await summarizer.summarize(myTextString);
console.log(summary);

Generating a summary stream

js
const stream = summarizer.summarizeStreaming(myTextString);
let summary = "";

for await (const chunk of stream) {
  summary += chunk;
}

console.log("Stream complete");
summaryOutput.textContent = summary;

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also