config.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """配置管理模块"""
  2. import os
  3. from pathlib import Path
  4. from pydantic_settings import BaseSettings
  5. from dotenv import load_dotenv
  6. # 加载环境变量
  7. load_dotenv()
  8. class Settings(BaseSettings):
  9. """应用配置"""
  10. # LLM配置(支持多种命名方式)
  11. llm_api_key: str = ""
  12. llm_base_url: str = "https://api.openai.com/v1"
  13. llm_model_id: str = "gpt-4"
  14. llm_timeout: int = 180
  15. # 兼容旧字段名
  16. openai_api_key: str = "" # 兼容字段,会自动映射到 llm_api_key
  17. openai_base_url: str = "https://api.openai.com/v1"
  18. openai_model: str = "gpt-4"
  19. # 搜索 API 配置
  20. tavily_api_key: str = ""
  21. serpapi_api_key: str = ""
  22. # 系统配置
  23. max_depth: int = 3
  24. approval_threshold: int = 75 # 评审通过阈值(分数 >= 此值则通过)
  25. revision_threshold: int = 60 # 修改阈值(分数 < 此值则需要重写)
  26. enable_parallel: bool = False
  27. enable_search: bool = True # 是否启用搜索功能
  28. enable_review: bool = True # 是否启用评审功能(仅 ReAct 模式)
  29. max_revisions: int = 2 # 最大修改次数
  30. # 服务器配置(可选,用于 API 服务)
  31. host: str = "0.0.0.0"
  32. port: int = 8000
  33. cors_origins: str = ""
  34. log_level: str = "INFO"
  35. # 其他服务配置(可选,忽略未使用的)
  36. unsplash_access_key: str = ""
  37. unsplash_secret_key: str = ""
  38. vite_api_base_url: str = ""
  39. amap_api_key: str = ""
  40. vite_amap_web_key: str = ""
  41. # 字数配置
  42. word_count_level_1: int = 600
  43. word_count_level_2: int = 400
  44. word_count_level_3: int = 200
  45. word_count_tolerance: float = 0.1
  46. class Config:
  47. env_file = ".env"
  48. case_sensitive = False
  49. extra = "ignore" # 忽略未定义的字段,避免验证错误
  50. # 全局配置实例
  51. _settings = None
  52. def get_settings() -> Settings:
  53. """获取配置实例(单例模式)"""
  54. global _settings
  55. if _settings is None:
  56. _settings = Settings()
  57. # 兼容处理:如果使用旧字段名,自动映射到新字段名
  58. if _settings.openai_api_key and not _settings.llm_api_key:
  59. _settings.llm_api_key = _settings.openai_api_key
  60. if _settings.openai_base_url and _settings.llm_base_url == "https://api.openai.com/v1":
  61. _settings.llm_base_url = _settings.openai_base_url
  62. if _settings.openai_model and _settings.llm_model_id == "gpt-4":
  63. _settings.llm_model_id = _settings.openai_model
  64. return _settings
  65. def get_word_count(level: int) -> int:
  66. """获取指定层级的目标字数"""
  67. settings = get_settings()
  68. word_counts = {
  69. 1: settings.word_count_level_1,
  70. 2: settings.word_count_level_2,
  71. 3: settings.word_count_level_3
  72. }
  73. return word_counts.get(level, 400)