|
@@ -3,12 +3,14 @@ package com.qmth.ops.biz.ai.client;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.qmth.boot.core.ai.model.llm.ChatRequest;
|
|
import com.qmth.boot.core.ai.model.llm.ChatRequest;
|
|
import com.qmth.boot.core.ai.model.llm.ChatResult;
|
|
import com.qmth.boot.core.ai.model.llm.ChatResult;
|
|
|
|
+import com.qmth.boot.core.ai.model.llm.LlmAppType;
|
|
import com.qmth.boot.core.rateLimit.service.RateLimiter;
|
|
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 java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.time.Duration;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 大模型chat类接口基础实现
|
|
* 大模型chat类接口基础实现
|
|
@@ -24,7 +26,8 @@ public abstract class ChatApiClient {
|
|
private RateLimiter queryRateLimiter;
|
|
private RateLimiter queryRateLimiter;
|
|
|
|
|
|
public ChatApiClient(ChatApiConfig config) {
|
|
public ChatApiClient(ChatApiConfig config) {
|
|
- this.client = new OkHttpClient.Builder().connectionPool(new ConnectionPool()).build();
|
|
|
|
|
|
+ this.client = new OkHttpClient.Builder().connectionPool(new ConnectionPool())
|
|
|
|
+ .connectTimeout(Duration.ofSeconds(10)).readTimeout(Duration.ofSeconds(30)).build();
|
|
this.mapper = new ObjectMapper();
|
|
this.mapper = new ObjectMapper();
|
|
this.config = config;
|
|
this.config = config;
|
|
if (config.getQpm() > 0) {
|
|
if (config.getQpm() > 0) {
|
|
@@ -36,22 +39,22 @@ public abstract class ChatApiClient {
|
|
return config;
|
|
return config;
|
|
}
|
|
}
|
|
|
|
|
|
- protected abstract Headers buildHeader(Headers.Builder headerBuilder);
|
|
|
|
|
|
+ protected abstract Headers buildHeader(Headers.Builder headerBuilder, LlmAppType appType);
|
|
|
|
|
|
- protected abstract Object buildRequest(ChatRequest request);
|
|
|
|
|
|
+ protected abstract Object buildRequest(ChatRequest request, LlmAppType appType);
|
|
|
|
|
|
protected abstract ChatResult buildResult(byte[] data, ObjectMapper mapper) throws IOException;
|
|
protected abstract ChatResult buildResult(byte[] data, ObjectMapper mapper) throws IOException;
|
|
|
|
|
|
protected abstract ChatResult handleError(byte[] data, int statusCode, ObjectMapper mapper);
|
|
protected abstract ChatResult handleError(byte[] data, int statusCode, ObjectMapper mapper);
|
|
|
|
|
|
- public ChatResult call(ChatRequest request) throws Exception {
|
|
|
|
|
|
+ public ChatResult call(ChatRequest request, LlmAppType appType) throws Exception {
|
|
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());
|
|
}
|
|
}
|
|
RequestBody body = RequestBody
|
|
RequestBody body = RequestBody
|
|
- .create(MediaType.parse("application/json"), mapper.writeValueAsBytes(buildRequest(request)));
|
|
|
|
- Request httpRequest = new Request.Builder().url(config.getUrl()).headers(buildHeader(new Headers.Builder()))
|
|
|
|
- .post(body).build();
|
|
|
|
|
|
+ .create(MediaType.parse("application/json"), mapper.writeValueAsBytes(buildRequest(request, appType)));
|
|
|
|
+ Request httpRequest = new Request.Builder().url(config.getUrl())
|
|
|
|
+ .headers(buildHeader(new Headers.Builder(), appType)).post(body).build();
|
|
Response response = client.newCall(httpRequest).execute();
|
|
Response response = client.newCall(httpRequest).execute();
|
|
byte[] data = response.body() != null ? response.body().bytes() : null;
|
|
byte[] data = response.body() != null ? response.body().bytes() : null;
|
|
if (response.isSuccessful()) {
|
|
if (response.isSuccessful()) {
|