config.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from functools import lru_cache
  2. from pydantic_settings import BaseSettings, SettingsConfigDict
  3. from pydantic import Field
  4. class Settings(BaseSettings):
  5. # -----------------------
  6. # App
  7. # -----------------------
  8. app_name: str = "MyApp"
  9. env: str = "PROD"
  10. debug: bool = False
  11. # -----------------------
  12. # Database / Cache
  13. # -----------------------
  14. database_url: str = Field(..., description="Async database DSN")
  15. redis_url: str
  16. # -----------------------
  17. # Security / API Keys
  18. # -----------------------
  19. stripe_api_key: str
  20. stripe_webhook_secret: str
  21. telegram_api_token: str = Field("", description="Telegram bot API token")
  22. wechat_api_token: str = Field("", description="Wechat webhook API token")
  23. whatsapp_api_base_url: str = Field("https://waha.text.skin", description="Whatsapp API base url")
  24. whatsapp_api_key: str = Field("", description="Whatsapp API key")
  25. whatsapp_session: str = Field("default", description="Whatsapp session")
  26. # -----------------------
  27. # Storage (SeaweedFS) & Network
  28. # -----------------------
  29. api_base_url: str = Field("https://api.text.skin", description="Public API base URL for resource links")
  30. seaweedfs_master_url: str = Field("http://seaweedfs-master:9333", description="SeaweedFS Master URL")
  31. seaweedfs_volume_override_host: str = Field("", description="Optional host/IP to override SeaweedFS Volume address for cross-network access")
  32. # -----------------------
  33. # LLM Configuration (OpenAI-compatible)
  34. # -----------------------
  35. llm_api_key: str = Field("", description="LLM API Key (defaults to openai_api_key)")
  36. llm_base_url: str = Field("https://dashscope.aliyuncs.com/compatible-mode/v1", description="OpenAI-compatible Base URL")
  37. llm_model: str = Field("qwen-plus", description="Default model name, e.g., qwen-plus, gpt-4o, deepseek-chat")
  38. llm_temperature: float = Field(0.0, description="LLM sampling temperature")
  39. llm_timeout_seconds: int = Field(30, description="LLM request timeout in seconds")
  40. model_config = SettingsConfigDict(
  41. env_file=".env",
  42. env_file_encoding="utf-8",
  43. case_sensitive=False,
  44. extra="ignore",
  45. )
  46. @lru_cache
  47. def get_settings() -> Settings:
  48. """
  49. 避免多次实例化 Settings(FastAPI 官方推荐)
  50. """
  51. return Settings()
  52. settings = get_settings()