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

LanguageModel

· 52CE 我爱测

The **`LanguageModel`** interface of the Prompt API represents a session with a browser-provided language model. It exposes static methods for creating sessions and checking availability, as well as instance methods for prompting the model, appending context, and managing the con

Prompt API

The LanguageModel interface of the Prompt API represents a session with a browser-provided language model. It exposes static methods for creating sessions and checking availability, as well as instance methods for prompting the model, appending context, and managing the context window.

LanguageModel instances cannot be constructed directly. Instead, use the static LanguageModel.create_static method.

Instance properties

  • LanguageModel.contextUsage
    • : Returns the number of context window tokens currently consumed by this session.
  • LanguageModel.contextWindow
    • : Returns the total context window size available for this session, in tokens.

Static methods

  • LanguageModel.availability_static
    • : Returns a Promise that resolves with an enumerated value indicating whether the language model is available for the given options.
  • LanguageModel.create_static
    • : Returns a Promise that resolves with a new LanguageModel session, downloading the model data if necessary.

Instance methods

  • LanguageModel.append()
    • : Returns a Promise that resolves when the given input has been added to the session's context window, without generating a response.
  • LanguageModel.clone()
    • : Returns a Promise that resolves with a new LanguageModel session that is a copy of the session it is called on, including all context.
  • LanguageModel.destroy()
    • : Releases the resources assigned to the LanguageModel instance it is called on and stops any further activity on it.
  • LanguageModel.measureContextUsage()
    • : Returns a Promise that resolves with the number of context window tokens that the given input would consume if it were used in an operation such as prompt() or append().
  • LanguageModel.prompt()
    • : Returns a Promise that resolves with the model's complete response to the given input.
  • LanguageModel.promptStreaming()
    • : Returns a ReadableStream that streams the model's response to the given input as it is generated.

Events

  • LanguageModel.contextoverflow_event
    • : Fired when a prompt(), promptStreaming(), or append() call exceeds the context window size.

Examples

Creating a session and prompting the model

This example first calls LanguageModel.create_static to get a new session. It specifies that the language model adopt a "system" role and defines how it should behave. Note that the example uses await because create() returns a Promise. This may take some time to resolve if the model needs to be downloaded.

After creating the session, the example calls LanguageModel.prompt() to ask a specific question.

const session = await LanguageModel.create({
  initialPrompts: [
    {
      role: "system",
      content: "You are a helpful assistant.",
    },
  ],
});

const response = await session.prompt("What is the capital of France?");
console.log(response); // "The capital of France is Paris."

Streaming a response

This example calls LanguageModel.promptStreaming() to get an instance of ReadableStream and writes it to the console in chunks.

const session = await LanguageModel.create();
const readableStream = session.promptStreaming("Tell me a short story.");

for await (const chunk of readableStream) {
  console.log(chunk);
}

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

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

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