A toolkit of browser built-in AI API, including Prompt API, Translation API, Write API, Summarize API, etc.
npm install @rejax/browser-ai
- Language Detection
- Translation
- Prompt/Chat Capabilities
- Text Summarization
- Origin Trial Token Setting
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.
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'
})
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...
}
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...')
}
Set an Origin Trial token for API access:
import { setOriginTrialToken } from '@rejax/browser-ai'
setOriginTrialToken('your-token-here')
interface DetectionResult {
text: string // Detected language code
value: string // Same as text
}
interface UsabilityResult {
available: boolean
apiPath: string[]
createFuncName: string
}
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
}
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.
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.
MIT