2023-12-17 22:38:03 +01:00
|
|
|
import { isMobile } from './RossAscends-mods.js';
|
2024-07-07 20:02:51 +02:00
|
|
|
import { amount_gen, callPopup, eventSource, event_types, getRequestHeaders, max_context, online_status, setGenerationParamsFromPreset } from '../script.js';
|
2023-12-19 15:38:11 +01:00
|
|
|
import { textgenerationwebui_settings as textgen_settings, textgen_types } from './textgen-settings.js';
|
2024-02-25 21:47:07 +01:00
|
|
|
import { tokenizers } from './tokenizers.js';
|
2024-07-07 20:02:51 +02:00
|
|
|
import { renderTemplateAsync } from './templates.js';
|
|
|
|
import { POPUP_TYPE, callGenericPopup } from './popup.js';
|
2023-12-17 22:38:03 +01:00
|
|
|
|
|
|
|
let mancerModels = [];
|
|
|
|
let togetherModels = [];
|
2024-01-24 13:59:27 +01:00
|
|
|
let infermaticAIModels = [];
|
2024-03-07 10:55:08 +01:00
|
|
|
let dreamGenModels = [];
|
2024-05-03 00:40:40 +02:00
|
|
|
let vllmModels = [];
|
2024-03-01 22:02:43 +01:00
|
|
|
let aphroditeModels = [];
|
2024-06-27 15:06:11 +02:00
|
|
|
let featherlessModels = [];
|
2024-02-25 21:47:07 +01:00
|
|
|
export let openRouterModels = [];
|
2023-12-17 22:38:03 +01:00
|
|
|
|
2024-05-06 18:26:20 +02:00
|
|
|
/**
|
|
|
|
* List of OpenRouter providers.
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
const OPENROUTER_PROVIDERS = [
|
|
|
|
'OpenAI',
|
|
|
|
'Anthropic',
|
|
|
|
'HuggingFace',
|
|
|
|
'Google',
|
|
|
|
'Mancer',
|
|
|
|
'Mancer 2',
|
|
|
|
'Together',
|
|
|
|
'DeepInfra',
|
|
|
|
'Azure',
|
|
|
|
'Modal',
|
|
|
|
'AnyScale',
|
|
|
|
'Replicate',
|
|
|
|
'Perplexity',
|
|
|
|
'Recursal',
|
|
|
|
'Fireworks',
|
|
|
|
'Mistral',
|
|
|
|
'Groq',
|
|
|
|
'Cohere',
|
|
|
|
'Lepton',
|
|
|
|
'OctoAI',
|
|
|
|
'Novita',
|
|
|
|
'Lynn',
|
|
|
|
'Lynn 2',
|
2024-06-22 01:03:39 +02:00
|
|
|
'DeepSeek',
|
|
|
|
'Infermatic',
|
2024-05-06 18:26:20 +02:00
|
|
|
];
|
|
|
|
|
2023-12-19 15:38:11 +01:00
|
|
|
export async function loadOllamaModels(data) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
console.error('Invalid Ollama models data', data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data.find(x => x.id === textgen_settings.ollama_model)) {
|
|
|
|
textgen_settings.ollama_model = data[0]?.id || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#ollama_model').empty();
|
|
|
|
for (const model of data) {
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = model.id;
|
|
|
|
option.text = model.name;
|
|
|
|
option.selected = model.id === textgen_settings.ollama_model;
|
|
|
|
$('#ollama_model').append(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-17 22:38:03 +01:00
|
|
|
export async function loadTogetherAIModels(data) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
console.error('Invalid Together AI models data', data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:21:34 +02:00
|
|
|
data.sort((a, b) => a.name.localeCompare(b.name));
|
2023-12-17 22:38:03 +01:00
|
|
|
togetherModels = data;
|
|
|
|
|
2023-12-19 15:38:11 +01:00
|
|
|
if (!data.find(x => x.name === textgen_settings.togetherai_model)) {
|
|
|
|
textgen_settings.togetherai_model = data[0]?.name || '';
|
|
|
|
}
|
|
|
|
|
2023-12-17 22:38:03 +01:00
|
|
|
$('#model_togetherai_select').empty();
|
|
|
|
for (const model of data) {
|
|
|
|
// Hey buddy, I think you've got the wrong door.
|
|
|
|
if (model.display_type === 'image') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = model.name;
|
|
|
|
option.text = model.display_name;
|
|
|
|
option.selected = model.name === textgen_settings.togetherai_model;
|
|
|
|
$('#model_togetherai_select').append(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 13:59:27 +01:00
|
|
|
export async function loadInfermaticAIModels(data) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
console.error('Invalid Infermatic AI models data', data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:21:34 +02:00
|
|
|
data.sort((a, b) => a.id.localeCompare(b.id));
|
2024-01-24 13:59:27 +01:00
|
|
|
infermaticAIModels = data;
|
|
|
|
|
|
|
|
if (!data.find(x => x.id === textgen_settings.infermaticai_model)) {
|
|
|
|
textgen_settings.infermaticai_model = data[0]?.id || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#model_infermaticai_select').empty();
|
|
|
|
for (const model of data) {
|
|
|
|
if (model.display_type === 'image') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = model.id;
|
|
|
|
option.text = model.id;
|
|
|
|
option.selected = model.id === textgen_settings.infermaticai_model;
|
|
|
|
$('#model_infermaticai_select').append(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 10:55:08 +01:00
|
|
|
export async function loadDreamGenModels(data) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
console.error('Invalid DreamGen models data', data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dreamGenModels = data;
|
|
|
|
|
|
|
|
if (!data.find(x => x.id === textgen_settings.dreamgen_model)) {
|
|
|
|
textgen_settings.dreamgen_model = data[0]?.id || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#model_dreamgen_select').empty();
|
|
|
|
for (const model of data) {
|
|
|
|
if (model.display_type === 'image') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = model.id;
|
|
|
|
option.text = model.id;
|
|
|
|
option.selected = model.id === textgen_settings.dreamgen_model;
|
|
|
|
$('#model_dreamgen_select').append(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-17 22:38:03 +01:00
|
|
|
export async function loadMancerModels(data) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
console.error('Invalid Mancer models data', data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:21:34 +02:00
|
|
|
data.sort((a, b) => a.name.localeCompare(b.name));
|
2023-12-17 22:38:03 +01:00
|
|
|
mancerModels = data;
|
|
|
|
|
2023-12-19 15:38:11 +01:00
|
|
|
if (!data.find(x => x.id === textgen_settings.mancer_model)) {
|
|
|
|
textgen_settings.mancer_model = data[0]?.id || '';
|
|
|
|
}
|
|
|
|
|
2023-12-17 22:38:03 +01:00
|
|
|
$('#mancer_model').empty();
|
|
|
|
for (const model of data) {
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = model.id;
|
|
|
|
option.text = model.name;
|
|
|
|
option.selected = model.id === textgen_settings.mancer_model;
|
|
|
|
$('#mancer_model').append(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 21:47:07 +01:00
|
|
|
export async function loadOpenRouterModels(data) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
console.error('Invalid OpenRouter models data', data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:21:34 +02:00
|
|
|
data.sort((a, b) => a.name.localeCompare(b.name));
|
2024-02-25 21:47:07 +01:00
|
|
|
openRouterModels = data;
|
|
|
|
|
|
|
|
if (!data.find(x => x.id === textgen_settings.openrouter_model)) {
|
|
|
|
textgen_settings.openrouter_model = data[0]?.id || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#openrouter_model').empty();
|
|
|
|
for (const model of data) {
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = model.id;
|
|
|
|
option.text = model.id;
|
|
|
|
option.selected = model.id === textgen_settings.openrouter_model;
|
|
|
|
$('#openrouter_model').append(option);
|
|
|
|
}
|
2024-03-13 01:55:09 +01:00
|
|
|
|
|
|
|
// Calculate the cost of the selected model + update on settings change
|
|
|
|
calculateOpenRouterCost();
|
2024-02-25 21:47:07 +01:00
|
|
|
}
|
|
|
|
|
2024-05-03 00:40:40 +02:00
|
|
|
export async function loadVllmModels(data) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
console.error('Invalid vLLM models data', data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vllmModels = data;
|
|
|
|
|
|
|
|
if (!data.find(x => x.id === textgen_settings.vllm_model)) {
|
|
|
|
textgen_settings.vllm_model = data[0]?.id || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#vllm_model').empty();
|
|
|
|
for (const model of data) {
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = model.id;
|
|
|
|
option.text = model.id;
|
|
|
|
option.selected = model.id === textgen_settings.vllm_model;
|
|
|
|
$('#vllm_model').append(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 22:02:43 +01:00
|
|
|
export async function loadAphroditeModels(data) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
console.error('Invalid Aphrodite models data', data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aphroditeModels = data;
|
|
|
|
|
|
|
|
if (!data.find(x => x.id === textgen_settings.aphrodite_model)) {
|
|
|
|
textgen_settings.aphrodite_model = data[0]?.id || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#aphrodite_model').empty();
|
|
|
|
for (const model of data) {
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = model.id;
|
|
|
|
option.text = model.id;
|
|
|
|
option.selected = model.id === textgen_settings.aphrodite_model;
|
|
|
|
$('#aphrodite_model').append(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 15:06:11 +02:00
|
|
|
export async function loadFeatherlessModels(data) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
console.error('Invalid Featherless models data', data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:17:03 +02:00
|
|
|
data.sort((a, b) => a.id.localeCompare(b.id));
|
2024-06-27 15:06:11 +02:00
|
|
|
featherlessModels = data;
|
|
|
|
|
|
|
|
if (!data.find(x => x.id === textgen_settings.featherless_model)) {
|
|
|
|
textgen_settings.featherless_model = data[0]?.id || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#featherless_model').empty();
|
|
|
|
for (const model of data) {
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = model.id;
|
|
|
|
option.text = model.id;
|
|
|
|
option.selected = model.id === textgen_settings.featherless_model;
|
|
|
|
$('#featherless_model').append(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onFeatherlessModelSelect() {
|
|
|
|
const modelId = String($('#featherless_model').val());
|
|
|
|
textgen_settings.featherless_model = modelId;
|
|
|
|
$('#api_button_textgenerationwebui').trigger('click');
|
2024-08-01 22:17:03 +02:00
|
|
|
const model = featherlessModels.find(x => x.id === modelId);
|
|
|
|
setGenerationParamsFromPreset({ max_length: model.context_length });
|
2024-06-27 15:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-17 22:38:03 +01:00
|
|
|
function onMancerModelSelect() {
|
|
|
|
const modelId = String($('#mancer_model').val());
|
|
|
|
textgen_settings.mancer_model = modelId;
|
|
|
|
$('#api_button_textgenerationwebui').trigger('click');
|
|
|
|
|
|
|
|
const limits = mancerModels.find(x => x.id === modelId)?.limits;
|
2024-05-16 23:19:43 +02:00
|
|
|
setGenerationParamsFromPreset({ max_length: limits.context });
|
2023-12-17 22:38:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function onTogetherModelSelect() {
|
|
|
|
const modelName = String($('#model_togetherai_select').val());
|
|
|
|
textgen_settings.togetherai_model = modelName;
|
|
|
|
$('#api_button_textgenerationwebui').trigger('click');
|
|
|
|
const model = togetherModels.find(x => x.name === modelName);
|
|
|
|
setGenerationParamsFromPreset({ max_length: model.context_length });
|
|
|
|
}
|
|
|
|
|
2024-01-24 13:59:27 +01:00
|
|
|
function onInfermaticAIModelSelect() {
|
|
|
|
const modelName = String($('#model_infermaticai_select').val());
|
|
|
|
textgen_settings.infermaticai_model = modelName;
|
2024-02-16 16:07:06 +01:00
|
|
|
$('#api_button_textgenerationwebui').trigger('click');
|
2024-01-24 13:59:27 +01:00
|
|
|
const model = infermaticAIModels.find(x => x.id === modelName);
|
|
|
|
setGenerationParamsFromPreset({ max_length: model.context_length });
|
|
|
|
}
|
|
|
|
|
2024-03-07 10:55:08 +01:00
|
|
|
function onDreamGenModelSelect() {
|
|
|
|
const modelName = String($('#model_dreamgen_select').val());
|
|
|
|
textgen_settings.dreamgen_model = modelName;
|
|
|
|
$('#api_button_textgenerationwebui').trigger('click');
|
|
|
|
// TODO(DreamGen): Consider retuning max_tokens from API and setting it here.
|
|
|
|
}
|
|
|
|
|
2023-12-19 15:38:11 +01:00
|
|
|
function onOllamaModelSelect() {
|
|
|
|
const modelId = String($('#ollama_model').val());
|
|
|
|
textgen_settings.ollama_model = modelId;
|
|
|
|
$('#api_button_textgenerationwebui').trigger('click');
|
|
|
|
}
|
|
|
|
|
2024-02-25 21:47:07 +01:00
|
|
|
function onOpenRouterModelSelect() {
|
|
|
|
const modelId = String($('#openrouter_model').val());
|
|
|
|
textgen_settings.openrouter_model = modelId;
|
|
|
|
$('#api_button_textgenerationwebui').trigger('click');
|
|
|
|
const model = openRouterModels.find(x => x.id === modelId);
|
|
|
|
setGenerationParamsFromPreset({ max_length: model.context_length });
|
|
|
|
}
|
|
|
|
|
2024-05-03 00:40:40 +02:00
|
|
|
function onVllmModelSelect() {
|
|
|
|
const modelId = String($('#vllm_model').val());
|
|
|
|
textgen_settings.vllm_model = modelId;
|
|
|
|
$('#api_button_textgenerationwebui').trigger('click');
|
|
|
|
}
|
|
|
|
|
2024-03-01 22:02:43 +01:00
|
|
|
function onAphroditeModelSelect() {
|
|
|
|
const modelId = String($('#aphrodite_model').val());
|
|
|
|
textgen_settings.aphrodite_model = modelId;
|
|
|
|
$('#api_button_textgenerationwebui').trigger('click');
|
|
|
|
}
|
|
|
|
|
2023-12-17 22:38:03 +01:00
|
|
|
function getMancerModelTemplate(option) {
|
|
|
|
const model = mancerModels.find(x => x.id === option?.element?.value);
|
|
|
|
|
|
|
|
if (!option.id || !model) {
|
|
|
|
return option.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
const creditsPerPrompt = (model.limits?.context - model.limits?.completion) * model.pricing?.prompt;
|
|
|
|
const creditsPerCompletion = model.limits?.completion * model.pricing?.completion;
|
|
|
|
const creditsTotal = Math.round(creditsPerPrompt + creditsPerCompletion).toFixed(0);
|
|
|
|
|
|
|
|
return $((`
|
|
|
|
<div class="flex-container flexFlowColumn">
|
|
|
|
<div><strong>${DOMPurify.sanitize(model.name)}</strong> | <span>${model.limits?.context} ctx</span> / <span>${model.limits?.completion} res</span> | <small>Credits per request (max): ${creditsTotal}</small></div>
|
|
|
|
</div>
|
|
|
|
`));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTogetherModelTemplate(option) {
|
|
|
|
const model = togetherModels.find(x => x.name === option?.element?.value);
|
|
|
|
|
|
|
|
if (!option.id || !model) {
|
|
|
|
return option.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $((`
|
|
|
|
<div class="flex-container flexFlowColumn">
|
|
|
|
<div><strong>${DOMPurify.sanitize(model.name)}</strong> | <span>${model.context_length || '???'} tokens</span></div>
|
|
|
|
<div><small>${DOMPurify.sanitize(model.description)}</small></div>
|
|
|
|
</div>
|
|
|
|
`));
|
|
|
|
}
|
|
|
|
|
2024-01-24 13:59:27 +01:00
|
|
|
function getInfermaticAIModelTemplate(option) {
|
|
|
|
const model = infermaticAIModels.find(x => x.id === option?.element?.value);
|
|
|
|
|
|
|
|
if (!option.id || !model) {
|
|
|
|
return option.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $((`
|
|
|
|
<div class="flex-container flexFlowColumn">
|
|
|
|
<div><strong>${DOMPurify.sanitize(model.id)}</strong></div>
|
|
|
|
</div>
|
|
|
|
`));
|
|
|
|
}
|
|
|
|
|
2024-03-07 10:55:08 +01:00
|
|
|
function getDreamGenModelTemplate(option) {
|
|
|
|
const model = dreamGenModels.find(x => x.id === option?.element?.value);
|
|
|
|
|
|
|
|
if (!option.id || !model) {
|
|
|
|
return option.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $((`
|
|
|
|
<div class="flex-container flexFlowColumn">
|
|
|
|
<div><strong>${DOMPurify.sanitize(model.id)}</strong></div>
|
|
|
|
</div>
|
|
|
|
`));
|
|
|
|
}
|
|
|
|
|
2024-02-25 21:47:07 +01:00
|
|
|
function getOpenRouterModelTemplate(option) {
|
|
|
|
const model = openRouterModels.find(x => x.id === option?.element?.value);
|
|
|
|
|
|
|
|
if (!option.id || !model) {
|
|
|
|
return option.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tokens_dollar = Number(1 / (1000 * model.pricing?.prompt));
|
|
|
|
let tokens_rounded = (Math.round(tokens_dollar * 1000) / 1000).toFixed(0);
|
|
|
|
|
|
|
|
const price = 0 === Number(model.pricing?.prompt) ? 'Free' : `${tokens_rounded}k t/$ `;
|
|
|
|
|
|
|
|
return $((`
|
|
|
|
<div class="flex-container flexFlowColumn" title="${DOMPurify.sanitize(model.id)}">
|
|
|
|
<div><strong>${DOMPurify.sanitize(model.name)}</strong> | ${model.context_length} ctx | <small>${price}</small></div>
|
|
|
|
</div>
|
|
|
|
`));
|
|
|
|
}
|
|
|
|
|
2024-05-03 00:40:40 +02:00
|
|
|
function getVllmModelTemplate(option) {
|
|
|
|
const model = vllmModels.find(x => x.id === option?.element?.value);
|
|
|
|
|
|
|
|
if (!option.id || !model) {
|
|
|
|
return option.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $((`
|
|
|
|
<div class="flex-container flexFlowColumn">
|
|
|
|
<div><strong>${DOMPurify.sanitize(model.id)}</strong></div>
|
|
|
|
</div>
|
|
|
|
`));
|
|
|
|
}
|
|
|
|
|
2024-03-01 22:02:43 +01:00
|
|
|
function getAphroditeModelTemplate(option) {
|
|
|
|
const model = aphroditeModels.find(x => x.id === option?.element?.value);
|
|
|
|
|
|
|
|
if (!option.id || !model) {
|
|
|
|
return option.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $((`
|
|
|
|
<div class="flex-container flexFlowColumn">
|
|
|
|
<div><strong>${DOMPurify.sanitize(model.id)}</strong></div>
|
|
|
|
</div>
|
|
|
|
`));
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:17:03 +02:00
|
|
|
function getFeatherlessModelTemplate(option) {
|
|
|
|
const model = featherlessModels.find(x => x.id === option?.element?.value);
|
|
|
|
|
|
|
|
if (!option.id || !model) {
|
|
|
|
return option.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $((`
|
|
|
|
<div class="flex-container flexFlowColumn">
|
|
|
|
<div><strong>${DOMPurify.sanitize(model.name)}</strong> | <span>${model.context_length || '???'} tokens</span></div>
|
|
|
|
</div>
|
|
|
|
`));
|
|
|
|
}
|
|
|
|
|
2023-12-19 15:38:11 +01:00
|
|
|
async function downloadOllamaModel() {
|
|
|
|
try {
|
|
|
|
const serverUrl = textgen_settings.server_urls[textgen_types.OLLAMA];
|
|
|
|
|
|
|
|
if (!serverUrl) {
|
|
|
|
toastr.info('Please connect to an Ollama server first.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const html = `Enter a model tag, for example <code>llama2:latest</code>.<br>
|
|
|
|
See <a target="_blank" href="https://ollama.ai/library">Library</a> for available models.`;
|
|
|
|
const name = await callPopup(html, 'input', '', { okButton: 'Download' });
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
toastr.info('Download may take a while, please wait...', 'Working on it');
|
|
|
|
|
|
|
|
const response = await fetch('/api/backends/text-completions/ollama/download', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: getRequestHeaders(),
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: name,
|
|
|
|
api_server: serverUrl,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(response.statusText);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force refresh the model list
|
|
|
|
toastr.success('Download complete. Please select the model from the dropdown.');
|
|
|
|
$('#api_button_textgenerationwebui').trigger('click');
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
toastr.error('Failed to download Ollama model. Please try again.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-07 20:02:51 +02:00
|
|
|
async function downloadTabbyModel() {
|
|
|
|
try {
|
|
|
|
const serverUrl = textgen_settings.server_urls[textgen_types.TABBY];
|
|
|
|
|
|
|
|
if (online_status === 'no_connection' || !serverUrl) {
|
|
|
|
toastr.info('Please connect to a TabbyAPI server first.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const downloadHtml = $(await renderTemplateAsync('tabbyDownloader'));
|
|
|
|
const popupResult = await callGenericPopup(downloadHtml, POPUP_TYPE.CONFIRM, '', { okButton: 'Download', cancelButton: 'Cancel' });
|
|
|
|
|
|
|
|
// User cancelled the download
|
|
|
|
if (!popupResult) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-07 21:40:03 +02:00
|
|
|
const repoId = downloadHtml.find('input[name="hf_repo_id"]').val().toString();
|
2024-07-07 20:02:51 +02:00
|
|
|
if (!repoId) {
|
|
|
|
toastr.error('A HuggingFace repo ID must be provided. Skipping Download.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-07 21:40:03 +02:00
|
|
|
if (repoId.split('/').length !== 2) {
|
2024-07-07 20:02:51 +02:00
|
|
|
toastr.error('A HuggingFace repo ID must be formatted as Author/Name. Please try again.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
repo_id: repoId,
|
|
|
|
folder_name: downloadHtml.find('input[name="folder_name"]').val() || undefined,
|
|
|
|
revision: downloadHtml.find('input[name="revision"]').val() || undefined,
|
|
|
|
token: downloadHtml.find('input[name="hf_token"]').val() || undefined,
|
2024-07-07 21:40:03 +02:00
|
|
|
};
|
2024-07-07 20:02:51 +02:00
|
|
|
|
2024-07-07 21:40:03 +02:00
|
|
|
for (const suffix of ['include', 'exclude']) {
|
2024-07-07 20:02:51 +02:00
|
|
|
const patterns = downloadHtml.find(`textarea[name="tabby_download_${suffix}"]`).val().toString();
|
|
|
|
if (patterns) {
|
2024-07-07 21:40:03 +02:00
|
|
|
params[suffix] = patterns.split('\n');
|
2024-07-07 20:02:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Params for the server side of ST
|
|
|
|
params['api_server'] = serverUrl;
|
2024-07-07 21:40:03 +02:00
|
|
|
params['api_type'] = textgen_settings.type;
|
2024-07-07 20:02:51 +02:00
|
|
|
|
|
|
|
toastr.info('Downloading. Check the Tabby console for progress reports.');
|
|
|
|
|
|
|
|
const response = await fetch('/api/backends/text-completions/tabby/download', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: getRequestHeaders(),
|
|
|
|
body: JSON.stringify(params),
|
|
|
|
});
|
|
|
|
|
2024-07-07 20:40:24 +02:00
|
|
|
if (response.status === 403) {
|
2024-07-07 21:40:03 +02:00
|
|
|
toastr.error('The provided key has invalid permissions. Please use an admin key for downloading.');
|
2024-07-07 20:40:24 +02:00
|
|
|
return;
|
|
|
|
} else if (!response.ok) {
|
2024-07-07 20:02:51 +02:00
|
|
|
throw new Error(response.statusText);
|
|
|
|
}
|
|
|
|
|
|
|
|
toastr.success('Download complete.');
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2024-07-07 21:40:03 +02:00
|
|
|
toastr.error('Failed to download HuggingFace model in TabbyAPI. Please try again.');
|
2024-07-07 20:02:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 01:55:09 +01:00
|
|
|
function calculateOpenRouterCost() {
|
|
|
|
if (textgen_settings.type !== textgen_types.OPENROUTER) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cost = 'Unknown';
|
|
|
|
const model = openRouterModels.find(x => x.id === textgen_settings.openrouter_model);
|
|
|
|
|
|
|
|
if (model?.pricing) {
|
|
|
|
const completionCost = Number(model.pricing.completion);
|
|
|
|
const promptCost = Number(model.pricing.prompt);
|
|
|
|
const completionTokens = amount_gen;
|
|
|
|
const promptTokens = (max_context - completionTokens);
|
|
|
|
const totalCost = (completionCost * completionTokens) + (promptCost * promptTokens);
|
|
|
|
if (!isNaN(totalCost)) {
|
|
|
|
cost = '$' + totalCost.toFixed(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 10:19:30 +01:00
|
|
|
$('#or_prompt_cost').text(cost);
|
2024-03-13 01:55:09 +01:00
|
|
|
|
|
|
|
// Schedule an update when settings change
|
|
|
|
eventSource.removeListener(event_types.SETTINGS_UPDATED, calculateOpenRouterCost);
|
|
|
|
eventSource.once(event_types.SETTINGS_UPDATED, calculateOpenRouterCost);
|
|
|
|
}
|
|
|
|
|
2024-02-25 21:47:07 +01:00
|
|
|
export function getCurrentOpenRouterModelTokenizer() {
|
|
|
|
const modelId = textgen_settings.openrouter_model;
|
|
|
|
const model = openRouterModels.find(x => x.id === modelId);
|
|
|
|
switch (model?.architecture?.tokenizer) {
|
|
|
|
case 'Llama2':
|
|
|
|
return tokenizers.LLAMA;
|
2024-05-03 22:59:39 +02:00
|
|
|
case 'Llama3':
|
|
|
|
return tokenizers.LLAMA3;
|
|
|
|
case 'Yi':
|
|
|
|
return tokenizers.YI;
|
2024-02-25 21:47:07 +01:00
|
|
|
case 'Mistral':
|
|
|
|
return tokenizers.MISTRAL;
|
|
|
|
default:
|
|
|
|
return tokenizers.OPENAI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 10:55:08 +01:00
|
|
|
export function getCurrentDreamGenModelTokenizer() {
|
2024-03-07 17:28:38 +01:00
|
|
|
const modelId = textgen_settings.dreamgen_model;
|
|
|
|
const model = dreamGenModels.find(x => x.id === modelId);
|
2024-03-07 10:55:08 +01:00
|
|
|
if (model.id.startsWith('opus-v1-sm')) {
|
|
|
|
return tokenizers.MISTRAL;
|
|
|
|
} else if (model.id.startsWith('opus-v1-lg')) {
|
|
|
|
return tokenizers.YI;
|
|
|
|
} else if (model.id.startsWith('opus-v1-xl')) {
|
|
|
|
return tokenizers.LLAMA;
|
|
|
|
} else {
|
|
|
|
return tokenizers.MISTRAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-17 22:38:03 +01:00
|
|
|
jQuery(function () {
|
|
|
|
$('#mancer_model').on('change', onMancerModelSelect);
|
|
|
|
$('#model_togetherai_select').on('change', onTogetherModelSelect);
|
2024-01-24 13:59:27 +01:00
|
|
|
$('#model_infermaticai_select').on('change', onInfermaticAIModelSelect);
|
2024-03-07 10:55:08 +01:00
|
|
|
$('#model_dreamgen_select').on('change', onDreamGenModelSelect);
|
2023-12-19 15:38:11 +01:00
|
|
|
$('#ollama_model').on('change', onOllamaModelSelect);
|
2024-02-25 21:47:07 +01:00
|
|
|
$('#openrouter_model').on('change', onOpenRouterModelSelect);
|
2023-12-19 15:38:11 +01:00
|
|
|
$('#ollama_download_model').on('click', downloadOllamaModel);
|
2024-05-03 00:40:40 +02:00
|
|
|
$('#vllm_model').on('change', onVllmModelSelect);
|
2024-03-01 22:02:43 +01:00
|
|
|
$('#aphrodite_model').on('change', onAphroditeModelSelect);
|
2024-06-27 15:06:11 +02:00
|
|
|
$('#featherless_model').on('change', onFeatherlessModelSelect);
|
2024-07-07 20:02:51 +02:00
|
|
|
$('#tabby_download_model').on('click', downloadTabbyModel);
|
2023-12-17 22:38:03 +01:00
|
|
|
|
2024-05-06 18:26:20 +02:00
|
|
|
const providersSelect = $('.openrouter_providers');
|
|
|
|
for (const provider of OPENROUTER_PROVIDERS) {
|
|
|
|
providersSelect.append($('<option>', {
|
|
|
|
value: provider,
|
|
|
|
text: provider,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-12-17 22:38:03 +01:00
|
|
|
if (!isMobile()) {
|
|
|
|
$('#mancer_model').select2({
|
|
|
|
placeholder: 'Select a model',
|
|
|
|
searchInputPlaceholder: 'Search models...',
|
|
|
|
searchInputCssClass: 'text_pole',
|
|
|
|
width: '100%',
|
|
|
|
templateResult: getMancerModelTemplate,
|
|
|
|
});
|
|
|
|
$('#model_togetherai_select').select2({
|
|
|
|
placeholder: 'Select a model',
|
|
|
|
searchInputPlaceholder: 'Search models...',
|
|
|
|
searchInputCssClass: 'text_pole',
|
|
|
|
width: '100%',
|
|
|
|
templateResult: getTogetherModelTemplate,
|
|
|
|
});
|
2023-12-19 15:38:11 +01:00
|
|
|
$('#ollama_model').select2({
|
|
|
|
placeholder: 'Select a model',
|
|
|
|
searchInputPlaceholder: 'Search models...',
|
|
|
|
searchInputCssClass: 'text_pole',
|
|
|
|
width: '100%',
|
|
|
|
});
|
2024-01-24 13:59:27 +01:00
|
|
|
$('#model_infermaticai_select').select2({
|
|
|
|
placeholder: 'Select a model',
|
|
|
|
searchInputPlaceholder: 'Search models...',
|
|
|
|
searchInputCssClass: 'text_pole',
|
|
|
|
width: '100%',
|
|
|
|
templateResult: getInfermaticAIModelTemplate,
|
|
|
|
});
|
2024-03-07 10:55:08 +01:00
|
|
|
$('#model_dreamgen_select').select2({
|
|
|
|
placeholder: 'Select a model',
|
|
|
|
searchInputPlaceholder: 'Search models...',
|
|
|
|
searchInputCssClass: 'text_pole',
|
|
|
|
width: '100%',
|
2024-04-18 04:10:17 +02:00
|
|
|
templateResult: getDreamGenModelTemplate,
|
2024-03-07 10:55:08 +01:00
|
|
|
});
|
2024-02-25 21:47:07 +01:00
|
|
|
$('#openrouter_model').select2({
|
|
|
|
placeholder: 'Select a model',
|
|
|
|
searchInputPlaceholder: 'Search models...',
|
|
|
|
searchInputCssClass: 'text_pole',
|
|
|
|
width: '100%',
|
|
|
|
templateResult: getOpenRouterModelTemplate,
|
|
|
|
});
|
2024-05-03 00:40:40 +02:00
|
|
|
$('#vllm_model').select2({
|
|
|
|
placeholder: 'Select a model',
|
|
|
|
searchInputPlaceholder: 'Search models...',
|
|
|
|
searchInputCssClass: 'text_pole',
|
|
|
|
width: '100%',
|
|
|
|
templateResult: getVllmModelTemplate,
|
|
|
|
});
|
2024-03-01 22:02:43 +01:00
|
|
|
$('#aphrodite_model').select2({
|
|
|
|
placeholder: 'Select a model',
|
|
|
|
searchInputPlaceholder: 'Search models...',
|
|
|
|
searchInputCssClass: 'text_pole',
|
|
|
|
width: '100%',
|
|
|
|
templateResult: getAphroditeModelTemplate,
|
|
|
|
});
|
2024-06-28 18:13:46 +02:00
|
|
|
$('#featherless_model').select2({
|
|
|
|
placeholder: 'Select a model',
|
|
|
|
searchInputPlaceholder: 'Search models...',
|
|
|
|
searchInputCssClass: 'text_pole',
|
|
|
|
width: '100%',
|
2024-08-01 22:17:03 +02:00
|
|
|
templateResult: getFeatherlessModelTemplate,
|
2024-06-28 18:13:46 +02:00
|
|
|
});
|
2024-05-06 18:26:20 +02:00
|
|
|
providersSelect.select2({
|
|
|
|
sorter: data => data.sort((a, b) => a.text.localeCompare(b.text)),
|
|
|
|
placeholder: 'Select providers. No selection = all providers.',
|
|
|
|
searchInputPlaceholder: 'Search providers...',
|
|
|
|
searchInputCssClass: 'text_pole',
|
|
|
|
width: '100%',
|
|
|
|
});
|
|
|
|
providersSelect.on('select2:select', function (/** @type {any} */ evt) {
|
|
|
|
const element = evt.params.data.element;
|
|
|
|
const $element = $(element);
|
|
|
|
|
|
|
|
$element.detach();
|
|
|
|
$(this).append($element);
|
|
|
|
$(this).trigger('change');
|
|
|
|
});
|
2023-12-17 22:38:03 +01:00
|
|
|
}
|
|
|
|
});
|