Skip to content

Commit

Permalink
Feat [Explicit] [LLM APIs] [Text Moderation Fallback] Error Handling (C…
Browse files Browse the repository at this point in the history
…hatGPTNextWeb#294)

- [+] feat(textmoderation.ts): implement error handling for fallback moderation request
- [+] refactor(textmoderation.ts): remove redundant error logging from moderateText function

Note: By implementing in this manner, the issue of repetition should be resolved when an error is first encountered during text moderation. Essentially, both components already possess their own error handling mechanisms.
  • Loading branch information
H0llyW00dzZ authored Feb 28, 2024
1 parent 0a5488e commit a6ab683
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/client/platforms/textmoderation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,23 @@ export async function sendModerationRequest(
if (!moderationResult.flagged) {
const stable = OpenaiPath.TextModerationModels.stable; // Fall back to "stable" if "latest" is still false
moderationPayload.model = stable;
// Initiate a fallback request only if the initial moderation did not flag the content as problematic.
// This condition assumes that the initial request was successful but did not identify any content violations.
// Should the fallback request encounter an error, it triggers an exception, halting further processing.
// This adjustment specifically addresses the issue of redundant logging when an error occurs during the initial request.
const fallbackModerationResponse = await fetch(moderationPath, {
method: "POST",
body: JSON.stringify(moderationPayload),
headers: getHeaders(),
});

if (!fallbackModerationResponse.ok) {
// Immediately handle the error without attempting to parse further
const errorBody = await fallbackModerationResponse.json();
console.error(`Fallback moderation request failed: ${JSON.stringify(errorBody)}`);
throw new Error(`[${fallbackModerationResponse.status}] Failed to get fallback moderation response: ${JSON.stringify(errorBody)}`);
}

const fallbackModerationJson = await fallbackModerationResponse.json();

if (fallbackModerationJson.results && fallbackModerationJson.results.length > 0) {
Expand Down Expand Up @@ -149,7 +160,6 @@ export async function moderateText(
}
}
} catch (e) {
console.error("[Request] failed to make a moderation request", e);
throw e; // Rethrow the error to be handled by the caller
}

Expand Down

0 comments on commit a6ab683

Please sign in to comment.