Skip to content

Commit da893fe

Browse files
author
mengqian
committed
Merge remote-tracking branch 'origin/main' into feature/file-parsing
# Conflicts: # core/src/main/java/ai/z/openapi/AbstractAiClient.java
2 parents d26c2d8 + e5660e3 commit da893fe

31 files changed

+883
-84
lines changed

README.md

Lines changed: 5 additions & 5 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.5</version>
33+
<version>0.0.6</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.5'
42+
implementation 'ai.z.openapi:zai-sdk:0.0.6'
4343
}
4444
```
4545

@@ -124,7 +124,7 @@ ZaiClient client = ZaiClient.builder()
124124

125125
// Create chat request
126126
ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
127-
.model(Constants.ModelChatGLM4)
127+
.model("glm-4.6")
128128
.messages(Arrays.asList(
129129
ChatMessage.builder()
130130
.role(ChatMessageRole.USER.value())
@@ -152,7 +152,7 @@ if (response.isSuccess()) {
152152
```java
153153
// Create streaming request
154154
ChatCompletionCreateParams streamRequest = ChatCompletionCreateParams.builder()
155-
.model(Constants.ModelChatGLM4)
155+
.model("glm-4.6")
156156
.messages(Arrays.asList(
157157
ChatMessage.builder()
158158
.role(ChatMessageRole.USER.value())
@@ -281,7 +281,7 @@ public class AIController {
281281
@PostMapping("/chat")
282282
public ResponseEntity<String> chat(@RequestBody ChatRequest request) {
283283
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
284-
.model(Constants.ModelChatGLM4)
284+
.model("glm-4.6")
285285
.messages(Arrays.asList(
286286
ChatMessage.builder()
287287
.role(ChatMessageRole.USER.value())

README_CN.md

Lines changed: 5 additions & 5 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.5</version>
33+
<version>0.0.6</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.5'
42+
implementation 'ai.z.openapi:zai-sdk:0.0.6'
4343
}
4444
```
4545

@@ -123,7 +123,7 @@ ZaiClient client = ZaiClient.builder()
123123

124124
// 创建对话请求
125125
ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
126-
.model(Constants.ModelChatGLM4)
126+
.model("glm-4.6")
127127
.messages(Arrays.asList(
128128
ChatMessage.builder()
129129
.role(ChatMessageRole.USER.value())
@@ -151,7 +151,7 @@ if (response.isSuccess()) {
151151
```java
152152
// 创建流式请求
153153
ChatCompletionCreateParams streamRequest = ChatCompletionCreateParams.builder()
154-
.model(Constants.ModelChatGLM4)
154+
.model("glm-4.6")
155155
.messages(Arrays.asList(
156156
ChatMessage.builder()
157157
.role(ChatMessageRole.USER.value())
@@ -282,7 +282,7 @@ public class AIController {
282282
@PostMapping("/chat")
283283
public ResponseEntity<String> chat(@RequestBody ChatRequest request) {
284284
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
285-
.model(Constants.ModelChatGLM4)
285+
.model("glm-4.6")
286286
.messages(Arrays.asList(
287287
ChatMessage.builder()
288288
.role(ChatMessageRole.USER.value())

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import ai.z.openapi.service.assistant.AssistantServiceImpl;
2828
import ai.z.openapi.service.voiceclone.VoiceCloneService;
2929
import ai.z.openapi.service.voiceclone.VoiceCloneServiceImpl;
30+
import ai.z.openapi.service.moderations.ModerationService;
31+
import ai.z.openapi.service.moderations.ModerationServiceImpl;
3032
import ai.z.openapi.core.config.ZaiConfig;
3133
import ai.z.openapi.core.model.BiFlowableClientResponse;
3234
import ai.z.openapi.core.model.ClientRequest;
@@ -109,6 +111,9 @@ public abstract class AbstractAiClient extends AbstractClientBaseService {
109111
/** FileParsing service for fileParsing operations */
110112
private FileParsingService fileParsingService;
111113

114+
/** Moderation service for content safety detection */
115+
private ModerationService moderationService;
116+
112117
/**
113118
* Constructs a new AbstractAiClient with the specified configuration.
114119
* @param config the configuration object containing API keys, timeouts, and other
@@ -273,6 +278,18 @@ public synchronized FileParsingService fileParsing() {
273278
return fileParsingService;
274279
}
275280

281+
/**
282+
* Returns the moderation service for content safety detection. This service handles
283+
* content moderation for text, image, video, and audio inputs.
284+
* @return the ModerationService instance (lazily initialized)
285+
*/
286+
public synchronized ModerationService moderations() {
287+
if (moderationService == null) {
288+
this.moderationService = new ModerationServiceImpl(this);
289+
}
290+
return moderationService;
291+
}
292+
276293
// ==================== Utility Methods ====================
277294

278295
/**

core/src/main/java/ai/z/openapi/api/audio/AudioApi.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ public interface AudioApi {
3939
@POST("audio/speech")
4040
Single<ResponseBody> audioSpeech(@Body AudioSpeechRequest request);
4141

42+
/**
43+
* Text-to-Speech (TTS) conversion using GLM-4-Voice Converts text input into
44+
* natural-sounding speech audio with emotion and tone control Supports multiple
45+
* voices, languages, speed adjustment, and various audio formats Features advanced
46+
* voice synthesis with customizable emotional expressions and dialects
47+
* @param request TTS parameters including text, voice selection, emotion, speed,
48+
* tone, and output format
49+
* @return Generated high-quality audio streaming in specified format with natural
50+
* prosody
51+
*/
52+
@Streaming
53+
@POST("audio/speech")
54+
Call<ResponseBody> audioSpeechStreaming(@Body AudioSpeechRequest request);
55+
4256
/**
4357
* Voice cloning and customization using advanced neural models Creates custom voice
4458
* models from provided audio samples with high fidelity Enables personalized speech
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ai.z.openapi.api.moderations;
2+
3+
import ai.z.openapi.service.moderations.ModerationCreateParams;
4+
import ai.z.openapi.service.moderations.ModerationResult;
5+
import io.reactivex.rxjava3.core.Single;
6+
import retrofit2.http.Body;
7+
import retrofit2.http.POST;
8+
9+
/**
10+
* Moderation API for content safety detection Provides content moderation capabilities
11+
* for text, image, audio, and video formats Accurately identifies risky content including
12+
* adult content, violence, illegal content, etc. Returns structured moderation results
13+
* including content type, risk type, and specific risk segments
14+
*/
15+
public interface ModerationApi {
16+
17+
/**
18+
* Create a content moderation request for safety detection Analyzes content for
19+
* potential risks including adult content, violence, illegal activities Supports
20+
* multiple content types: text strings, images, audio, and video files Returns
21+
* detailed risk assessment with structured results and specific risk segments
22+
* @param request Moderation parameters including content to analyze (text string or
23+
* multimedia object) Text: maximum 2000 characters Images: less than 10M, minimum
24+
* resolution 20x20, maximum 6000x6000 Video: recommended duration 30 seconds Audio:
25+
* recommended duration 60 seconds
26+
* @return Moderation response with risk level (PASS/REVIEW/REJECT), content type,
27+
* risk types, and processing time information
28+
*/
29+
@POST("moderations")
30+
Single<ModerationResult> createModeration(@Body ModerationCreateParams request);
31+
32+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ private Constants() {
3535
// Text Generation Models
3636
// =============================================================================
3737

38+
/**
39+
* GLM-4.6 model code
40+
*/
41+
public static final String ModelGLM4_6 = "glm-4.6";
42+
43+
/**
44+
* GLM-4.6-air model code
45+
*/
46+
public static final String ModelGLM4_6_AIR = "glm-4.6-air";
47+
48+
/**
49+
* GLM-4.6-flash model code
50+
*/
51+
public static final String ModelGLM4_6_FLASH = "glm-4.6-flash";
52+
3853
/**
3954
* GLM-4.5 model code
4055
*/

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

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ public class ZaiConfig {
5454
*/
5555
private String apiKey;
5656

57-
/**
58-
* API id component.
59-
*/
60-
private String apiId;
61-
62-
/**
63-
* API secret component.
64-
*/
65-
private String apiSecret;
66-
6757
/**
6858
* Custom Http Request Headers
6959
*/
@@ -84,7 +74,8 @@ public class ZaiConfig {
8474
/**
8575
* Flag to disable token caching.
8676
*/
87-
private boolean disableTokenCache;
77+
@Builder.Default
78+
private boolean disableTokenCache = true;
8879

8980
/**
9081
* Maximum number of idle connections in the connection pool.
@@ -96,7 +87,7 @@ public class ZaiConfig {
9687
* Keep alive duration for connections in the pool (in seconds).
9788
*/
9889
@Builder.Default
99-
private long connectionPoolKeepAliveDuration = 1;
90+
private long connectionPoolKeepAliveDuration = 10;
10091

10192
/**
10293
* Time unit for connection pool keep alive duration.
@@ -149,10 +140,8 @@ public ZaiConfig(String apiKey) {
149140
this.apiKey = apiKey;
150141
String[] arrStr = apiKey.split("\\.");
151142
if (arrStr.length != 2) {
152-
throw new RuntimeException("invalid apiSecretKey");
143+
throw new RuntimeException("invalid api Key");
153144
}
154-
this.apiId = arrStr[0];
155-
this.apiSecret = arrStr[1];
156145
}
157146

158147
/**
@@ -166,8 +155,6 @@ public void setApiKey(String apiKey) {
166155
if (arrStr.length != 2) {
167156
throw new RuntimeException("invalid api Key");
168157
}
169-
this.apiId = arrStr[0];
170-
this.apiSecret = arrStr[1];
171158
}
172159

173160
/**
@@ -194,38 +181,10 @@ public String getApiKey() {
194181
if (value != null && !value.isEmpty()) {
195182
// Parse value and set components
196183
this.apiKey = value;
197-
String[] arrStr = value.split("\\.");
198-
if (arrStr.length == 2) {
199-
this.apiId = arrStr[0];
200-
this.apiSecret = arrStr[1];
201-
}
202-
return value;
203184
}
204185
return apiKey;
205186
}
206187

207-
/**
208-
* Gets API key with system property and environment variable fallback.
209-
*/
210-
public String getApiId() {
211-
if (apiId != null && !apiId.isEmpty()) {
212-
return apiId;
213-
}
214-
getApiKey();
215-
return apiId;
216-
}
217-
218-
/**
219-
* Gets API secret with system property and environment variable fallback.
220-
*/
221-
public String getApiSecret() {
222-
if (apiSecret != null && !apiSecret.isEmpty()) {
223-
return apiSecret;
224-
}
225-
getApiKey();
226-
return apiSecret;
227-
}
228-
229188
/**
230189
* Gets expire millis with system property and environment variable fallback.
231190
*/

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.5")
43+
.header("Zai-SDK-Ver", "0.0.6")
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/core/token/TokenManager.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ public TokenManager(ICache cache) {
4444
* @return valid JWT token
4545
*/
4646
public String getToken(ZaiConfig config) {
47-
String tokenCacheKey = genTokenCacheKey(config.getApiId());
47+
String[] arrStr = config.getApiKey().split("\\.");
48+
if (arrStr.length != 2) {
49+
throw new RuntimeException("invalid api Key");
50+
}
51+
String appId = arrStr[0];
52+
String tokenCacheKey = genTokenCacheKey(appId);
4853
String cacheToken = cache.get(tokenCacheKey);
4954
if (StringUtils.isNotEmpty(cacheToken)) {
5055
return cacheToken;
@@ -62,9 +67,15 @@ public String getToken(ZaiConfig config) {
6267
private static String createJwt(ZaiConfig config) {
6368
Algorithm alg;
6469
String algId = config.getAlg();
70+
String[] arrStr = config.getApiKey().split("\\.");
71+
if (arrStr.length != 2) {
72+
throw new RuntimeException("invalid api Key");
73+
}
74+
String appId = arrStr[0];
75+
String apiSecret = arrStr[1];
6576
if ("HS256".equals(algId)) {
6677
try {
67-
alg = Algorithm.HMAC256(config.getApiSecret().getBytes(StandardCharsets.UTF_8));
78+
alg = Algorithm.HMAC256(apiSecret.getBytes(StandardCharsets.UTF_8));
6879
}
6980
catch (Exception e) {
7081
logger.error("Failed to create HMAC256 algorithm", e);
@@ -79,7 +90,7 @@ private static String createJwt(ZaiConfig config) {
7990

8091
Map<String, Object> payload = new HashMap<>();
8192
// here the api_key is the apiId
82-
payload.put("api_key", config.getApiId());
93+
payload.put("api_key", appId);
8394
payload.put("exp", System.currentTimeMillis() + config.getExpireMillis() + DELAY_EXPIRE_TIME);
8495
payload.put("timestamp", Calendar.getInstance().getTimeInMillis());
8596
Map<String, Object> headerClaims = new HashMap<>();

core/src/main/java/ai/z/openapi/service/audio/AudioService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ public interface AudioService {
1212
*/
1313
AudioSpeechResponse createSpeech(AudioSpeechRequest request);
1414

15+
/**
16+
* Creates speech from text using text-to-speech.
17+
* @param request the speech generation request
18+
* @return AudioSpeechStreamingResponse containing the generated speech streaming
19+
*/
20+
AudioSpeechStreamingResponse createStreamingSpeechStreaming(AudioSpeechRequest request);
21+
1522
/**
1623
* Creates customized speech with specific voice characteristics.
1724
* @param request the speech customization request

0 commit comments

Comments
 (0)