|
@@ -5,12 +5,10 @@ import cn.jlsxwkj.common.reader.ParagraphDocReader;
|
|
|
import cn.jlsxwkj.common.reader.ParagraphTextReader;
|
|
|
import cn.jlsxwkj.common.utils.Log;
|
|
|
import cn.jlsxwkj.common.utils.MergeDocuments;
|
|
|
+import cn.jlsxwkj.moudles.chathistory.ChatHistory;
|
|
|
+import cn.jlsxwkj.moudles.chathistory.ChatHistoryService;
|
|
|
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;
|
|
|
-import org.springframework.ai.chat.model.ChatResponse;
|
|
|
+import org.springframework.ai.chat.messages.*;
|
|
|
import org.springframework.ai.chat.prompt.Prompt;
|
|
|
import org.springframework.ai.document.Document;
|
|
|
import org.springframework.ai.document.DocumentReader;
|
|
@@ -47,6 +45,11 @@ public class ChatService {
|
|
|
@Resource
|
|
|
private OllamaChatModel ollamaChatModel;
|
|
|
/**
|
|
|
+ * 历史记录
|
|
|
+ */
|
|
|
+ @Resource
|
|
|
+ private ChatHistoryService chatHistoryService;
|
|
|
+ /**
|
|
|
* md5 校验
|
|
|
*/
|
|
|
private static final MD5 MD5 = cn.hutool.crypto.digest.MD5.create();
|
|
@@ -58,10 +61,33 @@ public class ChatService {
|
|
|
* 用户对话上下文
|
|
|
*/
|
|
|
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
|
|
|
* @param file 保存的文件
|
|
|
* @return 保存状态
|
|
@@ -114,14 +140,9 @@ public class ChatService {
|
|
|
* @return 文本内容
|
|
|
*/
|
|
|
public String search(String keyword) {
|
|
|
- //相似度不少于 50%
|
|
|
- SearchRequest searchRequest = SearchRequest.query(keyword).withSimilarityThreshold(0.5);
|
|
|
- //提取文本内容
|
|
|
- List<Document> documents = vectorStore.similaritySearch(searchRequest);
|
|
|
- //合并文档
|
|
|
- List<Document> mergeDocuments = MergeDocuments.mergeDocuments(documents);
|
|
|
- //获取文档中的文本并以换行分割
|
|
|
- return mergeDocuments.stream().map(Document::getContent).collect(Collectors.joining("\n"));
|
|
|
+ return MergeDocuments.mergeDocuments(
|
|
|
+ vectorStore.similaritySearch(SearchRequest.query(keyword).withSimilarityThreshold(0.5))
|
|
|
+ ).stream().map(Document::getContent).collect(Collectors.joining("\n"));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -136,14 +157,13 @@ public class ChatService {
|
|
|
String content = search(message);
|
|
|
//封装prompt并调用大模型
|
|
|
Prompt chatPrompt2String = getChatPrompt2String(message, content);
|
|
|
- return ollamaChatModel.stream(chatPrompt2String)
|
|
|
- .map(response -> {
|
|
|
+ return ollamaChatModel.stream(chatPrompt2String).map(
|
|
|
+ response -> {
|
|
|
String str = response.getResult().getOutput().getContent();
|
|
|
chatMessage.append(str);
|
|
|
return str.replace(" ", " ");
|
|
|
})
|
|
|
- .concatWithValues("<{完成}>")
|
|
|
- .onErrorStop()
|
|
|
+ .concatWithValues("<{完成}>").onErrorStop()
|
|
|
.doOnComplete(() -> {
|
|
|
LIST_MESSAGE.add(new AssistantMessage(chatMessage.toString()));
|
|
|
LIST_MESSAGE.forEach(x -> Log.info(this.getClass(),
|
|
@@ -164,10 +184,9 @@ public class ChatService {
|
|
|
chatMessage = new StringBuffer();
|
|
|
//查询获取文档信息
|
|
|
String content = search(message);
|
|
|
- //封装prompt并调用大模型
|
|
|
- ChatResponse call = ollamaChatModel.call(getChatPrompt2String(message, content));
|
|
|
//获取消息
|
|
|
- String result = call.getResult().getOutput().getContent();
|
|
|
+ String result = ollamaChatModel.call(getChatPrompt2String(message, content))
|
|
|
+ .getResult().getOutput().getContent();
|
|
|
chatMessage.append(result);
|
|
|
LIST_MESSAGE.add(new AssistantMessage(chatMessage.toString()));
|
|
|
LIST_MESSAGE.forEach(x -> Log.info(this.getClass(),
|