-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
522 lines (444 loc) · 17.8 KB
/
index.js
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
import { extension_settings } from '../../../extensions.js';
import { callPopup, getRequestHeaders, online_status, saveSettingsDebounced } from '../../../../script.js';
import { textgen_types, textgenerationwebui_settings } from '../../../textgen-settings.js';
import { findSecret } from '../../../secrets.js';
import EventSourceStream from '../../../sse-stream.js';
const extensionName = 'ST-tabbyAPI-loader';
// Used for settings
const settingsName = 'tabbyLoader';
const extensionFolderPath = `scripts/extensions/third-party/${extensionName}`;
let extensionSettings = extension_settings[settingsName];
const defaultSettings = {};
// TODO: Make a common tabbyRequest function
// Cached models list
let models = [];
let draftModels = [];
const cache_mode = {
FP16: 0,
Q4: 1,
Q6: 2,
Q8: 3,
}
// From https://stackoverflow.com/questions/9907419/how-to-get-a-key-in-a-javascript-object-by-its-value
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
// Check if user is connected to TabbyAPI
function verifyTabby(logError = true) {
const result = online_status !== 'no_connection' || textgenerationwebui_settings.type === textgen_types.TABBY;
if (!result && logError) {
toastr.error('TabbyLoader: Please connect to a TabbyAPI instance to use this extension');
}
return result;
}
// Fetch a cleaned URL
// Use the override if specified
function getTabbyURL() {
const apiUrl = $('#tabby_api_url_text').val();
let url = extensionSettings?.urlOverride ? extensionSettings.urlOverride : apiUrl;
if (extensionSettings?.useProxy) {
url = `/proxy/${url}`;
}
return url.replace(/\/$/, '');
}
// Fetch the TabbyAPI admin key if present
// TODO: Add check to see if the key has admin permissions
async function getTabbyAuth() {
// In localStorage? Return it.
let authToken = localStorage.getItem('Tabby_Admin');
// Not in localStorage? Try secrets
if (!authToken) {
console.warn('TabbyLoader: Could not find your TabbyAPI admin key in localStorage, attempting to fetch from secrets...');
try {
authToken = await findSecret('api_key_tabby');
} catch (error) {
console.error(`TabbyLoader: ${error}`);
console.error('Admin key error: Please make sure allowKeysExposure is true in config.conf and an API key is set for TabbyAPI.');
}
}
// If a failure on both fronts
if (!authToken) {
console.error(
'TabbyLoader: Admin key not found.',
'Please make sure allowKeysExposure is true in config.conf if fetching from secrets.',
);
}
return authToken;
}
// Fetch the model list for autocomplete population
async function fetchModels() {
if (!verifyTabby(false)) {
console.error('TabbyLoader: Could not connect to TabbyAPI');
return;
}
var models, draftModels;
// Remove trailing URL slash
const apiUrl = getTabbyURL();
try {
const authToken = await getTabbyAuth();
if (!authToken) {
return;
}
const response = await fetch(`${apiUrl}/v1/model/list`, {
headers: {
'X-api-key': authToken,
},
});
if (response.ok) {
models = await response.json();
} else {
console.error(`Request to /v1/model/list failed with a statuscode of ${response.status}:\n${response.statusText}`);
return [];
}
const draftModelListResponse = await fetch(`${apiUrl}/v1/model/draft/list`, {
headers: {
'X-api-key': authToken,
},
});
if (draftModelListResponse.ok) {
draftModels = await draftModelListResponse.json();
} else {
console.error(`Request to /v1/model/list failed with a statuscode of ${response.status}:\n${response.statusText}`);
return [];
}
return [models.data.map((e) => e.id), draftModels.data.map((e) => e.id)];
} catch (error) {
console.error(error);
return [];
}
}
// This function is called when the button is clicked
async function onLoadModelClick() {
if (!verifyTabby()) {
return;
}
const modelValue = $('#model_list').val();
const draftModelValue = $('#draft_model_list').val();
if (!modelValue || !models.includes(modelValue)) {
toastr.error('TabbyLoader: Please make sure the model name is spelled correctly before loading!');
return;
}
if (draftModelValue !== '' && !draftModels.includes(draftModelValue)) {
toastr.error('TabbyLoader: Please make sure the draft model name is spelled correctly before loading!');
return;
}
const tabbyURL = getTabbyURL();
const body = {
name: modelValue,
max_seq_len: Number(extensionSettings?.modelParams?.maxSeqLen) || 0,
cache_size: Number(extensionSettings?.modelParams?.cacheSize) || null,
max_batch_size: Number(extensionSettings?.modelParams?.maxBatchSize) || null,
rope_scale: Number(extensionSettings?.modelParams?.ropeScale) || null,
rope_alpha: Number(extensionSettings?.modelParams?.ropeAlpha) || null,
gpu_split_auto: extensionSettings?.modelParams?.gpuSplitAuto,
cache_mode: extensionSettings?.modelParams?.cacheMode,
fasttensors: extensionSettings?.modelParams?.fasttensors,
};
if (draftModelValue) {
body.draft = {
draft_model_name: draftModelValue,
draft_rope_scale: Number(extensionSettings?.modelParams?.draft.draft_ropeAlpha) || null,
draft_rope_alpha: Number(extensionSettings?.modelParams?.draft.draft_ropeScale) || null,
};
}
if (!body.gpu_split_auto) {
// TODO: Add a check for an empty array here
const gpuSplit = extensionSettings?.modelParams?.gpuSplit;
if (Array.isArray(gpuSplit) && gpuSplit?.length > 0) {
body['gpu_split'] = gpuSplit;
} else {
console.error(`TabbyLoader: GPU split ${gpuSplit} is invalid. Set to auto or adjust your parameters!`);
toastr.error('TabbyLoader: Invalid GPU split. Set GPU split to auto or adjust your parameters');
return;
}
}
const authToken = await getTabbyAuth();
if (!authToken) {
// eslint-disable-next-line
toastr.error(
'TabbyLoader: Admin key not found. Please provide one in SillyTavern\'s model settings or in the extension box.'
);
return;
}
try {
const response = await fetch(`${tabbyURL}/v1/model/load`, {
method: 'POST',
headers: {
...getRequestHeaders(),
'X-admin-key': authToken,
},
body: JSON.stringify(body),
});
if (response.ok) {
const eventStream = new EventSourceStream();
response.body.pipeThrough(eventStream);
const reader = eventStream.readable.getReader();
const progressContainer = $('#loading_progress_container').hide();
progressContainer.show();
let soFar = 0;
let times;
draftModelValue ? times = 2 : times = 1;
while (true) {
const { value, done } = await reader.read();
console.log(soFar, times);
if (done && soFar === times) break;
const packet = JSON.parse(value.data);
if (packet.error) {
throw new Error(packet.error.message)
}
const numerator = parseInt(packet.module) ?? 0;
const denominator = parseInt(packet.modules) ?? 0;
const percent = numerator / denominator * 100;
if (packet.status === 'finished') {
if (soFar === times - 1) {
progressContainer.hide();
toastr.info('TabbyLoader: Model loaded');
} else {
$('#loading_progressbar').progressbar('value', 0);
toastr.info('TabbyLoader: Draft Model loaded');
}
soFar++;
} else {
$('#loading_progressbar').progressbar('value', Math.round(percent ?? 0));
}
}
} else {
const responseJson = await response.json();
console.error('TabbyLoader: Could not load the model because:\n', responseJson?.detail ?? response.statusText);
toastr.error('TabbyLoader: Could not load the model. Please check the JavaScript or TabbyAPI console for details.');
}
} catch (error) {
console.error('TabbyLoader: Could not load the model because:\n', error);
toastr.error('Could not load the model. Please check the TabbyAPI console for details.');
} finally {
$('#loading_progressbar').progressbar('value', 0);
$('#loading_progress_container').hide();
}
}
async function onUnloadModelClick() {
verifyTabby();
const tabbyURL = getTabbyURL();
const authToken = await getTabbyAuth();
if (!authToken) {
return;
}
const response = await fetch(`${tabbyURL}/v1/model/unload`, {
method: 'POST',
headers: {
...getRequestHeaders(),
'X-admin-key': authToken,
},
});
if (response.ok) {
toastr.info('TabbyLoader: Model unloaded');
} else {
const responseJson = await response.json();
console.error('TabbyLoader: Could not unload the model because:\n', responseJson?.detail ?? response.statusText);
toastr.error('Could not unload the model. Please check the JavaScript or TabbyAPI console for details.');
}
}
async function onParameterEditorClick() {
const parameterHtml = $(await $.get(`${extensionFolderPath}/modelParameters.html`));
parameterHtml
.find('input[name="max_seq_len"]')
.val(extensionSettings?.modelParams?.maxSeqLen ?? 4096);
parameterHtml
.find('input[name="cache_size"]')
.val(extensionSettings?.modelParams?.cacheSize ?? 'Max Seq Len');
parameterHtml
.find('input[name="max_batch_size"]')
.val(extensionSettings?.modelParams?.maxBatchSize ?? 'Auto');
parameterHtml
.find('input[name="fasttensors"]')
.prop('checked', extensionSettings?.modelParams?.fasttensors ?? false);
parameterHtml
.find('select[name="cache_mode_select"]')
.val(cache_mode[extensionSettings?.modelParams?.cacheMode ?? "FP16"]);
// Rope and Draft rope
parameterHtml
.find('input[name="rope_scale"]')
.val(extensionSettings?.modelParams?.ropeScale ?? 'Auto');
parameterHtml
.find('input[name="rope_alpha"]')
.val(extensionSettings?.modelParams?.ropeAlpha ?? 'Auto');
parameterHtml
.find('input[name="draft_rope_scale"]')
.val(extensionSettings?.modelParams?.draft?.draft_ropeScale ?? 'Auto');
parameterHtml
.find('input[name="draft_rope_alpha"]')
.val(extensionSettings?.modelParams?.draft?.draft_ropeAlpha ?? 'Auto');
// MARK: GPU split options
const gpuSplitAuto = extensionSettings?.modelParams?.gpuSplitAuto ?? true;
const gpuSplitValue = extensionSettings?.modelParams?.gpuSplit;
const gpuSplitTextbox = parameterHtml
.find('input[name="gpu_split_value"]')
.val(JSON.stringify(gpuSplitValue?.length > 0 ? gpuSplitValue : undefined))
.prop('disabled', gpuSplitAuto);
parameterHtml
.find('input[name="gpu_split_auto"]')
.prop('checked', gpuSplitAuto)
.on('click', function () {
gpuSplitTextbox.prop('disabled', $(this).prop('checked'));
});
const popupResult = await callPopup(parameterHtml, 'confirm', undefined, { okButton: 'Save' });
if (popupResult) {
const newParams = {
maxSeqLen: Number(parameterHtml.find('input[name="max_seq_len"]').val()) || 4096,
cacheSize: Number(parameterHtml.find('input[name="cache_mode"]').val()) || null,
maxBatchSize: Number(parameterHtml.find('input[name="max_batch_size"]').val()) || null,
ropeScale: Number(parameterHtml.find('input[name="rope_scale"]').val()) || null,
ropeAlpha: Number(parameterHtml.find('input[name="rope_alpha"]').val()) || null,
draft: {
draft_ropeScale: Number(parameterHtml.find('input[name="draft_rope_scale"]').val()) || null,
draft_ropeAlpha: Number(parameterHtml.find('input[name="draft_rope_alpha"]').val()) || null,
},
gpuSplitAuto: parameterHtml.find('input[name="gpu_split_auto"]').prop('checked'),
fasttensors: parameterHtml.find('input[name="fasttensors"]').prop('checked'),
cacheMode: getKeyByValue(
cache_mode,
Number(
parameterHtml.find('select[name="cache_mode_select"]').find(":selected").val()
) || 0
),
};
// Handle GPU split setting
const gpuSplitVal = parameterHtml.find('input[name="gpu_split_value"]').val();
try {
if (gpuSplitVal) {
const gpuSplitArray = JSON.parse(gpuSplitVal) ?? [];
if (Array.isArray(gpuSplitArray)) {
newParams['gpuSplit'] = gpuSplitArray;
} else {
console.error(`Provided GPU split value (${gpuSplitArray}) is not an array.`);
newParams['gpuSplit'] = [];
}
}
} catch (error) {
console.error(error);
newParams['gpuSplit'] = [];
}
Object.assign(extensionSettings, { modelParams: newParams });
saveSettingsDebounced();
}
}
function migrateSettings() {
let performSave = false;
if ('eightBitCache' in extensionSettings?.modelParams) {
const newParams = {
cacheMode: extensionSettings?.modelParams?.eightBitCache ? 'FP8' : 'FP16'
};
delete extensionSettings.modelParams.eightBitCache;
Object.assign(extensionSettings?.modelParams, newParams);
performSave = true;
}
if (performSave) {
saveSettingsDebounced();
}
}
async function loadSettings() {
//Create the settings if they don't exist
extension_settings[settingsName] = extension_settings[settingsName] || {};
if (Object.keys(extension_settings[settingsName]).length === 0) {
Object.assign(extension_settings[settingsName], defaultSettings);
// Refresh the reference to the extension_settings object
extensionSettings = extension_settings[settingsName];
saveSettingsDebounced();
}
migrateSettings();
$('#tabby_url_override').val(extensionSettings?.urlOverride ?? '');
$('#tabby_use_proxy').prop('checked', extensionSettings?.useProxy ?? false);
// Updating settings in the UI
const placeholder = await getTabbyAuth() ? '✔️ Key found' : '❌ Missing key';
$('#tabby_admin_key').attr('placeholder', placeholder);
}
// This function is called when the extension is loaded
jQuery(async () => {
// This is an example of loading HTML from a file
const settingsHtml = await $.get(`${extensionFolderPath}/dropdown.html`);
// Append settingsHtml to extensions_settings
// extension_settings and extensions_settings2 are the left and right columns of the settings menu
// Left should be extensions that deal with system functions and right should be visual/UI related
$('#extensions_settings').append(settingsHtml);
try {
let allmodels = await fetchModels();
models = allmodels[0]
draftModels = allmodels[1]
} catch(error) {
console.error(error)
}
$('#model_list')
.autocomplete({
source: (_, response) => {
return response(models);
},
minLength: 0,
})
.focus(function () {
$(this)
.autocomplete(
'search',
$(this).val(),
);
});
$('#draft_model_list')
.autocomplete({
source: (_, response) => {
return response(draftModels);
},
minLength: 0,
})
.focus(function () {
$(this)
.autocomplete(
'search',
$(this).val(),
);
});
// These are examples of listening for events
$('#load_model_button').on('click', function () {
if (verifyTabby()) {
onLoadModelClick();
}
});
$('#unload_model_button').on('click', function () {
if (verifyTabby()) {
onUnloadModelClick();
}
});
$('#reload_model_list_button').on('click', async function () {
if (verifyTabby()) {
let allmodels = await fetchModels();
models = allmodels[0]
draftModels = allmodels[1]
}
});
$('#tabby_admin_key_clear').on('click', function () {
localStorage.removeItem('Tabby_Admin');
});
$('#tabby_admin_key').on('input', function () {
const value = $(this).val();
if (value) {
localStorage.setItem('Tabby_Admin', value);
}
});
$('#tabby_url_override').on('input', function () {
const value = $(this).val();
if (value !== undefined) {
extensionSettings.urlOverride = value;
saveSettingsDebounced();
}
});
$('#tabby_use_proxy').on('input', function () {
extensionSettings.useProxy = !!$(this).prop('checked');
saveSettingsDebounced();
});
$('#loading_progressbar').progressbar({
value: 0,
});
$('#loading_progress_container').hide();
$('#open_parameter_editor').on('click', function () {
onParameterEditorClick();
});
// Load settings when starting things up (if you have any)
await loadSettings();
});