mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 04:35:14 +08:00
增加超时时间和读取超时时间设置
This commit is contained in:
parent
3be0111884
commit
82399a185e
@ -110,4 +110,36 @@ public interface AIConfig {
|
||||
*/
|
||||
Map<String, Object> getAdditionalConfigMap();
|
||||
|
||||
/**
|
||||
* 设置连接超时时间
|
||||
*
|
||||
* @param timeout 连接超时时间
|
||||
* @since 5.8.39
|
||||
*/
|
||||
void setTimeout(int timeout);
|
||||
|
||||
/**
|
||||
* 获取连接超时时间
|
||||
*
|
||||
* @return timeout
|
||||
* @since 5.8.39
|
||||
*/
|
||||
int getTimeout();
|
||||
|
||||
/**
|
||||
* 设置读取超时时间
|
||||
*
|
||||
* @param readTimeout 连接超时时间
|
||||
* @since 5.8.39
|
||||
*/
|
||||
void setReadTimeout(int readTimeout);
|
||||
|
||||
/**
|
||||
* 获取读取超时时间
|
||||
*
|
||||
* @return readTimeout
|
||||
* @since 5.8.39
|
||||
*/
|
||||
int getReadTimeout();
|
||||
|
||||
}
|
||||
|
@ -106,6 +106,34 @@ public class AIConfigBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置连接超时时间,不设置为默认值
|
||||
*
|
||||
* @param timeout 超时时间
|
||||
* @return config
|
||||
* @since 5.8.39
|
||||
*/
|
||||
public synchronized AIConfigBuilder setTimout(final int timeout) {
|
||||
if (timeout > 0) {
|
||||
config.setTimeout(timeout);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置读取超时时间,不设置为默认值
|
||||
*
|
||||
* @param readTimout 取超时时间
|
||||
* @return config
|
||||
* @since 5.8.39
|
||||
*/
|
||||
public synchronized AIConfigBuilder setReadTimout(final int readTimout) {
|
||||
if (readTimout > 0) {
|
||||
config.setReadTimeout(readTimout);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回config实例
|
||||
*
|
||||
|
@ -59,7 +59,7 @@ public class BaseAIService {
|
||||
return HttpRequest.get(config.getApiUrl() + endpoint)
|
||||
.header(Header.ACCEPT, "application/json")
|
||||
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
||||
.timeout(180000)
|
||||
.timeout(config.getTimeout())
|
||||
.execute();
|
||||
} catch (final AIException e) {
|
||||
throw new AIException("Failed to send GET request: " + e.getMessage(), e);
|
||||
@ -80,7 +80,7 @@ public class BaseAIService {
|
||||
.header(Header.ACCEPT, "application/json")
|
||||
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
||||
.body(paramJson)
|
||||
.timeout(180000)
|
||||
.timeout(config.getTimeout())
|
||||
.execute();
|
||||
} catch (final AIException e) {
|
||||
throw new AIException("Failed to send POST request:" + e.getMessage(), e);
|
||||
@ -103,7 +103,7 @@ public class BaseAIService {
|
||||
.header(Header.ACCEPT, "application/json")
|
||||
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
||||
.form(paramMap)
|
||||
.timeout(180000)
|
||||
.timeout(config.getTimeout())
|
||||
.execute();
|
||||
} catch (final AIException e) {
|
||||
throw new AIException("Failed to send POST request:" + e.getMessage(), e);
|
||||
@ -128,9 +128,9 @@ public class BaseAIService {
|
||||
connection.setRequestProperty(Header.AUTHORIZATION.getValue(), "Bearer " + config.getApiKey());
|
||||
connection.setDoOutput(true);
|
||||
//5分钟
|
||||
connection.setReadTimeout(300000);
|
||||
connection.setReadTimeout(config.getReadTimeout());
|
||||
//3分钟
|
||||
connection.setConnectTimeout(180000);
|
||||
connection.setConnectTimeout(config.getTimeout());
|
||||
// 发送请求体
|
||||
try (OutputStream os = connection.getOutputStream()) {
|
||||
String jsonInputString = JSONUtil.toJsonStr(paramMap);
|
||||
|
@ -35,6 +35,10 @@ public class BaseConfig implements AIConfig {
|
||||
protected volatile String model;
|
||||
//动态扩展字段
|
||||
protected Map<String, Object> additionalConfig = new ConcurrentHashMap<>();
|
||||
//连接超时时间
|
||||
protected volatile int timeout = 180000;
|
||||
//读取超时时间
|
||||
protected volatile int readTimeout = 300000;
|
||||
|
||||
@Override
|
||||
public void setApiKey(final String apiKey) {
|
||||
@ -81,4 +85,23 @@ public class BaseConfig implements AIConfig {
|
||||
return new ConcurrentHashMap<>(additionalConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTimeout(final int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getReadTimeout() {
|
||||
return readTimeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadTimeout(final int readTimeout) {
|
||||
this.readTimeout = readTimeout;
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ class DeepSeekServiceTest {
|
||||
|
||||
deepSeekService.chat(prompt, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
@ -92,7 +92,7 @@ class DeepSeekServiceTest {
|
||||
|
||||
deepSeekService.beta(beta, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
|
@ -55,7 +55,7 @@ class DoubaoServiceTest {
|
||||
|
||||
doubaoService.chat(prompt, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
@ -111,7 +111,7 @@ class DoubaoServiceTest {
|
||||
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||
doubaoService.chatVision(prompt,images, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
@ -188,7 +188,7 @@ class DoubaoServiceTest {
|
||||
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||
doubaoService.botsChat(messages, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
@ -286,7 +286,7 @@ class DoubaoServiceTest {
|
||||
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||
doubaoService.chatContext(messages,contextId, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
|
@ -56,7 +56,7 @@ class GrokServiceTest {
|
||||
|
||||
grokService.chat(prompt, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
@ -96,7 +96,7 @@ class GrokServiceTest {
|
||||
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||
grokService.message(prompt, 4096, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
@ -130,7 +130,7 @@ class GrokServiceTest {
|
||||
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||
grokService.chatVision(prompt,images, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
|
@ -59,7 +59,7 @@ class HutoolServiceTest {
|
||||
|
||||
hutoolService.chat(prompt, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
@ -102,7 +102,7 @@ class HutoolServiceTest {
|
||||
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||
hutoolService.chatVision(prompt,images, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data:[DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
|
@ -62,7 +62,7 @@ class OpenaiServiceTest {
|
||||
|
||||
openaiService.chat(prompt, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
@ -107,7 +107,7 @@ class OpenaiServiceTest {
|
||||
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||
openaiService.chatVision(prompt,images, data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
@ -229,7 +229,7 @@ class OpenaiServiceTest {
|
||||
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||
openaiService.chatReasoning(messages,OpenaiCommon.OpenaiReasoning.HIGH.getEffort(), data -> {
|
||||
assertNotNull(data);
|
||||
if (data.equals("data: [DONE]")) {
|
||||
if (data.contains("[DONE]")) {
|
||||
// 设置结束标志
|
||||
isDone.set(true);
|
||||
} else if (data.contains("\"error\"")) {
|
||||
|
Loading…
Reference in New Issue
Block a user