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