WIP Work on custom local bridge AI

This commit is contained in:
Laurent Destailleur 2024-07-16 18:08:26 +02:00
parent 19462bf5d5
commit 811f4b6c15
3 changed files with 29 additions and 13 deletions

View File

@ -49,11 +49,6 @@ $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;
@ -68,6 +63,7 @@ $formSetup = new FormSetup($db);
$arrayofia = array(
'chatgpt' => 'ChatGPT',
'groq' => 'Groq',
'custom' => 'Custom'
//'gemini' => 'Gemini'
);
@ -86,6 +82,11 @@ foreach ($arrayofia as $ia => $ialabel) {
$item->defaultFieldValue = '';
$item->fieldParams['hideGenerateButton'] = 1;
$item->cssClass = 'minwidth500 text-security';
$item = $formSetup->newItem('AI_API_'.strtoupper($ia).'_URL'); // Name of constant must end with _KEY so it is encrypted when saved into database.
$item->nameText = $langs->trans("AI_API_URL").' ('.$ialabel.')';
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth500';
}
$setupnotempty = + count($formSetup->items);
@ -93,6 +94,14 @@ $setupnotempty = + count($formSetup->items);
$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
// Access control
if (!$user->admin) {
accessforbidden();
}
if (!isModEnabled('ai')) {
accessforbidden('Module AI not activated.');
}
/*
* Actions

View File

@ -80,33 +80,34 @@ class Ai
public function generateContent($instructions, $model = 'auto', $function = 'textgeneration', $format = '')
{
if (empty($this->apiKey)) {
return array('error' => true, 'message' => 'API key is not defined for the AI enabled service '.$this->apiService);
return array('error' => true, 'message' => 'API key is not defined for the AI enabled service ('.$this->apiService.')');
}
if (empty($this->apiEndpoint)) {
if ($function == 'imagegeneration') {
if ($this->apiService == 'chatgpt') {
$this->apiEndpoint = 'https://api.openai.com/v1/images/generations';
$this->apiEndpoint = getDolGlobalString('AI_API_CHATGPT_URL', 'https://api.openai.com/v1/images/generations');
if ($model == 'auto') {
$model = getDolGlobalString('AI_API_CHATGPT_MODEL_IMAGE', 'dall-e-3');
}
}
} elseif ($function == 'audiogeneration') {
if ($this->apiService == 'chatgpt') {
$this->apiEndpoint = 'https://api.openai.com/v1/audio/speech';
$this->apiEndpoint = getDolGlobalString('AI_API_CHATGPT_URL', 'https://api.openai.com/v1/audio/speech');
if ($model == 'auto') {
$model = getDolGlobalString('AI_API_CHATGPT_MODEL_AUDIO', 'tts-1');
}
}
} elseif ($function == 'transcription') {
if ($this->apiService == 'chatgpt') {
$this->apiEndpoint = 'https://api.openai.com/v1/audio/transcriptions';
$this->apiEndpoint = getDolGlobalString('AI_API_CHATGPT_URL', 'https://api.openai.com/v1/transcriptions');
if ($model == 'auto') {
$model = getDolGlobalString('AI_API_CHATGPT_MODEL_TRANSCRIPT', 'whisper-1');
}
}
} elseif ($function == 'translation') {
if ($this->apiService == 'chatgpt') {
$this->apiEndpoint = getDolGlobalString('AI_API_CHATGPT_URL', 'https://api.openai.com/v1/translation');
$this->apiEndpoint = 'https://api.openai.com/v1/audio/translations';
if ($model == 'auto') {
$model = getDolGlobalString('AI_API_CHATGPT_MODEL_TRANSLATE', 'whisper-1');
@ -114,15 +115,20 @@ class Ai
}
} else { // else textgeneration...
if ($this->apiService == 'groq') {
$this->apiEndpoint = 'https://api.groq.com/openai/v1/chat/completions';
$this->apiEndpoint = getDolGlobalString('AI_API_CHATGPT_URL', 'https://api.openai.com/v1/chat/completions');
if ($model == 'auto') {
$model = getDolGlobalString('AI_API_GROK_MODEL_TEXT', 'mixtral-8x7b-32768'); // 'llama3-8b-8192', 'gemma-7b-it'
}
} elseif ($this->apiService == 'chatgpt') {
$this->apiEndpoint = 'https://api.openai.com/v1/chat/completions';
$this->apiEndpoint = getDolGlobalString('AI_API_CHATGPT_URL', 'https://api.openai.com/v1/chat/completions');
if ($model == 'auto') {
$model = getDolGlobalString('AI_API_CHATGPT_MODEL_TEXT', 'gpt-3.5-turbo');
}
} elseif ($this->apiService == 'custom') {
$this->apiEndpoint = getDolGlobalString('AI_API_CUSTOM_URL', '');
if ($model == 'auto') {
$model = getDolGlobalString('AI_API_CUSTOM_URL', 'gpt-3.5-turbo');
}
}
}
}
@ -169,7 +175,7 @@ class Ai
throw new Exception('API request failed. No http received');
}
if (!empty($response['http_code']) && $response['http_code'] != 200) {
throw new Exception('API request failed with status code ' . $response['http_code']);
throw new Exception('API request on endpoint '.$this->apiEndpoint.' failed with status code ' . $response['http_code']);
}
// Decode JSON response
$decodedResponse = json_decode($response['content'], true);

View File

@ -2480,6 +2480,7 @@ CustomPrompt=Custom prompts
AiDescription=AI (Artificial Intelligence) features
AiDescriptionLong=Provides AI (Artificial Intelligence) features in different parts of the application. Need external AI API.
AI_API_KEY=Key for AI api
AI_API_URL=Endpoint URL for AI api
AI_API_SERVICE=Service to use for AI features
AiSetup=AI module setup
AiCustomPrompt=AI custom prompt
@ -2509,4 +2510,4 @@ WebsiteTemplateWasCopied=The website template(s) "%s" provided by this module ha
EnabledByDefaultAtInstall=Enabled by default at install
VulnerableToRCEAttack=You are vulnerable to RCE attacks by using the custom dol_json_decode function
BlackListWords=Black list of words
AddBlackList=Add to black list
AddBlackList=Add to black list