From b3ba8ec7bc5a2189efcde8d4eee25ac40cc6be91 Mon Sep 17 00:00:00 2001 From: Tomsun28 Date: Wed, 5 Nov 2025 18:23:36 +0800 Subject: [PATCH] chore: expand extra json, update timeout (#62) --- README.md | 6 ++-- README_CN.md | 6 ++-- .../java/ai/z/openapi/AbstractAiClient.java | 13 ++++++-- .../ai/z/openapi/core/config/ZaiConfig.java | 31 +++++++++---------- .../core/token/HttpRequestInterceptor.java | 2 +- .../ai/z/openapi/service/CommonRequest.java | 17 ++++++++++ .../main/java/ai/z/openapi/utils/OkHttps.java | 12 +++---- pom.xml | 2 +- .../ChatCompletionStreamExample.java | 8 ++--- 9 files changed, 60 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index fc13bcf..91ea82a 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Add the following dependency to your `pom.xml`: ai.z.openapi zai-sdk - 0.0.6 + 0.1.0 ``` @@ -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(); diff --git a/README_CN.md b/README_CN.md index 7def537..ef9c7f2 100644 --- a/README_CN.md +++ b/README_CN.md @@ -30,7 +30,7 @@ Z.ai AI 平台官方 Java SDK,提供统一接口访问强大的AI能力,包 ai.z.openapi zai-sdk - 0.0.6 + 0.1.0 ``` @@ -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(); diff --git a/core/src/main/java/ai/z/openapi/AbstractAiClient.java b/core/src/main/java/ai/z/openapi/AbstractAiClient.java index 31b350b..7df1add 100644 --- a/core/src/main/java/ai/z/openapi/AbstractAiClient.java +++ b/core/src/main/java/ai/z/openapi/AbstractAiClient.java @@ -313,8 +313,17 @@ public abstract class AbstractAiClient extends AbstractClientBaseService { *

*/ 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); } } diff --git a/core/src/main/java/ai/z/openapi/core/config/ZaiConfig.java b/core/src/main/java/ai/z/openapi/core/config/ZaiConfig.java index ac8856d..c85c235 100644 --- a/core/src/main/java/ai/z/openapi/core/config/ZaiConfig.java +++ b/core/src/main/java/ai/z/openapi/core/config/ZaiConfig.java @@ -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); diff --git a/core/src/main/java/ai/z/openapi/core/token/HttpRequestInterceptor.java b/core/src/main/java/ai/z/openapi/core/token/HttpRequestInterceptor.java index 3432a08..e9398e0 100644 --- a/core/src/main/java/ai/z/openapi/core/token/HttpRequestInterceptor.java +++ b/core/src/main/java/ai/z/openapi/core/token/HttpRequestInterceptor.java @@ -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 entry : config.getCustomHeaders().entrySet()) { diff --git a/core/src/main/java/ai/z/openapi/service/CommonRequest.java b/core/src/main/java/ai/z/openapi/service/CommonRequest.java index 71a703f..d13fd14 100644 --- a/core/src/main/java/ai/z/openapi/service/CommonRequest.java +++ b/core/src/main/java/ai/z/openapi/service/CommonRequest.java @@ -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 extraJson; + /** + * Expose dynamic properties as top-level fields during serialization. + */ + @JsonAnyGetter + public Map getExtraJsonFlattened() { + return extraJson == null ? Collections.emptyMap() : extraJson; + } + } diff --git a/core/src/main/java/ai/z/openapi/utils/OkHttps.java b/core/src/main/java/ai/z/openapi/utils/OkHttps.java index 6df7e1a..af2e964 100644 --- a/core/src/main/java/ai/z/openapi/utils/OkHttps.java +++ b/core/src/main/java/ai/z/openapi/utils/OkHttps.java @@ -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 { diff --git a/pom.xml b/pom.xml index 2bb20dc..243e11c 100644 --- a/pom.xml +++ b/pom.xml @@ -45,7 +45,7 @@ - 0.0.6.2 + 0.1.0 8 UTF-8 UTF-8 diff --git a/samples/src/main/ai.z.openapi.samples/ChatCompletionStreamExample.java b/samples/src/main/ai.z.openapi.samples/ChatCompletionStreamExample.java index afa5352..fc9300c 100644 --- a/samples/src/main/ai.z.openapi.samples/ChatCompletionStreamExample.java +++ b/samples/src/main/ai.z.openapi.samples/ChatCompletionStreamExample.java @@ -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(); + } } } } \ No newline at end of file