anthropic.php
2 years ago
core.php
2 years ago
factory.php
2 years ago
google.php
2 years ago
huggingface.php
2 years ago
openai.php
2 years ago
openrouter.php
2 years ago
factory.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Engines_Factory { |
| 4 | |
| 5 | private static function get_default_env_id( $core ) : ?string { |
| 6 | return $core->get_option( 'ai_default_env' ); |
| 7 | } |
| 8 | |
| 9 | private static function get_default_model( $core ) : ?string { |
| 10 | return $core->get_option( 'ai_default_model' ); |
| 11 | } |
| 12 | |
| 13 | private static function get_env_from_id( $core, $envId ) : ?array { |
| 14 | $envs = $core->get_option( 'ai_envs' ); |
| 15 | foreach ( $envs as $env ) { |
| 16 | if ( $env['id'] === $envId ) { |
| 17 | return $env; |
| 18 | } |
| 19 | } |
| 20 | throw new Exception( "AI Engine: No environment found for ID ($envId)." ); |
| 21 | } |
| 22 | |
| 23 | private static function get_env_from_type( $core, $type, $envId ) : ?array { |
| 24 | $type = is_array( $type ) ? $type : [ $type ]; |
| 25 | |
| 26 | // Try first to find the env with the ID provided. |
| 27 | if ( !empty( $envId ) ) { |
| 28 | $env = self::get_env_from_id( $core, $envId ); |
| 29 | if ( in_array( $env['type'], $type ) ) { |
| 30 | return $env; |
| 31 | } |
| 32 | else { |
| 33 | throw new Exception( "AI Engine: Environment ID ($envId) is not of type ($type)." ); |
| 34 | } |
| 35 | } |
| 36 | // If not, we will try to find the default one. |
| 37 | $envId = self::get_default_env_id( $core ); |
| 38 | $env = self::get_env_from_id( $core, $envId ); |
| 39 | if ( in_array( $env['type'], $type ) ) { |
| 40 | return $env; |
| 41 | } |
| 42 | // If not, we will try to find the first one. |
| 43 | $envs = $core->get_option( 'ai_envs' ); |
| 44 | foreach ( $envs as $env ) { |
| 45 | if ( in_array( $env['type'], $type ) ) { |
| 46 | return $env; |
| 47 | } |
| 48 | } |
| 49 | throw new Exception( "AI Engine: No environment found for type ($type)." ); |
| 50 | } |
| 51 | |
| 52 | public static function get( $core, $envId = null ) : ?Meow_MWAI_Engines_Core { |
| 53 | // If no envId is provided, we will use the default one as well as the default model. |
| 54 | $model = null; |
| 55 | if ( empty( $envId ) ) { |
| 56 | $envId = self::get_default_env_id( $core ); |
| 57 | //$model = self::get_default_model( $core ); |
| 58 | } |
| 59 | $env = self::get_env_from_id( $core, $envId ); |
| 60 | if ( $env['type'] === 'openai' || $env['type'] === 'azure' ) { |
| 61 | $engine = Meow_MWAI_Engines_OpenAI::create( $core, $env ); |
| 62 | return $engine; |
| 63 | } |
| 64 | else if ( $env['type'] === 'google' ) { |
| 65 | $engine = new Meow_MWAI_Engines_Google( $core, $env ); |
| 66 | return $engine; |
| 67 | } |
| 68 | else if ( $env['type'] === 'anthropic' ) { |
| 69 | $engine = new Meow_MWAI_Engines_Anthropic( $core, $env ); |
| 70 | return $engine; |
| 71 | } |
| 72 | else if ( $env['type'] === 'openrouter' ) { |
| 73 | $engine = new Meow_MWAI_Engines_OpenRouter( $core, $env ); |
| 74 | return $engine; |
| 75 | } |
| 76 | else if ( $env['type'] === 'huggingface' ) { |
| 77 | $engine = new Meow_MWAI_Engines_HuggingFace( $core, $env ); |
| 78 | return $engine; |
| 79 | } |
| 80 | throw new Exception( "AI Engine: Unknown engine type ({$env['type']})." ); |
| 81 | } |
| 82 | |
| 83 | public static function get_openai( $core, $envId = null ) : Meow_MWAI_Engines_OpenAI { |
| 84 | $env = self::get_env_from_type( $core, [ 'openai', 'azure '], $envId ); |
| 85 | $engine = Meow_MWAI_Engines_OpenAI::create( $core, $env ); |
| 86 | return $engine; |
| 87 | } |
| 88 | |
| 89 | } |
| 90 |