core.php
60 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Engines_Core { |
| 4 | private $core = null; |
| 5 | private $openai = null; |
| 6 | |
| 7 | public function __construct( $core ) { |
| 8 | $this->core = $core; |
| 9 | $this->openai = new Meow_MWAI_Engines_OpenAI( $this->core ); |
| 10 | } |
| 11 | |
| 12 | public function run( $query, $streamCallback = null ) { |
| 13 | |
| 14 | // Check if the query is allowed. |
| 15 | $limits = $this->core->get_option( 'limits' ); |
| 16 | $allowed = apply_filters( 'mwai_ai_allowed', true, $query, $limits ); |
| 17 | if ( $allowed !== true ) { |
| 18 | $message = is_string( $allowed ) ? $allowed : 'Unauthorized query.'; |
| 19 | throw new Exception( $message ); |
| 20 | } |
| 21 | |
| 22 | // Allow to modify the query before it is sent. |
| 23 | $query = apply_filters( 'mwai_ai_query', $query ); |
| 24 | |
| 25 | // Important as it makes sure everything is consolidated in the query. |
| 26 | $query->finalChecks(); |
| 27 | |
| 28 | // Run the query |
| 29 | // Only OpenAI is handled for now, so we send all the queries there. |
| 30 | $reply = null; |
| 31 | if ( $query instanceof Meow_MWAI_Query_Text ) { |
| 32 | $reply = $this->openai->run_completion_query( $query, $streamCallback ); |
| 33 | } |
| 34 | else if ( $query instanceof Meow_MWAI_Query_Assistant ) { |
| 35 | $reply = null; |
| 36 | $reply = apply_filters( 'mwai_ai_query_assistant', $reply, $query ); |
| 37 | if ( $reply === null ) { |
| 38 | throw new Exception( 'Assistants are not supported in this version of AI Engine.' ); |
| 39 | } |
| 40 | } |
| 41 | else if ( $query instanceof Meow_MWAI_Query_Embed ) { |
| 42 | $reply = $this->openai->run_embedding_query( $query ); |
| 43 | } |
| 44 | else if ( $query instanceof Meow_MWAI_Query_Image ) { |
| 45 | $reply = $this->openai->run_images_query( $query ); |
| 46 | } |
| 47 | else if ( $query instanceof Meow_MWAI_Query_Transcribe ) { |
| 48 | $reply = $this->openai->run_transcribe_query( $query ); |
| 49 | } |
| 50 | else { |
| 51 | throw new Exception( 'Unknown query type.' ); |
| 52 | } |
| 53 | |
| 54 | // Allow to modify the reply before it is sent. |
| 55 | $reply = apply_filters( 'mwai_ai_reply', $reply, $query ); |
| 56 | |
| 57 | return $reply; |
| 58 | } |
| 59 | } |
| 60 |