-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCustomTimeoutExample.java
More file actions
62 lines (55 loc) · 2.3 KB
/
CustomTimeoutExample.java
File metadata and controls
62 lines (55 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package ai.z.openapi.samples;
import ai.z.openapi.ZhipuAiClient;
import ai.z.openapi.core.Constants;
import ai.z.openapi.service.model.ChatCompletionCreateParams;
import ai.z.openapi.service.model.ChatCompletionResponse;
import ai.z.openapi.service.model.ChatMessage;
import ai.z.openapi.service.model.ChatMessageRole;
import ai.z.openapi.service.model.ChatThinking;
import ai.z.openapi.service.model.ChatThinkingType;
import ai.z.openapi.service.model.ResponseFormat;
import ai.z.openapi.service.model.ResponseFormatType;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/**
* Chat Completion Example
* Demonstrates how to use ZaiClient for basic chat conversations
*/
public class CustomTimeoutExample {
public static void main(String[] args) {
// Create client, recommended to set API Key via environment variable
// export ZAI_API_KEY=your.api_key
// for Z.ai use the `ZaiClient`, for Zhipu AI use the ZhipuAiClient
ZhipuAiClient client = ZhipuAiClient.builder()
.networkConfig(0, 10, 30, 30, TimeUnit.SECONDS)
.build();
// Create chat request
ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
.model("glm-4.6")
.messages(Arrays.asList(
ChatMessage.builder()
.role(ChatMessageRole.USER.value())
.content("Hello, how to learn english?")
.build()
))
.stream(false)
.thinking(ChatThinking.builder().type(ChatThinkingType.ENABLED.value()).build())
.responseFormat(ResponseFormat.builder().type(ResponseFormatType.TEXT.value()).build())
.build();
try {
// Execute request
ChatCompletionResponse response = client.chat().createChatCompletion(request);
if (response.isSuccess()) {
Object content = response.getData().getChoices().get(0).getMessage();
System.out.println("Response: " + content);
} else {
System.err.println("Error: " + response.getMsg());
}
} catch (Exception e) {
System.err.println("Exception occurred: " + e.getMessage());
e.printStackTrace();
} finally {
client.close();
}
}
}