fix(kb): 修复 metadata 覆写数据库图谱配置,改用文件级持久化

- update_kb_config 不再触发全量 _save_metadata,避免内存 metadata 覆盖数据库已有图谱配置
- 补全文件大小时改用 _persist_file 逐文件写入
- delete_file_chunks_only 通过 chunk_repo 检查文件是否有图谱数据再决定是否清理
This commit is contained in:
Wenjie Zhang 2026-05-17 19:54:54 +08:00
parent d292e53c23
commit f2d68ebdaa
3 changed files with 16 additions and 20 deletions

View File

@ -1242,8 +1242,6 @@ class KnowledgeBase(ABC):
if update_llm_model_spec:
self.databases_meta[db_id]["llm_model_spec"] = llm_model_spec
asyncio.create_task(self._save_metadata())
return self.get_database_info(db_id)
def get_retrievers(self) -> dict[str, dict]:
@ -1394,7 +1392,9 @@ class KnowledgeBase(ABC):
if updated:
logger.info(f"Filled {updated}/{len(files_to_update)} missing file sizes from MinIO for {self.kb_type}")
await self._save_metadata()
for file_id, file_size in results:
if file_size is not None:
await self._persist_file(file_id)
async def _save_metadata(self) -> None:
from yuxi.repositories.evaluation_repository import EvaluationRepository
@ -1421,19 +1421,6 @@ class KnowledgeBase(ABC):
}
if existing is None:
await kb_repo.create(payload)
else:
await kb_repo.update(
db_id,
{
"name": payload["name"],
"description": payload["description"],
"kb_type": payload["kb_type"],
"embedding_model_spec": payload["embedding_model_spec"],
"llm_model_spec": payload["llm_model_spec"],
"query_params": payload["query_params"],
"additional_params": payload["additional_params"],
},
)
for file_id, meta in self.files_meta.items():
db_id = meta.get("database_id")

View File

@ -633,7 +633,7 @@ class MilvusKB(KnowledgeBase):
request_params=params,
)
self.files_meta[file_id]["processing_params"] = params
await self._save_metadata()
await self._persist_file(file_id)
logger.debug(f"[index_file] file_id={file_id}, processing_params={params}")
# Add to processing queue
@ -1189,15 +1189,15 @@ class MilvusKB(KnowledgeBase):
async def delete_file_chunks_only(self, db_id: str, file_id: str) -> None:
"""仅删除文件的chunks数据保留元数据用于更新操作"""
graph_config = (self.databases_meta.get(db_id, {}).get("metadata") or {}).get("graph_build_config")
if graph_config:
chunk_repo = KnowledgeChunkRepository()
if await chunk_repo.count_graph_indexed_by_file_id(file_id):
from yuxi.knowledge.graphs.milvus_graph_service import MilvusGraphService
try:
await MilvusGraphService().delete_file_graph(db_id, file_id)
except Exception as e:
logger.error(f"Failed to delete graph data for file {file_id}: {e}")
await KnowledgeChunkRepository().delete_by_file_id(file_id)
await chunk_repo.delete_by_file_id(file_id)
collection = await self._get_milvus_collection(db_id)
if collection:

View File

@ -97,6 +97,15 @@ class KnowledgeChunkRepository:
async def count_graph_indexed_by_db_id(self, db_id: str) -> int:
return await self._count_by_db_id(db_id, KnowledgeChunk.graph_indexed.is_(True))
async def count_graph_indexed_by_file_id(self, file_id: str) -> int:
async with pg_manager.get_async_session_context() as session:
result = await session.execute(
select(func.count())
.select_from(KnowledgeChunk)
.where(KnowledgeChunk.file_id == file_id, KnowledgeChunk.graph_indexed.is_(True))
)
return int(result.scalar() or 0)
async def count_graph_pending_by_db_id(self, db_id: str) -> int:
return await self._count_by_db_id(db_id, KnowledgeChunk.graph_indexed.is_not(True))