config.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from pathlib import Path
  2. import yaml
  3. class Settings:
  4. secret_key: str = ''
  5. bisheng_base_url: str = ''
  6. bisheng_websocket_url: str = ''
  7. ragflow_base_url: str = ''
  8. database_url: str = ''
  9. PUBLIC_KEY: str
  10. PRIVATE_KEY: str
  11. bisheng_agent_id: str
  12. ragflow_agent_id: str
  13. ragflow_chat_id: str
  14. def __init__(self, **kwargs):
  15. # Check if all required fields are provided and set them
  16. for field in self.__annotations__.keys():
  17. if field not in kwargs:
  18. raise ValueError(f"Missing setting: {field}")
  19. setattr(self, field, kwargs[field])
  20. def to_dict(self):
  21. """Return the settings as a dictionary."""
  22. return {k: getattr(self, k) for k in self.__annotations__.keys()}
  23. def __repr__(self):
  24. """Return a string representation of the settings."""
  25. return f"Settings({self.to_dict()})"
  26. def load_yaml(file_path: Path) -> dict:
  27. with file_path.open('r', encoding="utf-8") as fr:
  28. try:
  29. data = yaml.safe_load(fr)
  30. return data
  31. except yaml.YAMLError as e:
  32. print(f"Error loading YAML file {file_path}: {e}")
  33. return {}
  34. # Use pathlib to handle file paths
  35. config_yaml_path = Path(__file__).parent / 'config.yaml'
  36. settings_data = load_yaml(config_yaml_path)
  37. # Initialize settings object
  38. settings = Settings(**settings_data)
  39. # Print the loaded settings
  40. print(f"Loaded settings: {settings}")