get( 'adminAssistant.provider', '' ) ) { return 'aisentic'; } $model = (string) $settings->get( 'adminAssistant.model', 'gemini-2.5-flash' ); return 0 === stripos( $model, 'gemini' ) ? 'gemini' : 'openai'; } /** * True when the selected provider has the key it needs. * * @param Settings $settings Settings. * @return bool */ public static function has_credentials( Settings $settings ): bool { $key_paths = [ 'aisentic' => 'llmProviders.aisentic.apiKey', 'gemini' => 'llmProviders.google.apiKey', 'openai' => 'llmProviders.openai.apiKey', ]; return '' !== (string) $settings->get( $key_paths[ self::selected_kind( $settings ) ], '' ); } /** * Build the selected provider. * * @param Settings $settings Settings. * @return AIProviderInterface * @throws \Exception When the selected provider has no key. */ public static function make( Settings $settings ): AIProviderInterface { // Aisentic AI Provider (highest priority) if ( 'aisentic' === $settings->get( 'adminAssistant.provider', '' ) ) { $endpoint = (string) $settings->get( 'llmProviders.aisentic.endpoint', AISENTIC_AI_PROVIDER ); $api_key = (string) $settings->get( 'llmProviders.aisentic.apiKey', '' ); if ( empty( $endpoint ) || empty( $api_key ) ) { throw new \Exception( 'Aisentic AI Provider endpoint and API key must be configured.' ); } return new Aisentic_Provider( $endpoint, $api_key ); } // Get model from the settings, default to gemini-2.5-flash $model = (string) $settings->get( 'adminAssistant.model', 'gemini-2.5-flash' ); // Get max tokens from the settings, default to 2048 $max_tokens = (int) $settings->get( 'adminAssistant.maxTokens', 2048 ); // Detect provider based on model . If model starts with 'gemini', use Google provider, and check if google key is configured if ( stripos( $model, 'gemini' ) === 0 ) { $key = (string) $settings->get( 'llmProviders.google.apiKey' ); if ( empty( $key ) ) { throw new \Exception( 'Google API key is not configured.' ); } return new \NeuronAI\Providers\Gemini\Gemini( $key, $model ); } // Default to OpenAI $key = (string) $settings->get( 'llmProviders.openai.apiKey' ); if ( empty( $key ) ) { throw new \Exception( 'OpenAI API key is not configured.' ); } // Get parameters for OpenAI provider $params = []; if ( $max_tokens > 0 ) { // GPT-5, GPT-4o, and o1/o3 series use max_completion_tokens instead of max_tokens $use_max_completion_tokens = ( stripos( $model, 'gpt-5' ) !== false || stripos( $model, 'gpt-4o' ) !== false || stripos( $model, 'o1-' ) !== false || stripos( $model, 'o3-' ) !== false ); if ( $use_max_completion_tokens ) { $params['max_completion_tokens'] = $max_tokens; } else { $params['max_tokens'] = $max_tokens; } } return new \NeuronAI\Providers\OpenAI\OpenAI( $key, $model, $params ); } }