mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 20:29:19 +08:00
feat: 长期记忆改为摘要检索并由模型按需载入
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""Agent 工具面:本地 builtin;MCP 传输在 infrastructure.mcp。"""
|
||||
"""Agent 工具面:本地 builtin / 记忆;MCP 传输在 infrastructure.mcp。"""
|
||||
|
||||
from agent.tools.builtin import builtin_tools
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
"""长期记忆工具:摘要检索 + 按需载入正文(绑定当前租户用户)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
from langchain_core.tools import BaseTool, tool
|
||||
|
||||
from application.memory_service import MemoryService
|
||||
from domain.models import Actor, MemoryKind
|
||||
|
||||
def memory_tools(svc: MemoryService, actor: Actor) -> list[BaseTool]:
|
||||
"""闭包绑定 company/user,供本轮 LLM 调用。"""
|
||||
|
||||
@tool
|
||||
async def memory_search(query: str, limit: int = 5) -> str:
|
||||
"""按关键词搜索当前用户的长期记忆摘要(不含正文)。
|
||||
|
||||
需要用户偏好、称呼、事实或历史约定时先调用本工具。
|
||||
命中后用 memory_load 按 id 载入正文再引用;禁止编造未载入的内容。
|
||||
"""
|
||||
rows = await svc.search(actor, query or "", limit=limit)
|
||||
return json.dumps({"items": rows}, ensure_ascii=False)
|
||||
|
||||
@tool
|
||||
async def memory_load(ids: list[str]) -> str:
|
||||
"""按记忆 id 载入完整正文;单次最多 5 条。
|
||||
|
||||
ids 来自 memory_search 的返回。仅对已载入内容作答。
|
||||
"""
|
||||
rows = await svc.load(actor, ids or [])
|
||||
return json.dumps({"items": rows}, ensure_ascii=False)
|
||||
|
||||
@tool
|
||||
async def memory_save(text: str, kind: str = "fact") -> str:
|
||||
"""将用户明确要求记住的内容写入长期记忆(跨会话有效)。
|
||||
|
||||
用户说「记住…」时也可由系统自动写入;本工具用于你主动落库。
|
||||
kind 可选:preference / fact / entity。
|
||||
"""
|
||||
kind_map = {
|
||||
"preference": MemoryKind.PREFERENCE,
|
||||
"fact": MemoryKind.FACT,
|
||||
"entity": MemoryKind.ENTITY,
|
||||
"summary": MemoryKind.SUMMARY,
|
||||
}
|
||||
mk = kind_map.get((kind or "fact").lower(), MemoryKind.FACT)
|
||||
try:
|
||||
saved = await svc.save(actor, text, kind=mk)
|
||||
except ValueError as e:
|
||||
return json.dumps({"ok": False, "message": str(e)}, ensure_ascii=False)
|
||||
return json.dumps({"ok": True, **saved}, ensure_ascii=False)
|
||||
|
||||
return [memory_search, memory_load, memory_save]
|
||||
Reference in New Issue
Block a user