Skip to content

Commit 2747fa7

Browse files
committed
feat(gemini): add vision support for gemini
1 parent 8539f35 commit 2747fa7

File tree

2 files changed

+107
-49
lines changed

2 files changed

+107
-49
lines changed

core/components/modai/src/Services/ChatGPT.php

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

core/components/modai/src/Services/Gemini.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,64 @@ public function getCompletions(array $data, CompletionsConfig $config): string
9797

9898
public function getVision(string $prompt, string $image, VisionConfig $config): string
9999
{
100-
throw new \Exception("not implemented");
100+
$apiKey = $this->modx->getOption('modai.api.gemini.key');
101+
if (empty($apiKey)) {
102+
throw new \Exception('Missing modai.api.gemini.key');
103+
}
104+
105+
$image = str_replace('data:image/png;base64,', '', $image);
106+
107+
$input = [
108+
'contents' => [
109+
'parts' => [
110+
[
111+
"text" => $prompt,
112+
],
113+
[
114+
"inline_data" => [
115+
"mime_type" => "image/png",
116+
"data" => $image,
117+
]
118+
],
119+
]
120+
],
121+
];
122+
123+
$url = self::COMPLETIONS_API;
124+
$url = str_replace("{model}", $config->getModel(), $url);
125+
$url = str_replace("{apiKey}", $apiKey, $url);
126+
127+
$ch = curl_init($url);
128+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
129+
curl_setopt($ch, CURLOPT_POST, true);
130+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($input));
131+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
132+
'Content-Type: application/json',
133+
]);
134+
135+
$response = curl_exec($ch);
136+
if (curl_errno($ch)) {
137+
$error_msg = curl_error($ch);
138+
curl_close($ch);
139+
throw new \Exception($error_msg);
140+
}
141+
142+
curl_close($ch);
143+
144+
$result = json_decode($response, true);
145+
if (!is_array($result)) {
146+
throw new \Exception('Invalid response');
147+
}
148+
149+
if (isset($result['error'])) {
150+
throw new \Exception($result['error']['message']);
151+
}
152+
153+
if (!isset($result['candidates'][0]['content']['parts'][0]['text'])) {
154+
throw new \Exception("There was an error generating a response.");
155+
}
156+
157+
return $result['candidates'][0]['content']['parts'][0]['text'];
101158
}
102159

103160
public function generateImage(string $prompt, ImageConfig $config): array

0 commit comments

Comments
 (0)