Fixed not getting edited state

This commit is contained in:
Lucas Oliveira 2025-09-18 13:21:32 -03:00
parent c401d2bb22
commit ba9ba0a36a

View file

@ -13,6 +13,7 @@ logger = get_logger(__name__)
@dataclass @dataclass
class ProviderConfig: class ProviderConfig:
"""Model provider configuration.""" """Model provider configuration."""
model_provider: str = "openai" # openai, anthropic, etc. model_provider: str = "openai" # openai, anthropic, etc.
api_key: str = "" api_key: str = ""
@ -20,6 +21,7 @@ class ProviderConfig:
@dataclass @dataclass
class KnowledgeConfig: class KnowledgeConfig:
"""Knowledge/ingestion configuration.""" """Knowledge/ingestion configuration."""
embedding_model: str = "text-embedding-3-small" embedding_model: str = "text-embedding-3-small"
chunk_size: int = 1000 chunk_size: int = 1000
chunk_overlap: int = 200 chunk_overlap: int = 200
@ -30,6 +32,7 @@ class KnowledgeConfig:
@dataclass @dataclass
class AgentConfig: class AgentConfig:
"""Agent configuration.""" """Agent configuration."""
llm_model: str = "gpt-4o-mini" 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." 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 @dataclass
class OpenRAGConfig: class OpenRAGConfig:
"""Complete OpenRAG configuration.""" """Complete OpenRAG configuration."""
provider: ProviderConfig provider: ProviderConfig
knowledge: KnowledgeConfig knowledge: KnowledgeConfig
agent: AgentConfig agent: AgentConfig
@ -48,7 +52,8 @@ class OpenRAGConfig:
return cls( return cls(
provider=ProviderConfig(**data.get("provider", {})), provider=ProviderConfig(**data.get("provider", {})),
knowledge=KnowledgeConfig(**data.get("knowledge", {})), 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]: def to_dict(self) -> Dict[str, Any]:
@ -61,7 +66,7 @@ class ConfigManager:
def __init__(self, config_file: Optional[str] = None): def __init__(self, config_file: Optional[str] = None):
"""Initialize configuration manager. """Initialize configuration manager.
Args: Args:
config_file: Path to configuration file. Defaults to 'config.yaml' in project root. config_file: Path to configuration file. Defaults to 'config.yaml' in project root.
""" """
@ -70,7 +75,7 @@ class ConfigManager:
def load_config(self) -> OpenRAGConfig: def load_config(self) -> OpenRAGConfig:
"""Load configuration from environment variables and config file. """Load configuration from environment variables and config file.
Priority order: Priority order:
1. Environment variables (highest) 1. Environment variables (highest)
2. Configuration file 2. Configuration file
@ -80,47 +85,46 @@ class ConfigManager:
return self._config return self._config
# Start with defaults # Start with defaults
config_data = { config_data = {"provider": {}, "knowledge": {}, "agent": {}}
"provider": {},
"knowledge": {},
"agent": {}
}
# Load from config file if it exists # Load from config file if it exists
if self.config_file.exists(): if self.config_file.exists():
try: try:
with open(self.config_file, 'r') as f: with open(self.config_file, "r") as f:
file_config = yaml.safe_load(f) or {} file_config = yaml.safe_load(f) or {}
# Merge file config # Merge file config
for section in ["provider", "knowledge", "agent"]: for section in ["provider", "knowledge", "agent"]:
if section in file_config: if section in file_config:
config_data[section].update(file_config[section]) config_data[section].update(file_config[section])
config_data["edited"] = file_config.get("edited", False)
logger.info(f"Loaded configuration from {self.config_file}") logger.info(f"Loaded configuration from {self.config_file}")
except Exception as e: except Exception as e:
logger.warning(f"Failed to load config file {self.config_file}: {e}") logger.warning(f"Failed to load config file {self.config_file}: {e}")
# Create config object first to check edited flags # Create config object first to check edited flags
temp_config = OpenRAGConfig.from_dict(config_data) temp_config = OpenRAGConfig.from_dict(config_data)
# Override with environment variables (highest priority, but respect edited flags) # Override with environment variables (highest priority, but respect edited flags)
self._load_env_overrides(config_data, temp_config) self._load_env_overrides(config_data, temp_config)
# Create config object # Create config object
self._config = OpenRAGConfig.from_dict(config_data) self._config = OpenRAGConfig.from_dict(config_data)
logger.debug("Configuration loaded", config=self._config.to_dict()) logger.debug("Configuration loaded", config=self._config.to_dict())
return self._config 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.""" """Load environment variable overrides, respecting edited flag."""
# Skip all environment overrides if config has been manually edited # Skip all environment overrides if config has been manually edited
if temp_config and temp_config.edited: if temp_config and temp_config.edited:
logger.debug("Skipping all env overrides - config marked as edited") logger.debug("Skipping all env overrides - config marked as edited")
return return
# Provider settings # Provider settings
if os.getenv("MODEL_PROVIDER"): if os.getenv("MODEL_PROVIDER"):
config_data["provider"]["model_provider"] = os.getenv("MODEL_PROVIDER") config_data["provider"]["model_provider"] = os.getenv("MODEL_PROVIDER")
@ -140,9 +144,15 @@ class ConfigManager:
if os.getenv("CHUNK_OVERLAP"): if os.getenv("CHUNK_OVERLAP"):
config_data["knowledge"]["chunk_overlap"] = int(os.getenv("CHUNK_OVERLAP")) config_data["knowledge"]["chunk_overlap"] = int(os.getenv("CHUNK_OVERLAP"))
if os.getenv("OCR_ENABLED"): 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"): 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 # Agent settings
if os.getenv("LLM_MODEL"): if os.getenv("LLM_MODEL"):
@ -163,10 +173,10 @@ class ConfigManager:
def save_config_file(self, config: Optional[OpenRAGConfig] = None) -> bool: def save_config_file(self, config: Optional[OpenRAGConfig] = None) -> bool:
"""Save configuration to file. """Save configuration to file.
Args: Args:
config: Configuration to save. If None, uses current config. config: Configuration to save. If None, uses current config.
Returns: Returns:
True if saved successfully, False otherwise. True if saved successfully, False otherwise.
""" """
@ -179,13 +189,13 @@ class ConfigManager:
try: try:
# Ensure directory exists # Ensure directory exists
self.config_file.parent.mkdir(parents=True, exist_ok=True) 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) yaml.dump(config.to_dict(), f, default_flow_style=False, indent=2)
# Update cached config to reflect the edited flags # Update cached config to reflect the edited flags
self._config = config self._config = config
logger.info(f"Configuration saved to {self.config_file} - marked as edited") logger.info(f"Configuration saved to {self.config_file} - marked as edited")
return True return True
except Exception as e: except Exception as e: