|
|
@@ -113,7 +113,7 @@ class KnowledgeBaseService:
|
|
|
[Internal] 从 Milvus 数据中推断元数据并保存到 DB
|
|
|
仅当 DB 中没有定义元数据时调用
|
|
|
"""
|
|
|
- target_col = kb.collection_name1
|
|
|
+ target_col = kb.collection_name_parent
|
|
|
if not target_col:
|
|
|
return
|
|
|
|
|
|
@@ -233,13 +233,13 @@ class KnowledgeBaseService:
|
|
|
for kb in existing_kbs:
|
|
|
total_count = 0
|
|
|
|
|
|
- # 统计 collection1
|
|
|
- if kb.collection_name1 and kb.collection_name1 in milvus_names:
|
|
|
- total_count += await self._get_collection_row_count(kb.collection_name1)
|
|
|
+ # 统计 collection_name_parent
|
|
|
+ if kb.collection_name_parent and kb.collection_name_parent in milvus_names:
|
|
|
+ total_count += await self._get_collection_row_count(kb.collection_name_parent)
|
|
|
|
|
|
- # 统计 collection2
|
|
|
- if kb.collection_name2 and kb.collection_name2 in milvus_names:
|
|
|
- total_count += await self._get_collection_row_count(kb.collection_name2)
|
|
|
+ # 统计 collection_name_children
|
|
|
+ if kb.collection_name_children and kb.collection_name_children in milvus_names:
|
|
|
+ total_count += await self._get_collection_row_count(kb.collection_name_children)
|
|
|
|
|
|
if kb.document_count != total_count:
|
|
|
kb.document_count = total_count
|
|
|
@@ -258,8 +258,8 @@ class KnowledgeBaseService:
|
|
|
if keyword:
|
|
|
query = query.where(or_(
|
|
|
KnowledgeBase.name.like(f"%{keyword}%"),
|
|
|
- KnowledgeBase.collection_name1.like(f"%{keyword}%"),
|
|
|
- KnowledgeBase.collection_name2.like(f"%{keyword}%")
|
|
|
+ KnowledgeBase.collection_name_parent.like(f"%{keyword}%"),
|
|
|
+ KnowledgeBase.collection_name_children.like(f"%{keyword}%")
|
|
|
))
|
|
|
|
|
|
if status:
|
|
|
@@ -280,10 +280,10 @@ class KnowledgeBaseService:
|
|
|
# 设置 is_synced (辅助字段,不存库)
|
|
|
milvus_names_set = set(milvus_service.client.list_collections())
|
|
|
for item in items:
|
|
|
- c1_ok = item.collection_name1 in milvus_names_set
|
|
|
+ c1_ok = item.collection_name_parent in milvus_names_set
|
|
|
c2_ok = True
|
|
|
- if item.collection_name2:
|
|
|
- c2_ok = item.collection_name2 in milvus_names_set
|
|
|
+ if item.collection_name_children:
|
|
|
+ c2_ok = item.collection_name_children in milvus_names_set
|
|
|
|
|
|
item.is_synced = c1_ok and c2_ok
|
|
|
|
|
|
@@ -299,25 +299,25 @@ class KnowledgeBaseService:
|
|
|
async def create(self, db: AsyncSession, payload: KnowledgeBaseCreate) -> KnowledgeBase:
|
|
|
"""创建新知识库"""
|
|
|
# 1. 检查 DB 是否已存在
|
|
|
- # 检查 collection_name1
|
|
|
+ # 检查 collection_name_parent
|
|
|
exists1 = await db.execute(select(KnowledgeBase).where(
|
|
|
- KnowledgeBase.collection_name1 == payload.collection_name1,
|
|
|
+ KnowledgeBase.collection_name_parent == payload.collection_name_parent,
|
|
|
KnowledgeBase.is_deleted == 0
|
|
|
))
|
|
|
if exists1.scalars().first():
|
|
|
- raise ValueError(f"集合名称 {payload.collection_name1} 已存在")
|
|
|
+ raise ValueError(f"集合名称 {payload.collection_name_parent} 已存在")
|
|
|
|
|
|
- # 检查 collection_name2
|
|
|
- if payload.collection_name2:
|
|
|
+ # 检查 collection_name_children
|
|
|
+ if payload.collection_name_children:
|
|
|
exists2 = await db.execute(select(KnowledgeBase).where(
|
|
|
or_(
|
|
|
- KnowledgeBase.collection_name1 == payload.collection_name2,
|
|
|
- KnowledgeBase.collection_name2 == payload.collection_name2
|
|
|
+ KnowledgeBase.collection_name_parent == payload.collection_name_children,
|
|
|
+ KnowledgeBase.collection_name_children == payload.collection_name_children
|
|
|
),
|
|
|
KnowledgeBase.is_deleted == 0
|
|
|
))
|
|
|
if exists2.scalars().first():
|
|
|
- raise ValueError(f"集合名称 {payload.collection_name2} 已存在")
|
|
|
+ raise ValueError(f"集合名称 {payload.collection_name_children} 已存在")
|
|
|
|
|
|
try:
|
|
|
# 3. 创建 DB 记录
|
|
|
@@ -325,8 +325,8 @@ class KnowledgeBaseService:
|
|
|
new_kb = KnowledgeBase(
|
|
|
id=str(uuid.uuid4()),
|
|
|
name=payload.name,
|
|
|
- collection_name1=payload.collection_name1,
|
|
|
- collection_name2=payload.collection_name2,
|
|
|
+ collection_name_parent=payload.collection_name_parent,
|
|
|
+ collection_name_children=payload.collection_name_children,
|
|
|
description=payload.description,
|
|
|
status=payload.status or "normal",
|
|
|
created_by="admin",
|
|
|
@@ -373,10 +373,10 @@ class KnowledgeBaseService:
|
|
|
if payload.description is not None:
|
|
|
kb.description = payload.description
|
|
|
# 同步更新 Milvus 描述 (如果 Milvus 中存在该集合)
|
|
|
- if kb.collection_name1 and milvus_service.has_collection(kb.collection_name1):
|
|
|
- milvus_service.update_collection_description(kb.collection_name1, payload.description)
|
|
|
- if kb.collection_name2 and milvus_service.has_collection(kb.collection_name2):
|
|
|
- milvus_service.update_collection_description(kb.collection_name2, payload.description)
|
|
|
+ if kb.collection_name_parent and milvus_service.has_collection(kb.collection_name_parent):
|
|
|
+ milvus_service.update_collection_description(kb.collection_name_parent, payload.description)
|
|
|
+ if kb.collection_name_children and milvus_service.has_collection(kb.collection_name_children):
|
|
|
+ milvus_service.update_collection_description(kb.collection_name_children, payload.description)
|
|
|
|
|
|
if payload.status is not None:
|
|
|
kb.status = payload.status
|
|
|
@@ -423,8 +423,8 @@ class KnowledgeBaseService:
|
|
|
|
|
|
# 同步操作 Milvus Load/Release
|
|
|
targets = []
|
|
|
- if kb.collection_name1: targets.append(kb.collection_name1)
|
|
|
- if kb.collection_name2: targets.append(kb.collection_name2)
|
|
|
+ if kb.collection_name_parent: targets.append(kb.collection_name_parent)
|
|
|
+ if kb.collection_name_children: targets.append(kb.collection_name_children)
|
|
|
|
|
|
for col in targets:
|
|
|
if milvus_service.has_collection(col):
|
|
|
@@ -455,10 +455,10 @@ class KnowledgeBaseService:
|
|
|
# 尝试获取最新计数
|
|
|
try:
|
|
|
real_count = 0
|
|
|
- if kb.collection_name1 and milvus_service.has_collection(kb.collection_name1):
|
|
|
- real_count += await self._get_collection_row_count(kb.collection_name1)
|
|
|
- if kb.collection_name2 and milvus_service.has_collection(kb.collection_name2):
|
|
|
- real_count += await self._get_collection_row_count(kb.collection_name2)
|
|
|
+ if kb.collection_name_parent and milvus_service.has_collection(kb.collection_name_parent):
|
|
|
+ real_count += await self._get_collection_row_count(kb.collection_name_parent)
|
|
|
+ if kb.collection_name_children and milvus_service.has_collection(kb.collection_name_children):
|
|
|
+ real_count += await self._get_collection_row_count(kb.collection_name_children)
|
|
|
current_count = real_count
|
|
|
except:
|
|
|
pass
|
|
|
@@ -469,8 +469,8 @@ class KnowledgeBaseService:
|
|
|
try:
|
|
|
# 1. 删除 Milvus 集合
|
|
|
targets = []
|
|
|
- if kb.collection_name1: targets.append(kb.collection_name1)
|
|
|
- if kb.collection_name2: targets.append(kb.collection_name2)
|
|
|
+ if kb.collection_name_parent: targets.append(kb.collection_name_parent)
|
|
|
+ if kb.collection_name_children: targets.append(kb.collection_name_children)
|
|
|
|
|
|
for col in targets:
|
|
|
try:
|
|
|
@@ -647,8 +647,8 @@ class KnowledgeBaseService:
|
|
|
# 查找知识库 (匹配任意一个 collection_name)
|
|
|
result = await db.execute(select(KnowledgeBase).where(
|
|
|
or_(
|
|
|
- KnowledgeBase.collection_name1 == collection_name,
|
|
|
- KnowledgeBase.collection_name2 == collection_name
|
|
|
+ KnowledgeBase.collection_name_parent == collection_name,
|
|
|
+ KnowledgeBase.collection_name_children == collection_name
|
|
|
),
|
|
|
KnowledgeBase.is_deleted == 0
|
|
|
))
|
|
|
@@ -667,10 +667,10 @@ class KnowledgeBaseService:
|
|
|
count1 = 0
|
|
|
count2 = 0
|
|
|
|
|
|
- if kb.collection_name1:
|
|
|
- count1 = await self._get_collection_row_count(kb.collection_name1)
|
|
|
- if kb.collection_name2:
|
|
|
- count2 = await self._get_collection_row_count(kb.collection_name2)
|
|
|
+ if kb.collection_name_parent:
|
|
|
+ count1 = await self._get_collection_row_count(kb.collection_name_parent)
|
|
|
+ if kb.collection_name_children:
|
|
|
+ count2 = await self._get_collection_row_count(kb.collection_name_children)
|
|
|
|
|
|
total_count = count1 + count2
|
|
|
|