config.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """配置管理模块"""
  2. import os
  3. from pathlib import Path
  4. from typing import List
  5. from pydantic_settings import BaseSettings
  6. from dotenv import load_dotenv
  7. # 加载环境变量
  8. # 首先尝试加载当前目录的.env
  9. load_dotenv()
  10. # 然后尝试加载HelloAgents的.env(如果存在)
  11. helloagents_env = Path(__file__).parent.parent.parent.parent / "HelloAgents" / ".env"
  12. if helloagents_env.exists():
  13. load_dotenv(helloagents_env, override=False) # 不覆盖已有的环境变量
  14. class Settings(BaseSettings):
  15. """应用配置"""
  16. # 应用基本配置
  17. app_name: str = "HelloAgents智能旅行助手"
  18. app_version: str = "1.0.0"
  19. debug: bool = False
  20. # 服务器配置
  21. host: str = "0.0.0.0"
  22. port: int = 8000
  23. # CORS配置 - 使用字符串,在代码中分割
  24. cors_origins: str = "http://localhost:5173,http://localhost:3000,http://127.0.0.1:5173,http://127.0.0.1:3000"
  25. # 高德地图API配置
  26. amap_api_key: str = ""
  27. # Unsplash API配置
  28. unsplash_access_key: str = ""
  29. unsplash_secret_key: str = ""
  30. # LLM配置 (从环境变量读取,由HelloAgents管理)
  31. openai_api_key: str = ""
  32. openai_base_url: str = "https://api.openai.com/v1"
  33. openai_model: str = "gpt-4"
  34. # 日志配置
  35. log_level: str = "INFO"
  36. class Config:
  37. env_file = ".env"
  38. case_sensitive = False
  39. extra = "ignore" # 忽略额外的环境变量
  40. def get_cors_origins_list(self) -> List[str]:
  41. """获取CORS origins列表"""
  42. return [origin.strip() for origin in self.cors_origins.split(',')]
  43. # 创建全局配置实例
  44. settings = Settings()
  45. def get_settings() -> Settings:
  46. """获取配置实例"""
  47. return settings
  48. # 验证必要的配置
  49. def validate_config():
  50. """验证配置是否完整"""
  51. errors = []
  52. warnings = []
  53. if not settings.amap_api_key:
  54. errors.append("AMAP_API_KEY未配置")
  55. # HelloAgentsLLM会自动从LLM_API_KEY读取,不强制要求OPENAI_API_KEY
  56. llm_api_key = os.getenv("LLM_API_KEY") or os.getenv("OPENAI_API_KEY")
  57. if not llm_api_key:
  58. warnings.append("LLM_API_KEY或OPENAI_API_KEY未配置,LLM功能可能无法使用")
  59. if errors:
  60. error_msg = "配置错误:\n" + "\n".join(f" - {e}" for e in errors)
  61. raise ValueError(error_msg)
  62. if warnings:
  63. print("\n⚠️ 配置警告:")
  64. for w in warnings:
  65. print(f" - {w}")
  66. return True
  67. # 打印配置信息(用于调试)
  68. def print_config():
  69. """打印当前配置(隐藏敏感信息)"""
  70. print(f"应用名称: {settings.app_name}")
  71. print(f"版本: {settings.app_version}")
  72. print(f"服务器: {settings.host}:{settings.port}")
  73. print(f"高德地图API Key: {'已配置' if settings.amap_api_key else '未配置'}")
  74. # 检查LLM配置
  75. llm_api_key = os.getenv("LLM_API_KEY") or os.getenv("OPENAI_API_KEY")
  76. llm_base_url = os.getenv("LLM_BASE_URL") or settings.openai_base_url
  77. llm_model = os.getenv("LLM_MODEL_ID") or settings.openai_model
  78. print(f"LLM API Key: {'已配置' if llm_api_key else '未配置'}")
  79. print(f"LLM Base URL: {llm_base_url}")
  80. print(f"LLM Model: {llm_model}")
  81. print(f"日志级别: {settings.log_level}")