config.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from pydantic_settings import BaseSettings
  2. import yaml
  3. from pathlib import Path
  4. class AppConfig(BaseSettings):
  5. name: str = "shudao-chat-py"
  6. host: str = "0.0.0.0"
  7. port: int = 22001
  8. debug: bool = True
  9. class DatabaseConfig(BaseSettings):
  10. user: str
  11. password: str
  12. host: str
  13. port: int
  14. database: str
  15. pool_size: int = 100
  16. max_overflow: int = 10
  17. pool_recycle: int = 3600
  18. class DeepSeekConfig(BaseSettings):
  19. api_key: str
  20. api_url: str
  21. class Qwen3Config(BaseSettings):
  22. api_url: str
  23. model: str
  24. class IntentConfig(BaseSettings):
  25. api_url: str
  26. model: str
  27. class YoloConfig(BaseSettings):
  28. base_url: str
  29. class SearchConfig(BaseSettings):
  30. api_url: str
  31. heartbeat_url: str
  32. class DifyConfig(BaseSettings):
  33. workflow_url: str
  34. workflow_id: str
  35. auth_token: str
  36. class AuthConfig(BaseSettings):
  37. api_url: str
  38. class OSSConfig(BaseSettings):
  39. access_key_id: str
  40. access_key_secret: str
  41. bucket: str
  42. endpoint: str
  43. parse_encrypt_key: str
  44. class AIChatConfig(BaseSettings):
  45. api_url: str = "http://127.0.0.1:28002/api/v1"
  46. timeout: int = 600
  47. class ThinkingSummaryConfig(BaseSettings):
  48. """思考过程二次总结(方案三)配置"""
  49. enabled: bool = True
  50. max_points: int = 5
  51. max_input_chars: int = 1500
  52. max_output_chars: int = 600
  53. temperature: float = 0.2
  54. class Settings:
  55. def __init__(self, config_path: str = "config.yaml"):
  56. # 获取项目根目录
  57. if not Path(config_path).is_absolute():
  58. # 相对于当前文件的路径
  59. current_dir = Path(__file__).parent.parent
  60. config_file = current_dir / config_path
  61. else:
  62. config_file = Path(config_path)
  63. if config_file.exists():
  64. with open(config_file, 'r', encoding='utf-8') as f:
  65. config_data = yaml.safe_load(f)
  66. else:
  67. raise FileNotFoundError(f"配置文件不存在: {config_file}")
  68. self.app = AppConfig(**config_data.get('app', {}))
  69. self.database = DatabaseConfig(**config_data.get('database', {}))
  70. self.deepseek = DeepSeekConfig(**config_data.get('deepseek', {}))
  71. self.qwen3 = Qwen3Config(**config_data.get('qwen3', {}))
  72. self.intent = IntentConfig(**config_data.get('intent', {}))
  73. self.yolo = YoloConfig(**config_data.get('yolo', {}))
  74. self.search = SearchConfig(**config_data.get('search', {}))
  75. self.dify = DifyConfig(**config_data.get('dify', {}))
  76. self.auth = AuthConfig(**config_data.get('auth', {}))
  77. self.oss = OSSConfig(**config_data.get('oss', {}))
  78. self.aichat = AIChatConfig(**config_data.get('aichat', {}))
  79. self.thinking_summary = ThinkingSummaryConfig(**config_data.get('thinking_summary', {}))
  80. self.base_url = config_data.get(
  81. 'base_url', 'https://aqai.shudaodsj.com:22001')
  82. settings = Settings()
  83. def get_base_url() -> str:
  84. return settings.base_url
  85. def get_proxy_url(original_url: str) -> str:
  86. """将原始URL转换为代理URL"""
  87. if not original_url:
  88. return ""
  89. return f"{settings.base_url}/apiv1/oss/parse?url={original_url}"