|
@@ -1,13 +1,14 @@
|
|
-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;
|
|
|
|
-import ai.huisuan.back.service.ChatService;
|
|
|
|
|
|
+package ai.java.back.service.impl;
|
|
|
|
+
|
|
|
|
+import ai.java.back.entity.DialogueSessionInfo;
|
|
|
|
+import ai.java.back.entity.HistoryRecordInfo;
|
|
|
|
+import ai.java.back.model.*;
|
|
|
|
+import ai.java.back.repository.DialogueSessionsRepository;
|
|
|
|
+import ai.java.back.repository.HistoryRecordsRepository;
|
|
|
|
+import ai.java.back.service.ChatService;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
|
import okhttp3.*;
|
|
import okhttp3.*;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.NotNull;
|
|
@@ -72,7 +73,7 @@ public class ChatServiceImpl implements ChatService {
|
|
// 构建请求体
|
|
// 构建请求体
|
|
List<OllamaMessage> ollamaMessages = new ArrayList<>();
|
|
List<OllamaMessage> ollamaMessages = new ArrayList<>();
|
|
|
|
|
|
- List<Message> messages = requestDTO.getHistory().getMessages();
|
|
|
|
|
|
+ List<Message> messages = requestDTO.getHistory() != null ? requestDTO.getHistory().getMessages() : new ArrayList<>();
|
|
for (Message message : messages) {
|
|
for (Message message : messages) {
|
|
OllamaMessage ollamaMessage = new OllamaMessage(message.getRole(), message.getContent());
|
|
OllamaMessage ollamaMessage = new OllamaMessage(message.getRole(), message.getContent());
|
|
ollamaMessages.add(ollamaMessage);
|
|
ollamaMessages.add(ollamaMessage);
|
|
@@ -99,40 +100,65 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public ChatResponse parseResponse(String responseBody,ChatRequest chatRequest) {
|
|
|
|
-
|
|
|
|
- // id之后设置
|
|
|
|
- Message message= new Message(1,"model",responseBody, Instant.now());
|
|
|
|
- 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;
|
|
|
|
- }
|
|
|
|
|
|
+ public ChatResponse parseResponse(String responseBody, ChatRequest chatRequest) {
|
|
|
|
+ // 直接将响应内容作为普通字符串处理
|
|
|
|
+ Message message = new Message( "model", responseBody, Instant.now());
|
|
|
|
|
|
|
|
+ // 确保 history 不为 null
|
|
|
|
+ if (chatRequest.getHistory() == null) {
|
|
|
|
+ chatRequest.setHistory(new History());
|
|
|
|
+ }
|
|
|
|
+ History history = chatRequest.getHistory();
|
|
|
|
+
|
|
|
|
+ // 构建 ChatResponse
|
|
|
|
+ ChatResponse responseDTO = new ChatResponse();
|
|
|
|
+ responseDTO.setAnswer(message);
|
|
|
|
+ responseDTO.setModel(message.getRole());
|
|
|
|
+ responseDTO.setSessionId(chatRequest.getSessionId());
|
|
|
|
+ responseDTO.setCreatedAt(message.getTimestamp());
|
|
|
|
+ history.getMessages().add(chatRequest.getQuestion());
|
|
|
|
+ history.setSessionId(chatRequest.getSessionId());
|
|
|
|
+ responseDTO.setHistory(history);
|
|
|
|
+
|
|
|
|
+ return responseDTO;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public ChatResponse completions(ChatRequest requestDTO) throws IOException {
|
|
public ChatResponse completions(ChatRequest requestDTO) throws IOException {
|
|
- Request request = buildOllamaRequest(requestDTO,false);
|
|
|
|
- Response response = client.newCall(request).execute();
|
|
|
|
- System.out.println(response);
|
|
|
|
- return parseResponse(response.body().string(),requestDTO);
|
|
|
|
|
|
+ Request request = buildOllamaRequest(requestDTO, false);
|
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
|
+ System.out.println(response);
|
|
|
|
+
|
|
|
|
+ // 解析响应内容并提取嵌套的 content 字段
|
|
|
|
+ String responseBody = response.body().string();
|
|
|
|
+ String extractedContent;
|
|
|
|
+ try {
|
|
|
|
+ JsonNode rootNode = objectMapper.readTree(responseBody);
|
|
|
|
+ extractedContent = rootNode.path("message").path("content").asText();
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
+ throw new RuntimeException("Failed to parse response body", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Message message = new Message("model", extractedContent, Instant.now());
|
|
|
|
+
|
|
|
|
+ // 确保 history 不为 null
|
|
|
|
+ if (requestDTO.getHistory() == null) {
|
|
|
|
+ requestDTO.setHistory(new History());
|
|
|
|
+ }
|
|
|
|
+ History history = requestDTO.getHistory();
|
|
|
|
+
|
|
|
|
+ // 构建 ChatResponse
|
|
|
|
+ ChatResponse responseDTO = new ChatResponse();
|
|
|
|
+ responseDTO.setAnswer(message);
|
|
|
|
+ responseDTO.setModel(message.getRole());
|
|
|
|
+ responseDTO.setSessionId(requestDTO.getSessionId());
|
|
|
|
+ responseDTO.setCreatedAt(message.getTimestamp());
|
|
|
|
+ history.getMessages().add(requestDTO.getQuestion());
|
|
|
|
+ history.setSessionId(requestDTO.getSessionId());
|
|
|
|
+ responseDTO.setHistory(history);
|
|
|
|
+
|
|
|
|
+ return responseDTO;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -159,18 +185,32 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(body.byteStream()));
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(body.byteStream()));
|
|
|
|
|
|
- StringBuilder fullResponse = new StringBuilder();
|
|
|
|
|
|
+ // 在流式输出中累积内容
|
|
|
|
+ StringBuilder accumulatedContent = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ // 在 while 循环中累积 content
|
|
String line;
|
|
String line;
|
|
- while ((line=reader.readLine()) != null) {
|
|
|
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
OllamaChatResponse chunk = objectMapper.readValue(line, OllamaChatResponse.class);
|
|
OllamaChatResponse chunk = objectMapper.readValue(line, OllamaChatResponse.class);
|
|
System.out.println(chunk);
|
|
System.out.println(chunk);
|
|
- fullResponse.append(chunk.getMessage().getContent());
|
|
|
|
|
|
+ accumulatedContent.append(chunk.getMessage().getContent());
|
|
processChunk(emitter, chunk);
|
|
processChunk(emitter, chunk);
|
|
if (chunk.isDone()) {
|
|
if (chunk.isDone()) {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- ChatResponse chatResponse = parseResponse(fullResponse.toString(),requestDTO);
|
|
|
|
|
|
+
|
|
|
|
+ // 在流结束后保存累积的内容到数据库
|
|
|
|
+ String finalContent = accumulatedContent.toString();
|
|
|
|
+ HistoryRecordInfo historyRecordInfo = new HistoryRecordInfo();
|
|
|
|
+ historyRecordInfo.setContent(finalContent);
|
|
|
|
+ historyRecordInfo.setRole("model");
|
|
|
|
+ historyRecordInfo.setTimestamp(Instant.now());
|
|
|
|
+ DialogueSessionInfo dialogueSessionInfo = dialogueSessionsRepository.findById(requestDTO.getSessionId()).orElse(null);
|
|
|
|
+ historyRecordInfo.setSession(dialogueSessionInfo);
|
|
|
|
+ historyRecordsRepository.save(historyRecordInfo);
|
|
|
|
+
|
|
|
|
+ ChatResponse chatResponse = parseResponse(finalContent,requestDTO);
|
|
emitter.send(chatResponse);
|
|
emitter.send(chatResponse);
|
|
emitter.complete();
|
|
emitter.complete();
|
|
|
|
|