mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-03-14 05:03:43 +08:00
feat: sse对话样式优化
This commit is contained in:
@@ -12,6 +12,7 @@ import okhttp3.sse.EventSource;
|
||||
import okhttp3.sse.EventSourceListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.ruoyi.chat.service.chat.IChatCostService;
|
||||
import org.ruoyi.chat.util.SSEUtil;
|
||||
import org.ruoyi.common.chat.entity.chat.ChatCompletionResponse;
|
||||
import org.ruoyi.common.chat.request.ChatRequest;
|
||||
import org.ruoyi.common.core.utils.SpringUtils;
|
||||
@@ -84,10 +85,10 @@ public class SSEEventSourceListener extends EventSourceListener {
|
||||
modelName = completionResponse.getModel();
|
||||
}
|
||||
stringBuffer.append(content);
|
||||
emitter.send(content);
|
||||
emitter.send(data);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
emitter.completeWithError(e);
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.ruoyi.chat.enums.ChatModeType;
|
||||
import org.ruoyi.chat.service.chat.IChatCostService;
|
||||
import org.ruoyi.chat.service.chat.ISseService;
|
||||
import org.ruoyi.chat.util.IpUtil;
|
||||
import org.ruoyi.chat.util.SSEUtil;
|
||||
import org.ruoyi.common.chat.config.LocalCache;
|
||||
import org.ruoyi.common.chat.entity.Tts.TextToSpeech;
|
||||
import org.ruoyi.common.chat.entity.chat.ChatCompletion;
|
||||
@@ -88,7 +89,7 @@ public class SseServiceImpl implements ISseService {
|
||||
switchModelAndHandle(chatRequest,sseEmitter);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(),e);
|
||||
sseEmitter.completeWithError(e);
|
||||
SSEUtil.sendErrorEvent(sseEmitter,e.getMessage());
|
||||
}
|
||||
return sseEmitter;
|
||||
}
|
||||
|
||||
@@ -21,11 +21,8 @@ public class SSEUtil {
|
||||
* @param errorMessage 错误信息
|
||||
*/
|
||||
public static void sendErrorEvent(ResponseBodyEmitter sseEmitter, String errorMessage) {
|
||||
SseEmitter.SseEventBuilder event = SseEmitter.event()
|
||||
.name("error")
|
||||
.data(errorMessage);
|
||||
try {
|
||||
sseEmitter.send(event);
|
||||
sseEmitter.send(errorMessage);
|
||||
} catch (IOException e) {
|
||||
log.error("SSE发送失败: {}", e.getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user