Class GenaiProviderManager
The GenaiProviderManager offers static methods to instantiate
Genai and EmbeddingProvider implementations based on a
provider/model string, using Java reflection. Providers are expected to
follow a conventional package and class naming pattern, or may be specified
by fully qualified class name.
Provider Naming Convention
- Provider and model are specified as
Provider:Model(e.g.,OpenAI:gpt-4). - If the provider name contains a dot (
.), it is treated as a fully qualified class name. - Otherwise, the provider is resolved using the pattern
org.machanism.machai.ai.provider.impl.{provider}Provider.
Usage Example
Configurator conf = ...;
Genai provider = GenaiProviderManager.getProvider("OpenAI:gpt-4", conf);
EmbeddingProvider embeddingProvider = GenaiProviderManager.getEmbeddingProvider("OpenAI:embedding-model", conf);
If the provider cannot be found or instantiated, an
IllegalArgumentException is thrown.
- Author:
- Viktor Tovstyi
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation of this utility class. -
Method Summary
Modifier and TypeMethodDescriptionstatic EmbeddingProvidergetEmbeddingProvider(String embeddingModel, Configurator conf) Dynamically loads and initializes anEmbeddingProviderbased on the specified provider/model string.static GenaigetProvider(String chatModel, Configurator conf) Dynamically loads and initializes aGenaiprovider based on the specified provider/model string.private static booleanisLoadable(String className) Checks whether the specified class name can be loaded.private static StringresolveClassName(String providerName) Resolves the provider class name based on the provider name and a conventional naming pattern.
-
Field Details
-
AI_CLIENT_CLASS_NAME_PATTERN
- See Also:
-
-
Constructor Details
-
GenaiProviderManager
private GenaiProviderManager()Private constructor to prevent instantiation of this utility class.
-
-
Method Details
-
getProvider
Dynamically loads and initializes aGenaiprovider based on the specified provider/model string.The provider name and model are parsed from the input string (format:
Provider:Model). The provider class is resolved using a conventional naming pattern or as a fully qualified class name. The provider is instantiated and initialized with the specified model and configuration.- Parameters:
chatModel- the provider/model string (e.g.,OpenAI:gpt-4)conf- the configuration object for provider initialization- Returns:
- the initialized
Genaiprovider instance, ornullif the provider name is blank - Throws:
IllegalArgumentException- if the provider name is invalid, or if the provider cannot be found or instantiated
-
getEmbeddingProvider
Dynamically loads and initializes anEmbeddingProviderbased on the specified provider/model string.The provider name and model are parsed from the input string (format:
Provider:Model). The provider class is resolved using a conventional naming pattern or as a fully qualified class name. The provider is instantiated and initialized with the specified model and configuration.- Parameters:
embeddingModel- the provider/model string (e.g.,OpenAI:embedding-model)conf- the configuration object for provider initialization- Returns:
- the initialized
EmbeddingProviderinstance, ornullif the provider name is blank - Throws:
IllegalArgumentException- if the provider cannot be found, does not implementEmbeddingProvider, or cannot be instantiated
-
resolveClassName
Resolves the provider class name based on the provider name and a conventional naming pattern.If the provider name contains a dot (
.), it is treated as a fully qualified class name. Otherwise, the provider is resolved using the specified pattern. If the class is not loadable, a fallback naming convention is used.- Parameters:
providerName- the provider name (e.g.,OpenAI) The conventional naming pattern isorg.machanism.machai.ai.provider.impl.%sProvider.- Returns:
- the resolved class name
-
isLoadable
Checks whether the specified class name can be loaded.- Parameters:
className- the fully qualified class name to check- Returns:
trueif the class can be loaded,falseotherwise
-