vanna_combinations.py 6.3 KB

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