|
@@ -1,5 +1,7 @@
|
|
|
package ai.huisuan.back.service.impl;
|
|
|
|
|
|
+import ai.huisuan.back.entity.DialogueSessionInfo;
|
|
|
+import ai.huisuan.back.entity.HistoryRecordInfo;
|
|
|
import ai.huisuan.back.model.*;
|
|
|
import ai.huisuan.back.repository.DialogueSessionsRepository;
|
|
|
import ai.huisuan.back.repository.HistoryRecordsRepository;
|
|
@@ -20,9 +22,6 @@ import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.time.Duration;
|
|
|
import java.time.Instant;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.UUID;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
@@ -45,7 +44,6 @@ public class ChatServiceImpl implements ChatService {
|
|
|
private static final Duration READ_TIMEOUT = Duration.ofSeconds(600);
|
|
|
private static final Duration WRITE_TIMEOUT = Duration.ofSeconds(300);
|
|
|
private static final Duration SSE_TIMEOUT = Duration.ofMinutes(10);
|
|
|
- private final DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
|
|
|
|
|
|
public ChatServiceImpl(ObjectMapper objectMapper,
|
|
|
DialogueSessionsRepository dialogueSessionsRepository,
|
|
@@ -62,6 +60,16 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
|
@Override
|
|
|
public Request buildOllamaRequest(ChatRequest requestDTO,boolean stream) throws JsonProcessingException {
|
|
|
+ // 保存数据
|
|
|
+ HistoryRecordInfo historyRecordInfo = new HistoryRecordInfo();
|
|
|
+ historyRecordInfo.setContent(requestDTO.getQuestion().getContent());
|
|
|
+ historyRecordInfo.setRole(requestDTO.getQuestion().getRole());
|
|
|
+ historyRecordInfo.setTimestamp(Instant.now());
|
|
|
+ DialogueSessionInfo dialogueSessionInfo = dialogueSessionsRepository.findById(requestDTO.getSessionId()).orElse(null);
|
|
|
+ historyRecordInfo.setSession(dialogueSessionInfo);
|
|
|
+ historyRecordsRepository.save(historyRecordInfo);
|
|
|
+
|
|
|
+ // 构建请求体
|
|
|
List<OllamaMessage> ollamaMessages = new ArrayList<>();
|
|
|
|
|
|
List<Message> messages = requestDTO.getHistory().getMessages();
|
|
@@ -98,12 +106,22 @@ public class ChatServiceImpl implements ChatService {
|
|
|
History history =chatRequest.getHistory();
|
|
|
history.getMessages().add(chatRequest.getQuestion());
|
|
|
history.getMessages().add(new Message());
|
|
|
-
|
|
|
ChatResponse responseDTO = new ChatResponse();
|
|
|
responseDTO.setAnswer(message);
|
|
|
responseDTO.setModel(message.getRole());
|
|
|
responseDTO.setCreatedAt(message.getTimestamp());
|
|
|
responseDTO.setHistory(history);
|
|
|
+
|
|
|
+ // 保存数据到数据库
|
|
|
+ HistoryRecordInfo historyRecordInfo = new HistoryRecordInfo();
|
|
|
+ historyRecordInfo.setContent(responseBody);
|
|
|
+ historyRecordInfo.setRole(responseDTO.getAnswer().getRole());
|
|
|
+ historyRecordInfo.setTimestamp(Instant.now());
|
|
|
+ DialogueSessionInfo dialogueSessionInfo = dialogueSessionsRepository.findById(chatRequest.getSessionId()).orElse(null);
|
|
|
+ historyRecordInfo.setSession(dialogueSessionInfo);
|
|
|
+ historyRecordsRepository.save(historyRecordInfo);
|
|
|
+
|
|
|
+ // 返回结果
|
|
|
return responseDTO;
|
|
|
}
|
|
|
|
|
@@ -130,6 +148,7 @@ public class ChatServiceImpl implements ChatService {
|
|
|
return emitter;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
private void executeStreamingRequest(SseEmitter emitter, ChatRequest requestDTO) throws JsonProcessingException {
|
|
|
Request request = buildOllamaRequest(requestDTO,true);
|
|
|
client.newCall(request).enqueue(new Callback() {
|
|
@@ -146,7 +165,7 @@ public class ChatServiceImpl implements ChatService {
|
|
|
OllamaChatResponse chunk = objectMapper.readValue(line, OllamaChatResponse.class);
|
|
|
System.out.println(chunk);
|
|
|
fullResponse.append(chunk.getMessage().getContent());
|
|
|
- processChunk( requestDTO, emitter, chunk);
|
|
|
+ processChunk(emitter, chunk);
|
|
|
if (chunk.isDone()) {
|
|
|
break;
|
|
|
}
|
|
@@ -164,8 +183,7 @@ public class ChatServiceImpl implements ChatService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void processChunk( ChatRequest requestDTO,
|
|
|
- SseEmitter emitter, OllamaChatResponse chunk) throws IOException {
|
|
|
+ private void processChunk(SseEmitter emitter, OllamaChatResponse chunk) throws IOException {
|
|
|
ChatStreamResponse chatStreamResponse = new ChatStreamResponse();
|
|
|
chatStreamResponse.setContent(chunk.getMessage().getContent());
|
|
|
chatStreamResponse.setDone(chunk.isDone());
|