core.php
325 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Engines_Core { |
| 4 | private $core = null; |
| 5 | private $openai = null; |
| 6 | private $localApiKey = null; |
| 7 | private $localService = null; |
| 8 | private $localAzureEndpoint = null; |
| 9 | private $localAzureApiKey = null; |
| 10 | private $localAzureDeployments = null; |
| 11 | private $openaiEndpoint = 'https://api.openai.com/v1'; |
| 12 | private $azureApiVersion = '?api-version=2023-03-15-preview'; |
| 13 | |
| 14 | public function __construct( $core ) { |
| 15 | $this->core = $core; |
| 16 | $this->openai = new Meow_MWAI_Engines_OpenAI( $this->core ); |
| 17 | $this->localService = $this->core->get_option( 'openai_service' ); |
| 18 | $this->localApiKey = $this->core->get_option( 'openai_apikey' ); |
| 19 | $this->localAzureEndpoint = $this->core->get_option( 'openai_azure_endpoint' ); |
| 20 | $this->localAzureApiKey = $this->core->get_option( 'openai_azure_apikey' ); |
| 21 | $this->localAzureDeployments = $this->core->get_option( 'openai_azure_deployments' ); |
| 22 | } |
| 23 | |
| 24 | private function buildHeaders( $query ) { |
| 25 | $headers = array( |
| 26 | 'Content-Type' => 'application/json', |
| 27 | 'Authorization' => 'Bearer ' . $query->apiKey, |
| 28 | ); |
| 29 | if ( $query->service === 'azure' ) { |
| 30 | $headers = array( 'Content-Type' => 'application/json', 'api-key' => $query->azureApiKey ); |
| 31 | } |
| 32 | return $headers; |
| 33 | } |
| 34 | |
| 35 | private function buildOptions( $headers, $json = null, $forms = null ) { |
| 36 | |
| 37 | // Build body |
| 38 | $body = null; |
| 39 | if ( !empty( $forms ) ) { |
| 40 | $boundary = wp_generate_password ( 24, false ); |
| 41 | $headers['Content-Type'] = 'multipart/form-data; boundary=' . $boundary; |
| 42 | $body = $this->openai->buildFormBody( $forms, $boundary ); |
| 43 | } |
| 44 | else if ( !empty( $json ) ) { |
| 45 | $body = json_encode( $json ); |
| 46 | } |
| 47 | |
| 48 | // Build options |
| 49 | $options = array( |
| 50 | 'headers' => $headers, |
| 51 | 'method' => 'POST', |
| 52 | 'timeout' => MWAI_TIMEOUT, |
| 53 | 'body' => $body, |
| 54 | 'sslverify' => false |
| 55 | ); |
| 56 | |
| 57 | return $options; |
| 58 | } |
| 59 | |
| 60 | private function runQuery( $url, $options ) { |
| 61 | try { |
| 62 | $response = wp_remote_get( $url, $options ); |
| 63 | if ( is_wp_error( $response ) ) { |
| 64 | throw new Exception( $response->get_error_message() ); |
| 65 | } |
| 66 | $response = wp_remote_retrieve_body( $response ); |
| 67 | |
| 68 | // If Headers contains multipart/form-data then we don't need to decode the response |
| 69 | if ( strpos( $options['headers']['Content-Type'], 'multipart/form-data' ) !== false ) { |
| 70 | return $response; |
| 71 | } |
| 72 | |
| 73 | $data = json_decode( $response, true ); |
| 74 | $this->openai->handleResponseErrors( $data ); |
| 75 | return $data; |
| 76 | } |
| 77 | catch ( Exception $e ) { |
| 78 | error_log( $e->getMessage() ); |
| 79 | throw $e; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private function applyQueryParameters( $query ) { |
| 84 | if ( empty( $query->service ) ) { |
| 85 | $query->service = $this->localService; |
| 86 | } |
| 87 | |
| 88 | // OpenAI will be used by default for everything |
| 89 | if ( empty( $query->apiKey ) ) { |
| 90 | $query->apiKey = $this->localApiKey; |
| 91 | } |
| 92 | |
| 93 | // But if the service is set to Azure and the deployments/models are available, |
| 94 | // then we will use Azure instead. |
| 95 | if ( $query->service === 'azure' && !empty( $this->localAzureDeployments ) ) { |
| 96 | $found = false; |
| 97 | foreach ( $this->localAzureDeployments as $deployment ) { |
| 98 | if ( $deployment['model'] === $query->model ) { |
| 99 | $query->azureDeployment = $deployment['name']; |
| 100 | if ( empty( $query->azureEndpoint ) ) { |
| 101 | $query->azureEndpoint = $this->localAzureEndpoint; |
| 102 | } |
| 103 | if ( empty( $query->azureApiKey ) ) { |
| 104 | $query->azureApiKey = $this->localAzureApiKey; |
| 105 | } |
| 106 | $found = true; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | if ( !$found ) { |
| 111 | error_log( 'Azure deployment not found for model: ' . $query->model ); |
| 112 | $query->service = 'openai'; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private function getAudio( $url ) { |
| 118 | require_once( ABSPATH . 'wp-admin/includes/media.php' ); |
| 119 | $tmpFile = tempnam( sys_get_temp_dir(), 'audio_' ); |
| 120 | file_put_contents( $tmpFile, file_get_contents( $url ) ); |
| 121 | $length = null; |
| 122 | $metadata = wp_read_audio_metadata( $tmpFile ); |
| 123 | if ( isset( $metadata['length'] ) ) { |
| 124 | $length = $metadata['length']; |
| 125 | } |
| 126 | $data = file_get_contents( $tmpFile ); |
| 127 | unlink( $tmpFile ); |
| 128 | return [ 'data' => $data, 'length' => $length ]; |
| 129 | } |
| 130 | |
| 131 | private function runTranscribeQuery( $query ) { |
| 132 | $this->applyQueryParameters( $query ); |
| 133 | |
| 134 | // Prepare the request |
| 135 | $modeEndpoint = $query->mode === 'translation' ? 'translations' : 'transcriptions'; |
| 136 | $url = 'https://api.openai.com/v1/audio/' . $modeEndpoint; |
| 137 | $audioData = $this->getAudio( $query->url ); |
| 138 | $body = array( |
| 139 | 'prompt' => $query->prompt, |
| 140 | 'model' => $query->model, |
| 141 | 'response_format' => 'text', |
| 142 | 'file' => basename( $query->url ), |
| 143 | 'data' => $audioData['data'] |
| 144 | ); |
| 145 | $headers = $this->buildHeaders( $query ); |
| 146 | $options = $this->buildOptions( $headers, null, $body ); |
| 147 | |
| 148 | // Perform the request |
| 149 | try { |
| 150 | $data = $this->runQuery( $url, $options ); |
| 151 | if ( empty( $data ) ) { |
| 152 | throw new Exception( 'Invalid data for transcription.' ); |
| 153 | } |
| 154 | $usage = $this->core->recordAudioUsage( $query->model, $audioData['length'] ); |
| 155 | $reply = new Meow_MWAI_Reply( $query ); |
| 156 | $reply->setUsage( $usage ); |
| 157 | $reply->setChoices( $data ); |
| 158 | return $reply; |
| 159 | } |
| 160 | catch ( Exception $e ) { |
| 161 | error_log( $e->getMessage() ); |
| 162 | throw new Exception( 'Error while calling OpenAI: ' . $e->getMessage() ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private function runEmbeddingQuery( $query ) { |
| 167 | $this->applyQueryParameters( $query ); |
| 168 | |
| 169 | // Prepare the request |
| 170 | $url = 'https://api.openai.com/v1/embeddings'; |
| 171 | $body = array( 'input' => $query->prompt, 'model' => $query->model ); |
| 172 | if ( $query->service === 'azure' ) { |
| 173 | $url = trailingslashit( $query->azureEndpoint ) . 'openai/deployments/' . |
| 174 | $query->azureDeployment . '/embeddings?api-version=2023-03-15-preview'; |
| 175 | $body = array( "input" => $query->prompt ); |
| 176 | } |
| 177 | $headers = $this->buildHeaders( $query ); |
| 178 | $options = $this->buildOptions( $headers, $body ); |
| 179 | |
| 180 | // Perform the request |
| 181 | try { |
| 182 | $data = $this->runQuery( $url, $options ); |
| 183 | if ( empty( $data ) || !isset( $data['data'] ) ) { |
| 184 | throw new Exception( 'Invalid data for embedding.' ); |
| 185 | } |
| 186 | $usage = $data['usage']; |
| 187 | $this->core->recordTokensUsage( $query->model, $usage['prompt_tokens'] ); |
| 188 | $reply = new Meow_MWAI_Reply( $query ); |
| 189 | $reply->setUsage( $usage ); |
| 190 | $reply->setChoices( $data['data'] ); |
| 191 | return $reply; |
| 192 | } |
| 193 | catch ( Exception $e ) { |
| 194 | error_log( $e->getMessage() ); |
| 195 | $service = $query->service === 'azure' ? 'Azure' : 'OpenAI'; |
| 196 | throw new Exception( "Error while calling {$service}: " . $e->getMessage() ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private function runCompletionQuery( $query ) { |
| 201 | $this->applyQueryParameters( $query ); |
| 202 | if ( $query->mode !== 'chat' && $query->mode !== 'completion' ) { |
| 203 | throw new Exception( 'Unknown mode for query: ' . $query->mode ); |
| 204 | } |
| 205 | |
| 206 | // Prepare the request |
| 207 | $body = array( |
| 208 | "model" => $query->model, |
| 209 | "stop" => $query->stop, |
| 210 | "n" => $query->maxResults, |
| 211 | "max_tokens" => $query->maxTokens, |
| 212 | "temperature" => $query->temperature, |
| 213 | ); |
| 214 | if ( $query->mode === 'chat' ) { |
| 215 | $body['messages'] = $query->messages; |
| 216 | } |
| 217 | else if ( $query->mode === 'completion' ) { |
| 218 | $body['prompt'] = $query->getPrompt(); |
| 219 | } |
| 220 | $url = $query->service === 'azure' ? trailingslashit( $query->azureEndpoint ) . |
| 221 | 'openai/deployments/' . $query->azureDeployment : $this->openaiEndpoint; |
| 222 | if ( $query->mode === 'chat' ) { |
| 223 | $url .= $query->service === 'azure' ? '/chat/completions' . $this->azureApiVersion : '/chat/completions'; |
| 224 | } |
| 225 | else if ($query->mode === 'completion') { |
| 226 | $url .= $query->service === 'azure' ? '/completions' . $this->azureApiVersion : '/completions'; |
| 227 | } |
| 228 | $headers = $this->buildHeaders( $query ); |
| 229 | $options = $this->buildOptions( $headers, $body ); |
| 230 | |
| 231 | try { |
| 232 | $data = $this->runQuery( $url, $options ); |
| 233 | if ( !$data['model'] ) { |
| 234 | error_log( print_r( $data, 1 ) ); |
| 235 | throw new Exception( "Got an unexpected response from OpenAI. Check your PHP Error Logs." ); |
| 236 | } |
| 237 | $reply = new Meow_MWAI_Reply( $query ); |
| 238 | try { |
| 239 | $usage = $this->core->recordTokensUsage( |
| 240 | $data['model'], |
| 241 | $data['usage']['prompt_tokens'], |
| 242 | $data['usage']['completion_tokens'] |
| 243 | ); |
| 244 | } |
| 245 | catch ( Exception $e ) { |
| 246 | error_log( $e->getMessage() ); |
| 247 | } |
| 248 | $reply->setUsage( $usage ); |
| 249 | $reply->setChoices( $data['choices'] ); |
| 250 | return $reply; |
| 251 | } |
| 252 | catch ( Exception $e ) { |
| 253 | error_log( $e->getMessage() ); |
| 254 | $service = $query->service === 'azure' ? 'Azure' : 'OpenAI'; |
| 255 | throw new Exception( "Error while calling {$service}: " . $e->getMessage() ); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // Request to DALL-E API |
| 260 | private function runImagesQuery( $query ) { |
| 261 | $this->applyQueryParameters( $query ); |
| 262 | |
| 263 | // Prepare the request |
| 264 | $url = 'https://api.openai.com/v1/images/generations'; |
| 265 | $body = array( |
| 266 | "prompt" => $query->prompt, |
| 267 | "n" => $query->maxResults, |
| 268 | "size" => '1024x1024', |
| 269 | ); |
| 270 | $headers = $this->buildHeaders( $query ); |
| 271 | $options = $this->buildOptions( $headers, $body ); |
| 272 | |
| 273 | // Perform the request |
| 274 | try { |
| 275 | $data = $this->runQuery( $url, $options ); |
| 276 | $reply = new Meow_MWAI_Reply( $query ); |
| 277 | $usage = $this->core->recordImagesUsage( "dall-e", "1024x1024", $query->maxResults ); |
| 278 | $reply->setUsage( $usage ); |
| 279 | $reply->setChoices( $data['data'] ); |
| 280 | $reply->setType( 'images' ); |
| 281 | return $reply; |
| 282 | } |
| 283 | catch ( Exception $e ) { |
| 284 | error_log( $e->getMessage() ); |
| 285 | throw new Exception( 'Error while calling OpenAI: ' . $e->getMessage() ); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | public function run( $query ) { |
| 290 | // Check if the query is allowed |
| 291 | $limits = $this->core->get_option( 'limits' ); |
| 292 | $ok = apply_filters( 'mwai_ai_allowed', true, $query, $limits ); |
| 293 | if ( $ok !== true ) { |
| 294 | $message = is_string( $ok ) ? $ok : 'Unauthorized query.'; |
| 295 | throw new Exception( $message ); |
| 296 | } |
| 297 | |
| 298 | // Allow to modify the query |
| 299 | $query = apply_filters( 'mwai_ai_query', $query ); |
| 300 | $query->finalChecks(); |
| 301 | |
| 302 | // Run the query |
| 303 | $reply = null; |
| 304 | if ( $query instanceof Meow_MWAI_QueryText ) { |
| 305 | $reply = $this->runCompletionQuery( $query ); |
| 306 | } |
| 307 | else if ( $query instanceof Meow_MWAI_QueryEmbed ) { |
| 308 | $reply = $this->runEmbeddingQuery( $query ); |
| 309 | } |
| 310 | else if ( $query instanceof Meow_MWAI_QueryImage ) { |
| 311 | $reply = $this->runImagesQuery( $query ); |
| 312 | } |
| 313 | else if ( $query instanceof Meow_MWAI_QueryTranscribe ) { |
| 314 | $reply = $this->runTranscribeQuery( $query ); |
| 315 | } |
| 316 | else { |
| 317 | throw new Exception( 'Unknown query type.' ); |
| 318 | } |
| 319 | |
| 320 | // Let's allow some modififications of the reply |
| 321 | $reply = apply_filters( 'mwai_ai_reply', $reply, $query ); |
| 322 | return $reply; |
| 323 | } |
| 324 | } |
| 325 |