From 89e2fd4e2991b5d01429fefcbcd695f301875dba Mon Sep 17 00:00:00 2001 From: Lamrani Abdel Date: Wed, 17 Jan 2024 17:53:35 +0100 Subject: [PATCH] create script for generate_content --- htdocs/ai/class/ai.class.php | 90 +++++++++++++++++++++++ htdocs/ai/lib/generate_content.lib.php | 70 ++++++++++++++++++ htdocs/core/class/html.formmail.class.php | 39 +++++++++- 3 files changed, 195 insertions(+), 4 deletions(-) create mode 100644 htdocs/ai/class/ai.class.php create mode 100644 htdocs/ai/lib/generate_content.lib.php diff --git a/htdocs/ai/class/ai.class.php b/htdocs/ai/class/ai.class.php new file mode 100644 index 00000000000..8349d6f3aa5 --- /dev/null +++ b/htdocs/ai/class/ai.class.php @@ -0,0 +1,90 @@ + + * Copyright (C) 2005-2016 Regis Houssin + * Copyright (C) 2012 J. Fernando Lagrange + * Copyright (C) 2015 Raphaël Doursenaud + * Copyright (C) 2023 Eric Seigne + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * or see https://www.gnu.org/ + */ + +/** + * Class for AI + */ +class Ai +{ + + /** + * @var DoliDB $db Database object + */ + protected $db; + /** + * @var string $apiEndpoint + */ + private $apiEndpoint; + + /** + * @var string $apiKey + */ + private $apiKey; + + /** + * Constructor + * + * @param string $apiEndpoint Endpoint of api + * @param boolean $apiKey key of api + */ + public function __construct($apiEndpoint, $apiKey) + { + $this->apiEndpoint = $apiEndpoint; + $this->apiKey = $apiKey; + } + + /** + * Generate response of instructions + * @param string $instructions instruction for generate content + * @return mixed $response + */ + public function generateContent($instructions) + { + try { + $ch = curl_init($this->apiEndpoint); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['prompt' => $instructions])); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Authorization: Bearer ' . $this->apiKey, + 'Content-Type: application/json' + ]); + + $response = curl_exec($ch); + + if (curl_errno($ch)) { + throw new Exception('cURL error: ' . curl_error($ch)); + } + + $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + if ($statusCode != 200) { + throw new Exception('API request failed with status code ' . $statusCode); + } + + return $response; + } catch (Exception $e) { + error_log($e->getMessage()); + return null; + } finally { + curl_close($ch); + } + } +} diff --git a/htdocs/ai/lib/generate_content.lib.php b/htdocs/ai/lib/generate_content.lib.php new file mode 100644 index 00000000000..efe8a019c86 --- /dev/null +++ b/htdocs/ai/lib/generate_content.lib.php @@ -0,0 +1,70 @@ + + * Copyright (C) 2005-2016 Regis Houssin + * Copyright (C) 2012 J. Fernando Lagrange + * Copyright (C) 2015 Raphaël Doursenaud + * Copyright (C) 2023 Eric Seigne + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * or see https://www.gnu.org/ + */ + +/** + * \file htdocs/ai/lib/generate_content.lib.php + * \brief Library of ai script + */ + +if (!defined('NOTOKENRENEWAL')) { + define('NOTOKENRENEWAL', '1'); // Disables token renewal +} +if (!defined('NOREQUIREMENU')) { + define('NOREQUIREMENU', '1'); +} +if (!defined('NOREQUIREHTML')) { + define('NOREQUIREHTML', '1'); +} +if (!defined('NOREQUIREAJAX')) { + define('NOREQUIREAJAX', '1'); +} +if (!defined('NOREQUIRESOC')) { + define('NOREQUIRESOC', '1'); +} + +require_once '../../main.inc.php'; +require_once DOL_DOCUMENT_ROOT.'/ai/class/ai.class.php'; + + +//get data from AJAX +$rawData = file_get_contents('php://input'); +$jsonData = json_decode($rawData, true); + +if (is_null($jsonData)) { + dol_print_error('data with format JSON valide.'); +} +$token = GETPOST('token'); +if ($token !== currentToken()) { // Remplacez 'newToken' par le nom de votre variable de session contenant le token + dol_print_error('CSRF token validation failed.'); + exit; +} +$chatGPT = new Ai('API_ENDPOINT', 'API_KEY'); + +$instructions = dol_string_nohtmltag($jsonData['instructions'], 1, 'UTF-8'); + +$generatedContent = $chatGPT->generateContent($instructions); + +if ($generatedContent) { + print $generatedContent; +} else { + dol_print_error('error!!'); +} diff --git a/htdocs/core/class/html.formmail.class.php b/htdocs/core/class/html.formmail.class.php index 1f99b92d214..7dea09e2e9b 100644 --- a/htdocs/core/class/html.formmail.class.php +++ b/htdocs/core/class/html.formmail.class.php @@ -755,7 +755,7 @@ class FormMail extends Form if (!empty($this->withtoccuser) && is_array($this->withtoccuser) && getDolGlobalString('MAIN_MAIL_ENABLED_USER_DEST_SELECT')) { $out .= ''; $out .= $langs->trans("MailToCCUsers"); - $out .= '<>'; + $out .= ''; // multiselect array convert html entities into options tags, even if we don't want this, so we encode them a second time $tmparray = $this->withtoccuser; @@ -889,7 +889,7 @@ class FormMail extends Form //input prompt AI require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; - if (isModEnabled('ai') && $apiKey = dolibarr_get_const($this->db, 'AI_CHATGPT_API_KEY', 0)) { + if (isModEnabled('ai')) { $out .= $this->getHtmlForInstruction(); } // Message @@ -1376,18 +1376,49 @@ class FormMail extends Form { global $langs, $form; + $baseUrl = dol_buildpath('/', 1); + $out = ''; $out .= ''; $out .= $form->textwithpicto($langs->trans('helpWithAi'), $langs->trans("YouCanMakeSomeInstructionForEmail")); $out .= ''; $out .= ''; - $out .= ''; - $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; $out .= "\n"; + $out .= " + "; return $out; } + + /** * Return templates of email with type = $type_template or type = 'all'. * This search into table c_email_templates. Used by the get_form function.