config.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Application configuration"""
  2. import json
  3. import os
  4. from pathlib import Path
  5. from typing import Optional
  6. from dotenv import load_dotenv
  7. # Load .env file at module import time
  8. load_dotenv(dotenv_path=Path(__file__).parent / ".env")
  9. class Settings:
  10. """Application settings"""
  11. # ── Third-party service config (loaded from .env) ────────────────────────
  12. # LLM (ModelScope / OpenAI-compatible)
  13. LLM_MODEL_ID: str = os.getenv("LLM_MODEL_ID", "qwen-flash")
  14. LLM_API_KEY: Optional[str] = os.getenv("LLM_API_KEY", "")
  15. LLM_BASE_URL: str = os.getenv("LLM_BASE_URL", "https://api-inference.modelscope.cn/v1/")
  16. LLM_TIMEOUT: int = int(os.getenv("LLM_TIMEOUT", "30"))
  17. # Tavily search API
  18. TAVILY_API_KEY: Optional[str] = os.getenv("TAVILY_API_KEY", "")
  19. # ── Game config (code-level defaults, NOT stored in .env) ────────────────
  20. MAX_QUESTIONS: int = 10 # max questions per game
  21. MAX_HINTS: int = 3 # max hints per game
  22. # ── Server config (code-level defaults, NOT stored in .env) ─────────────
  23. HOST: str = "0.0.0.0"
  24. PORT: int = 8000
  25. @classmethod
  26. def validate(cls):
  27. """Validate critical config values"""
  28. if not cls.LLM_API_KEY:
  29. print("⚠️ Warning: LLM_API_KEY is not set")
  30. print(" Please configure LLM_API_KEY in the .env file")
  31. return False
  32. print(f"✅ LLM config:")
  33. print(f" Model : {cls.LLM_MODEL_ID}")
  34. print(f" Base URL: {cls.LLM_BASE_URL}")
  35. return True
  36. _settings_instance: Optional[Settings] = None
  37. def get_config() -> Settings:
  38. """Return the singleton application settings instance"""
  39. global _settings_instance
  40. if _settings_instance is None:
  41. _settings_instance = Settings()
  42. return _settings_instance