29if (!defined(
'NOTOKENRENEWAL')) {
30 define(
'NOTOKENRENEWAL',
'1');
32if (!defined(
'NOREQUIREMENU')) {
33 define(
'NOREQUIREMENU',
'1');
35if (!defined(
'NOREQUIREHTML')) {
36 define(
'NOREQUIREHTML',
'1');
38if (!defined(
'NOREQUIREAJAX')) {
39 define(
'NOREQUIREAJAX',
'1');
41if (!defined(
'NOREQUIRESOC')) {
42 define(
'NOREQUIRESOC',
'1');
45require
'../../main.inc.php';
46require_once DOL_DOCUMENT_ROOT.
'/core/lib/geturl.lib.php';
47require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
48require_once DOL_DOCUMENT_ROOT.
'/ai/lib/ai.lib.php';
58 http_response_code(403);
59 print json_encode(array(
'error' =>
'Module not enabled'));
63if (empty($user->admin) && !$user->hasRight(
'ai',
'assistant',
'use')) {
64 http_response_code(403);
65 print json_encode(array(
'error' =>
'Permission denied'));
70if (empty($serviceKey) || $serviceKey ==
'-1') {
71 print json_encode(array(
'service' =>
'',
'models' => array()));
78 $cache = json_decode($cacheraw,
true);
79 if (is_array($cache) && !empty($cache[
'service']) && $cache[
'service'] === $serviceKey
80 && !empty($cache[
'ts']) && (
dol_now() - (
int) $cache[
'ts']) < 3600
81 && !empty($cache[
'models']) && is_array($cache[
'models'])) {
82 print json_encode(array(
'service' => $serviceKey,
'models' => $cache[
'models'],
'cached' =>
true));
88$adapterType = $servicesList[$serviceKey][
'adapter_type'] ??
'openai';
89$defUrl = $servicesList[$serviceKey][
'url'] ??
'';
90$baseUrl = rtrim(
getDolGlobalString(
'AI_API_'.strtoupper($serviceKey).
'_URL') ?: $defUrl,
'/');
93if (preg_match(
'/^crypt:/', $apiKey)) {
96if (empty($apiKey) || empty($baseUrl)) {
97 print json_encode(array(
'service' => $serviceKey,
'models' => array(),
'error' =>
'Provider not configured'));
102if ($adapterType ===
'anthropic') {
103 $headers = array(
'x-api-key: '.$apiKey,
'anthropic-version: 2023-06-01');
104 $res =
getURLContent($baseUrl.
'/models?limit=100',
'GET',
'', 1, $headers, array(
'http',
'https'), 2);
105 $json = json_decode($res[
'content'] ??
'',
true);
106 foreach ((array) ($json[
'data'] ?? array()) as $m) {
107 if (!empty($m[
'id'])) {
108 $models[] = (
string) $m[
'id'];
111} elseif ($adapterType ===
'google') {
112 $res =
getURLContent($baseUrl.
'/models?pageSize=200&key='.urlencode($apiKey),
'GET',
'', 1, array(), array(
'http',
'https'), 2);
113 $json = json_decode($res[
'content'] ??
'',
true);
114 foreach ((array) ($json[
'models'] ?? array()) as $m) {
115 if (!empty($m[
'name'])) {
116 $models[] = preg_replace(
'/^models\//',
'', (
string) $m[
'name']);
121 $headers = array(
'Authorization: Bearer '.$apiKey);
122 $res =
getURLContent($baseUrl.
'/models',
'GET',
'', 1, $headers, array(
'http',
'https'), 2);
123 $json = json_decode($res[
'content'] ??
'',
true);
124 foreach ((array) ($json[
'data'] ?? array()) as $m) {
125 if (!empty($m[
'id'])) {
126 $models[] = (
string) $m[
'id'];
131$models = array_values(array_unique($models));
135 dolibarr_set_const(
$db,
'AI_MODELS_LIST_CACHE', json_encode(array(
'service' => $serviceKey,
'ts' =>
dol_now(),
'models' => $models)),
'chaine', 0,
'',
$conf->entity);
138print json_encode(array(
'service' => $serviceKey,
'models' => $models,
'cached' =>
false));
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
getListOfAIServices()
Get list of available ai services.
dol_now($mode='gmt')
Return date for now.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1, $timeoutconnect=0, $timeoutresponse=0, $otherCurlOptions=array(), $morelogsuffix='')
Function to get a content from an URL (use proxy if proxy defined).
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.