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

Prompt API

· 52CE 我爱测

[!WARNING] > This feature is currently opposed by two browser vendor(s). See the Standards positions section below for details of opposition.

Prompt API

[!WARNING] This feature is currently opposed by two browser vendor(s). See the Standards positions section below for details of opposition.

The Prompt API allows web pages to directly prompt a language model provided by the user agent via a JavaScript interface, without needing to manage implementation-specific details of the AI model being used.

Concepts and usage

Using AI prompts to return information is very common on the web, with examples including rapid information look up, code and content generation, chatbots to provide customer service functions, image identification and description, audio transcription, and more.

The Prompt API provides an asynchronous (Promise-based) mechanism for a website to prompt the browser's own internal AI model. Having an on-device model is useful and efficient because sensitive data can stay on the user's device, the model is available offline, and developers can avoid the cost and latency of API calls to external services. The API abstracts away model-specific details such as tokenization and templating, so developers don't need to handle those differences across different implementations.

All interaction with the language model happens through a LanguageModel session. You can use this session to specify context for the model, such as providing documents, background information, or conversation history, and to prompt it for responses to specific questions.

Before creating a session, you can call the static LanguageModel.availability_static method to determine whether the language model supports a given configuration on the current device. This allows pages to adapt gracefully if data for the desired configuration can't be provided or is not downloaded. For example, you could display a download prompt or fall back to a cloud-based AI service, rather than creating a session only to have it fail.

A session is created by calling the static LanguageModel.create_static method. Once you have a session, you can call LanguageModel.append() to preload content into the session without generating a response, and LanguageModel.prompt() or LanguageModel.promptStreaming() to send text or multimodal input and receive the response.

You can cancel pending operations such as create(), prompt(), and append() using an AbortController.

After a LanguageModel instance has been created, you can release its assigned resources and stop any further activity by calling its LanguageModel.destroy() method. You are encouraged to do this after you've finished with the object as it can consume a lot of resources.

To get started, check out Using the Prompt API for a walkthrough of the basics.

Interfaces

  • CreateMonitor
    • : Provides information on the progress of an AI model download, for example a language pack or some fine-tuning data.
  • LanguageModel
    • : Represents a session with a browser-provided language model. Provides static methods for creating sessions and checking availability, and instance methods for prompting the model, managing context, cloning sessions, etc.

HTTP headers

  • Permissions-Policy; the Permissions-Policy/language-model directive
    • : Controls access to the prompt functionality. Where a policy specifically disallows its use, the LanguageModel.availability_static static method will return unavailable, and any attempts to call other LanguageModel methods will fail with a NotAllowedError DOMException.

Security considerations

The Prompt API is restricted to secure contexts (HTTPS). In addition, creation of LanguageModel objects requires that the user has recently interacted with the page (transient user activation is required).

Access to the API is also controlled via the Permissions-Policy/language-model Permissions-Policy directive.

Examples

For full examples, check out our guides, starting with Using the Prompt API.

Also see the Chrome dev rel team's demos:

Specifications

Standards positions

Two browser vendors oppose this specification. Known standards positions are as follows:

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

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

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