|
@@ -1,14 +1,17 @@
|
|
package cn.jlsxwkj.moudles.chat;
|
|
package cn.jlsxwkj.moudles.chat;
|
|
|
|
|
|
|
|
+import cn.dev33.satoken.SaManager;
|
|
import cn.hutool.crypto.digest.MD5;
|
|
import cn.hutool.crypto.digest.MD5;
|
|
import cn.jlsxwkj.common.reader.ParagraphDocReader;
|
|
import cn.jlsxwkj.common.reader.ParagraphDocReader;
|
|
import cn.jlsxwkj.common.reader.ParagraphTextReader;
|
|
import cn.jlsxwkj.common.reader.ParagraphTextReader;
|
|
import cn.jlsxwkj.common.utils.Log;
|
|
import cn.jlsxwkj.common.utils.Log;
|
|
import cn.jlsxwkj.common.utils.MergeDocuments;
|
|
import cn.jlsxwkj.common.utils.MergeDocuments;
|
|
-import cn.jlsxwkj.moudles.chathistory.ChatHistory;
|
|
|
|
import cn.jlsxwkj.moudles.chathistory.ChatHistoryService;
|
|
import cn.jlsxwkj.moudles.chathistory.ChatHistoryService;
|
|
import jakarta.annotation.Resource;
|
|
import jakarta.annotation.Resource;
|
|
-import org.springframework.ai.chat.messages.*;
|
|
|
|
|
|
+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;
|
|
import org.springframework.ai.chat.prompt.Prompt;
|
|
import org.springframework.ai.chat.prompt.Prompt;
|
|
import org.springframework.ai.document.Document;
|
|
import org.springframework.ai.document.Document;
|
|
import org.springframework.ai.document.DocumentReader;
|
|
import org.springframework.ai.document.DocumentReader;
|
|
@@ -50,6 +53,10 @@ public class ChatService {
|
|
@Resource
|
|
@Resource
|
|
private ChatHistoryService chatHistoryService;
|
|
private ChatHistoryService chatHistoryService;
|
|
/**
|
|
/**
|
|
|
|
+ * 单条消息
|
|
|
|
+ */
|
|
|
|
+ private static StringBuffer chatMessage;
|
|
|
|
+ /**
|
|
* md5 校验
|
|
* md5 校验
|
|
*/
|
|
*/
|
|
private static final MD5 MD5 = cn.hutool.crypto.digest.MD5.create();
|
|
private static final MD5 MD5 = cn.hutool.crypto.digest.MD5.create();
|
|
@@ -61,31 +68,6 @@ public class ChatService {
|
|
* 用户对话上下文
|
|
* 用户对话上下文
|
|
*/
|
|
*/
|
|
private static final List<Message> LIST_MESSAGE = new ArrayList<>();
|
|
private static final List<Message> LIST_MESSAGE = new ArrayList<>();
|
|
- /**
|
|
|
|
- * 单条消息
|
|
|
|
- */
|
|
|
|
- private static StringBuffer chatMessage;
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 保存会话
|
|
|
|
- * @return 保存条数
|
|
|
|
- */
|
|
|
|
- @SuppressWarnings("unused")
|
|
|
|
- public String saveDialog() {
|
|
|
|
- List<ChatHistory> chatHistoryList = new ArrayList<>();
|
|
|
|
- List<Message> userMessageList = LIST_MESSAGE.stream().filter(x -> x.getMessageType().equals(MessageType.USER)).toList();
|
|
|
|
- List<Message> chatMessageList = LIST_MESSAGE.stream().filter(x -> x.getMessageType().equals(MessageType.ASSISTANT)).toList();
|
|
|
|
- for (int i = 0; i < userMessageList.size(); i++) {
|
|
|
|
- String userMessage = userMessageList.get(i).getContent();
|
|
|
|
- String chatMessage = chatMessageList.get(i).getContent();
|
|
|
|
- ChatHistory chatHistory = new ChatHistory();
|
|
|
|
- chatHistory.setUserId("test");
|
|
|
|
- chatHistory.setUserQ(userMessage);
|
|
|
|
- chatHistory.setChatA(chatMessage);
|
|
|
|
- chatHistoryList.add(chatHistory);
|
|
|
|
- }
|
|
|
|
- return chatHistoryService.insertAll(chatHistoryList);
|
|
|
|
- }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 使用spring ai解析txt文档并保存至 pg
|
|
* 使用spring ai解析txt文档并保存至 pg
|
|
@@ -160,16 +142,15 @@ public class ChatService {
|
|
return ollamaChatModel.stream(chatPrompt2String).map(
|
|
return ollamaChatModel.stream(chatPrompt2String).map(
|
|
response -> {
|
|
response -> {
|
|
String str = response.getResult().getOutput().getContent();
|
|
String str = response.getResult().getOutput().getContent();
|
|
|
|
+ System.out.print(str);
|
|
chatMessage.append(str);
|
|
chatMessage.append(str);
|
|
return str.replace(" ", " ");
|
|
return str.replace(" ", " ");
|
|
})
|
|
})
|
|
.concatWithValues("<{完成}>").onErrorStop()
|
|
.concatWithValues("<{完成}>").onErrorStop()
|
|
.doOnComplete(() -> {
|
|
.doOnComplete(() -> {
|
|
|
|
+ System.out.println();
|
|
LIST_MESSAGE.add(new AssistantMessage(chatMessage.toString()));
|
|
LIST_MESSAGE.add(new AssistantMessage(chatMessage.toString()));
|
|
- LIST_MESSAGE.forEach(x -> Log.info(this.getClass(),
|
|
|
|
- "message {} : {}",
|
|
|
|
- x.getMessageType(),
|
|
|
|
- x.getContent()));
|
|
|
|
|
|
+ saveDialog(message, chatMessage.toString());
|
|
});
|
|
});
|
|
|
|
|
|
}
|
|
}
|
|
@@ -188,11 +169,9 @@ public class ChatService {
|
|
String result = ollamaChatModel.call(getChatPrompt2String(message, content))
|
|
String result = ollamaChatModel.call(getChatPrompt2String(message, content))
|
|
.getResult().getOutput().getContent();
|
|
.getResult().getOutput().getContent();
|
|
chatMessage.append(result);
|
|
chatMessage.append(result);
|
|
|
|
+ Log.info(this.getClass(),"{} message : {}", "chat", result);
|
|
LIST_MESSAGE.add(new AssistantMessage(chatMessage.toString()));
|
|
LIST_MESSAGE.add(new AssistantMessage(chatMessage.toString()));
|
|
- LIST_MESSAGE.forEach(x -> Log.info(this.getClass(),
|
|
|
|
- "message {} : {}",
|
|
|
|
- x.getMessageType(),
|
|
|
|
- x.getContent()));
|
|
|
|
|
|
+ saveDialog(message, chatMessage.toString());
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -204,9 +183,18 @@ public class ChatService {
|
|
* @return 提示词
|
|
* @return 提示词
|
|
*/
|
|
*/
|
|
private Prompt getChatPrompt2String(String message, String context) {
|
|
private Prompt getChatPrompt2String(String message, String context) {
|
|
|
|
+ Log.info(this.getClass(),"{} message : {}", "user", message);
|
|
Prompt prompt = new Prompt(LIST_MESSAGE);
|
|
Prompt prompt = new Prompt(LIST_MESSAGE);
|
|
LIST_MESSAGE.add(new SystemMessage(context));
|
|
LIST_MESSAGE.add(new SystemMessage(context));
|
|
LIST_MESSAGE.add(new UserMessage(message));
|
|
LIST_MESSAGE.add(new UserMessage(message));
|
|
return prompt;
|
|
return prompt;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 保存会话
|
|
|
|
+ */
|
|
|
|
+ private void saveDialog(String userQ, String chatA) {
|
|
|
|
+ String userId = SaManager.getStpLogic("").getLoginIdAsString();
|
|
|
|
+ chatHistoryService.insert(userId, userQ, chatA);
|
|
|
|
+ }
|
|
}
|
|
}
|