Class AiProviderFactory
This class centralizes the logic for selecting and constructing concrete
AiProviderClient implementations based on the configuration provided
through AiOptions. It abstracts provider instantiation from the rest
of the application so that higher-level components interact only with the
AiProviderClient interface.
Provider Resolution
When an explicit provider is configured in AiOptions.provider(), the
factory constructs the corresponding client implementation. When
AiProvider.AUTO is selected, the factory attempts to determine a
suitable provider automatically using the following strategy:
- Attempt to use a locally running
OllamaClient. - If Ollama is not reachable and an API key is configured, fall back to an OpenAI-compatible provider.
- If no provider can be resolved, an
AiSuggestionExceptionis thrown.
Azure OpenAI
AiProvider.AZURE_OPENAI constructs an AzureOpenAiClient.
This provider is never selected automatically; it must be configured
explicitly. The AiOptions.baseUrl() must point to the Azure OpenAI
resource endpoint and AiOptions.modelName() must match the
deployment name as shown in the Azure portal.
The factory ensures that returned clients are usable by verifying provider availability when required.
This class is intentionally non-instantiable and exposes only static factory methods.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic AiProviderClientCreates a provider-specificAiProviderClientbased on the supplied configuration.static AiProviderClientcreate(AiOptions options, RateLimitListener rateLimitListener) Creates a provider-specificAiProviderClientbased on the supplied configuration, notifyingrateLimitListenerbefore each HTTP 429 pause.
-
Method Details
-
create
Creates a provider-specificAiProviderClientbased on the supplied configuration.Rate-limit events are silently discarded. Use
create(AiOptions, RateLimitListener)when the caller needs to be informed of HTTP 429 pauses.The selected provider determines which concrete implementation is instantiated and how availability checks are performed. When
AiProvider.AUTOis configured, the method delegates provider selection toauto(AiOptions, RateLimitListener).- Parameters:
options- AI configuration describing provider, model, endpoint, authentication, and runtime limits- Returns:
- initialized provider client ready to perform inference requests
- Throws:
AiSuggestionException- if the provider cannot be initialized, required authentication is missing, or no suitable provider can be resolved- See Also:
-
create
public static AiProviderClient create(AiOptions options, RateLimitListener rateLimitListener) throws AiSuggestionException Creates a provider-specificAiProviderClientbased on the supplied configuration, notifyingrateLimitListenerbefore each HTTP 429 pause.The selected provider determines which concrete implementation is instantiated and how availability checks are performed. When
AiProvider.AUTOis configured, the method delegates provider selection toauto(AiOptions, RateLimitListener).- Parameters:
options- AI configuration describing provider, model, endpoint, authentication, and runtime limitsrateLimitListener- callback invoked before each rate-limit sleep; must not benull- Returns:
- initialized provider client ready to perform inference requests
- Throws:
AiSuggestionException- if the provider cannot be initialized, required authentication is missing, or no suitable provider can be resolved- See Also:
-