chore: expand extra json, update timeout (#62)

This commit is contained in:
Tomsun28 2025-11-05 18:23:36 +08:00 committed by GitHub
parent 3573209ffd
commit b3ba8ec7bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 60 additions and 37 deletions

View file

@ -30,7 +30,7 @@ Add the following dependency to your `pom.xml`:
<dependency>
<groupId>ai.z.openapi</groupId>
<artifactId>zai-sdk</artifactId>
<version>0.0.6</version>
<version>0.1.0</version>
</dependency>
```
@ -39,7 +39,7 @@ Add the following dependency to your `build.gradle` (for Groovy DSL):
```groovy
dependencies {
implementation 'ai.z.openapi:zai-sdk:0.0.6'
implementation 'ai.z.openapi:zai-sdk:0.1.0'
}
```
@ -132,7 +132,7 @@ ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
.build()
))
.stream(false)
.temperature(0.7f)
.temperature(1.0f)
.maxTokens(1024)
.build();

View file

@ -30,7 +30,7 @@ Z.ai AI 平台官方 Java SDK提供统一接口访问强大的AI能力
<dependency>
<groupId>ai.z.openapi</groupId>
<artifactId>zai-sdk</artifactId>
<version>0.0.6</version>
<version>0.1.0</version>
</dependency>
```
@ -39,7 +39,7 @@ Z.ai AI 平台官方 Java SDK提供统一接口访问强大的AI能力
```groovy
dependencies {
implementation 'ai.z.openapi:zai-sdk:0.0.6'
implementation 'ai.z.openapi:zai-sdk:0.1.0'
}
```
@ -131,7 +131,7 @@ ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
.build()
))
.stream(false)
.temperature(0.7f)
.temperature(1.0f)
.maxTokens(1024)
.build();

View file

