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.{provider}.{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
  • 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