From 63d224d8aff3185d22c76b02dc9c0b06efa41d9f Mon Sep 17 00:00:00 2001 From: maver Date: Mon, 24 Jul 2023 17:30:31 +0200 Subject: [PATCH] Refactor and simplify setup method for prompt manager. --- public/script.js | 23 +++++++---------------- public/scripts/openai.js | 22 ++++++++++------------ 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/public/script.js b/public/script.js index 36f93abc7..0da69d741 100644 --- a/public/script.js +++ b/public/script.js @@ -79,7 +79,7 @@ import { import { setOpenAIMessageExamples, setOpenAIMessages, - setupOpenAIPromptManager, + setupChatCompletionPromptManager, prepareOpenAIMessages, sendOpenAIRequest, loadOpenAISettings, @@ -90,8 +90,6 @@ import { openai_messages_count, getTokenCountOpenAI, chat_completion_sources, - setupWindowAiPromptManager, - setupClaudePromptManager, getTokenizerModel, getChatCompletionModel, } from "./scripts/openai.js"; @@ -4562,20 +4560,13 @@ function changeMainAPI() { } switch (oai_settings.chat_completion_source) { - case chat_completion_sources.OPENAI: - setupOpenAIPromptManager(oai_settings); - break; - case chat_completion_sources.CLAUDE: - setupClaudePromptManager(oai_settings); - break; - case chat_completion_sources.WINDOWAI: - setupWindowAiPromptManager(oai_settings); - break; - case chat_completion_sources.OPENROUTER: - setupWindowAiPromptManager(oai_settings); - break; case chat_completion_sources.SCALE: - setupWindowAiPromptManager(oai_settings); + case chat_completion_sources.OPENROUTER: + case chat_completion_sources.WINDOWAI: + case chat_completion_sources.CLAUDE: + case chat_completion_sources.OPENAI: + default: + setupChatCompletionPromptManager(oai_settings); break; } } diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 11bcca1bc..a0fc452a6 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -61,9 +61,7 @@ export { loadOpenAISettings, setOpenAIMessages, setOpenAIMessageExamples, - setupOpenAIPromptManager, - setupClaudePromptManager, - setupWindowAiPromptManager, + setupChatCompletionPromptManager, generateOpenAIPromptCache, prepareOpenAIMessages, sendOpenAIRequest, @@ -294,9 +292,15 @@ function setOpenAIMessageExamples(mesExamplesArray) { } } -function setupOpenAIPromptManager(openAiSettings) { +/** + * One-time setup for prompt manager module. + * + * @param openAiSettings + * @returns {PromptManagerModule|null} + */ +function setupChatCompletionPromptManager(openAiSettings) { // Do not set up prompt manager more than once - if (promptManager) return true; + if (promptManager) return promptManager; promptManager = new PromptManager(); @@ -326,14 +330,8 @@ function setupOpenAIPromptManager(openAiSettings) { promptManager.init(configuration, openAiSettings); promptManager.render(); -} -function setupClaudePromptManager(claudeSettings) { - setupOpenAIPromptManager(claudeSettings); -} - -function setupWindowAiPromptManager(windowAiSettings) { - setupOpenAIPromptManager(windowAiSettings); + return promptManager; } function generateOpenAIPromptCache() {