modules
3 years ago
admin.php
3 years ago
ai.php
3 years ago
answer.php
3 years ago
api.php
3 years ago
core.php
3 years ago
init.php
3 years ago
openai.php
3 years ago
query.php
3 years ago
queryembed.php
3 years ago
queryimage.php
3 years ago
querytext.php
3 years ago
querytranscribe.php
3 years ago
rest.php
3 years ago
security.php
3 years ago
ui.php
3 years ago
querytext.php
259 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_QueryText extends Meow_MWAI_Query { |
| 4 | public $maxTokens = 1024; |
| 5 | public $temperature = 0.8; |
| 6 | public $stop = null; |
| 7 | public $messages = []; |
| 8 | public $context = null; |
| 9 | public $newMessage = null; |
| 10 | |
| 11 | public function __construct( $prompt = '', $maxTokens = 1024, $model = 'gpt-3.5-turbo' ) { |
| 12 | parent::__construct( $prompt ); |
| 13 | $this->setModel( $model ); |
| 14 | $this->setMaxTokens( $maxTokens ); |
| 15 | } |
| 16 | |
| 17 | public function getLastPrompt() { |
| 18 | if ( empty( $this->messages ) ) { |
| 19 | return $this->prompt; |
| 20 | } |
| 21 | $lastMessage = end( $this->messages ); |
| 22 | return $lastMessage['content']; |
| 23 | } |
| 24 | |
| 25 | // Quick and dirty token estimation |
| 26 | // Let's keep this synchronized with Helpers in JS |
| 27 | function estimateTokens( $content ) |
| 28 | { |
| 29 | $text = ""; |
| 30 | // https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb |
| 31 | if ( is_array( $content ) ) { |
| 32 | foreach ( $content as $message ) { |
| 33 | $name = ""; |
| 34 | $role = $message['role']; |
| 35 | $content = $message['content']; |
| 36 | $text .= "=#=$role\n$content=#=\n"; |
| 37 | } |
| 38 | } |
| 39 | else { |
| 40 | $text = $content; |
| 41 | } |
| 42 | $tokens = 0; |
| 43 | return apply_filters( 'mwai_estimate_tokens', (int)$tokens, $text, $this->model ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Make sure the maxTokens is not greater than the model's context length. |
| 48 | */ |
| 49 | public function checkFix() { |
| 50 | if ( empty( $this->model ) ) { return; } |
| 51 | $realMax = 4096; |
| 52 | $finetuneFamily = preg_match('/^([a-zA-Z]{0,32}):/', $this->model, $matches ); |
| 53 | $finetuneFamily = ( isset( $matches ) && count( $matches ) > 0 ) ? $matches[1] : 'N/A'; |
| 54 | $foundModel = null; |
| 55 | foreach ( MWAI_OPENAI_MODELS as $currentModel ) { |
| 56 | if ( $currentModel['model'] === $this->model || $currentModel['family'] === $finetuneFamily ) { |
| 57 | $foundModel = $currentModel['name']; |
| 58 | $realMax = $currentModel['maxTokens']; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | $estimatedTokens = $this->estimateTokens( $this->messages ); |
| 63 | if ( $estimatedTokens > $realMax ) { |
| 64 | throw new Exception( "The prompt is too long! It contains about $estimatedTokens tokens (estimation). The model $foundModel only accepts a maximum of $realMax tokens. " ); |
| 65 | } |
| 66 | $realMax = (int)($realMax - $estimatedTokens) - 16; |
| 67 | if ( $this->maxTokens > $realMax ) { |
| 68 | $this->maxTokens = $realMax; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * ID of the model to use. |
| 74 | * @param string $model ID of the model to use. |
| 75 | */ |
| 76 | public function setModel( $model ) { |
| 77 | $this->model = $model; |
| 78 | $this->mode = 'completion'; |
| 79 | foreach ( MWAI_OPENAI_MODELS as $currentModel ) { |
| 80 | if ( $currentModel['model'] === $this->model ) { |
| 81 | if ( $currentModel['mode'] ) { |
| 82 | $this->mode = $currentModel['mode']; |
| 83 | } |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Given a prompt, the model will return one or more predicted completions. |
| 91 | * It can also return the probabilities of alternative tokens at each position. |
| 92 | * @param string $prompt The prompt to generate completions. |
| 93 | */ |
| 94 | public function setPrompt( $prompt ) { |
| 95 | parent::setPrompt( $prompt ); |
| 96 | $this->validateMessages(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Similar to the prompt, but focus on the new/last message. |
| 101 | * Only used when the model has a chat mode (and only used in messages). |
| 102 | * @param string $prompt The messages to generate completions. |
| 103 | */ |
| 104 | public function setNewMessage( $newMessage ) { |
| 105 | $this->newMessage = $newMessage; |
| 106 | $this->validateMessages(); |
| 107 | } |
| 108 | |
| 109 | public function replace( $search, $replace ) { |
| 110 | $this->prompt = str_replace( $search, $replace, $this->prompt ); |
| 111 | $this->validateMessages(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Similar to the prompt, but use an array of messages instead. |
| 116 | * @param string $prompt The messages to generate completions. |
| 117 | */ |
| 118 | public function setMessages( $messages ) { |
| 119 | $messages = array_map( function( $message ) { |
| 120 | return [ 'role' => $message['role'], 'content' => $message['content'] ]; |
| 121 | }, $messages ); |
| 122 | $this->messages = $messages; |
| 123 | $this->validateMessages(); |
| 124 | } |
| 125 | |
| 126 | public function getLastMessage() { |
| 127 | if ( !empty( $this->messages ) ) { |
| 128 | return $this->messages[count( $this->messages ) - 1]; |
| 129 | } |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | // Function that adds a message just before the last message |
| 134 | public function injectContext( $content ) { |
| 135 | if ( !empty( $this->messages ) ) { |
| 136 | $lastMessage = $this->getLastMessage(); |
| 137 | $lastMessageIndex = count( $this->messages ) - 1; |
| 138 | $this->messages[$lastMessageIndex] = [ 'role' => 'system', 'content' => $content ]; |
| 139 | array_push( $this->messages, $lastMessage ); |
| 140 | } |
| 141 | $this->validateMessages(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * The context that is used for the chat completion (mode === 'chat'). |
| 146 | * @param string $context The context to use. |
| 147 | */ |
| 148 | public function setContext( $context ) { |
| 149 | $this->context = apply_filters( 'mwai_ai_context', $context, $this ); |
| 150 | $this->validateMessages(); |
| 151 | } |
| 152 | |
| 153 | private function validateMessages() { |
| 154 | // Messages should end with either the prompt or, if exists, the newMessage. |
| 155 | $message = empty( $this->newMessage ) ? $this->prompt : $this->newMessage; |
| 156 | if ( empty( $this->messages ) ) { |
| 157 | $this->messages = [ [ 'role' => 'user', 'content' => $message ] ]; |
| 158 | } |
| 159 | else { |
| 160 | $last = &$this->messages[ count( $this->messages ) - 1 ]; |
| 161 | if ( $last['role'] === 'user' ) { |
| 162 | $last['content'] = $message; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // The main context must be first. |
| 167 | if ( !empty( $this->context ) ) { |
| 168 | if ( is_array( $this->messages ) && count( $this->messages ) > 0 ) { |
| 169 | if ( $this->messages[0]['role'] !== 'system' ) { |
| 170 | array_unshift( $this->messages, [ 'role' => 'system', 'content' => $this->context ] ); |
| 171 | } |
| 172 | else { |
| 173 | $this->messages[0]['content'] = $this->context; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * The maximum number of tokens to generate in the completion. |
| 181 | * The token count of your prompt plus max_tokens cannot exceed the model's context length. |
| 182 | * Most models have a context length of 2048 tokens (except for the newest models, which support 4096). |
| 183 | * @param float $prompt The maximum number of tokens. |
| 184 | */ |
| 185 | public function setMaxTokens( $maxTokens ) { |
| 186 | $this->maxTokens = intval( $maxTokens ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Set the sampling temperature to use. Higher values means the model will take more risks. |
| 191 | * Try 0.9 for more creative applications, and 0 for ones with a well-defined answer. |
| 192 | * @param float $temperature The temperature. |
| 193 | */ |
| 194 | public function setTemperature( $temperature ) { |
| 195 | $temperature = floatval( $temperature ); |
| 196 | if ( $temperature > 1 ) { |
| 197 | $temperature = 1; |
| 198 | } |
| 199 | if ( $temperature < 0 ) { |
| 200 | $temperature = 0; |
| 201 | } |
| 202 | $this->temperature = round( $temperature, 2 ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Up to 4 sequences where the API will stop generating further tokens. |
| 207 | * The returned text will not contain the stop sequence. |
| 208 | * @param float $stop The stop. |
| 209 | */ |
| 210 | public function setStop( $stop ) { |
| 211 | if ( !empty( $stop ) ) { |
| 212 | $this->stop = $stop; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Based on the params of the query, update the attributes |
| 217 | public function injectParams( $params ) { |
| 218 | if ( isset( $params['model'] ) ) { |
| 219 | $this->setModel( $params['model'] ); |
| 220 | } |
| 221 | if ( isset( $params['prompt'] ) ) { |
| 222 | $this->setPrompt( $params['prompt'] ); |
| 223 | } |
| 224 | if ( isset( $params['context'] ) ) { |
| 225 | $this->setContext( $params['context'] ); |
| 226 | } |
| 227 | if ( isset( $params['messages'] ) ) { |
| 228 | $this->setMessages( $params['messages'] ); |
| 229 | } |
| 230 | if ( isset( $params['newMessage'] ) ) { |
| 231 | $this->setNewMessage( $params['newMessage'] ); |
| 232 | } |
| 233 | if ( isset( $params['maxTokens'] ) ) { |
| 234 | $this->setMaxTokens( $params['maxTokens'] ); |
| 235 | } |
| 236 | if ( isset( $params['temperature'] ) ) { |
| 237 | $this->setTemperature( $params['temperature'] ); |
| 238 | } |
| 239 | if ( isset( $params['stop'] ) ) { |
| 240 | $this->setStop( $params['stop'] ); |
| 241 | } |
| 242 | if ( isset( $params['maxResults'] ) ) { |
| 243 | $this->setMaxResults( $params['maxResults'] ); |
| 244 | } |
| 245 | if ( isset( $params['env'] ) ) { |
| 246 | $this->setEnv( $params['env'] ); |
| 247 | } |
| 248 | if ( isset( $params['session'] ) ) { |
| 249 | $this->setSession( $params['session'] ); |
| 250 | } |
| 251 | // Should add the params related to Open AI and Azure |
| 252 | if ( isset( $params['service'] ) ) { |
| 253 | $this->setService( $params['service'] ); |
| 254 | } |
| 255 | if ( isset( $params['apiKey'] ) ) { |
| 256 | $this->setApiKey( $params['apiKey'] ); |
| 257 | } |
| 258 | } |
| 259 | } |