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
querytext.php
213 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_QueryText extends Meow_MWAI_Query { |
| 4 | public $maxTokens = 16; |
| 5 | public $temperature = 0.8; |
| 6 | public $stop = null; |
| 7 | public $messages = []; |
| 8 | public $context = null; |
| 9 | |
| 10 | public function __construct( $prompt = '', $maxTokens = 16, $model = 'gpt-3.5-turbo' ) { |
| 11 | $this->setPrompt( $prompt ); |
| 12 | $this->setModel( $model ); |
| 13 | $this->setMaxTokens( $maxTokens ); |
| 14 | } |
| 15 | |
| 16 | // Quick and dirty token estimation |
| 17 | function estimateTokens( $text, $method = "max" ) |
| 18 | { |
| 19 | // method can be "average", "words", "chars", "max", "min", defaults to "max" |
| 20 | // "average" is the average of words and chars |
| 21 | // "words" is the word count divided by 0.75 |
| 22 | // "chars" is the char count divided by 4 |
| 23 | // "max" is the max of word and char |
| 24 | // "min" is the min of word and char |
| 25 | $word_count = count(explode(" ", $text)); |
| 26 | $char_count = strlen($text); |
| 27 | $tokens_count_word_est = $word_count / 0.75; |
| 28 | $tokens_count_char_est = $char_count / 4.0; |
| 29 | $output = 0; |
| 30 | if ( $method == 'average' ) { |
| 31 | $output = ($tokens_count_word_est + $tokens_count_char_est) / 2; |
| 32 | } |
| 33 | else if ( $method == 'words' ) { |
| 34 | $output = $tokens_count_word_est; |
| 35 | } |
| 36 | else if ( $method == 'chars' ) { |
| 37 | $output = $tokens_count_char_est; |
| 38 | } |
| 39 | else if ( $method == 'max') { |
| 40 | $output = max($tokens_count_word_est, $tokens_count_char_est); |
| 41 | } |
| 42 | else if ( $method == 'min') { |
| 43 | $output = min($tokens_count_word_est, $tokens_count_char_est); |
| 44 | } |
| 45 | else { |
| 46 | // return invalid method message |
| 47 | return "Invalid method. Use 'average', 'words', 'chars', 'max', or 'min'."; |
| 48 | } |
| 49 | return (int)$output; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Make sure the maxTokens is not greater than the model's context length. |
| 54 | */ |
| 55 | private function validateMaxTokens() { |
| 56 | $realMax = 256; |
| 57 | $finetuneFamily = preg_match('/^([a-zA-Z]{0,32}):/', $this->model, $matches ); |
| 58 | $finetuneFamily = ( isset( $matches ) && count( $matches ) > 0 ) ? $matches[1] : 'N/A'; |
| 59 | $foundModel = null; |
| 60 | foreach ( MWAI_OPENAI_MODELS as $currentModel ) { |
| 61 | if ( $currentModel['model'] === $this->model || $currentModel['family'] === $finetuneFamily ) { |
| 62 | $foundModel = $currentModel; |
| 63 | $realMax = $currentModel['maxTokens']; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | $estimatedTokens = $this->estimateTokens( $this->prompt ); |
| 68 | $realMax = $realMax - $estimatedTokens - 32; |
| 69 | if ( $this->maxTokens > $realMax ) { |
| 70 | $this->maxTokens = $realMax; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * ID of the model to use. |
| 76 | * @param string $model ID of the model to use. |
| 77 | */ |
| 78 | public function setModel( $model ) { |
| 79 | $this->model = $model; |
| 80 | if ( $model === 'gpt-3.5-turbo') { |
| 81 | $this->mode = 'chat'; |
| 82 | } |
| 83 | else { |
| 84 | $this->mode = 'completion'; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Given a prompt, the model will return one or more predicted completions. |
| 90 | * It can also return the probabilities of alternative tokens at each position. |
| 91 | * @param string $prompt The prompt to generate completions. |
| 92 | */ |
| 93 | public function setPrompt( $prompt ) { |
| 94 | parent::setPrompt( $prompt ); |
| 95 | $this->validateMaxTokens(); |
| 96 | $this->validateMessages(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Similar to the prompt, but use an array of messages instead. |
| 101 | * @param string $prompt The messages to generate completions. |
| 102 | */ |
| 103 | public function setMessages( $messages ) { |
| 104 | $messages = array_map( function( $message ) { |
| 105 | return [ 'role' => $message['role'], 'content' => $message['content'] ]; |
| 106 | }, $messages ); |
| 107 | $this->messages = $messages; |
| 108 | $this->validateMessages(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * The context that is used for the chat completion (mode === 'chat'). |
| 113 | * @param string $context The context to use. |
| 114 | */ |
| 115 | public function setContext( $context ) { |
| 116 | $this->context = apply_filters( 'mwai_ai_context', $context, $this ); |
| 117 | $this->validateMessages(); |
| 118 | } |
| 119 | |
| 120 | private function validateMessages() { |
| 121 | if ( empty( $this->messages ) ) { |
| 122 | $this->messages = []; |
| 123 | if ( !empty( $this->prompt ) ) { |
| 124 | array_push( $this->messages, [ 'role' => 'user', 'content' => $this->prompt ] ); |
| 125 | } |
| 126 | } |
| 127 | if ( !empty( $this->context ) ) { |
| 128 | if ( is_array( $this->messages ) && count( $this->messages ) > 0 ) { |
| 129 | if ( $this->messages[0]['role'] !== 'system' ) { |
| 130 | array_unshift( $this->messages, [ 'role' => 'system', 'content' => $this->context ] ); |
| 131 | } |
| 132 | else { |
| 133 | $this->messages[0]['content'] = $this->context; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * The maximum number of tokens to generate in the completion. |
| 141 | * The token count of your prompt plus max_tokens cannot exceed the model's context length. |
| 142 | * Most models have a context length of 2048 tokens (except for the newest models, which support 4096). |
| 143 | * @param float $prompt The maximum number of tokens. |
| 144 | */ |
| 145 | public function setMaxTokens( $maxTokens ) { |
| 146 | $this->maxTokens = intval( $maxTokens ); |
| 147 | $this->validateMaxTokens(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Set the sampling temperature to use. Higher values means the model will take more risks. |
| 152 | * Try 0.9 for more creative applications, and 0 for ones with a well-defined answer. |
| 153 | * @param float $temperature The temperature. |
| 154 | */ |
| 155 | public function setTemperature( $temperature ) { |
| 156 | $temperature = floatval( $temperature ); |
| 157 | if ( $temperature > 1 ) { |
| 158 | $temperature = 1; |
| 159 | } |
| 160 | if ( $temperature < 0 ) { |
| 161 | $temperature = 0; |
| 162 | } |
| 163 | $this->temperature = $temperature; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Up to 4 sequences where the API will stop generating further tokens. |
| 168 | * The returned text will not contain the stop sequence. |
| 169 | * @param float $stop The stop. |
| 170 | */ |
| 171 | public function setStop( $stop ) { |
| 172 | if ( !empty( $stop ) ) { |
| 173 | $this->stop = $stop; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Based on the params of the query, update the attributes |
| 178 | public function injectParams( $params ) { |
| 179 | if ( isset( $params['model'] ) ) { |
| 180 | $this->setModel( $params['model'] ); |
| 181 | } |
| 182 | if ( isset( $params['prompt'] ) ) { |
| 183 | $this->setPrompt( $params['prompt'] ); |
| 184 | } |
| 185 | if ( isset( $params['messages'] ) ) { |
| 186 | $this->setMessages( $params['messages'] ); |
| 187 | } |
| 188 | if ( isset( $params['context'] ) ) { |
| 189 | $this->setContext( $params['context'] ); |
| 190 | } |
| 191 | if ( isset( $params['maxTokens'] ) ) { |
| 192 | $this->setMaxTokens( $params['maxTokens'] ); |
| 193 | } |
| 194 | if ( isset( $params['temperature'] ) ) { |
| 195 | $this->setTemperature( $params['temperature'] ); |
| 196 | } |
| 197 | if ( isset( $params['stop'] ) ) { |
| 198 | $this->setStop( $params['stop'] ); |
| 199 | } |
| 200 | if ( isset( $params['apiKey'] ) ) { |
| 201 | $this->setApiKey( $params['apiKey'] ); |
| 202 | } |
| 203 | if ( isset( $params['maxResults'] ) ) { |
| 204 | $this->setMaxResults( $params['maxResults'] ); |
| 205 | } |
| 206 | if ( isset( $params['env'] ) ) { |
| 207 | $this->setEnv( $params['env'] ); |
| 208 | } |
| 209 | if ( isset( $params['session'] ) ) { |
| 210 | $this->setSession( $params['session'] ); |
| 211 | } |
| 212 | } |
| 213 | } |