config.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """配置管理模块,从.env文件加载环境变量"""
  2. import os
  3. from dotenv import load_dotenv
  4. # 加载.env文件中的环境变量
  5. load_dotenv()
  6. class ModelConfig:
  7. """模型配置类"""
  8. @staticmethod
  9. def get_model_provider() -> str:
  10. """获取模型提供商"""
  11. return os.getenv("MODEL_PROVIDER", "openai")
  12. @staticmethod
  13. def get_model_name() -> str:
  14. """获取模型名称"""
  15. return os.getenv("MODEL_NAME", "Qwen/Qwen3-VL-8B-Instruct")
  16. @staticmethod
  17. def get_embedding_model_name() -> str:
  18. """获取模型名称"""
  19. return os.getenv("EMBEDDING_MODEL_NAME", "Qwen/Qwen3-Embedding-0.6B")
  20. @staticmethod
  21. def get_base_url() -> str:
  22. """获取模型API基础URL"""
  23. return os.getenv("BASE_URL", "https://api.openai.com/v1")
  24. @staticmethod
  25. def get_api_key() -> str:
  26. """获取模型API密钥"""
  27. return os.getenv("API_KEY", "")
  28. @staticmethod
  29. def get_model_config() -> dict:
  30. """获取完整的模型配置"""
  31. return {
  32. "model_provider": ModelConfig.get_model_provider(),
  33. "model": ModelConfig.get_model_name(),
  34. "base_url": ModelConfig.get_base_url(),
  35. "api_key": ModelConfig.get_api_key()
  36. }
  37. @staticmethod
  38. def get_embedding_model_config() -> dict:
  39. """获取完整的模型配置"""
  40. return {
  41. "model_provider": ModelConfig.get_model_provider(),
  42. "model": ModelConfig.get_embedding_model_name(),
  43. "base_url": ModelConfig.get_base_url(),
  44. "api_key": ModelConfig.get_api_key()
  45. }
  46. @staticmethod
  47. def get_multimodal_embedding_model_name() -> str:
  48. """获取多模态嵌入模型名称"""
  49. return os.getenv("MULTIMODAL_EMBEDDING_MODEL_NAME", "qwen2.5-vl-embedding")
  50. @staticmethod
  51. def get_dashscope_api_key() -> str:
  52. """获取DASHSCOPE API密钥"""
  53. return os.getenv("DASHSCOPE", "")
  54. # RAGFLOW配置
  55. @staticmethod
  56. def get_ragflow_api_url() -> str:
  57. """获取RAGFLOW API基础URL"""
  58. return os.getenv("RAGFLOW_API_URL", "http://192.168.16.134/")
  59. @staticmethod
  60. def get_ragflow_api_key() -> str:
  61. """获取RAGFLOW API密钥"""
  62. return os.getenv("RAGFLOW_API_KEY", "")
  63. @staticmethod
  64. def get_dataset_id() -> str:
  65. """获取数据集ID"""
  66. return os.getenv("DATASET_ID", "")
  67. class AppConfig:
  68. """应用配置类"""
  69. @staticmethod
  70. def get_log_level() -> str:
  71. """获取日志级别"""
  72. return os.getenv("LOG_LEVEL", "INFO")
  73. class VectorDBConfig:
  74. """向量数据库配置类"""
  75. @staticmethod
  76. def get_vector_db_type() -> str:
  77. """获取向量数据库类型"""
  78. return os.getenv("VECTOR_DB_TYPE", "es")
  79. @staticmethod
  80. def get_infinity_host() -> str:
  81. """获取Infinity向量数据库主机"""
  82. return os.getenv("INFINITY_HOST", "localhost")
  83. @staticmethod
  84. def get_infinity_port() -> int:
  85. """获取Infinity向量数据库端口"""
  86. return int(os.getenv("INFINITY_PORT", "23810"))
  87. @staticmethod
  88. def get_infinity_user() -> str:
  89. """获取Infinity向量数据库用户名"""
  90. return os.getenv("INFINITY_USER", "admin")
  91. @staticmethod
  92. def get_infinity_password() -> str:
  93. """获取Infinity向量数据库密码"""
  94. return os.getenv("INFINITY_PASSWORD", "admin")
  95. # 导出配置实例
  96. model_config = ModelConfig.get_model_config()
  97. app_config = {
  98. "log_level": AppConfig.get_log_level()
  99. }
  100. vector_db_config = {
  101. "type": VectorDBConfig.get_vector_db_type(),
  102. "infinity": {
  103. "host": VectorDBConfig.get_infinity_host(),
  104. "port": VectorDBConfig.get_infinity_port(),
  105. "user": VectorDBConfig.get_infinity_user(),
  106. "password": VectorDBConfig.get_infinity_password()
  107. }
  108. }