Skip to content

Commit b3ba8ec

Browse files
authored
chore: expand extra json, update timeout (#62)
1 parent 3573209 commit b3ba8ec

File tree

9 files changed

+60
-37
lines changed

9 files changed

+60
-37
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Add the following dependency to your `pom.xml`:
3030
<dependency>
3131
<groupId>ai.z.openapi</groupId>
3232
<artifactId>zai-sdk</artifactId>
33-
<version>0.0.6</version>
33+
<version>0.1.0</version>
3434
</dependency>
3535
```
3636

@@ -39,7 +39,7 @@ Add the following dependency to your `build.gradle` (for Groovy DSL):
3939

4040
```groovy
4141
dependencies {
42-
implementation 'ai.z.openapi:zai-sdk:0.0.6'
42+
implementation 'ai.z.openapi:zai-sdk:0.1.0'
4343
}
4444
```
4545

@@ -132,7 +132,7 @@ ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
132132
.build()
133133
))
134134
.stream(false)
135-
.temperature(0.7f)
135+
.temperature(1.0f)
136136
.maxTokens(1024)
137137
.build();
138138

README_CN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Z.ai AI 平台官方 Java SDK,提供统一接口访问强大的AI能力,包
3030
<dependency>
3131
<groupId>ai.z.openapi</groupId>
3232
<artifactId>zai-sdk</artifactId>
33-
<version>0.0.6</version>
33+
<version>0.1.0</version>
3434
</dependency>
3535
```
3636

@@ -39,7 +39,7 @@ Z.ai AI 平台官方 Java SDK,提供统一接口访问强大的AI能力,包
3939

4040
```groovy
4141
dependencies {
42-
implementation 'ai.z.openapi:zai-sdk:0.0.6'
42+
implementation 'ai.z.openapi:zai-sdk:0.1.0'
4343
}
4444
```
4545

@@ -131,7 +131,7 @@ ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
131131
.build()
132132
))
133133
.stream(false)
134-
.temperature(0.7f)
134+
.temperature(1.0f)
135135
.maxTokens(1024)
136136
.build();
137137