@ -313,8 +313,17 @@ public abstract class AbstractAiClient extends AbstractClientBaseService {
* </p>
*/
public void close() {
if (httpClient != null) {
httpClient.dispatcher().executorService().shutdown();
try {
if (httpClient != null) {
httpClient.dispatcher().executorService().shutdown();
httpClient.connectionPool().evictAll();
if (httpClient.cache() != null) {
httpClient.cache().close();
}
}
}
catch (Exception e) {
logger.error("Error closing http client", e);
}
}

View file

@ -96,28 +96,25 @@ public class ZaiConfig {
private TimeUnit connectionPoolTimeUnit = TimeUnit.SECONDS;
/**
* Request timeout in specified time unit.
* Request timeout in specified time unit. The whole timeout for complete calls, is
* the okhttp call timeout.
*/
@Builder.Default
private int requestTimeOut = 300;
private Integer requestTimeOut;
/**
* Connection timeout in specified time unit.
*/
@Builder.Default
private int connectTimeout = 100;
private Integer connectTimeout;
/**
* Read timeout in specified time unit.
*/
@Builder.Default
private int readTimeout = 100;
private Integer readTimeout;
/**
* Write timeout in specified time unit.
*/
@Builder.Default
private int writeTimeout = 100;
private Integer writeTimeout;
/**
* Time unit for timeout configurations.
@ -282,8 +279,8 @@ public class ZaiConfig {
/**
* Gets request timeout with system property and environment variable fallback.
*/
public int getRequestTimeOut() {
if (requestTimeOut != 300) {
public Integer getRequestTimeOut() {
if (requestTimeOut != null) {
return requestTimeOut;
}
String propValue = System.getProperty(ENV_REQUEST_TIMEOUT);
@ -302,8 +299,8 @@ public class ZaiConfig {
/**
* Gets connect timeout with system property and environment variable fallback.
*/
public int getConnectTimeout() {
if (connectTimeout != 100) {
public Integer getConnectTimeout() {
if (connectTimeout != null) {
return connectTimeout;
}
String propValue = System.getProperty(ENV_CONNECT_TIMEOUT);
@ -322,8 +319,8 @@ public class ZaiConfig {
/**
* Gets read timeout with system property and environment variable fallback.
*/
public int getReadTimeout() {
if (readTimeout != 100) {
public Integer getReadTimeout() {
if (readTimeout != null) {
return readTimeout;
}
String propValue = System.getProperty(ENV_READ_TIMEOUT);
@ -342,8 +339,8 @@ public class ZaiConfig {
/**
* Gets write timeout with system property and environment variable fallback.
*/
public int getWriteTimeout() {
if (writeTimeout != 100) {
public Integer getWriteTimeout() {
if (writeTimeout != null) {
return writeTimeout;
}
String propValue = System.getProperty(ENV_WRITE_TIMEOUT);

View file

@ -40,7 +40,7 @@ public class HttpRequestInterceptor implements Interceptor {
.newBuilder()
.header("Authorization", "Bearer " + accessToken)
.header("x-source-channel", source_channel)
.header("Zai-SDK-Ver", "0.0.6")
.header("Zai-SDK-Ver", "0.1.0")
.header("Accept-Language", "en-US,en");
if (Objects.nonNull(config.getCustomHeaders())) {
for (Map.Entry<String, String> entry : config.getCustomHeaders().entrySet()) {

View file

@ -1,11 +1,14 @@
package ai.z.openapi.service;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.util.Collections;
import java.util.Map;
/**
@ -33,6 +36,20 @@ public class CommonRequest {
@JsonProperty("user_id")
private String userId;
/**
* Extra custom parameters merged into the top-level JSON. This map will not be
* serialized as a nested "extraJson" object; instead, its entries are flattened into
* the request JSON via {@link JsonAnyGetter}.
*/
@JsonIgnore
private Map<String, Object> extraJson;
/**
* Expose dynamic properties as top-level fields during serialization.
*/
@JsonAnyGetter
public Map<String, Object> getExtraJsonFlattened() {
return extraJson == null ? Collections.emptyMap() : extraJson;
}
}

View file

@ -15,8 +15,8 @@ import java.util.concurrent.TimeUnit;
*/
public final class OkHttps {
// Default timeout values
private static final int DEFAULT_CALL_TIMEOUT_SECONDS = 360;
// The default value is 0 which imposes no timeout.
private static final int DEFAULT_CALL_TIMEOUT_SECONDS = 0;
private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 10;
@ -62,7 +62,7 @@ public final class OkHttps {
TimeUnit timeUnit = config.getTimeOutTimeUnit();
// Configure call timeout
if (config.getRequestTimeOut() > 0) {
if (config.getRequestTimeOut() != null && config.getRequestTimeOut() > 0) {
builder.callTimeout(config.getRequestTimeOut(), timeUnit);
}
else {
@ -70,7 +70,7 @@ public final class OkHttps {
}
// Configure connect timeout
if (config.getConnectTimeout() > 0) {
if (config.getConnectTimeout() != null && config.getConnectTimeout() > 0) {
builder.connectTimeout(config.getConnectTimeout(), timeUnit);
}
else {
@ -78,7 +78,7 @@ public final class OkHttps {
}
// Configure read timeout
if (config.getReadTimeout() > 0) {
if (config.getReadTimeout() != null && config.getReadTimeout() > 0) {
builder.readTimeout(config.getReadTimeout(), timeUnit);
}
else {
@ -86,7 +86,7 @@ public final class OkHttps {
}
// Configure write timeout
if (config.getWriteTimeout() > 0) {
if (config.getWriteTimeout() != null && config.getWriteTimeout() > 0) {
builder.writeTimeout(config.getWriteTimeout(), timeUnit);
}
else {

View file

@ -45,7 +45,7 @@
</scm>
<properties>
<revision>0.0.6.2</revision>
<revision>0.1.0</revision>
<java.version>8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

View file

@ -2,7 +2,6 @@ package ai.z.openapi.samples;
import ai.z.openapi.ZaiClient;
import ai.z.openapi.service.model.*;
import ai.z.openapi.core.Constants;
import java.util.Arrays;
/**
@ -49,15 +48,16 @@ public class ChatCompletionStreamExample {
// Process streaming response completion event
() -> System.out.println("\nStreaming response completed")
);
// Wait for streaming response to complete
Thread.sleep(10000); // Wait for 10 seconds
} else {
System.err.println("Error: " + response.getMsg());
}
} catch (Exception e) {
System.err.println("Exception occurred: " + e.getMessage());
e.printStackTrace();
} finally {
if (client != null) {
client.close();
}
}
}
}