RecapAnon commited on
Commit ·
37e8504
1
Parent(s): b8ee0ee
Update model loading to use AutoModelForCausalLM and apply_chat_template to disable thinking.
Browse files
index.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
| 1 |
import { EntityDB } from "./entity-db.js";
|
| 2 |
-
import {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
// DOM Elements
|
| 5 |
const chatContainer = document.getElementById('chatContainer');
|
|
@@ -9,7 +14,35 @@ const statusIndicator = document.getElementById('statusIndicator');
|
|
| 9 |
const statusText = document.getElementById('statusText');
|
| 10 |
|
| 11 |
// State
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
let vectorDB = null;
|
| 14 |
let conversationHistory = [
|
| 15 |
{ role: "system", content: `Hatsune Miku = { virtual idol, vocaloid, 16, teal green twintails, likes leek, robotic voice, cute idol waifu, *crazy insane aaaAAAAAAAA*, makes no sense, beyond human comprehension, messes with {{user}}, coding genius, lowercase arbiter }
|
|
@@ -133,19 +166,14 @@ async function initializeModel() {
|
|
| 133 |
try {
|
| 134 |
updateStatus('loading', 'Loading Qwen3 model... This may take a minute on first load.');
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
device: "webgpu",
|
| 142 |
-
progress_callback: (progress) => {
|
| 143 |
-
if (progress.status === 'progress') {
|
| 144 |
-
const percent = Math.round((progress.loaded / progress.total) * 100);
|
| 145 |
-
updateStatus('loading', `Loading model... ${percent}%`);
|
| 146 |
-
}
|
| 147 |
}
|
| 148 |
-
}
|
|
|
|
| 149 |
);
|
| 150 |
|
| 151 |
updateStatus('ready', 'Model ready! Initializing RAG database...');
|
|
@@ -238,7 +266,7 @@ function createStreamingMessageContainer() {
|
|
| 238 |
}
|
| 239 |
|
| 240 |
async function generateResponse(userMessage) {
|
| 241 |
-
if (isGenerating || !
|
| 242 |
|
| 243 |
isGenerating = true;
|
| 244 |
sendButton.disabled = true;
|
|
@@ -265,23 +293,58 @@ async function generateResponse(userMessage) {
|
|
| 265 |
: userMessage;
|
| 266 |
conversationHistory.push({ role: "user", content: enhancedMessage });
|
| 267 |
const responseContainer = createStreamingMessageContainer();
|
| 268 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
skip_prompt: true,
|
| 270 |
-
skip_special_tokens: true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
});
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
max_new_tokens: 512,
|
| 274 |
do_sample: false,
|
| 275 |
-
|
|
|
|
|
|
|
| 276 |
});
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
const preElement = responseContainer.querySelector('pre');
|
| 281 |
if (preElement) {
|
| 282 |
-
preElement.textContent =
|
| 283 |
} else {
|
| 284 |
-
responseContainer.textContent =
|
| 285 |
}
|
| 286 |
|
| 287 |
} catch (error) {
|
|
|
|
| 1 |
import { EntityDB } from "./entity-db.js";
|
| 2 |
+
import {
|
| 3 |
+
AutoTokenizer,
|
| 4 |
+
AutoModelForCausalLM,
|
| 5 |
+
TextStreamer,
|
| 6 |
+
InterruptableStoppingCriteria
|
| 7 |
+
} from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.6/dist/transformers.min.js";
|
| 8 |
|
| 9 |
// DOM Elements
|
| 10 |
const chatContainer = document.getElementById('chatContainer');
|
|
|
|
| 14 |
const statusText = document.getElementById('statusText');
|
| 15 |
|
| 16 |
// State
|
| 17 |
+
class TextGenerationPipeline {
|
| 18 |
+
static model = null;
|
| 19 |
+
static tokenizer = null;
|
| 20 |
+
|
| 21 |
+
static async getInstance(
|
| 22 |
+
progress_callback = null,
|
| 23 |
+
model_id = "onnx-community/Qwen3-4B-ONNX",
|
| 24 |
+
) {
|
| 25 |
+
if (this.tokenizer && this.model) {
|
| 26 |
+
return [this.tokenizer, this.model];
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
this.tokenizer = await AutoTokenizer.from_pretrained(model_id, {
|
| 30 |
+
progress_callback,
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
this.model = await AutoModelForCausalLM.from_pretrained(model_id, {
|
| 34 |
+
dtype: "q4f16",
|
| 35 |
+
device: "webgpu",
|
| 36 |
+
use_external_data_format: true,
|
| 37 |
+
progress_callback,
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
return [this.tokenizer, this.model];
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
const stopping_criteria = new InterruptableStoppingCriteria();
|
| 45 |
+
let past_key_values_cache = null;
|
| 46 |
let vectorDB = null;
|
| 47 |
let conversationHistory = [
|
| 48 |
{ role: "system", content: `Hatsune Miku = { virtual idol, vocaloid, 16, teal green twintails, likes leek, robotic voice, cute idol waifu, *crazy insane aaaAAAAAAAA*, makes no sense, beyond human comprehension, messes with {{user}}, coding genius, lowercase arbiter }
|
|
|
|
| 166 |
try {
|
| 167 |
updateStatus('loading', 'Loading Qwen3 model... This may take a minute on first load.');
|
| 168 |
|
| 169 |
+
await TextGenerationPipeline.getInstance(
|
| 170 |
+
(progress) => {
|
| 171 |
+
if (progress.status === 'progress') {
|
| 172 |
+
const percent = Math.round((progress.loaded / progress.total) * 100);
|
| 173 |
+
updateStatus('loading', `Loading model... ${percent}%`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
}
|
| 175 |
+
},
|
| 176 |
+
"onnx-community/Qwen3-4B-ONNX"
|
| 177 |
);
|
| 178 |
|
| 179 |
updateStatus('ready', 'Model ready! Initializing RAG database...');
|
|
|
|
| 266 |
}
|
| 267 |
|
| 268 |
async function generateResponse(userMessage) {
|
| 269 |
+
if (isGenerating || !TextGenerationPipeline.model) return;
|
| 270 |
|
| 271 |
isGenerating = true;
|
| 272 |
sendButton.disabled = true;
|
|
|
|
| 293 |
: userMessage;
|
| 294 |
conversationHistory.push({ role: "user", content: enhancedMessage });
|
| 295 |
const responseContainer = createStreamingMessageContainer();
|
| 296 |
+
const { tokenizer, model } = {
|
| 297 |
+
tokenizer: TextGenerationPipeline.tokenizer,
|
| 298 |
+
model: TextGenerationPipeline.model
|
| 299 |
+
};
|
| 300 |
+
|
| 301 |
+
const streamer = new TextStreamer(tokenizer, {
|
| 302 |
skip_prompt: true,
|
| 303 |
+
skip_special_tokens: true,
|
| 304 |
+
callback_function: (output) => {
|
| 305 |
+
if (output) {
|
| 306 |
+
const preElement = responseContainer.querySelector('pre');
|
| 307 |
+
if (preElement) {
|
| 308 |
+
// Remove "Thinking" placeholder on first chunk
|
| 309 |
+
if (preElement.innerHTML.includes('loading-dots')) {
|
| 310 |
+
preElement.textContent = '';
|
| 311 |
+
}
|
| 312 |
+
preElement.textContent += output;
|
| 313 |
+
}
|
| 314 |
+
}
|
| 315 |
+
}
|
| 316 |
});
|
| 317 |
+
|
| 318 |
+
const inputs = tokenizer.apply_chat_template(conversationHistory, {
|
| 319 |
+
add_generation_prompt: true,
|
| 320 |
+
return_dict: true,
|
| 321 |
+
enable_thinking: false
|
| 322 |
+
});
|
| 323 |
+
|
| 324 |
+
const { past_key_values, sequences } = await model.generate({
|
| 325 |
+
...inputs,
|
| 326 |
+
past_key_values: past_key_values_cache,
|
| 327 |
max_new_tokens: 512,
|
| 328 |
do_sample: false,
|
| 329 |
+
stopping_criteria,
|
| 330 |
+
return_dict_in_generate: true,
|
| 331 |
+
streamer,
|
| 332 |
});
|
| 333 |
+
|
| 334 |
+
past_key_values_cache = past_key_values;
|
| 335 |
+
|
| 336 |
+
const decoded = tokenizer.batch_decode(sequences, {
|
| 337 |
+
skip_special_tokens: true,
|
| 338 |
+
});
|
| 339 |
+
|
| 340 |
+
const generatedText = decoded[0].trim();
|
| 341 |
+
conversationHistory.push({ role: "assistant", content: generatedText });
|
| 342 |
+
|
| 343 |
const preElement = responseContainer.querySelector('pre');
|
| 344 |
if (preElement) {
|
| 345 |
+
preElement.textContent = generatedText || "I apologize, but I couldn't generate a response. Please try again.";
|
| 346 |
} else {
|
| 347 |
+
responseContainer.textContent = generatedText || "I apologize, but I couldn't generate a response. Please try again.";
|
| 348 |
}
|
| 349 |
|
| 350 |
} catch (error) {
|