Web API של LiteRT-LM ל-JavaScript ו-TypeScript בדפדפן. זוהי גרסת טרום-השקה מוקדמת שתומכת בהזנת טקסט ובהוצאת טקסט שפועלת ב-WebGPU.
מודלים נתמכים
בשלב הזה, LiteRT-LM JS API תומך בקבוצה מוגבלת של מודלים שתואמים לאינטרנט.
אנחנו פועלים להרחבת התמיכה כך שתכלול קבצים של מודלים כלליים של .litertlm, אבל בשלב הזה אנחנו תומכים במודלים הבאים:
gemma-4-E2B-it-web.litertlmfrom litert-community/gemma-4-E2B-it-litert-lmgemma-4-E4B-it-web.litertlmמתוך litert-community/gemma-4-E4B-it-litert-lm
מבוא
הנה דוגמה לאפליקציית צ'אט REPL שנבנתה באמצעות JavaScript API:
<div id="out" style="white-space: pre-wrap; font-family: monospace;"></div>
<input id="in" onkeydown="if(event.key === 'Enter') repl(this)">
<script type="module">
import { Engine } from 'https://cdn.jsdelivr.net/npm/@litert-lm/core/+esm';
const engine = await Engine.create({
// Load the Gemma 4 E2B model
model: 'https://huggingface.co/litert-community/gemma-4-E2B-it-litert-lm/resolve/main/gemma-4-E2B-it-web.litertlm'
// Or use the E4B model by swapping in this line
// model: 'https://huggingface.co/litert-community/gemma-4-E4B-it-litert-lm/resolve/main/gemma-4-E4B-it-web.litertlm'
});
const chat = await engine.createConversation();
window.repl = async (el) => {
const text = el.value;
el.value = ''; // Clear immediately
out.append(`\n>>> ${text}\nAI: `);
for await (const chunk of chat.sendMessageStreaming(text)) {
out.append(chunk.content[0].text);
}
};
</script>
תחילת העבודה
LiteRT-LM זמין כחבילת npm. אפשר להתקין את הגרסה העדכנית מ-npm או לייבא אותה ישירות מ-CDN:
# From npm
npm i --save @litert-lm/core
# From a CDN (in your JavaScript file)
import * as litertlm from 'https://cdn.jsdelivr.net/npm/@litert-lm/core/+esm';
הפעלת המנוע
Engine היא נקודת הכניסה ל-API. הוא מטפל בטעינת המודל, ביצירת סשן ובניהול משאבים. חשוב לזכור delete את המנוע כדי לשחרר משאבים כשאין יותר צורך במודל.
הערה: טעינת המודל יכולה להימשך כמה שניות.
import {Engine, EngineSettings} from '@litert-lm/core';
const engineSettings = {
model: 'url/path/to/model.litertlm', // or a ReadableStream, or a Blob
// You can configure context length and other settings here
mainExecutorSettings: {
maxNumTokens: 8192,
},
} satisfies EngineSettings;
const engine = await Engine.create(engineSettings);
// ... Use the engine to create a conversation ...
// Delete the engine when done.
await engine.delete();
יצירת שיחה
אחרי שמאתחלים את המנוע, יוצרים מכונת Conversation. אתם יכולים לספק ConversationConfig כדי להתאים אישית את ההתנהגות שלו.
const conversation = await engine.createConversation({
preface: {
messages: [
{role: 'system', content: 'You are a helpful assistant'}
]
}
});
conversation.sendMessage({
role: 'user',
content: 'Write a poem',
});
שליחת הודעות
אתם יכולים לשלוח הודעות עם או בלי סטרימינג.
דוגמה לשימוש שלא קשור לסטרימינג
// Simple string input
let response = await conversation.sendMessage("What is the capital of France?");
console.log(response.content[0].text);
// Or with full message structure
response = await conversation.sendMessage({role: 'user', content: '...'});
דוגמה לסטרימינג
// sendMessageStreaming returns a ReadableStream of response chunks
const stream = conversation.sendMessageStreaming('Tell me a long story.');
for await (const chunk of stream) {
// Chunks are Records containing pieces of the response
for (const item of chunk.content) {
if (item.type === 'text') {
console.log(item.text);
}
}
}
ביטול היצירה
אפשר לבטל יצירה שמתבצעת באופן מפורש על ידי קריאה ל-cancel() במופע Conversation:
// Cancel any ongoing generation
conversation.cancel();
אם אתם מזרמים את התשובה, יציאה מוקדמת מהלולאה for await...of (למשל באמצעות break) תבטל אוטומטית את תהליך היצירה שמתבצע:
for await (const chunk of stream) {
if (shouldStop()) {
break; // Cancels the stream and underlying generation
}
}