From 0882fb2d15d18ded7dd4021ac8870232f6426983 Mon Sep 17 00:00:00 2001 From: dylan Date: Sat, 26 Oct 2024 16:42:09 +1300 Subject: [PATCH 1/8] Add NanoGPT as chat completions provider --- public/index.html | 71 ++++++++++++++++++++++++++++++++ public/scripts/openai.js | 31 ++++++++++++++ public/scripts/secrets.js | 2 + public/scripts/slash-commands.js | 1 + src/constants.js | 1 + 5 files changed, 106 insertions(+) diff --git a/public/index.html b/public/index.html index 0d6f1105b..6f0c67322 100644 --- a/public/index.html +++ b/public/index.html @@ -2602,6 +2602,7 @@ + @@ -3084,6 +3085,76 @@ +
+

NanoGPT API Key

+
+ + +
+
+ For privacy reasons, your API key will be hidden after you reload the page. +
+

NanoGPT Model

+ + +

Perplexity API Key

diff --git a/public/scripts/openai.js b/public/scripts/openai.js index bdb5f7f68..2b2306ec3 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -181,6 +181,7 @@ export const chat_completion_sources = { GROQ: 'groq', ZEROONEAI: '01ai', BLOCKENTROPY: 'blockentropy', + NANOGPT: 'nanogpt', }; const character_names_behavior = { @@ -250,6 +251,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: '', @@ -326,6 +328,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: '', @@ -1479,6 +1482,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}`); } @@ -2960,6 +2965,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; @@ -3040,6 +3046,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); @@ -3734,6 +3742,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], @@ -3983,6 +3992,11 @@ async function onModelChange() { oai_settings.groq_model = value; } + if ($(this).is('#model_nanogpt_select')) { + 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; @@ -4497,6 +4511,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(); @@ -4566,6 +4593,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'); } @@ -5287,6 +5317,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); diff --git a/public/scripts/secrets.js b/public/scripts/secrets.js index 24de91514..6bb97a26a 100644 --- a/public/scripts/secrets.js +++ b/public/scripts/secrets.js @@ -34,6 +34,7 @@ export const SECRET_KEYS = { STABILITY: 'api_key_stability', BLOCKENTROPY: 'api_key_blockentropy', CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts', + NANOGPT: 'api_key_nanogpt', TAVILY: 'api_key_tavily', }; @@ -67,6 +68,7 @@ const INPUT_MAP = { [SECRET_KEYS.ZEROONEAI]: '#api_key_01ai', [SECRET_KEYS.HUGGINGFACE]: '#api_key_huggingface', [SECRET_KEYS.BLOCKENTROPY]: '#api_key_blockentropy', + [SECRET_KEYS.NANOGPT]: '#api_key_nanogpt', }; async function clearSecret() { diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 4c377d22a..3a96b65bf 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -3632,6 +3632,7 @@ function getModelOptions(quiet) { { id: 'model_cohere_select', api: 'openai', type: chat_completion_sources.COHERE }, { id: 'model_perplexity_select', api: 'openai', type: chat_completion_sources.PERPLEXITY }, { id: 'model_groq_select', api: 'openai', type: chat_completion_sources.GROQ }, + { id: 'model_nanogpt_select', api: 'openai', type: chat_completion_sources.NANOGPT }, { id: 'model_01ai_select', api: 'openai', type: chat_completion_sources.ZEROONEAI }, { id: 'model_blockentropy_select', api: 'openai', type: chat_completion_sources.BLOCKENTROPY }, { id: 'model_novel_select', api: 'novel', type: null }, diff --git a/src/constants.js b/src/constants.js index 0befdaf65..35ba17f85 100644 --- a/src/constants.js +++ b/src/constants.js @@ -200,6 +200,7 @@ export const CHAT_COMPLETION_SOURCES = { GROQ: 'groq', ZEROONEAI: '01ai', BLOCKENTROPY: 'blockentropy', + NANOGPT: 'nanogpt', }; /** From 4b5f485bd535eb75dff86a8d91190822bcdb66e6 Mon Sep 17 00:00:00 2001 From: dylan Date: Sat, 26 Oct 2024 16:57:49 +1300 Subject: [PATCH 2/8] Add NanoGPT as chat completions provider --- src/endpoints/backends/chat-completions.js | 10 ++++++++++ src/endpoints/secrets.js | 1 + 2 files changed, 11 insertions(+) diff --git a/src/endpoints/backends/chat-completions.js b/src/endpoints/backends/chat-completions.js index 0d80ed453..f32d6babd 100644 --- a/src/endpoints/backends/chat-completions.js +++ b/src/endpoints/backends/chat-completions.js @@ -48,6 +48,7 @@ const API_MAKERSUITE = 'https://generativelanguage.googleapis.com'; const API_01AI = 'https://api.01.ai/v1'; const API_BLOCKENTROPY = 'https://api.blockentropy.ai/v1'; const API_AI21 = 'https://api.ai21.com/studio/v1'; +const API_NANOGPT = 'https://nano-gpt.com/api/v1'; /** * Applies a post-processing step to the generated messages. @@ -656,6 +657,10 @@ router.post('/status', jsonParser, async function (request, response_getstatus_o api_url = API_BLOCKENTROPY; api_key_openai = readSecret(request.user.directories, SECRET_KEYS.BLOCKENTROPY); headers = {}; + } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.NANOGPT) { + api_url = API_NANOGPT; + api_key_openai = readSecret(request.user.directories, SECRET_KEYS.NANOGPT); + headers = {}; } else { console.log('This chat completion source is not supported yet.'); return response_getstatus_openai.status(400).send({ error: true }); @@ -906,6 +911,11 @@ router.post('/generate', jsonParser, function (request, response) { apiKey = readSecret(request.user.directories, SECRET_KEYS.GROQ); headers = {}; bodyParams = {}; + } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.NANOGPT) { + apiUrl = API_NANOGPT; + apiKey = readSecret(request.user.directories, SECRET_KEYS.NANOGPT); + headers = {}; + bodyParams = {}; } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.ZEROONEAI) { apiUrl = API_01AI; apiKey = readSecret(request.user.directories, SECRET_KEYS.ZEROONEAI); diff --git a/src/endpoints/secrets.js b/src/endpoints/secrets.js index e450c994c..28ad66c65 100644 --- a/src/endpoints/secrets.js +++ b/src/endpoints/secrets.js @@ -48,6 +48,7 @@ export const SECRET_KEYS = { BLOCKENTROPY: 'api_key_blockentropy', CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts', TAVILY: 'api_key_tavily', + NANOGPT: 'api_key_nanogpt', }; // These are the keys that are safe to expose, even if allowKeysExposure is false From e7522bba763f405fa651729e41c42bb4e70b2f07 Mon Sep 17 00:00:00 2001 From: dylan Date: Tue, 29 Oct 2024 19:38:46 +1300 Subject: [PATCH 3/8] Populate model list from models endpoint --- public/index.html | 66 ++++------------------------------------ public/scripts/openai.js | 17 +++++++++++ 2 files changed, 23 insertions(+), 60 deletions(-) diff --git a/public/index.html b/public/index.html index 6f0c67322..7b3673c0d 100644 --- a/public/index.html +++ b/public/index.html @@ -3094,66 +3094,12 @@
For privacy reasons, your API key will be hidden after you reload the page.
-

NanoGPT Model

- - +
+

NanoGPT Model

+ +

Perplexity API Key

diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 2b2306ec3..cd76fc117 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -1641,6 +1641,23 @@ 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( + $('