From ab2aaa1757fc549948dbe701249a106a9f143b4d Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Thu, 28 Dec 2023 14:07:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9B=A0=E4=B8=BA?= =?UTF-8?q?=E5=8D=95=E4=BE=8B=20OkHttpClient=20=E7=9A=84=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=AF=BC=E8=87=B4ws=E8=BF=9E=E6=8E=A5=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/teammodel/ai/SparkGptClient.java | 20 ++++++++++++------- .../ai/listener/SparkGptStreamListener.java | 5 +++-- .../service/impl/ChatMessageServiceImpl.java | 10 +++++----- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/java/cn/teammodel/ai/SparkGptClient.java b/src/main/java/cn/teammodel/ai/SparkGptClient.java index fd008df..a9dd202 100644 --- a/src/main/java/cn/teammodel/ai/SparkGptClient.java +++ b/src/main/java/cn/teammodel/ai/SparkGptClient.java @@ -32,7 +32,7 @@ import java.util.concurrent.TimeUnit; public class SparkGptClient implements InitializingBean { @Resource private SparkGptProperties sparkGptProperties; - private OkHttpClient okHttpClient; +// private OkHttpClient okHttpClient; private String authUrl; /** @@ -46,12 +46,12 @@ public class SparkGptClient implements InitializingBean { this.authUrl = authUrl.replace("http://", "ws://").replace("https://", "wss://"); log.info("[SPARK CHAT] 鉴权 endpoint : {}", this.authUrl); // 初始化 okHttpClient - this.okHttpClient = new OkHttpClient() - .newBuilder() - .connectTimeout(90, TimeUnit.SECONDS) - .readTimeout(90, TimeUnit.SECONDS) // sse 接口的 readTimeout 不能设置小了 - .writeTimeout(90, TimeUnit.SECONDS) - .build(); +// this.okHttpClient = new OkHttpClient() +// .newBuilder() +// .connectTimeout(90, TimeUnit.SECONDS) +// .readTimeout(90, TimeUnit.SECONDS) // sse 接口的 readTimeout 不能设置小了 +// .writeTimeout(90, TimeUnit.SECONDS) +// .build(); } /** @@ -64,6 +64,12 @@ public class SparkGptClient implements InitializingBean { // 设置请求参数 listener.setRequestJson(param.toJsonParams()); log.info("[SPARK CHAT] 请求参数 {}", JSONUtil.parseObj(param.toJsonParams()).toStringPretty()); + OkHttpClient okHttpClient = new OkHttpClient() + .newBuilder() + .connectTimeout(90, TimeUnit.SECONDS) + .readTimeout(90, TimeUnit.SECONDS) // sse 接口的 readTimeout 不能设置小了 + .writeTimeout(90, TimeUnit.SECONDS) + .build(); okHttpClient.newWebSocket(request, listener); } catch (Exception e) { log.error("[SPARK CHAT] Spark AI 请求异常: {}", e.getMessage()); diff --git a/src/main/java/cn/teammodel/ai/listener/SparkGptStreamListener.java b/src/main/java/cn/teammodel/ai/listener/SparkGptStreamListener.java index 66937a0..771f058 100644 --- a/src/main/java/cn/teammodel/ai/listener/SparkGptStreamListener.java +++ b/src/main/java/cn/teammodel/ai/listener/SparkGptStreamListener.java @@ -63,8 +63,9 @@ public class SparkGptStreamListener extends WebSocketListener { } // 推送回答 segment String msgSegment = sparkChatResponse.getPayload().getChoices().getText().get(0).getContent(); - // 处理消息格式(空格和换行符) - msgSegment = StrUtil.replace(msgSegment, " ", " ").replaceAll("\n", "\n"); +// 处理消息格式(空格和换行符) +// msgSegment = StrUtil.replace(msgSegment, " ", " ").replaceAll("\n", "\n"); + msgSegment = StrUtil.replace(msgSegment, " ", " ").replaceAll("\n", "
"); answer += msgSegment; SseHelper.send(sseEmitter, msgSegment); diff --git a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java index 09f975a..aa7aa4d 100644 --- a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java @@ -93,14 +93,14 @@ public class ChatMessageServiceImpl implements ChatMessageService { if (ObjectUtils.isNotEmpty(context)) { HistoryCache.putContext(sessionId, context); - // convert DB Message to Spark Message - context.forEach(item -> { - paramMessages.add(SparkChatRequestParam.Message.ofUser(item.getUserText())); - paramMessages.add(SparkChatRequestParam.Message.ofAssistant(item.getGptText())); - }); } } + // convert DB Message to Spark Message + context.forEach(item -> { + paramMessages.add(SparkChatRequestParam.Message.ofUser(item.getUserText())); + paramMessages.add(SparkChatRequestParam.Message.ofAssistant(item.getGptText())); + }); paramMessages.add(SparkChatRequestParam.Message.ofUser(prompt)); return paramMessages; }