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
rest.php
3 years ago
shortcodes.php
3 years ago
ui.php
3 years ago
ai.php
91 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_AI { |
| 4 | private $core = null; |
| 5 | private $model = "text-davinci-003"; |
| 6 | private $apiKey = null; |
| 7 | |
| 8 | public function __construct( $core ) { |
| 9 | $this->core = $core; |
| 10 | $this->apiKey = $this->core->get_option( 'openai_apikey' ); |
| 11 | } |
| 12 | |
| 13 | // Record usage of the API on a monthly basis |
| 14 | public function record_usage( $model, $prompt_tokens, $completion_tokens ) { |
| 15 | if ( !$model || !$prompt_tokens || !$completion_tokens ) { |
| 16 | throw new Exception( 'Missing parameters for record_usage.' ); |
| 17 | } |
| 18 | $usage = $this->core->get_option( 'openai_usage' ); |
| 19 | $month = date( 'Y-m' ); |
| 20 | if ( !isset( $usage[$month] ) ) { |
| 21 | $usage[$month] = array(); |
| 22 | } |
| 23 | if ( !isset( $usage[$month][$model] ) ) { |
| 24 | $usage[$month][$model] = array( |
| 25 | 'prompt_tokens' => 0, |
| 26 | 'completion_tokens' => 0, |
| 27 | 'total_tokens' => 0 |
| 28 | ); |
| 29 | } |
| 30 | $usage[$month][$model]['prompt_tokens'] += $prompt_tokens; |
| 31 | $usage[$month][$model]['completion_tokens'] += $completion_tokens; |
| 32 | $usage[$month][$model]['total_tokens'] += $prompt_tokens + $completion_tokens; |
| 33 | $this->core->update_option( 'openai_usage', $usage ); |
| 34 | return [ |
| 35 | 'prompt_tokens' => $prompt_tokens, |
| 36 | 'completion_tokens' => $completion_tokens, |
| 37 | 'total_tokens' => $prompt_tokens + $completion_tokens |
| 38 | ]; |
| 39 | } |
| 40 | |
| 41 | public function run( $query ) { |
| 42 | $apiKey = $this->apiKey; |
| 43 | if ( !empty( $query->apiKey ) ) { |
| 44 | $apiKey = $query->apiKey; |
| 45 | } |
| 46 | $url = 'https://api.openai.com/v1/completions'; |
| 47 | $options = array( |
| 48 | "headers" => "Content-Type: application/json\r\n" . "Authorization: Bearer " . $apiKey . "\r\n", |
| 49 | "method" => "POST", |
| 50 | "timeout" => 60, |
| 51 | "body" => json_encode( array( |
| 52 | "model" => $query->model, |
| 53 | "prompt" => $query->prompt, |
| 54 | "n" => $query->maxResults, |
| 55 | "max_tokens" => $query->maxTokens, |
| 56 | "temperature" => $query->temperature, |
| 57 | ) ), |
| 58 | "sslverify" => false |
| 59 | ); |
| 60 | |
| 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 | $data = json_decode( $response, true ); |
| 68 | |
| 69 | // Error handling |
| 70 | if ( isset( $data['error'] ) ) { |
| 71 | $message = $data['error']['message']; |
| 72 | // If the message contains "Incorrect API key provided: THE_KEY.", replace the key by "----". |
| 73 | if ( preg_match( '/API key provided(: .*)\./', $message, $matches ) ) { |
| 74 | $message = str_replace( $matches[1], '', $message ); |
| 75 | } |
| 76 | throw new Exception( $message ); |
| 77 | } |
| 78 | $answer = new Meow_MWAI_Answer( $query ); |
| 79 | $usage = $this->record_usage( $data['model'], $data['usage']['prompt_tokens'], |
| 80 | $data['usage']['completion_tokens'] ); |
| 81 | $answer->setUsage( $usage ); |
| 82 | $answer->setChoices( $data['choices'] ); |
| 83 | return $answer; |
| 84 | } |
| 85 | catch ( Exception $e ) { |
| 86 | error_log( $e->getMessage() ); |
| 87 | throw new Exception( 'Error while calling OpenAI: ' . $e->getMessage() ); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 |