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
querytext.php
359 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_QueryText extends Meow_MWAI_Query { |
| 4 | public $maxTokens = 1024; |
| 5 | public $temperature = 0.8; |
| 6 | public $maxSentences = 15; |
| 7 | public $isChat = false; |
| 8 | public $stop = null; |
| 9 | public $messages = []; |
| 10 | public $context = null; |
| 11 | public $newMessage = null; |
| 12 | public $promptEnding = null; |
| 13 | public $casuallyFineTuned = false; |
| 14 | |
| 15 | public function __construct( $prompt = '', $maxTokens = 1024, $model = 'gpt-3.5-turbo' ) { |
| 16 | parent::__construct( $prompt ); |
| 17 | $this->setModel( $model ); |
| 18 | $this->setMaxTokens( $maxTokens ); |
| 19 | } |
| 20 | |
| 21 | public function getLastPrompt() { |
| 22 | if ( empty( $this->messages ) ) { |
| 23 | return $this->prompt; |
| 24 | } |
| 25 | $lastMessage = end( $this->messages ); |
| 26 | return $lastMessage['content']; |
| 27 | } |
| 28 | |
| 29 | // Quick and dirty token estimation |
| 30 | // Let's keep this synchronized with Helpers in JS |
| 31 | function estimateTokens( $content ) |
| 32 | { |
| 33 | $text = ""; |
| 34 | // https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb |
| 35 | if ( is_array( $content ) ) { |
| 36 | foreach ( $content as $message ) { |
| 37 | $name = ""; |
| 38 | $role = $message['role']; |
| 39 | $content = $message['content']; |
| 40 | $text .= "=#=$role\n$content=#=\n"; |
| 41 | } |
| 42 | } |
| 43 | else { |
| 44 | $text = $content; |
| 45 | } |
| 46 | $tokens = 0; |
| 47 | return apply_filters( 'mwai_estimate_tokens', (int)$tokens, $text, $this->model ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Make sure the maxTokens is not greater than the model's context length. |
| 52 | */ |
| 53 | public function finalChecks() { |
| 54 | if ( empty( $this->model ) ) { return; } |
| 55 | |
| 56 | // Make sure the max tokens are respected. |
| 57 | $realMax = 4096; |
| 58 | $finetuneFamily = preg_match('/^([a-zA-Z]{0,32}):/', $this->model, $matches ); |
| 59 | $finetuneFamily = ( isset( $matches ) && count( $matches ) > 0 ) ? $matches[1] : 'N/A'; |
| 60 | $foundModel = null; |
| 61 | foreach ( MWAI_OPENAI_MODELS as $currentModel ) { |
| 62 | if ( $currentModel['model'] === $this->model || $currentModel['family'] === $finetuneFamily ) { |
| 63 | $foundModel = $currentModel['name']; |
| 64 | $realMax = $currentModel['maxTokens']; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | $estimatedTokens = $this->estimateTokens( $this->messages ); |
| 69 | if ( $estimatedTokens > $realMax ) { |
| 70 | throw new Exception( "AI Engine: The prompt is too long! It contains about $estimatedTokens tokens (estimation). The model $foundModel only accepts a maximum of $realMax tokens. " ); |
| 71 | } |
| 72 | $realMax = (int)($realMax - $estimatedTokens) - 16; |
| 73 | if ( $this->maxTokens > $realMax ) { |
| 74 | $this->maxTokens = $realMax; |
| 75 | } |
| 76 | |
| 77 | // Make sure the number of messages is not too great |
| 78 | if ( !empty( $this->maxSentences ) ) { |
| 79 | $context = array_shift( $this->messages ); |
| 80 | if ( !empty( $this->messages ) ) { |
| 81 | $this->messages = array_slice( $this->messages, -$this->maxSentences * 2 ); |
| 82 | } |
| 83 | else { |
| 84 | $this->messages = []; |
| 85 | } |
| 86 | if ( !empty( $context ) ) { |
| 87 | array_unshift( $this->messages, $context ); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * ID of the model to use. |
| 94 | * @param string $model ID of the model to use. |
| 95 | */ |
| 96 | public function setModel( $model ) { |
| 97 | $this->model = $model; |
| 98 | $this->mode = 'completion'; |
| 99 | foreach ( MWAI_OPENAI_MODELS as $currentModel ) { |
| 100 | if ( $currentModel['model'] === $this->model ) { |
| 101 | if ( $currentModel['mode'] ) { |
| 102 | $this->mode = $currentModel['mode']; |
| 103 | } |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Given a prompt, the model will return one or more predicted completions. |
| 111 | * It can also return the probabilities of alternative tokens at each position. |
| 112 | * @param string $prompt The prompt to generate completions. |
| 113 | */ |
| 114 | public function setPrompt( $prompt ) { |
| 115 | parent::setPrompt( $prompt ); |
| 116 | $this->validateMessages(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * The prompt is used by models who uses Text Completion (and not Chat Completion). |
| 121 | * This returns the prompt if it's not a chat, otherwise it will build a prompt with |
| 122 | * all the messages nicely formatted. |
| 123 | */ |
| 124 | public function getPrompt() { |
| 125 | if ( !$this->isChat ) { |
| 126 | return $this->prompt; |
| 127 | } |
| 128 | |
| 129 | $first = reset( $this->messages ); |
| 130 | $prompt = ""; |
| 131 | if ( $first && $first['role'] === 'system' ) { |
| 132 | $prompt = $first['content'] . "\n\n"; |
| 133 | } |
| 134 | |
| 135 | // Casually Fine-Tuned or Prompt-Ending |
| 136 | if ( !empty( $this->promptEnding ) ) { |
| 137 | $last = end( $this->messages ); |
| 138 | if ( $last && $last['role'] === 'user' ) { |
| 139 | $prompt = $last['content'] . $this->promptEnding; |
| 140 | } |
| 141 | return $prompt; |
| 142 | } |
| 143 | |
| 144 | // Standard Completion |
| 145 | while ( $message = next( $this->messages ) ) { |
| 146 | $role = $message['role']; |
| 147 | $content = $message['content']; |
| 148 | if ( $role === 'system' ) { |
| 149 | $prompt .= "$content\n\n"; |
| 150 | } |
| 151 | if ( $role === 'user' ) { |
| 152 | $prompt .= "User: $content\n"; |
| 153 | } |
| 154 | if ( $role === 'assistant' ) { |
| 155 | $prompt .= "AI: $content\n"; |
| 156 | } |
| 157 | } |
| 158 | $prompt .= "AI: "; |
| 159 | return $prompt; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Similar to the prompt, but focus on the new/last message. |
| 164 | * Only used when the model has a chat mode (and only used in messages). |
| 165 | * @param string $prompt The messages to generate completions. |
| 166 | */ |
| 167 | public function setNewMessage( $newMessage ) { |
| 168 | $this->newMessage = $newMessage; |
| 169 | $this->validateMessages(); |
| 170 | } |
| 171 | |
| 172 | public function replace( $search, $replace ) { |
| 173 | $this->prompt = str_replace( $search, $replace, $this->prompt ); |
| 174 | $this->validateMessages(); |
| 175 | } |
| 176 | |
| 177 | public function setIsChat( $isChat ) { |
| 178 | $this->isChat = $isChat; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Similar to the prompt, but use an array of messages instead. |
| 183 | * @param string $prompt The messages to generate completions. |
| 184 | */ |
| 185 | public function setMessages( $messages ) { |
| 186 | $messages = array_map( function( $message ) { |
| 187 | return [ 'role' => $message['role'], 'content' => $message['content'] ]; |
| 188 | }, $messages ); |
| 189 | $this->messages = $messages; |
| 190 | $this->validateMessages(); |
| 191 | } |
| 192 | |
| 193 | public function getLastMessage() { |
| 194 | if ( !empty( $this->messages ) ) { |
| 195 | $lastMessageIndex = count( $this->messages ) - 1; |
| 196 | $lastMessage = $this->messages[$lastMessageIndex]; |
| 197 | return $lastMessage['content']; |
| 198 | } |
| 199 | return null; |
| 200 | } |
| 201 | |
| 202 | // Function that adds a message just before the last message |
| 203 | public function injectContext( $content ) { |
| 204 | if ( !empty( $this->messages ) ) { |
| 205 | $lastMessageIndex = count( $this->messages ) - 1; |
| 206 | $lastMessage = $this->messages[$lastMessageIndex]; |
| 207 | $this->messages[$lastMessageIndex] = [ 'role' => 'system', 'content' => $content ]; |
| 208 | array_push( $this->messages, $lastMessage ); |
| 209 | } |
| 210 | $this->validateMessages(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * The context that is used for the chat completion (mode === 'chat'). |
| 215 | * @param string $context The context to use. |
| 216 | */ |
| 217 | public function setContext( $context ) { |
| 218 | $this->context = apply_filters( 'mwai_ai_context', $context, $this ); |
| 219 | $this->validateMessages(); |
| 220 | } |
| 221 | |
| 222 | private function validateMessages() { |
| 223 | // Messages should end with either the prompt or, if exists, the newMessage. |
| 224 | $message = empty( $this->newMessage ) ? $this->prompt : $this->newMessage; |
| 225 | if ( empty( $this->messages ) ) { |
| 226 | $this->messages = [ [ 'role' => 'user', 'content' => $message ] ]; |
| 227 | } |
| 228 | else { |
| 229 | $last = &$this->messages[ count( $this->messages ) - 1 ]; |
| 230 | if ( $last['role'] === 'user' ) { |
| 231 | $last['content'] = $message; |
| 232 | } |
| 233 | else { |
| 234 | array_push( $this->messages, [ 'role' => 'user', 'content' => $message ] ); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // The main context must be first. |
| 239 | if ( !empty( $this->context ) ) { |
| 240 | if ( is_array( $this->messages ) && count( $this->messages ) > 0 ) { |
| 241 | if ( $this->messages[0]['role'] !== 'system' ) { |
| 242 | array_unshift( $this->messages, [ 'role' => 'system', 'content' => $this->context ] ); |
| 243 | } |
| 244 | else { |
| 245 | $this->messages[0]['content'] = $this->context; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * The maximum number of tokens to generate in the completion. |
| 253 | * The token count of your prompt plus max_tokens cannot exceed the model's context length. |
| 254 | * Most models have a context length of 2048 tokens (except for the newest models, which support 4096). |
| 255 | * @param float $prompt The maximum number of tokens. |
| 256 | */ |
| 257 | public function setMaxTokens( $maxTokens ) { |
| 258 | $this->maxTokens = intval( $maxTokens ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Set the sampling temperature to use. Higher values means the model will take more risks. |
| 263 | * Try 0.9 for more creative applications, and 0 for ones with a well-defined answer. |
| 264 | * @param float $temperature The temperature. |
| 265 | */ |
| 266 | public function setTemperature( $temperature ) { |
| 267 | $temperature = floatval( $temperature ); |
| 268 | if ( $temperature > 1 ) { |
| 269 | $temperature = 1; |
| 270 | } |
| 271 | if ( $temperature < 0 ) { |
| 272 | $temperature = 0; |
| 273 | } |
| 274 | $this->temperature = round( $temperature, 2 ); |
| 275 | } |
| 276 | |
| 277 | public function setMaxSentences( $maxSentences ) { |
| 278 | if ( !empty( $maxSentences ) ) { |
| 279 | $this->maxSentences = intval( $maxSentences ); |
| 280 | $this->validateMessages(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | public function setStop( $stop ) { |
| 285 | $this->stop = $stop; |
| 286 | } |
| 287 | |
| 288 | // Based on the params of the query, update the attributes |
| 289 | public function injectParams( $params ) { |
| 290 | if ( isset( $params['model'] ) ) { |
| 291 | $this->setModel( $params['model'] ); |
| 292 | } |
| 293 | if ( isset( $params['casually_fine_tuned'] ) && $params['casually_fine_tuned'] === true ) { |
| 294 | $this->promptEnding = "\\n\\n###\\n\\n"; |
| 295 | $this->stop = "\\n\\n"; |
| 296 | $this->casuallyFineTuned = true; |
| 297 | } |
| 298 | if ( isset( $params['casuallyFineTuned'] ) && $params['casuallyFineTuned'] === true ) { |
| 299 | $this->promptEnding = "\\n\\n###\\n\\n"; |
| 300 | $this->stop = "\\n\\n"; |
| 301 | $this->casuallyFineTuned = true; |
| 302 | } |
| 303 | if ( isset( $params['prompt'] ) ) { |
| 304 | $this->setPrompt( $params['prompt'] ); |
| 305 | } |
| 306 | if ( isset( $params['context'] ) ) { |
| 307 | $this->setContext( $params['context'] ); |
| 308 | } |
| 309 | if ( isset( $params['messages'] ) ) { |
| 310 | $this->setMessages( $params['messages'] ); |
| 311 | } |
| 312 | if ( isset( $params['new_message'] ) ) { |
| 313 | $this->setNewMessage( $params['newMessage'] ); |
| 314 | } |
| 315 | if ( isset( $params['newMessage'] ) ) { |
| 316 | $this->setNewMessage( $params['newMessage'] ); |
| 317 | } |
| 318 | if ( isset( $params['max_tokens'] ) && intval( $params['max_tokens'] ) > 0 ) { |
| 319 | $this->setMaxTokens( intval( $params['max_tokens'] ) ); |
| 320 | } |
| 321 | if ( isset( $params['maxTokens'] ) && intval( $params['maxTokens'] ) > 0 ) { |
| 322 | $this->setMaxTokens( intval( $params['maxTokens'] ) ); |
| 323 | } |
| 324 | if ( isset( $params['max_sentences'] ) && intval( $params['max_sentences'] ) > 0 ) { |
| 325 | $this->setMaxSentences( intval( $params['max_sentences'] ) ); |
| 326 | } |
| 327 | if ( isset( $params['maxSentences'] ) && intval( $params['maxSentences'] ) > 0 ) { |
| 328 | $this->setMaxSentences( intval( $params['maxSentences'] ) ); |
| 329 | } |
| 330 | if ( isset( $params['temperature'] ) ) { |
| 331 | $this->setTemperature( $params['temperature'] ); |
| 332 | } |
| 333 | if ( isset( $params['stop'] ) ) { |
| 334 | $this->setStop( $params['stop'] ); |
| 335 | } |
| 336 | if ( isset( $params['max_results'] ) ) { |
| 337 | $this->setMaxResults( $params['max_results'] ); |
| 338 | } |
| 339 | if ( isset( $params['maxResults'] ) ) { |
| 340 | $this->setMaxResults( $params['maxResults'] ); |
| 341 | } |
| 342 | if ( isset( $params['env'] ) ) { |
| 343 | $this->setEnv( $params['env'] ); |
| 344 | } |
| 345 | if ( isset( $params['session'] ) ) { |
| 346 | $this->setSession( $params['session'] ); |
| 347 | } |
| 348 | // Should add the params related to Open AI and Azure |
| 349 | if ( isset( $params['service'] ) ) { |
| 350 | $this->setService( $params['service'] ); |
| 351 | } |
| 352 | if ( isset( $params['api_key'] ) ) { |
| 353 | $this->setApiKey( $params['apiKey'] ); |
| 354 | } |
| 355 | if ( isset( $params['apiKey'] ) ) { |
| 356 | $this->setApiKey( $params['apiKey'] ); |
| 357 | } |
| 358 | } |
| 359 | } |