|
|
|
@ -10,6 +10,7 @@ import com.google.gson.JsonObject;
|
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
|
import com.supervision.minio.domain.MinioFile;
|
|
|
|
|
import com.supervision.minio.service.MinioService;
|
|
|
|
|
import com.supervision.police.domain.ModelCase;
|
|
|
|
|
import com.supervision.police.vo.dify.DatasetReqVO;
|
|
|
|
|
import com.supervision.police.vo.dify.DatasetResVO;
|
|
|
|
|
import com.supervision.police.vo.dify.DifyChatReqVO;
|
|
|
|
@ -30,9 +31,8 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import static com.supervision.common.constant.DifyConstants.*;
|
|
|
|
|
|
|
|
|
@ -257,6 +257,58 @@ public class DifyApiUtil {
|
|
|
|
|
return documents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void uploadCaseFileToDifyKnowledgeBase(ModelCase modelCase,List<String> allFileIds) {
|
|
|
|
|
|
|
|
|
|
if (StrUtil.isEmpty(modelCase.getKnowledgeBaseId())){
|
|
|
|
|
log.warn("uploadRecordFileToKnowledgeBase:案件:{}案件或者知识库为空,不进行知识库维护!", modelCase.getCaseName());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<Document> documents = this.queryDocuments(modelCase.getKnowledgeBaseId());
|
|
|
|
|
Set<String> documentFileIds = documents.stream().map(Document::getFileId).collect(Collectors.toSet());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 只上传 doc docx、txt、md、pdf 文件,且文件大小不能超过15mb
|
|
|
|
|
List<MinioFile> minioFiles = minioService.listMinioFile(allFileIds);
|
|
|
|
|
List<String> recordFileIds = minioFiles.stream().filter(minioFile -> {
|
|
|
|
|
boolean currentFileSize = minioFile.getSize() < 15 * 1024 * 1024;
|
|
|
|
|
if (!currentFileSize) {
|
|
|
|
|
log.warn("文件大小超过15mb,不进行知识库维护:{}", minioFile.getFilename());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
boolean currentFileType = StrUtil.equalsAny(minioFile.getFileType(), "doc", "docx", "txt", "md", "pdf");
|
|
|
|
|
if (!currentFileType) {
|
|
|
|
|
log.warn("文件:{}类型非doc、docx、txt、md、pdf,不进行知识库维护...", minioFile.getFilename());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}).map(MinioFile::getId).toList();
|
|
|
|
|
|
|
|
|
|
Map<String, MinioFile> fileMap = minioFiles.stream().collect(Collectors.toMap(MinioFile::getId, target -> target));
|
|
|
|
|
|
|
|
|
|
log.info("案件:{},共有:{}个笔录文件,符合上传要求的文件有:{}",modelCase.getCaseName(), allFileIds.size(), recordFileIds.size());
|
|
|
|
|
for (String recordId : recordFileIds) {
|
|
|
|
|
// 把新增的笔录数据添加到到知识库
|
|
|
|
|
if (!documentFileIds.contains(recordId)){
|
|
|
|
|
log.info("案件:{},笔录文件:{},添加到知识库...",modelCase.getCaseName(), fileMap.get(recordId).getFilename());
|
|
|
|
|
this.createDocumentByFile(modelCase.getKnowledgeBaseId(),recordId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (CollUtil.isNotEmpty(recordFileIds)){
|
|
|
|
|
for (Document document : documents) {
|
|
|
|
|
String fileId = document.getFileId();
|
|
|
|
|
if (StrUtil.isNotEmpty(fileId) && !recordFileIds.contains(fileId)){
|
|
|
|
|
// 删除不在笔录文件列表中的知识库
|
|
|
|
|
log.info("案件:{},笔录文件:{},从知识库中删除...",modelCase.getCaseName(), document.getName());
|
|
|
|
|
this.deleteDocument(modelCase.getKnowledgeBaseId(),document.getId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.info("案件:{}上传笔录文件到知识库完成!",modelCase.getCaseName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成文档名称
|
|
|
|
@ -265,7 +317,7 @@ public class DifyApiUtil {
|
|
|
|
|
* @param fileId 文件id
|
|
|
|
|
* @return 一个完整的文档名: 文件名 + "_" + 文件ID +"." + 后缀
|
|
|
|
|
*/
|
|
|
|
|
public String generateDocumentName(String fileName, String fileId) {
|
|
|
|
|
private String generateDocumentName(String fileName, String fileId) {
|
|
|
|
|
|
|
|
|
|
String[] split = fileName.split("\\.");
|
|
|
|
|
List<String> nameTrunk = new ArrayList<>(Arrays.asList(split).subList(0, split.length - 1));
|
|
|
|
@ -283,7 +335,7 @@ public class DifyApiUtil {
|
|
|
|
|
* @param documentName 文档名
|
|
|
|
|
* @return key:文件名,value:文件id
|
|
|
|
|
*/
|
|
|
|
|
public Pair<String, String> decodeDocumentName(String documentName) {
|
|
|
|
|
private Pair<String, String> decodeDocumentName(String documentName) {
|
|
|
|
|
|
|
|
|
|
if (StrUtil.isEmpty(documentName)) {
|
|
|
|
|
return Pair.of(null, null);
|
|
|
|
|