81 public function generateContent($instructions, $model =
'auto', $function =
'textgeneration', $format =
'')
83 if (empty($this->apiKey)) {
84 return array(
'error' =>
true,
'message' =>
'API key is not defined for the AI enabled service ('.$this->apiService.
')');
89 if (empty($this->apiEndpoint)) {
90 if ($function ==
'imagegeneration') {
91 if ($this->apiService ==
'chatgpt') {
92 $this->apiEndpoint =
getDolGlobalString(
'AI_API_CHATGPT_URL',
'https://api.openai.com/v1').
'/images/generations';
93 } elseif ($this->apiService ==
'groq') {
94 $this->apiEndpoint =
getDolGlobalString(
'AI_API_GROK_URL',
'https://api.groq.com/openai/v1').
'/images/generations';
95 } elseif ($this->apiService ==
'custom') {
96 $this->apiEndpoint =
getDolGlobalString(
'AI_API_CUSTOM_URL',
'').
'/images/generations';
98 } elseif ($function ==
'audiogeneration') {
99 if ($this->apiService ==
'chatgpt') {
100 $this->apiEndpoint =
getDolGlobalString(
'AI_API_CHATGPT_URL',
'https://api.openai.com/v1').
'/audio/speech';
101 } elseif ($this->apiService ==
'groq') {
102 $this->apiEndpoint =
getDolGlobalString(
'AI_API_GROK_URL',
'https://api.groq.com/openai/v1').
'/audio/speech';
103 } elseif ($this->apiService ==
'custom') {
106 } elseif ($function ==
'transcription') {
107 if ($this->apiService ==
'chatgpt') {
108 $this->apiEndpoint =
getDolGlobalString(
'AI_API_CHATGPT_URL',
'https://api.openai.com/v1').
'/transcriptions';
109 } elseif ($this->apiService ==
'groq') {
110 $this->apiEndpoint =
getDolGlobalString(
'AI_API_GROK_URL',
'https://api.groq.com/openai/v1').
'/transcriptions';
111 } elseif ($this->apiService ==
'custom') {
114 } elseif ($function ==
'translation') {
115 if ($this->apiService ==
'chatgpt') {
116 $this->apiEndpoint =
getDolGlobalString(
'AI_API_CHATGPT_URL',
'https://api.openai.com/v1').
'/translations';
117 } elseif ($this->apiService ==
'groq') {
118 $this->apiEndpoint =
getDolGlobalString(
'AI_API_GROK_URL',
'https://api.groq.com/openai/v1').
'/translations';
119 } elseif ($this->apiService ==
'custom') {
123 if ($this->apiService ==
'chatgpt') {
124 $this->apiEndpoint =
getDolGlobalString(
'AI_API_CHATGPT_URL',
'https://api.openai.com/v1').
'/chat/completions';
125 } elseif ($this->apiService ==
'groq') {
126 $this->apiEndpoint =
getDolGlobalString(
'AI_API_GROK_URL',
'https://api.groq.com/openai/v1').
'/chat/completions';
127 } elseif ($this->apiService ==
'custom') {
135 if (empty($model) || $model ==
'auto') {
137 if ($function ==
'imagegeneration') {
138 if ($this->apiService ==
'chatgpt') {
140 } elseif ($this->apiService ==
'groq') {
142 } elseif ($this->apiService ==
'custom') {
145 } elseif ($function ==
'audiogeneration') {
146 if ($this->apiService ==
'chatgpt') {
148 } elseif ($this->apiService ==
'groq') {
150 } elseif ($this->apiService ==
'custom') {
153 } elseif ($function ==
'transcription') {
154 if ($this->apiService ==
'chatgpt') {
156 } elseif ($this->apiService ==
'groq') {
158 } elseif ($this->apiService ==
'custom') {
161 } elseif ($function ==
'translation') {
162 if ($this->apiService ==
'chatgpt') {
164 } elseif ($this->apiService ==
'groq') {
166 } elseif ($this->apiService ==
'custom') {
170 if ($this->apiService ==
'chatgpt') {
172 } elseif ($this->apiService ==
'groq') {
174 } elseif ($this->apiService ==
'custom') {
180 dol_syslog(
"Call API for apiKey=".substr($this->apiKey, 0, 3).
'***********, apiEndpoint='.$this->apiEndpoint.
", model=".$model);
183 if (empty($this->apiEndpoint)) {
184 throw new Exception(
'The AI service '.$this->apiService.
' is not yet supported for the type of request '.$function);
188 $configurations = json_decode($configurationsJson,
true);
193 if (isset($configurations[$function])) {
194 if (isset($configurations[$function][
'prePrompt'])) {
195 $prePrompt = $configurations[$function][
'prePrompt'];
198 if (isset($configurations[$function][
'postPrompt'])) {
199 $postPrompt = $configurations[$function][
'postPrompt'];
202 $fullInstructions = ($prePrompt ? $prePrompt.
' ' :
'').$instructions.($postPrompt ?
'. '.$postPrompt :
'');
227 $payload = json_encode([
229 [
'role' =>
'user',
'content' => $fullInstructions]
236 'Authorization: Bearer ' . $this->apiKey,
237 'Content-Type: application/json'
241 $response =
getURLContent($this->apiEndpoint,
'POST', $payload, 1, $headers, array(
'http',
'https'), $localurl);
243 if (empty($response[
'http_code'])) {
244 throw new Exception(
'API request failed. No http received');
246 if (!empty($response[
'http_code']) && $response[
'http_code'] != 200) {
247 throw new Exception(
'API request on AI endpoint '.$this->apiEndpoint.
' failed with status code '.$response[
'http_code'].(empty($response[
'content']) ?
'' :
' - '.$response[
'content']));
251 dol_syslog(
"response content = ".var_export($response[
'content'],
true));
255 $decodedResponse = json_decode($response[
'content'],
true);
258 $generatedContent = $decodedResponse[
'choices'][0][
'message'][
'content'];
263 if ($format ==
'html') {
265 dol_syslog(
"Result was detected as not HTML so we convert it into HTML.");
266 $generatedContent =
dol_nl2br($generatedContent);
268 dol_syslog(
"Result was detected as already HTML. Do nothing.");
276 return $generatedContent;
278 $errormessage = $e->getMessage();
279 if (!empty($response[
'content'])) {
280 $decodedResponse = json_decode($response[
'content'],
true);
283 if (!empty($decodedResponse[
'error'][
'message'])) {
284 $errormessage .=
' - '.$decodedResponse[
'error'][
'message'];
290 'message' => $errormessage,
291 'code' => (empty($response[
'http_code']) ? 0 : $response[
'http_code']),
292 'curl_error_no' => (!empty($response[
'curl_error_no']) ? $response[
'curl_error_no'] :
''),
294 'service' => $this->apiService,
295 'function' => $function