mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
uptate:删除知识库无用接口和方法
This commit is contained in:
-33
@@ -43,15 +43,6 @@ public class KnowledgeFilesController {
|
||||
return new Result<PageData<KnowledgeFilesDTO>>().ok(pageData);
|
||||
}
|
||||
|
||||
@GetMapping("/documents/{document_id}")
|
||||
@Operation(summary = "根据文档ID获取文档详情")
|
||||
@RequiresPermissions("sys:role:normal")
|
||||
public Result<KnowledgeFilesDTO> getByDocumentId(@PathVariable("dataset_id") String datasetId,
|
||||
@PathVariable("document_id") String documentId) {
|
||||
KnowledgeFilesDTO knowledgeFilesDTO = knowledgeFilesService.getByDocumentId(documentId);
|
||||
return new Result<KnowledgeFilesDTO>().ok(knowledgeFilesDTO);
|
||||
}
|
||||
|
||||
@PostMapping("/documents")
|
||||
@Operation(summary = "上传文档到知识库")
|
||||
@RequiresPermissions("sys:role:normal")
|
||||
@@ -70,18 +61,6 @@ public class KnowledgeFilesController {
|
||||
return new Result<KnowledgeFilesDTO>().ok(resp);
|
||||
}
|
||||
|
||||
@PutMapping("/documents/{document_id}")
|
||||
@Operation(summary = "更新文档配置")
|
||||
@RequiresPermissions("sys:role:normal")
|
||||
public Result<KnowledgeFilesDTO> update(@PathVariable("dataset_id") String datasetId,
|
||||
@PathVariable("document_id") String documentId,
|
||||
@RequestBody @Validated KnowledgeFilesDTO knowledgeFilesDTO) {
|
||||
knowledgeFilesDTO.setDatasetId(datasetId);
|
||||
knowledgeFilesDTO.setDocumentId(documentId);
|
||||
KnowledgeFilesDTO resp = knowledgeFilesService.update(knowledgeFilesDTO);
|
||||
return new Result<KnowledgeFilesDTO>().ok(resp);
|
||||
}
|
||||
|
||||
@DeleteMapping("/documents/{document_id}")
|
||||
@Operation(summary = "删除单个文档")
|
||||
@Parameter(name = "document_id", description = "文档ID", required = true)
|
||||
@@ -92,18 +71,6 @@ public class KnowledgeFilesController {
|
||||
return new Result<>();
|
||||
}
|
||||
|
||||
@DeleteMapping("/documents")
|
||||
@Operation(summary = "批量删除文档")
|
||||
@RequiresPermissions("sys:role:normal")
|
||||
public Result<Void> deleteBatch(@PathVariable("dataset_id") String datasetId,
|
||||
@RequestBody Map<String, List<String>> requestBody) {
|
||||
List<String> ids = requestBody.get("ids");
|
||||
if (ids != null && !ids.isEmpty()) {
|
||||
knowledgeFilesService.deleteBatch(ids);
|
||||
}
|
||||
return new Result<>();
|
||||
}
|
||||
|
||||
@PostMapping("/chunks")
|
||||
@Operation(summary = "批量解析文档(切块)")
|
||||
@RequiresPermissions("sys:role:normal")
|
||||
|
||||
@@ -44,7 +44,7 @@ public class KnowledgeFilesDTO implements Serializable {
|
||||
@Schema(description = "解析器配置")
|
||||
private Map<String, Object> parserConfig;
|
||||
|
||||
@Schema(description = "状态(0:待解析 1:解析中 2:解析成功 3:解析失败)")
|
||||
@Schema(description = "状态")
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "创建者")
|
||||
|
||||
-45
@@ -23,22 +23,6 @@ public interface KnowledgeFilesService {
|
||||
*/
|
||||
PageData<KnowledgeFilesDTO> getPageList(KnowledgeFilesDTO knowledgeFilesDTO, Integer page, Integer limit);
|
||||
|
||||
/**
|
||||
* 根据ID获取文档详情
|
||||
*
|
||||
* @param id 文档ID
|
||||
* @return 文档详情
|
||||
*/
|
||||
KnowledgeFilesDTO getById(String id);
|
||||
|
||||
/**
|
||||
* 根据文档ID获取文档详情
|
||||
*
|
||||
* @param documentId 文档ID
|
||||
* @return 文档详情
|
||||
*/
|
||||
KnowledgeFilesDTO getByDocumentId(String documentId);
|
||||
|
||||
/**
|
||||
* 根据文档ID和知识库ID获取文档详情
|
||||
*
|
||||
@@ -63,28 +47,6 @@ public interface KnowledgeFilesService {
|
||||
Map<String, Object> metaFields, String chunkMethod,
|
||||
Map<String, Object> parserConfig);
|
||||
|
||||
/**
|
||||
* 更新文档配置
|
||||
*
|
||||
* @param knowledgeFilesDTO 文档信息
|
||||
* @return 更新的文档信息
|
||||
*/
|
||||
KnowledgeFilesDTO update(KnowledgeFilesDTO knowledgeFilesDTO);
|
||||
|
||||
/**
|
||||
* 根据ID删除文档
|
||||
*
|
||||
* @param id 文档ID
|
||||
*/
|
||||
void delete(String id);
|
||||
|
||||
/**
|
||||
* 根据文档ID删除文档
|
||||
*
|
||||
* @param documentId 文档ID
|
||||
*/
|
||||
void deleteByDocumentId(String documentId);
|
||||
|
||||
/**
|
||||
* 根据文档ID和知识库ID删除文档
|
||||
*
|
||||
@@ -93,13 +55,6 @@ public interface KnowledgeFilesService {
|
||||
*/
|
||||
void deleteByDocumentId(String documentId, String datasetId);
|
||||
|
||||
/**
|
||||
* 批量删除文档
|
||||
*
|
||||
* @param ids 文档ID列表
|
||||
*/
|
||||
void deleteBatch(List<String> ids);
|
||||
|
||||
|
||||
/**
|
||||
* 获取RAG配置信息
|
||||
|
||||
+5
-87
@@ -65,9 +65,6 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
|
||||
IPage<KnowledgeBaseEntity> knowledgeBaseEntityIPage = knowledgeBaseDao.selectPage(pageInfo, queryWrapper);
|
||||
|
||||
// 同步RAGFlow API的数据集状态(可选功能,可根据需要开启)
|
||||
syncRAGFlowDatasetStatus(knowledgeBaseEntityIPage.getRecords());
|
||||
|
||||
// 获取分页数据
|
||||
PageData<KnowledgeBaseDTO> pageData = getPageData(knowledgeBaseEntityIPage, KnowledgeBaseDTO.class);
|
||||
|
||||
@@ -214,8 +211,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
if (StringUtils.isNotBlank(entity.getDatasetId()) && StringUtils.isNotBlank(entity.getRagModelId())) {
|
||||
try {
|
||||
log.info("开始调用RAGFlow API删除数据集");
|
||||
Map<String, Object> ragConfig = getRAGConfig(entity.getRagModelId());
|
||||
validateRagConfig(ragConfig);
|
||||
Map<String, Object> ragConfig = getValidatedRAGConfig(entity.getRagModelId());
|
||||
deleteDatasetInRAGFlow(entity.getDatasetId(), ragConfig);
|
||||
log.info("RAGFlow API删除调用完成");
|
||||
apiDeleteSuccess = true;
|
||||
@@ -597,16 +593,11 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
* 获取RAG配置并验证
|
||||
*/
|
||||
private Map<String, Object> getValidatedRAGConfig(String ragModelId) {
|
||||
Map<String, Object> ragConfig;
|
||||
if (StringUtils.isNotBlank(ragModelId)) {
|
||||
ragConfig = getRAGConfig(ragModelId);
|
||||
return getRAGConfig(ragModelId);
|
||||
} else {
|
||||
ragConfig = getDefaultRAGConfig();
|
||||
return getDefaultRAGConfig();
|
||||
}
|
||||
|
||||
// 验证配置
|
||||
validateRagConfig(ragConfig);
|
||||
return ragConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -692,80 +683,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步RAGFlow API的数据集状态(可选功能)
|
||||
*/
|
||||
private void syncRAGFlowDatasetStatus(List<KnowledgeBaseEntity> entities) {
|
||||
if (entities == null || entities.isEmpty()) {
|
||||
log.debug("没有需要同步状态的数据集");
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("开始同步RAGFlow数据集状态,共{}个数据集", entities.size());
|
||||
|
||||
for (KnowledgeBaseEntity entity : entities) {
|
||||
if (StringUtils.isNotBlank(entity.getDatasetId()) && StringUtils.isNotBlank(entity.getRagModelId())) {
|
||||
try {
|
||||
log.debug("开始同步数据集 {} 的状态", entity.getDatasetId());
|
||||
|
||||
Map<String, Object> ragConfig = getValidatedRAGConfig(entity.getRagModelId());
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
// 使用正确的API端点获取数据集列表,然后过滤
|
||||
String url = baseUrl + "/api/v1/datasets";
|
||||
log.debug("请求URL: {}", url);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Authorization", "Bearer " + apiKey);
|
||||
|
||||
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
|
||||
|
||||
// 发送GET请求获取所有数据集
|
||||
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Map.class);
|
||||
|
||||
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
|
||||
Map<String, Object> responseBody = response.getBody();
|
||||
// 根据响应结构判断数据集是否存在
|
||||
boolean exists = checkDatasetExists(responseBody, entity.getDatasetId());
|
||||
|
||||
if (exists) {
|
||||
log.info("数据集 {} 在RAGFlow中状态正常", entity.getDatasetId());
|
||||
} else {
|
||||
log.warn("数据集 {} 在RAGFlow中不存在", entity.getDatasetId());
|
||||
}
|
||||
} else {
|
||||
log.warn("获取数据集列表失败,状态码: {}", response.getStatusCode());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("同步数据集 {} 状态失败: {}", entity.getDatasetId(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.info("RAGFlow数据集状态同步完成");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查数据集是否存在
|
||||
*/
|
||||
private boolean checkDatasetExists(Map<String, Object> responseBody, String datasetId) {
|
||||
try {
|
||||
// 根据RAGFlow API的实际响应结构来解析
|
||||
if (responseBody.containsKey("data")) {
|
||||
Object data = responseBody.get("data");
|
||||
if (data instanceof List) {
|
||||
List<Map<String, Object>> datasets = (List<Map<String, Object>>) data;
|
||||
return datasets.stream()
|
||||
.anyMatch(dataset -> datasetId.equals(dataset.get("id")) ||
|
||||
datasetId.equals(dataset.get("dataset_id")));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
log.error("检查数据集存在性失败: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+11
-120
@@ -1,10 +1,9 @@
|
||||
package xiaozhi.modules.knowledge.service.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -55,7 +54,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
page, limit);
|
||||
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getRAGConfig();
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -337,14 +336,6 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Map中获取字符串值
|
||||
*/
|
||||
private String getStringValue(Map<String, Object> map, String key) {
|
||||
Object value = map.get(key);
|
||||
return value != null ? value.toString() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从多个可能的字段名中获取字符串值
|
||||
*/
|
||||
@@ -371,34 +362,6 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KnowledgeFilesDTO getById(String id) {
|
||||
if (StringUtils.isBlank(id)) {
|
||||
throw new RenException(ErrorCode.IDENTIFIER_NOT_NULL);
|
||||
}
|
||||
|
||||
log.info("=== 开始根据ID获取文档 ===");
|
||||
log.info("文档ID: {}", id);
|
||||
|
||||
try {
|
||||
KnowledgeFilesDTO queryDTO = new KnowledgeFilesDTO();
|
||||
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR, "请使用getByDocumentId方法,并提供datasetId");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("根据ID获取文档失败: {}", e.getMessage(), e);
|
||||
throw new RenException(ErrorCode.RAG_API_QUERY_FAILED, "获取文档失败: " + e.getMessage());
|
||||
} finally {
|
||||
log.info("=== 根据ID获取文档操作结束 ===");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public KnowledgeFilesDTO getByDocumentId(String documentId) {
|
||||
// 重载方法,保持向后兼容,但实际需要datasetId
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR, "请使用getByDocumentId(documentId, datasetId)方法,并提供datasetId");
|
||||
}
|
||||
|
||||
@Override
|
||||
public KnowledgeFilesDTO getByDocumentId(String documentId, String datasetId) {
|
||||
if (StringUtils.isBlank(documentId) || StringUtils.isBlank(datasetId)) {
|
||||
@@ -410,7 +373,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getRAGConfig();
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -546,56 +509,6 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public KnowledgeFilesDTO update(KnowledgeFilesDTO knowledgeFilesDTO) {
|
||||
if (knowledgeFilesDTO == null || StringUtils.isBlank(knowledgeFilesDTO.getDocumentId())) {
|
||||
throw new RenException(ErrorCode.IDENTIFIER_NOT_NULL);
|
||||
}
|
||||
|
||||
log.info("=== 开始更新文档操作 ===");
|
||||
log.info("更新文档documentId: {}", knowledgeFilesDTO.getDocumentId());
|
||||
|
||||
try {
|
||||
// 调用RAGFlow API更新文档配置
|
||||
log.info("开始调用RAGFlow API更新文档配置");
|
||||
updateDocumentInRAGFlow(
|
||||
knowledgeFilesDTO.getDocumentId(),
|
||||
knowledgeFilesDTO.getName(),
|
||||
knowledgeFilesDTO.getMetaFields(),
|
||||
knowledgeFilesDTO.getChunkMethod(),
|
||||
knowledgeFilesDTO.getParserConfig()
|
||||
);
|
||||
log.info("RAGFlow API更新调用完成");
|
||||
|
||||
// 返回更新后的文档信息(通过查询获取最新状态)
|
||||
// 需要datasetId,这里假设knowledgeFilesDTO中包含datasetId
|
||||
if (StringUtils.isBlank(knowledgeFilesDTO.getDatasetId())) {
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR, "更新文档需要datasetId");
|
||||
}
|
||||
return getByDocumentId(knowledgeFilesDTO.getDocumentId(), knowledgeFilesDTO.getDatasetId());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("更新文档失败: {}", e.getMessage(), e);
|
||||
if (e instanceof RenException) {
|
||||
throw (RenException) e;
|
||||
}
|
||||
throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED, "更新文档失败: " + e.getMessage());
|
||||
} finally {
|
||||
log.info("=== 更新文档操作结束 ===");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String documentId) {
|
||||
// 重载方法,保持向后兼容,但实际需要datasetId
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR, "请使用deleteByDocumentId(documentId, datasetId)方法,并提供datasetId");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteByDocumentId(String documentId) {
|
||||
// 重载方法,保持向后兼容,但实际需要datasetId
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR, "请使用deleteByDocumentId(documentId, datasetId)方法,并提供datasetId");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteByDocumentId(String documentId, String datasetId) {
|
||||
@@ -621,21 +534,6 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteBatch(List<String> ids) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR);
|
||||
}
|
||||
|
||||
log.info("=== 开始批量删除文档操作 ===");
|
||||
log.info("批量删除文档数量: {}", ids.size());
|
||||
|
||||
// 由于批量删除需要datasetId,这里无法处理
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR, "批量删除需要datasetId,请使用单个删除接口");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取文件类型 - 支持RAGFlow四种文档格式类型
|
||||
*/
|
||||
@@ -764,13 +662,6 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认RAG配置
|
||||
*/
|
||||
private Map<String, Object> getRAGConfig() {
|
||||
return getDefaultRAGConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用RAGFlow API上传文档 - 流式上传版本
|
||||
*/
|
||||
@@ -779,7 +670,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
Map<String, Object> parserConfig) {
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getRAGConfig();
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -971,7 +862,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
String chunkMethod, Map<String, Object> parserConfig) {
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getRAGConfig();
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -1045,7 +936,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
private void deleteDocumentInRAGFlow(String documentId, String datasetId) {
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getRAGConfig();
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -1172,7 +1063,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getRAGConfig();
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -1253,7 +1144,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getRAGConfig();
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -1344,7 +1235,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getRAGConfig();
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -1745,7 +1636,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getRAGConfig();
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user