优化上传文档到知识库(公共库)接口
This commit is contained in:
@@ -93,8 +93,9 @@ public class KnowledgeBaseController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
boolean result = knowledgeBaseService.uploadDocuments(kbId, files);
|
||||
|
||||
List<String> fileIds = knowledgeBaseService.uploadDocuments(kbId, files);
|
||||
boolean result = knowledgeBaseService.submitDocuments(kbId, fileIds);
|
||||
|
||||
if (result) {
|
||||
return success("成功上传 " + files.length + " 个文件");
|
||||
} else {
|
||||
|
||||
@@ -58,15 +58,15 @@ public interface KnowledgeBaseService {
|
||||
/**
|
||||
* 上传知识库文档
|
||||
*/
|
||||
boolean uploadDocuments(String kbId, MultipartFile[] files);
|
||||
List<String> uploadDocuments(String kbId, MultipartFile[] files);
|
||||
|
||||
/**
|
||||
* 知识库追加导入已解析的文档
|
||||
*/
|
||||
void submitDocuments(String kbId, String fileId);
|
||||
boolean submitDocuments(String kbId, String fileId);
|
||||
|
||||
/**
|
||||
* 知识库追加导入已解析的文档
|
||||
*/
|
||||
void submitDocuments(String kbId, List<String> fileIds);
|
||||
boolean submitDocuments(String kbId, List<String> fileIds);
|
||||
}
|
||||
@@ -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<String> uploadDocuments(String kbId, MultipartFile[] files) {
|
||||
String workspaceId = config.getWorkspaceId();
|
||||
int count = files.length;
|
||||
try {
|
||||
@@ -173,16 +176,16 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
|
||||
|
||||
List<String> 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<String> 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<String> fileIds) {
|
||||
public boolean submitDocuments(String kbId, List<String> 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user