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
75 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_AI { |
| 4 | private $core = null; |
| 5 | private $model = "text-davinci-003"; |
| 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 run( $query ) { |
| 41 | $url = 'https://api.openai.com/v1/completions'; |
| 42 | $options = array( |
| 43 | "http" => array( |
| 44 | "header" => "Content-Type: application/json\r\n" . "Authorization: Bearer " . $this->apiKey . "\r\n", |
| 45 | "method" => "POST", |
| 46 | "content" => json_encode( array( |
| 47 | "model" => $query->model, |
| 48 | "prompt" => $query->prompt, |
| 49 | "n" => $query->maxResults, |
| 50 | "max_tokens" => $query->maxTokens, |
| 51 | "temperature" => $query->temperature, |
| 52 | ) ), |
| 53 | ), |
| 54 | "ssl" => array( |
| 55 | "verify_peer" => false, |
| 56 | "verify_peer_name" => false, |
| 57 | ) |
| 58 | ); |
| 59 | |
| 60 | //error_log( print_r( $options, 1 ) ); |
| 61 | |
| 62 | $context = stream_context_create( $options ); |
| 63 | $response = file_get_contents( $url, false, $context ); |
| 64 | if ( !$response ) { |
| 65 | throw new Exception( 'No answer from OpenAI.' ); |
| 66 | } |
| 67 | $data = json_decode( $response, true ); |
| 68 | $answer = new Meow_MWAI_Answer( $query ); |
| 69 | $usage = $this->record_usage( $data['model'], $data['usage']['prompt_tokens'], $data['usage']['completion_tokens'] ); |
| 70 | $answer->setUsage( $usage ); |
| 71 | $answer->setChoices( $data['choices'] ); |
| 72 | return $answer; |
| 73 | } |
| 74 | } |
| 75 |