|
@@ -8,8 +8,11 @@ import com.qmth.boot.core.rateLimit.service.RateLimiter;
|
|
import com.qmth.boot.core.rateLimit.service.impl.MemoryRateLimiter;
|
|
import com.qmth.boot.core.rateLimit.service.impl.MemoryRateLimiter;
|
|
import com.qmth.ops.biz.ai.exception.ChatRateLimitExceeded;
|
|
import com.qmth.ops.biz.ai.exception.ChatRateLimitExceeded;
|
|
import okhttp3.*;
|
|
import okhttp3.*;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -17,6 +20,8 @@ import java.time.Duration;
|
|
*/
|
|
*/
|
|
public abstract class ChatApiClient {
|
|
public abstract class ChatApiClient {
|
|
|
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ChatApiClient.class);
|
|
|
|
+
|
|
private OkHttpClient client;
|
|
private OkHttpClient client;
|
|
|
|
|
|
private ObjectMapper mapper;
|
|
private ObjectMapper mapper;
|
|
@@ -51,16 +56,28 @@ public abstract class ChatApiClient {
|
|
if (queryRateLimiter != null && !queryRateLimiter.acquire()) {
|
|
if (queryRateLimiter != null && !queryRateLimiter.acquire()) {
|
|
throw new ChatRateLimitExceeded(config.getSupplier(), config.getModel(), config.getQpm());
|
|
throw new ChatRateLimitExceeded(config.getSupplier(), config.getModel(), config.getQpm());
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ Object params = buildRequest(request, appType);
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
|
+ log.debug("ChatRequest:{}", mapper.writeValueAsString(params));
|
|
|
|
+ }
|
|
|
|
+
|
|
RequestBody body = RequestBody
|
|
RequestBody body = RequestBody
|
|
- .create(MediaType.parse("application/json"), mapper.writeValueAsBytes(buildRequest(request, appType)));
|
|
|
|
|
|
+ .create(MediaType.parse("application/json"), mapper.writeValueAsBytes(params));
|
|
Request httpRequest = new Request.Builder().url(config.getUrl())
|
|
Request httpRequest = new Request.Builder().url(config.getUrl())
|
|
.headers(buildHeader(new Headers.Builder(), appType)).post(body).build();
|
|
.headers(buildHeader(new Headers.Builder(), appType)).post(body).build();
|
|
- Response response = client.newCall(httpRequest).execute();
|
|
|
|
- byte[] data = response.body() != null ? response.body().bytes() : null;
|
|
|
|
- if (response.isSuccessful()) {
|
|
|
|
- return buildResult(data, mapper);
|
|
|
|
- } else {
|
|
|
|
- return handleError(data, response.code(), mapper);
|
|
|
|
|
|
+
|
|
|
|
+ try (Response response = client.newCall(httpRequest).execute();) {
|
|
|
|
+ byte[] data = response.body() != null ? response.body().bytes() : null;
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
|
+ log.debug("ChatResult:{}", data != null ? new String(data, StandardCharsets.UTF_8) : null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (response.isSuccessful()) {
|
|
|
|
+ return buildResult(data, mapper);
|
|
|
|
+ } else {
|
|
|
|
+ return handleError(data, response.code(), mapper);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|