Class GeminiAPI

java.lang.Object
uno.anahata.ai.gemini.GeminiAPI

public class GeminiAPI extends Object
Manages the connection to the Gemini API, including API key rotation, model discovery, and metadata caching.

This class loads API keys from a configuration file and provides a round-robin mechanism for selecting a key for each request, helping to distribute load and stay within rate limits. It also dynamically fetches available models and their metadata from the Gemini API.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Default model ID used if no other model is selected.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new GeminiAPI instance and loads API keys from the configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets the list of all available model IDs by querying the Gemini API.
    List<com.google.genai.types.Model>
    Fetches the full list of available models and their metadata from the Gemini API.
    com.google.genai.Client
    Gets a new Client instance using the next available API key in the pool (round-robin).
    List<com.google.genai.types.Model>
    Returns a list of models that support the 'generateContent' action, filtered for relevance to the chat assistant.
    Gets the currently selected model ID.
    com.google.genai.types.Model
    Retrieves metadata for a specific model, using the cache if available.
    boolean
    Checks if any API keys were successfully loaded.
    void
    Reloads the API keys from disk and clears the model metadata cache.
    void
    setModelId(String modelId)
    Sets the model ID to be used for subsequent requests.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_MODEL_ID

      public static final String DEFAULT_MODEL_ID
      Default model ID used if no other model is selected.
      See Also:
  • Constructor Details

    • GeminiAPI

      public GeminiAPI(ChatConfig config)
      Constructs a new GeminiAPI instance and loads API keys from the configuration.
      Parameters:
      config - The chat configuration.
  • Method Details

    • isHasKeys

      public boolean isHasKeys()
      Checks if any API keys were successfully loaded.
      Returns:
      true if the key pool is not empty.
    • reload

      public void reload()
      Reloads the API keys from disk and clears the model metadata cache.
    • getClient

      public com.google.genai.Client getClient()
      Gets a new Client instance using the next available API key in the pool (round-robin).
      Returns:
      A configured Gemini Client.
      Throws:
      IllegalStateException - if no API keys are available.
    • getModelId

      public String getModelId()
      Gets the currently selected model ID.
      Returns:
      The model ID (e.g., "gemini-3-flash-preview").
    • setModelId

      public void setModelId(String modelId)
      Sets the model ID to be used for subsequent requests.
      Parameters:
      modelId - The new model ID.
    • getAvailableModelIds

      public List<String> getAvailableModelIds()
      Gets the list of all available model IDs by querying the Gemini API.
      Returns:
      A list of model names (e.g., ["models/gemini-3-flash", ...]).
    • getAvailableModels

      public List<com.google.genai.types.Model> getAvailableModels()
      Fetches the full list of available models and their metadata from the Gemini API. The results are cached to avoid redundant API calls.
      Returns:
      A list of Model objects.
    • getFilteredModels

      public List<com.google.genai.types.Model> getFilteredModels()
      Returns a list of models that support the 'generateContent' action, filtered for relevance to the chat assistant.
      Returns:
      A filtered list of Model objects.
    • getModelMetadata

      public com.google.genai.types.Model getModelMetadata(String modelId)
      Retrieves metadata for a specific model, using the cache if available.
      Parameters:
      modelId - The model ID (e.g., "gemini-3-flash-preview").
      Returns:
      The Model metadata object, or null if it could not be retrieved.