assistant.php
2 years ago
assistfeedback.php
2 years ago
base.php
2 years ago
droppedfile.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
droppedfile.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_DroppedFile { |
| 4 | private $data; |
| 5 | private $rawData; |
| 6 | private $type; // Defines what the data is about ('refId', 'url', or 'data') |
| 7 | private $purpose; // Can be 'assistant' or 'vision' => this needs to be checked |
| 8 | private $mimeType; // 'image/jpeg' or any other mime type |
| 9 | |
| 10 | static function from_url( $url, $purpose, $mimeType = null ) { |
| 11 | if ( empty( $mimeType ) ) { |
| 12 | $mimeType = Meow_MWAI_Core::get_mime_type( $url ); |
| 13 | } |
| 14 | return new Meow_MWAI_Query_DroppedFile( $url, 'url', $purpose, $mimeType ); |
| 15 | } |
| 16 | |
| 17 | static function from_data( $data, $purpose, $mimeType = null ) { |
| 18 | return new Meow_MWAI_Query_DroppedFile( $data, 'data', $purpose, $mimeType ); |
| 19 | } |
| 20 | |
| 21 | static function from_path( $path, $purpose, $mimeType = null ) { |
| 22 | $data = file_get_contents( $path ); |
| 23 | if ( empty( $mimeType ) ) { |
| 24 | $mimeType = Meow_MWAI_Core::get_mime_type( $path ); |
| 25 | } |
| 26 | return new Meow_MWAI_Query_DroppedFile( $data, 'data', $purpose, $mimeType ); |
| 27 | } |
| 28 | |
| 29 | public function __construct( $data, $type, $purpose, $mimeType = null ) { |
| 30 | if ( !empty( $type ) && $type !== 'refId' && $type !== 'url' && $type !== 'data' ) { |
| 31 | throw new Exception( "AI Engine: The file type can only be refId, url or data." ); |
| 32 | } |
| 33 | if ( !empty( $purpose ) && $purpose !== 'assistant-in' && $purpose !== 'vision' ) { |
| 34 | throw new Exception( "AI Engine: The file purpose can only be assistant or vision." ); |
| 35 | } |
| 36 | $this->data = $data; |
| 37 | $this->type = $type; |
| 38 | $this->purpose = $purpose; |
| 39 | $this->mimeType = $mimeType; |
| 40 | } |
| 41 | |
| 42 | public function get_url() { |
| 43 | if ( $this->type === 'url' ) { |
| 44 | return $this->data; |
| 45 | } |
| 46 | throw new Exception( "AI Engine: The file is not an URL." ); |
| 47 | } |
| 48 | |
| 49 | private function get_raw_data() { |
| 50 | if ( !empty( $this->rawData ) ) { |
| 51 | return $this->rawData; |
| 52 | } |
| 53 | if ( $this->type === 'url' ) { |
| 54 | $this->rawData = file_get_contents( $this->data ); |
| 55 | return $this->rawData; |
| 56 | } |
| 57 | else if ( $this->type === 'data' ) { |
| 58 | return $this->data; |
| 59 | } |
| 60 | throw new Exception( "AI Engine: The file is not data or an URL." ); |
| 61 | } |
| 62 | |
| 63 | public function get_data() { |
| 64 | if ( $this->type === 'url' ) { |
| 65 | return $this->get_raw_data(); |
| 66 | } |
| 67 | else if ( $this->type === 'data' ) { |
| 68 | return $this->data; |
| 69 | } |
| 70 | throw new Exception( "AI Engine: The file is not data or an URL." ); |
| 71 | } |
| 72 | |
| 73 | public function get_base64() { |
| 74 | $data = $this->get_raw_data(); |
| 75 | return base64_encode( $data ); |
| 76 | } |
| 77 | |
| 78 | // Will return something like "data:image/jpeg;base64,{data}" |
| 79 | public function get_inline_base64_url() { |
| 80 | $b64 = $this->get_base64(); |
| 81 | return "data:{$this->mimeType};base64,{$b64}"; |
| 82 | } |
| 83 | |
| 84 | public function get_type() { |
| 85 | return $this->type; |
| 86 | } |
| 87 | |
| 88 | public function get_purpose() { |
| 89 | return $this->purpose; |
| 90 | } |
| 91 | |
| 92 | public function get_mimeType() { |
| 93 | return $this->mimeType; |
| 94 | } |
| 95 | } |
| 96 |