config.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. """
  2. InnoCore AI 核心配置模块
  3. """
  4. from typing import Dict, List, Optional, Any
  5. from dataclasses import dataclass, field
  6. from enum import Enum
  7. import os
  8. from dotenv import load_dotenv
  9. load_dotenv()
  10. class LLMProvider(Enum):
  11. """LLM提供商枚举"""
  12. OPENAI = "openai"
  13. CLAUDE = "claude"
  14. MODELSCOPE = "modelscope" # 阿里云 ModelScope
  15. OLLAMA = "ollama" # 本地部署
  16. DASHSCOPE = "dashscope" # 阿里云灵积(推荐用于 Qwen 系列)
  17. class VectorDBType(Enum):
  18. """向量数据库类型枚举"""
  19. QDRANT = "qdrant"
  20. CHROMA = "chroma"
  21. PINECONE = "pinecone"
  22. @dataclass
  23. class LLMConfig:
  24. """LLM配置"""
  25. provider: LLMProvider = LLMProvider.OPENAI
  26. model_name: str = "gpt-3.5-turbo" # OpenAI: gpt-4, gpt-3.5-turbo, gpt-4-turbo-preview
  27. # DashScope: qwen-turbo, qwen-plus, qwen-max
  28. # ModelScope: qwen/Qwen2.5-7B-Instruct
  29. api_key: Optional[str] = None
  30. base_url: Optional[str] = None
  31. temperature: float = 0.7
  32. max_tokens: int = 4000
  33. timeout: int = 60
  34. @dataclass
  35. class VectorDBConfig:
  36. """向量数据库配置"""
  37. db_type: VectorDBType = VectorDBType.QDRANT
  38. host: str = "localhost"
  39. port: int = 6333
  40. api_key: Optional[str] = None
  41. collection_name_prefix: str = "innocore"
  42. embedding_model: str = "text-embedding-3-small"
  43. @dataclass
  44. class DatabaseConfig:
  45. """关系数据库配置"""
  46. host: str = "localhost"
  47. port: int = 5432
  48. database: str = "innocore_ai"
  49. username: str = "postgres"
  50. password: str = "password"
  51. pool_size: int = 10
  52. @dataclass
  53. class RedisConfig:
  54. """Redis配置"""
  55. host: str = "localhost"
  56. port: int = 6379
  57. db: int = 0
  58. password: Optional[str] = None
  59. max_connections: int = 20
  60. @dataclass
  61. class ExternalAPIConfig:
  62. """外部API配置"""
  63. crossref_api_key: Optional[str] = None
  64. google_scholar_api_key: Optional[str] = None
  65. serpapi_key: Optional[str] = None
  66. arxiv_base_url: str = "http://export.arxiv.org/api/query"
  67. ieee_base_url: str = "https://ieeexploreapi.ieee.org/api/v1"
  68. @dataclass
  69. class InnoCoreConfig:
  70. """InnoCore AI 主配置类"""
  71. # 基础配置
  72. app_name: str = "InnoCore AI"
  73. debug: bool = False
  74. log_level: str = "INFO"
  75. # LLM配置
  76. llm: LLMConfig = field(default_factory=LLMConfig)
  77. # 向量数据库配置
  78. vector_db: VectorDBConfig = field(default_factory=VectorDBConfig)
  79. # 关系数据库配置
  80. database: DatabaseConfig = field(default_factory=DatabaseConfig)
  81. # Redis配置
  82. redis: RedisConfig = field(default_factory=RedisConfig)
  83. # 外部API配置
  84. external_apis: ExternalAPIConfig = field(default_factory=ExternalAPIConfig)
  85. # Agent配置
  86. agent_max_steps: int = 5
  87. agent_timeout: int = 300
  88. concurrent_agents: int = 4
  89. # RAG配置
  90. retrieval_top_k: int = 5
  91. similarity_threshold: float = 0.7
  92. hybrid_search_weights: Dict[str, float] = field(default_factory=lambda: {
  93. "vector": 0.7,
  94. "keyword": 0.3
  95. })
  96. # 性能配置
  97. cache_ttl: int = 3600 # 缓存过期时间(秒)
  98. batch_size: int = 10
  99. max_concurrent_requests: int = 50
  100. def __post_init__(self):
  101. """初始化后处理"""
  102. # 从环境变量加载配置
  103. self.llm.api_key = self.llm.api_key or os.getenv("OPENAI_API_KEY")
  104. self.llm.base_url = self.llm.base_url or os.getenv("OPENAI_BASE_URL")
  105. # 从环境变量加载模型名称(如果设置了)
  106. env_model = os.getenv("OPENAI_MODEL") or os.getenv("LLM_MODEL")
  107. if env_model:
  108. self.llm.model_name = env_model
  109. self.database.password = self.database.password or os.getenv("DATABASE_PASSWORD")
  110. self.redis.password = self.redis.password or os.getenv("REDIS_PASSWORD")
  111. self.external_apis.crossref_api_key = self.external_apis.crossref_api_key or os.getenv("CROSSREF_API_KEY")
  112. self.external_apis.google_scholar_api_key = self.external_apis.google_scholar_api_key or os.getenv("GOOGLE_SCHOLAR_API_KEY")
  113. self.external_apis.serpapi_key = self.external_apis.serpapi_key or os.getenv("SERPAPI_KEY")
  114. self.debug = os.getenv("DEBUG", "false").lower() == "true"
  115. self.log_level = os.getenv("LOG_LEVEL", "INFO")
  116. # 全局配置实例
  117. config = InnoCoreConfig()
  118. def get_config() -> InnoCoreConfig:
  119. """获取全局配置实例"""
  120. return config
  121. def update_config(**kwargs) -> None:
  122. """更新配置"""
  123. global config
  124. for key, value in kwargs.items():
  125. if hasattr(config, key):
  126. setattr(config, key, value)