From 59b33236696bf195b3679b02ea4e7767e7fef915 Mon Sep 17 00:00:00 2001 From: lamrani abdelwadoud Date: Wed, 7 Feb 2024 19:41:50 +0100 Subject: [PATCH] NEW fuctionnality for stock configuration prompt in AI module (#28030) --- htdocs/ai/admin/custom_prompt.php | 232 ++++++++++++++++++++++++++++ htdocs/ai/ajax/generate_content.php | 2 +- htdocs/ai/class/ai.class.php | 23 ++- htdocs/ai/lib/ai.lib.php | 5 + htdocs/langs/en_US/ai.lang | 4 +- 5 files changed, 262 insertions(+), 4 deletions(-) create mode 100644 htdocs/ai/admin/custom_prompt.php diff --git a/htdocs/ai/admin/custom_prompt.php b/htdocs/ai/admin/custom_prompt.php new file mode 100644 index 0000000000000..379aa079d40ad --- /dev/null +++ b/htdocs/ai/admin/custom_prompt.php @@ -0,0 +1,232 @@ + + * Copyright (C) 2022 Alice Adminson + * + * 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 . + */ + +/** + * \file ai/admin/custom_prompt.php + * \ingroup ai + * \brief Ai other custom page. + */ + +// Load Dolibarr environment +require '../../main.inc.php'; + +global $langs, $user, $conf; + +// Libraries +require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php"; +require_once '../lib/ai.lib.php'; + +// Parameters +$action = GETPOST('action', 'aZ09'); +$backtopage = GETPOST('backtopage', 'alpha'); +$modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php + +$value = GETPOST('value', 'alpha'); +$label = GETPOST('label', 'alpha'); +$scandir = GETPOST('scan_dir', 'alpha'); +$type = 'myobject'; + +$error = 0; +$setupnotempty = 0; + +// Access control +if (!$user->admin) { + accessforbidden(); +} + + +// Set this to 1 to use the factory to manage constants. Warning, the generated module will be compatible with version v15+ only +$useFormSetup = 1; + +if (!class_exists('FormSetup')) { + require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; +} + +$formSetup = new FormSetup($db); + +// Setup conf AI_PROMPT +$item = $formSetup->newItem('AI_CONFIGURATIONS_PROMPT'); +$item->defaultFieldValue = ''; + +$setupnotempty =+ count($formSetup->items); + +$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); + +/* + * Actions + */ + +$modulename = GETPOST('module_name'); +$pre_prompt = GETPOST('prePrompt', 'alpha'); +$post_prompt = GETPOST('postPrompt', 'alpha'); + // get all configs in const AI + + $currentConfigurationsJson = dolibarr_get_const($db, 'AI_CONFIGURATIONS_PROMPT', $conf->entity); + $currentConfigurations = json_decode($currentConfigurationsJson, true); + +if ($action == 'update' && !GETPOST('cancel')) { + $error = 0; + if (empty($modulename)) { + $error++; + setEventMessages($langs->trans('ErrorInputRequired'), null, 'errors'); + } + if (!is_array($currentConfigurations)) { + $currentConfigurations = []; + } + + if (empty($pre_prompt) && empty($post_prompt)) { + if (isset($currentConfigurations[$modulename])) { + unset($currentConfigurations[$modulename]); + } + } else { + $currentConfigurations[$modulename] = [ + 'prePrompt' => $pre_prompt, + 'postPrompt' => $post_prompt, + ]; + } + + $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE); + $result = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity); + if (!$error) { + if ($result) { + header("Location: ".$_SERVER['PHP_SELF']); + setEventMessages($langs->trans("CongigurationUpdated"), null, 'mesgs'); + exit; + } else { + setEventMessages($langs->trans("ErrorUpdating"), null, 'errors'); + } + } +} + +/* + * View + */ + + $form = new Form($db); + + $help_url = ''; + $page_name = "AiCustomPrompt"; + + llxHeader('', $langs->trans($page_name), $help_url); + + // Subheader + $linkback = ''.$langs->trans("BackToModuleList").''; + + print load_fiche_titre($langs->trans($page_name), $linkback, 'title_setup'); + + // Configuration header + $head = aiAdminPrepareHead(); + print dol_get_fiche_head($head, 'custom', $langs->trans($page_name), -1, "fa-microchip"); + +if ($action == 'edit') { + $out = '
'; + $out .='
'; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= '
'.$langs->trans('Settings').''.$langs->trans('Value').'
'; + $out .= ''.$langs->trans("Module").''; + $out .= ''; + + $sql = "SELECT name FROM llx_const WHERE name LIKE 'MAIN_MODULE_%' AND value = '1'"; + $resql = $db->query($sql); + + if ($resql) { + $out .= ''; + $out .= '
'; + $out .= 'pre-Prompt'; + $out .= ''; + $out .= ''; + $out .= '
'; + $out .= 'Post-prompt'; + $out .= ''; + $out .= ''; + $out .= '
'; + $out .= $form->buttonsSaveCancel("Save", 'Cancel'); + + $out .= '
'; + $out .= ""; + + print $out; + + print '
'; +} elseif (!empty($formSetup->items)) { + print $formSetup->generateOutput(); + print '
'; + print ''.$langs->trans("Modify").''; + print '
'; +} else { + print '
'.$langs->trans("NothingToSetup"); +} + +if (empty($setupnotempty)) { + print '
'.$langs->trans("NothingToSetup"); +} + + +// Page end +print dol_get_fiche_end(); + +llxFooter(); +$db->close(); diff --git a/htdocs/ai/ajax/generate_content.php b/htdocs/ai/ajax/generate_content.php index c38ab4ef54b99..34d7f1967530d 100644 --- a/htdocs/ai/ajax/generate_content.php +++ b/htdocs/ai/ajax/generate_content.php @@ -58,7 +58,7 @@ $instructions = dol_string_nohtmltag($jsonData['instructions'], 1, 'UTF-8'); -$generatedContent = $chatGPT->generateContent($instructions); +$generatedContent = $chatGPT->generateContent($instructions, 'gpt-3.5-turbo', 'MAILING'); if (is_array($generatedContent) && $generatedContent['error']) { print "Error : " . $generatedContent['message']; diff --git a/htdocs/ai/class/ai.class.php b/htdocs/ai/class/ai.class.php index dd103c837e001..2771c1b0e0610 100644 --- a/htdocs/ai/class/ai.class.php +++ b/htdocs/ai/class/ai.class.php @@ -58,16 +58,35 @@ public function __construct($db) * Generate response of instructions * @param string $instructions instruction for generate content * @param string $model model name (chat,text,image...) + * @param string $moduleName Name of module * @return mixed $response */ - public function generateContent($instructions, $model = 'gpt-3.5-turbo') + public function generateContent($instructions, $model = 'gpt-3.5-turbo', $moduleName = 'MAILING') { + global $conf; try { + $configurationsJson = dolibarr_get_const($this->db, 'AI_CONFIGURATIONS_PROMPT', $conf->entity); + $configurations = json_decode($configurationsJson, true); + + $prePrompt = ''; + $postPrompt = ''; + + if (isset($configurations[$moduleName])) { + if (isset($configurations[$moduleName]['prePrompt'])) { + $prePrompt = $configurations[$moduleName]['prePrompt']; + } + + if (isset($configurations[$moduleName]['postPrompt'])) { + $postPrompt = $configurations[$moduleName]['postPrompt']; + } + } + $fullInstructions = $prePrompt.' '.$instructions.' .'.$postPrompt; + $ch = curl_init($this->apiEndpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ 'messages' => [ - ['role' => 'user', 'content' => $instructions] + ['role' => 'user', 'content' => $fullInstructions] ], 'model' => $model ])); diff --git a/htdocs/ai/lib/ai.lib.php b/htdocs/ai/lib/ai.lib.php index ad5977573087e..d9b0b7858e929 100644 --- a/htdocs/ai/lib/ai.lib.php +++ b/htdocs/ai/lib/ai.lib.php @@ -40,6 +40,11 @@ function aiAdminPrepareHead() $head[$h][2] = 'settings'; $h++; + $head[$h][0] = dol_buildpath("/ai/admin/custom_prompt.php", 1); + $head[$h][1] = $langs->trans("CustomPrompt"); + $head[$h][2] = 'custom'; + $h++; + /* $head[$h][0] = dol_buildpath("/ai/admin/myobject_extrafields.php", 1); $head[$h][1] = $langs->trans("ExtraFields"); diff --git a/htdocs/langs/en_US/ai.lang b/htdocs/langs/en_US/ai.lang index 60332e76457e9..6d39073c8e2c6 100644 --- a/htdocs/langs/en_US/ai.lang +++ b/htdocs/langs/en_US/ai.lang @@ -1,4 +1,6 @@ AiDescription=AI (Artificial Intelligence) features AiDescriptionLong=Provides AI (Artificial Intelligence) features in different part of the application. Need external AI API. AI_KEY_API_CHATGPT= Key for ChatGPT IA api -AiSetup=AI module setup \ No newline at end of file +AiSetup=AI module setup +AiCustomPrompt=AI customs prompt +AI_CONFIGURATIONS_PROMPT=Custom prompt \ No newline at end of file