CrazyIter_Bin 4 months ago
commit bfb5eb7beb

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

Loading…
Cancel
Save