Load and cache configuration. Args: path: Optional path to config file force_reload: Force reload even if cached Returns: Loaded and validated configuration dictionary
(cls, path: str = None, force_reload: bool = False)
| 19 | |
| 20 | @classmethod |
| 21 | def load(cls, path: str = None, force_reload: bool = False) -> Dict[str, Any]: |
| 22 | """ |
| 23 | Load and cache configuration. |
| 24 | |
| 25 | Args: |
| 26 | path: Optional path to config file |
| 27 | force_reload: Force reload even if cached |
| 28 | |
| 29 | Returns: |
| 30 | Loaded and validated configuration dictionary |
| 31 | """ |
| 32 | if cls._cached_config and not force_reload: |
| 33 | return cls._cached_config |
| 34 | |
| 35 | if not path: |
| 36 | # Priority order for config files |
| 37 | config_locations = [ |
| 38 | Path.home() / ".optillm" / "proxy_config.yaml", |
| 39 | Path.home() / ".optillm" / "proxy_config.yml", |
| 40 | Path(__file__).parent / "example_config.yaml", |
| 41 | ] |
| 42 | |
| 43 | for config_path in config_locations: |
| 44 | if config_path.exists(): |
| 45 | path = config_path |
| 46 | logger.info(f"Using config from: {path}") |
| 47 | break |
| 48 | else: |
| 49 | # No config found, create default |
| 50 | path = config_locations[0] |
| 51 | cls._create_default(path) |
| 52 | |
| 53 | cls._config_path = Path(path) |
| 54 | |
| 55 | try: |
| 56 | with open(path, 'r') as f: |
| 57 | config = yaml.safe_load(f) or {} |
| 58 | |
| 59 | # Validate structure |
| 60 | if not isinstance(config, dict): |
| 61 | raise ValueError("Configuration must be a dictionary") |
| 62 | |
| 63 | # Interpolate environment variables |
| 64 | config = cls._interpolate_env_vars(config) |
| 65 | |
| 66 | # Apply defaults and validate |
| 67 | config = cls._apply_defaults(config) |
| 68 | config = cls._validate_config(config) |
| 69 | |
| 70 | cls._cached_config = config |
| 71 | logger.debug(f"Loaded config with {len(config.get('providers', []))} providers") |
| 72 | return config |
| 73 | |
| 74 | except Exception as e: |
| 75 | logger.error(f"Failed to load proxy config from {path}: {e}") |
| 76 | return cls._get_minimal_config() |
| 77 | |
| 78 | @classmethod |