From ba9ba0a36a59ee5abd5911c65b1d29db2825a993 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Thu, 18 Sep 2025 13:21:32 -0300 Subject: [PATCH] Fixed not getting edited state --- src/config/config_manager.py | 58 +++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/config/config_manager.py b/src/config/config_manager.py index 93e6a279..d2de63d3 100644 --- a/src/config/config_manager.py +++ b/src/config/config_manager.py @@ -13,6 +13,7 @@ logger = get_logger(__name__) @dataclass class ProviderConfig: """Model provider configuration.""" + model_provider: str = "openai" # openai, anthropic, etc. api_key: str = "" @@ -20,6 +21,7 @@ class ProviderConfig: @dataclass class KnowledgeConfig: """Knowledge/ingestion configuration.""" + embedding_model: str = "text-embedding-3-small" chunk_size: int = 1000 chunk_overlap: int = 200 @@ -30,6 +32,7 @@ class KnowledgeConfig: @dataclass class AgentConfig: """Agent configuration.""" + llm_model: str = "gpt-4o-mini" system_prompt: str = "You are a helpful AI assistant with access to a knowledge base. Answer questions based on the provided context." @@ -37,6 +40,7 @@ class AgentConfig: @dataclass class OpenRAGConfig: """Complete OpenRAG configuration.""" + provider: ProviderConfig knowledge: KnowledgeConfig agent: AgentConfig @@ -48,7 +52,8 @@ class OpenRAGConfig: return cls( provider=ProviderConfig(**data.get("provider", {})), knowledge=KnowledgeConfig(**data.get("knowledge", {})), - agent=AgentConfig(**data.get("agent", {})) + agent=AgentConfig(**data.get("agent", {})), + edited=data.get("edited", False), ) def to_dict(self) -> Dict[str, Any]: @@ -61,7 +66,7 @@ class ConfigManager: def __init__(self, config_file: Optional[str] = None): """Initialize configuration manager. - + Args: config_file: Path to configuration file. Defaults to 'config.yaml' in project root. """ @@ -70,7 +75,7 @@ class ConfigManager: def load_config(self) -> OpenRAGConfig: """Load configuration from environment variables and config file. - + Priority order: 1. Environment variables (highest) 2. Configuration file @@ -80,47 +85,46 @@ class ConfigManager: return self._config # Start with defaults - config_data = { - "provider": {}, - "knowledge": {}, - "agent": {} - } + config_data = {"provider": {}, "knowledge": {}, "agent": {}} # Load from config file if it exists if self.config_file.exists(): try: - with open(self.config_file, 'r') as f: + with open(self.config_file, "r") as f: file_config = yaml.safe_load(f) or {} - + # Merge file config for section in ["provider", "knowledge", "agent"]: if section in file_config: config_data[section].update(file_config[section]) - + config_data["edited"] = file_config.get("edited", False) + logger.info(f"Loaded configuration from {self.config_file}") except Exception as e: logger.warning(f"Failed to load config file {self.config_file}: {e}") # Create config object first to check edited flags temp_config = OpenRAGConfig.from_dict(config_data) - + # Override with environment variables (highest priority, but respect edited flags) self._load_env_overrides(config_data, temp_config) # Create config object self._config = OpenRAGConfig.from_dict(config_data) - + logger.debug("Configuration loaded", config=self._config.to_dict()) return self._config - def _load_env_overrides(self, config_data: Dict[str, Any], temp_config: Optional["OpenRAGConfig"] = None) -> None: + def _load_env_overrides( + self, config_data: Dict[str, Any], temp_config: Optional["OpenRAGConfig"] = None + ) -> None: """Load environment variable overrides, respecting edited flag.""" - + # Skip all environment overrides if config has been manually edited if temp_config and temp_config.edited: logger.debug("Skipping all env overrides - config marked as edited") return - + # Provider settings if os.getenv("MODEL_PROVIDER"): config_data["provider"]["model_provider"] = os.getenv("MODEL_PROVIDER") @@ -140,9 +144,15 @@ class ConfigManager: if os.getenv("CHUNK_OVERLAP"): config_data["knowledge"]["chunk_overlap"] = int(os.getenv("CHUNK_OVERLAP")) if os.getenv("OCR_ENABLED"): - config_data["knowledge"]["ocr"] = os.getenv("OCR_ENABLED").lower() in ("true", "1", "yes") + config_data["knowledge"]["ocr"] = os.getenv("OCR_ENABLED").lower() in ( + "true", + "1", + "yes", + ) if os.getenv("PICTURE_DESCRIPTIONS_ENABLED"): - config_data["knowledge"]["picture_descriptions"] = os.getenv("PICTURE_DESCRIPTIONS_ENABLED").lower() in ("true", "1", "yes") + config_data["knowledge"]["picture_descriptions"] = os.getenv( + "PICTURE_DESCRIPTIONS_ENABLED" + ).lower() in ("true", "1", "yes") # Agent settings if os.getenv("LLM_MODEL"): @@ -163,10 +173,10 @@ class ConfigManager: def save_config_file(self, config: Optional[OpenRAGConfig] = None) -> bool: """Save configuration to file. - + Args: config: Configuration to save. If None, uses current config. - + Returns: True if saved successfully, False otherwise. """ @@ -179,13 +189,13 @@ class ConfigManager: try: # Ensure directory exists self.config_file.parent.mkdir(parents=True, exist_ok=True) - - with open(self.config_file, 'w') as f: + + with open(self.config_file, "w") as f: yaml.dump(config.to_dict(), f, default_flow_style=False, indent=2) - + # Update cached config to reflect the edited flags self._config = config - + logger.info(f"Configuration saved to {self.config_file} - marked as edited") return True except Exception as e: