""" Configuration module for GAIA Agent. Loads settings from environment variables with sensible defaults. """ import os from typing import Optional, Any from dotenv import load_dotenv # Load environment variables load_dotenv() def get_env_bool(key: str, default: bool = False) -> bool: """Get boolean from environment variable.""" value = os.getenv(key, str(default)).lower() return value in ('true', '1', 'yes', 'on', 'enabled') def get_env_int(key: str, default: int = 0) -> int: """Get integer from environment variable.""" try: return int(os.getenv(key, str(default))) except ValueError: return default def get_env_float(key: str, default: float = 0.0) -> float: """Get float from environment variable.""" try: return float(os.getenv(key, str(default))) except ValueError: return default class GAIAConfig: """GAIA Agent configuration loaded from environment variables.""" # API Configuration ANTHROPIC_API_KEY: str = os.getenv("ANTHROPIC_API_KEY", "") OPENAI_API_KEY: str = os.getenv("OPENAI_API_KEY", "") # Logging LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO") # Main Cache Configuration ENABLE_CACHE: bool = get_env_bool("ENABLE_CACHE", True) CACHE_TTL_SECONDS: int = get_env_int("CACHE_TTL_SECONDS", 3600) CACHE_MAX_SIZE: int = get_env_int("CACHE_MAX_SIZE", 200) # Tool-specific Cache Settings WEB_SEARCH_CACHE_TTL: int = get_env_int("WEB_SEARCH_CACHE_TTL", 1800) WEB_SEARCH_CACHE_SIZE: int = get_env_int("WEB_SEARCH_CACHE_SIZE", 200) WIKIPEDIA_CACHE_TTL: int = get_env_int("WIKIPEDIA_CACHE_TTL", 3600) WIKIPEDIA_CACHE_SIZE: int = get_env_int("WIKIPEDIA_CACHE_SIZE", 150) CALCULATOR_CACHE_TTL: int = get_env_int("CALCULATOR_CACHE_TTL", 7200) CALCULATOR_CACHE_SIZE: int = get_env_int("CALCULATOR_CACHE_SIZE", 100) # Performance Monitoring ENABLE_PERFORMANCE_MONITORING: bool = get_env_bool("ENABLE_PERFORMANCE_MONITORING", True) PERFORMANCE_LOG_INTERVAL: int = get_env_int("PERFORMANCE_LOG_INTERVAL", 300) # Retry Configuration MAX_RETRIES: int = get_env_int("MAX_RETRIES", 3) RETRY_BACKOFF_SECONDS: float = get_env_float("RETRY_BACKOFF_SECONDS", 1.0) API_RETRY_ATTEMPTS: int = get_env_int("API_RETRY_ATTEMPTS", 3) # Tool Timeouts SEARCH_TIMEOUT: int = get_env_int("SEARCH_TIMEOUT", 10) CALCULATOR_TIMEOUT: int = get_env_int("CALCULATOR_TIMEOUT", 5) WIKIPEDIA_TIMEOUT: int = get_env_int("WIKIPEDIA_TIMEOUT", 8) @classmethod def validate(cls) -> bool: """Validate required configuration.""" if not cls.ANTHROPIC_API_KEY: raise ValueError("ANTHROPIC_API_KEY is required but not set") if cls.CACHE_TTL_SECONDS <= 0: raise ValueError("CACHE_TTL_SECONDS must be positive") if cls.CACHE_MAX_SIZE <= 0: raise ValueError("CACHE_MAX_SIZE must be positive") return True @classmethod def get_cache_config(cls) -> dict: """Get cache configuration as dictionary.""" return { 'enabled': cls.ENABLE_CACHE, 'ttl': cls.CACHE_TTL_SECONDS, 'max_size': cls.CACHE_MAX_SIZE, 'tools': { 'web_search': { 'ttl': cls.WEB_SEARCH_CACHE_TTL, 'max_size': cls.WEB_SEARCH_CACHE_SIZE }, 'wikipedia': { 'ttl': cls.WIKIPEDIA_CACHE_TTL, 'max_size': cls.WIKIPEDIA_CACHE_SIZE }, 'calculator': { 'ttl': cls.CALCULATOR_CACHE_TTL, 'max_size': cls.CALCULATOR_CACHE_SIZE } } } @classmethod def get_retry_config(cls) -> dict: """Get retry configuration as dictionary.""" return { 'max_retries': cls.MAX_RETRIES, 'backoff': cls.RETRY_BACKOFF_SECONDS, 'api_attempts': cls.API_RETRY_ATTEMPTS } @classmethod def get_timeout_config(cls) -> dict: """Get timeout configuration as dictionary.""" return { 'search': cls.SEARCH_TIMEOUT, 'calculator': cls.CALCULATOR_TIMEOUT, 'wikipedia': cls.WIKIPEDIA_TIMEOUT } @classmethod def summary(cls) -> str: """Get configuration summary.""" lines = [ "🔧 GAIA Agent Configuration", "━━━━━━━━━━━━━━━━━━━━━━━━━━━━", f"Cache: {'✅ Enabled' if cls.ENABLE_CACHE else '❌ Disabled'}", f"Cache TTL: {cls.CACHE_TTL_SECONDS}s ({cls.CACHE_TTL_SECONDS//3600}h {(cls.CACHE_TTL_SECONDS%3600)//60}m)", f"Cache Size: {cls.CACHE_MAX_SIZE} entries", f"Performance Monitoring: {'✅ Enabled' if cls.ENABLE_PERFORMANCE_MONITORING else '❌ Disabled'}", f"Max Retries: {cls.MAX_RETRIES}", f"Retry Backoff: {cls.RETRY_BACKOFF_SECONDS}s", "", "Tool-specific settings:", f" 🔍 Web Search: TTL={cls.WEB_SEARCH_CACHE_TTL}s, Size={cls.WEB_SEARCH_CACHE_SIZE}", f" 📚 Wikipedia: TTL={cls.WIKIPEDIA_CACHE_TTL}s, Size={cls.WIKIPEDIA_CACHE_SIZE}", f" 🧮 Calculator: TTL={cls.CALCULATOR_CACHE_TTL}s, Size={cls.CALCULATOR_CACHE_SIZE}" ] return "\n".join(lines) # Global configuration instance config = GAIAConfig() def load_config() -> GAIAConfig: """Load and validate configuration.""" config.validate() return config # Export commonly used functions def is_cache_enabled() -> bool: """Check if caching is enabled.""" return config.ENABLE_CACHE def is_monitoring_enabled() -> bool: """Check if performance monitoring is enabled.""" return config.ENABLE_PERFORMANCE_MONITORING def get_cache_ttl() -> int: """Get default cache TTL.""" return config.CACHE_TTL_SECONDS