Enhance AI features.

This commit is contained in:
ldestailleur 2025-04-07 21:44:26 +02:00
parent 08700647e6
commit 9f7f1dbeff
2 changed files with 47 additions and 17 deletions

View file

@ -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 '<br><a href="'.DOL_MAIN_URL_ROOT.'/ai/admin/setup.php">'.$langs->trans('ErrorGoToModuleSetup').'</a>';
} 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 {

View file

@ -111,6 +111,8 @@ class FormAI extends Form
$langs->load("other");
$messageaiwait = '<i class="fa fa-spinner fa-spin fa-2x fa-fw valignmiddle marginrightonly"></i>'.$langs->trans("AIProcessingPleaseWait", getDolGlobalString('AI_API_SERVICE', 'chatgpt'));
$htmlContent = preg_replace('/[^a-z0-9_]/', '', $htmlContent);
$out = '<!-- getSectionForAIEnhancement -->';
@ -135,7 +137,7 @@ class FormAI extends Form
$out .= '</div>';
$out .= '<div id="ai_status_message'.$htmlContent.'" class="fieldrequired hideobject marginrightonly margintoponly">';
$out .= '<i class="fa fa-spinner fa-spin fa-2x fa-fw valignmiddle marginrightonly"></i>'.$langs->trans("AIProcessingPleaseWait", getDolGlobalString('AI_API_SERVICE', 'chatgpt'));
$out .= $messageaiwait;
$out .= '</div>';
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);
}
}
});