Simplify AI memory pipeline
This commit is contained in:
@@ -8,9 +8,11 @@ Celery task 只能作为同步入口包装 async service,不再维护 sync mem
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.logging import get_logger
|
||||
from app.features.memory.embedding_service import MemoryEmbeddingService
|
||||
from app.features.memory.ingest_service import MemoryIngestService
|
||||
from app.features.memory.repo import (
|
||||
create_curation_action,
|
||||
mark_facts_stale_for_excluded_chunk,
|
||||
set_chunk_excluded,
|
||||
set_memory_fact_status,
|
||||
)
|
||||
@@ -78,7 +80,28 @@ class MemoryService:
|
||||
"""Run post-ingest enrichment through the async memory path."""
|
||||
from app.features.memory.enrichment import enrich_memory_after_ingest_async
|
||||
|
||||
await enrich_memory_after_ingest_async(self._db, user_id, source_id, llm=llm)
|
||||
await enrich_memory_after_ingest_async(
|
||||
self._db,
|
||||
user_id,
|
||||
source_id,
|
||||
llm=llm,
|
||||
raise_on_failure=True,
|
||||
)
|
||||
|
||||
async def embed_source(
|
||||
self,
|
||||
user_id: str,
|
||||
source_id: str,
|
||||
*,
|
||||
raise_on_failure: bool = False,
|
||||
) -> dict:
|
||||
"""Embed persisted memory chunks and update embedding status."""
|
||||
service = MemoryEmbeddingService(self._db, embedding_provider=self._embedding)
|
||||
return await service.embed_source(
|
||||
user_id,
|
||||
source_id,
|
||||
raise_on_failure=raise_on_failure,
|
||||
)
|
||||
|
||||
async def compact_user(self, user_id: str, context: dict | None = None) -> dict:
|
||||
"""Run near-duplicate compaction through the async memory path."""
|
||||
@@ -92,13 +115,21 @@ class MemoryService:
|
||||
ok = await set_chunk_excluded(self._db, chunk_id, user_id, True)
|
||||
if not ok:
|
||||
return False
|
||||
stale_count = await mark_facts_stale_for_excluded_chunk(
|
||||
self._db,
|
||||
user_id=user_id,
|
||||
chunk_id=chunk_id,
|
||||
)
|
||||
await create_curation_action(
|
||||
self._db,
|
||||
user_id=user_id,
|
||||
action_type="exclude",
|
||||
target_type="chunk",
|
||||
target_id=chunk_id,
|
||||
details={"reason": reason} if reason else None,
|
||||
details={
|
||||
**({"reason": reason} if reason else {}),
|
||||
"staled_fact_count": stale_count,
|
||||
},
|
||||
)
|
||||
await self._db.commit()
|
||||
return True
|
||||
@@ -113,7 +144,7 @@ class MemoryService:
|
||||
action_type="restore",
|
||||
target_type="chunk",
|
||||
target_id=chunk_id,
|
||||
details=None,
|
||||
details={"fact_restore_policy": "requires_reenrichment"},
|
||||
)
|
||||
await self._db.commit()
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user