image.php
7 months ago
message-builder.php
3 weeks ago
model-environment.php
7 months ago
response-id-manager.php
3 months ago
session.php
11 months ago
usage-stats.php
1 month ago
model-environment.php
182 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Services_ModelEnvironment { |
| 4 | private $core; |
| 5 | |
| 6 | public function __construct( $core ) { |
| 7 | $this->core = $core; |
| 8 | } |
| 9 | |
| 10 | public function validate_env_model( $query ) { |
| 11 | if ( !$query || !is_object( $query ) ) { |
| 12 | throw new Exception( 'Invalid query object provided to validate_env_model.' ); |
| 13 | } |
| 14 | |
| 15 | // The query object uses envId (string ID) for the environment |
| 16 | $env = $query->envId ?? null; |
| 17 | $model = $query->model; |
| 18 | |
| 19 | // For assistant queries with a valid envId already set, respect it |
| 20 | if ( $query instanceof Meow_MWAI_Query_Assistant && !empty( $env ) && !empty( $query->assistantId ) ) { |
| 21 | // Set model to 'n/a' for assistants since they don't need a model |
| 22 | if ( empty( $model ) ) { |
| 23 | $query->model = 'n/a'; |
| 24 | } |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | if ( empty( $env ) && empty( $model ) ) { |
| 29 | // Use specialized defaults based on query type |
| 30 | if ( $query instanceof Meow_MWAI_Query_Image ) { |
| 31 | $this->set_default_env_and_model( $query, 'ai_images_default_env', 'ai_images_default_model' ); |
| 32 | } |
| 33 | else if ( $query instanceof Meow_MWAI_Query_Transcribe ) { |
| 34 | $this->set_default_env_and_model( $query, 'ai_audio_default_env', 'ai_audio_default_model' ); |
| 35 | } |
| 36 | else if ( $query instanceof Meow_MWAI_Query_Embed ) { |
| 37 | $this->set_default_env_and_model( $query, 'ai_embeddings_default_env', 'ai_embeddings_default_model' ); |
| 38 | } |
| 39 | else { |
| 40 | $this->set_default_env_and_model( $query, 'ai_default_env', 'ai_default_model' ); |
| 41 | } |
| 42 | } |
| 43 | else if ( empty( $env ) && !empty( $model ) ) { |
| 44 | // For embeddings queries, require a configured environment |
| 45 | if ( $query instanceof Meow_MWAI_Query_Embed ) { |
| 46 | throw new Exception( __( 'AI Engine: No embeddings environment is configured. Please go to Settings > Default Environments for AI > Embeddings and select an environment.', 'ai-engine' ) ); |
| 47 | } |
| 48 | |
| 49 | // If the model is available in the list of models, we can use it |
| 50 | $envs = $this->core->get_option( 'ai_envs' ); |
| 51 | $models = $this->core->get_option( 'ai_models' ); |
| 52 | |
| 53 | // First check custom models |
| 54 | if ( !empty( $models ) ) { |
| 55 | foreach ( $models as $currentModel ) { |
| 56 | if ( $currentModel['model'] === $model && isset( $currentModel['envId'] ) ) { |
| 57 | $query->envId = $currentModel['envId']; |
| 58 | // Note: Don't set $query->env here as it expects an object, not a string |
| 59 | $query->model = $currentModel['model']; |
| 60 | return; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Then check models in environments |
| 66 | foreach ( $envs as $envId => $env ) { |
| 67 | if ( isset( $env['models'] ) ) { |
| 68 | foreach ( $env['models'] as $envModel ) { |
| 69 | if ( $envModel['model'] === $model ) { |
| 70 | $query->envId = $envId; |
| 71 | // Note: Don't set $query->env here as it expects an object, not a string |
| 72 | $query->model = $model; |
| 73 | return; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | throw new Exception( 'The environment is required.' ); |
| 80 | } |
| 81 | else if ( !empty( $env ) && empty( $model ) ) { |
| 82 | // EnvId is set but model is empty - get first model from selected environment |
| 83 | $envData = $this->get_ai_env( $env ); |
| 84 | if ( !empty( $envData['models'] ) && is_array( $envData['models'] ) ) { |
| 85 | $firstModel = reset( $envData['models'] ); |
| 86 | if ( !empty( $firstModel['model'] ) ) { |
| 87 | $query->model = $firstModel['model']; |
| 88 | return; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Fallback: if environment has no models, try type-specific defaults |
| 93 | if ( $query instanceof Meow_MWAI_Query_Image ) { |
| 94 | $this->set_default_model_only( $query, 'ai_images_default_model' ); |
| 95 | } |
| 96 | else if ( $query instanceof Meow_MWAI_Query_Transcribe ) { |
| 97 | $this->set_default_model_only( $query, 'ai_audio_default_model' ); |
| 98 | } |
| 99 | else if ( $query instanceof Meow_MWAI_Query_Embed ) { |
| 100 | $this->set_default_model_only( $query, 'ai_embeddings_default_model' ); |
| 101 | } |
| 102 | else { |
| 103 | $this->set_default_model_only( $query, 'ai_default_model' ); |
| 104 | } |
| 105 | } |
| 106 | else { |
| 107 | // We have both, let's continue |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private function set_default_env_and_model( $query, $envOption, $modelOption ) { |
| 112 | $env = $this->core->get_option( $envOption ); |
| 113 | $model = $this->core->get_option( $modelOption ); |
| 114 | if ( !empty( $env ) ) { |
| 115 | // Use envId property which is what the query object uses |
| 116 | $query->envId = $env; |
| 117 | // Note: Don't set $query->env here as it expects an object, not a string |
| 118 | } |
| 119 | if ( !empty( $model ) ) { |
| 120 | $query->model = $model; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private function set_default_model_only( $query, $modelOption ) { |
| 125 | // Only set the model, preserve existing envId |
| 126 | $model = $this->core->get_option( $modelOption ); |
| 127 | if ( !empty( $model ) ) { |
| 128 | $query->model = $model; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public function get_embeddings_env( $envId = null ) { |
| 133 | // Use provided envId or fall back to default |
| 134 | if ( empty( $envId ) ) { |
| 135 | $envId = $this->core->get_option( 'embeddings_default_env' ); |
| 136 | } |
| 137 | |
| 138 | // Get embeddings environments (not AI environments) |
| 139 | $envs = $this->core->get_option( 'embeddings_envs' ); |
| 140 | if ( !empty( $envs ) ) { |
| 141 | foreach ( $envs as $env ) { |
| 142 | if ( isset( $env['id'] ) && $env['id'] === $envId ) { |
| 143 | return $env; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | public function get_ai_env( $envId ) { |
| 152 | $envs = $this->core->get_option( 'ai_envs' ); |
| 153 | if ( !empty( $envs ) ) { |
| 154 | foreach ( $envs as $env ) { |
| 155 | if ( isset( $env['id'] ) && $env['id'] === $envId ) { |
| 156 | return $env; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | return null; |
| 161 | } |
| 162 | |
| 163 | public function get_assistant( $envId, $assistantId ) { |
| 164 | $env = $this->get_ai_env( $envId ); |
| 165 | if ( isset( $env['assistants'] ) ) { |
| 166 | foreach ( $env['assistants'] as $assistant ) { |
| 167 | if ( $assistant['id'] === $assistantId ) { |
| 168 | return $assistant; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | public function get_engine_models( $query ) { |
| 176 | $envId = $query->envId; |
| 177 | $env = $this->get_ai_env( $envId ); |
| 178 | $models = apply_filters( 'mwai_engine_models', [], $env, $query ); |
| 179 | return $models; |
| 180 | } |
| 181 | } |
| 182 |