chore: add cogvideo glmthinking example (#18)
This commit is contained in:
parent
86d68b14c5
commit
0ace1cd7de
9 changed files with 293 additions and 50 deletions
|
|
@ -40,7 +40,7 @@ public class VideoCreateParams implements ClientRequest<VideoCreateParams> {
|
|||
* Image size:
|
||||
*/
|
||||
@JsonProperty("image_url")
|
||||
private String imageUrl;
|
||||
private Object imageUrl;
|
||||
|
||||
/**
|
||||
* Call specified model to optimize the prompt, recommend using GLM-4-Air and
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ public class ClientConfigurationExample {
|
|||
.apiKey("your.api.key.your.api.secret")
|
||||
.baseUrl("https://api.z.ai/api/paas/v4/")
|
||||
.enableTokenCache()
|
||||
.tokenExpire(3600000) // 1小时
|
||||
.tokenExpire(3600000) // 1 hour
|
||||
.connectionPool(10, 5, TimeUnit.MINUTES)
|
||||
.build();
|
||||
System.out.println("✓ Advanced client created successfully");
|
||||
|
|
|
|||
174
samples/src/main/ai.z.openapi.samples/CogVideoX3Example.java
Normal file
174
samples/src/main/ai.z.openapi.samples/CogVideoX3Example.java
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
package ai.z.openapi.samples;
|
||||
|
||||
import ai.z.openapi.ZaiClient;
|
||||
import ai.z.openapi.service.videos.VideoCreateParams;
|
||||
import ai.z.openapi.service.videos.VideoObject;
|
||||
import ai.z.openapi.service.videos.VideosResponse;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* CogVideoX-3 Example
|
||||
* Demonstrates how to use ZaiClient for advanced video generation with CogVideoX-3
|
||||
* Features: Text-to-video, Image-to-video, First-last frame generation
|
||||
*/
|
||||
public class CogVideoX3Example {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Create client, recommended to set API Key via environment variable
|
||||
// export ZAI_API_KEY=your.api.key
|
||||
ZaiClient client = ZaiClient.builder().ofZHIPU().build();
|
||||
|
||||
// Or set API Key via code
|
||||
// ZaiClient client = ZaiClient.builder().ofZHIPU()
|
||||
// .apiKey("your_api_key")
|
||||
// .build();
|
||||
|
||||
// Video generation examples
|
||||
textToVideoExample(client);
|
||||
imageToVideoExample(client);
|
||||
firstLastFrameVideoExample(client);
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of text-to-video generation using CogVideoX-3
|
||||
*/
|
||||
private static void textToVideoExample(ZaiClient client) {
|
||||
System.out.println("=== CogVideoX-3 Text-to-Video Generation Example ===");
|
||||
|
||||
try {
|
||||
VideoCreateParams request = VideoCreateParams.builder()
|
||||
.model("cogvideox-3")
|
||||
.prompt("A cute kitten chasing butterflies in a garden, bright sunshine, blooming flowers, clear and stable picture")
|
||||
.quality("quality") // "quality" for quality priority, "speed" for speed priority
|
||||
.withAudio(true)
|
||||
.size("1920x1080") // Video resolution, supports up to 4K
|
||||
.fps(30) // Frame rate, can be 30 or 60
|
||||
.build();
|
||||
|
||||
VideosResponse response = client.videos().videoGenerations(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
String taskId = response.getData().getId();
|
||||
System.out.println("Video generation task submitted, Task ID: " + taskId);
|
||||
System.out.println("Please wait for video generation to complete...");
|
||||
|
||||
// Wait and check result
|
||||
Thread.sleep(60000); // Wait 1 minute
|
||||
checkVideoResult(client, taskId);
|
||||
} else {
|
||||
System.err.println("Error: " + response.getMsg());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Exception occurred: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of image-to-video generation using CogVideoX-3
|
||||
*/
|
||||
private static void imageToVideoExample(ZaiClient client) {
|
||||
System.out.println("\n=== CogVideoX-3 Image-to-Video Generation Example ===");
|
||||
|
||||
try {
|
||||
VideoCreateParams request = VideoCreateParams.builder()
|
||||
.model("cogvideox-3")
|
||||
.imageUrl("https://img.iplaysoft.com/wp-content/uploads/2019/free-images/free_stock_photo.jpg")
|
||||
.prompt("Make the scene come alive, showing natural dynamic effects")
|
||||
.quality("quality")
|
||||
.withAudio(true)
|
||||
.size("1920x1080")
|
||||
.fps(30)
|
||||
.build();
|
||||
|
||||
VideosResponse response = client.videos().videoGenerations(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
String taskId = response.getData().getId();
|
||||
System.out.println("Image-to-video task submitted, Task ID: " + taskId);
|
||||
System.out.println("Please wait for video generation to complete...");
|
||||
|
||||
// Wait and check result
|
||||
Thread.sleep(60000); // Wait 1 minute
|
||||
checkVideoResult(client, taskId);
|
||||
} else {
|
||||
System.err.println("Error: " + response.getMsg());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Exception occurred: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of first-last frame video generation using CogVideoX-3
|
||||
* This is a new feature in CogVideoX-3
|
||||
*/
|
||||
private static void firstLastFrameVideoExample(ZaiClient client) {
|
||||
System.out.println("\n=== CogVideoX-3 First-Last Frame Video Generation Example ===");
|
||||
|
||||
try {
|
||||
// Define first and last frame URLs
|
||||
String firstFrameUrl = "https://gd-hbimg.huaban.com/ccee58d77afe8f5e17a572246b1994f7e027657fe9e6-qD66In_fw1200webp";
|
||||
String lastFrameUrl = "https://gd-hbimg.huaban.com/cc2601d568a72d18d90b2cc7f1065b16b2d693f7fa3f7-hDAwNq_fw1200webp";
|
||||
|
||||
VideoCreateParams request = VideoCreateParams.builder()
|
||||
.model("cogvideox-3")
|
||||
.imageUrl(Arrays.asList(firstFrameUrl, lastFrameUrl)) // Pass first and last frame URLs
|
||||
.prompt("Dragon King transforms into Ao Bing, ink wash style rendering, main subject slowly transforms with rotating camera movement, smooth and natural transition")
|
||||
.quality("quality")
|
||||
.withAudio(true)
|
||||
.size("1920x1080")
|
||||
.fps(30)
|
||||
.build();
|
||||
|
||||
VideosResponse response = client.videos().videoGenerations(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
String taskId = response.getData().getId();
|
||||
System.out.println("First-last frame video generation task submitted, Task ID: " + taskId);
|
||||
System.out.println("Please wait for video generation to complete...");
|
||||
System.out.println("Note: First-last frame generation can create coherent transition videos, naturally connecting static frames into dynamic narratives.");
|
||||
|
||||
// Wait and check result
|
||||
Thread.sleep(60000); // Wait 1 minute
|
||||
checkVideoResult(client, taskId);
|
||||
} else {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Exception occurred: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check video generation result
|
||||
*/
|
||||
private static void checkVideoResult(ZaiClient client, String taskId) {
|
||||
try {
|
||||
VideosResponse response = client.videos().videoGenerationsResult(taskId);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
VideoObject result = response.getData();
|
||||
String status = result.getTaskStatus();
|
||||
|
||||
System.out.println("Task status: " + status);
|
||||
|
||||
if ("SUCCESS".equals(status)) {
|
||||
System.out.println("Video generation successful!");
|
||||
if (result.getVideoResult() != null && !result.getVideoResult().isEmpty()) {
|
||||
System.out.println("Video URL: " + result.getVideoResult().get(0).getUrl());
|
||||
}
|
||||
} else if ("PROCESSING".equals(status)) {
|
||||
System.out.println("Video is still being generated, please check again later...");
|
||||
}
|
||||
} else {
|
||||
System.err.println("Query result error: " + response.getMsg());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Exception occurred while querying video result: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -41,15 +41,7 @@ public class CogVideoXExample {
|
|||
try {
|
||||
// Execute request
|
||||
VideosResponse response = client.videos().videoGenerations(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
System.out.println("Video generation request successful!");
|
||||
System.out.println("Task ID: " + response.getData().getId());
|
||||
System.out.println("\nNote: Video generation is an asynchronous process.");
|
||||
System.out.println("Use the Task ID to check the status and retrieve the result later.");
|
||||
} else {
|
||||
System.err.println("Error: " + response.getMsg());
|
||||
}
|
||||
System.out.println("Response: " + response.getData());
|
||||
} catch (Exception e) {
|
||||
System.err.println("Exception occurred: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
|
|
@ -70,16 +62,13 @@ public class CogVideoXExample {
|
|||
System.out.println("Note: In a real application, replace 'your-task-id-here' with an actual task ID.");
|
||||
|
||||
try {
|
||||
// Skip the actual API call in this example to avoid errors with a fake task ID
|
||||
if (!taskId.equals("your-task-id-here")) {
|
||||
// Execute request to check result
|
||||
VideosResponse response = client.videos().videoGenerationsResult(taskId);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
System.out.println("Video generation: " + response.getData());
|
||||
} else {
|
||||
System.err.println("Error: " + response.getMsg());
|
||||
}
|
||||
// Execute request to check result
|
||||
VideosResponse response = client.videos().videoGenerationsResult(taskId);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
System.out.println("Video generation: " + response.getData());
|
||||
} else {
|
||||
System.err.println("Error: " + response.getMsg());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Exception occurred: " + e.getMessage());
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ public class EmbeddingsExample {
|
|||
// Create embedding request
|
||||
EmbeddingCreateParams request = EmbeddingCreateParams.builder()
|
||||
.model(Constants.ModelEmbedding3)
|
||||
.input(Arrays.asList("Hello world", "How are you?", "今天天气怎么样?"))
|
||||
.input(Arrays.asList("Hello world", "How are you?", "How is the weather today?"))
|
||||
.build();
|
||||
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
package ai.z.openapi.samples;
|
||||
|
||||
import ai.z.openapi.ZaiClient;
|
||||
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.ImageUrl;
|
||||
import ai.z.openapi.service.model.MessageContent;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class GLM41VThinkingExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String apiKey = ""; // Please fill in your own API Key
|
||||
ZaiClient client = ZaiClient.builder().ofZHIPU()
|
||||
.apiKey(apiKey)
|
||||
.build();
|
||||
|
||||
ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
|
||||
.model("glm-4.1v-thinking-flashx")
|
||||
.messages(Arrays.asList(
|
||||
ChatMessage.builder()
|
||||
.role(ChatMessageRole.USER.value())
|
||||
.content(Arrays.asList(
|
||||
MessageContent.builder()
|
||||
.type("text")
|
||||
.text("Describe this image")
|
||||
.build(),
|
||||
MessageContent.builder()
|
||||
.type("image_url")
|
||||
.imageUrl(ImageUrl.builder()
|
||||
.url("https://aigc-files.bigmodel.cn/api/cogview/20250723213827da171a419b9b4906_0.png")
|
||||
.build())
|
||||
.build()))
|
||||
.build()
|
||||
))
|
||||
.build();
|
||||
|
||||
ChatCompletionResponse response = client.chat().createChatCompletion(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
Object reply = response.getData().getChoices().get(0).getMessage().getContent();
|
||||
System.out.println(reply);
|
||||
} else {
|
||||
System.err.println("Error: " + response.getMsg());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
50
samples/src/main/ai.z.openapi.samples/GLM4VPlusExample.java
Normal file
50
samples/src/main/ai.z.openapi.samples/GLM4VPlusExample.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package ai.z.openapi.samples;
|
||||
|
||||
import ai.z.openapi.ZaiClient;
|
||||
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.ImageUrl;
|
||||
import ai.z.openapi.service.model.MessageContent;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class GLM4VPlusExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String apiKey = ""; // Please fill in your own API Key
|
||||
ZaiClient client = ZaiClient.builder().ofZHIPU()
|
||||
.apiKey(apiKey)
|
||||
.build();
|
||||
|
||||
ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
|
||||
.model("glm-4v-plus-0111")
|
||||
.messages(Arrays.asList(
|
||||
ChatMessage.builder()
|
||||
.role(ChatMessageRole.USER.value())
|
||||
.content(Arrays.asList(
|
||||
MessageContent.builder()
|
||||
.type("text")
|
||||
.text("What is in this image?")
|
||||
.build(),
|
||||
MessageContent.builder()
|
||||
.type("image_url")
|
||||
.imageUrl(ImageUrl.builder()
|
||||
.url("https://aigc-files.bigmodel.cn/api/cogview/20250723213827da171a419b9b4906_0.png")
|
||||
.build())
|
||||
.build()))
|
||||
.build()
|
||||
))
|
||||
.build();
|
||||
|
||||
ChatCompletionResponse response = client.chat().createChatCompletion(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
Object reply = response.getData().getChoices().get(0).getMessage().getContent();
|
||||
System.out.println(reply);
|
||||
} else {
|
||||
System.err.println("Error: " + response.getMsg());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
package ai.z.openapi.samples;
|
||||
|
||||
import ai.z.openapi.ZaiClient;
|
||||
import ai.z.openapi.service.image.*;
|
||||
import ai.z.openapi.core.Constants;
|
||||
import ai.z.openapi.service.image.CreateImageRequest;
|
||||
import ai.z.openapi.service.image.ImageResponse;
|
||||
|
||||
/**
|
||||
* Image Generation Example
|
||||
|
|
@ -12,37 +13,15 @@ public class ImageGenerationExample {
|
|||
|
||||
public static void main(String[] args) {
|
||||
// Create client
|
||||
ZaiClient client = ZaiClient.builder().build();
|
||||
ZaiClient client = ZaiClient.builder().ofZHIPU().build();
|
||||
|
||||
// Create image generation request
|
||||
CreateImageRequest request = CreateImageRequest.builder()
|
||||
.model(Constants.ModelCogView3Plus)
|
||||
.model(Constants.ModelCogView4250304)
|
||||
.prompt("A beautiful sunset over mountains, digital art style")
|
||||
.size("1024x1024")
|
||||
.build();
|
||||
|
||||
try {
|
||||
// Execute request
|
||||
ImageResponse response = client.images().createImage(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
System.out.println("Successfully generated image:");
|
||||
System.out.println("Creation time: " + response.getData().getCreated());
|
||||
|
||||
response.getData().getData().forEach(image -> {
|
||||
System.out.println("\nImage URL: " + image.getUrl());
|
||||
if (image.getRevisedPrompt() != null) {
|
||||
System.out.println("Revised prompt: " + image.getRevisedPrompt());
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("\nTip: Please copy the URL above to your browser to view the generated image");
|
||||
} else {
|
||||
System.err.println("Error: " + response.getMsg());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Exception occurred: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
ImageResponse response = client.images().createImage(request);
|
||||
System.out.println(response.getData());
|
||||
}
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ public class StreamingChatExample {
|
|||
.content("Tell me a story")
|
||||
.build()
|
||||
))
|
||||
.stream(true) // 启用流式响应
|
||||
.stream(true) // Enable streaming response
|
||||
.build();
|
||||
|
||||
try {
|
||||
|
|
|
|||
Loading…
Reference in a new issue