Skip to content

fix(api): 修复 get_entity 漏查低频实体与多信源提前返回 - #264

Open
muyuzhong wants to merge 1 commit into
Zleap-AI:mainfrom
muyuzhong:fix/get-entity-lookup
Open

muyuzhong wants to merge 1 commit into
Zleap-AI:mainfrom
muyuzhong:fix/get-entity-lookup

Conversation

@muyuzhong

Copy link
Copy Markdown

知识库导入的文档多了之后(抽取出的实体超过 200 个),在问答里问一些出现频次不高的专名/人物时,发现 Agent 调 get_entity 会返回“未找到该实体”;另外会话同时勾选多个知识库时,也只能拿到第一个命中库里的实体上下文,后面库里的同名实体信息没带出来。

顺着 get_entity 排查定位到两处原因:

  1. find_entity_context 先调 list_entities(..., limit=200) 取热度前 200 的实体再在内存里匹配名字,排在 200 名外的低频实体直接被截断了(若前 200 里碰巧有包含该词的高频子串实体,还会误命中子串)。
  2. tools/builtin.py 里的 GetEntityTool 遍历信源时命中第一个就直接 return 了,没有像 mcp/server.py 里的 get_entity 那样查完所有已选信源再汇总。

改动范围

  • ContentReader.list_entities 增加可选 query 参数(不传时完全走原逻辑),传入时直接在 SQL 侧做 LIKE 转义与匹配,按 精确 > 前缀 > 子串、同级按热度排序。
  • find_entity_context 改为传 query=name, limit=1,保持原有函数签名与返回值不变。
  • GetEntityTool.invoke 对齐 MCP 端 get_entity 行为,并发查询全部已挂载信源并拼接来源前缀,单个信源异常仅记日志不中断其余信源。

测试覆盖情况

  • uv run ruff check sag_api/ sag_agent/ tests/ 通过
  • uv run pytest -q tests/test_insights.py tests/test_agent_tools.py tests/test_mcp.py tests/test_agentic.py(88 passed)

find_entity_context 原先只从热度前 200 的实体中匹配名称,实体较多时查不到低频实体;改为在 SQL 中直接按名称过滤排序。同时对齐 MCP 端 get_entity 行为,多信源下并发查询全部信源而非命中首个即返回。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant