From 309eda6e9acd894ae06c8a5f0dc19cbc03feefac Mon Sep 17 00:00:00 2001 From: muyuzhong <2416118206@qq.com> Date: Thu, 12 Nov 2026 16:16:31 +0800 Subject: [PATCH] =?UTF-8?q?fix(api):=20=E4=BF=AE=E5=A4=8D=20get=5Fentity?= =?UTF-8?q?=20=E6=BC=8F=E6=9F=A5=E4=BD=8E=E9=A2=91=E5=AE=9E=E4=BD=93?= =?UTF-8?q?=E4=B8=8E=E5=A4=9A=E4=BF=A1=E6=BA=90=E6=8F=90=E5=89=8D=E8=BF=94?= =?UTF-8?q?=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit find_entity_context 原先只从热度前 200 的实体中匹配名称,实体较多时查不到低频实体;改为在 SQL 中直接按名称过滤排序。同时对齐 MCP 端 get_entity 行为,多信源下并发查询全部信源而非命中首个即返回。 --- apps/api/sag_api/sag/content_reader.py | 23 ++++- apps/api/sag_api/sag/engine_manager.py | 3 +- apps/api/sag_api/services/insight_service.py | 10 +- apps/api/sag_api/tools/builtin.py | 24 +++-- apps/api/tests/test_agent_tools.py | 30 ++++++ apps/api/tests/test_insights.py | 102 +++++++++++++++++++ 6 files changed, 173 insertions(+), 19 deletions(-) diff --git a/apps/api/sag_api/sag/content_reader.py b/apps/api/sag_api/sag/content_reader.py index 25e49cce..1d7d8899 100644 --- a/apps/api/sag_api/sag/content_reader.py +++ b/apps/api/sag_api/sag/content_reader.py @@ -328,11 +328,17 @@ async def list_entities( source_ids: tuple[str, ...], source: Source | None = None, types: list[str] | None = None, + query: str | None = None, limit: int = 100, ) -> list[EntityInfo]: - """读取该源的事件—实体图谱,按热度(关联事件数)排序。extract 后才有数据。""" + """读取该源的事件—实体图谱,按热度(关联事件数)排序。extract 后才有数据。 + + 传入 ``query`` 时先按名称过滤全部实体,再按 精确 > 前缀 > 子串、同级热度排序。 + """ + if query is not None and not query.strip(): + return [] await self._access.slot(source_config_id, source) # 确保引擎 / DB 已初始化 - from sqlalchemy import func, select + from sqlalchemy import case, func, or_, select from zleap.sag.db.models import Entity, EventEntity, SourceEvent heat = func.count(EventEntity.id) @@ -344,13 +350,24 @@ async def list_entities( ] if types: conds.append(Entity.type.in_(list(types))) + order_by = [heat.desc(), Entity.name, Entity.id] + if query is not None: + lowered = query.strip().lower() + needle = lowered.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + + def _name_like(pattern: str) -> Any: + return or_(Entity.normalized_name.like(pattern, escape="\\"), Entity.name.ilike(pattern, escape="\\")) + + exact = or_(Entity.normalized_name == lowered, func.lower(Entity.name) == lowered) + conds.append(_name_like(f"%{needle}%")) + order_by.insert(0, case((exact, 0), (_name_like(f"{needle}%"), 1), else_=2)) stmt = ( select(Entity, heat.label("heat")) .join(EventEntity, EventEntity.entity_id == Entity.id) .join(SourceEvent, SourceEvent.id == EventEntity.event_id) .where(*conds) .group_by(Entity.id) - .order_by(heat.desc(), Entity.name, Entity.id) + .order_by(*order_by) .limit(limit) ) sf = await self._access.relational_session_factory(source_config_id) diff --git a/apps/api/sag_api/sag/engine_manager.py b/apps/api/sag_api/sag/engine_manager.py index 14fdde11..0aabb179 100644 --- a/apps/api/sag_api/sag/engine_manager.py +++ b/apps/api/sag_api/sag/engine_manager.py @@ -1079,10 +1079,11 @@ async def list_entities( source_ids: tuple[str, ...], source: Source | None = None, types: list[str] | None = None, + query: str | None = None, limit: int = 100, ) -> list[EntityInfo]: return await self._content.list_entities( - source_config_id, source_ids=source_ids, source=source, types=types, limit=limit + source_config_id, source_ids=source_ids, source=source, types=types, query=query, limit=limit ) async def source_graph( diff --git a/apps/api/sag_api/services/insight_service.py b/apps/api/sag_api/services/insight_service.py index dd16ef9a..51bf385e 100644 --- a/apps/api/sag_api/services/insight_service.py +++ b/apps/api/sag_api/services/insight_service.py @@ -48,14 +48,12 @@ async def find_entity_context( source.sag_source_config_id, source_ids=input_ids, source=source, - limit=200, + query=name, + limit=1, ) - lowered = name.strip().lower() - match = next((entity for entity in entities if (entity.name or "").lower() == lowered), None) - if match is None: - match = next((entity for entity in entities if lowered in (entity.name or "").lower()), None) - if match is None: + if not entities: return None + match = entities[0] snippets = await engine_manager.entity_context( source.sag_source_config_id, match.id, diff --git a/apps/api/sag_api/tools/builtin.py b/apps/api/sag_api/tools/builtin.py index 9bf080bc..4625c704 100644 --- a/apps/api/sag_api/tools/builtin.py +++ b/apps/api/sag_api/tools/builtin.py @@ -360,16 +360,22 @@ async def invoke(self, args: dict[str, Any], ctx: ToolContext) -> ToolResult: return ToolResult(content="(未找到该实体)") from sag_api.services.insight_service import find_entity_context - for source in ctx.sources: - found = await find_entity_context(ctx.engine_manager, source, name) - if found is not None: + async def _one(source: Any) -> str | None: + try: + found = await find_entity_context(ctx.engine_manager, source, name) + if found is None: + return None match, snippets = found - body = "\n\n".join(snippets) if snippets else match.description or "" - return ToolResult( - content=f"实体「{match.name}」({match.type}):\n{body}".strip(), - data={"entity_id": match.id, "source_id": source.id}, - ) - return ToolResult(content="(未找到该实体)") + body = "\n\n".join(snippets) if snippets else (match.description or "") + prefix = f"来源:{source.name}(source_id={source.id})\n" if len(ctx.sources) > 1 else "" + return f"{prefix}实体「{match.name}」({match.type}):\n{body}".strip() + except Exception as error: + log.warning("查询实体失败 source_id=%s:%s", source.id, error) + return None + + results = await asyncio.gather(*(_one(source) for source in ctx.sources)) + matches = [result for result in results if result] + return ToolResult(content="\n\n".join(matches) if matches else "(未找到该实体)") class GetTimeTool(Tool): diff --git a/apps/api/tests/test_agent_tools.py b/apps/api/tests/test_agent_tools.py index f67b9092..4e3f1bbc 100644 --- a/apps/api/tests/test_agent_tools.py +++ b/apps/api/tests/test_agent_tools.py @@ -872,3 +872,33 @@ async def test_legacy_persona_can_chat_without_calling_its_search_tool(strategy) assert answer.status_code == 200, answer.text assert answer.json()["choices"][0]["message"]["content"] == "最终答案" assert fake.calls == 1 + + +@pytest.mark.asyncio +async def test_get_entity_aggregates_sources_concurrently_and_isolates_failures(monkeypatch): + """多信源并发查询并按信源顺序聚合;单信源异常不阻断其余信源。""" + import asyncio + + from sag_api.sag import EntityInfo + from sag_api.tools.builtin import GetEntityTool + + c_done = asyncio.Event() + + async def fake_find_entity_context(engine_manager, source, name): + if source.id == "src-b": + raise RuntimeError("engine failure") + if source.id == "src-a": + await c_done.wait() # a 必须等 c 完成后才返回:串行实现会在这里卡死 + else: + c_done.set() + return EntityInfo(id=f"ent-{source.id}", name=name, type="person", description=""), [f"{source.name}片段"] + + monkeypatch.setattr("sag_api.services.insight_service.find_entity_context", fake_find_entity_context) + sources = [SimpleNamespace(id=f"src-{key}", name=f"{key}库") for key in "abc"] + ctx = ToolContext(engine_manager=SimpleNamespace(), sources=sources) + result = await asyncio.wait_for(GetEntityTool().invoke({"name": "关羽"}, ctx), timeout=2) + + assert result.content == ( + "来源:a库(source_id=src-a)\n实体「关羽」(person):\na库片段\n\n" + "来源:c库(source_id=src-c)\n实体「关羽」(person):\nc库片段" + ) diff --git a/apps/api/tests/test_insights.py b/apps/api/tests/test_insights.py index 2ba35bdd..40151c49 100644 --- a/apps/api/tests/test_insights.py +++ b/apps/api/tests/test_insights.py @@ -424,3 +424,105 @@ async def fake_source_graph( assert source is not None await session.delete(source) await session.commit() + + +@pytest.mark.asyncio +async def test_find_entity_context_searches_beyond_top_200_by_name_rank(): + """按名称查实体不受热门 Top-200 截断;精确 > 前缀 > 子串优先于热度;LIKE 通配符按字面匹配。""" + from zleap.sag.db.models import ( + DataSource, + Entity, + EntityType, + EventEntity, + SourceEvent, + ) + + from sag_api.core.db import SessionLocal + from sag_api.db.models import Document, Source + from sag_api.enums import DocumentStatus + from sag_api.main import app + from sag_api.services.insight_service import find_entity_context + + async with _draining_app_lifespan(app): + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://t") as client: + email = f"longtail-{uuid.uuid4().hex}@x.com" + reg = await client.post("/api/v1/auth/register", json={"email": email, "password": "password123"}) + headers = {"Authorization": f"Bearer {reg.json()['access_token']}"} + sid = (await client.post("/api/v1/sources", headers=headers, json={"name": "长尾实体库"})).json()["id"] + async with SessionLocal() as s: + s.add( + Document( + id=uuid.uuid4().hex, + source_id=sid, + filename="长尾.md", + content_type="text/markdown", + size_bytes=1, + storage_path="/tmp/longtail.md", + status=DocumentStatus.READY, + chunk_count=1, + sag_source_id="lt-1", + ) + ) + await s.commit() + async with SessionLocal() as s: + source = await s.get(Source, sid) + scid = source.sag_source_config_id + + # 实体名 → 关联事项数(热度)。排序样本的热度刻意与名称优先级相反。 + heats = {f"热门人物{i:03d}": 2 for i in range(200)} + heats |= {"诸葛亮": 1, "赤壁": 1, "赤壁之战": 2, "火烧赤壁": 3, "满分100%": 1} + factory = await app.state.engine_manager.get_sag_session_factory(scid) + async with factory() as s: + await s.merge(DataSource(id=scid, name="长尾实体库")) + entity_type = EntityType(id=uuid.uuid4().hex, type="person", name="人物") + events = [ + SourceEvent( + id=uuid.uuid4().hex, + data_source_id=scid, + source_type="doc", + source_id="lt-1", + title=f"事项{i}", + summary=f"事项{i}", + content=f"事项{i}", + chunk_id="lt-chunk", + rank=i, + ) + for i in range(3) + ] + entities = { + name: Entity( + id=uuid.uuid4().hex, + data_source_id=scid, + entity_type_id=entity_type.id, + type="person", + name=name, + normalized_name=name.lower(), + description="", + ) + for name in heats + } + s.add_all([entity_type, *events, *entities.values()]) + await s.flush() + s.add_all( + EventEntity(id=uuid.uuid4().hex, event_id=event.id, entity_id=entities[name].id, weight=1.0) + for name, heat in heats.items() + for event in events[:heat] + ) + await s.commit() + + manager = app.state.engine_manager + + async def names(query: str | None, limit: int) -> list[str]: + rows = await manager.list_entities(scid, source_ids=("lt-1",), source=source, query=query, limit=limit) + return [row.name for row in rows] + + assert "诸葛亮" not in await names(None, 200) + for query in ("诸葛亮", "葛亮"): + found = await find_entity_context(manager, source, query) + assert found is not None and found[0].name == "诸葛亮" + assert await names("赤壁", 10) == ["赤壁", "赤壁之战", "火烧赤壁"] + assert await names("%", 10) == ["满分100%"] + + async with SessionLocal() as s: + await s.delete(await s.get(Source, sid)) + await s.commit()