dolibarr 20.0.0
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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
28if (!defined('NOTOKENRENEWAL')) {
29 define('NOTOKENRENEWAL', '1'); // Disables token renewal
30}
31if (!defined('NOREQUIREMENU')) {
32 define('NOREQUIREMENU', '1');
33}
34if (!defined('NOREQUIREHTML')) {
35 define('NOREQUIREHTML', '1');
36}
37if (!defined('NOREQUIREAJAX')) {
38 define('NOREQUIREAJAX', '1');
39}
40if (!defined('NOREQUIRESOC')) {
41 define('NOREQUIRESOC', '1');
42}
43
44require '../../main.inc.php';
45
46require_once DOL_DOCUMENT_ROOT.'/ai/class/ai.class.php';
47
48
49/*
50 * View
51 */
52
54
55//get data from AJAX
56$rawData = file_get_contents('php://input');
57$jsonData = json_decode($rawData, true);
58
59if (is_null($jsonData)) {
60 dol_print_error('data with format JSON valide.');
61}
62$ai = new Ai($db);
63
64// Get parameters
65$function = empty($jsonData['function']) ? 'textgeneration' : $jsonData['function']; // Default value. Can also be 'textgenerationemail', 'textgenerationwebpage', ...
66$instructions = dol_string_nohtmltag($jsonData['instructions'], 1, 'UTF-8');
67$format = empty($jsonData['format']) ? '' : $jsonData['format'];
68
69$generatedContent = $ai->generateContent($instructions, 'auto', $function, $format);
70
71if (is_array($generatedContent) && $generatedContent['error']) {
72 // Output error
73 if (!empty($generatedContent['code']) && $generatedContent['code'] == 429) {
74 print "Quota or allowed period exceeded. Retry Later !";
75 } elseif ($generatedContent['code'] >= 400) {
76 print "Error : " . $generatedContent['message'];
77 print '<br><a href="'.DOL_MAIN_URL_ROOT.'/ai/admin/setup.php">'.$langs->trans('ErrorGoToModuleSetup').'</a>';
78 } else {
79 print "Error returned by API call: " . $generatedContent['message'];
80 }
81} else {
82 print $generatedContent;
83}
Class for AI.
Definition ai.class.php:34
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
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.