config.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """配置管理"""
  2. from pydantic_settings import BaseSettings
  3. from functools import lru_cache
  4. import os
  5. from dotenv import load_dotenv
  6. load_dotenv()
  7. class Settings(BaseSettings):
  8. """应用配置"""
  9. # 应用基本配置
  10. app_name: str = "Way_to_Engineer"
  11. app_version: str = "1.0.0"
  12. debug: bool = False
  13. # 服务器配置
  14. host: str = "0.0.0.0"
  15. port: int = 12000
  16. # CORS配置
  17. cors_origins: str = "http://localhost:5173,http://localhost:3000,http://127.0.0.1:5173,http://127.0.0.1:3000"
  18. # DeepSeek API
  19. deepseek_api_key: str = ""
  20. deepseek_model_id: str = "deepseek-chat"
  21. deepseek_base_url: str = "https://api.deepseek.com/v1"
  22. # 日志配置
  23. log_level: str = "INFO"
  24. # LLM配置
  25. llm_timeout: int = 60
  26. class Config:
  27. env_file = ".env"
  28. case_sensitive = False
  29. env_file_encoding = "utf-8"
  30. def get_cors_origins_list(self) -> list[str]:
  31. """获取CORS允许的源列表"""
  32. return [origin.strip() for origin in self.cors_origins.split(",")]
  33. # 创建全局配置实例
  34. settings = Settings()
  35. # 获取全局配置实例
  36. def get_settings() -> Settings:
  37. return settings
  38. def validate_config():
  39. """验证配置"""
  40. warnings = []
  41. llm_api_key = os.getenv("DEEPSEEK_API_KEY")
  42. if not llm_api_key:
  43. warnings.append("LLM API Key未设置,将无法使用LLM功能")
  44. if warnings:
  45. print("\n⚠️ 配置警告:")
  46. for w in warnings:
  47. print(f" - {w}")
  48. return True
  49. def print_config():
  50. """打印配置"""
  51. print(f"应用名称: {settings.app_name}")
  52. print(f"版本: {settings.app_version}")
  53. print(f"服务器: {settings.host}:{settings.port}")
  54. # 检查LLM配置
  55. llm_api_key = os.getenv("LLM_API_KEY") or os.getenv("DEEPSEEK_API_KEY")
  56. llm_base_url = os.getenv("LLM_BASE_URL") or os.getenv("DEEPSEEK_BASE_URL") or settings.deepseek_base_url
  57. llm_model = os.getenv("LLM_MODEL_ID") or os.getenv("DEEPSEEK_MODEL_ID")
  58. print(f"LLM API Key: {'已配置' if llm_api_key else '未配置'}")
  59. print(f"LLM Base URL: {llm_base_url}")
  60. print(f"LLM Model: {llm_model}")
  61. print(f"日志级别: {settings.log_level}")
  62. if __name__ == "__main__":
  63. print_config()