diff --git a/src/main/java/cn/teammodel/ai/SparkGptClient.java b/src/main/java/cn/teammodel/ai/SparkGptClient.java index a9dd202..24b0e3d 100644 --- a/src/main/java/cn/teammodel/ai/SparkGptClient.java +++ b/src/main/java/cn/teammodel/ai/SparkGptClient.java @@ -32,8 +32,8 @@ import java.util.concurrent.TimeUnit; public class SparkGptClient implements InitializingBean { @Resource private SparkGptProperties sparkGptProperties; -// private OkHttpClient okHttpClient; - private String authUrl; + private OkHttpClient okHttpClient; +// private String authUrl; /** * 静态构造对象方法 @@ -42,16 +42,12 @@ public class SparkGptClient implements InitializingBean { // 初始化缓存 HistoryCache.init(sparkGptProperties.getCache_timeout(), sparkGptProperties.getCache_context()); // 初始化 authUrl - authUrl = genAuthUrl(sparkGptProperties.getEndpoint(), sparkGptProperties.getApiKey(), sparkGptProperties.getApiSecret()); - 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(); +// log.info("[SPARK CHAT] 鉴权 endpoint : {}", this.authUrl); + this.okHttpClient = new OkHttpClient.Builder() + .connectTimeout(180, TimeUnit.SECONDS) + .readTimeout(180, TimeUnit.SECONDS) // sse 接口的 readTimeout 不能设置小了 + .writeTimeout(180, TimeUnit.SECONDS) + .build(); } /** @@ -60,16 +56,13 @@ public class SparkGptClient implements InitializingBean { public void streamChatCompletion(SparkChatRequestParam param, SparkGptStreamListener listener) { try { param.setAppId(sparkGptProperties.getAppId()); + String authUrl = genAuthUrl(sparkGptProperties.getEndpoint(), sparkGptProperties.getApiKey(), sparkGptProperties.getApiSecret()); + authUrl = authUrl.replace("http://", "ws://").replace("https://", "wss://"); Request request = new Request.Builder().url(authUrl).build(); // 设置请求参数 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(); + // 不能使用单例 client okHttpClient.newWebSocket(request, listener); } catch (Exception e) { log.error("[SPARK CHAT] Spark AI 请求异常: {}", e.getMessage());