Skip to content

Commit f422602

Browse files
authored
chore: add custom timeout and audio speech samples (#53)
1 parent ea87069 commit f422602

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ai.z.openapi.samples;
2+
3+
import ai.z.openapi.ZhipuAiClient;
4+
import ai.z.openapi.core.Constants;
5+
import ai.z.openapi.service.audio.AudioSpeechRequest;
6+
import ai.z.openapi.service.audio.AudioSpeechResponse;
7+
8+
/**
9+
* Audio Speech Example
10+
* Demonstrates how to use ZaiClient for audio speech
11+
*/
12+
public class AudioSpeechExample {
13+
14+
public static void main(String[] args) {
15+
// Create client, recommended to set API Key via environment variable
16+
// export ZAI_API_KEY=your.api_key
17+
// for Z.ai use the `ZaiClient`, for Zhipu AI use the ZhipuAiClient
18+
ZhipuAiClient client = ZhipuAiClient.builder().build();
19+
20+
// Create request
21+
AudioSpeechRequest request = AudioSpeechRequest.builder()
22+
.model(Constants.ModelTTS)
23+
.input("Hello, this is a test for text-to-speech functionality.")
24+
.voice("tongtong")
25+
.build();
26+
27+
try {
28+
// Execute request
29+
AudioSpeechResponse response = client.audio().createSpeech(request);
30+
31+
if (response.isSuccess()) {
32+
System.out.println("Response: " + response.getData());
33+
} else {
34+
System.err.println("Error: " + response.getMsg());
35+
}
36+
} catch (Exception e) {
37+
System.err.println("Exception occurred: " + e.getMessage());
38+
e.printStackTrace();
39+
}
40+
}
41+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ai.z.openapi.samples;
2+
3+
import ai.z.openapi.ZhipuAiClient;
4+
import ai.z.openapi.core.Constants;
5+
import ai.z.openapi.service.model.ChatCompletionCreateParams;
6+
import ai.z.openapi.service.model.ChatCompletionResponse;
7+
import ai.z.openapi.service.model.ChatMessage;
8+
import ai.z.openapi.service.model.ChatMessageRole;
9+
import ai.z.openapi.service.model.ChatThinking;
10+
import ai.z.openapi.service.model.ChatThinkingType;
11+
import ai.z.openapi.service.model.ResponseFormat;
12+
import ai.z.openapi.service.model.ResponseFormatType;
13+
14+
import java.util.Arrays;
15+
import java.util.concurrent.TimeUnit;
16+
17+
/**
18+
* Chat Completion Example
19+
* Demonstrates how to use ZaiClient for basic chat conversations
20+
*/
21+
public class CustomTimeoutExample {
22+
23+
public static void main(String[] args) {
24+
// Create client, recommended to set API Key via environment variable
25+
// export ZAI_API_KEY=your.api_key
26+
// for Z.ai use the `ZaiClient`, for Zhipu AI use the ZhipuAiClient
27+
ZhipuAiClient client = ZhipuAiClient.builder()
28+
.networkConfig(30, 10, 30, 30, TimeUnit.SECONDS)
29+
.build();
30+
31+
// Create chat request
32+
ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
33+
.model(Constants.ModelChatGLM4_5)
34+
.messages(Arrays.asList(
35+
ChatMessage.builder()
36+
.role(ChatMessageRole.USER.value())
37+
.content("Hello, how to learn english?")
38+
.build()
39+
))
40+
.stream(false)
41+
.thinking(ChatThinking.builder().type(ChatThinkingType.ENABLED.value()).build())
42+
.responseFormat(ResponseFormat.builder().type(ResponseFormatType.TEXT.value()).build())
43+
.temperature(0.7f)
44+
.maxTokens(1024)
45+
.build();
46+
47+
try {
48+
// Execute request
49+
ChatCompletionResponse response = client.chat().createChatCompletion(request);
50+
51+
if (response.isSuccess()) {
52+
Object content = response.getData().getChoices().get(0).getMessage();
53+
System.out.println("Response: " + content);
54+
} else {
55+
System.err.println("Error: " + response.getMsg());
56+
}
57+
} catch (Exception e) {
58+
System.err.println("Exception occurred: " + e.getMessage());
59+
e.printStackTrace();
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)