utils.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. """
  2. 配置相关的工具函数
  3. 用于处理不同模型类型的配置选择逻辑
  4. """
  5. def get_current_embedding_config():
  6. """
  7. 根据EMBEDDING_MODEL_TYPE返回当前应该使用的embedding配置
  8. Returns:
  9. dict: 当前的embedding配置字典
  10. Raises:
  11. ImportError: 如果无法导入app_config
  12. ValueError: 如果EMBEDDING_MODEL_TYPE值无效
  13. """
  14. try:
  15. import app_config
  16. except ImportError:
  17. raise ImportError("无法导入 app_config.py,请确保该文件存在")
  18. if app_config.EMBEDDING_MODEL_TYPE == "ollama":
  19. return app_config.OLLAMA_EMBEDDING_CONFIG
  20. elif app_config.EMBEDDING_MODEL_TYPE == "api":
  21. return app_config.API_EMBEDDING_CONFIG
  22. else:
  23. raise ValueError(f"不支持的EMBEDDING_MODEL_TYPE: {app_config.EMBEDDING_MODEL_TYPE}")
  24. def get_current_llm_config():
  25. """
  26. 根据LLM_MODEL_TYPE和API_LLM_MODEL返回当前应该使用的LLM配置
  27. Returns:
  28. dict: 当前的LLM配置字典
  29. Raises:
  30. ImportError: 如果无法导入app_config
  31. ValueError: 如果LLM_MODEL_TYPE或API_LLM_MODEL值无效
  32. """
  33. try:
  34. import app_config
  35. except ImportError:
  36. raise ImportError("无法导入 app_config.py,请确保该文件存在")
  37. if app_config.LLM_MODEL_TYPE == "ollama":
  38. return app_config.OLLAMA_LLM_CONFIG
  39. elif app_config.LLM_MODEL_TYPE == "api":
  40. if app_config.API_LLM_MODEL == "qianwen":
  41. return app_config.API_QIANWEN_CONFIG
  42. elif app_config.API_LLM_MODEL == "deepseek":
  43. return app_config.API_DEEPSEEK_CONFIG
  44. else:
  45. raise ValueError(f"不支持的API_LLM_MODEL: {app_config.API_LLM_MODEL}")
  46. else:
  47. raise ValueError(f"不支持的LLM_MODEL_TYPE: {app_config.LLM_MODEL_TYPE}")
  48. def get_current_vector_db_config():
  49. """
  50. 根据VECTOR_DB_TYPE返回当前应该使用的向量数据库配置
  51. Returns:
  52. dict: 当前的向量数据库配置字典
  53. Raises:
  54. ImportError: 如果无法导入app_config
  55. ValueError: 如果VECTOR_DB_TYPE值无效
  56. """
  57. try:
  58. import app_config
  59. except ImportError:
  60. raise ImportError("无法导入 app_config.py,请确保该文件存在")
  61. if app_config.VECTOR_DB_TYPE == "pgvector":
  62. return app_config.PGVECTOR_CONFIG
  63. elif app_config.VECTOR_DB_TYPE == "chromadb":
  64. # ChromaDB通常不需要复杂配置,返回项目根目录路径
  65. import os
  66. return {"path": os.path.dirname(os.path.dirname(os.path.abspath(__file__)))}
  67. else:
  68. raise ValueError(f"不支持的VECTOR_DB_TYPE: {app_config.VECTOR_DB_TYPE}")
  69. def get_current_model_info():
  70. """
  71. 获取当前配置的模型信息摘要
  72. Returns:
  73. dict: 包含当前所有模型配置信息的字典
  74. Raises:
  75. ImportError: 如果无法导入app_config
  76. """
  77. try:
  78. import app_config
  79. except ImportError:
  80. raise ImportError("无法导入 app_config.py,请确保该文件存在")
  81. # 获取LLM模型名称
  82. if app_config.LLM_MODEL_TYPE == "ollama":
  83. llm_model_name = app_config.OLLAMA_LLM_CONFIG.get("model", "unknown")
  84. else:
  85. llm_model_name = app_config.API_LLM_MODEL
  86. # 获取Embedding模型名称
  87. if app_config.EMBEDDING_MODEL_TYPE == "ollama":
  88. embedding_model_name = app_config.OLLAMA_EMBEDDING_CONFIG.get("model_name", "unknown")
  89. else:
  90. embedding_model_name = app_config.API_EMBEDDING_CONFIG.get("model_name", "unknown")
  91. return {
  92. "llm_type": app_config.LLM_MODEL_TYPE,
  93. "llm_model": llm_model_name,
  94. "embedding_type": app_config.EMBEDDING_MODEL_TYPE,
  95. "embedding_model": embedding_model_name,
  96. "vector_db": app_config.VECTOR_DB_TYPE
  97. }
  98. def is_using_ollama_llm():
  99. """
  100. 检查当前是否使用Ollama作为LLM提供商
  101. Returns:
  102. bool: 如果使用Ollama LLM返回True,否则返回False
  103. """
  104. try:
  105. import app_config
  106. return app_config.LLM_MODEL_TYPE == "ollama"
  107. except ImportError:
  108. return False
  109. def is_using_ollama_embedding():
  110. """
  111. 检查当前是否使用Ollama作为Embedding提供商
  112. Returns:
  113. bool: 如果使用Ollama Embedding返回True,否则返回False
  114. """
  115. try:
  116. import app_config
  117. return app_config.EMBEDDING_MODEL_TYPE == "ollama"
  118. except ImportError:
  119. return False
  120. def is_using_api_llm():
  121. """
  122. 检查当前是否使用API作为LLM提供商
  123. Returns:
  124. bool: 如果使用API LLM返回True,否则返回False
  125. """
  126. try:
  127. import app_config
  128. return app_config.LLM_MODEL_TYPE == "api"
  129. except ImportError:
  130. return False
  131. def is_using_api_embedding():
  132. """
  133. 检查当前是否使用API作为Embedding提供商
  134. Returns:
  135. bool: 如果使用API Embedding返回True,否则返回False
  136. """
  137. try:
  138. import app_config
  139. return app_config.EMBEDDING_MODEL_TYPE == "api"
  140. except ImportError:
  141. return False
  142. def print_current_config():
  143. """
  144. 打印当前的配置信息,用于调试和确认配置
  145. """
  146. try:
  147. model_info = get_current_model_info()
  148. print("=== 当前模型配置 ===")
  149. print(f"LLM提供商: {model_info['llm_type']}")
  150. print(f"LLM模型: {model_info['llm_model']}")
  151. print(f"Embedding提供商: {model_info['embedding_type']}")
  152. print(f"Embedding模型: {model_info['embedding_model']}")
  153. print(f"向量数据库: {model_info['vector_db']}")
  154. print("==================")
  155. except Exception as e:
  156. print(f"无法获取配置信息: {e}")