config.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. creem_api_key: str = Field("", description="Creem API Key")
  22. creem_webhook_secret: str = Field("", description="Creem Webhook Secret")
  23. creem_base_url: str = Field("https://test-api.creem.io", description="Creem Base URL (or https://api.creem.io)")
  24. creem_product_id: str = Field("", description="Default Creem Product ID (optional)")
  25. telegram_api_token: str = Field("", description="Telegram bot API token")
  26. wechat_api_token: str = Field("", description="Wechat webhook API token")
  27. whatsapp_api_base_url: str = Field("https://waha.text.skin", description="Whatsapp API base url")
  28. whatsapp_api_key: str = Field("", description="Whatsapp API key")
  29. whatsapp_session: str = Field("default", description="Whatsapp session")
  30. # -----------------------
  31. # Storage (SeaweedFS) & Network
  32. # -----------------------
  33. api_base_url: str = Field("https://api.text.skin", description="Public API base URL for resource links")
  34. seaweedfs_master_url: str = Field("http://seaweedfs-master:9333", description="SeaweedFS Master URL")
  35. seaweedfs_volume_override_host: str = Field("", description="Optional host/IP to override SeaweedFS Volume address for cross-network access")
  36. # -----------------------
  37. # LLM Configuration (OpenAI-compatible)
  38. # -----------------------
  39. llm_api_key: str = Field("", description="LLM API Key (defaults to openai_api_key)")
  40. llm_base_url: str = Field("https://api.deepseek.com", description="OpenAI-compatible Base URL")
  41. llm_model: str = Field("deepseek-v4-flash", description="Default model name, e.g., deepseek-v4-flash, deepseek-v4-pro")
  42. llm_temperature: float = Field(0.0, description="LLM sampling temperature")
  43. llm_timeout_seconds: int = Field(60, description="LLM request timeout in seconds")
  44. model_config = SettingsConfigDict(
  45. env_file=".env",
  46. env_file_encoding="utf-8",
  47. case_sensitive=False,
  48. extra="ignore",
  49. )
  50. @lru_cache
  51. def get_settings() -> Settings:
  52. """
  53. 避免多次实例化 Settings(FastAPI 官方推荐)
  54. """
  55. return Settings()
  56. settings = get_settings()