dolibarr 25.0.0-alpha
UniversalLLMAdapter Class Reference

Public Member Functions

 __construct (string $type, string $key, string $baseUrl, string $model, int $timeout)
 Constructor.
 
 generate (string $system, string $userMsg, string $mode='text', array $attachments=array(), array $history=array())
 Generate a response using the configured LLM provider.
 

Private Member Functions

 callOpenAI (string $sys, string $msg, string $mode='text', array $attachments=array(), array $history=array())
 Call OpenAI-compatible API.
 
 callAnthropic (string $sys, string $msg, string $mode='text', array $attachments=array(), array $history=array())
 Call Anthropic API (Claude)
 
 callGoogle (string $sys, string $msg, string $mode='text', array $attachments=array(), array $history=array())
 Call Google Gemini API.
 
 clearModelFailure ()
 Drop a recorded model failure once that model answers again.
 
 recordModelFailure (int $httpCode, string $msg)
 Record a "model not found / retired" type provider failure into the constant AI_MODEL_RUNTIME_FAILURE, displayed as a warning banner on the models admin page.
 
 encodeRequestForLog (array $data)
 JSON-encode a request for the log with base64 payloads removed, so the 60k truncation in ai_log_request() never swallows the text prompt (for Anthropic the document block precedes the text block) and document contents never land in llx_ai_request_log.
 
 curl (string $url, array $data, array $headers, bool $isClaude=false, bool $isGemini=false)
 Execute HTTP Request via cURL.
 

Detailed Description

Definition at line 27 of file llmadapter.class.php.

Constructor & Destructor Documentation

◆ __construct()

UniversalLLMAdapter::__construct ( string $type,
string $key,
string $baseUrl,
string $model,
int $timeout )

Constructor.

Parameters
string$typeThe LLM type
string$keyAPI Key
string$baseUrlAPI Base URL
string$modelModel name
int$timeoutTimeout in seconds

Definition at line 62 of file llmadapter.class.php.

References type.

Member Function Documentation

◆ callAnthropic()

UniversalLLMAdapter::callAnthropic ( string $sys,
string $msg,
string $mode = 'text',
array $attachments = array(),
array $history = array() )
private

Call Anthropic API (Claude)

Parameters
string$sysSystem prompt
string$msgUser message
string$modeResponse mode (default: text)
array<int,array{mime:string,data:string}>$attachments Optional attachments sent as native multimodal parts
array<int,array{role:string,text:string}>$history Optional prior turns inserted before the current query
Returns
string|null Response content or null on failure

Definition at line 179 of file llmadapter.class.php.

References curl(), and encodeRequestForLog().

Referenced by generate().

◆ callGoogle()

UniversalLLMAdapter::callGoogle ( string $sys,
string $msg,
string $mode = 'text',
array $attachments = array(),
array $history = array() )
private

Call Google Gemini API.

Parameters
string$sysSystem prompt
string$msgUser message
string$modeResponse mode (default: text)
array<int,array{mime:string,data:string}>$attachments Optional attachments sent as native multimodal parts
array<int,array{role:string,text:string}>$history Optional prior turns inserted before the current query
Returns
string|null Response content or null on failure

Definition at line 231 of file llmadapter.class.php.

References curl(), and encodeRequestForLog().

Referenced by generate().

◆ callOpenAI()

UniversalLLMAdapter::callOpenAI ( string $sys,
string $msg,
string $mode = 'text',
array $attachments = array(),
array $history = array() )
private

Call OpenAI-compatible API.

Parameters
string$sysSystem prompt
string$msgUser message
string$mode'json' or 'text'
array<int,array{mime:string,data:string}>$attachments Optional attachments sent as native multimodal parts
array<int,array{role:string,text:string}>$history Optional prior turns inserted before the current query
Returns
string|null Response content or null on failure

Definition at line 107 of file llmadapter.class.php.

References curl(), and encodeRequestForLog().

Referenced by generate().

◆ clearModelFailure()

UniversalLLMAdapter::clearModelFailure ( )
private

Drop a recorded model failure once that model answers again.

Returns
void

Definition at line 285 of file llmadapter.class.php.

References $conf, dolibarr_del_const(), and getDolGlobalString().

Referenced by curl().

◆ curl()

UniversalLLMAdapter::curl ( string $url,
array $data,
array $headers,
bool $isClaude = false,
bool $isGemini = false )
private

Execute HTTP Request via cURL.

Parameters
string$urlTarget API URL
array<string,mixed>$data Request payload (keys are strings, values vary)
array<int,string>$headers List of HTTP headers (indexed array of strings)
bool$isClaudeFlag to handle Anthropic response format
bool$isGeminiFlag to handle Gemini response format
Returns
string|null Returns the extracted text, an error message, or null

Definition at line 385 of file llmadapter.class.php.

References clearModelFailure(), getURLContent(), and recordModelFailure().

Referenced by callAnthropic(), callGoogle(), and callOpenAI().

◆ encodeRequestForLog()

UniversalLLMAdapter::encodeRequestForLog ( array $data)
private

JSON-encode a request for the log with base64 payloads removed, so the 60k truncation in ai_log_request() never swallows the text prompt (for Anthropic the document block precedes the text block) and document contents never land in llx_ai_request_log.

Parameters
array<string,mixed>$data Request payload
Returns
string JSON with long base64 runs replaced by a placeholder
Parameters
string[]$mRegex matches: [1] = base64-like run
Returns
string

Definition at line 350 of file llmadapter.class.php.

Referenced by callAnthropic(), callGoogle(), and callOpenAI().

◆ generate()

UniversalLLMAdapter::generate ( string $system,
string $userMsg,
string $mode = 'text',
array $attachments = array(),
array $history = array() )

Generate a response using the configured LLM provider.

Attachments are sent as NATIVE multimodal parts — instead of inlining base64 into the text prompt — which is what allows the provider to actually see the file (vision/document understanding).

Parameters
string$systemThe system prompt/instruction
string$userMsgThe specific user query
string$mode'json' for strict JSON (MCP), 'text' for legacy (default)
array<int,array{mime:string,data:string}>$attachments Optional documents/images, each entry is array('mime' => 'image/png', 'data' => '<base64>')
array<int,array{role:string,text:string}>$history Optional prior conversation turns (role 'user'|'assistant'), sent as native multi-turn messages before the current query. Caller sanitizes and caps them.
Returns
string|null The text response from the AI or null on failure

Definition at line 85 of file llmadapter.class.php.

References callAnthropic(), callGoogle(), callOpenAI(), and type.

◆ recordModelFailure()

UniversalLLMAdapter::recordModelFailure ( int $httpCode,
string $msg )
private

Record a "model not found / retired" type provider failure into the constant AI_MODEL_RUNTIME_FAILURE, displayed as a warning banner on the models admin page.

Runtime is the only fully reliable signal for a retired model: a provider's listing can be incomplete, and a listed model can still be rejected at call time (e.g. models restricted to existing customers).

Parameters
int$httpCodeHTTP status returned by the provider
string$msgError message returned by the provider
Returns
void

Definition at line 315 of file llmadapter.class.php.

References $conf, dol_now(), dol_trunc(), dolibarr_set_const(), and getDolGlobalString().

Referenced by curl().


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