ブラウザの JavaScript と TypeScript 用の LiteRT-LM の Web API。これは、WebGPU で実行されるテキスト入力 / テキスト出力に対応した初期プレビュー版です。
サポートされているモデル
現在、LiteRT-LM JS API は、ウェブ互換モデルの限定されたセットをサポートしています。
一般的な .litertlm モデルファイルをカバーするように拡張する作業を進めていますが、現時点では次のモデルがサポートされています。
gemma-4-E2B-it-web.litertlmlitert-community/gemma-4-E2B-it-litert-lm のgemma-4-E4B-it-web.litertlmlitert-community/gemma-4-E4B-it-litert-lm から
はじめに
JavaScript API で構築された REPL チャットアプリのサンプルを次に示します。
<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);
}
}
}
生成をキャンセルする
進行中の生成を明示的にキャンセルするには、Conversation インスタンスで cancel() を呼び出します。
// 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
}
}