vanna_combinations.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. """
  2. Vanna LLM与向量数据库的组合类
  3. 统一管理所有LLM提供商与向量数据库的组合
  4. """
  5. from core.logging import get_app_logger
  6. # 初始化logger
  7. _logger = get_app_logger("VannaCombinations")
  8. # 向量数据库导入
  9. from vanna.chromadb import ChromaDB_VectorStore
  10. try:
  11. from custompgvector import PG_VectorStore
  12. except ImportError:
  13. _logger.warning("无法导入 PG_VectorStore,PGVector相关组合类将不可用")
  14. PG_VectorStore = None
  15. # LLM提供商导入 - 使用新的重构后的实现
  16. from customllm.qianwen_chat import QianWenChat
  17. from customllm.deepseek_chat import DeepSeekChat
  18. try:
  19. from customllm.ollama_chat import OllamaChat
  20. except ImportError:
  21. _logger.warning("无法导入 OllamaChat,Ollama相关组合类将不可用")
  22. OllamaChat = None
  23. # ===== API LLM + ChromaDB 组合 =====
  24. class QianWenChromaDB(ChromaDB_VectorStore, QianWenChat):
  25. """QianWen LLM + ChromaDB 向量数据库组合"""
  26. def __init__(self, config=None):
  27. ChromaDB_VectorStore.__init__(self, config=config)
  28. QianWenChat.__init__(self, config=config)
  29. class DeepSeekChromaDB(ChromaDB_VectorStore, DeepSeekChat):
  30. """DeepSeek LLM + ChromaDB 向量数据库组合"""
  31. def __init__(self, config=None):
  32. ChromaDB_VectorStore.__init__(self, config=config)
  33. DeepSeekChat.__init__(self, config=config)
  34. # ===== API LLM + PGVector 组合 =====
  35. if PG_VectorStore is not None:
  36. class QianWenPGVector(PG_VectorStore, QianWenChat):
  37. """QianWen LLM + PGVector 向量数据库组合"""
  38. def __init__(self, config=None):
  39. PG_VectorStore.__init__(self, config=config)
  40. QianWenChat.__init__(self, config=config)
  41. class DeepSeekPGVector(PG_VectorStore, DeepSeekChat):
  42. """DeepSeek LLM + PGVector 向量数据库组合"""
  43. def __init__(self, config=None):
  44. PG_VectorStore.__init__(self, config=config)
  45. DeepSeekChat.__init__(self, config=config)
  46. else:
  47. # 如果PG_VectorStore不可用,创建占位符类
  48. class QianWenPGVector:
  49. def __init__(self, config=None):
  50. raise ImportError("PG_VectorStore 不可用,无法创建 QianWenPGVector 实例")
  51. class DeepSeekPGVector:
  52. def __init__(self, config=None):
  53. raise ImportError("PG_VectorStore 不可用,无法创建 DeepSeekPGVector 实例")
  54. # ===== Ollama LLM + ChromaDB 组合 =====
  55. if OllamaChat is not None:
  56. class OllamaChromaDB(ChromaDB_VectorStore, OllamaChat):
  57. """Ollama LLM + ChromaDB 向量数据库组合"""
  58. def __init__(self, config=None):
  59. ChromaDB_VectorStore.__init__(self, config=config)
  60. OllamaChat.__init__(self, config=config)
  61. else:
  62. class OllamaChromaDB:
  63. def __init__(self, config=None):
  64. raise ImportError("OllamaChat 不可用,无法创建 OllamaChromaDB 实例")
  65. # ===== Ollama LLM + PGVector 组合 =====
  66. if OllamaChat is not None and PG_VectorStore is not None:
  67. class OllamaPGVector(PG_VectorStore, OllamaChat):
  68. """Ollama LLM + PGVector 向量数据库组合"""
  69. def __init__(self, config=None):
  70. PG_VectorStore.__init__(self, config=config)
  71. OllamaChat.__init__(self, config=config)
  72. else:
  73. class OllamaPGVector:
  74. def __init__(self, config=None):
  75. error_msg = []
  76. if OllamaChat is None:
  77. error_msg.append("OllamaChat 不可用")
  78. if PG_VectorStore is None:
  79. error_msg.append("PG_VectorStore 不可用")
  80. raise ImportError(f"{', '.join(error_msg)},无法创建 OllamaPGVector 实例")
  81. # ===== 组合类映射表 =====
  82. # LLM类型到类名的映射
  83. LLM_CLASS_MAP = {
  84. "qianwen": {
  85. "chromadb": QianWenChromaDB,
  86. "pgvector": QianWenPGVector,
  87. },
  88. "deepseek": {
  89. "chromadb": DeepSeekChromaDB,
  90. "pgvector": DeepSeekPGVector,
  91. },
  92. "ollama": {
  93. "chromadb": OllamaChromaDB,
  94. "pgvector": OllamaPGVector,
  95. }
  96. }
  97. def get_vanna_class(llm_type: str, vector_db_type: str):
  98. """
  99. 根据LLM类型和向量数据库类型获取对应的Vanna组合类
  100. Args:
  101. llm_type: LLM类型 ("qianwen", "deepseek", "ollama")
  102. vector_db_type: 向量数据库类型 ("chromadb", "pgvector")
  103. Returns:
  104. 对应的Vanna组合类
  105. Raises:
  106. ValueError: 如果不支持的组合类型
  107. """
  108. llm_type = llm_type.lower()
  109. vector_db_type = vector_db_type.lower()
  110. if llm_type not in LLM_CLASS_MAP:
  111. raise ValueError(f"不支持的LLM类型: {llm_type},支持的类型: {list(LLM_CLASS_MAP.keys())}")
  112. if vector_db_type not in LLM_CLASS_MAP[llm_type]:
  113. raise ValueError(f"不支持的向量数据库类型: {vector_db_type},支持的类型: {list(LLM_CLASS_MAP[llm_type].keys())}")
  114. return LLM_CLASS_MAP[llm_type][vector_db_type]
  115. def list_available_combinations():
  116. """
  117. 列出所有可用的LLM与向量数据库组合
  118. Returns:
  119. dict: 可用组合的字典
  120. """
  121. available = {}
  122. for llm_type, vector_dbs in LLM_CLASS_MAP.items():
  123. available[llm_type] = []
  124. for vector_db_type, cls in vector_dbs.items():
  125. try:
  126. # 尝试创建实例来检查是否可用
  127. cls(config={})
  128. available[llm_type].append(vector_db_type)
  129. except ImportError:
  130. # 如果导入错误,说明不可用
  131. continue
  132. except Exception:
  133. # 其他错误(如配置错误)仍然认为是可用的
  134. available[llm_type].append(vector_db_type)
  135. return available
  136. def print_available_combinations():
  137. """打印所有可用的组合"""
  138. _logger.info("可用的LLM与向量数据库组合:")
  139. _logger.info("=" * 40)
  140. combinations = list_available_combinations()
  141. for llm_type, vector_dbs in combinations.items():
  142. _logger.info(f"\n{llm_type.upper()} LLM:")
  143. for vector_db in vector_dbs:
  144. class_name = LLM_CLASS_MAP[llm_type][vector_db].__name__
  145. _logger.info(f" + {vector_db} -> {class_name}")
  146. if not any(combinations.values()):
  147. _logger.warning("没有可用的组合,请检查依赖是否正确安装")
  148. # ===== 向后兼容性支持 =====
  149. # 为了保持向后兼容,可以在这里添加别名
  150. # 例如:
  151. # VannaQwenChromaDB = QianWenChromaDB # 旧的命名风格