diff --git a/src/main/java/com/supervision/chat/client/LangChainChatService.java b/src/main/java/com/supervision/chat/client/LangChainChatService.java index bb3ce29..47849a4 100644 --- a/src/main/java/com/supervision/chat/client/LangChainChatService.java +++ b/src/main/java/com/supervision/chat/client/LangChainChatService.java @@ -5,10 +5,9 @@ import com.supervision.chat.client.dto.DeleteFileDTO; import com.supervision.chat.client.dto.LangChainChatRes; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.service.annotation.GetExchange; import org.springframework.web.service.annotation.HttpExchange; import org.springframework.web.service.annotation.PostExchange; @@ -57,5 +56,8 @@ public interface LangChainChatService { @PostExchange(url = "delete_docs", contentType = MediaType.APPLICATION_JSON_VALUE) LangChainChatRes deleteFile(@RequestBody DeleteFileDTO deleteFileDTO); + @GetExchange(url = "list_files") + LangChainChatRes queryFileList(@RequestParam String knowledge_base_name); + } diff --git a/src/main/java/com/supervision/police/service/impl/NoteRecordServiceImpl.java b/src/main/java/com/supervision/police/service/impl/NoteRecordServiceImpl.java index 17f0302..3517f48 100644 --- a/src/main/java/com/supervision/police/service/impl/NoteRecordServiceImpl.java +++ b/src/main/java/com/supervision/police/service/impl/NoteRecordServiceImpl.java @@ -1,5 +1,6 @@ package com.supervision.police.service.impl; +import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.supervision.chat.UploadParamEnum; import com.supervision.chat.client.CustomMultipartFile; @@ -21,7 +22,9 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.io.InputStream; +import java.util.HashSet; import java.util.List; +import java.util.Set; @Slf4j @Service @@ -43,45 +46,60 @@ public class NoteRecordServiceImpl extends ServiceImpl recordList = this.lambdaQuery().eq(NoteRecord::getCaseId, caseId).list(); ModelCase modelCase = modelCaseService.getById(caseId); + if (StringUtils.isEmpty(modelCase.getCaseNo())) { + log.info("案件编号不能为空"); + return; + } + // 查询知识库中已经存在的文件 + LangChainChatRes queryFileListRes = langChainChatService.queryFileList(modelCase.getCaseNo()); + if (!queryFileListRes.getCode().equals(200)) { + log.info("获取知识库已存在的文件失败"); + } + HashSet knowledgeFileNameSet = new HashSet<>(JSONUtil.toList((String) queryFileListRes.getData(), String.class)); + Set databaseFileNameSet = new HashSet<>(); for (NoteRecord record : recordList) { - if (StringUtils.isEmpty(modelCase.getCaseNo())) { - throw new BusinessException("案件编号不能为空"); - } for (String fileId : record.getFileIds().split(",")) { MinioFile minioFile = minioService.getMinioFile(fileId); InputStream inputStream = null; - try { - inputStream = minioService.getObjectInputStream(minioFile); - // 这里需要把文件传输,传输到知识库中去 - CustomMultipartFile mockMultipartFile = new CustomMultipartFile(minioFile.getFilename(), inputStream); - LangChainChatRes langChainChatRes = langChainChatService.uploadFile(modelCase.getCaseNo(), - mockMultipartFile, - "问讯笔录", - UploadParamEnum.to_vector_store.getBooleanValue(), - UploadParamEnum.override.getBooleanValue(), - UploadParamEnum.not_refresh_vs_cache.getBooleanValue(), - UploadParamEnum.chunk_size.getIntValue(), - UploadParamEnum.chunk_overlap.getIntValue(), - UploadParamEnum.zh_title_enhance.getBooleanValue(), - UploadParamEnum.docs.getStrValue()); - if (!langChainChatRes.getCode().equals(200)) { - log.error("上传文件到知识库失败:{}", langChainChatRes.getMsg()); + databaseFileNameSet.add(minioFile.getFilename()); + if (!knowledgeFileNameSet.contains(minioFile.getFilename())){ + try { + inputStream = minioService.getObjectInputStream(minioFile); + // 这里需要把文件传输,传输到知识库中去 + CustomMultipartFile mockMultipartFile = new CustomMultipartFile(minioFile.getFilename(), inputStream); + LangChainChatRes langChainChatRes = langChainChatService.uploadFile(modelCase.getCaseNo(), + mockMultipartFile, + "问讯笔录", + UploadParamEnum.to_vector_store.getBooleanValue(), + UploadParamEnum.override.getBooleanValue(), + UploadParamEnum.not_refresh_vs_cache.getBooleanValue(), + UploadParamEnum.chunk_size.getIntValue(), + UploadParamEnum.chunk_overlap.getIntValue(), + UploadParamEnum.zh_title_enhance.getBooleanValue(), + UploadParamEnum.docs.getStrValue()); + if (!langChainChatRes.getCode().equals(200)) { + log.error("上传文件到知识库失败:{}", langChainChatRes.getMsg()); + }else { + knowledgeFileNameSet.add(minioFile.getFilename()); + } + } catch (Exception e) { + e.printStackTrace(); + log.error("从minio中获取文件失败:{}", e.getMessage()); + continue; } - } catch (Exception e) { - e.printStackTrace(); - log.error("从minio中获取文件失败:{}", e.getMessage()); - continue; } } } - - // 删除文件 -// LangChainChatRes langChainChatRes = langChainChatService.deleteFile(DeleteFileDTO.create(noteRecord.getCaseId(), minioFile.getFilename())); -// if (!langChainChatRes.getCode().equals(200)) { -// throw new BusinessException("删除文件:" + minioFile.getFilename() + " 失败!"); -// } - + // 遍历完成,删除知识库有,数据库没有的文件 + for (String fileName : knowledgeFileNameSet) { + if (!databaseFileNameSet.contains(fileName)) { + LangChainChatRes langChainChatRes = langChainChatService.deleteFile(DeleteFileDTO.create(modelCase.getCaseNo(), fileName)); + if (!langChainChatRes.getCode().equals(200)) { + log.error("删除文件:{} 失败!", fileName); + } + } + } } }