assistant.php
2 years ago
base.php
2 years ago
embed.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
122 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Text extends Meow_MWAI_Query_Base implements JsonSerializable { |
| 4 | |
| 5 | // Core Content |
| 6 | public ?string $imageUrl = null; |
| 7 | public ?string $imageData = null; |
| 8 | |
| 9 | // Parameters |
| 10 | public float $temperature = 0.8; |
| 11 | public int $maxTokens = 1024; |
| 12 | public ?string $stop = null; |
| 13 | public ?string $responseFormat = null; |
| 14 | |
| 15 | public function __construct( ?string $message = '', ?int $maxTokens = null, string $model = null ) { |
| 16 | parent::__construct( $message ); |
| 17 | if ( !empty( $model ) ) { |
| 18 | $this->set_model( $model ); |
| 19 | } |
| 20 | if ( !empty( $maxTokens ) ) { |
| 21 | $this->set_max_tokens( $maxTokens ); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | #[\ReturnTypeWillChange] |
| 26 | public function jsonSerialize() { |
| 27 | return [ |
| 28 | 'class' => get_class( $this ), |
| 29 | 'message' => $this->message, |
| 30 | 'context' => $this->context, |
| 31 | 'messages' => $this->messages, |
| 32 | 'imageUrl' => $this->imageUrl, |
| 33 | 'mode' => $this->mode, |
| 34 | 'model' => $this->model, |
| 35 | 'maxTokens' => $this->maxTokens, |
| 36 | 'temperature' => $this->temperature, |
| 37 | 'maxMessages' => $this->maxMessages, |
| 38 | 'session' => $this->session, |
| 39 | 'scope' => $this->scope, |
| 40 | 'envId' => $this->envId, |
| 41 | 'stop' => $this->stop |
| 42 | ]; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * The type of return expected from the API. It can be either null or "json". |
| 47 | * @param int $maxResults The maximum number of completions. |
| 48 | */ |
| 49 | public function set_response_format( $responseFormat ) { |
| 50 | if ( !empty( $responseFormat ) && $responseFormat !== 'json' ) { |
| 51 | throw new Exception( "AI Engine: The response format can only be null or json." ); |
| 52 | } |
| 53 | $this->responseFormat = $responseFormat; |
| 54 | } |
| 55 | |
| 56 | public function set_image( string $imageUrl ): void { |
| 57 | $this->imageUrl = $imageUrl; |
| 58 | } |
| 59 | |
| 60 | public function set_image_data( string $imageData ): void { |
| 61 | $this->imageData = $imageData; |
| 62 | } |
| 63 | |
| 64 | public function get_image_url() { |
| 65 | if ( !empty( $this->imageUrl ) ) { |
| 66 | return $this->imageUrl; |
| 67 | } |
| 68 | if ( !empty( $this->imageData ) ) { |
| 69 | return "data:image/jpeg;base64,{$this->imageData}"; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * The maximum number of tokens to generate in the completion. |
| 75 | * The token count of your prompt plus max_tokens cannot exceed the model's context length. |
| 76 | * Most models have a context length of 2048 tokens (except for the newest models, which support 4096). |
| 77 | * @param float $maxTokens The maximum number of tokens. |
| 78 | */ |
| 79 | public function set_max_tokens( int $maxTokens ): void { |
| 80 | $this->maxTokens = $maxTokens; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set the sampling temperature to use. Higher values means the model will take more risks. |
| 85 | * Try 0.9 for more creative applications, and 0 for ones with a well-defined reply. |
| 86 | * @param float $temperature The temperature. |
| 87 | */ |
| 88 | public function set_temperature( float $temperature ): void { |
| 89 | $temperature = floatval( $temperature ); |
| 90 | if ( $temperature > 1 ) { |
| 91 | $temperature = 1; |
| 92 | } |
| 93 | if ( $temperature < 0 ) { |
| 94 | $temperature = 0; |
| 95 | } |
| 96 | $this->temperature = round( $temperature, 2 ); |
| 97 | } |
| 98 | |
| 99 | public function set_stop( string $stop ): void { |
| 100 | $this->stop = $stop; |
| 101 | } |
| 102 | |
| 103 | // Based on the params of the query, update the attributes |
| 104 | public function inject_params( array $params ): void |
| 105 | { |
| 106 | parent::inject_params( $params ); |
| 107 | $params = $this->convert_keys( $params ); |
| 108 | |
| 109 | if ( !empty( $params['maxTokens'] ) && intval( $params['maxTokens'] ) > 0 ) { |
| 110 | $this->set_max_tokens( intval( $params['maxTokens'] ) ); |
| 111 | } |
| 112 | if ( !empty( $params['temperature'] ) ) { |
| 113 | $this->set_temperature( $params['temperature'] ); |
| 114 | } |
| 115 | if ( !empty( $params['stop'] ) ) { |
| 116 | $this->set_stop( $params['stop'] ); |
| 117 | } |
| 118 | if ( !empty( $params['responseFormat'] ) ) { |
| 119 | $this->set_response_format( $params['responseFormat'] ); |
| 120 | } |
| 121 | } |
| 122 | } |