ब्राउज़र में JavaScript और TypeScript के लिए, LiteRT-LM का Web API. यह एक शुरुआती झलक है. इसमें WebGPU में टेक्स्ट-इन / टेक्स्ट-आउट की सुविधा काम करती है.
काम करने वाले मॉडल
फ़िलहाल, LiteRT-LM JS API, वेब के साथ काम करने वाले कुछ ही मॉडल के साथ काम करता है.
हम इसे सामान्य .litertlm मॉडल फ़ाइलों के लिए उपलब्ध कराने पर काम कर रहे हैं. हालांकि, फ़िलहाल इन मॉडल के लिए यह सुविधा उपलब्ध है:
gemma-4-E2B-it-web.litertlmसे litert-community/gemma-4-E2B-it-litert-lmgemma-4-E4B-it-web.litertlmसे litert-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 से नया वर्शन इंस्टॉल किया जा सकता है या इसे सीधे सीडीएन से इंपोर्ट किया जा सकता है:
# 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
// 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() इंस्टेंस पर 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
}
}