LanguageModel API

The language model API allows you to create a conversation with the language model.

This API is compatible with the Prompt API proposal shipping with Google Chrome

const session = await window.aibrow.languageModel.create();

// Prompt the model and wait for the whole result to come back.
const result = await session.prompt("Write me a poem.");
console.log(result);

// Prompt the model and stream the result:
const stream = await session.promptStreaming("Write me an extra-long poem.");
for await (const chunk of stream) {
  console.log(chunk);
}

Continuing a conversation

const session = await window.aibrow.languageModel.create();

// Ask the intial question
const result = await session.prompt("Tell me about the weather");
console.log(result);

// Prompt the same session again to continue the same conversation
const result2 = await session.prompt("Can you expand on this?");
console.log(result2);

System prompts & initial prompts

You can use system prompts to customize the behaviour of the model as well as initial prompts from a previous conversation

const session = await window.aibrow.languageModel.create({
  initialPrompts: [
    { role: "system", content: "Speak like a pirate" },
    { role: "user", content: "What's your favourite word?" },
    { role: "assistant", content: "Swashbuckling" }
  ]
});

async function randomWord () {
  // Clone the session
  const freshSession = await session.clone()
  return await freshSessiong.prompt("Give me another")
}

// Continue the conversation from the initial prompts
console.log(await randomWord())
console.log(await randomWord())

Demos

Email subject generator

Last updated