Class ProcessProviderManager

java.lang.Object
org.machanism.machai.process.manager.ProcessProviderManager

public class ProcessProviderManager extends Object
Utility class for dynamically loading and initializing generative AI providers and embedding providers.

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 Details

    • PROCESS_PROVIDER_CLASS_NAME_PATTERNS

      private static final String[] 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

      public static ProcessProvider getProvider(String chatModel, Configurator conf)
      Dynamically loads and initializes a ProcessProvider provider 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 ProcessProvider provider instance, or null if the provider name is blank
      Throws:
      IllegalArgumentException - if the provider name is invalid, or if the provider cannot be found or instantiated
    • getEmbeddingProvider

      public static EmbeddingProvider getEmbeddingProvider(String embeddingModel, Configurator conf)
      Dynamically loads and initializes an EmbeddingProvider 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, 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 EmbeddingProvider instance, or null if the provider name is blank
      Throws:
      IllegalArgumentException - if the provider cannot be found, does not implement EmbeddingProvider, 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 using org.machanism.machai.genai.provider.impl.%sProvider.
      Returns:
      the resolved class name
      Throws:
      ClassNotFoundException
    • resolveClassName

      private static String resolveClassName(String providerName)
      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