Merge pull request #3024 from dylan1951/add-nano-gpt-provider

Add NanoGPT chat completions provider
This commit is contained in:
Cohee
2024-10-31 19:30:24 +02:00
committed by GitHub
9 changed files with 142 additions and 0 deletions

View File

@ -182,6 +182,7 @@ export const chat_completion_sources = {
GROQ: 'groq',
ZEROONEAI: '01ai',
BLOCKENTROPY: 'blockentropy',
NANOGPT: 'nanogpt',
};
const character_names_behavior = {
@ -251,6 +252,7 @@ const default_settings = {
cohere_model: 'command-r-plus',
perplexity_model: 'llama-3.1-70b-instruct',
groq_model: 'llama-3.1-70b-versatile',
nanogpt_model: 'gpt-4o-mini',
zerooneai_model: 'yi-large',
blockentropy_model: 'be-70b-base-llama3.1',
custom_model: '',
@ -327,6 +329,7 @@ const oai_settings = {
cohere_model: 'command-r-plus',
perplexity_model: 'llama-3.1-70b-instruct',
groq_model: 'llama-3.1-70b-versatile',
nanogpt_model: 'gpt-4o-mini',
zerooneai_model: 'yi-large',
blockentropy_model: 'be-70b-base-llama3.1',
custom_model: '',
@ -1480,6 +1483,8 @@ function getChatCompletionModel() {
return oai_settings.zerooneai_model;
case chat_completion_sources.BLOCKENTROPY:
return oai_settings.blockentropy_model;
case chat_completion_sources.NANOGPT:
return oai_settings.nanogpt_model;
default:
throw new Error(`Unknown chat completion source: ${oai_settings.chat_completion_source}`);
}
@ -1637,6 +1642,24 @@ function saveModelList(data) {
}
}
}
if (oai_settings.chat_completion_source == chat_completion_sources.NANOGPT) {
$('#model_nanogpt_select').empty();
model_list.forEach((model) => {
$('#model_nanogpt_select').append(
$('<option>', {
value: model.id,
text: model.id,
}));
});
const selectedModel = model_list.find(model => model.id === oai_settings.nanogpt_model);
if (model_list.length > 0 && (!selectedModel || !oai_settings.nanogpt_model)) {
oai_settings.nanogpt_model = model_list[0].id;
}
$('#model_nanogpt_select').val(oai_settings.nanogpt_model).trigger('change');
}
}
function appendOpenRouterOptions(model_list, groupModels = false, sort = false) {
@ -2961,6 +2984,7 @@ function loadOpenAISettings(data, settings) {
oai_settings.cohere_model = settings.cohere_model ?? default_settings.cohere_model;
oai_settings.perplexity_model = settings.perplexity_model ?? default_settings.perplexity_model;
oai_settings.groq_model = settings.groq_model ?? default_settings.groq_model;
oai_settings.nanogpt_model = settings.nanogpt_model ?? default_settings.nanogpt_model;
oai_settings.blockentropy_model = settings.blockentropy_model ?? default_settings.blockentropy_model;
oai_settings.zerooneai_model = settings.zerooneai_model ?? default_settings.zerooneai_model;
oai_settings.custom_model = settings.custom_model ?? default_settings.custom_model;
@ -3041,6 +3065,8 @@ function loadOpenAISettings(data, settings) {
$(`#model_perplexity_select option[value="${oai_settings.perplexity_model}"`).attr('selected', true);
$('#model_groq_select').val(oai_settings.groq_model);
$(`#model_groq_select option[value="${oai_settings.groq_model}"`).attr('selected', true);
$('#model_nanogpt_select').val(oai_settings.nanogpt_model);
$(`#model_nanogpt_select option[value="${oai_settings.nanogpt_model}"`).attr('selected', true);
$('#model_01ai_select').val(oai_settings.zerooneai_model);
$('#model_blockentropy_select').val(oai_settings.blockentropy_model);
$('#custom_model_id').val(oai_settings.custom_model);
@ -3735,6 +3761,7 @@ function onSettingsPresetChange() {
cohere_model: ['#model_cohere_select', 'cohere_model', false],
perplexity_model: ['#model_perplexity_select', 'perplexity_model', false],
groq_model: ['#model_groq_select', 'groq_model', false],
nanogpt_model: ['#model_nanogpt_select', 'nanogpt_model', false],
zerooneai_model: ['#model_01ai_select', 'zerooneai_model', false],
blockentropy_model: ['#model_blockentropy_select', 'blockentropy_model', false],
custom_model: ['#custom_model_id', 'custom_model', false],
@ -3984,6 +4011,16 @@ async function onModelChange() {
oai_settings.groq_model = value;
}
if ($(this).is('#model_nanogpt_select')) {
if (!value) {
console.debug('Null NanoGPT model selected. Ignoring.');
return;
}
console.log('NanoGPT model changed to', value);
oai_settings.nanogpt_model = value;
}
if (value && $(this).is('#model_01ai_select')) {
console.log('01.AI model changed to', value);
oai_settings.zerooneai_model = value;
@ -4294,6 +4331,18 @@ async function onModelChange() {
$('#temp_openai').attr('max', oai_max_temp).val(oai_settings.temp_openai).trigger('input');
}
if (oai_settings.chat_completion_source === chat_completion_sources.NANOGPT) {
if (oai_settings.max_context_unlocked) {
$('#openai_max_context').attr('max', unlocked_max);
} else {
$('#openai_max_context').attr('max', max_128k);
}
oai_settings.openai_max_context = Math.min(Number($('#openai_max_context').attr('max')), oai_settings.openai_max_context);
$('#openai_max_context').val(oai_settings.openai_max_context).trigger('input');
$('#temp_openai').attr('max', oai_max_temp).val(oai_settings.temp_openai).trigger('input');
}
if (oai_settings.chat_completion_source === chat_completion_sources.COHERE) {
oai_settings.pres_pen_openai = Math.min(Math.max(0, oai_settings.pres_pen_openai), 1);
$('#pres_pen_openai').attr('max', 1).attr('min', 0).val(oai_settings.pres_pen_openai).trigger('input');
@ -4498,6 +4547,19 @@ async function onConnectButtonClick(e) {
}
}
if (oai_settings.chat_completion_source == chat_completion_sources.NANOGPT) {
const api_key_nanogpt = String($('#api_key_nanogpt').val()).trim();
if (api_key_nanogpt.length) {
await writeSecret(SECRET_KEYS.NANOGPT, api_key_nanogpt);
}
if (!secret_state[SECRET_KEYS.NANOGPT]) {
console.log('No secret key saved for NanoGPT');
return;
}
}
if (oai_settings.chat_completion_source == chat_completion_sources.ZEROONEAI) {
const api_key_01ai = String($('#api_key_01ai').val()).trim();
@ -4567,6 +4629,9 @@ function toggleChatCompletionForms() {
else if (oai_settings.chat_completion_source == chat_completion_sources.GROQ) {
$('#model_groq_select').trigger('change');
}
else if (oai_settings.chat_completion_source == chat_completion_sources.NANOGPT) {
$('#model_nanogpt_select').trigger('change');
}
else if (oai_settings.chat_completion_source == chat_completion_sources.ZEROONEAI) {
$('#model_01ai_select').trigger('change');
}
@ -5288,6 +5353,7 @@ export function initOpenAI() {
$('#model_cohere_select').on('change', onModelChange);
$('#model_perplexity_select').on('change', onModelChange);
$('#model_groq_select').on('change', onModelChange);
$('#model_nanogpt_select').on('change', onModelChange);
$('#model_01ai_select').on('change', onModelChange);
$('#model_blockentropy_select').on('change', onModelChange);
$('#model_custom_select').on('change', onModelChange);