pub struct AIClientFactory;Expand description
AI client factory for creating provider instances.
The AIClientFactory provides a centralized way to create AI provider
instances based on configuration. It supports multiple AI service providers
and handles the complexity of client initialization and configuration.
§Design Pattern
This factory follows the Abstract Factory pattern, providing a single interface for creating different types of AI clients while hiding the specific implementation details from the consumer.
§Provider Selection
The factory automatically selects the appropriate provider implementation
based on the provider field in the configuration:
"openai"- Creates an OpenAI client instance"anthropic"- Creates an Anthropic client (future support)"google"- Creates a Google AI client (future support)"local"- Creates a local model client (future support)
§Error Handling
The factory returns detailed errors for:
- Unsupported provider names
- Invalid configuration parameters
- Missing required credentials
- Network connectivity issues during client creation
§Examples
use subx_cli::services::ai::AIClientFactory;
use subx_cli::config::AIConfig;
// Create a configured OpenAI client
let config = AIConfig {
provider: "openai".to_string(),
api_key: Some("sk-your-api-key".to_string()),
model: "gpt-4.1".to_string(),
base_url: "https://api.openai.com/v1".to_string(),
max_sample_length: 2000,
temperature: 0.3,
retry_attempts: 3,
retry_delay_ms: 1000,
};
let client = AIClientFactory::create_client(&config)?;
// Client is ready for content analysisImplementations§
Source§impl AIClientFactory
impl AIClientFactory
Sourcepub fn create_client(config: &AIConfig) -> Result<Box<dyn AIProvider>>
pub fn create_client(config: &AIConfig) -> Result<Box<dyn AIProvider>>
Creates an AI provider instance based on the provided configuration.
This method examines the provider field in the configuration and
instantiates the appropriate AI client implementation. The returned
client is fully configured and ready for use.
§Arguments
config- AI configuration containing provider details and credentials
§Returns
Returns a boxed trait object implementing AIProvider that can be used
for content analysis and subtitle matching operations.
§Errors
This method returns an error if:
- The specified provider is not supported
- Required configuration fields are missing or invalid
- API credentials are invalid or missing
- Network connectivity issues prevent client initialization
§Examples
use subx_cli::services::ai::AIClientFactory;
use subx_cli::config::AIConfig;
let config = AIConfig {
provider: "openai".to_string(),
api_key: Some("sk-key".to_string()),
model: "gpt-4.1".to_string(),
// ... other configuration
};
match AIClientFactory::create_client(&config) {
Ok(client) => {
// Use client for analysis
let result = client.analyze_content(request).await?;
}
Err(e) => {
eprintln!("Failed to create AI client: {}", e);
}
}§Supported Providers
Current supported providers:
"openai"- OpenAI GPT models with chat completion API