-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.js
More file actions
738 lines (655 loc) · 26.6 KB
/
base.js
File metadata and controls
738 lines (655 loc) · 26.6 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/**
* USAGE:
*
* node local-llama.js <path to config json>
*
* Example:
* Windows:
* node .\local-llama.js .\testing\model.json
* Unix/Linux/Mac:
* node ./local-llama.js ./testing/model.json
*
* Example (debug mode):
* Windows:
* $env:DEBUG=1; node .\local-llama.js .\testing\model.json
* Unix/Linux/Mac:
* DEBUG=1 node ./local-llama.js ./testing/model.json
*
* Remember in Windows
* Remove-Item Env:DEBUG
*
* JSON File settings example
*
* {
* // the path of the model relative to the json file
* "modelPath": "./model.json",
* "mode": "mistral",
* // standard generation used in roleplay contexts
* "standard": {
* // temperature base
* "temperature": 1.0,
* "maxTokens": 512,
* // dynamic temperature range, if given it will vary temperature between these values
* "dynamicTemperature": [0.8, 1.05],
* // minimum probability for dry run detection
* "minP": 0.025,
* // dry sampler settings
* "dry": {
* "multiplier": 0.8,
* "base": 1.74,
* "length": 5
* },
* // xtc sampler settings (should probably not use both dry and xtc at the same time)
* },
* "analyze": {
* // analysis generation settings
* "temperature": 0.4,
* "topP": 0.8,
* "topK": 40,
* "repeatPenalty": 1.1,
* "frequencyPenalty": 0.0,
* "presencePenalty": 0.0,
* "maxTokens": 512,
* }
* }
*/
import fs from 'fs';
const { LlamaCompletion, getLlama } = await import('node-llama-cpp');
import path from 'path';
/**
* @type {import('node-llama-cpp').LlamaModel}
*/
export let MODEL = /** @type {any} */ (null);
let LLAMA = await getLlama();
export let MODEL_PATH = "";
/**
* @param {string} string
* @returns
*/
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
/**
* @type {{
* modelPath: string;
* mode: "mistral" | "llama3";
* standard: {temperature: number; temperatureRange?: [number, number]; topP?: number; minP?: number; repeatPenalty?: number; frequencyPenalty?: number; presencePenalty?: number; maxTokens: number;},
* analyze: {temperature: number; temperatureRange?: [number, number]; topP?: number; minP?: number; repeatPenalty?: number; frequencyPenalty?: number; presencePenalty?: number; maxTokens: number;},
* }}
*/
let CONFIG = /** @type {any} */ (null);
let CONFIG_PATH = "";
/**
* @param {*} config
*/
function checkConfigValidity(config) {
// implement any additional checks if needed
if (typeof config.maxTokens !== "number") {
throw new Error("Invalid config: maxTokens must be a number");
}
if (typeof config.temperature !== "number") {
throw new Error("Invalid config: temperature must be a number");
}
if (config.temperatureRange !== undefined) {
if (!Array.isArray(config.temperatureRange) || config.temperatureRange.length !== 2 ||
typeof config.temperatureRange[0] !== "number" || typeof config.temperatureRange[1] !== "number") {
throw new Error("Invalid config: temperatureRange must be an array of two numbers");
}
}
if (config.topP !== undefined && typeof config.topP !== "number") {
throw new Error("Invalid config: topP must be a number");
}
if (config.repeatPenalty !== undefined && typeof config.repeatPenalty !== "number") {
throw new Error("Invalid config: repeatPenalty must be a number");
}
if (config.frequencyPenalty !== undefined && typeof config.frequencyPenalty !== "number") {
throw new Error("Invalid config: frequencyPenalty must be a number");
}
if (config.presencePenalty !== undefined && typeof config.presencePenalty !== "number") {
throw new Error("Invalid config: presencePenalty must be a number");
}
if (config.minP !== undefined && typeof config.minP !== "number") {
throw new Error("Invalid config: minP must be a number");
}
if (config.dry !== undefined) {
if (typeof config.dry !== "object") {
throw new Error("Invalid config: dry must be an object");
}
if (typeof config.dry.multiplier !== "number") {
throw new Error("Invalid config: dry.multiplier must be a number");
}
if (typeof config.dry.base !== "number") {
throw new Error("Invalid config: dry.base must be a number");
}
if (typeof config.dry.length !== "number") {
throw new Error("Invalid config: dry.length must be a number");
}
}
if (config.xtc !== undefined) {
if (typeof config.xtc !== "object") {
throw new Error("Invalid config: xtc must be an object");
}
// TODO: add xtc specific checks
}
}
/**
* @type {AbortController | null}
*/
export let CONTROLLER = null;
/**
* @param {string} configPath
* @return {Promise<{endToken: string}>} The end token to use for the current model, based on the config mode
*/
export async function loadConfig(configPath) {
console.log("Loading config:", configPath);
const configContent = await fs.promises.readFile(configPath, 'utf-8');
CONFIG = JSON.parse(configContent);
CONFIG_PATH = configPath;
// check that everything lines up
if (!CONFIG.standard || !CONFIG.analyze) {
console.log(CONFIG);
throw new Error("Invalid config file, missing standard or analyze sections");
}
checkConfigValidity(CONFIG.standard);
checkConfigValidity(CONFIG.analyze);
console.log("Config loaded successfully");
if (CONFIG.mode !== "mistral" && CONFIG.mode !== "llama3" && CONFIG.mode !== undefined) {
throw new Error("Invalid config: mode must be 'mistral' or 'llama3' if provided");
}
if (MODEL_PATH !== CONFIG.modelPath) {
// use relative path from config file
const baseDir = path.dirname(configPath);
const modelFullPath = path.resolve(baseDir, CONFIG.modelPath);
await loadModel(modelFullPath);
}
if (CONFIG.mode === "mistral") {
return { endToken: "</s>" };
} else {
return { endToken: "<|eot_id|>" };
}
}
/**
* @param {string} model
* @returns
*/
async function loadModel(model) {
console.log("Loading model:", model);
if (MODEL_PATH === model && MODEL !== null) {
console.log('Model already loaded');
return;
}
if (MODEL !== null) {
console.log('Unloading previous model');
await MODEL.dispose();
MODEL = /** @type {any} */ (null);
MODEL_PATH = "";
}
console.log('GPU Support:', LLAMA.gpu || 'Unknown');
const LLAMA_MODEL = await LLAMA.loadModel({
modelPath: model,
gpuLayers: "auto",
defaultContextFlashAttention: true,
});
MODEL = LLAMA_MODEL
MODEL_PATH = model;
// Create a simple HTTP server that takes a prompt and returns a response
console.log('Model loaded successfully');
}
const DEBUG = process.env.DEBUG === "1";
console.log("DEBUG mode:", DEBUG);
/**
* @type {import('node-llama-cpp').Token[] | null}
*/
//let ANALYSIS_TOKENS = null;
/**
* @type {string | null}
*/
let ANALYSIS_TEXT = null;
/**
* @param {number} minTemp
* @param {number} maxTemp
*/
function getDynamicTemperature(minTemp, maxTemp) {
return Math.random() * (maxTemp - minTemp) + minTemp;
}
/**
*
* @param {{system: string, userTrail: string}} data
* @param {() => void} onDone
* @param {(error: Error) => void} onError
*/
export async function prepareAnalysis(data, onDone, onError) {
if (!MODEL) {
throw new Error("Model not loaded");
}
if (!CONFIG) {
throw new Error("Config not loaded");
}
if (!data.system || typeof data.system !== "string") {
throw new Error("Invalid system format or missing");
}
if (typeof data.userTrail !== "string") {
throw new Error("Invalid userTrail format");
}
try {
//const context = await MODEL.createContext();
//const contextSequence = context.getSequence();
//contextSequence.eraseContextTokenRanges
// TODO optimize this, for now just retokenize every time
if (CONFIG.mode === "mistral") {
ANALYSIS_TEXT = `<s>[SYSTEM_PROMPT] ${data.system}[/SYSTEM_PROMPT][INST] ${data.userTrail}`;
} else {
ANALYSIS_TEXT = `<|start_header_id|>system<|end_header_id|>\n\n${data.system}<|eot_id><|start_header_id|>user<|end_header_id|>\n\n${data.userTrail}`;
}
if (DEBUG) {
console.log("Prepared analysis text:", ANALYSIS_TEXT);
}
onDone();
} catch (e) {
// @ts-ignore
onError(e);
}
}
/**
*
* @param {{
* question: string;
* stopAt: Array<string>;
* stopAfter: Array<string>;
* maxParagraphs: number;
* maxCharacters: number;
* trail: string | null;
* grammar: string | null;
* }} data
* @param {(v: string) => void} onAnswer
* @param {(err: Error) => void} onError
*/
export async function runQuestion(data, onAnswer, onError) {
if (CONTROLLER) {
throw new Error("Another generation is already in progress");
}
if (!MODEL) {
throw new Error("Model not loaded");
}
if (!CONFIG) {
throw new Error("Config not loaded");
}
if (!ANALYSIS_TEXT) {
throw new Error("Analysis not prepared");
}
if (!data.question || typeof data.question !== "string") {
throw new Error("Invalid question format");
}
if (!Array.isArray(data.stopAt)) {
throw new Error("Invalid stopAt format");
}
if (!Array.isArray(data.stopAfter)) {
throw new Error("Invalid stopAfter format");
}
if (typeof data.maxParagraphs !== "number" || isNaN(data.maxParagraphs) || data.maxParagraphs < 0) {
throw new Error("Invalid maxParagraphs format");
}
if (typeof data.maxCharacters !== "number" || isNaN(data.maxCharacters) || data.maxCharacters < 0) {
throw new Error("Invalid maxCharacters format");
}
if (data.trail !== null && typeof data.trail !== "string") {
throw new Error("Invalid trail format");
}
if (data.grammar !== null && typeof data.grammar !== "string") {
throw new Error("Invalid grammar format");
}
const regexStopAfter = data.stopAfter.map(s => new RegExp(`(^|[.,;])\\s*${escapeRegExp(s)}\\s*([.,;]|$)`, 'i'));
let prompt = "";
if (CONFIG.mode === "mistral") {
prompt = ANALYSIS_TEXT + "\n\n" + data.question + `\n[/INST]\n\n` + (data.trail || "");
} else {
prompt = ANALYSIS_TEXT + "\n" + data.question + `\n<|start_header_id|>assistant<|end_header_id|>\n\n` + (data.trail || "");
}
let context = null
let completion = null;
let answer = "";
CONTROLLER = new AbortController();
try {
const grammar = data.grammar ? await LLAMA.createGrammar({
grammar: data.grammar,
}) : undefined;
// Create context and completion for raw text
context = await MODEL.createContext();
completion = new LlamaCompletion({
contextSequence: context.getSequence(),
});
const CONFIG_TO_USE = data.gear === "cardtype-gen" ? CONFIG.standard : CONFIG.analyze;
const basicConfig = {
temperature: CONFIG_TO_USE.temperature,
topP: CONFIG_TO_USE.topP,
minP: CONFIG_TO_USE.minP,
repeatPenalty: {
penalty: CONFIG_TO_USE.repeatPenalty,
frequencyPenalty: CONFIG_TO_USE.frequencyPenalty,
presencePenalty: CONFIG_TO_USE.presencePenalty,
},
customStopTriggers: (CONFIG.mode === "mistral" ? ["</s>", "[INST]"] : ["<|eot_id|>", "<|start_header_id|>"]).concat(data.stopAt || []),
maxTokens: CONFIG_TO_USE.maxTokens || 512,
}
if (CONFIG_TO_USE.temperatureRange) {
basicConfig.temperature = getDynamicTemperature(CONFIG_TO_USE.temperatureRange[0], CONFIG_TO_USE.temperatureRange[1]);
}
if (typeof data.maxParagraphs === "number" && DEBUG) {
console.log("Max paragraphs limit set to:", data.maxParagraphs);
}
if (typeof data.maxCharacters === "number" && DEBUG) {
console.log("Max characters limit set to:", data.maxCharacters);
}
// TODO add XTC and dry sampling options from config
let accumulatedText = "";
if (DEBUG) {
console.log("Generation config:", basicConfig);
console.log("Prompt:", prompt);
console.log("Using grammar:", data.grammar);
}
await completion.generateCompletion(prompt, {
...basicConfig,
signal: CONTROLLER.signal,
stopOnAbortSignal: true,
grammar,
onTextChunk(textSrc) {
try {
const text = textSrc;
accumulatedText += text;
if (DEBUG) {
// use this weird character to denote token boundaries
process.stdout.write(text + "§");
}
if (typeof data.maxParagraphs === "number" && data.maxParagraphs > 0) {
// For the non prototype this can be optimized better but for now it's fine
// count paragraphs
let paragraphCount = 0;
for (let i = 0; i < accumulatedText.length; i++) {
if (accumulatedText[i] === '\n' && accumulatedText[i + 1] === '\n') {
paragraphCount += 1;
}
//console.log("Current paragraph count:", paragraphCount);
// this should hit exactly at paragraph end
if (paragraphCount >= data.maxParagraphs) {
//console.log("Max paragraphs reached:", paragraphCount, "stopping completion early.");
// I think newlines are whole tokens, but just in case the text contains some text too
const potentialPartBeforeNew = text.split("\n")[0]
if (potentialPartBeforeNew.length > 0) {
answer += potentialPartBeforeNew;
}
console.log("\nAborting completion due to max paragraphs limit.");
CONTROLLER?.abort();
CONTROLLER = null;
return;
}
}
}
if (typeof data.maxCharacters === "number" && data.maxCharacters > 0) {
const characterCount = accumulatedText.length;
//console.log("Current character count:", characterCount);
if (characterCount >= data.maxCharacters) {
//console.log("Trying to abort but no paragraph end found yet.");
// let's find if our text is finally finishing a paragraph
if (text.indexOf('\n') !== -1) {
//console.log("Max characters reached:", characterCount, "stopping completion at this paragraph end.");
const potentialPartBeforeNew = text.split("\n")[0]
if (potentialPartBeforeNew.length > 0) {
answer += potentialPartBeforeNew;
}
console.log("\nAborting completion due to max characters limit.");
CONTROLLER?.abort();
CONTROLLER = null;
return;
}
}
}
answer += text;
if (regexStopAfter.length > 0) {
for (const stopRegex of regexStopAfter) {
if (stopRegex.test(answer)) {
console.log("\nAborting completion due to stopAfter trigger matched:", stopRegex);
CONTROLLER?.abort();
CONTROLLER = null;
return;
}
}
}
} catch (e) {
// @ts-ignore
console.log("\nError in onToken callback:", e.message);
throw e;
}
}
});
} catch (e) {
console.log("");
// @ts-ignore
console.log(e.message);
// @ts-ignore
onError(e);
}
if (context) {
await context.dispose();
context = null;
}
console.log("");
// For the love of god stop adding newlines at the end of the answer
while (answer[answer.length - 1] === '\n') {
answer = answer.slice(0, -1);
}
onAnswer(answer);
CONTROLLER = null;
}
/**
*
* @param {{messages: Array<{role: string, content: string}>, stopAt: Array<string>, stopAfter: Array<string>, maxParagraphs: number, maxCharacters: number, startCountingFromToken: string | null, trail: string | null, gear: string}} data
* @param {(text: string) => void} onToken
* @param {() => void} onDone
* @param {(error: Error) => void} onError
*/
export async function generateCompletion(data, onToken, onDone, onError) {
if (CONTROLLER) {
throw new Error("Another generation is already in progress");
}
if (!MODEL) {
throw new Error("Model not loaded");
}
if (!CONFIG) {
throw new Error("Config not loaded");
}
if (!Array.isArray(data.messages)) {
throw new Error("Invalid messages format");
}
if (!Array.isArray(data.stopAt)) {
throw new Error("Invalid stopAt format");
} else if (data.stopAt.some(s => typeof s !== "string")) {
throw new Error("Invalid stopAt format, all stops must be strings");
}
if (typeof data.maxParagraphs !== "number" || isNaN(data.maxParagraphs) || data.maxParagraphs < 0) {
throw new Error("Invalid maxParagraphs format");
}
if (typeof data.maxCharacters !== "number" || isNaN(data.maxCharacters) || data.maxCharacters < 0) {
throw new Error("Invalid maxCharacters format");
}
if (data.startCountingFromToken !== null && typeof data.startCountingFromToken !== "string") {
throw new Error("Invalid startCountingFromToken format");
}
if (data.trail !== null && typeof data.trail !== "string") {
throw new Error("Invalid trail format");
}
if (!Array.isArray(data.stopAfter)) {
throw new Error("Invalid stopAfter format");
}
if (data.grammar !== null && typeof data.grammar !== "string") {
throw new Error("Invalid grammar format");
}
// clear previous analysis
ANALYSIS_TEXT = null;
let prompt = "";
if (CONFIG.mode === "mistral") {
prompt += "<s>";
}
for (const msg of data.messages) {
if (typeof msg.content !== "string") {
throw new Error("Invalid message content");
} else if (typeof msg.role !== "string") {
throw new Error("Invalid message role");
} else if (!["user", "assistant", "system"].includes(msg.role)) {
throw new Error("Invalid message role: " + msg.role);
}
if (CONFIG.mode === "mistral") {
if (msg.role === "system") {
prompt += `[SYSTEM_PROMPT] ${msg.content}[/SYSTEM_PROMPT][INST]`;
} else {
prompt += "\n\n" + msg.content
}
} else {
prompt += `<|start_header_id|>${msg.role}<|end_header_id|>\n\n${msg.content}<|eot_id>`;
}
}
if (CONFIG.mode === "mistral") {
prompt += "[/INST]\n\n";
} else {
prompt += "\n<|start_header_id|>assistant<|end_header_id|>\n\n";
}
if (data.trail) {
prompt += data.trail;
}
const grammar = data.grammar ? await LLAMA.createGrammar({
grammar: data.grammar,
}) : undefined;
let context = null
let completion = null;
CONTROLLER = new AbortController();
try {
// Create context and completion for raw text
context = await MODEL.createContext();
completion = new LlamaCompletion({
contextSequence: context.getSequence()
});
const basicConfig = {
temperature: CONFIG.standard.temperature,
topP: CONFIG.standard.topP,
minP: CONFIG.standard.minP,
repeatPenalty: {
penalty: CONFIG.standard.repeatPenalty,
frequencyPenalty: CONFIG.standard.frequencyPenalty,
presencePenalty: CONFIG.standard.presencePenalty,
},
customStopTriggers: (CONFIG.mode === "mistral" ? ["</s>", "[INST]"] : ["<|eot_id|>", "<|start_header_id|>"]).concat(data.stopAt || []),
maxTokens: CONFIG.standard.maxTokens || 512,
}
if (CONFIG.standard.temperatureRange) {
basicConfig.temperature = getDynamicTemperature(CONFIG.standard.temperatureRange[0], CONFIG.standard.temperatureRange[1]);
}
// TODO add XTC and dry sampling options from config
if (typeof data.maxParagraphs === "number" && DEBUG) {
console.log("Max paragraphs limit set to:", data.maxParagraphs);
}
if (typeof data.maxCharacters === "number" && DEBUG) {
console.log("Max characters limit set to:", data.maxCharacters);
}
let hasBegunCounting = data.startCountingFromToken === null ? true : false;
let accumulatedText = "";
let accumulatedTextForCounting = "";
if (DEBUG) {
console.log("Generation config:", basicConfig);
console.log("Prompt:", prompt);
}
const regexStopAfter = data.stopAfter.map(s => new RegExp(`(^|[.,;])\\s*${escapeRegExp(s)}\\s*([.,;]|$)`, 'i'));
await completion.generateCompletion(prompt, {
...basicConfig,
signal: CONTROLLER.signal,
stopOnAbortSignal: true,
grammar,
onTextChunk(textSrc) {
try {
const text = textSrc;
accumulatedText += text;
if (DEBUG) {
// use this weird character to denote token boundaries
process.stdout.write(text + "§");
}
if (!hasBegunCounting && data.startCountingFromToken && accumulatedText.includes(data.startCountingFromToken)) {
hasBegunCounting = true;
}
// Always accumulate text if we need to track limits
if (hasBegunCounting) {
accumulatedTextForCounting += text;
}
if (typeof data.maxParagraphs === "number" && data.maxParagraphs > 0) {
// For the non prototype this can be optimized better but for now it's fine
// count paragraphs
let paragraphCount = 0;
for (let i = 0; i < accumulatedTextForCounting.length; i++) {
if (accumulatedTextForCounting[i] === '\n' && accumulatedTextForCounting[i + 1] === '\n') {
paragraphCount += 1;
}
//console.log("Current paragraph count:", paragraphCount);
// this should hit exactly at paragraph end
if (paragraphCount >= data.maxParagraphs) {
//console.log("Max paragraphs reached:", paragraphCount, "stopping completion early.");
// I think newlines are whole tokens, but just in case the text contains some text too
const potentialPartBeforeNew = text.split("\n")[0]
if (potentialPartBeforeNew.length > 0) {
onToken(potentialPartBeforeNew);
}
console.log("\nAborting completion due to max paragraphs limit.");
CONTROLLER?.abort();
CONTROLLER = null;
return;
}
}
}
if (typeof data.maxCharacters === "number" && data.maxCharacters > 0) {
const characterCount = accumulatedText.length;
//console.log("Current character count:", characterCount);
if (characterCount >= data.maxCharacters) {
//console.log("Trying to abort but no paragraph end found yet.");
// let's find if our text is finally finishing a paragraph
if (text.indexOf('\n') !== -1) {
//console.log("Max characters reached:", characterCount, "stopping completion at this paragraph end.");
const potentialPartBeforeNew = text.split("\n")[0]
if (potentialPartBeforeNew.length > 0) {
onToken(potentialPartBeforeNew);
}
console.log("\nAborting completion due to max characters limit.");
CONTROLLER?.abort();
CONTROLLER = null;
return;
}
}
}
onToken(text);
if (regexStopAfter.length > 0) {
for (const stopRegex of regexStopAfter) {
if (stopRegex.test(accumulatedTextForCounting)) {
console.log("\nAborting completion due to stopAfter trigger matched:", stopRegex);
CONTROLLER?.abort();
CONTROLLER = null;
return;
}
}
}
} catch (e) {
// @ts-ignore
console.log("\nError in onToken callback:", e.message);
throw e;
}
}
});
} catch (e) {
console.log("");
// @ts-ignore
console.log(e.message);
// @ts-ignore
onError(e);
}
if (context) {
await context.dispose();
context = null;
}
console.log("");
onDone();
CONTROLLER = null;
}