Web API của LiteRT-LM cho JavaScript và TypeScript trong trình duyệt. Đây là bản xem trước ban đầu hỗ trợ văn bản đầu vào / đầu ra chạy trong WebGPU.
Giới thiệu
Sau đây là một ứng dụng trò chuyện REPL mẫu được tạo bằng 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>
Bắt đầu
LiteRT-LM có sẵn dưới dạng một gói npm. Bạn có thể cài đặt phiên bản mới nhất từ npm hoặc nhập trực tiếp từ 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';
Khởi chạy Công cụ
Engine là điểm truy cập vào API. Lớp này xử lý việc tải mô hình, tạo phiên và quản lý tài nguyên. Hãy nhớ delete công cụ để giải phóng tài nguyên khi không cần đến mô hình nữa.
Lưu ý: Quá trình khởi tạo công cụ có thể mất vài giây để tải mô hình.
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();
Tạo cuộc trò chuyện
Sau khi khởi động công cụ, hãy tạo một thực thể Conversation. Bạn có thể cung cấp một ConversationConfig để tuỳ chỉnh hành vi của nó.
const conversation = await engine.createConversation({
preface: {
messages: [
{role: 'system', content: 'You are a helpful assistant'}
]
}
});
conversation.sendMessage({
role: 'user',
content: 'Write a poem',
});
Gửi tin nhắn
Bạn có thể gửi tin nhắn có hoặc không có nội dung phát trực tiếp.
Ví dụ không phát trực tuyến
// 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: '...'});
Ví dụ về hoạt động phát trực tuyến
// 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);
}
}
}
Huỷ tạo
Bạn có thể huỷ một quá trình tạo đang diễn ra một cách rõ ràng bằng cách gọi cancel() trên thực thể Conversation:
// Cancel any ongoing generation
conversation.cancel();
Nếu bạn đang truyền trực tuyến câu trả lời, việc thoát sớm khỏi vòng lặp for await...of (chẳng hạn như với break) cũng sẽ tự động huỷ quá trình tạo đang diễn ra:
for await (const chunk of stream) {
if (shouldStop()) {
break; // Cancels the stream and underlying generation
}
}