File size: 6,065 Bytes
65a2540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
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