Class GenaiProviderManager

java.lang.Object
org.machanism.machai.ai.manager.GenaiProviderManager

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

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 Details

  • Constructor Details

    • GenaiProviderManager

      private GenaiProviderManager()
      Private constructor to prevent instantiation of this utility class.
  • Method Details

    • getProvider

      public static Genai getProvider(String chatModel, Configurator conf)
      Dynamically loads and initializes a Genai 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 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 Genai 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 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
    • resolveClassName

      private static String resolveClassName(String providerName)
      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 is org.machanism.machai.ai.provider.impl.%sProvider.
      Returns:
      the resolved class name
    • isLoadable

      private static boolean isLoadable(String className)
      Checks whether the specified class name can be loaded.
      Parameters:
      className - the fully qualified class name to check
      Returns:
      true if the class can be loaded, false otherwise