core.php
135 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Engines_Core { |
| 4 | protected $core = null; |
| 5 | public $env = null; |
| 6 | public $envId = null; |
| 7 | public $envType = null; |
| 8 | |
| 9 | public function __construct( $core, $env ) { |
| 10 | $this->core = $core; |
| 11 | $this->env = $env; |
| 12 | $this->envId = $env['id']; |
| 13 | $this->envType = $env['type']; |
| 14 | } |
| 15 | |
| 16 | public function run( $query, $streamCallback = null ) { |
| 17 | |
| 18 | // Check if the query is allowed. |
| 19 | $limits = $this->core->get_option( 'limits' ); |
| 20 | $allowed = apply_filters( 'mwai_ai_allowed', true, $query, $limits ); |
| 21 | if ( $allowed !== true ) { |
| 22 | $message = is_string( $allowed ) ? $allowed : 'Unauthorized query.'; |
| 23 | throw new Exception( $message ); |
| 24 | } |
| 25 | |
| 26 | // Allow to modify the query before it is sent. It should not be a Meow_MWAI_Query_Embed. |
| 27 | if ( !( $query instanceof Meow_MWAI_Query_Embed ) ) { |
| 28 | $query = apply_filters( 'mwai_ai_query', $query ); |
| 29 | } |
| 30 | |
| 31 | // Important as it makes sure everything is consolidated in the query and the engine. |
| 32 | $this->final_checks( $query ); |
| 33 | |
| 34 | // Run the query |
| 35 | $reply = null; |
| 36 | if ( $query instanceof Meow_MWAI_Query_Text ) { |
| 37 | $reply = $this->run_completion_query( $query, $streamCallback ); |
| 38 | } |
| 39 | else if ( $query instanceof Meow_MWAI_Query_Assistant ) { |
| 40 | $reply = null; |
| 41 | $reply = apply_filters( 'mwai_ai_query_assistant', $reply, $query ); |
| 42 | if ( $reply === null ) { |
| 43 | throw new Exception( 'Assistants are not supported in this version of AI Engine.' ); |
| 44 | } |
| 45 | } |
| 46 | else if ( $query instanceof Meow_MWAI_Query_Embed ) { |
| 47 | $reply = $this->run_embedding_query( $query ); |
| 48 | } |
| 49 | else if ( $query instanceof Meow_MWAI_Query_Image ) { |
| 50 | $reply = $this->run_images_query( $query ); |
| 51 | } |
| 52 | else if ( $query instanceof Meow_MWAI_Query_Transcribe ) { |
| 53 | $reply = $this->run_transcribe_query( $query ); |
| 54 | } |
| 55 | else { |
| 56 | throw new Exception( 'Unknown query type.' ); |
| 57 | } |
| 58 | |
| 59 | // Allow to modify the reply before it is sent. |
| 60 | $reply = apply_filters( 'mwai_ai_reply', $reply, $query ); |
| 61 | |
| 62 | return $reply; |
| 63 | } |
| 64 | |
| 65 | public function retrieve_model_info( $model ) { |
| 66 | $models = $this->get_models(); |
| 67 | foreach ( $models as $currentModel ) { |
| 68 | if ( $currentModel['model'] === $model ) { |
| 69 | return $currentModel; |
| 70 | } |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | public function final_checks( Meow_MWAI_Query_Base $query ) { |
| 76 | $query->final_checks(); |
| 77 | //$found = false; |
| 78 | |
| 79 | // Check if the model is available, except if it's an assistant |
| 80 | if ( !( $query instanceof Meow_MWAI_Query_Assistant ) ) { |
| 81 | // TODO: Avoid checking on the finetuned models for now. |
| 82 | if ( substr( $query->model, 0, 3 ) === 'ft:' ) { |
| 83 | return; |
| 84 | } |
| 85 | $model_info = $this->retrieve_model_info( $query->model ); |
| 86 | if ( $model_info === false ) { |
| 87 | throw new Exception( "AI Engine: The model '{$query->model}' is not available." ); |
| 88 | } |
| 89 | if ( isset( $model_info['mode'] ) ) { |
| 90 | $query->mode = $model_info['mode']; |
| 91 | } |
| 92 | |
| 93 | // TODO: I am not sure this is actually useful, and it breaks the mechanics of picking a model. |
| 94 | // if ( !$found && ( $query instanceof Meow_MWAI_Query_Embed ) ) { |
| 95 | // $ai_embeddings_default_env = $this->core->get_option( 'ai_embeddings_default_env' ); |
| 96 | // $ai_embeddings_default_model = $this->core->get_option( 'ai_embeddings_default_model' ); |
| 97 | // if ( empty( $ai_embeddings_default_env ) || empty( $ai_embeddings_default_model ) ) { |
| 98 | // throw new Exception( 'AI Engine: The default environment and model for embeddings are not set.' ); |
| 99 | // } |
| 100 | // $query->set_env_id( $ai_embeddings_default_env ); |
| 101 | // $query->set_model( $ai_embeddings_default_model ); |
| 102 | // $found = true; |
| 103 | // } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public function get_models() { |
| 108 | throw new Exception( 'Not implemented.' ); |
| 109 | } |
| 110 | |
| 111 | public function retrieve_models() { |
| 112 | throw new Exception( 'Not implemented.' ); |
| 113 | } |
| 114 | |
| 115 | public function run_completion_query( Meow_MWAI_Query_Base $query, $streamCallback = null ) : Meow_MWAI_Reply { |
| 116 | throw new Exception( 'Not implemented.' ); |
| 117 | } |
| 118 | |
| 119 | public function run_embedding_query( Meow_MWAI_Query_Base $query ) { |
| 120 | throw new Exception( 'Not implemented.' ); |
| 121 | } |
| 122 | |
| 123 | public function run_images_query( Meow_MWAI_Query_Base $query ) { |
| 124 | throw new Exception( 'Not implemented.' ); |
| 125 | } |
| 126 | |
| 127 | public function run_transcribe_query( Meow_MWAI_Query_Base $query ) { |
| 128 | throw new Exception( 'Not implemented.' ); |
| 129 | } |
| 130 | |
| 131 | public function get_price( Meow_MWAI_Query_Base $query, Meow_MWAI_Reply $reply ) { |
| 132 | throw new Exception( 'Not implemented.' ); |
| 133 | } |
| 134 | } |
| 135 |