|
@@ -48,7 +48,9 @@ public class ChatService {
|
|
|
|
|
|
private static final MD5 MD5 = cn.hutool.crypto.digest.MD5.create();
|
|
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 String PATH = System.getProperty("user.dir") + "/ai_doc/";
|
|
- private static final List<Message> LIST_MESSAGE = new ArrayList<>();
|
|
|
|
|
|
+ private static final int MAX_LIST_MESSAGE_LENGTH = 3;
|
|
|
|
+ private static final int MAX_MESSAGE_LENGTH = 512;
|
|
|
|
+ private static List<Message> listMessage = new ArrayList<>();
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
private VectorStore vectorStore;
|
|
private VectorStore vectorStore;
|
|
@@ -125,8 +127,10 @@ public class ChatService {
|
|
String fileName = supportFile.get(1);
|
|
String fileName = supportFile.get(1);
|
|
String fileTypeCn = supportFile.get(2);
|
|
String fileTypeCn = supportFile.get(2);
|
|
DocumentReader reader = supportFile.get(3);
|
|
DocumentReader reader = supportFile.get(3);
|
|
- String context = fileTypeCn + " : " + fileName + " ====> " + mergeDocuments(reader.get()).replace("\n", " ");
|
|
|
|
- return stream(message, new UserMessage(context + " : " + message));
|
|
|
|
|
|
+ String context = fileTypeCn + " :: " + fileName + " ====> " + mergeDocuments(reader.get()) + "\n" + "↑ " + message;
|
|
|
|
+ UserMessage userMessage = new UserMessage(context);
|
|
|
|
+ Log.info(this.getClass(), context);
|
|
|
|
+ return stream(message, userMessage);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -137,8 +141,8 @@ public class ChatService {
|
|
}
|
|
}
|
|
|
|
|
|
private String mergeDocuments(List<Document> documents) {
|
|
private String mergeDocuments(List<Document> documents) {
|
|
- return MergeDocuments.mergeDocuments(documents)
|
|
|
|
- .stream().map(Document::getContent).collect(Collectors.joining("\n"));
|
|
|
|
|
|
+ return MergeDocuments.mergeDocuments(documents).stream()
|
|
|
|
+ .map(Document::getContent).collect(Collectors.joining("\n"));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -149,15 +153,28 @@ public class ChatService {
|
|
* @return 提示词
|
|
* @return 提示词
|
|
*/
|
|
*/
|
|
private Prompt getChatPrompt2String(String message, Message context) {
|
|
private Prompt getChatPrompt2String(String message, Message context) {
|
|
- ChatService.LIST_MESSAGE.add(new SystemMessage(userConfig.getSysMessage()));
|
|
|
|
|
|
+ if (listMessage.size() > MAX_LIST_MESSAGE_LENGTH) {
|
|
|
|
+ listMessage = listMessage.subList(listMessage.size() - MAX_LIST_MESSAGE_LENGTH, listMessage.size())
|
|
|
|
+ .stream()
|
|
|
|
+ .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));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return msg;
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
+ }
|
|
|
|
+ listMessage.add(new SystemMessage(userConfig.getSysMessage()));
|
|
if (context.getMessageType().equals(MessageType.SYSTEM)) {
|
|
if (context.getMessageType().equals(MessageType.SYSTEM)) {
|
|
- ChatService.LIST_MESSAGE.add(new UserMessage(message));
|
|
|
|
- ChatService.LIST_MESSAGE.add(context);
|
|
|
|
|
|
+ listMessage.add(new UserMessage(message));
|
|
|
|
+ listMessage.add(context);
|
|
}
|
|
}
|
|
if (context.getMessageType().equals(MessageType.USER)) {
|
|
if (context.getMessageType().equals(MessageType.USER)) {
|
|
- ChatService.LIST_MESSAGE.add(context);
|
|
|
|
|
|
+ return new Prompt(context);
|
|
}
|
|
}
|
|
- return new Prompt(ChatService.LIST_MESSAGE);
|
|
|
|
|
|
+ return new Prompt(listMessage);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -175,12 +192,13 @@ public class ChatService {
|
|
response -> {
|
|
response -> {
|
|
String str = response.getResult().getOutput().getContent();
|
|
String str = response.getResult().getOutput().getContent();
|
|
chatMessage.append(str);
|
|
chatMessage.append(str);
|
|
- return str.replace(" ", " ");
|
|
|
|
|
|
+ return str.replace(" ", " ")
|
|
|
|
+ .replace("\t", " ");
|
|
}).concatWithValues("<{完成}>")
|
|
}).concatWithValues("<{完成}>")
|
|
.onErrorComplete()
|
|
.onErrorComplete()
|
|
.doOnComplete(() -> {
|
|
.doOnComplete(() -> {
|
|
String chatMsg = chatMessage.toString();
|
|
String chatMsg = chatMessage.toString();
|
|
- ChatService.LIST_MESSAGE.add(new AssistantMessage(chatMsg));
|
|
|
|
|
|
+ listMessage.add(new AssistantMessage(chatMsg));
|
|
try {
|
|
try {
|
|
chatHistoryService.insert(userId, message, chatMsg);
|
|
chatHistoryService.insert(userId, message, chatMsg);
|
|
} catch (InsertFailException e) {
|
|
} catch (InsertFailException e) {
|