@@ -18,54 +18,6 @@ public function __construct(modX &$modx)
1818 $ this ->modx =& $ modx ;
1919 }
2020
21- public function generateImage (string $ prompt , ImageConfig $ config ): array {
22- $ apiKey = $ this ->modx ->getOption ('modai.api.chatgpt.key ' );
23- if (empty ($ apiKey )) {
24- throw new \Exception ('Missing modai.api.chatgpt.key ' );
25- }
26-
27- $ input = [
28- 'prompt ' => $ prompt ,
29- 'model ' => $ config ->getModel (),
30- 'n ' => $ config ->getN (),
31- 'size ' => $ config ->getSize (),
32- 'quality ' => $ config ->getQuality ()
33- ];
34-
35- $ ch = curl_init (self ::IMAGES_API );
36- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
37- curl_setopt ($ ch , CURLOPT_POST , true );
38- curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ input ));
39- curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
40- 'Content-Type: application/json ' ,
41- 'Authorization: Bearer ' . $ apiKey
42- ]);
43-
44- $ response = curl_exec ($ ch );
45- if (curl_errno ($ ch )) {
46- $ error_msg = curl_error ($ ch );
47- curl_close ($ ch );
48- throw new \Exception ($ error_msg );
49- }
50-
51- curl_close ($ ch );
52-
53- $ result = json_decode ($ response , true );
54- if (!is_array ($ result )) {
55- throw new \Exception ('Invalid response ' );
56- }
57-
58- if (isset ($ result ['error ' ])) {
59- throw new \Exception ($ result ['error ' ]['message ' ]);
60- }
61-
62- if (!isset ($ result ['data ' ][0 ]['url ' ])) {
63- throw new \Exception ("There was an error generating a response. " );
64- }
65-
66- return [ 'url ' => $ result ['data ' ][0 ]['url ' ] ];
67- }
68-
6921 /**
7022 * @throws \Exception
7123 */
@@ -196,4 +148,53 @@ public function getVision(string $prompt, string $image, VisionConfig $config):
196148 return $ result ['choices ' ][0 ]['message ' ]['content ' ];
197149 }
198150
151+
152+ public function generateImage (string $ prompt , ImageConfig $ config ): array {
153+ $ apiKey = $ this ->modx ->getOption ('modai.api.chatgpt.key ' );
154+ if (empty ($ apiKey )) {
155+ throw new \Exception ('Missing modai.api.chatgpt.key ' );
156+ }
157+
158+ $ input = [
159+ 'prompt ' => $ prompt ,
160+ 'model ' => $ config ->getModel (),
161+ 'n ' => $ config ->getN (),
162+ 'size ' => $ config ->getSize (),
163+ 'quality ' => $ config ->getQuality ()
164+ ];
165+
166+ $ ch = curl_init (self ::IMAGES_API );
167+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
168+ curl_setopt ($ ch , CURLOPT_POST , true );
169+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ input ));
170+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
171+ 'Content-Type: application/json ' ,
172+ 'Authorization: Bearer ' . $ apiKey
173+ ]);
174+
175+ $ response = curl_exec ($ ch );
176+ if (curl_errno ($ ch )) {
177+ $ error_msg = curl_error ($ ch );
178+ curl_close ($ ch );
179+ throw new \Exception ($ error_msg );
180+ }
181+
182+ curl_close ($ ch );
183+
184+ $ result = json_decode ($ response , true );
185+ if (!is_array ($ result )) {
186+ throw new \Exception ('Invalid response ' );
187+ }
188+
189+ if (isset ($ result ['error ' ])) {
190+ throw new \Exception ($ result ['error ' ]['message ' ]);
191+ }
192+
193+ if (!isset ($ result ['data ' ][0 ]['url ' ])) {
194+ throw new \Exception ("There was an error generating a response. " );
195+ }
196+
197+ return [ 'url ' => $ result ['data ' ][0 ]['url ' ] ];
198+ }
199+
199200}
0 commit comments