优化上传文档到知识库(公共库)接口
This commit is contained in:
@@ -93,7 +93,8 @@ 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) {
|
if (result) {
|
||||||
return success("成功上传 " + files.length + " 个文件");
|
return success("成功上传 " + files.length + " 个文件");
|
||||||
|
|||||||
@@ -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 {
|
try {
|
||||||
Client client = clientFactory.createClient();
|
Client client = clientFactory.createClient();
|
||||||
DeleteIndexDocumentResponse indexDocumentResponse = AiCloudKnowledgeBaseUtil.deleteIndexDocument(client, workspaceId, kbId, ids);
|
DeleteIndexDocumentResponse indexDocumentResponse = AiCloudKnowledgeBaseUtil.deleteIndexDocument(client, workspaceId, kbId, ids);
|
||||||
|
for(String id : ids) {
|
||||||
|
AiCloudKnowledgeBaseUtil.deleteAppDocument(client, workspaceId, id);
|
||||||
|
}
|
||||||
return indexDocumentResponse.getBody().getSuccess();
|
return indexDocumentResponse.getBody().getSuccess();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("删除知识库下的文档失败: " + e.getMessage(), e);
|
throw new RuntimeException("删除知识库下的文档失败: " + e.getMessage(), e);
|
||||||
@@ -165,7 +168,7 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean uploadDocuments(String kbId, MultipartFile[] files) {
|
public List<String> uploadDocuments(String kbId, MultipartFile[] files) {
|
||||||
String workspaceId = config.getWorkspaceId();
|
String workspaceId = config.getWorkspaceId();
|
||||||
int count = files.length;
|
int count = files.length;
|
||||||
try {
|
try {
|
||||||
@@ -173,16 +176,16 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
|
|||||||
|
|
||||||
List<String> fileIds = new ArrayList<>();
|
List<String> fileIds = new ArrayList<>();
|
||||||
for(MultipartFile file : files) {
|
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();
|
String fileId = addFileResponse.getBody().getData().getFileId();
|
||||||
fileIds.add(fileId);
|
fileIds.add(fileId);
|
||||||
}
|
}
|
||||||
// List<String> fileIds = AiCloudKnowledgeBaseUtil.uploadDocuments(client, workspaceId, kbId, files);
|
// List<String> fileIds = AiCloudKnowledgeBaseUtil.uploadDocuments(client, workspaceId, kbId, files);
|
||||||
//上传切片完成后删除原文档(释放云空间)
|
//上传切片完成后删除原文档(释放云空间)
|
||||||
for(String fileId : fileIds) {
|
// for(String fileId : fileIds) {
|
||||||
AiCloudKnowledgeBaseUtil.deleteAppDocument(client, workspaceId, fileId);
|
// AiCloudKnowledgeBaseUtil.deleteAppDocument(client, workspaceId, fileId);
|
||||||
}
|
// }
|
||||||
return !fileIds.isEmpty() && fileIds.size() == count;
|
return fileIds;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("上传文档到知识库失败: " + e.getMessage(), e);
|
throw new RuntimeException("上传文档到知识库失败: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@@ -190,25 +193,28 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
|
|||||||
|
|
||||||
@Async
|
@Async
|
||||||
@Override
|
@Override
|
||||||
public void submitDocuments(String kbId, String fileId) {
|
public boolean submitDocuments(String kbId, String fileId) {
|
||||||
String workspaceId = config.getWorkspaceId();
|
String workspaceId = config.getWorkspaceId();
|
||||||
try {
|
try {
|
||||||
Client client = clientFactory.createClient();
|
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) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("添加文档到知识库失败: " + e.getMessage(), e);
|
throw new RuntimeException("添加文档到知识库失败: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void submitDocuments(String kbId, List<String> fileIds) {
|
public boolean submitDocuments(String kbId, List<String> fileIds) {
|
||||||
String workspaceId = config.getWorkspaceId();
|
String workspaceId = config.getWorkspaceId();
|
||||||
|
boolean result = true;
|
||||||
try {
|
try {
|
||||||
Client client = clientFactory.createClient();
|
Client client = clientFactory.createClient();
|
||||||
AiCloudKnowledgeBaseUtil.submitIndexAddDocumentsJob(client, workspaceId, kbId, fileIds);
|
result = result && AiCloudKnowledgeBaseUtil.submitIndexAddDocumentsJob(client, workspaceId, kbId, fileIds).getBody().getSuccess();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("添加文档到知识库失败: " + e.getMessage(), e);
|
throw new RuntimeException("添加文档到知识库失败: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user