Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions apps/api/sag_api/sag/content_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion apps/api/sag_api/sag/engine_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 4 additions & 6 deletions apps/api/sag_api/services/insight_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 15 additions & 9 deletions apps/api/sag_api/tools/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
30 changes: 30 additions & 0 deletions apps/api/tests/test_agent_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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库片段"
)
102 changes: 102 additions & 0 deletions apps/api/tests/test_insights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()