Skip to content

fix(ai): 修复 AI 聊天缓存类型异常和 SSE 连接关闭问题 #1678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,28 @@ private SseEmitter chatWithBaichuanAi(ChatQueryRequest queryRequest, SseEmitter
* @return
*/
private List<FastChatMessage> getFastChatMessage(String uid, String prompt) {
List<FastChatMessage> messages = (List<FastChatMessage>)LocalCache.CACHE.get(uid);
List<FastChatMessage> messages = null;
Object cachedItem = LocalCache.CACHE.get(uid);

if (cachedItem instanceof List) {
try {
messages = (List<FastChatMessage>) cachedItem;
} catch (ClassCastException e) {
// 处理类型转换异常,创建新的消息列表
log.warn("Error casting cached item to List<FastChatMessage>: {}", e.getMessage());
messages = Lists.newArrayList();
}
} else {
// 如果缓存项不是List类型,创建新的消息列表
messages = Lists.newArrayList();
}

if (CollectionUtils.isNotEmpty(messages)) {
if (messages.size() >= contextLength) {
messages = messages.subList(1, contextLength);
}
} else {
messages = Lists.newArrayList();
}

FastChatMessage currentMessage = new FastChatMessage(FastChatRole.USER).setContent(prompt);
messages.add(currentMessage);
return messages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ public void onEvent(EventSource eventSource, String id, String type, String data
public void onClosed(EventSource eventSource) {
log.info("REST AI close sse connection...");
try {
sseEmitter.send(SseEmitter.event()
// 使用 try-catch 直接尝试发送事件,如果已完成会抛出异常被捕获
this.sseEmitter.send(SseEmitter.event()
.id("[DONE]")
.data("[DONE]")
.reconnectTime(3000));
} catch (IOException e) {
throw new RuntimeException(e);
this.sseEmitter.complete();
} catch (Exception e) {
// 可能是 SseEmitter 已经完成状态,记录日志但不重新抛出异常
log.info("SseEmitter may already be completed: {}", e.getMessage());
}
sseEmitter.complete();
}

@Override
Expand Down