chore: expand extra json, update timeout (#62)
This commit is contained in:
parent
3573209ffd
commit
b3ba8ec7bc
9 changed files with 60 additions and 37 deletions
|
|
@ -30,7 +30,7 @@ Add the following dependency to your `pom.xml`:
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ai.z.openapi</groupId>
|
<groupId>ai.z.openapi</groupId>
|
||||||
<artifactId>zai-sdk</artifactId>
|
<artifactId>zai-sdk</artifactId>
|
||||||
<version>0.0.6</version>
|
<version>0.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -39,7 +39,7 @@ Add the following dependency to your `build.gradle` (for Groovy DSL):
|
||||||
|
|
||||||
```groovy
|
```groovy
|
||||||
dependencies {
|
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()
|
.build()
|
||||||
))
|
))
|
||||||
.stream(false)
|
.stream(false)
|
||||||
.temperature(0.7f)
|
.temperature(1.0f)
|
||||||
.maxTokens(1024)
|
.maxTokens(1024)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ Z.ai AI 平台官方 Java SDK,提供统一接口访问强大的AI能力,包
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ai.z.openapi</groupId>
|
<groupId>ai.z.openapi</groupId>
|
||||||
<artifactId>zai-sdk</artifactId>
|
<artifactId>zai-sdk</artifactId>
|
||||||
<version>0.0.6</version>
|
<version>0.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -39,7 +39,7 @@ Z.ai AI 平台官方 Java SDK,提供统一接口访问强大的AI能力,包
|
||||||
|
|
||||||
```groovy
|
```groovy
|
||||||
dependencies {
|
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()
|
.build()
|
||||||
))
|
))
|
||||||
.stream(false)
|
.stream(false)
|
||||||
.temperature(0.7f)
|
.temperature(1.0f)
|
||||||
.maxTokens(1024)
|
.maxTokens(1024)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -313,8 +313,17 @@ public abstract class AbstractAiClient extends AbstractClientBaseService {
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public void close() {
|
public void close() {
|
||||||
if (httpClient != null) {
|
try {
|
||||||
httpClient.dispatcher().executorService().shutdown();
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -96,28 +96,25 @@ public class ZaiConfig {
|
||||||
private TimeUnit connectionPoolTimeUnit = TimeUnit.SECONDS;
|
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 Integer requestTimeOut;
|
||||||
private int requestTimeOut = 300;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connection timeout in specified time unit.
|
* Connection timeout in specified time unit.
|
||||||
*/
|
*/
|
||||||
@Builder.Default
|
private Integer connectTimeout;
|
||||||
private int connectTimeout = 100;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read timeout in specified time unit.
|
* Read timeout in specified time unit.
|
||||||
*/
|
*/
|
||||||
@Builder.Default
|
private Integer readTimeout;
|
||||||
private int readTimeout = 100;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write timeout in specified time unit.
|
* Write timeout in specified time unit.
|
||||||
*/
|
*/
|
||||||
@Builder.Default
|
private Integer writeTimeout;
|
||||||
private int writeTimeout = 100;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Time unit for timeout configurations.
|
* Time unit for timeout configurations.
|
||||||
|
|
@ -282,8 +279,8 @@ public class ZaiConfig {
|
||||||
/**
|
/**
|
||||||
* Gets request timeout with system property and environment variable fallback.
|
* Gets request timeout with system property and environment variable fallback.
|
||||||
*/
|
*/
|
||||||
public int getRequestTimeOut() {
|
public Integer getRequestTimeOut() {
|
||||||
if (requestTimeOut != 300) {
|
if (requestTimeOut != null) {
|
||||||
return requestTimeOut;
|
return requestTimeOut;
|
||||||
}
|
}
|
||||||
String propValue = System.getProperty(ENV_REQUEST_TIMEOUT);
|
String propValue = System.getProperty(ENV_REQUEST_TIMEOUT);
|
||||||
|
|
@ -302,8 +299,8 @@ public class ZaiConfig {
|
||||||
/**
|
/**
|
||||||
* Gets connect timeout with system property and environment variable fallback.
|
* Gets connect timeout with system property and environment variable fallback.
|
||||||
*/
|
*/
|
||||||
public int getConnectTimeout() {
|
public Integer getConnectTimeout() {
|
||||||
if (connectTimeout != 100) {
|
if (connectTimeout != null) {
|
||||||
return connectTimeout;
|
return connectTimeout;
|
||||||
}
|
}
|
||||||
String propValue = System.getProperty(ENV_CONNECT_TIMEOUT);
|
String propValue = System.getProperty(ENV_CONNECT_TIMEOUT);
|
||||||
|
|
@ -322,8 +319,8 @@ public class ZaiConfig {
|
||||||
/**
|
/**
|
||||||
* Gets read timeout with system property and environment variable fallback.
|
* Gets read timeout with system property and environment variable fallback.
|
||||||
*/
|
*/
|
||||||
public int getReadTimeout() {
|
public Integer getReadTimeout() {
|
||||||
if (readTimeout != 100) {
|
if (readTimeout != null) {
|
||||||
return readTimeout;
|
return readTimeout;
|
||||||
}
|
}
|
||||||
String propValue = System.getProperty(ENV_READ_TIMEOUT);
|
String propValue = System.getProperty(ENV_READ_TIMEOUT);
|
||||||
|
|
@ -342,8 +339,8 @@ public class ZaiConfig {
|
||||||
/**
|
/**
|
||||||
* Gets write timeout with system property and environment variable fallback.
|
* Gets write timeout with system property and environment variable fallback.
|
||||||
*/
|
*/
|
||||||
public int getWriteTimeout() {
|
public Integer getWriteTimeout() {
|
||||||
if (writeTimeout != 100) {
|
if (writeTimeout != null) {
|
||||||
return writeTimeout;
|
return writeTimeout;
|
||||||
}
|
}
|
||||||
String propValue = System.getProperty(ENV_WRITE_TIMEOUT);
|
String propValue = System.getProperty(ENV_WRITE_TIMEOUT);
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ public class HttpRequestInterceptor implements Interceptor {
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.header("Authorization", "Bearer " + accessToken)
|
.header("Authorization", "Bearer " + accessToken)
|
||||||
.header("x-source-channel", source_channel)
|
.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");
|
.header("Accept-Language", "en-US,en");
|
||||||
if (Objects.nonNull(config.getCustomHeaders())) {
|
if (Objects.nonNull(config.getCustomHeaders())) {
|
||||||
for (Map.Entry<String, String> entry : config.getCustomHeaders().entrySet()) {
|
for (Map.Entry<String, String> entry : config.getCustomHeaders().entrySet()) {
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,14 @@
|
||||||
package ai.z.openapi.service;
|
package ai.z.openapi.service;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.experimental.SuperBuilder;
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -33,6 +36,20 @@ public class CommonRequest {
|
||||||
@JsonProperty("user_id")
|
@JsonProperty("user_id")
|
||||||
private String userId;
|
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;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,8 @@ import java.util.concurrent.TimeUnit;
|
||||||
*/
|
*/
|
||||||
public final class OkHttps {
|
public final class OkHttps {
|
||||||
|
|
||||||
// Default timeout values
|
// The default value is 0 which imposes no timeout.
|
||||||
private static final int DEFAULT_CALL_TIMEOUT_SECONDS = 360;
|
private static final int DEFAULT_CALL_TIMEOUT_SECONDS = 0;
|
||||||
|
|
||||||
private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 10;
|
private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 10;
|
||||||
|
|
||||||
|
|
@ -62,7 +62,7 @@ public final class OkHttps {
|
||||||
TimeUnit timeUnit = config.getTimeOutTimeUnit();
|
TimeUnit timeUnit = config.getTimeOutTimeUnit();
|
||||||
|
|
||||||
// Configure call timeout
|
// Configure call timeout
|
||||||
if (config.getRequestTimeOut() > 0) {
|
if (config.getRequestTimeOut() != null && config.getRequestTimeOut() > 0) {
|
||||||
builder.callTimeout(config.getRequestTimeOut(), timeUnit);
|
builder.callTimeout(config.getRequestTimeOut(), timeUnit);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -70,7 +70,7 @@ public final class OkHttps {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure connect timeout
|
// Configure connect timeout
|
||||||
if (config.getConnectTimeout() > 0) {
|
if (config.getConnectTimeout() != null && config.getConnectTimeout() > 0) {
|
||||||
builder.connectTimeout(config.getConnectTimeout(), timeUnit);
|
builder.connectTimeout(config.getConnectTimeout(), timeUnit);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -78,7 +78,7 @@ public final class OkHttps {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure read timeout
|
// Configure read timeout
|
||||||
if (config.getReadTimeout() > 0) {
|
if (config.getReadTimeout() != null && config.getReadTimeout() > 0) {
|
||||||
builder.readTimeout(config.getReadTimeout(), timeUnit);
|
builder.readTimeout(config.getReadTimeout(), timeUnit);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -86,7 +86,7 @@ public final class OkHttps {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure write timeout
|
// Configure write timeout
|
||||||
if (config.getWriteTimeout() > 0) {
|
if (config.getWriteTimeout() != null && config.getWriteTimeout() > 0) {
|
||||||
builder.writeTimeout(config.getWriteTimeout(), timeUnit);
|
builder.writeTimeout(config.getWriteTimeout(), timeUnit);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
||||||
2
pom.xml
2
pom.xml
|
|
@ -45,7 +45,7 @@
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<revision>0.0.6.2</revision>
|
<revision>0.1.0</revision>
|
||||||
<java.version>8</java.version>
|
<java.version>8</java.version>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package ai.z.openapi.samples;
|
||||||
|
|
||||||
import ai.z.openapi.ZaiClient;
|
import ai.z.openapi.ZaiClient;
|
||||||
import ai.z.openapi.service.model.*;
|
import ai.z.openapi.service.model.*;
|
||||||
import ai.z.openapi.core.Constants;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -49,15 +48,16 @@ public class ChatCompletionStreamExample {
|
||||||
// Process streaming response completion event
|
// Process streaming response completion event
|
||||||
() -> System.out.println("\nStreaming response completed")
|
() -> System.out.println("\nStreaming response completed")
|
||||||
);
|
);
|
||||||
|
|
||||||
// Wait for streaming response to complete
|
|
||||||
Thread.sleep(10000); // Wait for 10 seconds
|
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Error: " + response.getMsg());
|
System.err.println("Error: " + response.getMsg());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("Exception occurred: " + e.getMessage());
|
System.err.println("Exception occurred: " + e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (client != null) {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue