Tarayıcıda JavaScript ve TypeScript için LiteRT-LM'nin Web API'si. Bu, WebGPU'da çalışan metin girişi / metin çıkışı özelliğini destekleyen bir erken erişim sürümüdür.
Giriş
JavaScript API ile oluşturulmuş örnek bir REPL sohbet uygulamasını aşağıda bulabilirsiniz:
<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({ model: '/path/to/model.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>
Başlarken
LiteRT-LM, npm paketi olarak kullanılabilir. En son sürümü npm'den yükleyebilir veya doğrudan bir CDN'den içe aktarabilirsiniz:
# 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'i başlatma
Engine, API'nin giriş noktasıdır. Model yükleme, oturum oluşturma ve kaynak yönetimini gerçekleştirir. Model artık gerekli olmadığında kaynakları serbest bırakmak için motoru delete etmeyi unutmayın.
Not: Motorun başlatılması, modelin yüklenmesi için birkaç saniye sürebilir.
import {Engine, EngineSettings} from '@litert-lm/core';
const engineSettings = {
model: 'url/path/to/model.litertlm', // or a ReadableStream, or a Blob
} satisfies EngineSettings;
const engine = await Engine.create(engineSettings);
// ... Use the engine to create a conversation ...
// Delete the engine when done.
await engine.delete();
Görüşme oluşturma
Motor başlatıldıktan sonra Conversation örneği oluşturun. Davranışını özelleştirmek için ConversationConfig sağlayabilirsiniz.
const conversation = await engine.createConversation({
preface: {
messages: [
{role: 'system', content: 'You are a helpful assistant'}
]
}
});
conversation.sendMessage({
role: 'user',
content: 'Write a poem',
});
Mesajlar Gönderme
Yayın yaparak veya yapmadan mesaj gönderebilirsiniz.
Yayın Olmayan Örnek
// 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: '...'});
Yayın örneği
// 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);
}
}
}
Üretimi İptal Etme
Conversation örneğinde cancel() işlevini çağırarak devam eden bir oluşturma işlemini açıkça iptal edebilirsiniz:
// Cancel any ongoing generation
conversation.cancel();
Yanıtı yayınlıyorsanız for await...of döngüsünden erken çıkmak (ör. break ile) devam eden oluşturma işlemini de otomatik olarak iptal eder:
for await (const chunk of stream) {
if (shouldStop()) {
break; // Cancels the stream and underlying generation
}
}