GeezerVison is showing developer.mozilla.org — LanguageModel - Web APIs | MDN
LanguageModel
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
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() method.
contextoverflow
-
Fired when a prompt(), promptStreaming(), or append() call exceeds the context window size.
This example first calls create() 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 prompt() to ask a specific question.
js
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."
This example calls promptStreaming() to get an instance of ReadableStream and writes it to the console in chunks.
js
const session = await LanguageModel.create();
const readableStream = session.promptStreaming("Tell me a short story.");
for await (const chunk of readableStream) {
console.log(chunk);
}