Simplify AI memory pipeline

This commit is contained in:
Kevin
2026-04-30 16:22:55 +08:00
parent 7617ea902c
commit 3234396254
35 changed files with 1002 additions and 579 deletions

View File

@@ -7,7 +7,7 @@ from types import SimpleNamespace
import pytest
from app.features.memory.enrichment import enrich_memory_after_ingest_async
from app.features.memory.llm_schemas import EnrichmentPayload, parse_json_payload
from app.features.memory.llm_schemas import EnrichmentPayload
from app.features.memory.models import MemorySource
from app.features.user.models import User
@@ -19,8 +19,7 @@ def test_enrichment_payload_roundtrip() -> None:
'"object_json":{"value":"北京","approximate_era":"1990年代"},'
'"confidence":0.85,"source_chunk_id":"ch-1"}]}'
)
p = parse_json_payload(raw, EnrichmentPayload)
assert p is not None
p = EnrichmentPayload.model_validate_json(raw)
assert p.summary == "要点摘要"
assert len(p.facts) == 1
assert p.facts[0].subject == "王伟"
@@ -36,16 +35,27 @@ async def test_enrich_memory_after_ingest_async_single_llm_call(
invoke_count = {"n": 0}
async def fake_invoke(llm, prompt, max_tokens, agent):
async def fake_run(llm, numbered, narrator_label):
invoke_count["n"] += 1
assert agent == "memory.enrichment"
return (
'{"summary":"本轮要点",'
'"facts":[{"fact_type":"event","subject":"王伟","predicate":"",'
'"object_json":{"value":"上海"},"confidence":0.8,"source_chunk_id":"ch1"}]}'
assert "[chunk_id=ch1]" in numbered
assert narrator_label == "老王"
return EnrichmentPayload.model_validate(
{
"summary": "本轮要点",
"facts": [
{
"fact_type": "event",
"subject": "王伟",
"predicate": "",
"object_json": {"value": "上海"},
"confidence": 0.8,
"source_chunk_id": "ch1",
}
],
}
)
monkeypatch.setattr(mod, "ainvoke_json_object", fake_invoke)
monkeypatch.setattr(mod, "_run_enrichment_llm_async", fake_run)
summaries: list[dict] = []
facts: list[dict] = []
@@ -74,7 +84,7 @@ async def test_enrich_memory_after_ingest_async_single_llm_call(
if model is User and key == "u1":
return SimpleNamespace(nickname="老王")
if model is MemorySource and key == "src-1":
return SimpleNamespace(lineage_json=None)
return SimpleNamespace(user_id="u1", lineage_json=None)
return None
async def execute(self, _stmt):
@@ -105,10 +115,10 @@ async def test_enrich_memory_skips_when_parse_returns_none(
monkeypatch.setattr("app.core.config.settings.memory_enrichment_enabled", True)
async def fake_invoke(*_args, **_kwargs):
return "{not json"
async def fake_run(*_args, **_kwargs):
return None
monkeypatch.setattr(mod, "ainvoke_json_object", fake_invoke)
monkeypatch.setattr(mod, "_run_enrichment_llm_async", fake_run)
called = {"summary": False, "fact": False}
async def capture_summary(*_args, **_kwargs):
@@ -135,7 +145,7 @@ async def test_enrich_memory_skips_when_parse_returns_none(
if model is User and key == "u":
return None
if model is MemorySource and key == "s":
return SimpleNamespace(lineage_json=None)
return SimpleNamespace(user_id="u", lineage_json=None)
return None
async def execute(self, _stmt):