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