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
assistant.php
132 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Assistant 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 $filePurpose = null; // assistant, vision |
| 9 | |
| 10 | // Parameters |
| 11 | public ?string $chatId = null; |
| 12 | public ?string $assistantId = null; |
| 13 | public ?string $threadId = null; |
| 14 | |
| 15 | #region Constructors, Serialization |
| 16 | |
| 17 | public function __construct( ?string $message = '' ) { |
| 18 | parent::__construct( $message ); |
| 19 | $this->mode = "assistant"; |
| 20 | } |
| 21 | |
| 22 | #[\ReturnTypeWillChange] |
| 23 | public function jsonSerialize() { |
| 24 | return [ |
| 25 | 'message' => $this->message, |
| 26 | |
| 27 | 'ai' => [ |
| 28 | 'model' => $this->model, |
| 29 | 'assistantId' => $this->assistantId, |
| 30 | 'threadId' => $this->threadId, |
| 31 | ], |
| 32 | |
| 33 | 'context' => [ |
| 34 | ], |
| 35 | |
| 36 | 'system' => [ |
| 37 | 'class' => get_class( $this ), |
| 38 | 'envId' => $this->envId, |
| 39 | 'mode' => $this->mode, |
| 40 | 'scope' => $this->scope, |
| 41 | 'session' => $this->session, |
| 42 | 'chatId' => $this->chatId, |
| 43 | ] |
| 44 | ]; |
| 45 | |
| 46 | if ( !empty( $this->context ) ) { |
| 47 | $json['context']['context'] = $this->context; |
| 48 | } |
| 49 | |
| 50 | if ( !empty( $this->file ) ) { |
| 51 | $json['context']['hasFile'] = true; |
| 52 | if ( $this->fileType === 'url' ) { |
| 53 | $json['context']['fileUrl'] = $this->file; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return $json; |
| 58 | } |
| 59 | |
| 60 | #endregion |
| 61 | |
| 62 | #region File Handling |
| 63 | |
| 64 | public function set_file( string $file, string $fileType = null, string $filePurpose = null ): void { |
| 65 | if ( !empty( $fileType ) && $fileType !== 'refId' && $fileType !== 'url' && $fileType !== 'data' ) { |
| 66 | throw new Exception( "AI Engine: The file type can only be refId, url or data." ); |
| 67 | } |
| 68 | if ( !empty( $filePurpose ) && $filePurpose !== 'assistant-in' && $filePurpose !== 'vision' ) { |
| 69 | throw new Exception( "AI Engine: The file purpose can only be assistant or vision." ); |
| 70 | } |
| 71 | $this->file = $file; |
| 72 | $this->fileType = $fileType; |
| 73 | $this->filePurpose = $filePurpose; |
| 74 | } |
| 75 | |
| 76 | public function get_file_url() { |
| 77 | if ( $this->fileType === 'url' ) { |
| 78 | return $this->file; |
| 79 | } |
| 80 | else if ( $this->fileType === 'data' ) { |
| 81 | return "data:image/jpeg;base64,{$this->file}"; |
| 82 | } |
| 83 | else if ( $this->fileType === 'refId' ) { |
| 84 | throw new Exception( "AI Engine: The file type refId is not supported yet." ); |
| 85 | } |
| 86 | else { |
| 87 | return null; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | #endregion |
| 92 | |
| 93 | #region Parameters |
| 94 | |
| 95 | public function setAssistantId( string $assistantId ): void { |
| 96 | $this->assistantId = $assistantId; |
| 97 | } |
| 98 | |
| 99 | public function setChatId( string $chatId ): void { |
| 100 | $this->chatId = $chatId; |
| 101 | } |
| 102 | |
| 103 | public function setThreadId( string $threadId ): void { |
| 104 | $this->threadId = $threadId; |
| 105 | } |
| 106 | |
| 107 | #endregion |
| 108 | |
| 109 | #region Inject Params |
| 110 | |
| 111 | // Based on the params of the query, update the attributes |
| 112 | public function inject_params( array $params ): void |
| 113 | { |
| 114 | parent::inject_params( $params ); |
| 115 | |
| 116 | // Those are for the keys passed directly by the shortcode. |
| 117 | $params = $this->convert_keys( $params ); |
| 118 | |
| 119 | // Additional for Assistant. |
| 120 | if ( !empty( $params['chatId'] ) ) { |
| 121 | $this->setChatId( $params['chatId'] ); |
| 122 | } |
| 123 | if ( !empty( $params['assistantId'] ) ) { |
| 124 | $this->setAssistantId( $params['assistantId'] ); |
| 125 | } |
| 126 | if ( !empty( $params['threadId'] ) ) { |
| 127 | $this->setThreadId( $params['threadId'] ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | #endregion |
| 132 | } |