26require_once DOL_DOCUMENT_ROOT.
"/core/lib/admin.lib.php";
27require_once DOL_DOCUMENT_ROOT.
'/core/lib/geturl.lib.php';
80 public function generateContent($instructions, $model =
'auto', $function =
'textgeneration', $format =
'')
82 if (empty($this->apiKey)) {
83 return array(
'error' =>
true,
'message' =>
'API key is not defined for the AI enabled service '.$this->apiService);
86 if (empty($this->apiEndpoint)) {
87 if ($function ==
'imagegeneration') {
88 if ($this->apiService ==
'chatgpt') {
89 $this->apiEndpoint =
'https://api.openai.com/v1/images/generations';
90 if ($model ==
'auto') {
94 } elseif ($function ==
'audiotext') {
95 if ($this->apiService ==
'chatgpt') {
96 $this->apiEndpoint =
'https://api.openai.com/v1/audio/speech';
97 if ($model ==
'auto') {
101 } elseif ($function ==
'transcription') {
102 if ($this->apiService ==
'chatgpt') {
103 $this->apiEndpoint =
'https://api.openai.com/v1/audio/transcriptions';
104 if ($model ==
'auto') {
108 } elseif ($function ==
'translation') {
109 if ($this->apiService ==
'chatgpt') {
110 $this->apiEndpoint =
'https://api.openai.com/v1/audio/translations';
111 if ($model ==
'auto') {
116 if ($this->apiService ==
'groq') {
117 $this->apiEndpoint =
'https://api.groq.com/openai/v1/chat/completions';
118 if ($model ==
'auto') {
121 } elseif ($this->apiService ==
'chatgpt') {
122 $this->apiEndpoint =
'https://api.openai.com/v1/chat/completions';
123 if ($model ==
'auto') {
130 dol_syslog(
"Call API for apiEndpoint=".$this->apiEndpoint.
" apiKey=".substr($this->apiKey, 0, 3).
'***********, model='.$model);
133 if (empty($this->apiEndpoint)) {
134 throw new Exception(
'The AI service '.$this->apiService.
' is not yet supported for the type of request '.$function);
138 $configurations = json_decode($configurationsJson,
true);
143 if (isset($configurations[$function])) {
144 if (isset($configurations[$function][
'prePrompt'])) {
145 $prePrompt = $configurations[$function][
'prePrompt'];
148 if (isset($configurations[$function][
'postPrompt'])) {
149 $postPrompt = $configurations[$function][
'postPrompt'];
152 $fullInstructions = $prePrompt.
' '.$instructions.
' .'.$postPrompt;
155 $payload = json_encode([
157 [
'role' =>
'user',
'content' => $fullInstructions]
163 'Authorization: Bearer ' . $this->apiKey,
164 'Content-Type: application/json'
166 $response =
getURLContent($this->apiEndpoint,
'POST', $payload, 1, $headers);
168 if (empty($response[
'http_code'])) {
169 throw new Exception(
'API request failed. No http received');
171 if (!empty($response[
'http_code']) && $response[
'http_code'] != 200) {
172 throw new Exception(
'API request failed with status code ' . $response[
'http_code']);
175 $decodedResponse = json_decode($response[
'content'],
true);
178 $generatedContent = $decodedResponse[
'choices'][0][
'message'][
'content'];
180 dol_syslog(
"generatedContent=".$generatedContent);
183 if ($format ==
'html') {
185 dol_syslog(
"Result was detected as not HTML so we convert it into HTML.");
186 $generatedContent =
dol_nl2br($generatedContent);
188 dol_syslog(
"Result was detected as already HTML. Do nothing.");
196 return $generatedContent;
198 $errormessage = $e->getMessage();
199 if (!empty($response[
'content'])) {
200 $decodedResponse = json_decode($response[
'content'],
true);
203 if (!empty($decodedResponse[
'error'][
'message'])) {
204 $errormessage .=
' - '.$decodedResponse[
'error'][
'message'];
210 'message' => $errormessage,
211 'code' => (empty($response[
'http_code']) ? 0 : $response[
'http_code']),
212 'curl_error_no' => (empty($response[
'curl_error_no']) ? $response[
'curl_error_no'] :
''),
214 'service' => $this->apiService,
215 'function'=>$function
generateContent($instructions, $model='auto', $function='textgeneration', $format='')
Generate response of instructions.
__construct($db)
Constructor.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
dol_textishtml($msg, $option=0)
Return if a text is a html content.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
Function to get a content from an URL (use proxy if proxy defined).