mirror of
https://github.com/HeyPuter/puter.git
synced 2026-08-23 06:26:55 +00:00
fix: throw error not lorem when moderation fails
Instead of switching to test mode, which responds with "Lorem Ipsum" sample output to test the API, throw an appropriate error message for content moderation failures. This is especially useful when OpenAI's moderation is way way way way too aggressive as this confuses users.
This commit is contained in:
@@ -509,6 +509,12 @@ module.exports = class APIError {
|
||||
status: 400,
|
||||
message: ({ engine, valid_engines }) => `Invalid engine: ${quot(engine)}. Valid engines are: ${valid_engines.map(quot).join(', ')}.`,
|
||||
},
|
||||
|
||||
// Abuse prevention
|
||||
'moderation_failed': {
|
||||
status: 422,
|
||||
message: `Content moderation failed`,
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -324,6 +324,7 @@ class AIChatService extends BaseService {
|
||||
|
||||
if ( ! test_mode && ! await this.moderate(parameters) ) {
|
||||
test_mode = true;
|
||||
throw APIError.create('moderation_failed');
|
||||
}
|
||||
|
||||
if ( ! test_mode ) {
|
||||
@@ -622,6 +623,7 @@ class AIChatService extends BaseService {
|
||||
* Returns true if OpenAI service is unavailable or all messages pass moderation.
|
||||
*/
|
||||
async moderate({ messages }) {
|
||||
if ( process.env.TEST_MODERATION_FAILURE ) return false;
|
||||
for ( const msg of messages ) {
|
||||
const texts = [];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user