ब्राउज़र में JavaScript और TypeScript के लिए, LiteRT-LM का वेब एपीआई. यह शुरुआती झलक है, जो WebGPU में चलने वाले टेक्स्ट-इन / टेक्स्ट-आउट को सपोर्ट करती है.
परिचय
यहां 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({ 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>
शुरू करें
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, एपीआई का एंट्री पॉइंट है. यह मॉडल लोड करने, सेशन बनाने, और संसाधनों के मैनेजमेंट को हैंडल करता है. मॉडल की ज़रूरत न होने पर, संसाधनों को रिलीज़ करने के लिए इंजन को delete करना न भूलें.
ध्यान दें: इंजन शुरू करने पर, मॉडल लोड होने में कुछ सेकंड लग सकते हैं.
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();
बातचीत शुरू करना
इंजन शुरू होने के बाद, 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
}
}