refactor(config): Switch to environment variables for API keys
- Modifies docker-compose.yml to use for injecting the Gemini API key, which is more robust than volume mounting. - Updates helpers.py to prioritize reading the API key from the environment variable. - Removes the now-redundant file-based key loading logic from config.py and the Dockerfile. - This change completely bypasses the problematic file system interactions within the container, providing a definitive fix for the 'API Key missing' error.
This commit is contained in:
40
helpers.py
40
helpers.py
@@ -293,25 +293,33 @@ def get_email_address(firstname, lastname, website):
|
||||
# ==============================================================================
|
||||
|
||||
def _get_gemini_api_key():
|
||||
"""Retrieves Gemini API Key from Config or Environment."""
|
||||
"""
|
||||
Retrieves Gemini API Key, prioritizing environment variables as the most robust method.
|
||||
"""
|
||||
logger = logging.getLogger(__name__)
|
||||
print("DEBUG: _get_gemini_api_key called.") # Debug print
|
||||
api_key = Config.API_KEYS.get('openai') # Legacy slot
|
||||
print(f"DEBUG: API Key from Config.API_KEYS['openai']: {api_key if api_key else 'None'}") # Debug print
|
||||
logging.info("Attempting to retrieve Gemini API Key...")
|
||||
|
||||
if not api_key:
|
||||
# Fallback: Versuche Environment Variable, falls Config leer ist
|
||||
api_key = os.environ.get("OPENAI_API_KEY")
|
||||
print(f"DEBUG: API Key from env OPENAI_API_KEY: {api_key if api_key else 'None'}") # Debug print
|
||||
if not api_key:
|
||||
# Fallback 2: Versuche den Gemini Key direkt
|
||||
api_key = os.environ.get("GEMINI_API_KEY") or Config.API_KEYS.get('gemini')
|
||||
print(f"DEBUG: API Key from env GEMINI_API_KEY or Config.API_KEYS['gemini']: {api_key if api_key else 'None'}") # Debug print
|
||||
# Primary Method: Environment Variable (most robust for Docker)
|
||||
api_key = os.environ.get("GEMINI_API_KEY")
|
||||
if api_key:
|
||||
logging.info("Successfully loaded API key from GEMINI_API_KEY environment variable.")
|
||||
return api_key
|
||||
|
||||
# Fallback 1: Legacy Environment Variable
|
||||
api_key = os.environ.get("OPENAI_API_KEY")
|
||||
if api_key:
|
||||
logging.warning("Loaded API key from legacy OPENAI_API_KEY environment variable.")
|
||||
return api_key
|
||||
|
||||
# Fallback 2: File-based (less reliable with volume mounts)
|
||||
logging.warning("Could not find API key in environment variables. Falling back to file-based method.")
|
||||
api_key = Config.API_KEYS.get('openai') # Legacy slot in config
|
||||
if api_key:
|
||||
logging.info("Successfully loaded API key from config file.")
|
||||
return api_key
|
||||
|
||||
if not api_key:
|
||||
logger.error("Fehler: Kein API Key gefunden (weder als 'openai' noch 'gemini').")
|
||||
raise ValueError("API Key missing.")
|
||||
return api_key
|
||||
logger.error("CRITICAL: No API Key found in environment variables or config file.")
|
||||
raise ValueError("API Key missing.")
|
||||
|
||||
@retry_on_failure
|
||||
def call_gemini_flash(prompt, system_instruction=None, temperature=0.3, json_mode=False):
|
||||
|
||||
Reference in New Issue
Block a user