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