File size: 6,536 Bytes
d6ae3bc
 
 
 
 
 
 
 
 
 
d2ae80e
d6ae3bc
 
 
 
 
 
 
d2ae80e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6ae3bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9036e71
98c19a7
d6ae3bc
 
 
 
 
 
98c19a7
 
 
 
 
 
d6ae3bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98c19a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
  AutoModelForCausalLM,
  AutoTokenizer,
  InterruptableStoppingCriteria,
  TextStreamer,
  env,
} from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.2.0";

env.allowLocalModels = false;
env.useBrowserCache = true;
installFetchTelemetry("llm");

let tokenizer = null;
let model = null;
let modelId = "";
let stoppingCriteria = new InterruptableStoppingCriteria();
let currentTurnId = 0;

function installFetchTelemetry(scope) {
  const originalFetch = globalThis.fetch?.bind(globalThis);
  if (!originalFetch || globalThis.__browserSpeakFetchTelemetryInstalled) return;
  globalThis.__browserSpeakFetchTelemetryInstalled = true;
  globalThis.fetch = async (input, init) => {
    const startedAt = performance.now();
    const url = fetchUrl(input);
    const method = String(init?.method || input?.method || "GET").toUpperCase();
    try {
      const response = await originalFetch(input, init);
      self.postMessage({
        type: "network",
        scope,
        method,
        url,
        responseUrl: response.url || url,
        status: response.status,
        ok: response.ok,
        durationMs: performance.now() - startedAt,
      });
      return response;
    } catch (error) {
      self.postMessage({
        type: "network",
        scope,
        method,
        url,
        status: null,
        ok: false,
        durationMs: performance.now() - startedAt,
        error: error.message ?? String(error),
      });
      throw error;
    }
  };
}

function fetchUrl(input) {
  if (typeof input === "string") return input;
  if (input instanceof URL) return input.href;
  return input?.url ?? "";
}

self.onmessage = async (event) => {
  const message = event.data;
  try {
    if (message.type === "load") {
      await load(message);
    } else if (message.type === "generate") {
      currentTurnId = message.turnId;
      await generate(message.messages, message.turnId, {
        sentenceLimit: message.sentenceLimit ?? (message.stopAfterFirstSentence === false ? 0 : 1),
      });
    } else if (message.type === "interrupt") {
      stoppingCriteria.interrupt();
    }
  } catch (error) {
    self.postMessage({ type: "error", message: error.message ?? String(error) });
  }
};

async function load({ model: requestedModelId, device }) {
  modelId = requestedModelId;
  self.postMessage({ type: "status", message: "Loading", mode: "warn" });
  tokenizer = await AutoTokenizer.from_pretrained(modelId, {
    progress_callback: reportProgress("LLM tokenizer"),
  });
  model = await AutoModelForCausalLM.from_pretrained(modelId, {
    device,
    dtype: device === "webgpu" ? "q4f16" : "q4",
    progress_callback: reportProgress("LLM"),
  });
  self.postMessage({ type: "status", message: "Warming", mode: "warn" });
  const inputs = tokenizer("hello");
  await model.generate({ ...inputs, max_new_tokens: 1 });
  self.postMessage({ type: "ready" });
}

function reportProgress(label) {
  return (progress) => {
    if (progress.status === "progress") {
      const pct = Number.isFinite(progress.progress) ? ` ${progress.progress.toFixed(0)}%` : "";
      self.postMessage({ type: "status", message: `${label}${pct}`, mode: "warn" });
    }
  };
}

async function generate(messages, turnId, { sentenceLimit = 1 } = {}) {
  stoppingCriteria = new InterruptableStoppingCriteria();
  self.postMessage({ type: "start", turnId });
  const promptBuildStartedAt = performance.now();
  const promptMessages = normalizeMessages(messages);
  const inputs = tokenizer.apply_chat_template(promptMessages, {
    add_generation_prompt: true,
    enable_thinking: false,
    return_dict: true,
  });
  self.postMessage({
    type: "prompt",
    turnId,
    inputTokens: inputTokenCount(inputs),
    promptBuildMs: performance.now() - promptBuildStartedAt,
  });

  let firstTokenAt = 0;
  let tokenCount = 0;
  let tps = 0;
  let decodedText = "";
  let emittedChars = 0;
  let sentenceStopped = false;
  const startedAt = performance.now();

  const streamer = new TextStreamer(tokenizer, {
    skip_prompt: true,
    skip_special_tokens: true,
    token_callback_function: () => {
      tokenCount += 1;
      firstTokenAt ||= performance.now();
      const elapsed = performance.now() - startedAt;
      if (elapsed > 0) tps = (tokenCount / elapsed) * 1000;
    },
    callback_function: (text) => {
      if (turnId !== currentTurnId || sentenceStopped) return;
      decodedText += text;
      const boundary = sentenceLimit > 0 ? sentenceBoundary(decodedText, sentenceLimit) : -1;
      const emitUntil = boundary > 0 ? boundary : decodedText.length;
      const emitText = decodedText.slice(emittedChars, emitUntil);
      emittedChars = emitUntil;
      if (emitText) {
        self.postMessage({ type: "token", turnId, text: emitText, tps, tokenCount });
      }
      if (boundary > 0) {
        sentenceStopped = true;
        stoppingCriteria.interrupt();
      }
    },
  });

  await model.generate({
    ...inputs,
    max_new_tokens: maxNewTokens(),
    do_sample: false,
    repetition_penalty: 1.08,
    streamer,
    stopping_criteria: stoppingCriteria,
  });
  self.postMessage({ type: "complete", turnId, tokenCount, firstTokenMs: firstTokenAt - startedAt });
}

function maxNewTokens() {
  if (modelId.includes("Qwen3")) return 40;
  if (modelId.includes("135M")) return 64;
  return 48;
}

function sentenceBoundary(text, targetCount = 1) {
  const normalized = text.replace(/\s+/g, " ").trim();
  if (normalized.length < 12) return -1;
  const regex = /[.!?]["')\]]?(?:\s|$)/g;
  let match = null;
  let count = 0;
  while ((match = regex.exec(text))) {
    count += 1;
    if (count >= targetCount) return match.index + match[0].trimEnd().length;
  }
  return -1;
}

function normalizeMessages(messages) {
  if (typeof tokenizer.apply_chat_template === "function") return messages;
  const content = messages.map((message) => `${message.role}: ${message.content}`).join("\n");
  return [{ role: "user", content }];
}

function inputTokenCount(inputs) {
  const inputIds = inputs?.input_ids;
  if (!inputIds) return null;
  if (Array.isArray(inputIds)) {
    return Array.isArray(inputIds[0]) ? inputIds[0].length : inputIds.length;
  }
  if (Array.isArray(inputIds.dims) && inputIds.dims.length > 0) {
    return inputIds.dims[inputIds.dims.length - 1];
  }
  if (Number.isFinite(inputIds.size)) return inputIds.size;
  if (Number.isFinite(inputIds.length)) return inputIds.length;
  if (Number.isFinite(inputIds.data?.length)) return inputIds.data.length;
  return null;
}