From 9993e205fa594d647506c71b6cb8a8b1c20ff92d Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Thu, 21 Aug 2025 14:43:14 +0800 Subject: [PATCH] chore: add glm4.5v code and sample (#41) --- .github/workflows/lint-pr.yaml | 5 +- .../java/ai/z/openapi/core/Constants.java | 5 ++ .../ai.z.openapi.samples/GLM45VExample.java | 59 +++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 samples/src/main/ai.z.openapi.samples/GLM45VExample.java diff --git a/.github/workflows/lint-pr.yaml b/.github/workflows/lint-pr.yaml index 1b8a44e..6dd7f6c 100644 --- a/.github/workflows/lint-pr.yaml +++ b/.github/workflows/lint-pr.yaml @@ -2,10 +2,7 @@ name: "Lint PR" on: pull_request_target: - types: - - opened - - edited - - reopened + pull_request: jobs: lint-pr: diff --git a/core/src/main/java/ai/z/openapi/core/Constants.java b/core/src/main/java/ai/z/openapi/core/Constants.java index 886a103..ed8d492 100644 --- a/core/src/main/java/ai/z/openapi/core/Constants.java +++ b/core/src/main/java/ai/z/openapi/core/Constants.java @@ -40,6 +40,11 @@ public final class Constants { */ public static final String ModelChatGLM4_5 = "glm-4.5"; + /** + * GLM-4.5V model code + */ + public static final String ModelChatGLM4_5V = "glm-4.5v"; + /** * GLM-4.5-Air model code */ diff --git a/samples/src/main/ai.z.openapi.samples/GLM45VExample.java b/samples/src/main/ai.z.openapi.samples/GLM45VExample.java new file mode 100644 index 0000000..8e19481 --- /dev/null +++ b/samples/src/main/ai.z.openapi.samples/GLM45VExample.java @@ -0,0 +1,59 @@ +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.ImageUrl; +import ai.z.openapi.service.model.MessageContent; + +import java.io.IOException; +import java.util.Arrays; + +/** + * Streaming Chat Example + * Demonstrates how to use ZaiClient for streaming chat conversations + */ +public class GLM45VExample { + + 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(); + + // Create chat request + ChatCompletionCreateParams streamRequest = ChatCompletionCreateParams.builder() + .model(Constants.ModelChatGLM4_5V) + .messages(Arrays.asList( + ChatMessage.builder() + .role(ChatMessageRole.USER.value()) + .content(Arrays.asList( + MessageContent.builder() + .type("image_url") + .imageUrl(ImageUrl.builder() + .url("https://cdn.bigmodel.cn/static/logo/register.png") + .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()); + } + } +} \ No newline at end of file