File size: 3,275 Bytes
6d6b8af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Enhanced AI Configuration Manager

"""

import json
import os
from pathlib import Path
from typing import Any, Dict, Optional, Union
import logging

logger = logging.getLogger(__name__)

class EnhancedAIConfig:
    """Configuration manager for AI systems with enhanced functionality"""
    
    def __init__(self, config_path: Union[str, Path]):
        self.config_path = Path(config_path)
        self.defaults = {
            "host": "127.0.0.1",
            "port": 8000,
            "model_name": "gpt2-large",
            "quantum_fluctuation": 0.07,
            "spiderweb_dim": 5,
            "recursion_depth": 4,
            "perspectives": ["Newton", "DaVinci", "Ethical", "Quantum", "Memory"]
        }
        self.config = self._load_config()

    def _load_config(self) -> Dict[str, Any]:
        """Load configuration from file with fallback to defaults"""
        try:
            if self.config_path.exists():
                with open(self.config_path, 'r') as f:
                    config = json.load(f)
                logger.info(f"Configuration loaded from {self.config_path}")
                return config
            else:
                logger.warning(f"Config file {self.config_path} not found, using defaults")
                try:
                    self._save_config(self.defaults)  # Create default config
                except Exception as e:
                    logger.debug(f"Could not write default config: {e}")
                return self.defaults.copy()
        except Exception as e:
            logger.error(f"Error loading config: {e}")
            return self.defaults.copy()

    def _save_config(self, config: Dict[str, Any]):
        """Save configuration to file"""
        try:
            with open(self.config_path, 'w') as f:
                json.dump(config, f, indent=2)
            logger.info(f"Configuration saved to {self.config_path}")
        except Exception as e:
            logger.error(f"Error saving config: {e}")

    def get(self, key: str, default: Any = None) -> Any:
        """

        Get configuration value with fallback to default

        

        Args:

            key: Configuration key to retrieve

            default: Default value if key not found

            

        Returns:

            Configuration value

        """
        return self.config.get(key, self.defaults.get(key, default))

    def set(self, key: str, value: Any):
        """

        Set configuration value

        

        Args:

            key: Configuration key to set

            value: Value to set

        """
        self.config[key] = value
        self._save_config(self.config)

    def update(self, updates: Dict[str, Any]):
        """

        Update multiple configuration values

        

        Args:

            updates: Dictionary of updates to apply

        """
        self.config.update(updates)
        self._save_config(self.config)

    def reset(self):
        """Reset configuration to defaults"""
        self.config = self.defaults.copy()
        self._save_config(self.config)

    def get_all(self) -> Dict[str, Any]:
        """Get complete configuration"""
        return self.config.copy()