modules
3 years ago
admin.php
3 years ago
ai.php
3 years ago
answer.php
3 years ago
core.php
3 years ago
init.php
3 years ago
openai.php
3 years ago
query.php
3 years ago
queryimage.php
3 years ago
querytext.php
3 years ago
rest.php
3 years ago
ui.php
3 years ago
ai.php
189 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_AI { |
| 4 | private $core = null; |
| 5 | private $apiKey = null; |
| 6 | |
| 7 | public function __construct( $core ) { |
| 8 | $this->core = $core; |
| 9 | $this->apiKey = $this->core->get_option( 'openai_apikey' ); |
| 10 | } |
| 11 | |
| 12 | // Record usage of the API on a monthly basis |
| 13 | public function record_usage( $model, $prompt_tokens, $completion_tokens = 0 ) { |
| 14 | if ( !is_numeric( $prompt_tokens ) ) { |
| 15 | throw new Exception( 'Record usage: prompt_tokens is not a number.' ); |
| 16 | } |
| 17 | if ( !is_numeric( $completion_tokens ) ) { |
| 18 | $completion_tokens = 0; |
| 19 | } |
| 20 | if ( !$model ) { |
| 21 | throw new Exception( 'Record usage: model is missing.' ); |
| 22 | } |
| 23 | $usage = $this->core->get_option( 'openai_usage' ); |
| 24 | $month = date( 'Y-m' ); |
| 25 | if ( !isset( $usage[$month] ) ) { |
| 26 | $usage[$month] = array(); |
| 27 | } |
| 28 | if ( !isset( $usage[$month][$model] ) ) { |
| 29 | $usage[$month][$model] = array( |
| 30 | 'prompt_tokens' => 0, |
| 31 | 'completion_tokens' => 0, |
| 32 | 'total_tokens' => 0 |
| 33 | ); |
| 34 | } |
| 35 | $usage[$month][$model]['prompt_tokens'] += $prompt_tokens; |
| 36 | $usage[$month][$model]['completion_tokens'] += $completion_tokens; |
| 37 | $usage[$month][$model]['total_tokens'] += $prompt_tokens + $completion_tokens; |
| 38 | $this->core->update_option( 'openai_usage', $usage ); |
| 39 | return [ |
| 40 | 'prompt_tokens' => $prompt_tokens, |
| 41 | 'completion_tokens' => $completion_tokens, |
| 42 | 'total_tokens' => $prompt_tokens + $completion_tokens |
| 43 | ]; |
| 44 | } |
| 45 | |
| 46 | public function record_image_usage( $model, $resolution, $images ) { |
| 47 | if ( !$model || !$resolution || !$images ) { |
| 48 | throw new Exception( 'Missing parameters for record_dalle_usage.' ); |
| 49 | } |
| 50 | $usage = $this->core->get_option( 'openai_usage' ); |
| 51 | $month = date( 'Y-m' ); |
| 52 | if ( !isset( $usage[$month] ) ) { |
| 53 | $usage[$month] = array(); |
| 54 | } |
| 55 | if ( !isset( $usage[$month][$model] ) ) { |
| 56 | $usage[$month][$model] = array( |
| 57 | 'resolution' => array(), |
| 58 | 'images' => 0 |
| 59 | ); |
| 60 | } |
| 61 | if ( !isset( $usage[$month][$model]['resolution'][$resolution] ) ) { |
| 62 | $usage[$month][$model]['resolution'][$resolution] = 0; |
| 63 | } |
| 64 | $usage[$month][$model]['resolution'][$resolution] += $images; |
| 65 | $usage[$month][$model]['images'] += $images; |
| 66 | $this->core->update_option( 'openai_usage', $usage ); |
| 67 | return [ |
| 68 | 'resolution' => $resolution, |
| 69 | 'images' => $images |
| 70 | ]; |
| 71 | } |
| 72 | |
| 73 | public function runTextQuery( $query ) { |
| 74 | $apiKey = $this->apiKey; |
| 75 | if ( !empty( $query->apiKey ) ) { |
| 76 | $apiKey = $query->apiKey; |
| 77 | } |
| 78 | $url = 'https://api.openai.com/v1/completions'; |
| 79 | $options = array( |
| 80 | "headers" => "Content-Type: application/json\r\n" . "Authorization: Bearer " . $apiKey . "\r\n", |
| 81 | "method" => "POST", |
| 82 | "timeout" => 60, |
| 83 | "body" => json_encode( array( |
| 84 | "model" => $query->model, |
| 85 | "prompt" => $query->prompt, |
| 86 | "stop" => $query->stop, |
| 87 | "n" => $query->maxResults, |
| 88 | "max_tokens" => $query->maxTokens, |
| 89 | "temperature" => $query->temperature, |
| 90 | ) ), |
| 91 | "sslverify" => false |
| 92 | ); |
| 93 | |
| 94 | try { |
| 95 | $response = wp_remote_get( $url, $options ); |
| 96 | if ( is_wp_error( $response ) ) { |
| 97 | throw new Exception( $response->get_error_message() ); |
| 98 | } |
| 99 | $response = wp_remote_retrieve_body( $response ); |
| 100 | $data = json_decode( $response, true ); |
| 101 | |
| 102 | // Error handling |
| 103 | if ( isset( $data['error'] ) ) { |
| 104 | $message = $data['error']['message']; |
| 105 | // If the message contains "Incorrect API key provided: THE_KEY.", replace the key by "----". |
| 106 | if ( preg_match( '/API key provided(: .*)\./', $message, $matches ) ) { |
| 107 | $message = str_replace( $matches[1], '', $message ); |
| 108 | } |
| 109 | throw new Exception( $message ); |
| 110 | } |
| 111 | if ( !$data['model'] ) { |
| 112 | error_log( print_r( $data, 1 ) ); |
| 113 | throw new Exception( "Got an unexpected response from OpenAI. Check your PHP Error Logs." ); |
| 114 | } |
| 115 | $answer = new Meow_MWAI_Answer( $query ); |
| 116 | $usage = $this->record_usage( $data['model'], $data['usage']['prompt_tokens'], |
| 117 | $data['usage']['completion_tokens'] ); |
| 118 | $answer->setUsage( $usage ); |
| 119 | $answer->setChoices( $data['choices'] ); |
| 120 | return $answer; |
| 121 | } |
| 122 | catch ( Exception $e ) { |
| 123 | error_log( $e->getMessage() ); |
| 124 | throw new Exception( 'Error while calling OpenAI: ' . $e->getMessage() ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Request to DALL-E API |
| 129 | public function runImageQuery( $query ) { |
| 130 | $apiKey = $this->apiKey; |
| 131 | if ( !empty( $query->apiKey ) ) { |
| 132 | $apiKey = $query->apiKey; |
| 133 | } |
| 134 | $url = 'https://api.openai.com/v1/images/generations'; |
| 135 | $options = array( |
| 136 | "headers" => "Content-Type: application/json\r\n" . "Authorization: Bearer " . $apiKey . "\r\n", |
| 137 | "method" => "POST", |
| 138 | "timeout" => 60, |
| 139 | "body" => json_encode( array( |
| 140 | "prompt" => $query->prompt, |
| 141 | "n" => $query->maxResults, |
| 142 | "size" => '1024x1024', |
| 143 | ) ), |
| 144 | "sslverify" => false |
| 145 | ); |
| 146 | |
| 147 | try { |
| 148 | $response = wp_remote_get( $url, $options ); |
| 149 | if ( is_wp_error( $response ) ) { |
| 150 | throw new Exception( $response->get_error_message() ); |
| 151 | } |
| 152 | $response = wp_remote_retrieve_body( $response ); |
| 153 | $data = json_decode( $response, true ); |
| 154 | |
| 155 | // Error handling |
| 156 | if ( isset( $data['error'] ) ) { |
| 157 | $message = $data['error']['message']; |
| 158 | // If the message contains "Incorrect API key provided: THE_KEY.", replace the key by "----". |
| 159 | if ( preg_match( '/API key provided(: .*)\./', $message, $matches ) ) { |
| 160 | $message = str_replace( $matches[1], '', $message ); |
| 161 | } |
| 162 | throw new Exception( $message ); |
| 163 | } |
| 164 | |
| 165 | $answer = new Meow_MWAI_Answer( $query ); |
| 166 | $usage = $this->record_image_usage( "dall-e", "1024x1024", $query->maxResults ); |
| 167 | $answer->setUsage( $usage ); |
| 168 | $answer->setChoices( $data['data'] ); |
| 169 | return $answer; |
| 170 | } |
| 171 | catch ( Exception $e ) { |
| 172 | error_log( $e->getMessage() ); |
| 173 | throw new Exception( 'Error while calling OpenAI: ' . $e->getMessage() ); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | public function run( $query ) { |
| 178 | if ( $query instanceof Meow_MWAI_QueryText ) { |
| 179 | return $this->runTextQuery( $query ); |
| 180 | } |
| 181 | else if ( $query instanceof Meow_MWAI_QueryImage ) { |
| 182 | return $this->runImageQuery( $query ); |
| 183 | } |
| 184 | else { |
| 185 | throw new Exception( 'Invalid query.' ); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 |