LiteRT-LM 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 هي نقطة الدخول إلى واجهة برمجة التطبيقات. يتولّى هذا الصف معالجة تحميل النماذج وإنشاء الجلسات وإدارة الموارد. تذكَّر 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);
    }
  }
}

إلغاء عملية الإنشاء

يمكنك إلغاء عملية إنشاء مستمرة بشكل صريح من خلال استدعاء 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
  }
}