Skip to content

A toolkit of browser built-in AI API, including Prompt API, Translation API, Write API, Summarize API, etc.

Notifications You must be signed in to change notification settings

JaxNext/browser-ai-toolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@rejax/browser-ai

A toolkit of browser built-in AI API, including Prompt API, Translation API, Write API, Summarize API, etc.

Installation

npm install @rejax/browser-ai

Features

  • Language Detection
  • Translation
  • Prompt/Chat Capabilities
  • Text Summarization
  • Origin Trial Token Setting

Language Detection

The library provides automatic language detection capabilities:

import { detect, checkDetectorUsability } from '@rejax/browser-ai'

// Check if language detection is available
const usability = await checkDetectorUsability()

if (usability.available) {
  // Detect language of text
  const result = await detect('Hello world')
  console.log(result)
  // Output: { text: 'en', value: 'en' }
}

The detector will use either the Chrome AI API (ai.languageDetector) or Translation API (translation) depending on availability.

Translation

The translation module allows translating text between languages:

import { translate, checkTranslatorUsability, updateTranslator } from '@rejax/browser-ai'

// Check translation availability for language pair
const usability = await checkTranslatorUsability({
  sourceLanguage: 'en',
  targetLanguage: 'es'
})

if (usability.available) {
  // Translate text
  const translated = await translate({
    text: 'Hello world',
    sourceLanguage: 'en',
    targetLanguage: 'es'
  })
  console.log(translated) // "Hola mundo"
}

// Update translator for different languages
await updateTranslator({
  sourceLanguage: 'en',
  targetLanguage: 'fr'
})

Prompt/Chat Capabilities

The library provides access to language model and assistant capabilities:

import { promptStreaming, checkPromptUsability } from '@rejax/browser-ai'

// Check if prompt capabilities are available
const usability = await checkPromptUsability()

if (usability.available) {
  // Stream responses from the model
  const stream = await promptStreaming('Tell me a joke')
  // Handle stream response...
}

Text Summarization

Provides text summarization capabilities:

import { summarize, summarizeStreaming, checkSummarizerUsability } from '@rejax/browser-ai'

// Check if summarization is available
const usability = await checkSummarizerUsability()

if (usability.available) {
  // Get a summary
  const summary = await summarize('Long text to summarize...')
  
  // Or use streaming for real-time results
  const stream = await summarizeStreaming('Long text to summarize...')
}

Origin Trial Token

Set an Origin Trial token for API access:

import { setOriginTrialToken } from '@rejax/browser-ai'

setOriginTrialToken('your-token-here')

API Types

Language Detection

interface DetectionResult {
  text: string  // Detected language code
  value: string // Same as text
}

interface UsabilityResult {
  available: boolean
  apiPath: string[]
  createFuncName: string
}

Translation

interface TranslationParams {
  sourceLanguage: string
  targetLanguage: string
}

interface TranslationRequest extends TranslationParams {
  text: string
}

interface TranslationResult {
  available: boolean
  apiPath: string | string[]
  createFuncName: string
  canFuncName?: string
  msg?: string
}

Browser Compatibility

This library requires a browser that supports one or more of:

  • Chrome AI API (window.ai)
    • Language Model/Assistant API
    • Language Detector API
    • Summarizer API
  • Translation API (window.translation)

Currently Chrome/Chromium browsers version 120+ are supported.

Error Handling

The library will throw errors if:

  • Browser APIs are not available
  • Requested language pair is not supported
  • Translation fails
  • Session initialization fails
  • Summarization is not supported

Be sure to wrap API calls in try/catch blocks for proper error handling.

License

MIT

About

A toolkit of browser built-in AI API, including Prompt API, Translation API, Write API, Summarize API, etc.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published