dolibarr 21.0.0-alpha
generate_content.php
1<?php
2/* Copyright (C) 2008-2011 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
49if (!isModEnabled('ai')) {
50 accessforbidden('Module AI not enabled');
51}
52
53
54/*
55 * View
56 */
57
59
60//get data from AJAX
61$rawData = file_get_contents('php://input');
62$jsonData = json_decode($rawData, true);
63
64if (is_null($jsonData)) {
65 dol_print_error($db, 'data with format JSON valide.');
66}
67$ai = new Ai($db);
68
69// Get parameters
70$function = empty($jsonData['function']) ? 'textgeneration' : $jsonData['function']; // Default value. Can also be 'textgeneration', 'textgenerationemail', 'textgenerationwebpage', 'imagegeneration', 'videogeneration', ...
71$instructions = dol_string_nohtmltag($jsonData['instructions'], 1, 'UTF-8');
72$format = empty($jsonData['format']) ? '' : $jsonData['format'];
73
74$generatedContent = $ai->generateContent($instructions, 'auto', $function, $format);
75
76if (is_array($generatedContent) && $generatedContent['error']) {
77 // Output error
78 if (!empty($generatedContent['code']) && $generatedContent['code'] == 429) {
79 print "Quota or allowed period exceeded. Retry Later !";
80 } elseif ($generatedContent['code'] >= 400) {
81 print "Error : " . $generatedContent['message'];
82 print '<br><a href="'.DOL_MAIN_URL_ROOT.'/ai/admin/setup.php">'.$langs->trans('ErrorGoToModuleSetup').'</a>';
83 } else {
84 print "Error returned by API call: " . $generatedContent['message'];
85 }
86} else {
87 if ($function == 'textgenerationemail' || $function == 'textgenerationwebpage') {
88 print dolPrintHTML($generatedContent); // Note that common HTML tags are NOT escaped (but a sanitization is done)
89 } elseif ($function == 'imagegeneration') {
90 // TODO
91 } elseif ($function == 'videogeneration') {
92 // TODO
93 } elseif ($function == 'audiogeneration') {
94 // TODO
95 } else {
96 // Default case 'textgeneration'
97 print dolPrintText($generatedContent); // Note that common HTML tags are NOT escaped (but a sanitization is done)
98 }
99}
Class for AI.
Definition ai.class.php:35
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.
dolPrintText($s)
Return a string label (possible on several lines and that should not contains any HTML) ready to be o...
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.