From 107fe8554382be96cf86486f91b52cb1144d0cdf Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 23 Jan 2024 00:10:53 +0200 Subject: [PATCH] Add OpenRouter filtered reason display --- public/scripts/openai.js | 11 +++++++++++ src/endpoints/backends/chat-completions.js | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/public/scripts/openai.js b/public/scripts/openai.js index c16280491..eeb1758d7 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -1160,6 +1160,7 @@ function tryParseStreamingError(response, decoded) { } checkQuotaError(data); + checkModerationError(data); if (data.error) { toastr.error(data.error.message || response.statusText, 'Chat Completion API'); @@ -1187,6 +1188,15 @@ function checkQuotaError(data) { } } +function checkModerationError(data) { + const moderationError = data?.error?.message?.includes('requires moderation'); + if (moderationError) { + const moderationReason = `Reasons: ${data?.error?.metadata?.reasons?.join(', ') ?? '(N/A)'}`; + const flaggedText = data?.error?.metadata?.flagged_input ?? '(N/A)'; + toastr.info(flaggedText, moderationReason, { timeOut: 10000 }); + } +} + async function sendWindowAIRequest(messages, signal, stream) { if (!('ai' in window)) { return showWindowExtensionError(); @@ -1688,6 +1698,7 @@ async function sendOpenAIRequest(type, messages, signal) { const data = await response.json(); checkQuotaError(data); + checkModerationError(data); if (data.error) { toastr.error(data.error.message || response.statusText, 'API returned an error'); diff --git a/src/endpoints/backends/chat-completions.js b/src/endpoints/backends/chat-completions.js index 7fc294e04..d3f7c026f 100644 --- a/src/endpoints/backends/chat-completions.js +++ b/src/endpoints/backends/chat-completions.js @@ -831,7 +831,7 @@ router.post('/generate', jsonParser, function (request, response) { let json = await fetchResponse.json(); response.send(json); console.log(json); - console.log(json?.choices[0]?.message); + console.log(json?.choices?.[0]?.message); } else if (fetchResponse.status === 429 && retries > 0) { console.log(`Out of quota, retrying in ${Math.round(timeout / 1000)}s`); setTimeout(() => {