Class ProcessProviderManager
The ProcessProviderManager offers static methods to instantiate
ProcessProvider and EmbeddingProvider implementations based
on a provider/model string, using Java reflection. Chat providers must use
the conventional provider identifier, while embedding providers may
alternatively use a fully qualified implementation class name.
Provider Naming Convention
- Provider and model are specified as
Provider:Model(e.g.,OpenAI:gpt-4). - Chat-provider identifiers must be valid Java identifiers.
Embedding-provider identifiers containing a dot (
.) are treated as fully qualified class names. - Other provider identifiers are resolved using the pattern
org.machanism.machai.genai.provider.impl.{provider}Provider.
Usage Example
Configurator conf = ...;
ProcessProvider provider = ProcessProviderManager.getProvider("OpenAI:gpt-4", conf);
EmbeddingProvider embeddingProvider = ProcessProviderManager.getEmbeddingProvider("OpenAI:embedding-model", conf);
If the provider cannot be found or instantiated, an
IllegalArgumentException is thrown.
- Author:
- Viktor Tovstyi
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String[]Format used to derive the conventional fully qualified provider class name from a provider identifier. -
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 ProcessProvidergetProvider(String chatModel, Configurator conf) Dynamically loads and initializes aProcessProviderprovider based on the specified provider/model string.private static Class<? extends ProcessProvider> resolveClass(String providerName) Resolves the provider class name based on the provider name and a conventional naming pattern.private static StringresolveClassName(String providerName) Resolves the textual class name for a provider.
-
Field Details
-
PROCESS_PROVIDER_CLASS_NAME_PATTERNS
Format used to derive the conventional fully qualified provider class name from a provider identifier.
-
-
Constructor Details
-
ProcessProviderManager
private ProcessProviderManager()Private constructor to prevent instantiation of this utility class.
-
-
Method Details
-
getProvider
Dynamically loads and initializes aProcessProviderprovider 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. 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
ProcessProviderprovider 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, when the provider segment contains a dot, 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
-
resolveClass
private static Class<? extends ProcessProvider> resolveClass(String providerName) throws ClassNotFoundException 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 identifier (for example,OpenAI) or fully qualified embedding-provider class name. A simple identifier resolves usingorg.machanism.machai.genai.provider.impl.%sProvider.- Returns:
- the resolved class name
- Throws:
ClassNotFoundException
-
resolveClassName
Resolves the textual class name for a provider. Fully qualified names are returned unchanged; simple names are checked against the supported conventions. The nested fallback is retained for callers that use this resolver to inspect a potential provider class without loading it.- Parameters:
providerName- provider identifier or fully qualified class name- Returns:
- the resolved class name
-