assist-feedback.php
10 months ago
assistant.php
10 months ago
base.php
10 months ago
dropped-file.php
11 months ago
edit-image.php
11 months ago
embed.php
10 months ago
feedback.php
10 months ago
function.php
11 months ago
image.php
10 months ago
parameter.php
11 months ago
text.php
10 months ago
transcribe.php
11 months ago
text.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Text extends Meow_MWAI_Query_Base implements JsonSerializable { |
| 4 | // Core Content |
| 5 | public ?Meow_MWAI_Query_DroppedFile $attachedFile = null; |
| 6 | |
| 7 | // Parameters |
| 8 | public ?float $temperature = null; |
| 9 | public ?int $maxTokens = null; |
| 10 | public ?string $stop = null; |
| 11 | public ?string $responseFormat = null; |
| 12 | public ?string $reasoning = null; // GPT-5 reasoning effort |
| 13 | public ?string $verbosity = null; // GPT-5 verbosity level |
| 14 | |
| 15 | #region Constructors, Serialization |
| 16 | |
| 17 | public function __construct( ?string $message = '', ?int $maxTokens = null, string $model = null ) { |
| 18 | parent::__construct( $message ); |
| 19 | if ( !empty( $model ) ) { |
| 20 | $this->set_model( $model ); |
| 21 | } |
| 22 | if ( !empty( $maxTokens ) ) { |
| 23 | $this->set_max_tokens( $maxTokens ); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | #[\ReturnTypeWillChange] |
| 28 | public function jsonSerialize(): array { |
| 29 | $json = [ |
| 30 | 'message' => $this->message, |
| 31 | 'instructions' => $this->instructions, |
| 32 | |
| 33 | 'ai' => [ |
| 34 | 'model' => $this->model, |
| 35 | 'feature' => $this->feature, |
| 36 | 'maxTokens' => $this->maxTokens, |
| 37 | 'temperature' => $this->temperature, |
| 38 | ], |
| 39 | |
| 40 | 'system' => [ |
| 41 | 'class' => get_class( $this ), |
| 42 | 'envId' => $this->envId, |
| 43 | 'scope' => $this->scope, |
| 44 | 'session' => $this->session, |
| 45 | 'customId' => $this->customId, |
| 46 | 'maxMessages' => $this->maxMessages, |
| 47 | ] |
| 48 | ]; |
| 49 | |
| 50 | if ( !empty( $this->context ) ) { |
| 51 | $json['context']['content'] = $this->context; |
| 52 | } |
| 53 | |
| 54 | if ( !empty( $this->attachedFile ) ) { |
| 55 | $json['context']['hasFile'] = true; |
| 56 | if ( $this->attachedFile->get_type() === 'url' ) { |
| 57 | $json['context']['fileUrl'] = $this->attachedFile->get_url(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return $json; |
| 62 | } |
| 63 | |
| 64 | #endregion |
| 65 | |
| 66 | #region File Handling |
| 67 | |
| 68 | public function set_file( Meow_MWAI_Query_DroppedFile $file ): void { |
| 69 | $this->attachedFile = $file; |
| 70 | } |
| 71 | |
| 72 | #endregion |
| 73 | |
| 74 | #region Parameters |
| 75 | |
| 76 | /** |
| 77 | * The type of return expected from the API. It can be either null or "json". |
| 78 | * @param int $maxResults The maximum number of completions. |
| 79 | */ |
| 80 | public function set_response_format( $responseFormat ) { |
| 81 | if ( !empty( $responseFormat ) && $responseFormat !== 'json' ) { |
| 82 | throw new Exception( 'AI Engine: The response format can only be null or json.' ); |
| 83 | } |
| 84 | $this->responseFormat = $responseFormat; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * The maximum number of tokens to generate in the completion. |
| 89 | * The token count of your prompt plus max_tokens cannot exceed the model's context length. |
| 90 | * Most models have a context length of 2048 tokens (except for the newest models, which support 4096). |
| 91 | * @param float $maxTokens The maximum number of tokens. |
| 92 | */ |
| 93 | public function set_max_tokens( int $maxTokens ): void { |
| 94 | $this->maxTokens = $maxTokens; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Set the sampling temperature to use. Higher values means the model will take more risks. |
| 99 | * Try 0.9 for more creative applications, and 0 for ones with a well-defined reply. |
| 100 | * @param float $temperature The temperature. |
| 101 | */ |
| 102 | public function set_temperature( float $temperature ): void { |
| 103 | $temperature = floatval( $temperature ); |
| 104 | if ( $temperature > 1 ) { |
| 105 | $temperature = 1; |
| 106 | } |
| 107 | if ( $temperature < 0 ) { |
| 108 | $temperature = 0; |
| 109 | } |
| 110 | $this->temperature = round( $temperature, 2 ); |
| 111 | } |
| 112 | |
| 113 | public function set_stop( string $stop ): void { |
| 114 | $this->stop = $stop; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Set the reasoning effort for GPT-5 models. |
| 119 | * @param string $reasoning The reasoning effort level (minimal, low, medium, high). |
| 120 | */ |
| 121 | public function set_reasoning( string $reasoning ): void { |
| 122 | $valid = ['minimal', 'low', 'medium', 'high']; |
| 123 | if ( !in_array( $reasoning, $valid ) ) { |
| 124 | throw new Exception( 'AI Engine: Invalid reasoning level. Must be one of: minimal, low, medium, high.' ); |
| 125 | } |
| 126 | $this->reasoning = $reasoning; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Set the verbosity level for GPT-5 models. |
| 131 | * @param string $verbosity The verbosity level (low, medium, high). |
| 132 | */ |
| 133 | public function set_verbosity( string $verbosity ): void { |
| 134 | $valid = ['low', 'medium', 'high']; |
| 135 | if ( !in_array( $verbosity, $valid ) ) { |
| 136 | throw new Exception( 'AI Engine: Invalid verbosity level. Must be one of: low, medium, high.' ); |
| 137 | } |
| 138 | $this->verbosity = $verbosity; |
| 139 | } |
| 140 | |
| 141 | #endregion |
| 142 | |
| 143 | #region Inject Params |
| 144 | |
| 145 | // Based on the params of the query, update the attributes |
| 146 | public function inject_params( array $params ): void { |
| 147 | parent::inject_params( $params ); |
| 148 | $params = $this->convert_keys( $params ); |
| 149 | |
| 150 | if ( !empty( $params['maxTokens'] ) && intval( $params['maxTokens'] ) > 0 ) { |
| 151 | $this->set_max_tokens( intval( $params['maxTokens'] ) ); |
| 152 | } |
| 153 | if ( isset( $params['temperature'] ) && $params['temperature'] !== '' ) { |
| 154 | $this->set_temperature( $params['temperature'] ); |
| 155 | } |
| 156 | if ( !empty( $params['stop'] ) ) { |
| 157 | $this->set_stop( $params['stop'] ); |
| 158 | } |
| 159 | if ( !empty( $params['responseFormat'] ) ) { |
| 160 | $this->set_response_format( $params['responseFormat'] ); |
| 161 | } |
| 162 | // Accept both 'reasoning' and 'reasoningEffort' (UI uses reasoningEffort) |
| 163 | if ( !empty( $params['reasoning'] ) ) { |
| 164 | $this->set_reasoning( $params['reasoning'] ); |
| 165 | } |
| 166 | if ( !empty( $params['reasoningEffort'] ) ) { |
| 167 | $this->set_reasoning( $params['reasoningEffort'] ); |
| 168 | } |
| 169 | if ( !empty( $params['verbosity'] ) ) { |
| 170 | $this->set_verbosity( $params['verbosity'] ); |
| 171 | } |
| 172 | // Store prompt-related params as extra params |
| 173 | if ( !empty( $params['promptId'] ) ) { |
| 174 | $this->setExtraParam( 'promptId', $params['promptId'] ); |
| 175 | } |
| 176 | // TODO: Prompt Variables support - might be added later |
| 177 | // if ( !empty( $params['promptVariables'] ) ) { |
| 178 | // $this->setExtraParam( 'promptVariables', $params['promptVariables'] ); |
| 179 | // } |
| 180 | // if ( !empty( $params['promptVersion'] ) ) { |
| 181 | // $this->setExtraParam( 'promptVersion', $params['promptVersion'] ); |
| 182 | // } |
| 183 | } |
| 184 | |
| 185 | #endregion |
| 186 | } |
| 187 |