diff --git a/htdocs/ai/admin/setup.php b/htdocs/ai/admin/setup.php index 80ecd7828ef..2f1852ddd29 100644 --- a/htdocs/ai/admin/setup.php +++ b/htdocs/ai/admin/setup.php @@ -192,31 +192,35 @@ if (getDolGlobalString("AI_API_SERVICE")) { $functioncode = GETPOST('functioncode'); $out = ''; - if ($functioncode) { - $labeloffeature = empty($arrayofaifeatures[GETPOST('functioncode')]['label']) ? 'Undefined' : $arrayofaifeatures[GETPOST('functioncode')]['label']; + //if ($functioncode) { + $key = 'textgenerationother'; // The HTML ID of field to fill + + $labeloffeature = empty($arrayofaifeatures[$key]['label']) ? 'Undefined' : $arrayofaifeatures[$key]['label']; //$out .= $langs->trans("Test").' '.$labeloffeature.'...

'; - if (GETPOST('functioncode') == 'textgenerationemail') { - $key = 'textgenerationemail'; // The HTML ID of field to fill + //if (GETPOST('functioncode') == 'textgenerationemail') { - include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php'; - $showlinktoai = $key; // 'textgeneration', 'imagegeneration', ... - $showlinktoailabel = $langs->trans("Test").' '.$labeloffeature; - $showlinktolayout = 0; - $formmail = new FormMail($db); - $htmlname = $key; + print '
'; + //print '
'; - // Fill $out - include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php'; + include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php'; + include_once DOL_DOCUMENT_ROOT."/core/class/html.formai.class.php"; + $formai = new FormAI($db); + $formmail = new FormMail($db); - $out .= '
'; - } else { - $out .= $langs->trans("FeatureNotYetAvailable").'

'; - $functioncode = ''; - } - } + $showlinktoai = $key; // 'textgeneration', 'imagegeneration', ... + $showlinktoailabel = $langs->trans("AITestText"); + $showlinktolayout = 0; + $htmlname = $key; + // Fill $out + include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php'; + + $out .= '
'; // The div + //} + + /* if (!$functioncode) { // Combo list of AI features $out .= ''; } + */ + print $out; print ''; diff --git a/htdocs/ai/ajax/generate_content.php b/htdocs/ai/ajax/generate_content.php index fe3cf9e11b0..371e039c173 100644 --- a/htdocs/ai/ajax/generate_content.php +++ b/htdocs/ai/ajax/generate_content.php @@ -77,7 +77,7 @@ $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']; +$format = empty($jsonData['format']) ? '' : $jsonData['format']; // Can be '' for text, 'html', ... if ($function == 'texttranslation') { $instructions = $jsonData['instructions']; diff --git a/htdocs/ai/class/ai.class.php b/htdocs/ai/class/ai.class.php index 198706f0036..ad1642c52d3 100644 --- a/htdocs/ai/class/ai.class.php +++ b/htdocs/ai/class/ai.class.php @@ -82,7 +82,7 @@ class Ai { global $dolibarr_main_data_root; - if (empty($this->apiKey)) { + if (empty($this->apiKey) && in_array($this->apiService, array('chatgpt', 'groq'))) { return array('error' => true, 'message' => 'API key is not defined for the AI enabled service ('.$this->apiService.')'); } @@ -293,12 +293,20 @@ class Ai } } + // Decode JSON response $decodedResponse = json_decode($response['content'], true); // Extraction content - $generatedContent = $decodedResponse['choices'][0]['message']['content']; - + if (!empty($decodedResponse['error'])) { + if (is_scalar($decodedResponse['error'])) { + $generatedContent = $decodedResponse['error']; + } else { + $generatedContent = var_export($decodedResponse['error'], true); + } + } else { + $generatedContent = $decodedResponse['choices'][0]['message']['content']; + } dol_syslog("generatedContent=".dol_trunc($generatedContent, 50)); // If content is not HTML, we convert it into HTML diff --git a/htdocs/core/class/html.formai.class.php b/htdocs/core/class/html.formai.class.php index e3f835cac50..2af0bd127f6 100644 --- a/htdocs/core/class/html.formai.class.php +++ b/htdocs/core/class/html.formai.class.php @@ -91,12 +91,12 @@ class FormAI extends Form } /** - * Return Html code for AI instruction of message and autofill result + * Return Html code for AI instructions of message and autofill result * * @param string $function Function ('textgenerationmail', 'textgenerationwebpage', ...) * @param string $format Format for output ('', 'html', ...) * @param string $htmlContent HTML name of WYSIWYG field - * @return string HTML code to ask AI instruction and autofill result + * @return string HTML code to ask AI instructions and autofill result */ public function getSectionForAIEnhancement($function = 'textgeneration', $format = '', $htmlContent = 'message') { @@ -108,17 +108,18 @@ class FormAI extends Form $htmlContent = preg_replace('/[^a-z0-9_]/', '', $htmlContent); - $out = '