Conditions | 33 |
Paths | 16471 |
Total Lines | 136 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
84 | public function generateContent($instructions, $model = 'auto', $function = 'textgeneration', $format = '') |
||
85 | { |
||
86 | if (empty($this->apiKey)) { |
||
87 | return array('error' => true, 'message' => 'API key is not defined for the AI enabled service ' . $this->apiService); |
||
88 | } |
||
89 | |||
90 | if (empty($this->apiEndpoint)) { |
||
91 | if ($function == 'imagegeneration') { |
||
92 | if ($this->apiService == 'chatgpt') { |
||
93 | $this->apiEndpoint = 'https://api.openai.com/v1/images/generations'; |
||
94 | if ($model == 'auto') { |
||
95 | $model = getDolGlobalString('AI_API_CHATGPT_MODEL_IMAGE', 'dall-e-3'); |
||
96 | } |
||
97 | } |
||
98 | } elseif ($function == 'audiotext') { |
||
99 | if ($this->apiService == 'chatgpt') { |
||
100 | $this->apiEndpoint = 'https://api.openai.com/v1/audio/speech'; |
||
101 | if ($model == 'auto') { |
||
102 | $model = getDolGlobalString('AI_API_CHATGPT_MODEL_AUDIO', 'tts-1'); |
||
103 | } |
||
104 | } |
||
105 | } elseif ($function == 'transcription') { |
||
106 | if ($this->apiService == 'chatgpt') { |
||
107 | $this->apiEndpoint = 'https://api.openai.com/v1/audio/transcriptions'; |
||
108 | if ($model == 'auto') { |
||
109 | $model = getDolGlobalString('AI_API_CHATGPT_MODEL_TRANSCRIPT', 'whisper-1'); |
||
110 | } |
||
111 | } |
||
112 | } elseif ($function == 'translation') { |
||
113 | if ($this->apiService == 'chatgpt') { |
||
114 | $this->apiEndpoint = 'https://api.openai.com/v1/audio/translations'; |
||
115 | if ($model == 'auto') { |
||
116 | $model = getDolGlobalString('AI_API_CHATGPT_MODEL_TRANSLATE', 'whisper-1'); |
||
117 | } |
||
118 | } |
||
119 | } else { // else textgeneration... |
||
120 | if ($this->apiService == 'groq') { |
||
121 | $this->apiEndpoint = 'https://api.groq.com/openai/v1/chat/completions'; |
||
122 | if ($model == 'auto') { |
||
123 | $model = getDolGlobalString('AI_API_GROK_MODEL_TEXT', 'mixtral-8x7b-32768'); // 'llama3-8b-8192', 'gemma-7b-it' |
||
124 | } |
||
125 | } elseif ($this->apiService == 'chatgpt') { |
||
126 | $this->apiEndpoint = 'https://api.openai.com/v1/chat/completions'; |
||
127 | if ($model == 'auto') { |
||
128 | $model = getDolGlobalString('AI_API_CHATGPT_MODEL_TEXT', 'gpt-3.5-turbo'); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | dol_syslog("Call API for apiEndpoint=" . $this->apiEndpoint . " apiKey=" . substr($this->apiKey, 0, 3) . '***********, model=' . $model); |
||
135 | |||
136 | try { |
||
137 | if (empty($this->apiEndpoint)) { |
||
138 | throw new Exception('The AI service ' . $this->apiService . ' is not yet supported for the type of request ' . $function); |
||
139 | } |
||
140 | |||
141 | $configurationsJson = getDolGlobalString('AI_CONFIGURATIONS_PROMPT'); |
||
142 | $configurations = json_decode($configurationsJson, true); |
||
143 | |||
144 | $prePrompt = ''; |
||
145 | $postPrompt = ''; |
||
146 | |||
147 | if (isset($configurations[$function])) { |
||
148 | if (isset($configurations[$function]['prePrompt'])) { |
||
149 | $prePrompt = $configurations[$function]['prePrompt']; |
||
150 | } |
||
151 | |||
152 | if (isset($configurations[$function]['postPrompt'])) { |
||
153 | $postPrompt = $configurations[$function]['postPrompt']; |
||
154 | } |
||
155 | } |
||
156 | $fullInstructions = $prePrompt . ' ' . $instructions . ' .' . $postPrompt; |
||
157 | |||
158 | |||
159 | $payload = json_encode([ |
||
160 | 'messages' => [ |
||
161 | ['role' => 'user', 'content' => $fullInstructions] |
||
162 | ], |
||
163 | 'model' => $model |
||
164 | ]); |
||
165 | |||
166 | $headers = ([ |
||
167 | 'Authorization: Bearer ' . $this->apiKey, |
||
168 | 'Content-Type: application/json' |
||
169 | ]); |
||
170 | $response = getURLContent($this->apiEndpoint, 'POST', $payload, 1, $headers); |
||
171 | |||
172 | if (empty($response['http_code'])) { |
||
173 | throw new Exception('API request failed. No http received'); |
||
174 | } |
||
175 | if (!empty($response['http_code']) && $response['http_code'] != 200) { |
||
176 | throw new Exception('API request failed with status code ' . $response['http_code']); |
||
177 | } |
||
178 | // Decode JSON response |
||
179 | $decodedResponse = json_decode($response['content'], true); |
||
180 | |||
181 | // Extraction content |
||
182 | $generatedContent = $decodedResponse['choices'][0]['message']['content']; |
||
183 | |||
184 | dol_syslog("generatedContent=" . $generatedContent); |
||
185 | |||
186 | // If content is not HTML, we convert it into HTML |
||
187 | if ($format == 'html') { |
||
188 | if (!dol_textishtml($generatedContent)) { |
||
189 | dol_syslog("Result was detected as not HTML so we convert it into HTML."); |
||
190 | $generatedContent = dol_nl2br($generatedContent); |
||
191 | } else { |
||
192 | dol_syslog("Result was detected as already HTML. Do nothing."); |
||
193 | } |
||
194 | |||
195 | // TODO If content is for website module, we must |
||
196 | // - clan html header, keep body only and remove ``` ticks added by AI |
||
197 | // - add tags <section contenEditable="true"> </section> |
||
198 | } |
||
199 | |||
200 | return $generatedContent; |
||
201 | } catch (Exception $e) { |
||
202 | $errormessage = $e->getMessage(); |
||
203 | if (!empty($response['content'])) { |
||
204 | $decodedResponse = json_decode($response['content'], true); |
||
205 | |||
206 | // With OpenAI, error is into an object error into the content |
||
207 | if (!empty($decodedResponse['error']['message'])) { |
||
208 | $errormessage .= ' - ' . $decodedResponse['error']['message']; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | return array( |
||
213 | 'error' => true, |
||
214 | 'message' => $errormessage, |
||
215 | 'code' => (empty($response['http_code']) ? 0 : $response['http_code']), |
||
216 | 'curl_error_no' => (empty($response['curl_error_no']) ? $response['curl_error_no'] : ''), |
||
217 | 'format' => $format, |
||
218 | 'service' => $this->apiService, |
||
219 | 'function' => $function |
||
220 | ); |
||
224 |