mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
优化修改知识库接口部分业务逻辑
This commit is contained in:
+14
-16
@@ -1,7 +1,6 @@
|
||||
package xiaozhi.modules.knowledge.controller;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
@@ -24,8 +23,10 @@ import xiaozhi.common.exception.ErrorCode;
|
||||
import xiaozhi.common.exception.RenException;
|
||||
import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.common.utils.Result;
|
||||
import xiaozhi.common.utils.ToolUtil;
|
||||
import xiaozhi.modules.knowledge.dto.KnowledgeBaseDTO;
|
||||
import xiaozhi.modules.knowledge.service.KnowledgeBaseService;
|
||||
import xiaozhi.modules.model.entity.ModelConfigEntity;
|
||||
import xiaozhi.modules.security.user.SecurityUser;
|
||||
|
||||
@AllArgsConstructor
|
||||
@@ -131,20 +132,17 @@ public class KnowledgeBaseController {
|
||||
|
||||
// 获取当前登录用户ID
|
||||
Long currentUserId = SecurityUser.getUserId();
|
||||
String[] idArray = ids.split(",");
|
||||
for (String datasetId : idArray) {
|
||||
if (StringUtils.isNotBlank(datasetId)) {
|
||||
// 先获取现有知识库信息以检查权限
|
||||
KnowledgeBaseDTO existingKnowledgeBase = knowledgeBaseService.getByDatasetId(datasetId.trim());
|
||||
|
||||
List<String> idList = Arrays.asList(ids.split(","));
|
||||
List<KnowledgeBaseDTO> knowledgeBaseDTOs = Optional.ofNullable(knowledgeBaseService.getByDatasetIdList(idList)).orElseGet(ArrayList::new);
|
||||
if (ToolUtil.isNotEmpty(knowledgeBaseDTOs)) {
|
||||
knowledgeBaseDTOs.forEach(item->{
|
||||
// 检查权限:用户只能删除自己创建的知识库
|
||||
if (existingKnowledgeBase.getCreator() == null
|
||||
|| !existingKnowledgeBase.getCreator().equals(currentUserId)) {
|
||||
if (item.getCreator() == null || !item.getCreator().equals(currentUserId)) {
|
||||
throw new RenException(ErrorCode.NO_PERMISSION);
|
||||
}
|
||||
|
||||
knowledgeBaseService.deleteByDatasetId(datasetId.trim());
|
||||
}
|
||||
//删除
|
||||
knowledgeBaseService.deleteByDatasetId(item.getDatasetId());
|
||||
});
|
||||
}
|
||||
return new Result<>();
|
||||
}
|
||||
@@ -152,8 +150,8 @@ public class KnowledgeBaseController {
|
||||
@GetMapping("/rag-models")
|
||||
@Operation(summary = "获取RAG模型列表")
|
||||
@RequiresPermissions("sys:role:normal")
|
||||
public Result<List<Map<String, Object>>> getRAGModels() {
|
||||
List<Map<String, Object>> result = knowledgeBaseService.getRAGModels();
|
||||
return new Result<List<Map<String, Object>>>().ok(result);
|
||||
public Result<List<ModelConfigEntity>> getRAGModels() {
|
||||
List<ModelConfigEntity> result = knowledgeBaseService.getRAGModels();
|
||||
return new Result<List<ModelConfigEntity>>().ok(result);
|
||||
}
|
||||
}
|
||||
+8
-11
@@ -63,16 +63,11 @@ public class KnowledgeFilesController {
|
||||
// 验证知识库权限
|
||||
validateKnowledgeBasePermission(datasetId);
|
||||
|
||||
// 如果指定了状态参数,使用状态查询接口
|
||||
if (status != null) {
|
||||
PageData<KnowledgeFilesDTO> pageData = knowledgeFilesService.getPageListByStatus(datasetId, status, page, page_size);
|
||||
return new Result<PageData<KnowledgeFilesDTO>>().ok(pageData);
|
||||
}
|
||||
|
||||
// 否则使用通用查询接口
|
||||
//组装参数
|
||||
KnowledgeFilesDTO knowledgeFilesDTO = new KnowledgeFilesDTO();
|
||||
knowledgeFilesDTO.setDatasetId(datasetId);
|
||||
knowledgeFilesDTO.setName(name);
|
||||
knowledgeFilesDTO.setStatus(status);
|
||||
PageData<KnowledgeFilesDTO> pageData = knowledgeFilesService.getPageList(knowledgeFilesDTO, page, page_size);
|
||||
return new Result<PageData<KnowledgeFilesDTO>>().ok(pageData);
|
||||
}
|
||||
@@ -87,8 +82,11 @@ public class KnowledgeFilesController {
|
||||
@RequestParam(required = false, defaultValue = "10") Integer page_size) {
|
||||
// 验证知识库权限
|
||||
validateKnowledgeBasePermission(datasetId);
|
||||
|
||||
PageData<KnowledgeFilesDTO> pageData = knowledgeFilesService.getPageListByStatus(datasetId, status, page, page_size);
|
||||
//组装参数
|
||||
KnowledgeFilesDTO knowledgeFilesDTO = new KnowledgeFilesDTO();
|
||||
knowledgeFilesDTO.setDatasetId(datasetId);
|
||||
knowledgeFilesDTO.setStatus(status);
|
||||
PageData<KnowledgeFilesDTO> pageData = knowledgeFilesService.getPageList(knowledgeFilesDTO, page, page_size);
|
||||
return new Result<PageData<KnowledgeFilesDTO>>().ok(pageData);
|
||||
}
|
||||
|
||||
@@ -159,8 +157,7 @@ public class KnowledgeFilesController {
|
||||
// 验证知识库权限
|
||||
validateKnowledgeBasePermission(datasetId);
|
||||
|
||||
Map<String, Object> result = knowledgeFilesService.listChunks(datasetId, documentId, keywords, page, page_size,
|
||||
id);
|
||||
Map<String, Object> result = knowledgeFilesService.listChunks(datasetId, documentId, keywords, page, page_size, id);
|
||||
return new Result<Map<String, Object>>().ok(result);
|
||||
}
|
||||
|
||||
|
||||
+10
-1
@@ -7,6 +7,7 @@ import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.common.service.BaseService;
|
||||
import xiaozhi.modules.knowledge.dto.KnowledgeBaseDTO;
|
||||
import xiaozhi.modules.knowledge.entity.KnowledgeBaseEntity;
|
||||
import xiaozhi.modules.model.entity.ModelConfigEntity;
|
||||
|
||||
/**
|
||||
* 知识库知识库服务接口
|
||||
@@ -55,6 +56,14 @@ public interface KnowledgeBaseService extends BaseService<KnowledgeBaseEntity> {
|
||||
*/
|
||||
KnowledgeBaseDTO getByDatasetId(String datasetId);
|
||||
|
||||
/**
|
||||
* 根据知识库ID集合查询知识库
|
||||
*
|
||||
* @param datasetIdList 知识库ID集合
|
||||
* @return 知识库详情
|
||||
*/
|
||||
List<KnowledgeBaseDTO> getByDatasetIdList(List<String> datasetIdList);
|
||||
|
||||
/**
|
||||
* 根据知识库ID删除知识库
|
||||
*
|
||||
@@ -83,5 +92,5 @@ public interface KnowledgeBaseService extends BaseService<KnowledgeBaseEntity> {
|
||||
*
|
||||
* @return RAG模型列表
|
||||
*/
|
||||
List<Map<String, Object>> getRAGModels();
|
||||
List<ModelConfigEntity> getRAGModels();
|
||||
}
|
||||
-11
@@ -47,17 +47,6 @@ public interface KnowledgeFilesService {
|
||||
Map<String, Object> metaFields, String chunkMethod,
|
||||
Map<String, Object> parserConfig);
|
||||
|
||||
/**
|
||||
* 根据状态分页查询文档列表
|
||||
*
|
||||
* @param datasetId 知识库ID
|
||||
* @param status 文档解析状态(0-未开始,1-进行中,2-已取消,3-已完成,4-失败)
|
||||
* @param page 页码
|
||||
* @param limit 每页数量
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageData<KnowledgeFilesDTO> getPageListByStatus(String datasetId, Integer status, Integer page, Integer limit);
|
||||
|
||||
/**
|
||||
* 根据文档ID和知识库ID删除文档
|
||||
*
|
||||
|
||||
+26
-23
@@ -1,21 +1,12 @@
|
||||
package xiaozhi.modules.knowledge.service.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
@@ -32,6 +23,7 @@ import xiaozhi.common.redis.RedisUtils;
|
||||
import xiaozhi.common.service.impl.BaseServiceImpl;
|
||||
import xiaozhi.common.utils.ConvertUtils;
|
||||
import xiaozhi.common.utils.MessageUtils;
|
||||
import xiaozhi.common.utils.ToolUtil;
|
||||
import xiaozhi.modules.knowledge.dao.KnowledgeBaseDao;
|
||||
import xiaozhi.modules.knowledge.dto.KnowledgeBaseDTO;
|
||||
import xiaozhi.modules.knowledge.entity.KnowledgeBaseEntity;
|
||||
@@ -53,8 +45,6 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
private final ModelConfigService modelConfigService;
|
||||
private final ModelConfigDao modelConfigDao;
|
||||
private final RedisUtils redisUtils;
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public KnowledgeBaseEntity selectById(Serializable datasetId) {
|
||||
@@ -269,6 +259,26 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
return ConvertUtils.sourceToTarget(entity, KnowledgeBaseDTO.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据知识库ID集合查询知识库
|
||||
* @param datasetIdList 知识库ID集合
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<KnowledgeBaseDTO> getByDatasetIdList(List<String> datasetIdList) {
|
||||
//判断参数
|
||||
if (ToolUtil.isEmpty(datasetIdList)) {
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR);
|
||||
}
|
||||
//批量查询
|
||||
List<KnowledgeBaseEntity> entityList = knowledgeBaseDao.selectList(
|
||||
new QueryWrapper<KnowledgeBaseEntity>().in("dataset_id", datasetIdList));
|
||||
if (ToolUtil.isEmpty(entityList)) {
|
||||
throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS);
|
||||
}
|
||||
return ConvertUtils.sourceToTarget(entityList, KnowledgeBaseDTO.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteByDatasetId(String datasetId) {
|
||||
if (StringUtils.isBlank(datasetId)) {
|
||||
@@ -380,24 +390,17 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<String, Object>> getRAGModels() {
|
||||
public List<ModelConfigEntity> getRAGModels() {
|
||||
// 查询RAG类型的模型配置
|
||||
QueryWrapper<ModelConfigEntity> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("model_type", Constant.RAG_CONFIG_TYPE)
|
||||
QueryWrapper<ModelConfigEntity> queryWrapper = new QueryWrapper<ModelConfigEntity>()
|
||||
.select("id", "model_name")
|
||||
.eq("model_type", Constant.RAG_CONFIG_TYPE)
|
||||
.eq("is_enabled", 1)
|
||||
.orderByDesc("is_default")
|
||||
.orderByDesc("create_date");
|
||||
|
||||
List<ModelConfigEntity> modelConfigs = modelConfigDao.selectList(queryWrapper);
|
||||
|
||||
List<Map<String, Object>> modelList = new ArrayList<>();
|
||||
for (ModelConfigEntity modelConfig : modelConfigs) {
|
||||
Map<String, Object> modelInfo = new HashMap<>();
|
||||
modelInfo.put("id", modelConfig.getId());
|
||||
modelInfo.put("modelName", modelConfig.getModelName());
|
||||
modelList.add(modelInfo);
|
||||
}
|
||||
return modelList;
|
||||
return modelConfigs;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+9
-80
@@ -3,24 +3,10 @@ package xiaozhi.modules.knowledge.service.impl;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import java.util.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.core.io.AbstractResource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -31,6 +17,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import xiaozhi.common.exception.ErrorCode;
|
||||
import xiaozhi.common.exception.RenException;
|
||||
import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.common.utils.ToolUtil;
|
||||
import xiaozhi.modules.knowledge.dto.KnowledgeFilesDTO;
|
||||
import xiaozhi.modules.knowledge.rag.KnowledgeBaseAdapter;
|
||||
import xiaozhi.modules.knowledge.rag.KnowledgeBaseAdapterFactory;
|
||||
@@ -43,8 +30,6 @@ import xiaozhi.modules.knowledge.service.KnowledgeFilesService;
|
||||
public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
private final KnowledgeBaseService knowledgeBaseService;
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getRAGConfig(String ragModelId) {
|
||||
@@ -55,32 +40,29 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
public PageData<KnowledgeFilesDTO> getPageList(KnowledgeFilesDTO knowledgeFilesDTO, Integer page, Integer limit) {
|
||||
try {
|
||||
log.info("=== 开始获取文档列表 ===");
|
||||
log.info("查询条件: datasetId={}, name={}, status={}, page={}, limit={}",
|
||||
knowledgeFilesDTO != null ? knowledgeFilesDTO.getDatasetId() : null,
|
||||
knowledgeFilesDTO != null ? knowledgeFilesDTO.getName() : null,
|
||||
knowledgeFilesDTO != null ? knowledgeFilesDTO.getStatus() : null,
|
||||
page, limit);
|
||||
log.info("查询条件: datasetId={}, name={}, status={}, page={}, limit={}", knowledgeFilesDTO.getDatasetId(), knowledgeFilesDTO.getName(), knowledgeFilesDTO.getStatus(), page, limit);
|
||||
|
||||
// 获取数据集ID
|
||||
String datasetId = knowledgeFilesDTO != null ? knowledgeFilesDTO.getDatasetId() : null;
|
||||
if (StringUtils.isBlank(datasetId)) {
|
||||
String datasetId = knowledgeFilesDTO.getDatasetId();
|
||||
if (ToolUtil.isEmpty(datasetId)) {
|
||||
throw new RenException(ErrorCode.RAG_DATASET_ID_NOT_NULL);
|
||||
}
|
||||
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetId);
|
||||
|
||||
// 提取适配器类型
|
||||
String adapterType = extractAdapterType(ragConfig);
|
||||
|
||||
// 使用适配器工厂获取适配器实例
|
||||
KnowledgeBaseAdapter adapter = KnowledgeBaseAdapterFactory.getAdapter(adapterType, ragConfig);
|
||||
|
||||
// 构建查询参数
|
||||
Map<String, Object> queryParams = new HashMap<>();
|
||||
if (knowledgeFilesDTO != null && StringUtils.isNotBlank(knowledgeFilesDTO.getName())) {
|
||||
if (ToolUtil.isNotEmpty(knowledgeFilesDTO.getName())) {
|
||||
queryParams.put("keywords", knowledgeFilesDTO.getName());
|
||||
}
|
||||
if (ToolUtil.isNotEmpty(knowledgeFilesDTO.getStatus())) {
|
||||
queryParams.put("status", knowledgeFilesDTO.getStatus());
|
||||
}
|
||||
if (page > 0) {
|
||||
queryParams.put("page", page);
|
||||
}
|
||||
@@ -90,10 +72,8 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
// 调用适配器获取文档列表
|
||||
PageData<KnowledgeFilesDTO> result = adapter.getDocumentList(datasetId, queryParams, page, limit);
|
||||
|
||||
log.info("获取文档列表成功,共{}个文档,总数: {}", result.getList().size(), result.getTotal());
|
||||
return result;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取文档列表失败: {}", e.getMessage(), e);
|
||||
if (e instanceof RenException) {
|
||||
@@ -424,57 +404,6 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<KnowledgeFilesDTO> getPageListByStatus(String datasetId, Integer status, Integer page,
|
||||
Integer limit) {
|
||||
if (StringUtils.isBlank(datasetId)) {
|
||||
throw new RenException(ErrorCode.RAG_DATASET_ID_NOT_NULL);
|
||||
}
|
||||
|
||||
log.info("=== 开始根据状态查询文档列表 ===");
|
||||
log.info("datasetId: {}, status: {}, page: {}, limit: {}", datasetId, status, page, limit);
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetId);
|
||||
|
||||
// 提取适配器类型
|
||||
String adapterType = extractAdapterType(ragConfig);
|
||||
|
||||
// 使用适配器工厂获取适配器实例
|
||||
KnowledgeBaseAdapter adapter = KnowledgeBaseAdapterFactory.getAdapter(adapterType, ragConfig);
|
||||
|
||||
// 构建查询参数
|
||||
Map<String, Object> queryParams = new HashMap<>();
|
||||
if (page != null && page > 0) {
|
||||
queryParams.put("page", page);
|
||||
}
|
||||
if (limit != null && limit > 0) {
|
||||
queryParams.put("page_size", limit);
|
||||
}
|
||||
if (status != null) {
|
||||
queryParams.put("status", status);
|
||||
}
|
||||
|
||||
// 使用适配器获取文档列表
|
||||
PageData<KnowledgeFilesDTO> pageData = adapter.getDocumentList(datasetId, queryParams, page, limit);
|
||||
|
||||
if (pageData != null) {
|
||||
log.info("根据状态查询文档列表成功,datasetId: {}, 状态: {}, 文档数量: {}",
|
||||
datasetId, status, pageData.getList().size());
|
||||
return pageData;
|
||||
} else {
|
||||
throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("根据状态查询文档列表失败: {}", e.getMessage(), e);
|
||||
throw new RenException(ErrorCode.RAG_API_ERROR, e.getMessage());
|
||||
} finally {
|
||||
log.info("=== 根据状态查询文档列表操作结束 ===");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public KnowledgeFilesDTO uploadDocument(String datasetId, MultipartFile file, String name,
|
||||
Map<String, Object> metaFields, String chunkMethod,
|
||||
|
||||
Reference in New Issue
Block a user