Browse Source

将 chat 对话添加到历史对话记录

zhenghao 5 months ago
parent
commit
fb0c1d4c15
1 changed files with 27 additions and 7 deletions
  1. 27 7
      src/main/java/cn/jlsxwkj/moudles/chat/ChatService.java

+ 27 - 7
src/main/java/cn/jlsxwkj/moudles/chat/ChatService.java

@@ -6,6 +6,7 @@ import cn.jlsxwkj.common.reader.ParagraphTextReader;
 import cn.jlsxwkj.common.utils.Log;
 import cn.jlsxwkj.common.utils.MergeDocuments;
 import jakarta.annotation.Resource;
+import org.springframework.ai.chat.messages.AssistantMessage;
 import org.springframework.ai.chat.messages.Message;
 import org.springframework.ai.chat.messages.SystemMessage;
 import org.springframework.ai.chat.messages.UserMessage;
@@ -58,6 +59,8 @@ public class ChatService {
 	 */
 	private static final List<Message> LIST_MESSAGE = new ArrayList<>();
 
+	private static StringBuffer chatMessage;
+
 	/**
 	 * 使用spring ai解析txt文档并保存至 pg
 	 * @param file 保存的文件
@@ -128,14 +131,27 @@ public class ChatService {
 	 * @return 			回答内容
 	 */
 	public Flux<String> chatStream(String message) {
+		chatMessage = new StringBuffer();
 		//查询获取文档信息
 		String content = search(message);
 		//封装prompt并调用大模型
 		Prompt chatPrompt2String = getChatPrompt2String(message, content);
 		return ollamaChatModel.stream(chatPrompt2String)
-				.map(response -> response.getResult().getOutput().getContent())
+				.map(response -> {
+					String str = response.getResult().getOutput().getContent();
+					chatMessage.append(str);
+					return str.replace(" ", "  ");
+				})
+				.concatWithValues("<{完成}>")
 				.onErrorStop()
-				.concatWithValues("<{完成}>");
+				.doOnComplete(() -> {
+					LIST_MESSAGE.add(new AssistantMessage(chatMessage.toString()));
+					LIST_MESSAGE.forEach(x -> Log.info(this.getClass(),
+							"message {} : {}",
+							x.getMessageType(),
+							x.getContent()));
+				});
+
 	}
 
 	/**
@@ -145,12 +161,20 @@ public class ChatService {
 	 * @return 			回答内容
 	 */
 	public String chat(String message) {
+		chatMessage = new StringBuffer();
 		//查询获取文档信息
 		String content = search(message);
 		//封装prompt并调用大模型
 		ChatResponse call = ollamaChatModel.call(getChatPrompt2String(message, content));
 		//获取消息
-		return call.getResult().getOutput().getContent();
+		String result = call.getResult().getOutput().getContent();
+		chatMessage.append(result);
+		LIST_MESSAGE.add(new AssistantMessage(chatMessage.toString()));
+		LIST_MESSAGE.forEach(x -> Log.info(this.getClass(),
+				"message {} : {}",
+				x.getMessageType(),
+				x.getContent()));
+		return result;
 	}
 
 	/**
@@ -164,10 +188,6 @@ public class ChatService {
 		Prompt prompt = new Prompt(LIST_MESSAGE);
 		LIST_MESSAGE.add(new SystemMessage(context));
 		LIST_MESSAGE.add(new UserMessage(message));
-		LIST_MESSAGE.forEach(x -> Log.info(this.getClass(),
-				"message {} : {}",
-				x.getMessageType(),
-				x.getContent()));
 		return prompt;
 	}
 }