From 46d5f79fd96a1a30562e4b4c3be0e0fa08adc2b8 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 18 Mar 2025 21:33:11 +0200 Subject: [PATCH 1/2] OpenRouter: Allow applying prompt post-processing Fixes #3689 --- public/index.html | 16 +++++++++------- public/scripts/openai.js | 2 +- src/endpoints/backends/chat-completions.js | 17 +++++++++-------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/public/index.html b/public/index.html index ec0e088a3..d82d7f833 100644 --- a/public/index.html +++ b/public/index.html @@ -3415,13 +3415,6 @@
-

Prompt Post-Processing

-

@@ -3440,6 +3433,15 @@

+
+

Prompt Post-Processing

+ +
diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 80d1e9ed7..36e224bf9 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -2020,6 +2020,7 @@ async function sendOpenAIRequest(type, messages, signal) { 'reasoning_effort': String(oai_settings.reasoning_effort), 'enable_web_search': Boolean(oai_settings.enable_web_search), 'request_images': Boolean(oai_settings.request_images), + 'custom_prompt_post_processing': oai_settings.custom_prompt_post_processing, }; if (!canMultiSwipe && ToolManager.canPerformToolCalls(type)) { @@ -2100,7 +2101,6 @@ async function sendOpenAIRequest(type, messages, signal) { generate_data['custom_include_body'] = oai_settings.custom_include_body; generate_data['custom_exclude_body'] = oai_settings.custom_exclude_body; generate_data['custom_include_headers'] = oai_settings.custom_include_headers; - generate_data['custom_prompt_post_processing'] = oai_settings.custom_prompt_post_processing; } if (isCohere) { diff --git a/src/endpoints/backends/chat-completions.js b/src/endpoints/backends/chat-completions.js index 4d2eee774..e8d3842ad 100644 --- a/src/endpoints/backends/chat-completions.js +++ b/src/endpoints/backends/chat-completions.js @@ -1121,14 +1121,6 @@ router.post('/generate', function (request, response) { mergeObjectWithYaml(bodyParams, request.body.custom_include_body); mergeObjectWithYaml(headers, request.body.custom_include_headers); - - if (request.body.custom_prompt_post_processing) { - console.info('Applying custom prompt post-processing of type', request.body.custom_prompt_post_processing); - request.body.messages = postProcessPrompt( - request.body.messages, - request.body.custom_prompt_post_processing, - getPromptNames(request)); - } } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.PERPLEXITY) { apiUrl = API_PERPLEXITY; apiKey = readSecret(request.user.directories, SECRET_KEYS.PERPLEXITY); @@ -1160,6 +1152,15 @@ router.post('/generate', function (request, response) { return response.status(400).send({ error: true }); } + const postProcessTypes = [CHAT_COMPLETION_SOURCES.CUSTOM, CHAT_COMPLETION_SOURCES.OPENROUTER]; + if (postProcessTypes.includes(request.body.chat_completion_source) && request.body.custom_prompt_post_processing) { + console.info('Applying custom prompt post-processing of type', request.body.custom_prompt_post_processing); + request.body.messages = postProcessPrompt( + request.body.messages, + request.body.custom_prompt_post_processing, + getPromptNames(request)); + } + // A few of OpenAIs reasoning models support reasoning effort if ([CHAT_COMPLETION_SOURCES.CUSTOM, CHAT_COMPLETION_SOURCES.OPENAI].includes(request.body.chat_completion_source)) { if (['o1', 'o3-mini', 'o3-mini-2025-01-31'].includes(request.body.model)) { From fcaea46a54fdcda610dbbdd94446ee6b383dddde Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 18 Mar 2025 23:17:26 +0200 Subject: [PATCH 2/2] Apply post-process before setting cache at depth --- src/endpoints/backends/chat-completions.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/endpoints/backends/chat-completions.js b/src/endpoints/backends/chat-completions.js index e8d3842ad..a39dcbe2f 100644 --- a/src/endpoints/backends/chat-completions.js +++ b/src/endpoints/backends/chat-completions.js @@ -1048,6 +1048,15 @@ router.post('/generate', function (request, response) { let bodyParams; const isTextCompletion = Boolean(request.body.model && TEXT_COMPLETION_MODELS.includes(request.body.model)) || typeof request.body.messages === 'string'; + const postProcessTypes = [CHAT_COMPLETION_SOURCES.CUSTOM, CHAT_COMPLETION_SOURCES.OPENROUTER]; + if (Array.isArray(request.body.messages) && postProcessTypes.includes(request.body.chat_completion_source) && request.body.custom_prompt_post_processing) { + console.info('Applying custom prompt post-processing of type', request.body.custom_prompt_post_processing); + request.body.messages = postProcessPrompt( + request.body.messages, + request.body.custom_prompt_post_processing, + getPromptNames(request)); + } + if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.OPENAI) { apiUrl = new URL(request.body.reverse_proxy || API_OPENAI).toString(); apiKey = request.body.reverse_proxy ? request.body.proxy_password : readSecret(request.user.directories, SECRET_KEYS.OPENAI); @@ -1152,15 +1161,6 @@ router.post('/generate', function (request, response) { return response.status(400).send({ error: true }); } - const postProcessTypes = [CHAT_COMPLETION_SOURCES.CUSTOM, CHAT_COMPLETION_SOURCES.OPENROUTER]; - if (postProcessTypes.includes(request.body.chat_completion_source) && request.body.custom_prompt_post_processing) { - console.info('Applying custom prompt post-processing of type', request.body.custom_prompt_post_processing); - request.body.messages = postProcessPrompt( - request.body.messages, - request.body.custom_prompt_post_processing, - getPromptNames(request)); - } - // A few of OpenAIs reasoning models support reasoning effort if ([CHAT_COMPLETION_SOURCES.CUSTOM, CHAT_COMPLETION_SOURCES.OPENAI].includes(request.body.chat_completion_source)) { if (['o1', 'o3-mini', 'o3-mini-2025-01-31'].includes(request.body.model)) {