AiBrow
  • Welcome
  • AiBrow web API
    • Getting started
    • Feature comparison
  • AiBrow Extension
    • Getting started
    • Web polyfill
    • Helping users install the AiBrow extension
    • Remove the on-device helper or models
  • Examples
    • Embedding API
    • LanguageDetector API
    • LanguageModel API
    • Rewriter API
    • Summarizer API
    • Translator API
    • Writer API
    • Using different models
    • Model download feedback
    • Getting JSON output
  • API Reference
    • AI
      • AIBrow
      • BrowserAI
      • AIBrowWeb
    • AiBrow
      • Embedding
      • LanguageDetector
      • LanguageModel
      • Rewriter
      • Summarizer
      • Translator
      • Writer
    • Types
      • AIModelAvailability
      • AIModelCoreCompatibility
      • AIModelDtype
      • AIModelGpuEngine
      • AICreateMonitor
      • EmbeddingCreateOptions
      • LanguageDetectorCreateOptions
      • LanguageDetectorDetectResult
      • LanguageModelCreateOptions
      • RewriterCreateOptions
      • RewriterFormat
      • RewriterLength
      • RewriterTone
      • SummarizerCreateOptions
      • SummarizerFormat
      • SummarizerLength
      • SummarizerType
      • TranslatorCreateOptions
      • WriterCreateOptions
      • WriterFormat
      • WriterLength
      • WriterTone
    • Models
Powered by GitBook
On this page
  • Continuing a conversation
  • System prompts & initial prompts
  • Demos
  1. Examples

LanguageModel API

PreviousLanguageDetector APINextRewriter API

Last updated 12 days ago

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

This API is compatible with the shipping with Google Chrome

import AI from '@aibrow/web'
const session = await AI.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

import AI from '@aibrow/web'
const session = await AI.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

import AI from '@aibrow/web'

const session = await AI.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

Prompt API proposal
Email subject generator
Spreadsheet autofill