mirror of https://github.com/Mai-with-u/MaiBot.git
Update lpmm_get_knowledge.py
parent
fd35f17ade
commit
91cbe9f0fa
|
|
@ -43,17 +43,17 @@ class SearchKnowledgeFromLPMMTool(BaseTool):
|
||||||
knowledge_info = qa_manager.get_knowledge(query)
|
knowledge_info = qa_manager.get_knowledge(query)
|
||||||
logger.debug(f"知识库查询结果: {knowledge_info}")
|
logger.debug(f"知识库查询结果: {knowledge_info}")
|
||||||
if knowledge_info:
|
if knowledge_info:
|
||||||
content = f"You know this knowledge: {knowledge_info}"
|
content = f"你知道这些知识: {knowledge_info}"
|
||||||
else:
|
else:
|
||||||
content = f"You don't know much about {query}"
|
content = f"你不太了解有关{query}的知识"
|
||||||
return {"type": "lpmm_knowledge", "id": query, "content": content}
|
return {"type": "lpmm_knowledge", "id": query, "content": content}
|
||||||
# If embedding retrieval fails
|
# 如果获取嵌入失败
|
||||||
return {"type": "info", "id": query, "content": f"Unable to get embedding vector for '{query}', your lpmm knowledge base failed"}
|
return {"type": "info", "id": query, "content": f"无法获取关于'{query}'的嵌入向量,你lpmm知识库炸了"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"知识库搜索工具执行失败: {str(e)}")
|
logger.error(f"知识库搜索工具执行失败: {str(e)}")
|
||||||
# 在其他异常情况下,确保 id 仍然是 query (如果它被定义了)
|
# 在其他异常情况下,确保 id 仍然是 query (如果它被定义了)
|
||||||
query_id = query if "query" in locals() else "unknown_query"
|
query_id = query if "query" in locals() else "unknown_query"
|
||||||
return {"type": "info", "id": query_id, "content": f"lpmm knowledge base search failed: {str(e)}"}
|
return {"type": "info", "id": query_id, "content": f"lpmm知识库搜索失败,炸了: {str(e)}"}
|
||||||
|
|
||||||
# def get_info_from_db(
|
# def get_info_from_db(
|
||||||
# self, query_embedding: list, limit: int = 1, threshold: float = 0.5, return_raw: bool = False
|
# self, query_embedding: list, limit: int = 1, threshold: float = 0.5, return_raw: bool = False
|
||||||
|
|
@ -139,18 +139,18 @@ class SearchKnowledgeFromLPMMTool(BaseTool):
|
||||||
def _format_results(self, results: list) -> str:
|
def _format_results(self, results: list) -> str:
|
||||||
"""格式化结果"""
|
"""格式化结果"""
|
||||||
if not results:
|
if not results:
|
||||||
return "No relevant knowledge found."
|
return "未找到相关知识。"
|
||||||
|
|
||||||
formatted_string = "I found some relevant knowledge:\n"
|
formatted_string = "我找到了一些相关知识:\n"
|
||||||
for i, result in enumerate(results):
|
for i, result in enumerate(results):
|
||||||
# chunk_id = result.get("chunk_id")
|
# chunk_id = result.get("chunk_id")
|
||||||
text = result.get("text", "")
|
text = result.get("text", "")
|
||||||
source = result.get("source", "Unknown source")
|
source = result.get("source", "未知来源")
|
||||||
source_type = result.get("source_type", "Unknown type")
|
source_type = result.get("source_type", "未知类型")
|
||||||
similarity = result.get("similarity", 0.0)
|
similarity = result.get("similarity", 0.0)
|
||||||
|
|
||||||
formatted_string += (
|
formatted_string += (
|
||||||
f"{i + 1}. (Similarity: {similarity:.2f}) Type: {source_type}, Source: {source} \nContent fragment: {text}\n\n"
|
f"{i + 1}. (相似度: {similarity:.2f}) 类型: {source_type}, 来源: {source} \n内容片段: {text}\n\n"
|
||||||
)
|
)
|
||||||
# 暂时去掉chunk_id
|
# 暂时去掉chunk_id
|
||||||
# formatted_string += f"{i + 1}. (相似度: {similarity:.2f}) 类型: {source_type}, 来源: {source}, Chunk ID: {chunk_id} \n内容片段: {text}\n\n"
|
# formatted_string += f"{i + 1}. (相似度: {similarity:.2f}) 类型: {source_type}, 来源: {source}, Chunk ID: {chunk_id} \n内容片段: {text}\n\n"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue