29if (!defined(
'NOTOKENRENEWAL')) {
30 define(
'NOTOKENRENEWAL',
'1');
32if (!defined(
'NOREQUIREMENU')) {
33 define(
'NOREQUIREMENU',
'1');
35if (!defined(
'NOREQUIREHTML')) {
36 define(
'NOREQUIREHTML',
'1');
38if (!defined(
'NOREQUIREAJAX')) {
39 define(
'NOREQUIREAJAX',
'1');
41if (!defined(
'NOREQUIRESOC')) {
42 define(
'NOREQUIRESOC',
'1');
45require
'../../main.inc.php';
47require_once DOL_DOCUMENT_ROOT.
'/ai/class/ai.class.php';
57if (!isModEnabled(
'ai')) {
69$rawData = file_get_contents(
'php://input');
70$jsonData = json_decode($rawData,
true);
72if (is_null($jsonData)) {
78$function = empty($jsonData[
'function']) ?
'textgeneration' : $jsonData[
'function'];
80$format = empty($jsonData[
'format']) ?
'' : $jsonData[
'format'];
82if ($format ==
"html") {
83 $instructions = $jsonData[
'instructions'];
90dol_syslog(
"generate_content: function=".$function.
" format=".$format.
" instruction=".
dol_substr($instructions, 0, 200));
93$generatedContent = $ai->generateContent($instructions,
'auto', $function, $format);
95if (empty($instructions)) {
96 http_response_code(400);
97 print
"Error : empty message.";
98} elseif (is_null($generatedContent) || (is_array($generatedContent) && $generatedContent[
'error'])) {
100 if (!empty($generatedContent[
'code']) && $generatedContent[
'code'] == 429) {
101 http_response_code($generatedContent[
'code']);
102 print
"Quota or allowed period exceeded. Retry Later !";
103 } elseif (!empty($generatedContent[
'code']) && $generatedContent[
'code'] >= 400) {
104 http_response_code($generatedContent[
'code']);
105 print
"Error : " . $generatedContent[
'message'];
106 print
'<br><a href="'.DOL_MAIN_URL_ROOT.
'/ai/admin/setup.php">'.$langs->trans(
'ErrorGoToModuleSetup').
'</a>';
107 } elseif (!empty($generatedContent[
'message'])) {
108 http_response_code($generatedContent[
'code']);
109 print
"Error returned by API call: " . $generatedContent[
'message'];
111 http_response_code($generatedContent[
'code']);
112 print
"Error API returned no answer";
115 if ($function ==
'textgenerationemail' || $function ==
'textgenerationwebpage') {
117 } elseif ($function ==
'imagegeneration') {
119 } elseif ($function ==
'videogeneration') {
121 } elseif ($function ==
'audiogeneration') {
125 if ($format ==
"html") {
dolPrintHTML($s, $allowiframe=0)
Return a string (that can be on several lines) ready to be output on a HTML page.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_string_onlythesehtmltags($stringtoclean, $cleanalsosomestyles=1, $removeclassattribute=1, $cleanalsojavascript=0, $allowiframe=0, $allowed_tags=array(), $allowlink=0, $allowscript=0, $allowstyle=0, $allowphp=0)
Clean a string to keep only desirable HTML tags.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.