Class GenaiProviderManager

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

public class GenaiProviderManager extends Object
Factory/registry for resolving Genai implementations from a provider/model identifier and aggregating per-run Usage metrics.

The model identifier is formatted as Provider:Model (for example, OpenAI:gpt-4o-mini). If the provider portion is omitted (for example, gpt-4o-mini), the default provider is used.

Providers are instantiated via reflection. A provider can be referenced either by:

  • a short provider name (mapped to org.machanism.machai.ai.provider.<provider>.<Provider>Provider), or
  • a fully-qualified class name.

Usage


 Configurator conf = ...;
 Genai provider = GenaiProviderManager.getProvider("OpenAI:gpt-4o-mini", conf);
 provider.prompt("Hello!");
 String response = provider.perform();

 GenaiProviderManager.addUsage(provider.usage());
 GenaiProviderManager.logUsage();
 

Thread-safety: Usage aggregation is backed by a static ArrayList and is not synchronized.

Author:
Viktor Tovstyi
See Also:
  • Method Details

    • getProvider

      public static Genai getProvider(String chatModel, Configurator conf)
      Creates a provider instance for the given provider/model identifier and applies the selected model.
      Parameters:
      chatModel - the model identifier formatted as Provider:Model or just Model
      conf - configurator used to initialize the provider
      Returns:
      a new provider instance configured with the requested model
      Throws:
      IllegalArgumentException - if the provider cannot be resolved or instantiated
    • addUsage

      public static void addUsage(Usage usage)
      Adds a single Usage record to the in-memory aggregation list.
      Parameters:
      usage - usage to add
    • logUsage

      public static void logUsage()
      Logs a summary of the aggregated Usage records.