private void processChat(String userId, String message) { try { log.info("【普通对话】用户: {}, 消息: {}", userId, message); Prompt prompt = new Prompt(message); Flux<String> stream = chatClient.prompt(prompt).stream().content(); Disposable subscription = stream .doOnError(throwable -> { log.error("【对话错误】用户: {}, 错误: {}", userId, throwable.getMessage()); SSEServer.sendMsg(userId, "抱歉,服务出现了一点问题", SSEMsgType.FINISH); userSubscriptions.remove(userId); }) .subscribe( content -> { if (SSEServer.exists(userId)) { SSEServer.sendMsg(userId, content, SSEMsgType.ADD); } }, error -> { log.error("【对话流失败】用户: {}, 错误: {}", userId, error.getMessage()); userSubscriptions.remove(userId); }, () -> { log.info("【对话完成】用户: {}", userId); SSEServer.sendMsg(userId, "done", SSEMsgType.FINISH); SSEServer.close(userId); userSubscriptions.remove(userId); } ); userSubscriptions.put(userId, subscription); } catch (Exception e) { log.error("【对话异常】用户: {}, 错误: {}", userId, e.getMessage()); SSEServer.sendMsg(userId, "系统错误,请稍后重试", SSEMsgType.ERROR); } }
|
交流与讨论
评论由 Valine / LeanCloud 提供,点击后连接第三方服务。