dolibarr 24.0.0-beta
generate_content.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2008-2025 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2016 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2012 J. Fernando Lagrange <fernando@demo-tic.org>
5 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
6 * Copyright (C) 2023 Eric Seigne <eric.seigne@cap-rel.fr>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
29if (!defined('NOTOKENRENEWAL')) {
30 define('NOTOKENRENEWAL', '1'); // Disables token renewal
31}
32if (!defined('NOREQUIREMENU')) {
33 define('NOREQUIREMENU', '1');
34}
35if (!defined('NOREQUIREHTML')) {
36 define('NOREQUIREHTML', '1');
37}
38if (!defined('NOREQUIREAJAX')) {
39 define('NOREQUIREAJAX', '1');
40}
41if (!defined('NOREQUIRESOC')) {
42 define('NOREQUIRESOC', '1');
43}
44
45require '../../main.inc.php';
46
47require_once DOL_DOCUMENT_ROOT.'/ai/class/ai.class.php';
48
57if (!isModEnabled('ai')) {
58 accessforbidden('Module AI not enabled');
59}
60
61
62/*
63 * View
64 */
65
67
68//get data from AJAX
69$rawData = file_get_contents('php://input');
70$jsonData = json_decode($rawData, true);
71
72if (is_null($jsonData)) {
73 dol_print_error($db, 'data in POST has not a valid JSON format.');
74}
75$ai = new Ai($db);
76
77// Get parameters
78// Default value is 'textgeneration'.
79// Can also be 'textgeneration', 'textgenerationemail', 'textgenerationwebpage', 'textrephraser', 'textspellchecker', 'imagegeneration', 'videogeneration', ...
80$function = empty($jsonData['function']) ? 'textgeneration' : $jsonData['function'];
81
82$format = empty($jsonData['format']) ? '' : $jsonData['format']; // Can be '' for text, 'html', ...
83
84if ($format == "html") {
85 $instructions = $jsonData['instructions'];
86} else {
87 $instructions = dol_string_nohtmltag($jsonData['instructions'], 2, 'UTF-8');
88}
89
90
91// Note: The option AI_DEBUG will generate a log file dolibarr_ai.log when calling generateContent()
92dol_syslog("generate_content: function=".$function." format=".$format." instruction=".dol_substr($instructions, 0, 200));
93
94$generatedContent = $ai->generateContent($instructions, 'auto', $function, $format);
95
96if (empty($instructions)) {
97 http_response_code(400);
98 print "Error : empty message.";
99} elseif (is_null($generatedContent) || (is_array($generatedContent) && $generatedContent['error'])) {
100 // Output error
101 if (!empty($generatedContent['code']) && $generatedContent['code'] == 429) {
102 http_response_code($generatedContent['code']);
103 print "Quota or allowed period exceeded. Retry Later !";
104 } elseif (!empty($generatedContent['code']) && $generatedContent['code'] >= 400) {
105 http_response_code($generatedContent['code']);
106 print "Error : " . $generatedContent['message'];
107 print '<br><a href="'.DOL_MAIN_URL_ROOT.'/ai/admin/setup.php">'.$langs->trans('ErrorGoToModuleSetup').'</a>';
108 } elseif (!empty($generatedContent['message'])) {
109 http_response_code($generatedContent['code']);
110 print "Error returned by API call: " . $generatedContent['message'];
111 } else {
112 http_response_code($generatedContent['code']);
113 print "Error API returned no answer";
114 }
115} else {
116 if ($function == 'textgenerationemail' || $function == 'textgenerationwebpage') {
117 if ($format == 'html') {
118 print dolPrintHTML($generatedContent); // Note that common HTML tags are NOT escaped (but a sanitization is done)
119 } else {
120 // We must not use dolPrintText because dolPrintText format data including accent in htmlentities for a HTML output. We need a non formatted output.
121 //print dol_string_onlythesehtmltags('"Ça va" est une expression française et ceci une balise <a> html', 1, 1, 1);
122 print dol_string_onlythesehtmltags($generatedContent, 1, 1, 1);
123 //print dol_string_nohtmltag($generatedContent);
124 }
125 } elseif ($function == 'imagegeneration') {
126 // TODO
127 } elseif ($function == 'videogeneration') {
128 // TODO
129 } elseif ($function == 'audiogeneration') {
130 // TODO
131 } else {
132 // Default case 'textgeneration', 'texttranslation', 'textsummarize'
133 if ($format == "html") {
134 print dolPrintHTML($generatedContent);
135 } else {
136 // We must not use dolPrintText because dolPrintText format data including accent in htmlentities for a HTML output. We need a non formatted output.
137 //print dol_string_onlythesehtmltags('"Ça va" est une expression française et ceci une balise <a> html', 1, 1, 1);
138 print dol_string_onlythesehtmltags($generatedContent, 1, 1, 1);
139 }
140 }
141}
Class for AI feature.
Definition ai.class.php:36
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dolPrintHTML($s, $allowiframe=0, $moreallowedtags=array())
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.
isModEnabled($module)
Is Dolibarr module enabled.
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.