-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathChatCompletionBase64Example.java
More file actions
67 lines (59 loc) · 2.48 KB
/
ChatCompletionBase64Example.java
File metadata and controls
67 lines (59 loc) · 2.48 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
63
64
65
66
67
package ai.z.openapi.samples;
import ai.z.openapi.ZhipuAiClient;
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.ImageUrl;
import ai.z.openapi.service.model.MessageContent;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Base64;
/**
* Streaming Chat Example
* Demonstrates how to use ZaiClient for streaming chat conversations
*/
public class ChatCompletionBase64Example {
public static void main(String[] args) throws IOException {
// Create client
// for Z.ai use the `ZaiClient`, for Zhipu AI use the ZhipuAiClient
ZhipuAiClient client = ZhipuAiClient.builder().build();
String file = ClassLoader.getSystemResource("grounding.png").getFile();
byte[] bytes = Files.readAllBytes(new File(file).toPath());
Base64.Encoder encoder = Base64.getEncoder();
String base64 = encoder.encodeToString(bytes);
// Create chat request
ChatCompletionCreateParams streamRequest = ChatCompletionCreateParams.builder()
.model("glm-4.6v")
.messages(Arrays.asList(
ChatMessage.builder()
.role(ChatMessageRole.USER.value())
.content(Arrays.asList(
MessageContent.builder()
.type("image_url")
.imageUrl(ImageUrl.builder()
.url(base64)
.build())
.build(),
MessageContent.builder()
.type("text")
.text("What are the pic talk about?")
.build()
))
.build()
))
.thinking(ChatThinking.builder().type("enabled").build())
.build();
ChatCompletionResponse response = client.chat().createChatCompletion(streamRequest);
if (response.isSuccess()) {
Object content = response.getData().getChoices().get(0).getMessage();
System.out.println("Response: " + content);
} else {
System.err.println("Error: " + response.getMsg());
}
client.close();
}
}