Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/aimogen-pro/res/ollama/ollama.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
class AiomaticOllamaAPI
3
+
{
4
+
private $baseUrl;
5
+
public function __construct($baseUrl)
6
+
{
7
+
$this->baseUrl = rtrim($baseUrl, '/');
8
+
}
9
+
private function makeRequest($endpoint, $data, $stream)
10
+
{
11
+
$url = $this->baseUrl . $endpoint;
12
+
13
+
$payload = json_encode($data);
14
+
if ($payload === false) {
15
+
aiomatic_log_to_file('Failed to encode payload in request: ' . print_r($data, true));
16
+
return false;
17
+
}
18
+
19
+
$args = array(
20
+
'body' => $payload,
21
+
'headers' => array(
22
+
'Content-Type' => 'application/json',
23
+
),
24
+
'method' => 'POST',
25
+
'data_format' => 'body',
26
+
'timeout' => AIMOGEN_DEFAULT_BIG_TIMEOUT,
27
+
);
28
+
29
+
if ($stream === true) {
30
+
add_action('http_api_curl', array($this, 'filterCurlForStream'));
31
+
}
32
+
33
+
$response = wp_remote_post($url, $args);
34
+
35
+
if ($stream === true) {
36
+
remove_action('http_api_curl', array($this, 'filterCurlForStream'));
37
+
}
38
+
39
+
if (is_wp_error($response)) {
40
+
aiomatic_log_to_file('Error making request to ' . $url . ': ' . $response->get_error_message());
41
+
return false;
42
+
}
43
+
44
+
$body = wp_remote_retrieve_body($response);
45
+
46
+
if ($stream === true) {
47
+
return '';
48
+
}
49
+
$res = json_decode($body, true);
50
+
if ($res === null) {
51
+
aiomatic_log_to_file('Failed to decode response: ' . $body);
52
+
return false;
53
+
}
54
+
55
+
return $res;
56
+
}
57
+
public function filterCurlForStream($handle)
58
+
{
59
+
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
60
+
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
61
+
curl_setopt($handle, CURLOPT_WRITEFUNCTION, function ($curl_info, $data)
62
+
{
63
+
$my_copy_data = trim($data);
64
+
$prefix = 'data: ';
65
+
if (substr($my_copy_data, 0, strlen($prefix)) == $prefix) {
66
+
$my_copy_data = substr($my_copy_data, strlen($prefix));
67
+
}
68
+
$suffix = 'data: [DONE]';
69
+
$needle_length = strlen($suffix);
70
+
if (substr($my_copy_data, -$needle_length) === $suffix) {
71
+
$my_copy_data = substr($my_copy_data, 0, -$needle_length);
72
+
}
73
+
$my_copy_data = trim($my_copy_data);
74
+
$response = json_decode($my_copy_data, true);
75
+
if (isset($response['error']) && !empty($response['error']))
76
+
{
77
+
$message = isset($response['error']['message']) && !empty($response['error']['message']) ? $response['error']['message'] : '';
78
+
if (empty($message) && isset($response['error']['code']) && $response['error']['code'] == 'invalid_api_key') {
79
+
$message = "Incorrect API key provided. You can find your API key at https://platform.openai.com/account/api-keys.";
80
+
}
81
+
echo "event: message\n";
82
+
echo 'data: {"error":[{"message":"' . $message . '"}]}';
83
+
echo "\n\n";
84
+
$l1 = ob_get_length();
85
+
if($l1 === false)
86
+
{
87
+
$l1 = 0;
88
+
}
89
+
if (ob_get_length())
90
+
{
91
+
ob_end_flush();
92
+
}
93
+
flush();
94
+
echo 'data: {"choices":[{"finish_reason":"stop"}]}';
95
+
echo "\n\n";
96
+
$l2 = ob_get_length();
97
+
if($l2 === false)
98
+
{
99
+
$l2 = 0;
100
+
}
101
+
if (ob_get_length())
102
+
{
103
+
ob_end_flush();
104
+
}
105
+
flush();
106
+
return $l1 + $l2;
107
+
}
108
+
else
109
+
{
110
+
echo "data: {$data}\n\n";
111
+
if (ob_get_length())
112
+
{
113
+
ob_flush();
114
+
}
115
+
flush();
116
+
return strlen($data);
117
+
}
118
+
});
119
+
}
120
+
public function chatCompletion($model, $messages, $stream = true, $options = [], $functions = false, $toolOutputs = false, $vision_file = false)
121
+
{
122
+
if(!empty($vision_file) && isset($messages[0]))
123
+
{
124
+
if(is_array($messages[0]['content']) && isset($messages[0]['content'][0]['text']))
125
+
{
126
+
$messages[0]['content'] = $messages[0]['content'][0]['text'];
127
+
}
128
+
$base64_vision = '';
129
+
if(stristr($vision_file, 'http://localhost/') || stristr($vision_file, 'https://localhost/'))
130
+
{
131
+
$base64_vision = aiomatic_get_base64_from_url($vision_file);
132
+
}
133
+
if(!empty($base64_vision))
134
+
{
135
+
$vision_file = $base64_vision;
136
+
}
137
+
$messages[0]['images'] = array($vision_file);
138
+
}
139
+
if(!empty($toolOutputs) && is_array($toolOutputs))
140
+
{
141
+
foreach($toolOutputs as $tout)
142
+
{
143
+
if(isset($tout['message']))
144
+
{
145
+
$messages[] = $tout['message'];
146
+
}
147
+
if(isset($tout['output']))
148
+
{
149
+
$tool_rez = ['role' => 'tool', 'content' => $tout['output']];
150
+
$messages[] = $tool_rez;
151
+
}
152
+
}
153
+
}
154
+
$data = [
155
+
'model' => $model,
156
+
'messages' => $messages,
157
+
'stream' => $stream
158
+
];
159
+
if(!empty($functions))
160
+
{
161
+
if($functions !== false && !empty($functions) && isset($functions['functions']) && !empty($functions['functions']))
162
+
{
163
+
$data['tools'] = $functions['functions'];
164
+
}
165
+
}
166
+
if(!empty($options))
167
+
{
168
+
$data['options'] = $options;
169
+
}
170
+
$resp = $this->makeRequest('/api/chat', $data, $stream);
171
+
if($resp === false)
172
+
{
173
+
return false;
174
+
}
175
+
if($stream === true)
176
+
{
177
+
return '';
178
+
}
179
+
if(!isset($resp['message']['content']))
180
+
{
181
+
aiomatic_log_to_file('Failed to interpret Ollama chat API response: ' . print_r($resp, true));
182
+
return false;
183
+
}
184
+
return $resp;
185
+
}
186
+
public function embeddings($model, $prompt)
187
+
{
188
+
$data = [
189
+
'model' => $model,
190
+
'prompt' => $prompt
191
+
];
192
+
$resp = $this->makeRequest('/api/embeddings', $data, false);
193
+
if($resp === false)
194
+
{
195
+
return false;
196
+
}
197
+
if(!isset($resp['embedding']) || empty($resp['embedding']))
198
+
{
199
+
aiomatic_log_to_file('Failed to interpret Ollama embedding API response: ' . print_r($resp, true));
200
+
return false;
201
+
}
202
+
return $resp['embedding'];
203
+
}
204
+
public function generate($model, $prompt, $stream = false, $options = [])
205
+
{
206
+
$data = [
207
+
'model' => $model,
208
+
'prompt' => $prompt,
209
+
'stream' => $stream
210
+
];
211
+
if(!empty($options))
212
+
{
213
+
$data['options'] = $options;
214
+
}
215
+
$resp = $this->makeRequest('/api/generate', $data, $stream);
216
+
if($resp === false)
217
+
{
218
+
return false;
219
+
}
220
+
if($stream === true)
221
+
{
222
+
return '';
223
+
}
224
+
if(!isset($resp['response']))
225
+
{
226
+
aiomatic_log_to_file('Failed to interpret Ollama completion API response: ' . print_r($resp, true));
227
+
return false;
228
+
}
229
+
return $resp['response'];
230
+
}
231
+
}
232
+
?>