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
143 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 | $json = [ |
| 28 | 'instructions' => $this->instructions, |
| 29 | 'message' => $this->message, |
| 30 | |
| 31 | 'context' => [ |
| 32 | 'messages' => $this->messages |
| 33 | ], |
| 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']['context'] = $this->context; |
| 53 | } |
| 54 | |
| 55 | if ( !empty( $this->imageUrl ) || !empty( $this->imageData ) ) { |
| 56 | $json['context']['hasImage'] = true; |
| 57 | } |
| 58 | |
| 59 | if ( !empty( $this->imageUrl ) ) { |
| 60 | $json['context']['imageUrl'] = $this->imageUrl; |
| 61 | } |
| 62 | |
| 63 | return $json; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * The type of return expected from the API. It can be either null or "json". |
| 68 | * @param int $maxResults The maximum number of completions. |
| 69 | */ |
| 70 | public function set_response_format( $responseFormat ) { |
| 71 | if ( !empty( $responseFormat ) && $responseFormat !== 'json' ) { |
| 72 | throw new Exception( "AI Engine: The response format can only be null or json." ); |
| 73 | } |
| 74 | $this->responseFormat = $responseFormat; |
| 75 | } |
| 76 | |
| 77 | public function set_image( string $imageUrl ): void { |
| 78 | $this->imageUrl = $imageUrl; |
| 79 | } |
| 80 | |
| 81 | public function set_image_data( string $imageData ): void { |
| 82 | $this->imageData = $imageData; |
| 83 | } |
| 84 | |
| 85 | public function get_image_url() { |
| 86 | if ( !empty( $this->imageUrl ) ) { |
| 87 | return $this->imageUrl; |
| 88 | } |
| 89 | if ( !empty( $this->imageData ) ) { |
| 90 | return "data:image/jpeg;base64,{$this->imageData}"; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * The maximum number of tokens to generate in the completion. |
| 96 | * The token count of your prompt plus max_tokens cannot exceed the model's context length. |
| 97 | * Most models have a context length of 2048 tokens (except for the newest models, which support 4096). |
| 98 | * @param float $maxTokens The maximum number of tokens. |
| 99 | */ |
| 100 | public function set_max_tokens( int $maxTokens ): void { |
| 101 | $this->maxTokens = $maxTokens; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Set the sampling temperature to use. Higher values means the model will take more risks. |
| 106 | * Try 0.9 for more creative applications, and 0 for ones with a well-defined reply. |
| 107 | * @param float $temperature The temperature. |
| 108 | */ |
| 109 | public function set_temperature( float $temperature ): void { |
| 110 | $temperature = floatval( $temperature ); |
| 111 | if ( $temperature > 1 ) { |
| 112 | $temperature = 1; |
| 113 | } |
| 114 | if ( $temperature < 0 ) { |
| 115 | $temperature = 0; |
| 116 | } |
| 117 | $this->temperature = round( $temperature, 2 ); |
| 118 | } |
| 119 | |
| 120 | public function set_stop( string $stop ): void { |
| 121 | $this->stop = $stop; |
| 122 | } |
| 123 | |
| 124 | // Based on the params of the query, update the attributes |
| 125 | public function inject_params( array $params ): void |
| 126 | { |
| 127 | parent::inject_params( $params ); |
| 128 | $params = $this->convert_keys( $params ); |
| 129 | |
| 130 | if ( !empty( $params['maxTokens'] ) && intval( $params['maxTokens'] ) > 0 ) { |
| 131 | $this->set_max_tokens( intval( $params['maxTokens'] ) ); |
| 132 | } |
| 133 | if ( !empty( $params['temperature'] ) ) { |
| 134 | $this->set_temperature( $params['temperature'] ); |
| 135 | } |
| 136 | if ( !empty( $params['stop'] ) ) { |
| 137 | $this->set_stop( $params['stop'] ); |
| 138 | } |
| 139 | if ( !empty( $params['responseFormat'] ) ) { |
| 140 | $this->set_response_format( $params['responseFormat'] ); |
| 141 | } |
| 142 | } |
| 143 | } |