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