Back to Content

Summarizer: create() static method

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

latest4.2 KB
Original Source

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

The create() static method of the {{domxref("Summarizer")}} interface creates a new Summarizer instance from which to generate summaries.

[!NOTE] The create() method requires transient activation, that is, it must be invoked in response to a user action such as a mouse click or button press.

Syntax

js-nolint
Summarizer.create()
Summarizer.create(options)

Parameters

  • options {{optional_inline}}
    • : An object specifying configuration options for the Summarizer. Possible values include:
      • expectedInputLanguages
        • : An array of strings specifying the expected languages of the input text, which should be valid {{glossary("BCP 47 language tag", "BCP 47 language tags")}}. Defaults to ["en"].
      • expectedContextLanguages
        • : An array of strings specifying the expected languages of any provided context strings (either the sharedContext passed to the Summarizer, or a context specified during a {{domxref("Summarizer.summarize", "summarize()")}} or {{domxref("Summarizer.summarizeStreaming", "summarizeStreaming()")}} call), which should be valid BCP 47 language tags. Defaults to ["en"].
      • format
        • : An enumerated value specifying the text {{domxref("Summarizer.format", "format")}} you want summaries returned in. Defaults to markdown.
      • length
        • : An enumerated value specifying the relative {{domxref("Summarizer.length", "length")}} for the generated summaries. Defaults to short.
      • monitor
        • : A callback function with a {{domxref("CreateMonitor")}} argument that enables monitoring download progress of the AI model.
      • outputLanguage
        • : A string specifying the expected language of summaries generated by the Summarizer, which should be a valid BCP 47 language tag. Defaults to en.
      • sharedContext
        • : A {{domxref("Summarizer.sharedContext", "sharedContext")}} string describing the context the pieces of text to summarize are being used in, which helps the Summarizer generate more suitable summaries.
      • signal
        • : An {{domxref("AbortSignal")}} object instance, which allows a create() operation to be aborted via the associated {{domxref("AbortController")}}. The exact effect is dependant on when {{domxref("AbortController.abort()")}} is called:
          • If abort() is called before the create() promise resolves, the create() operation is cancelled.
          • If abort() is called after the create() promise fulfills, it has the same effect as calling {{domxref("Summarizer.destroy()")}}: The resources assigned to the resulting Summarizer instance are released, and any ongoing and subsequent Summarizer method calls will reject with an AbortError.
      • type
        • : An enumerated value specifying the {{domxref("Summarizer.type", "type")}} of summary you want this Summarizer to generate. Defaults to key-points.

Return value

A {{jsxref("Promise")}} that fulfills with a Summarizer object instance.

Exceptions

  • NotAllowedError {{domxref("DOMException")}}
    • : Thrown if usage of the Summarizer API is blocked by a {{httpheader('Permissions-Policy/summarizer','summarizer')}} {{httpheader("Permissions-Policy")}}.
  • NotSupportedError {{domxref("DOMException")}}
    • : Thrown if any of the language tags specified in expectedContextLanguages, expectedInputLanguages, or outputLanguage are invalid, or not supported.
  • OperationError {{domxref("DOMException")}}
    • : General-purpose exception thrown if Summarizer creation failed for any other reason.

Examples

Basic Summarizer creation

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",
});

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also