fix:优化记忆提取和聊天压缩

This commit is contained in:
SengokuCola
2025-11-10 12:27:54 +08:00
parent 10cd2474af
commit 71a2a4282b
5 changed files with 212 additions and 139 deletions

View File

@@ -12,8 +12,7 @@ logger = get_logger("memory_retrieval_tools")
async def query_jargon(
keyword: str,
chat_id: str,
fuzzy: bool = False,
search_all: bool = False
fuzzy: bool = False
) -> str:
"""根据关键词在jargon库中查询
@@ -21,7 +20,6 @@ async def query_jargon(
keyword: 关键词(黑话/俚语/缩写)
chat_id: 聊天ID
fuzzy: 是否使用模糊搜索默认False精确匹配
search_all: 是否搜索全库不限chat_id默认False仅搜索当前会话或global
Returns:
str: 查询结果
@@ -31,11 +29,10 @@ async def query_jargon(
if not content:
return "关键词为空"
# 根据参数执行搜索
search_chat_id = None if search_all else chat_id
# 执行搜索(仅搜索当前会话或全局)
results = search_jargon(
keyword=content,
chat_id=search_chat_id,
chat_id=chat_id,
limit=1,
case_sensitive=False,
fuzzy=fuzzy
@@ -46,15 +43,13 @@ async def query_jargon(
translation = result.get("translation", "").strip()
meaning = result.get("meaning", "").strip()
search_type = "模糊搜索" if fuzzy else "精确匹配"
search_scope = "全库" if search_all else "当前会话或全局"
output = f"{content}可能为黑话或者网络简写,翻译为:{translation},含义为:{meaning}"
logger.info(f"在jargon库中找到匹配{search_scope}{search_type}: {content}")
output = f'"{content}可能为黑话或者网络简写,翻译为:{translation},含义为:{meaning}"'
logger.info(f"在jargon库中找到匹配当前会话或全局{search_type}: {content}")
return output
# 未命中
search_type = "模糊搜索" if fuzzy else "精确匹配"
search_scope = "全库" if search_all else "当前会话或全局"
logger.info(f"在jargon库中未找到匹配{search_scope}{search_type}: {content}")
logger.info(f"在jargon库中未找到匹配当前会话或全局{search_type}: {content}")
return f"未在jargon库中找到'{content}'的解释"
except Exception as e:
@@ -66,7 +61,7 @@ def register_tool():
"""注册工具"""
register_memory_retrieval_tool(
name="query_jargon",
description="根据关键词在jargon库中查询黑话/俚语/缩写的含义。支持大小写不敏感搜索和模糊搜索。默认优先搜索当前会话或全局jargon,可以设置为搜索全库",
description="根据关键词在jargon库中查询黑话/俚语/缩写的含义。支持大小写不敏感搜索和模糊搜索。搜索当前会话或全局jargon。",
parameters=[
{
"name": "keyword",
@@ -79,12 +74,6 @@ def register_tool():
"type": "boolean",
"description": "是否使用模糊搜索部分匹配默认False精确匹配。当精确匹配找不到时可以尝试使用模糊搜索。",
"required": False
},
{
"name": "search_all",
"type": "boolean",
"description": "是否搜索全库不限chat_id默认False仅搜索当前会话或global的jargon。当在当前会话中找不到时可以尝试搜索全库。",
"required": False
}
],
execute_func=query_jargon