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