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 .= '