大网数据广告活动
Web 标准与前端开发

LanguageModel: availability() static method

· 52CE 我爱测

The **`availability()`** static method of the LanguageModel interface returns a status identifier indicating whether the browser's language model supports a given set of configuration options, without creating a session or triggering a download.

Prompt API

The availability() static method of the LanguageModel interface returns a status identifier indicating whether the browser's language model supports a given set of configuration options, without creating a session or triggering a download.

Use availability() before calling LanguageModel.create_static to determine whether the desired configuration is supported. This avoids initiating a session only to have it fail, and lets you provide a meaningful fallback to users when the configuration is not supported.

Syntax

LanguageModel.availability()
LanguageModel.availability(options)

Parameters

  • options
    • : An object that represents the base set of options used when checking language model support. Properties include:
      • expectedInputs
        • : An array of objects representing the required input modalities and languages. Each object can include the following properties:
          • type
            • : An enumerated value indicating the content type. Must be one of:
              • text
                • : Plain text content.
              • image
                • : Image content.
              • audio
                • : Audio content.
              • tool-call
                • : A tool invocation issued by the model.
              • tool-response
                • : The result of a tool invocation.
          • languages
            • : An array of strings containing BCP 47 language tags (for example, en, fr, ja) representing languages that the session is expected to handle. The user agent uses this list to determine whether the model supports the specified languages.
      • expectedOutputs
        • : An array of objects representing the required output modalities and languages. Each object can include the following properties:
          • type
            • : An enumerated value indicating the content type. Must be one of:
              • text
                • : Textual content.
              • image
                • : Image content.
              • audio
                • : Audio content.
              • tool-call
                • : A tool invocation issued by the model.
              • tool-response
                • : The result of a tool invocation.
          • languages
            • : An array of strings containing BCP 47 language tags (for example, en, fr, ja) that the session is expected to output.
      • tools
        • : An array of objects representing tools available to the AI. Each object can include the following properties:
          • name
            • : A string giving the tool a unique name the model uses to refer to it when issuing a tool call.
          • description
            • : A string describing what the tool does. The model uses this description to decide when and whether to invoke the tool.
          • inputSchema
            • : An object containing a JSON Schema that describes the tool's input parameters. The model uses this schema to construct the arguments it passes to the tool's execute function.
          • execute
            • : A callback function that the user agent invokes when the model calls this tool. It can receive any arguments provided by the model as appropriate and returns a Promise that resolves with a String representing the tool's result.

Return value

A Promise that resolves with one of the values listed below.

  • available
    • : The model is ready to use with the given options.
  • downloadable
    • : The model can support the given options but needs to download additional data to do so. The download has not yet started.
  • downloading
    • : The model can support the given options with an additional data download. The download is currently in progress.
  • unavailable
    • : The model cannot support the given options, or the user agent cannot determine availability, for example, due to a transient activation error. In that case, the caller should retry or fall back to an alternative implementation.

Exceptions

  • InvalidStateError DOMException
    • : Thrown if the calling document is not fully active.
  • NotAllowedError DOMException
    • : Thrown if usage of the method is blocked by a Permissions-Policy/language-model Permissions-Policy.

Examples

See also Using the Prompt API > Checking configuration support and the Complete example on the same page.

Requesting input support

This example shows how to determine whether text and image inputs are supported by the model.

const status = await LanguageModel.availability({
  expectedInputs: [{ type: "text" }, { type: "image" }],
});

Checking availability for a specific language

This example tests whether the model supports English before asking it to translate Japanese text to English.

const status = await LanguageModel.availability({
  expectedInputs: [{ type: "text", languages: ["ja"] }],
  expectedOutputs: [{ type: "text", languages: ["en"] }],
});

if (status === "available") {
  const session = await LanguageModel.create({
    expectedInputs: [{ type: "text", languages: ["ja"] }],
    expectedOutputs: [{ type: "text", languages: ["en"] }],
  });

  const translation = await session.prompt([
    {
      role: "user",
      content: "Translate the following text into English",
    },
    {
      role: "user",
      content: "桜はきれいです",
    },
  ]);

  console.log(translation);
}

Checking availability for multimodal input

Multimodal input describes sessions that can use more than one type of input, such as text and images. Since the availability of input types varies by language model, your code should check the availability of desired modes before creating a session. An example is shown here.

const availability = await LanguageModel.availability({
  expectedInputs: [{ type: "text" }, { type: "image" }],
  expectedOutputs: [{ type: "text", languages: ["en"] }],
});

if (availability === "unavailable") {
  console.warn("This configuration is not supported.");
} else {
  const session = await LanguageModel.create({
    expectedInputs: [{ type: "text" }, { type: "image" }],
    expectedOutputs: [{ type: "text", languages: ["en"] }],
  });
}

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。