feat:优化工具调用的得到的信息调用和缓存
This commit is contained in:
@@ -9,7 +9,7 @@ class CompareNumbersTool(BaseTool):
|
||||
"""比较两个数大小的工具"""
|
||||
|
||||
name = "compare_numbers"
|
||||
description = "比较两个数的大小,返回较大的数"
|
||||
description = "使用工具 比较两个数的大小,返回较大的数"
|
||||
parameters = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -39,10 +39,10 @@ class CompareNumbersTool(BaseTool):
|
||||
else:
|
||||
result = f"{num1} 等于 {num2}"
|
||||
|
||||
return {"name": self.name, "content": result}
|
||||
return {"type": "comparison_result", "id": f"{num1}_vs_{num2}", "content": result}
|
||||
except Exception as e:
|
||||
logger.error(f"比较数字失败: {str(e)}")
|
||||
return {"name": self.name, "content": f"比较数字失败: {str(e)}"}
|
||||
return {"type": "info", "id": f"{num1}_vs_{num2}", "content": f"比较数字失败,炸了: {str(e)}"}
|
||||
|
||||
|
||||
# 注册工具
|
||||
|
||||
@@ -11,7 +11,7 @@ class SearchKnowledgeTool(BaseTool):
|
||||
"""从知识库中搜索相关信息的工具"""
|
||||
|
||||
name = "search_knowledge"
|
||||
description = "从知识库中搜索相关信息"
|
||||
description = "使用工具从知识库中搜索相关信息"
|
||||
parameters = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -42,11 +42,11 @@ class SearchKnowledgeTool(BaseTool):
|
||||
content = f"你知道这些知识: {knowledge_info}"
|
||||
else:
|
||||
content = f"你不太了解有关{query}的知识"
|
||||
return {"name": "search_knowledge", "content": content}
|
||||
return {"name": "search_knowledge", "content": f"无法获取关于'{query}'的嵌入向量"}
|
||||
return {"type": "knowledge", "id": query, "content": content}
|
||||
return {"type": "info", "id": query, "content": f"无法获取关于'{query}'的嵌入向量,你知识库炸了"}
|
||||
except Exception as e:
|
||||
logger.error(f"知识库搜索工具执行失败: {str(e)}")
|
||||
return {"name": "search_knowledge", "content": f"知识库搜索失败: {str(e)}"}
|
||||
return {"type": "info", "id": query, "content": f"知识库搜索失败,炸了: {str(e)}"}
|
||||
|
||||
@staticmethod
|
||||
def get_info_from_db(
|
||||
|
||||
@@ -10,7 +10,7 @@ class GetMemoryTool(BaseTool):
|
||||
"""从记忆系统中获取相关记忆的工具"""
|
||||
|
||||
name = "get_memory"
|
||||
description = "从记忆系统中获取相关记忆"
|
||||
description = "使用工具从记忆系统中获取相关记忆"
|
||||
parameters = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -53,10 +53,11 @@ class GetMemoryTool(BaseTool):
|
||||
else:
|
||||
content = f"{topic}的记忆,你记不太清"
|
||||
|
||||
return {"name": "get_memory", "content": content}
|
||||
return {"type": "memory", "id": topic_list, "content": content}
|
||||
except Exception as e:
|
||||
logger.error(f"记忆获取工具执行失败: {str(e)}")
|
||||
return {"name": "get_memory", "content": f"记忆获取失败: {str(e)}"}
|
||||
# 在失败时也保持格式一致,但id可能不适用或设为None/Error
|
||||
return {"type": "memory_error", "id": topic_list, "content": f"记忆获取失败: {str(e)}"}
|
||||
|
||||
|
||||
# 注册工具
|
||||
|
||||
@@ -2,6 +2,7 @@ from src.do_tool.tool_can_use.base_tool import BaseTool
|
||||
from src.common.logger_manager import get_logger
|
||||
from typing import Dict, Any
|
||||
from datetime import datetime
|
||||
import time
|
||||
|
||||
logger = get_logger("get_time_date")
|
||||
|
||||
@@ -32,6 +33,7 @@ class GetCurrentDateTimeTool(BaseTool):
|
||||
current_weekday = datetime.now().strftime("%A")
|
||||
|
||||
return {
|
||||
"name": "get_current_date_time",
|
||||
"type": "time_info",
|
||||
"id": f"time_info_{time.time()}",
|
||||
"content": f"当前时间: {current_time}, 日期: {current_date}, 年份: {current_year}, 星期: {current_weekday}",
|
||||
}
|
||||
|
||||
@@ -46,11 +46,14 @@ class SearchKnowledgeFromLPMMTool(BaseTool):
|
||||
content = f"你知道这些知识: {knowledge_info}"
|
||||
else:
|
||||
content = f"你不太了解有关{query}的知识"
|
||||
return {"name": "search_knowledge", "content": content}
|
||||
return {"name": "search_knowledge", "content": f"无法获取关于'{query}'的嵌入向量"}
|
||||
return {"type": "lpmm_knowledge", "id": query, "content": content}
|
||||
# 如果获取嵌入失败
|
||||
return {"type": "info", "id": query, "content": f"无法获取关于'{query}'的嵌入向量,你lpmm知识库炸了"}
|
||||
except Exception as e:
|
||||
logger.error(f"知识库搜索工具执行失败: {str(e)}")
|
||||
return {"name": "search_knowledge", "content": f"知识库搜索失败: {str(e)}"}
|
||||
# 在其他异常情况下,确保 id 仍然是 query (如果它被定义了)
|
||||
query_id = query if 'query' in locals() else 'unknown_query'
|
||||
return {"type": "info", "id": query_id, "content": f"lpmm知识库搜索失败,炸了: {str(e)}"}
|
||||
|
||||
# def get_info_from_db(
|
||||
# self, query_embedding: list, limit: int = 1, threshold: float = 0.5, return_raw: bool = False
|
||||
@@ -133,6 +136,25 @@ class SearchKnowledgeFromLPMMTool(BaseTool):
|
||||
# # 返回所有找到的内容,用换行分隔
|
||||
# return "\n".join(str(result["content"]) for result in results)
|
||||
|
||||
def _format_results(self, results: list) -> str:
|
||||
"""格式化结果"""
|
||||
if not results:
|
||||
return "未找到相关知识。"
|
||||
|
||||
formatted_string = "我找到了一些相关知识:\n"
|
||||
for i, result in enumerate(results):
|
||||
chunk_id = result.get("chunk_id")
|
||||
text = result.get("text", "")
|
||||
source = result.get("source", "未知来源")
|
||||
source_type = result.get("source_type", "未知类型")
|
||||
similarity = result.get("similarity", 0.0)
|
||||
|
||||
formatted_string += f"{i + 1}. (相似度: {similarity:.2f}) 类型: {source_type}, 来源: {source} \n内容片段: {text}\n\n"
|
||||
# 暂时去掉chunk_id
|
||||
# formatted_string += f"{i + 1}. (相似度: {similarity:.2f}) 类型: {source_type}, 来源: {source}, Chunk ID: {chunk_id} \n内容片段: {text}\n\n"
|
||||
|
||||
return formatted_string
|
||||
|
||||
|
||||
# 注册工具
|
||||
# register_tool(SearchKnowledgeTool)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from src.do_tool.tool_can_use.base_tool import BaseTool, register_tool
|
||||
from src.plugins.person_info.person_info import person_info_manager
|
||||
from src.common.logger_manager import get_logger
|
||||
from src.plugins.person_info.relationship_manager import relationship_manager
|
||||
import json
|
||||
import time
|
||||
|
||||
logger = get_logger("rename_person_tool")
|
||||
|
||||
@@ -82,10 +84,10 @@ class RenamePersonTool(BaseTool):
|
||||
new_name = result["nickname"]
|
||||
reason = result.get("reason", "未提供理由")
|
||||
logger.info(f"成功为用户 {person_id} 取了新昵称: {new_name}")
|
||||
return {
|
||||
"name": self.name,
|
||||
"content": f"好的,我已经给 '{person_name_to_find}' 取了新昵称:'{new_name}'。因为:{reason}"
|
||||
}
|
||||
|
||||
content = f"已成功将用户 {person_name_to_find} 的备注名更新为 {new_name}"
|
||||
logger.info(content)
|
||||
return {"type": "info", "id": f"rename_success_{time.time()}", "content": content}
|
||||
else:
|
||||
logger.warning(f"为用户 {person_id} 调用 qv_person_name 后未能成功获取新昵称。")
|
||||
# 尝试从内存中获取可能已经更新的名字
|
||||
@@ -102,11 +104,9 @@ class RenamePersonTool(BaseTool):
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"执行 rename_person 工具时出错: {e}", exc_info=True)
|
||||
return {
|
||||
"name": self.name,
|
||||
"content": f"执行重命名操作时遇到内部错误: {e}"
|
||||
}
|
||||
error_msg = f"重命名失败: {str(e)}"
|
||||
logger.error(error_msg, exc_info=True)
|
||||
return {"type": "info_error", "id": f"rename_error_{time.time()}", "content": error_msg}
|
||||
|
||||
# 注册工具
|
||||
register_tool(RenamePersonTool)
|
||||
Reference in New Issue
Block a user