From 7151723894403595be174710ac76323cfb445da4 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Wed, 29 Jan 2025 16:45:15 -0500 Subject: [PATCH] dev: use message normalization and clean up openai service --- .../src/modules/puterai/AIChatService.js | 5 +++ .../puterai/OpenAICompletionService.js | 44 +------------------ 2 files changed, 6 insertions(+), 43 deletions(-) diff --git a/src/backend/src/modules/puterai/AIChatService.js b/src/backend/src/modules/puterai/AIChatService.js index 1ffa336c..40c606cd 100644 --- a/src/backend/src/modules/puterai/AIChatService.js +++ b/src/backend/src/modules/puterai/AIChatService.js @@ -27,6 +27,7 @@ const { TypedValue } = require("../../services/drivers/meta/Runtime"); const { Context } = require("../../util/context"); const { AsModeration } = require("./lib/AsModeration"); const FunctionCalling = require("./lib/FunctionCalling"); +const Messages = require("./lib/Messages"); // Maximum number of fallback attempts when a model fails, including the first attempt const MAX_FALLBACKS = 3 + 1; // includes first attempt @@ -351,6 +352,10 @@ class AIChatService extends BaseService { test_mode = true; } + if ( parameters.messages ) { + Messages.normalize_messages(parameters.messages); + } + if ( ! test_mode && ! await this.moderate(parameters) ) { test_mode = true; } diff --git a/src/backend/src/modules/puterai/OpenAICompletionService.js b/src/backend/src/modules/puterai/OpenAICompletionService.js index 2e8ec184..d8b7efec 100644 --- a/src/backend/src/modules/puterai/OpenAICompletionService.js +++ b/src/backend/src/modules/puterai/OpenAICompletionService.js @@ -251,48 +251,6 @@ class OpenAICompletionService extends BaseService { model = model ?? this.get_default_model(); - for ( let i = 0; i < messages.length; i++ ) { - let msg = messages[i]; - if ( typeof msg === 'string' ) msg = { content: msg }; - if ( typeof msg !== 'object' ) { - throw new Error('each message must be a string or an object'); - } - if ( ! msg.role ) msg.role = 'user'; - if ( msg.role === 'assistant' && ! msg.content ) { - continue; - } - if ( ! msg.content ) { - throw new Error('each message must have a `content` property'); - } - - const texts = []; - 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.hasOwnProperty('text') ) || - o.type === 'text')).map(o => o.text)); - } - else texts.push(msg.content.text); - } - - this.log.noticeme('OPENAI MODERATION CHECK', { - moderation, - context_value: Context.get('moderated'), - }) - if ( moderation && ! Context.get('moderated') ) { - console.log('RAN MODERATION'); - for ( const text of texts ) { - const moderation_result = await this.check_moderation(text); - if ( moderation_result.flagged ) { - throw new Error('message is not allowed'); - } - } - } - - messages[i] = msg; - } - messages.unshift({ role: 'system', content: 'You are running inside a Puter app.', @@ -320,7 +278,7 @@ class OpenAICompletionService extends BaseService { if ( ! msg.content ) continue; if ( typeof msg.content !== 'object' ) continue; - const content = smol.ensure_array(msg.content); + const content = msg.content; for ( const o of content ) { if ( ! o.hasOwnProperty('image_url') ) continue;