Add OpenRouter filtered reason display

This commit is contained in:
Cohee 2024-01-23 00:10:53 +02:00
parent 958cf6a373
commit 107fe85543
2 changed files with 12 additions and 1 deletions

View File

@ -1160,6 +1160,7 @@ function tryParseStreamingError(response, decoded) {
} }
checkQuotaError(data); checkQuotaError(data);
checkModerationError(data);
if (data.error) { if (data.error) {
toastr.error(data.error.message || response.statusText, 'Chat Completion API'); 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) { async function sendWindowAIRequest(messages, signal, stream) {
if (!('ai' in window)) { if (!('ai' in window)) {
return showWindowExtensionError(); return showWindowExtensionError();
@ -1688,6 +1698,7 @@ async function sendOpenAIRequest(type, messages, signal) {
const data = await response.json(); const data = await response.json();
checkQuotaError(data); checkQuotaError(data);
checkModerationError(data);
if (data.error) { if (data.error) {
toastr.error(data.error.message || response.statusText, 'API returned an error'); toastr.error(data.error.message || response.statusText, 'API returned an error');

View File

@ -831,7 +831,7 @@ router.post('/generate', jsonParser, function (request, response) {
let json = await fetchResponse.json(); let json = await fetchResponse.json();
response.send(json); response.send(json);
console.log(json); console.log(json);
console.log(json?.choices[0]?.message); console.log(json?.choices?.[0]?.message);
} else if (fetchResponse.status === 429 && retries > 0) { } else if (fetchResponse.status === 429 && retries > 0) {
console.log(`Out of quota, retrying in ${Math.round(timeout / 1000)}s`); console.log(`Out of quota, retrying in ${Math.round(timeout / 1000)}s`);
setTimeout(() => { setTimeout(() => {