From 9f7f1dbeffd931db4dd5e4fddbf553a35bf11958 Mon Sep 17 00:00:00 2001 From: ldestailleur Date: Mon, 7 Apr 2025 21:44:26 +0200 Subject: [PATCH] Enhance AI features. --- htdocs/ai/ajax/generate_content.php | 20 ++++++++--- htdocs/core/class/html.formai.class.php | 44 +++++++++++++++++-------- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/htdocs/ai/ajax/generate_content.php b/htdocs/ai/ajax/generate_content.php index 2c2d01d0269..2eab4c88f73 100644 --- a/htdocs/ai/ajax/generate_content.php +++ b/htdocs/ai/ajax/generate_content.php @@ -77,26 +77,38 @@ $ai = new Ai($db); // Get parameters $function = empty($jsonData['function']) ? 'textgeneration' : $jsonData['function']; // Default value. Can also be 'textgeneration', 'textgenerationemail', 'textgenerationwebpage', 'imagegeneration', 'videogeneration', ... $instructions = dol_string_nohtmltag($jsonData['instructions'], 1, 'UTF-8'); -$format = empty($jsonData['format']) ? '' : $jsonData['format']; // Can be '' for text, 'html', ... +$format = empty($jsonData['format']) ? '' : $jsonData['format']; // Can be '' for text, 'html', ... -if ($function == 'texttranslation' && $format == "html") { +if ($format == "html") { $instructions = $jsonData['instructions']; } else { $instructions = dol_string_nohtmltag($jsonData['instructions'], 1, 'UTF-8'); } + +// Note: The option AI_DEBUG will generate a log file dolibarr_ai.log when calling generateContent() +dol_syslog("generate_content: function=".$function." format=".$format." instruction=".dol_substr($instructions, 0, 200)); + + $generatedContent = $ai->generateContent($instructions, 'auto', $function, $format); -if (is_null($generatedContent) || (is_array($generatedContent) && $generatedContent['error'])) { +if (empty($instructions)) { + http_response_code(400); + print "Error : empty message."; +} elseif (is_null($generatedContent) || (is_array($generatedContent) && $generatedContent['error'])) { // Output error if (!empty($generatedContent['code']) && $generatedContent['code'] == 429) { + http_response_code($generatedContent['code']); print "Quota or allowed period exceeded. Retry Later !"; } elseif (!empty($generatedContent['code']) && $generatedContent['code'] >= 400) { + http_response_code($generatedContent['code']); print "Error : " . $generatedContent['message']; print '
'.$langs->trans('ErrorGoToModuleSetup').''; } elseif (!empty($generatedContent['message'])) { + http_response_code($generatedContent['code']); print "Error returned by API call: " . $generatedContent['message']; } else { + http_response_code($generatedContent['code']); print "Error API returned no answer"; } } else { @@ -109,7 +121,7 @@ if (is_null($generatedContent) || (is_array($generatedContent) && $generatedCont } elseif ($function == 'audiogeneration') { // TODO } else { - // Default case 'textgeneration' + // Default case 'textgeneration', 'texttranslation', 'textsummarize' if ($format == "html") { print dolPrintHTML($generatedContent); // Note that common HTML tags are NOT escaped (but a sanitization is done) } else { diff --git a/htdocs/core/class/html.formai.class.php b/htdocs/core/class/html.formai.class.php index 1cb1d8396fa..170d04ed380 100644 --- a/htdocs/core/class/html.formai.class.php +++ b/htdocs/core/class/html.formai.class.php @@ -111,6 +111,8 @@ class FormAI extends Form $langs->load("other"); + $messageaiwait = ''.$langs->trans("AIProcessingPleaseWait", getDolGlobalString('AI_API_SERVICE', 'chatgpt')); + $htmlContent = preg_replace('/[^a-z0-9_]/', '', $htmlContent); $out = ''; @@ -135,7 +137,7 @@ class FormAI extends Form $out .= ''; $out .= '
'; - $out .= ''.$langs->trans("AIProcessingPleaseWait", getDolGlobalString('AI_API_SERVICE', 'chatgpt')); + $out .= $messageaiwait; $out .= '
'; if ($function == 'imagegeneration') { @@ -191,10 +193,12 @@ class FormAI extends Form var userprompt = $('#ai_instructions".$htmlContent."').val(); var timeoutfinished = 0; var apicallfinished = 0; + instructions = ''; - htmlname = '".$htmlContent."'; + htmlname = '".dol_escape_js($htmlContent)."'; format = '".dol_escape_js($format)."'; functionai = $(element).data('functionai'); + texttomodify = ''; console.log('htmlname='+htmlname); if ($('#'+htmlname).is('div')) { @@ -211,9 +215,12 @@ class FormAI extends Form } } */ - - lang = $('#ai_translation'+htmlname+'_select').val(); - instructions = 'Translate the following text to ' + lang + ': ' + texttomodify; + if (!texttomodify) { + instructions = ''; + } else { + lang = $('#ai_translation'+htmlname+'_select').val(); + instructions = 'Translate only the following text to ' + lang + ': ' + texttomodify; + } } else if (functionai == 'textsummarize') { width = $('#ai_summarize'+htmlname+'_select').val(); arr = width.split('_'); @@ -221,7 +228,7 @@ class FormAI extends Form unit = arr[1]; if (width == undefined || unit == undefined){ console.log('Bad value so we choose 20 words') - width = '20'; + width = '50'; unit = 'w'; } switch(unit){ @@ -244,7 +251,9 @@ class FormAI extends Form instructions = userprompt; } + /* Show message API running */ $('#ai_status_message".$htmlContent."').show(); + $('#ai_status_message".$htmlContent."').html('".dol_escape_js($messageaiwait)."'); $('.icon-container .loader').show(); setTimeout(function() { @@ -252,7 +261,7 @@ class FormAI extends Form $('#ai_status_message".$htmlContent."').hide(); }, 30000); - console.log(instructions); + console.log('Instruction forged by javascript = '+instructions); callAIGenerator(functionai, instructions, format, htmlname); } @@ -324,9 +333,11 @@ class FormAI extends Form } }, error: function(xhr, status, error) { - alert(error); - console.error('error ajax', status, error); - $('#ai_status_message').hide(); + /* alert(error); */ + console.log('error ajax', status, error); + /*$('#ai_status_message'+htmlname).hide();*/ + $('#ai_status_message'+htmlname).val(error); + $('#ai_status_message'+htmlname).html(error); } }); } else { @@ -374,9 +385,16 @@ class FormAI extends Form $('#ai_dropdown'+htmlname).hide(); }, error: function(xhr, status, error) { - alert(error); - console.error('error ajax', status, error); - $('#ai_status_message'+htmlname).hide(); + /* alert(error); */ + console.log('error ajax ', status, error); + /* $('#ai_status_message'+htmlname).hide(); */ + if (xhr.responseText) { + $('#ai_status_message'+htmlname).val(xhr.responseText); + $('#ai_status_message'+htmlname).html(xhr.responseText); + } else { + $('#ai_status_message'+htmlname).val(error); + $('#ai_status_message'+htmlname).html(error); + } } });