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
140 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 | |
| 8 | public function __construct( $prompt = '', $maxTokens = 16, $model = 'text-davinci-003' ) { |
| 9 | $this->prompt = $prompt; |
| 10 | $this->maxTokens = $maxTokens; |
| 11 | $this->model = $model; |
| 12 | $this->mode = "completion"; |
| 13 | } |
| 14 | |
| 15 | // Quick and dirty token estimation |
| 16 | function estimate_tokens( $text, $method = "max" ) |
| 17 | { |
| 18 | // method can be "average", "words", "chars", "max", "min", defaults to "max" |
| 19 | // "average" is the average of words and chars |
| 20 | // "words" is the word count divided by 0.75 |
| 21 | // "chars" is the char count divided by 4 |
| 22 | // "max" is the max of word and char |
| 23 | // "min" is the min of word and char |
| 24 | $word_count = count(explode(" ", $text)); |
| 25 | $char_count = strlen($text); |
| 26 | $tokens_count_word_est = $word_count / 0.75; |
| 27 | $tokens_count_char_est = $char_count / 4.0; |
| 28 | $output = 0; |
| 29 | if ( $method == 'average' ) { |
| 30 | $output = ($tokens_count_word_est + $tokens_count_char_est) / 2; |
| 31 | } |
| 32 | else if ( $method == 'words' ) { |
| 33 | $output = $tokens_count_word_est; |
| 34 | } |
| 35 | else if ( $method == 'chars' ) { |
| 36 | $output = $tokens_count_char_est; |
| 37 | } |
| 38 | else if ( $method == 'max') { |
| 39 | $output = max($tokens_count_word_est, $tokens_count_char_est); |
| 40 | } |
| 41 | else if ( $method == 'min') { |
| 42 | $output = min($tokens_count_word_est, $tokens_count_char_est); |
| 43 | } |
| 44 | else { |
| 45 | // return invalid method message |
| 46 | return "Invalid method. Use 'average', 'words', 'chars', 'max', or 'min'."; |
| 47 | } |
| 48 | return (int)$output; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Make sure the maxTokens is not greater than the model's context length. |
| 54 | */ |
| 55 | private function validateMaxTokens() { |
| 56 | $contains = strpos( $this->model, 'davinci' ) !== false; |
| 57 | $realMax = $contains ? 4096 : 2048; |
| 58 | $estimatedTokens = $this->estimate_tokens( $this->prompt ); |
| 59 | $realMax = $realMax - $estimatedTokens - 32; |
| 60 | if ( $this->maxTokens > $realMax ) { |
| 61 | $this->maxTokens = $realMax; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Given a prompt, the model will return one or more predicted completions. |
| 67 | * It can also return the probabilities of alternative tokens at each position. |
| 68 | * @param string $prompt The prompt to generate completions. |
| 69 | */ |
| 70 | public function setPrompt( $prompt ) { |
| 71 | parent::setPrompt( $prompt ); |
| 72 | $this->validateMaxTokens(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * The maximum number of tokens to generate in the completion. |
| 77 | * The token count of your prompt plus max_tokens cannot exceed the model's context length. |
| 78 | * Most models have a context length of 2048 tokens (except for the newest models, which support 4096). |
| 79 | * @param float $prompt The maximum number of tokens. |
| 80 | */ |
| 81 | public function setMaxTokens( $maxTokens ) { |
| 82 | $this->maxTokens = intval( $maxTokens ); |
| 83 | $this->validateMaxTokens(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Set the sampling temperature to use. Higher values means the model will take more risks. |
| 88 | * Try 0.9 for more creative applications, and 0 for ones with a well-defined answer. |
| 89 | * @param float $temperature The temperature. |
| 90 | */ |
| 91 | public function setTemperature( $temperature ) { |
| 92 | $temperature = floatval( $temperature ); |
| 93 | if ( $temperature > 1 ) { |
| 94 | $temperature = 1; |
| 95 | } |
| 96 | if ( $temperature < 0 ) { |
| 97 | $temperature = 0; |
| 98 | } |
| 99 | $this->temperature = $temperature; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Up to 4 sequences where the API will stop generating further tokens. |
| 104 | * The returned text will not contain the stop sequence. |
| 105 | * @param float $stop The stop. |
| 106 | */ |
| 107 | public function setStop( $stop ) { |
| 108 | if ( !empty( $stop ) ) { |
| 109 | $this->stop = $stop; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Based on the params of the query, update the attributes |
| 114 | public function injectParams( $params ) { |
| 115 | if ( isset( $params['model'] ) ) { |
| 116 | $this->setModel( $params['model'] ); |
| 117 | } |
| 118 | if ( isset( $params['maxTokens'] ) ) { |
| 119 | $this->setMaxTokens( $params['maxTokens'] ); |
| 120 | } |
| 121 | if ( isset( $params['temperature'] ) ) { |
| 122 | $this->setTemperature( $params['temperature'] ); |
| 123 | } |
| 124 | if ( isset( $params['stop'] ) ) { |
| 125 | $this->setStop( $params['stop'] ); |
| 126 | } |
| 127 | if ( isset( $params['apiKey'] ) ) { |
| 128 | $this->setApiKey( $params['apiKey'] ); |
| 129 | } |
| 130 | if ( isset( $params['maxResults'] ) ) { |
| 131 | $this->setMaxResults( $params['maxResults'] ); |
| 132 | } |
| 133 | if ( isset( $params['env'] ) ) { |
| 134 | $this->setEnv( $params['env'] ); |
| 135 | } |
| 136 | if ( isset( $params['session'] ) ) { |
| 137 | $this->setSession( $params['session'] ); |
| 138 | } |
| 139 | } |
| 140 | } |