config.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from pydantic_settings import BaseSettings
  2. from functools import lru_cache
  3. from pathlib import Path
  4. import os
  5. def _find_env_file():
  6. candidates = [Path.cwd() / ".env", Path(__file__).resolve().parent.parent.parent.parent / ".env"]
  7. for p in candidates:
  8. if p.exists():
  9. return str(p)
  10. return ".env"
  11. class Settings(BaseSettings):
  12. app_name: str = "PharmacopoeiaAI"
  13. app_env: str = "development"
  14. app_debug: bool = True
  15. secret_key: str = "change-me"
  16. api_prefix: str = "/api/v1"
  17. postgres_host: str = "localhost"
  18. postgres_port: int = 5432
  19. postgres_db: str = "pharmacopoeia"
  20. postgres_user: str = "postgres"
  21. postgres_password: str = "postgres"
  22. redis_host: str = "localhost"
  23. redis_port: int = 6379
  24. redis_password: str = ""
  25. redis_db: int = 0
  26. milvus_host: str = "localhost"
  27. milvus_port: int = 19530
  28. milvus_collection: str = "drug_entries"
  29. llm_provider: str = "qwen"
  30. qwen_api_key: str = ""
  31. qwen_base_url: str = "https://dashscope.aliyuncs.com/compatible-mode/v1"
  32. qwen_model: str = "qwen-max"
  33. qwen_max_tokens: int = 4096
  34. qwen_temperature: float = 0.1
  35. qwen_vl_model: str = "qwen3.6-flash" # 视觉模型(图片分析+OCR)
  36. enable_web_search: bool = True # 是否启用 Qwen 联网搜索
  37. qwen_local_base_url: str = "http://localhost:8000/v1"
  38. qwen_local_model: str = "Qwen3-35B-A3B"
  39. embedding_model: str = "BAAI/bge-m3"
  40. embedding_dim: int = 1024
  41. embedding_device: str = "cpu"
  42. reranker_model: str = "BAAI/bge-reranker-v2-m3"
  43. wechat_appid: str = ""
  44. wechat_secret: str = ""
  45. rate_limit_per_minute: int = 60
  46. rate_limit_per_hour: int = 1000
  47. log_level: str = "INFO"
  48. log_file: str = "logs/app.log"
  49. @property
  50. def database_url(self) -> str:
  51. return (
  52. f"postgresql+asyncpg://{self.postgres_user}:{self.postgres_password}"
  53. f"@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}"
  54. )
  55. @property
  56. def database_url_sync(self) -> str:
  57. return (
  58. f"postgresql://{self.postgres_user}:{self.postgres_password}"
  59. f"@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}"
  60. )
  61. model_config = {"env_file": _find_env_file(), "case_sensitive": False, "extra": "ignore"}
  62. @lru_cache
  63. def get_settings() -> Settings:
  64. return Settings()