Web API ของ LiteRT-LM สำหรับ JavaScript และ TypeScript ในเบราว์เซอร์ นี่เป็นเวอร์ชันตัวอย่างแรกที่รองรับการรับข้อความเข้า / ส่งข้อความออกที่ทำงานใน WebGPU
บทนำ
ตัวอย่างแอปแชท 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({ 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
Engine คือจุดเริ่มต้นของ API ซึ่งจะจัดการการโหลดโมเดล การสร้างเซสชัน และการจัดการทรัพยากร อย่าลืม delete Engine เพื่อปล่อยทรัพยากรเมื่อไม่ต้องการใช้โมเดลอีกต่อไป
หมายเหตุ: การเริ่มต้นใช้งาน Engine อาจใช้เวลาหลายวินาทีในการโหลดโมเดล
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();
สร้างการสนทนา
เมื่อเริ่มต้นใช้งาน Engine แล้ว ให้สร้างอินสแตนซ์ 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
}
}