diff --git a/src/main/java/com/gxwebsoft/ai/controller/KnowledgeBaseController.java b/src/main/java/com/gxwebsoft/ai/controller/KnowledgeBaseController.java index a69fb5d..61ff0fb 100644 --- a/src/main/java/com/gxwebsoft/ai/controller/KnowledgeBaseController.java +++ b/src/main/java/com/gxwebsoft/ai/controller/KnowledgeBaseController.java @@ -93,8 +93,9 @@ public class KnowledgeBaseController extends BaseController { } } - boolean result = knowledgeBaseService.uploadDocuments(kbId, files); - + List fileIds = knowledgeBaseService.uploadDocuments(kbId, files); + boolean result = knowledgeBaseService.submitDocuments(kbId, fileIds); + if (result) { return success("成功上传 " + files.length + " 个文件"); } else { diff --git a/src/main/java/com/gxwebsoft/ai/service/KnowledgeBaseService.java b/src/main/java/com/gxwebsoft/ai/service/KnowledgeBaseService.java index dcb3909..3f59a7f 100644 --- a/src/main/java/com/gxwebsoft/ai/service/KnowledgeBaseService.java +++ b/src/main/java/com/gxwebsoft/ai/service/KnowledgeBaseService.java @@ -58,15 +58,15 @@ public interface KnowledgeBaseService { /** * 上传知识库文档 */ - boolean uploadDocuments(String kbId, MultipartFile[] files); + List uploadDocuments(String kbId, MultipartFile[] files); /** * 知识库追加导入已解析的文档 */ - void submitDocuments(String kbId, String fileId); + boolean submitDocuments(String kbId, String fileId); /** * 知识库追加导入已解析的文档 */ - void submitDocuments(String kbId, List fileIds); + boolean submitDocuments(String kbId, List fileIds); } \ No newline at end of file diff --git a/src/main/java/com/gxwebsoft/ai/service/impl/KnowledgeBaseServiceImpl.java b/src/main/java/com/gxwebsoft/ai/service/impl/KnowledgeBaseServiceImpl.java index d3b0b72..41acd13 100644 --- a/src/main/java/com/gxwebsoft/ai/service/impl/KnowledgeBaseServiceImpl.java +++ b/src/main/java/com/gxwebsoft/ai/service/impl/KnowledgeBaseServiceImpl.java @@ -158,6 +158,9 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService { try { Client client = clientFactory.createClient(); DeleteIndexDocumentResponse indexDocumentResponse = AiCloudKnowledgeBaseUtil.deleteIndexDocument(client, workspaceId, kbId, ids); + for(String id : ids) { + AiCloudKnowledgeBaseUtil.deleteAppDocument(client, workspaceId, id); + } return indexDocumentResponse.getBody().getSuccess(); } catch (Exception e) { throw new RuntimeException("删除知识库下的文档失败: " + e.getMessage(), e); @@ -165,7 +168,7 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService { } @Override - public boolean uploadDocuments(String kbId, MultipartFile[] files) { + public List uploadDocuments(String kbId, MultipartFile[] files) { String workspaceId = config.getWorkspaceId(); int count = files.length; try { @@ -173,16 +176,16 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService { List fileIds = new ArrayList<>(); for(MultipartFile file : files) { - AddFileResponse addFileResponse = AiCloudDataCenterUtil.uploadFile(client, workspaceId, "", file); + AddFileResponse addFileResponse = AiCloudDataCenterUtil.uploadFile(client, workspaceId, "cate_28190570c20043d692897701d4547401_10377381", file); String fileId = addFileResponse.getBody().getData().getFileId(); fileIds.add(fileId); } // List fileIds = AiCloudKnowledgeBaseUtil.uploadDocuments(client, workspaceId, kbId, files); //上传切片完成后删除原文档(释放云空间) - for(String fileId : fileIds) { - AiCloudKnowledgeBaseUtil.deleteAppDocument(client, workspaceId, fileId); - } - return !fileIds.isEmpty() && fileIds.size() == count; +// for(String fileId : fileIds) { +// AiCloudKnowledgeBaseUtil.deleteAppDocument(client, workspaceId, fileId); +// } + return fileIds; } catch (Exception e) { throw new RuntimeException("上传文档到知识库失败: " + e.getMessage(), e); } @@ -190,25 +193,28 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService { @Async @Override - public void submitDocuments(String kbId, String fileId) { + public boolean submitDocuments(String kbId, String fileId) { String workspaceId = config.getWorkspaceId(); try { Client client = clientFactory.createClient(); - AiCloudKnowledgeBaseUtil.submitIndexAddDocumentsJob(client, workspaceId, kbId, fileId); + boolean result = AiCloudKnowledgeBaseUtil.submitIndexAddDocumentsJob(client, workspaceId, kbId, fileId).getBody().getSuccess(); + return result; } catch (Exception e) { throw new RuntimeException("添加文档到知识库失败: " + e.getMessage(), e); } } @Override - public void submitDocuments(String kbId, List fileIds) { + public boolean submitDocuments(String kbId, List fileIds) { String workspaceId = config.getWorkspaceId(); + boolean result = true; try { Client client = clientFactory.createClient(); - AiCloudKnowledgeBaseUtil.submitIndexAddDocumentsJob(client, workspaceId, kbId, fileIds); + result = result && AiCloudKnowledgeBaseUtil.submitIndexAddDocumentsJob(client, workspaceId, kbId, fileIds).getBody().getSuccess(); } catch (Exception e) { throw new RuntimeException("添加文档到知识库失败: " + e.getMessage(), e); } + return result; }