|
@@ -15,7 +15,6 @@ import cn.jlsxwkj.common.utils.Log;
|
|
|
import cn.jlsxwkj.common.utils.MergeDocuments;
|
|
|
import cn.jlsxwkj.moudles.chathistory.ChatHistoryService;
|
|
|
import jakarta.annotation.Resource;
|
|
|
-import lombok.Data;
|
|
|
import org.reactivestreams.Subscription;
|
|
|
import org.springframework.ai.chat.messages.*;
|
|
|
import org.springframework.ai.chat.model.ChatResponse;
|
|
@@ -43,14 +42,11 @@ import java.util.stream.Collectors;
|
|
|
* @author zh
|
|
|
*/
|
|
|
@Service
|
|
|
-@Data
|
|
|
public class ChatService {
|
|
|
|
|
|
- private static final MD5 MD5 = cn.hutool.crypto.digest.MD5.create();
|
|
|
- private static final String PATH = System.getProperty("user.dir") + "/ai_doc/";
|
|
|
- private static final int MAX_LIST_MESSAGE_LENGTH = 3;
|
|
|
- private static final int MAX_MESSAGE_LENGTH = 512;
|
|
|
- private static List<Message> listMessage = new ArrayList<>();
|
|
|
+ private final MD5 md5 = cn.hutool.crypto.digest.MD5.create();
|
|
|
+ private final String PATH = System.getProperty("user.dir") + "/ai_doc/";
|
|
|
+ private List<Message> listMessage;
|
|
|
|
|
|
@Resource
|
|
|
private VectorStore vectorStore;
|
|
@@ -153,23 +149,31 @@ public class ChatService {
|
|
|
* @return 提示词
|
|
|
*/
|
|
|
private Prompt getChatPrompt2String(String message, Message context) {
|
|
|
- if (listMessage.size() > MAX_LIST_MESSAGE_LENGTH) {
|
|
|
- listMessage = listMessage.subList(listMessage.size() - MAX_LIST_MESSAGE_LENGTH, listMessage.size())
|
|
|
+ if (listMessage == null) {
|
|
|
+ listMessage = new ArrayList<>();
|
|
|
+ listMessage.add(new SystemMessage(userConfig.getSysMessage()));
|
|
|
+ }
|
|
|
+ if (listMessage.size() > userConfig.getMaxListMessageLength()) {
|
|
|
+ listMessage = listMessage.subList(listMessage.size() - userConfig.getMaxListMessageLength(), listMessage.size())
|
|
|
.stream()
|
|
|
+ .filter(msg -> !msg.getContent().equals(userConfig.getSysMessage()))
|
|
|
.map(msg -> {
|
|
|
if (msg.getMessageType().equals(MessageType.ASSISTANT)) {
|
|
|
String content = msg.getContent();
|
|
|
- if (content.length() > MAX_MESSAGE_LENGTH) {
|
|
|
- return new AssistantMessage(content.substring(0, MAX_MESSAGE_LENGTH));
|
|
|
+ if (content.length() > userConfig.getMaxMessageLength()) {
|
|
|
+ return new AssistantMessage(content.substring(0, userConfig.getMaxMessageLength()));
|
|
|
}
|
|
|
}
|
|
|
return msg;
|
|
|
- }).collect(Collectors.toList());
|
|
|
+ }).distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ listMessage.add(new SystemMessage(userConfig.getSysMessage()));
|
|
|
}
|
|
|
- listMessage.add(new SystemMessage(userConfig.getSysMessage()));
|
|
|
if (context.getMessageType().equals(MessageType.SYSTEM)) {
|
|
|
listMessage.add(new UserMessage(message));
|
|
|
- listMessage.add(context);
|
|
|
+ if (!context.getContent().isBlank()) {
|
|
|
+ listMessage.add(context);
|
|
|
+ }
|
|
|
}
|
|
|
if (context.getMessageType().equals(MessageType.USER)) {
|
|
|
return new Prompt(context);
|
|
@@ -225,7 +229,7 @@ public class ChatService {
|
|
|
String[] split = Objects.requireNonNull(file.getOriginalFilename()).split("\\.");
|
|
|
String fileType = split[split.length - 1].toLowerCase(Locale.ROOT);
|
|
|
File savePath = new File(PATH);
|
|
|
- File saveFile = new File(PATH + ChatService.MD5.digestHex(bytes) + "." + fileType);
|
|
|
+ File saveFile = new File(PATH + md5.digestHex(bytes) + "." + fileType);
|
|
|
String fileName = saveFile.getName();
|
|
|
String fileTypeCn;
|
|
|
if (!savePath.exists()) {
|