API-ja e Uebit LiteRT-LM

API-ja Web e LiteRT-LM për JavaScript dhe TypeScript në shfletues. Ky është një parapamje e hershme që mbështet hyrjen/daljen e tekstit që ekzekutohet në WebGPU.

Hyrje

Ja një shembull i një aplikacioni chati REPL i ndërtuar me API-në JavaScript:

<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>

Fillimi

LiteRT-LM është i disponueshëm si një paketë npm. Mund ta instaloni versionin më të fundit nga npm ose ta importoni direkt nga një 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';

Inicializoni Motorin

Engine është pika e hyrjes në API. Ai merret me ngarkimin e modelit, krijimin e sesionit dhe menaxhimin e burimeve. Mos harroni ta delete motorin për të liruar burimet kur modeli nuk është më i nevojshëm.

Shënim: Nisja e motorit mund të zgjasë disa sekonda për të ngarkuar modelin.

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();

Krijo një bisedë

Pasi motori të jetë inicializuar, krijoni një instancë Conversation . Mund të ofroni një ConversationConfig për të personalizuar sjelljen e tij.

const conversation = await engine.createConversation({
  preface: {
    messages: [
      {role: 'system', content: 'You are a helpful assistant'}
    ]
  }
});

conversation.sendMessage({
  role: 'user',
  content: 'Write a poem',
});

Dërgo Mesazhe

Mund të dërgoni mesazhe me ose pa transmetim.

Shembull jo-transmetues

// 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: '...'});

Shembull transmetimi

// 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);
    }
  }
}

Anulo Gjenerimin

Ju mund të anuloni një gjenerim në vazhdim në mënyrë të qartë duke thirrur cancel() në instancën e Conversation :

// Cancel any ongoing generation
conversation.cancel();

Nëse po transmetoni përgjigjen, dalja herët nga cikli for await...of (si p.sh. me break ) do të anulojë automatikisht edhe gjenerimin në vazhdim:

for await (const chunk of stream) {
  if (shouldStop()) {
    break; // Cancels the stream and underlying generation
  }
}