Class GeminiAPI
java.lang.Object
uno.anahata.ai.gemini.GeminiAPI
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
FieldsModifier and TypeFieldDescriptionstatic final StringDefault model ID used if no other model is selected. -
Constructor Summary
ConstructorsConstructorDescriptionGeminiAPI(ChatConfig config) Constructs a new GeminiAPI instance and loads API keys from the configuration. -
Method Summary
Modifier and TypeMethodDescriptionGets 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.ClientGets a newClientinstance 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.ModelgetModelMetadata(String modelId) Retrieves metadata for a specific model, using the cache if available.booleanChecks if any API keys were successfully loaded.voidreload()Reloads the API keys from disk and clears the model metadata cache.voidsetModelId(String modelId) Sets the model ID to be used for subsequent requests.
-
Field Details
-
DEFAULT_MODEL_ID
Default model ID used if no other model is selected.- See Also:
-
-
Constructor Details
-
GeminiAPI
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 newClientinstance 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
Gets the currently selected model ID.- Returns:
- The model ID (e.g., "gemini-3-flash-preview").
-
setModelId
Sets the model ID to be used for subsequent requests.- Parameters:
modelId- The new model ID.
-
getAvailableModelIds
-
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
Modelobjects.
-
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
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.
-