core/src/main/java/ai/z/openapi/AbstractAiClient.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,17 @@ public Retrofit retrofit() {
313313
* </p>
314314
*/
315315
public void close() {
316-
if (httpClient != null) {
317-
httpClient.dispatcher().executorService().shutdown();
316+
try {
317+
if (httpClient != null) {
318+
httpClient.dispatcher().executorService().shutdown();
319+
httpClient.connectionPool().evictAll();
320+
if (httpClient.cache() != null) {
321+
httpClient.cache().close();
322+
}
323+
}
324+
}
325+
catch (Exception e) {
326+
logger.error("Error closing http client", e);
318327
}
319328
}
320329

core/src/main/java/ai/z/openapi/core/config/ZaiConfig.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,25 @@ public class ZaiConfig {
9696
private TimeUnit connectionPoolTimeUnit = TimeUnit.SECONDS;
9797

9898
/**
99-
* Request timeout in specified time unit.
99+
* Request timeout in specified time unit. The whole timeout for complete calls, is
100+
* the okhttp call timeout.
100101
*/
101-
@Builder.Default
102-
private int requestTimeOut = 300;
102+
private Integer requestTimeOut;
103103

104104
/**
105105
* Connection timeout in specified time unit.
106106
*/
107-
@Builder.Default
108-
private int connectTimeout = 100;
107+
private Integer connectTimeout;
109108

110109
/**
111110
* Read timeout in specified time unit.
112111
*/
113-
@Builder.Default
114-
private int readTimeout = 100;
112+
private Integer readTimeout;
115113

116114
/**
117115
* Write timeout in specified time unit.
118116
*/
119-
@Builder.Default
120-
private int writeTimeout = 100;
117+
private Integer writeTimeout;
121118

122119
/**
123120
* Time unit for timeout configurations.
@@ -282,8 +279,8 @@ public TimeUnit getConnectionPoolTimeUnit() {
282279
/**
283280
* Gets request timeout with system property and environment variable fallback.
284281
*/
285-
public int getRequestTimeOut() {
286-
if (requestTimeOut != 300) {
282+
public Integer getRequestTimeOut() {
283+
if (requestTimeOut != null) {
287284
return requestTimeOut;
288285
}
289286
String propValue = System.getProperty(ENV_REQUEST_TIMEOUT);
@@ -302,8 +299,8 @@ public int getRequestTimeOut() {
302299
/**
303300
* Gets connect timeout with system property and environment variable fallback.
304301
*/
305-
public int getConnectTimeout() {
306-
if (connectTimeout != 100) {
302+
public Integer getConnectTimeout() {
303+
if (connectTimeout != null) {
307304
return connectTimeout;
308305
}
309306
String propValue = System.getProperty(ENV_CONNECT_TIMEOUT);
@@ -322,8 +319,8 @@ public int getConnectTimeout() {
322319
/**
323320
* Gets read timeout with system property and environment variable fallback.
324321
*/
325-
public int getReadTimeout() {
326-
if (readTimeout != 100) {
322+
public Integer getReadTimeout() {
323+
if (readTimeout != null) {
327324
return readTimeout;
328325
}
329326
String propValue = System.getProperty(ENV_READ_TIMEOUT);
@@ -342,8 +339,8 @@ public int getReadTimeout() {
342339
/**
343340
* Gets write timeout with system property and environment variable fallback.
344341
*/
345-
public int getWriteTimeout() {
346-
if (writeTimeout != 100) {
342+
public Integer getWriteTimeout() {
343+
if (writeTimeout != null) {
347344
return writeTimeout;
348345
}
349346
String propValue = System.getProperty(ENV_WRITE_TIMEOUT);

core/src/main/java/ai/z/openapi/core/token/HttpRequestInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Response intercept(Chain chain) throws IOException {
4040
.newBuilder()
4141
.header("Authorization", "Bearer " + accessToken)
4242
.header("x-source-channel", source_channel)
43-
.header("Zai-SDK-Ver", "0.0.6")
43+
.header("Zai-SDK-Ver", "0.1.0")
4444
.header("Accept-Language", "en-US,en");
4545
if (Objects.nonNull(config.getCustomHeaders())) {
4646
for (Map.Entry<String, String> entry : config.getCustomHeaders().entrySet()) {

core/src/main/java/ai/z/openapi/service/CommonRequest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package ai.z.openapi.service;
22

3+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
4+
import com.fasterxml.jackson.annotation.JsonIgnore;
35
import com.fasterxml.jackson.annotation.JsonProperty;
46
import lombok.AllArgsConstructor;
57
import lombok.Data;
68
import lombok.NoArgsConstructor;
79
import lombok.experimental.SuperBuilder;
810

11+
import java.util.Collections;
912
import java.util.Map;
1013

1114
/**
@@ -33,6 +36,20 @@ public class CommonRequest {
3336
@JsonProperty("user_id")
3437
private String userId;
3538

39+
/**
40+
* Extra custom parameters merged into the top-level JSON. This map will not be
41+
* serialized as a nested "extraJson" object; instead, its entries are flattened into
42+
* the request JSON via {@link JsonAnyGetter}.
43+
*/
44+
@JsonIgnore
3645
private Map<String, Object> extraJson;
3746

47+
/**
48+
* Expose dynamic properties as top-level fields during serialization.
49+
*/
50+
@JsonAnyGetter
51+
public Map<String, Object> getExtraJsonFlattened() {
52+
return extraJson == null ? Collections.emptyMap() : extraJson;
53+
}
54+
3855
}

core/src/main/java/ai/z/openapi/utils/OkHttps.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
public final class OkHttps {
1717

18-
// Default timeout values
19-
private static final int DEFAULT_CALL_TIMEOUT_SECONDS = 360;
18+
// The default value is 0 which imposes no timeout.
19+
private static final int DEFAULT_CALL_TIMEOUT_SECONDS = 0;
2020

2121
private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 10;
2222

@@ -62,31 +62,31 @@ private static void configureTimeouts(OkHttpClient.Builder builder, ZaiConfig co
6262
TimeUnit timeUnit = config.getTimeOutTimeUnit();
6363

6464
// Configure call timeout
65-
if (config.getRequestTimeOut() > 0) {
65+
if (config.getRequestTimeOut() != null && config.getRequestTimeOut() > 0) {
6666
builder.callTimeout(config.getRequestTimeOut(), timeUnit);
6767
}
6868
else {
6969
builder.callTimeout(DEFAULT_CALL_TIMEOUT_SECONDS, TimeUnit.SECONDS);
7070
}
7171

7272
// Configure connect timeout
73-
if (config.getConnectTimeout() > 0) {
73+
if (config.getConnectTimeout() != null && config.getConnectTimeout() > 0) {
7474
builder.connectTimeout(config.getConnectTimeout(), timeUnit);
7575
}
7676
else {
7777
builder.connectTimeout(DEFAULT_CONNECT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
7878
}
7979

8080
// Configure read timeout
81-
if (config.getReadTimeout() > 0) {
81+
if (config.getReadTimeout() != null && config.getReadTimeout() > 0) {
8282
builder.readTimeout(config.getReadTimeout(), timeUnit);
8383
}
8484
else {
8585
builder.readTimeout(DEFAULT_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS);
8686
}
8787

8888
// Configure write timeout
89-
if (config.getWriteTimeout() > 0) {
89+
if (config.getWriteTimeout() != null && config.getWriteTimeout() > 0) {
9090
builder.writeTimeout(config.getWriteTimeout(), timeUnit);
9191
}
9292
else {

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
</scm>
4646

4747
<properties>
48-
<revision>0.0.6.2</revision>
48+
<revision>0.1.0</revision>
4949
<java.version>8</java.version>
5050
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5151
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

samples/src/main/ai.z.openapi.samples/ChatCompletionStreamExample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ai.z.openapi.ZaiClient;
44
import ai.z.openapi.service.model.*;
5-
import ai.z.openapi.core.Constants;
65
import java.util.Arrays;
76

87
/**
@@ -49,15 +48,16 @@ public static void main(String[] args) {
4948
// Process streaming response completion event
5049
() -> System.out.println("\nStreaming response completed")
5150
);
52-
53-
// Wait for streaming response to complete
54-
Thread.sleep(10000); // Wait for 10 seconds
5551
} else {
5652
System.err.println("Error: " + response.getMsg());
5753
}
5854
} catch (Exception e) {
5955
System.err.println("Exception occurred: " + e.getMessage());
6056
e.printStackTrace();
57+
} finally {
58+
if (client != null) {
59+
client.close();
60+
}
6161
}
6262
}
6363
}

0 commit comments

Comments
 (0)