mirror of
https://github.com/Dolibarr/dolibarr.git
synced 2025-02-20 13:46:52 +01:00
create script for generate_content
This commit is contained in:
parent
5bae20ed3e
commit
89e2fd4e29
90
htdocs/ai/class/ai.class.php
Normal file
90
htdocs/ai/class/ai.class.php
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
/* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
|
||||
* Copyright (C) 2005-2016 Regis Houssin <regis.houssin@inodbox.com>
|
||||
* Copyright (C) 2012 J. Fernando Lagrange <fernando@demo-tic.org>
|
||||
* Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
|
||||
* Copyright (C) 2023 Eric Seigne <eric.seigne@cap-rel.fr>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
70
htdocs/ai/lib/generate_content.lib.php
Normal file
70
htdocs/ai/lib/generate_content.lib.php
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
/* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
|
||||
* Copyright (C) 2005-2016 Regis Houssin <regis.houssin@inodbox.com>
|
||||
* Copyright (C) 2012 J. Fernando Lagrange <fernando@demo-tic.org>
|
||||
* Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
|
||||
* Copyright (C) 2023 Eric Seigne <eric.seigne@cap-rel.fr>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
* 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!!');
|
||||
}
|
||||
|
|
@ -755,7 +755,7 @@ class FormMail extends Form
|
|||
if (!empty($this->withtoccuser) && is_array($this->withtoccuser) && getDolGlobalString('MAIN_MAIL_ENABLED_USER_DEST_SELECT')) {
|
||||
$out .= '<tr><td>';
|
||||
$out .= $langs->trans("MailToCCUsers");
|
||||
$out .= '</td><>';
|
||||
$out .= '</td>';
|
||||
|
||||
// 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 = '<tr>';
|
||||
$out .= '<td>';
|
||||
$out .= $form->textwithpicto($langs->trans('helpWithAi'), $langs->trans("YouCanMakeSomeInstructionForEmail"));
|
||||
$out .= '</td>';
|
||||
|
||||
$out .= '<td>';
|
||||
$out .= '<input type="text" class="quatrevingtpercent" id="instruction" name="instruction" placeholder="message with AI"/>';
|
||||
$out .= '<input type="submit" class="button smallpaddingimp" name="generate_email_content" value="'.$langs->trans('Generate').'" />';
|
||||
$out .= '<input type="hidden" id="csrf_token" name="token" value="'.newToken().'">';
|
||||
$out .= '<input type="text" class="quatrevingtpercent" id="ai_instructions" name="instruction" placeholder="message with AI"/>';
|
||||
$out .= '<button id="generate_button" type="button" class="button smallpaddingimp">'.$langs->trans('Generate').'</button>';
|
||||
$out .= "</td></tr>\n";
|
||||
$out .= "<script type='text/javascript'>
|
||||
$(document).ready(function() {
|
||||
$('#generate_button').click(function() {
|
||||
var instructions = $('#ai_instructions').val();
|
||||
var token = $('#csrf_token').val();
|
||||
|
||||
$.ajax({
|
||||
url: '".$baseUrl."ai/lib/generate_content.lib.php',
|
||||
method: 'POST',
|
||||
dataType: 'json',
|
||||
contentType: 'application/json',
|
||||
data: JSON.stringify({
|
||||
'token': token,
|
||||
'instructions': instructions,
|
||||
}),
|
||||
success: function(response) {
|
||||
console.log(response);
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error('error ajax', status, error);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
";
|
||||
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.
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user