添加 知识库信息更新方法,支持修改标题和描述

This commit is contained in:
Wenjie Zhang 2025-05-10 10:24:18 +08:00
parent d6a5a82a3d
commit 8a6664758e
5 changed files with 128 additions and 1 deletions

View File

@ -184,3 +184,18 @@ async def add_graph_entity(file_path: str = Body(...), kgdb_name: Optional[str]
logger.error(f"添加实体失败: {e}, {traceback.format_exc()}")
return {"message": f"添加实体失败: {e}", "status": "failed"}
@data.post("/update")
async def update_database_info(
db_id: str = Body(...),
name: str = Body(...),
description: str = Body(...),
current_user: User = Depends(get_admin_user)
):
logger.debug(f"Update database {db_id} info: {name}, {description}")
try:
database = knowledge_base.update_database(db_id, name, description)
return {"message": "更新成功", "database": database}
except Exception as e:
logger.error(f"更新数据库失败 {e}, {traceback.format_exc()}")
raise HTTPException(status_code=400, detail=f"更新数据库失败: {e}")

View File

@ -127,6 +127,21 @@ class KBDBManager:
return True
return False
def update_database(self, db_id, name, description):
"""更新知识库信息"""
with self.get_session() as session:
db = session.query(KnowledgeDatabase).filter_by(db_id=db_id).first()
if not db:
raise ValueError(f"数据库 {db_id} 不存在")
# 更新字段
db.name = name
db.description = description
session.commit()
# 返回更新后的数据库信息
return self._to_dict_safely(db)
# 文件操作方法
def add_file(self, db_id, file_id, filename, path, file_type, status="waiting"):
"""添加文件"""

View File

@ -136,8 +136,15 @@ class KnowledgeBase:
else:
db_copy = db_dict.copy()
try:
# 保存原始描述
original_description = db_copy.get("description")
milvus_info = self.get_collection_info(db_id)
db_copy.update(milvus_info)
# 如果原始描述不为空,恢复它
if original_description:
db_copy["description"] = original_description
except Exception as e:
logger.warning(f"获取知识库 ID: {db_id} 的Milvus信息失败: {e}")
# 添加一个默认的Milvus状态
@ -553,3 +560,14 @@ class KnowledgeBase:
db = self.db_manager.get_database_by_id(db_id)
return db["embed_model"] == self.embed_model.embed_model_fullname
def update_database(self, db_id, name, description):
"""更新知识库信息"""
# 检查知识库是否存在
db = self.db_manager.get_database_by_id(db_id)
if db is None:
raise Exception(f"数据库不存在: {db_id}")
# 调用db_manager更新知识库信息
updated_db = self.db_manager.update_database(db_id, name, description)
return updated_db

View File

@ -199,6 +199,20 @@ export const knowledgeBaseApi = {
checkAdminPermission()
return apiPost('/api/data/url-to-chunk', data, {}, true)
},
/**
* 更新知识库信息
* @param {string} dbId - 知识库ID
* @param {Object} data - 包含name和description的数据对象
* @returns {Promise} - 更新结果
*/
updateDatabaseInfo: async (dbId, data) => {
checkAdminPermission()
return apiPost('/api/data/update', {
db_id: dbId,
...data
}, {}, true)
},
}
// 图数据库管理API

View File

@ -14,12 +14,28 @@
<a-button type="primary" @click="backToDatabase">
<LeftOutlined /> 返回
</a-button>
<a-button type="primary" @click="showEditModal">
<EditOutlined />
</a-button>
<a-button type="primary" danger @click="deleteDatabse">
<DeleteOutlined /> 删除数据库
<DeleteOutlined />
</a-button>
</template>
</HeaderComponent>
<a-alert v-if="configStore.config.embed_model &&database.embed_model != configStore.config.embed_model" message="向量模型不匹配,请重新选择" type="warning" style="margin: 10px 20px;" />
<!-- 添加编辑对话框 -->
<a-modal v-model:open="editModalVisible" title="编辑知识库信息" @ok="handleEditSubmit">
<a-form :model="editForm" :rules="rules" ref="editFormRef" layout="vertical">
<a-form-item label="知识库名称" name="name" required>
<a-input v-model:value="editForm.name" placeholder="请输入知识库名称" />
</a-form-item>
<a-form-item label="知识库描述" name="description">
<a-textarea v-model:value="editForm.description" placeholder="请输入知识库描述" :rows="4" />
</a-form-item>
</a-form>
</a-modal>
<div class="db-main-container">
<a-tabs v-model:activeKey="state.curPage" class="atab-container" type="card">
@ -333,6 +349,7 @@ import {
LoadingOutlined,
FileOutlined,
LinkOutlined,
EditOutlined,
} from '@ant-design/icons-vue'
@ -806,6 +823,54 @@ const getAuthHeaders = () => {
return userStore.getAuthHeaders();
};
//
const editModalVisible = ref(false);
const editFormRef = ref(null);
const editForm = reactive({
name: '',
description: ''
});
const rules = {
name: [{ required: true, message: '请输入知识库名称' }]
};
//
const showEditModal = () => {
editForm.name = database.value.name || '';
editForm.description = database.value.description || '';
editModalVisible.value = true;
};
//
const handleEditSubmit = () => {
editFormRef.value.validate().then(() => {
updateDatabaseInfo();
}).catch(err => {
console.error('表单验证失败:', err);
});
};
//
const updateDatabaseInfo = async () => {
try {
state.lock = true;
const response = await knowledgeBaseApi.updateDatabaseInfo(databaseId.value, {
name: editForm.name,
description: editForm.description
});
message.success('知识库信息更新成功');
editModalVisible.value = false;
await getDatabaseInfo(); //
} catch (error) {
console.error(error);
message.error(error.message || '更新失败');
} finally {
state.lock = false;
}
};
</script>
<style lang="less" scoped>