|
@@ -0,0 +1,150 @@
|
|
|
|
+package com.caimei365.user.service.impl;
|
|
|
|
+
|
|
|
|
+import com.caimei365.user.mapper.ChatMapper;
|
|
|
|
+import com.caimei365.user.model.ResponseJson;
|
|
|
|
+import com.caimei365.user.model.dto.*;
|
|
|
|
+import com.caimei365.user.model.vo.ChatDialogueVo;
|
|
|
|
+import com.caimei365.user.service.ChatLogService;
|
|
|
|
+import com.caimei365.user.utils.QianFanUtils;
|
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+@Slf4j
|
|
|
|
+@Service
|
|
|
|
+public class ChatLogServiceImpl implements ChatLogService {
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private ChatMapper chatMapper;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResponseJson<ChatCompletionResponse> getAnswer(Integer userId, String question) {
|
|
|
|
+ log.info("dto------------->" + question);
|
|
|
|
+ ApiRequest chatRequest = new ApiRequest();
|
|
|
|
+ ApiRequest.Message message = new ApiRequest.Message();
|
|
|
|
+ question = question + ",内容不超过350字";
|
|
|
|
+ message.role("user").content(question);
|
|
|
|
+ ArrayList<ApiRequest.Message> messages = new ArrayList<>();
|
|
|
|
+ messages.add(message);
|
|
|
|
+ chatRequest.penaltyScore(1.0).top_p(0.6).enableTrace(true).enableCitation(false).messages(messages);
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
+ String jsonString = "";
|
|
|
|
+ String resString = "";
|
|
|
|
+ try {
|
|
|
|
+ jsonString = objectMapper.writeValueAsString(chatRequest);
|
|
|
|
+ log.info("request------------------------->" + jsonString);
|
|
|
|
+ ChatCompletionResponse chatCompletionResponse = QianFanUtils.firstChat(jsonString);
|
|
|
|
+ resString = objectMapper.writeValueAsString(chatCompletionResponse);
|
|
|
|
+ ChatDialogueVo chatDialogueVo = new ChatDialogueVo();
|
|
|
|
+ chatDialogueVo.chatText(jsonString).userId(userId);
|
|
|
|
+ chatMapper.insertChatDialogue(chatDialogueVo);
|
|
|
|
+ chatMapper.insertUserDetail(jsonString, chatDialogueVo.id());
|
|
|
|
+ chatMapper.insertAssistantDetail(resString, chatDialogueVo.id());
|
|
|
|
+ chatCompletionResponse.setChatId(chatDialogueVo.id());
|
|
|
|
+ return ResponseJson.success(chatCompletionResponse);
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResponseJson<List<ChatHistory>> getHistory(Integer userId) {
|
|
|
|
+ List<ChatHistory> chats = chatMapper.findHistory(userId);
|
|
|
|
+ try {
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
+ for (ChatHistory chat : chats) {
|
|
|
|
+ String firstQuestion = chat.getFirstQuestion();
|
|
|
|
+ ApiRequest apiRequest = objectMapper.readValue(firstQuestion, ApiRequest.class);
|
|
|
|
+ ApiRequest.Message message = apiRequest.messages().get(0);
|
|
|
|
+ chat.setFirstQuestion(message.content());
|
|
|
|
+ }
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ return ResponseJson.success(chats);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResponseJson<List<ChatHistoryMessage>> getHistoryDetail(Integer userId, Integer chatId) {
|
|
|
|
+ List<ChatHistoryMessage> messages = chatMapper.findHistoryMessage(userId, chatId);
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
+ try {
|
|
|
|
+// for (ChatHistoryMessage chm : messages) {
|
|
|
|
+// if (0 == chm.getIdentity()) {
|
|
|
|
+// ApiRequest question = objectMapper.readValue(chm.getMessage(), ApiRequest.class);
|
|
|
|
+// chm.setMessage(question.messages().get(0).content());
|
|
|
|
+// } else {
|
|
|
|
+// ChatCompletionResponse answer = objectMapper.readValue(chm.getMessage(), ChatCompletionResponse.class);
|
|
|
|
+// chm.setMessage(answer.getResult());
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < messages.size(); i++) {
|
|
|
|
+ if (0 == messages.get(i).getIdentity()) {
|
|
|
|
+ ApiRequest question = objectMapper.readValue(messages.get(i).getMessage(), ApiRequest.class);
|
|
|
|
+ messages.get(i).setMessage(question.messages().get(i).content());
|
|
|
|
+ } else {
|
|
|
|
+ ChatCompletionResponse answer = objectMapper.readValue(messages.get(i).getMessage(), ChatCompletionResponse.class);
|
|
|
|
+ messages.get(i).setMessage(answer.getResult());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ return ResponseJson.success(messages);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResponseJson<ChatCompletionResponse> getSecondAnswer(Integer chatId, Integer userId, String question) {
|
|
|
|
+ List<ChatHistoryMessage> historyMessage = chatMapper.findHistoryQuestion(chatId, userId);
|
|
|
|
+ if(historyMessage.size()<1){
|
|
|
|
+ return ResponseJson.error("无历史记录");
|
|
|
|
+ }
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
+ try {
|
|
|
|
+ ChatHistoryMessage questions = historyMessage.get(0);
|
|
|
|
+ ChatHistoryMessage answer = historyMessage.get(1);
|
|
|
|
+ /**
|
|
|
|
+ * 取最近回复的answer和question,填充assistant及content,再加入新一轮的question,发起提问
|
|
|
|
+ */
|
|
|
|
+ ApiRequest his = objectMapper.readValue(questions.getMessage(), ApiRequest.class);
|
|
|
|
+ List<ApiRequest.Message> messages = his.messages();
|
|
|
|
+ ChatCompletionResponse chatResp = objectMapper.readValue(answer.getMessage(), ChatCompletionResponse.class);
|
|
|
|
+ ApiRequest.Message resMes = new ApiRequest.Message();
|
|
|
|
+ resMes.role("assistant").content(chatResp.getResult());
|
|
|
|
+ messages.add(resMes);
|
|
|
|
+ /**
|
|
|
|
+ * 正常轮次提问
|
|
|
|
+ */
|
|
|
|
+ ApiRequest chatRequest = new ApiRequest();
|
|
|
|
+ ApiRequest.Message message = new ApiRequest.Message();
|
|
|
|
+ question = question + ",内容不超过350字";
|
|
|
|
+ message.role("user").content(question);
|
|
|
|
+ messages.add(message);
|
|
|
|
+ chatRequest.penaltyScore(1.0).top_p(0.6).enableTrace(true).enableCitation(false).messages(messages);
|
|
|
|
+ String jsonString = "";
|
|
|
|
+ String resString = "";
|
|
|
|
+ jsonString = objectMapper.writeValueAsString(chatRequest);
|
|
|
|
+ log.info("request------------------------->" + jsonString);
|
|
|
|
+ ChatCompletionResponse chatCompletionResponse = QianFanUtils.firstChat(jsonString);
|
|
|
|
+ resString = objectMapper.writeValueAsString(chatCompletionResponse);
|
|
|
|
+ ChatDialogueVo chatDialogueVo = new ChatDialogueVo();
|
|
|
|
+ chatDialogueVo.chatText(jsonString).userId(userId).id(chatId);
|
|
|
|
+ chatMapper.updateChatDialogue(chatDialogueVo);
|
|
|
|
+ chatMapper.insertUserDetail(jsonString, chatDialogueVo.id());
|
|
|
|
+ chatMapper.insertAssistantDetail(resString, chatDialogueVo.id());
|
|
|
|
+ return ResponseJson.success(chatCompletionResponse);
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|