dolibarr 25.0.0-alpha
Ai Class Reference

Class for AI feature. More...

Collaboration diagram for Ai:

Public Member Functions

 __construct ($db)
 Constructor.
 
 getApiService ()
 get API Service
 
 generateContent ($instructions, $model='auto', $function='textgeneration', $format='', $moreheaders=array(), $moreendpoint='')
 Generate the response of an AI prompt.
 
 getPayloadToConvertRawFileIntoText ($filepath)
 Build the payload of the first call of the raw file mode: the call that converts the raw binary file into text, so the OCR is done by the AI service instead of by a local tool.
 
 decodeJsonIntoArray ($json, $type)
 Decode JSON into array.
 

Detailed Description

Class for AI feature.

Definition at line 36 of file ai.class.php.

Constructor & Destructor Documentation

◆ __construct()

Ai::__construct ( $db)

Constructor.

Parameters
DoliDB$dbDatabase handler

Definition at line 79 of file ai.class.php.

References getDolGlobalString().

Member Function Documentation

◆ decodeJsonIntoArray()

Ai::decodeJsonIntoArray ( $json,
$type )

Decode JSON into array.

Parameters
array<string,mixed>$json JSON (The structure of this var can't be guess, it change at each call, depending on AI, so we must se a strict type for it)
string$typeType of document to get ('supplier_invoice', 'salary', 'social_contribution', 'expense', 'thirdparty', ...)
Returns
array<string,mixed> Array of values

Definition at line 637 of file ai.class.php.

References dol_concat(), dol_stringtotime(), and dol_strlen().

◆ generateContent()

Ai::generateContent ( $instructions,
$model = 'auto',
$function = 'textgeneration',
$format = '',
$moreheaders = array(),
$moreendpoint = '' )

Generate the response of an AI prompt.

Parameters
string|array<mixed,mixed>$instructions String instruction to generate content (or file path) or array of payload or ID of file with function threads
string$modelModel name ('gpt-4.1-turbo', 'gpt-4.1', 'dall-e-3', ...)
string$functionCode of the feature we want to use ('textgeneration', 'transcription', 'audiogeneration', 'imagegeneration', 'translation', 'docparsing')
string$formatFormat for output ('', 'html', ...)
array<string,string>$moreheaders More headers
string$moreendpointAdd a part to endpoint url
Returns
string|array{error:bool,message:string,code?:int,curl_error_no?:int,format?:string,service?:string,model?:string,function?:string} $response Text or array if error

Definition at line 109 of file ai.class.php.

References dol_nl2br(), dol_now(), dol_print_date(), dol_syslog(), dol_textishtml(), dol_trunc(), dolChmod(), getDolGlobalInt(), getDolGlobalString(), getListOfAIServices(), and getURLContent().

◆ getApiService()

Ai::getApiService ( )

get API Service

Returns
string API service

Definition at line 93 of file ai.class.php.

◆ getPayloadToConvertRawFileIntoText()

Ai::getPayloadToConvertRawFileIntoText ( $filepath)

Build the payload of the first call of the raw file mode: the call that converts the raw binary file into text, so the OCR is done by the AI service instead of by a local tool.

For Mistral, the call targets the dedicated OCR endpoint of the service, for the other OpenAI-compatible services, it is a multimodal chat completion with the file encoded into base64. The model used is AI_API_<SERVICE>_MODEL_DOCPARSING or its default value. As generateContent() does not complete the payload when it is an array, the model is set here.

Parameters
string$filepathFull path of the file to send
Returns
array<string,mixed> Array with keys 'payload' and 'function' for generateContent(), or array with key 'error' set if the service or the file type is not supported

Definition at line 579 of file ai.class.php.

References dol_basename(), dol_mimetype(), dol_osencode(), getDolGlobalString(), and getListOfAIServices().


The documentation for this class was generated from the following file: