admin.php
3 years ago
core.php
3 years ago
init.php
3 years ago
openai.php
3 years ago
rest.php
3 years ago
ui.php
3 years ago
openai.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_OpenAI { |
| 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 getCompletions( $prompt, $maxResults = 1 ) { |
| 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" => $this->model, |
| 48 | "prompt" => $prompt, |
| 49 | "n" => $maxResults, |
| 50 | "max_tokens" => 280, |
| 51 | "temperature" => 0.5 |
| 52 | ) ), |
| 53 | ), |
| 54 | "ssl" => array( |
| 55 | "verify_peer" => false, |
| 56 | "verify_peer_name" => false, |
| 57 | ) |
| 58 | ); |
| 59 | $context = stream_context_create( $options ); |
| 60 | $response = file_get_contents( $url, false, $context ); |
| 61 | if ( $response !== false ) { |
| 62 | $data = json_decode( $response, true ); |
| 63 | $usage = $this->record_usage( $data['model'], $data['usage']['prompt_tokens'], $data['usage']['completion_tokens'] ); |
| 64 | //error_log( print_r( $usage, 1 ) ); |
| 65 | $answer = ""; |
| 66 | $answers = array(); |
| 67 | foreach ( $data['choices'] as $choice ) { |
| 68 | $text = trim( $choice['text'] ); |
| 69 | $answers[] = $text; |
| 70 | $answer = $text; |
| 71 | } |
| 72 | return [ |
| 73 | "usage" => $usage, |
| 74 | "data" => $answer, |
| 75 | ]; |
| 76 | } |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | public function getRecommendedTitles( $text, $maxResults = 3 ) { |
| 81 | $url = 'https://api.openai.com/v1/completions'; |
| 82 | $options = array( |
| 83 | "http" => array( |
| 84 | "header" => "Content-Type: application/json\r\n" . "Authorization: Bearer " . $this->apiKey . "\r\n", |
| 85 | "method" => "POST", |
| 86 | "content" => json_encode( array( |
| 87 | "model" => $this->model, |
| 88 | "prompt" => 'Generate short SEO-friendly title. Text: ' . $text, |
| 89 | "n" => $maxResults, |
| 90 | "max_tokens" => 40, |
| 91 | "temperature" => 0.5 |
| 92 | ) ), |
| 93 | ), |
| 94 | "ssl" => array( |
| 95 | "verify_peer" => false, |
| 96 | "verify_peer_name" => false, |
| 97 | ) |
| 98 | ); |
| 99 | $context = stream_context_create( $options ); |
| 100 | $response = file_get_contents( $url, false, $context ); |
| 101 | if ( $response !== false ) { |
| 102 | $data = json_decode( $response, true ); |
| 103 | $usage = $this->record_usage( $data['model'], $data['usage']['prompt_tokens'], $data['usage']['completion_tokens'] ); |
| 104 | //error_log( print_r( $usage, 1 ) ); |
| 105 | $titles = array(); |
| 106 | foreach ( $data['choices'] as $choice ) { |
| 107 | $choice['text'] = str_replace( '"', '', $choice['text'] ); |
| 108 | $choice['text'] = preg_replace( '/\s+/', ' ', $choice['text'] ); |
| 109 | $choice['text'] = trim( $choice['text'] ); |
| 110 | $titles[] = $choice['text']; |
| 111 | } |
| 112 | return $titles; |
| 113 | } |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | public function getRecommendedExcerpts( $text, $maxResults = 3 ) { |
| 118 | $url = 'https://api.openai.com/v1/completions'; |
| 119 | $options = array( |
| 120 | "http" => array( |
| 121 | "header" => "Content-Type: application/json\r\n" . "Authorization: Bearer " . $this->apiKey . "\r\n", |
| 122 | "method" => "POST", |
| 123 | "content" => json_encode( array( |
| 124 | "model" => $this->model, |
| 125 | "prompt" => 'Generate short SEO-friendly excerpts (between 120 and 170 chars). Text: ' . $text, |
| 126 | "n" => $maxResults, |
| 127 | "max_tokens" => 140, |
| 128 | "temperature" => 0.5 |
| 129 | ) ), |
| 130 | ), |
| 131 | "ssl" => array( |
| 132 | "verify_peer" => false, |
| 133 | "verify_peer_name" => false, |
| 134 | ) |
| 135 | ); |
| 136 | $context = stream_context_create( $options ); |
| 137 | $response = file_get_contents( $url, false, $context ); |
| 138 | if ( $response !== false ) { |
| 139 | $data = json_decode( $response, true ); |
| 140 | $usage = $this->record_usage( $data['model'], $data['usage']['prompt_tokens'], $data['usage']['completion_tokens'] ); |
| 141 | //error_log( print_r( $usage, 1 ) ); |
| 142 | $excerpts = array(); |
| 143 | foreach ( $data['choices'] as $choice ) { |
| 144 | $choice['text'] = str_replace( '"', '', $choice['text'] ); |
| 145 | $choice['text'] = preg_replace( '/\s+/', ' ', $choice['text'] ); |
| 146 | $choice['text'] = trim( $choice['text'] ); |
| 147 | $excerpts[] = $choice['text']; |
| 148 | } |
| 149 | return $excerpts; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 |