#!/usr/bin/env python3 import yaml import os from typing import Dict, Any, List from pathlib import Path class ConfigManager: """Manages configuration loading and access for the chatbot application.""" def __init__(self, config_path: str = None): """ Initialize the configuration manager. Args: config_path: Path to the configuration file. Defaults to config.yaml in project root. """ if config_path is None: # Default to config.yaml in the project root project_root = Path(__file__).parent.parent config_path = project_root / "config.yaml" self.config_path = Path(config_path) self._config = self._load_config() def _load_config(self) -> Dict[str, Any]: """Load configuration from YAML file.""" try: with open(self.config_path, 'r', encoding='utf-8') as file: config = yaml.safe_load(file) return config or {} except FileNotFoundError: raise FileNotFoundError(f"Configuration file not found: {self.config_path}") except yaml.YAMLError as e: raise ValueError(f"Error parsing configuration file: {e}") def get(self, key_path: str, default: Any = None) -> Any: """ Get a configuration value using dot notation. Args: key_path: Dot-separated path to the configuration value (e.g., 'database.path') default: Default value to return if key is not found Returns: The configuration value or default if not found """ keys = key_path.split('.') value = self._config try: for key in keys: value = value[key] return value except (KeyError, TypeError): return default def get_database_config(self) -> Dict[str, Any]: """Get database configuration.""" return self.get('database', {}) def get_openai_config(self, component: str = None) -> Dict[str, Any]: """ Get OpenAI configuration. Args: component: Specific component configuration (e.g., 'intent_classifier') Returns: OpenAI configuration dictionary """ if component: return self.get(f'openai.{component}', {}) return self.get('openai', {}) def get_vector_store_config(self) -> Dict[str, Any]: """Get vector store configuration.""" return self.get('vector_store', {}) def get_search_config(self) -> Dict[str, Any]: """Get search configuration.""" return self.get('search', {}) def get_entity_extraction_config(self) -> Dict[str, Any]: """Get entity extraction configuration.""" return self.get('entity_extraction', {}) def get_business_logic_config(self) -> Dict[str, Any]: """Get business logic configuration.""" return self.get('business_logic', {}) def get_app_config(self) -> Dict[str, Any]: """Get application configuration.""" return self.get('app', {}) def is_feature_enabled(self, feature_name: str) -> bool: """ Check if a feature is enabled. Args: feature_name: Name of the feature to check Returns: True if feature is enabled, False otherwise """ return self.get(f'app.features.{feature_name}', True) # Global configuration instance _config_manager = None def get_config() -> ConfigManager: """Get the global configuration manager instance.""" global _config_manager if _config_manager is None: _config_manager = ConfigManager() return _config_manager def reload_config(): """Reload the configuration from file.""" global _config_manager _config_manager = ConfigManager()