Image Generation / Utils

Gemini Client

Wrapper that reads the API key from the environment and creates, only once, the Gemini SDK client used by the image generation module.

On this page

    Source: src/image_generation/utils/gemini_client.py. This page's original notebook was empty; the content was written from the code.

    #Overview

    The file has two dataclasses: GeminiConfig, which resolves the API key, and GeminiClient, which creates and keeps the genai.Client.

    #GeminiConfig

    The api_key_env field holds the name of the environment variable and defaults to GEMINI_API_KEY. The get_api_key() method reads that variable with os.getenv.

    Required variable. If the variable does not exist or is empty, get_api_key() raises EnvironmentError with the message "Variável de ambiente 'GEMINI_API_KEY' não encontrada.".

    .env
    GEMINI_API_KEY=********************

    #GeminiClient

    The get_client() method creates the genai.Client on the first call and reuses the same instance on the following ones (the client is kept in _client).

    from src.image_generation.utils.gemini_client import GeminiClient
    
    # Create the client on the first call
    client = GeminiClient().get_client()
    
    # Use a different environment variable name
    from src.image_generation.utils.gemini_client import GeminiConfig
    
    custom = GeminiClient(config=GeminiConfig(api_key_env="MY_GEMINI_KEY"))
    client = custom.get_client()

    #Notes

    The file loads the .env with load_dotenv() when imported. The client is used by Image Generator Service and by ImageGeneration in applications.py.

    Source: src/image_generation/utils/gemini_client.py

    Esc
    ↑↓navigate Enteropen