mirror of
https://github.com/HeyPuter/puter.git
synced 2026-05-04 16:40:41 +00:00
optimization: change for (msg.. messages) in moderation to pass in everything in one call (#1825)
This commit is contained in:
@@ -624,57 +624,42 @@ class AIChatService extends BaseService {
|
||||
*/
|
||||
async moderate({ messages }) {
|
||||
if ( process.env.TEST_MODERATION_FAILURE ) return false;
|
||||
for ( const msg of messages ) {
|
||||
const texts = [];
|
||||
|
||||
// Function calls have no content
|
||||
if ( msg.content === null ) continue;
|
||||
|
||||
if ( typeof msg.content === 'string' ) texts.push(msg.content);
|
||||
else if ( typeof msg.content === 'object' ) {
|
||||
if ( Array.isArray(msg.content) ) {
|
||||
texts.push(...msg.content.filter(o => ( !o.type && o['text'] ) || o.type === 'text').map(o => o.text));
|
||||
}
|
||||
else texts.push(msg.content.text);
|
||||
const fulltext = Messages.extract_text(messages);
|
||||
let mod_last_error = null;
|
||||
let mod_result = null;
|
||||
try {
|
||||
const svc_openai = this.services.get('openai-completion');
|
||||
mod_result = await svc_openai.check_moderation(fulltext);
|
||||
if ( mod_result.flagged ) return false;
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
mod_last_error = e;
|
||||
}
|
||||
try {
|
||||
const svc_claude = this.services.get('claude');
|
||||
const chat = svc_claude.as('puter-chat-completion');
|
||||
const mod = new AsModeration({
|
||||
chat,
|
||||
model: 'claude-3-haiku-20240307',
|
||||
});
|
||||
if ( ! await mod.moderate(fulltext) ) {
|
||||
return false;
|
||||
}
|
||||
mod_last_error = null;
|
||||
console.log("FINISHED MODERATION: ", Date.now() - modStartTime + "ms wasted")
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
mod_last_error = e;
|
||||
}
|
||||
|
||||
const fulltext = texts.join('\n');
|
||||
|
||||
let mod_last_error = null;
|
||||
let mod_result = null;
|
||||
try {
|
||||
const svc_openai = this.services.get('openai-completion');
|
||||
mod_result = await svc_openai.check_moderation(fulltext);
|
||||
if ( mod_result.flagged ) return false;
|
||||
continue;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
mod_last_error = e;
|
||||
}
|
||||
try {
|
||||
const svc_claude = this.services.get('claude');
|
||||
const chat = svc_claude.as('puter-chat-completion');
|
||||
const mod = new AsModeration({
|
||||
chat,
|
||||
model: 'claude-3-haiku-20240307',
|
||||
});
|
||||
if ( ! await mod.moderate(fulltext) ) {
|
||||
return false;
|
||||
}
|
||||
mod_last_error = null;
|
||||
continue;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
mod_last_error = e;
|
||||
}
|
||||
|
||||
if ( mod_last_error ) {
|
||||
this.log.error('moderation error', {
|
||||
fulltext,
|
||||
mod_last_error,
|
||||
});
|
||||
throw new Error('no working moderation service');
|
||||
}
|
||||
if ( mod_last_error ) {
|
||||
this.log.error('moderation error', {
|
||||
fulltext,
|
||||
mod_last_error,
|
||||
});
|
||||
throw new Error('no working moderation service');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user