|
|
|
@ -224,37 +224,37 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
|
SseHelper.send(sseEmitter, "[DONE]");
|
|
|
|
|
// 处理完成后的事件: 保存消息记录, 缓存更改
|
|
|
|
|
ChatSession.Message message = ChatSession.Message.of(userPrompt, s);
|
|
|
|
|
HistoryCache.updateContext(sessionId, message);
|
|
|
|
|
HistoryCache.updateContext(userId, message);
|
|
|
|
|
CosmosPatchOperations options = CosmosPatchOperations.create()
|
|
|
|
|
.replace("/updateTime", Instant.now().toEpochMilli())
|
|
|
|
|
.add("/history/-", message);
|
|
|
|
|
chatSessionRepository.save(sessionId, PK.of(PK.CHAT_SESSION), ChatSession.class, options);
|
|
|
|
|
chatSessionRepository.save(userId, PK.of(PK.CHAT_SESSION), ChatSession.class, options);
|
|
|
|
|
});
|
|
|
|
|
// 错误的回调
|
|
|
|
|
listener.setOnError((s) -> {
|
|
|
|
|
log.error("callback: ws error, info: " + s);
|
|
|
|
|
// 返还积分
|
|
|
|
|
});
|
|
|
|
|
List<SparkChatRequestParam.Message> messageList = fetchContext(sessionId, userPrompt);
|
|
|
|
|
List<SparkChatRequestParam.Message> messageList = fetchContext(userId, userPrompt);
|
|
|
|
|
SparkChatRequestParam requestParam = SparkChatRequestParam
|
|
|
|
|
.builder()
|
|
|
|
|
.uid(userId)
|
|
|
|
|
.chatId(sessionId)
|
|
|
|
|
.chatId(userId)
|
|
|
|
|
.messageList(messageList)
|
|
|
|
|
.build();
|
|
|
|
|
sparkGptClient.streamChatCompletion(requestParam, listener);
|
|
|
|
|
return sseEmitter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<SparkChatRequestParam.Message> fetchContext(String sessionId, String prompt) {
|
|
|
|
|
List<ChatSession.Message> context = HistoryCache.getContext(sessionId);
|
|
|
|
|
List<SparkChatRequestParam.Message> fetchContext(String userId, String prompt) {
|
|
|
|
|
List<ChatSession.Message> context = HistoryCache.getContext(userId);
|
|
|
|
|
List<SparkChatRequestParam.Message> paramMessages = new ArrayList<>();
|
|
|
|
|
// 暂未缓存,从数据库拉取
|
|
|
|
|
if (ObjectUtils.isEmpty(context)) {
|
|
|
|
|
context = chatSessionRepository.findLatestMessage(sessionId);
|
|
|
|
|
context = chatSessionRepository.findLatestMessage(userId);
|
|
|
|
|
|
|
|
|
|
if (ObjectUtils.isNotEmpty(context)) {
|
|
|
|
|
HistoryCache.putContext(sessionId, context);
|
|
|
|
|
HistoryCache.putContext(userId, context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|