clean code
This commit is contained in:
parent
dc7498806e
commit
b8476c2398
|
|
@ -6,7 +6,6 @@ import jakarta.servlet.ServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.StopWatch;
|
import org.springframework.util.StopWatch;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ import org.springframework.web.method.HandlerMethod;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@ package com.electromagnetic.industry.software.manage.controller;
|
||||||
|
|
||||||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||||
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
||||||
import com.electromagnetic.industry.software.manage.pojo.req.QueryDTO;
|
|
||||||
import com.electromagnetic.industry.software.manage.service.serviceimpl.ChatService;
|
|
||||||
import com.electromagnetic.industry.software.manage.ai.ChatTaskThread1;
|
import com.electromagnetic.industry.software.manage.ai.ChatTaskThread1;
|
||||||
import com.electromagnetic.industry.software.manage.ai.ChatTaskThread2;
|
import com.electromagnetic.industry.software.manage.ai.ChatTaskThread2;
|
||||||
import com.electromagnetic.industry.software.manage.ai.ChatTaskThread3;
|
import com.electromagnetic.industry.software.manage.ai.ChatTaskThread3;
|
||||||
import com.electromagnetic.industry.software.manage.ai.ThreadUtil;
|
import com.electromagnetic.industry.software.manage.ai.ThreadUtil;
|
||||||
|
import com.electromagnetic.industry.software.manage.pojo.req.QueryDTO;
|
||||||
|
import com.electromagnetic.industry.software.manage.service.serviceimpl.ChatService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.ai.chat.model.ChatResponse;
|
import org.springframework.ai.chat.model.ChatResponse;
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ import com.electromagnetic.industry.software.manage.pojo.req.CreateFolderDTO;
|
||||||
import com.electromagnetic.industry.software.manage.pojo.req.FileChunkDTO;
|
import com.electromagnetic.industry.software.manage.pojo.req.FileChunkDTO;
|
||||||
import com.electromagnetic.industry.software.manage.pojo.req.FileInfoQueryDTO;
|
import com.electromagnetic.industry.software.manage.pojo.req.FileInfoQueryDTO;
|
||||||
import com.electromagnetic.industry.software.manage.pojo.req.UpdateFileInfoDTO;
|
import com.electromagnetic.industry.software.manage.pojo.req.UpdateFileInfoDTO;
|
||||||
import com.electromagnetic.industry.software.manage.service.EdFileFavoriteService;
|
|
||||||
import com.electromagnetic.industry.software.manage.service.EdFileInfoService;
|
import com.electromagnetic.industry.software.manage.service.EdFileInfoService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
@ -20,7 +19,6 @@ import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.electromagnetic.industry.software.manage.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.electromagnetic.industry.software.manage.pojo.models.AiFileUploadRecord;
|
||||||
|
|
||||||
|
public interface AiFileUploadRecordMapper extends BaseMapper<AiFileUploadRecord> {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.electromagnetic.industry.software.manage.pojo.models;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
@TableName(value = "ai_file_upload_record")
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class AiFileUploadRecord extends BaseModel {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
private String fileName;
|
||||||
|
|
||||||
|
private String md5;
|
||||||
|
}
|
||||||
|
|
@ -4,14 +4,19 @@ import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.crypto.digest.DigestUtil;
|
import cn.hutool.crypto.digest.DigestUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||||
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
||||||
|
import com.electromagnetic.industry.software.common.util.IdWorker;
|
||||||
|
import com.electromagnetic.industry.software.manage.mapper.AiFileUploadRecordMapper;
|
||||||
|
import com.electromagnetic.industry.software.manage.pojo.models.AiFileUploadRecord;
|
||||||
import com.electromagnetic.industry.software.manage.pojo.req.QueryDTO;
|
import com.electromagnetic.industry.software.manage.pojo.req.QueryDTO;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.ai.chat.client.ChatClient;
|
import org.springframework.ai.chat.client.ChatClient;
|
||||||
import org.springframework.ai.chat.client.advisor.*;
|
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
|
||||||
|
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
||||||
|
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
|
||||||
import org.springframework.ai.chat.messages.UserMessage;
|
import org.springframework.ai.chat.messages.UserMessage;
|
||||||
import org.springframework.ai.chat.model.ChatResponse;
|
import org.springframework.ai.chat.model.ChatResponse;
|
||||||
import org.springframework.ai.chat.prompt.Prompt;
|
import org.springframework.ai.chat.prompt.Prompt;
|
||||||
|
|
@ -20,14 +25,13 @@ import org.springframework.ai.ollama.OllamaChatModel;
|
||||||
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
|
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
|
||||||
import org.springframework.ai.vectorstore.VectorStore;
|
import org.springframework.ai.vectorstore.VectorStore;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.codec.ServerSentEvent;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
@ -51,6 +55,9 @@ public class ChatService {
|
||||||
@Value("file.md5RecordPath")
|
@Value("file.md5RecordPath")
|
||||||
private String uploadFileMd5RecordPath;
|
private String uploadFileMd5RecordPath;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private AiFileUploadRecordMapper aiFileUploadRecordMapper;
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public void init() {
|
public void init() {
|
||||||
if (!FileUtil.exist(uploadFileMd5RecordPath)) {
|
if (!FileUtil.exist(uploadFileMd5RecordPath)) {
|
||||||
|
|
@ -63,24 +70,30 @@ public class ChatService {
|
||||||
vectorStore.write(documents);
|
vectorStore.write(documents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public ElectromagneticResult<?> addFromUpload(MultipartFile file) throws Exception {
|
public ElectromagneticResult<?> addFromUpload(MultipartFile file) throws Exception {
|
||||||
String fileType = FileUtil.extName(file.getOriginalFilename());
|
String fileType = FileUtil.extName(file.getOriginalFilename());
|
||||||
if (!StrUtil.equals(fileType, "pdf")) {
|
if (!StrUtil.equals(fileType, "pdf")) {
|
||||||
return ElectromagneticResultUtil.fail("-1", "当前仅支持pdf格式文件");
|
return ElectromagneticResultUtil.fail("-1", "当前仅支持pdf格式文件");
|
||||||
}
|
}
|
||||||
String fileMd5 = DigestUtil.md5Hex(file.getInputStream());
|
String fileMd5 = DigestUtil.md5Hex(file.getInputStream());
|
||||||
List<String> lines = FileUtil.readLines(uploadFileMd5RecordPath, Charset.defaultCharset());
|
|
||||||
if (lines.contains(fileMd5)) {
|
Long count = aiFileUploadRecordMapper.selectCount(Wrappers.lambdaQuery(AiFileUploadRecord.class)
|
||||||
|
.eq(AiFileUploadRecord::getMd5, fileMd5));
|
||||||
|
if (count > 0) {
|
||||||
return ElectromagneticResultUtil.success(fileMd5);
|
return ElectromagneticResultUtil.success(fileMd5);
|
||||||
}
|
}
|
||||||
|
|
||||||
String pdfTmpPath = FileUtil.getParent(uploadFileMd5RecordPath, 1) + File.separator + IdUtil.fastSimpleUUID() + "." + fileType;
|
String pdfTmpPath = FileUtil.getParent(uploadFileMd5RecordPath, 1) + File.separator + IdUtil.fastSimpleUUID() + "." + fileType;
|
||||||
FileUtil.writeFromStream(file.getInputStream(), pdfTmpPath);
|
FileUtil.writeFromStream(file.getInputStream(), pdfTmpPath);
|
||||||
|
|
||||||
PagePdfDocumentReader pagePdfDocumentReader = new PagePdfDocumentReader(pdfTmpPath);
|
List<Document> documents = new PagePdfDocumentReader(pdfTmpPath).read();
|
||||||
List<Document> documents = pagePdfDocumentReader.read();
|
|
||||||
vectorStore.write(documents);
|
vectorStore.write(documents);
|
||||||
lines.add(fileMd5);
|
|
||||||
FileUtil.writeLines(lines, uploadFileMd5RecordPath, Charset.defaultCharset());
|
aiFileUploadRecordMapper.insert(new AiFileUploadRecord().setId(IdWorker.getSnowFlakeIdString())
|
||||||
|
.setMd5(fileMd5)
|
||||||
|
.setFileName(file.getOriginalFilename()));
|
||||||
|
|
||||||
FileUtil.del(pdfTmpPath);
|
FileUtil.del(pdfTmpPath);
|
||||||
return ElectromagneticResultUtil.success(fileMd5);
|
return ElectromagneticResultUtil.success(fileMd5);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue