dolibarr 23.0.3
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$function = empty($jsonData['function']) ? 'textgeneration' : $jsonData['function']; // Default value. Can also be 'textgeneration', 'textgenerationemail', 'textgenerationwebpage', 'imagegeneration', 'videogeneration', ...
79
80$format = empty($jsonData['format']) ? '' : $jsonData['format']; // Can be '' for text, 'html', ...
81
82if ($format == "html") {
83 $instructions = $jsonData['instructions'];
84} else {
85 $instructions = dol_string_nohtmltag($jsonData['instructions'], 2, 'UTF-8');
86}
87
88
89// Note: The option AI_DEBUG will generate a log file dolibarr_ai.log when calling generateContent()
90dol_syslog("generate_content: function=".$function." format=".$format." instruction=".dol_substr($instructions, 0, 200));
91
92$generatedContent = $ai->generateContent($instructions, 'auto', $function, $format);
93
94if (empty($instructions)) {
95 http_response_code(400);
96 print "Error : empty message.";
97} elseif (is_null($generatedContent) || (is_array($generatedContent) && $generatedContent['error'])) {
98 // Output error
99 if (!empty($generatedContent['code']) && $generatedContent['code'] == 429) {
100 http_response_code($generatedContent['code']);
101 print "Quota or allowed period exceeded. Retry Later !";
102 } elseif (!empty($generatedContent['code']) && $generatedContent['code'] >= 400) {
103 http_response_code($generatedContent['code']);
104 print "Error : " . $generatedContent['message'];
105 print '<br><a href="'.DOL_MAIN_URL_ROOT.'/ai/admin/setup.php">'.$langs->trans('ErrorGoToModuleSetup').'</a>';
106 } elseif (!empty($generatedContent['message'])) {
107 http_response_code($generatedContent['code']);
108 print "Error returned by API call: " . $generatedContent['message'];
109 } else {
110 http_response_code($generatedContent['code']);
111 print "Error API returned no answer";
112 }
113} else {
114 if ($function == 'textgenerationemail' || $function == 'textgenerationwebpage') {
115 print dolPrintHTML($generatedContent); // Note that common HTML tags are NOT escaped (but a sanitization is done)
116 } elseif ($function == 'imagegeneration') {
117 // TODO
118 } elseif ($function == 'videogeneration') {
119 // TODO
120 } elseif ($function == 'audiogeneration') {
121 // TODO
122 } else {
123 // Default case 'textgeneration', 'texttranslation', 'textsummarize'
124 if ($format == "html") {
125 print dolPrintHTML($generatedContent);
126 } else {
127 // We must not use dolPrintText because dolPrintText format data including accent in htmlentities for a HTML output. We need a non formatted output.
128 //print dol_string_onlythesehtmltags('"Ça va" est une expression française et ceci une balise <a> html', 1, 1, 1);
129 print dol_string_onlythesehtmltags($generatedContent, 1, 1, 1);
130 }
131 }
132}
Class for AI.
Definition ai.class.php:36
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.
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.