Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Improve [Explicit] [LLM APIs] [Text Moderation] Error Handling (#290)
Browse files Browse the repository at this point in the history
* Feat [Explicit] [LLM APIs] [Text Moderation] Error Handling

- [+] feat(textmoderation.ts): improve error handling for moderation responses
- [+] feat(textmoderation.ts): add content type check for moderation responses

* Refactor [Explicit] [LLM APIs] [OpenAI] Display Error Messages

- [+] feat(openai.ts): add error handling for text moderation
- [+] refactor(openai.ts): improve error message display for text moderation
  • Loading branch information
H0llyW00dzZ authored Feb 27, 2024
1 parent e66da1f commit 1bf37c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
36 changes: 29 additions & 7 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,38 @@ export class ChatGPTApi implements LLMApi {

// Check if text moderation is enabled and required
if (textmoderation !== false &&
options.whitelist !== true &&
checkprovider !== ServiceProvider.Azure &&
textToModerate) { // Ensure textToModerate is not empty
// Call the moderateText method and handle the result
const moderationResult = await moderateText(moderationPath, textToModerate, OpenaiPath.TextModerationModels.latest);
if (moderationResult) {
options.onFinish(moderationResult); // Finish early if moderationResult is not null
options.whitelist !== true &&
checkprovider !== ServiceProvider.Azure &&
textToModerate) { // Ensure textToModerate is not empty
try {
// Call the moderateText method and handle the result
const moderationResult = await moderateText(moderationPath, textToModerate, OpenaiPath.TextModerationModels.latest);
if (moderationResult) {
options.onFinish(moderationResult); // Finish early if moderationResult is not null
return;
}
} catch (error) {
// Handle errors from moderateText
let errorMessage = 'An unknown error occurred during text moderation.';
if (error instanceof Error) {
errorMessage = error.message;
try {
// Attempt to parse the error message as JSON
const errorObj = JSON.parse(errorMessage.substring(errorMessage.indexOf('{')));
// Pretty-print the JSON error message
errorMessage = prettyObject(errorObj);
} catch {
// If parsing or formatting fails, use the original error message
}
}
// Format the error message for user-friendly display
const formattedError = `We encountered an issue while reviewing your message:\n${errorMessage}`;
// Use the onFinish callback or similar to display the error in the chat interface
options.onFinish(formattedError);
return;
}
}

const messages = options.messages.map((v) => ({
role: v.role,
content: visionModel ? v.content : getMessageTextContent(v),
Expand Down
9 changes: 7 additions & 2 deletions app/client/platforms/textmoderation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ export async function sendModerationRequest(
});

if (!moderationResponse.ok) {
// Handle non-2xx responses
throw new Error(`[${moderationResponse.status}] Failed to get moderation response`);
const errorBody = await moderationResponse.text(); // Attempt to read the response body
throw new Error(`[${moderationResponse.status}] Failed to get moderation response: ${errorBody}`);
}

const contentType = moderationResponse.headers.get("Content-Type");
if (!contentType || !contentType.includes("application/json")) {
throw new Error("Unexpected content type received from moderation response");
}

const moderationJson = await moderationResponse.json();
Expand Down

0 comments on commit 1bf37c0

Please sign in to comment.