Conditions | 25 |
Paths | > 20000 |
Total Lines | 103 |
Code Lines | 59 |
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 |
||
71 | public function generateContent($instructions, $model = 'auto', $function = 'textgeneration', $format = '') |
||
72 | { |
||
73 | if (empty($this->apiKey)) { |
||
74 | return ['error' => true, 'message' => 'API key is no defined']; |
||
75 | } |
||
76 | |||
77 | if (empty($this->apiEndpoint)) { |
||
78 | if ($function == 'textgeneration') { |
||
79 | $this->apiEndpoint = 'https://api.openai.com/v1/chat/completions'; |
||
80 | if ($model == 'auto') { |
||
81 | $model = getDolGlobalString('AI_API_CHATGPT_MODEL_TEXT', 'gpt-3.5-turbo'); |
||
82 | } |
||
83 | } |
||
84 | if ($function == 'imagegeneration') { |
||
85 | $this->apiEndpoint = 'https://api.openai.com/v1/images/generations'; |
||
86 | if ($model == 'auto') { |
||
87 | $model = getDolGlobalString('AI_API_CHATGPT_MODEL_IMAGE', 'dall-e-3'); |
||
88 | } |
||
89 | } |
||
90 | if ($function == 'audiotext') { |
||
91 | $this->apiEndpoint = 'https://api.openai.com/v1/audio/speech'; |
||
92 | if ($model == 'auto') { |
||
93 | $model = getDolGlobalString('AI_API_CHATGPT_MODEL_AUDIO', 'tts-1'); |
||
94 | } |
||
95 | } |
||
96 | if ($function == 'transcription') { |
||
97 | $this->apiEndpoint = 'https://api.openai.com/v1/audio/transcriptions'; |
||
98 | if ($model == 'auto') { |
||
99 | $model = getDolGlobalString('AI_API_CHATGPT_MODEL_TRANSCRIPT', 'whisper-1'); |
||
100 | } |
||
101 | } |
||
102 | if ($function == 'translation') { |
||
103 | $this->apiEndpoint = 'https://api.openai.com/v1/audio/translations'; |
||
104 | if ($model == 'auto') { |
||
105 | $model = getDolGlobalString('AI_API_CHATGPT_MODEL_TRANSLATE', 'whisper-1'); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | dol_syslog("Call API for apiEndpoint=" . $this->apiEndpoint . " apiKey=" . substr($this->apiKey, 0, 3) . '***********, model=' . $model); |
||
111 | |||
112 | try { |
||
113 | $configurationsJson = getDolGlobalString('AI_CONFIGURATIONS_PROMPT'); |
||
114 | $configurations = json_decode($configurationsJson, true); |
||
115 | |||
116 | $prePrompt = ''; |
||
117 | $postPrompt = ''; |
||
118 | |||
119 | if (isset($configurations[$function])) { |
||
120 | if (isset($configurations[$function]['prePrompt'])) { |
||
121 | $prePrompt = $configurations[$function]['prePrompt']; |
||
122 | } |
||
123 | |||
124 | if (isset($configurations[$function]['postPrompt'])) { |
||
125 | $postPrompt = $configurations[$function]['postPrompt']; |
||
126 | } |
||
127 | } |
||
128 | $fullInstructions = $prePrompt . ' ' . $instructions . ' .' . $postPrompt; |
||
129 | |||
130 | |||
131 | $payload = json_encode([ |
||
132 | 'messages' => [ |
||
133 | ['role' => 'user', 'content' => $fullInstructions], |
||
134 | ], |
||
135 | 'model' => $model, |
||
136 | ]); |
||
137 | |||
138 | $headers = ([ |
||
139 | 'Authorization: Bearer ' . $this->apiKey, |
||
140 | 'Content-Type: application/json', |
||
141 | ]); |
||
142 | $response = getURLContent($this->apiEndpoint, 'POST', $payload, 1, $headers); |
||
143 | |||
144 | if (empty($response['http_code'])) { |
||
145 | throw new Exception('API request failed. No http received'); |
||
|
|||
146 | } |
||
147 | if (!empty($response['http_code']) && $response['http_code'] != 200) { |
||
148 | throw new Exception('API request failed with status code ' . $response['http_code']); |
||
149 | } |
||
150 | // Decode JSON response |
||
151 | $decodedResponse = json_decode($response['content'], true); |
||
152 | |||
153 | // Extraction content |
||
154 | $generatedEmailContent = $decodedResponse['choices'][0]['message']['content']; |
||
155 | |||
156 | // If content is not HTML, we convert it into HTML |
||
157 | if (!dol_textishtml($generatedEmailContent)) { |
||
158 | $generatedEmailContent = dol_nl2br($generatedEmailContent); |
||
159 | } |
||
160 | |||
161 | return $generatedEmailContent; |
||
162 | } catch (Exception $e) { |
||
163 | $errormessage = $e->getMessage(); |
||
164 | if (!empty($response['content'])) { |
||
165 | $decodedResponse = json_decode($response['content'], true); |
||
166 | |||
167 | // With OpenAI, error is into an object error into the content |
||
168 | if (!empty($decodedResponse['error']['message'])) { |
||
169 | $errormessage .= ' - ' . $decodedResponse['error']['message']; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | return ['error' => true, 'message' => $errormessage, 'code' => (empty($response['http_code']) ? 0 : $response['http_code']), 'curl_error_no' => (empty($response['curl_error_no']) ? $response['curl_error_no'] : '')]; |
||
174 | } |
||
177 |