-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathAgentVideoExample.java
More file actions
72 lines (62 loc) · 2.88 KB
/
Copy pathAgentVideoExample.java
File metadata and controls
72 lines (62 loc) · 2.88 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
68
69
70
71
72
package ai.z.openapi.samples;
import ai.z.openapi.ZaiClient;
import ai.z.openapi.service.agents.AgentContent;
import ai.z.openapi.service.agents.AgentMessage;
import ai.z.openapi.service.agents.AgentsCompletionRequest;
import ai.z.openapi.service.model.ChatCompletionResponse;
import ai.z.openapi.service.model.ChatMessageRole;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Agent Example
* Demonstrates how to use ZaiClient for agent-based completions
*/
public class AgentVideoExample {
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.builder().ofZHIPU().build()
ZaiClient client = ZaiClient.builder().ofZAI().build();
syncAgentCompletion(client);
}
/**
* Example of synchronous agent completion
*/
private static void syncAgentCompletion(ZaiClient client) {
System.out.println("\n=== Synchronous Agent Completion Example ===");
// Create messages for the agent
List<AgentMessage> messages = new ArrayList<>();
AgentMessage userMessage = new AgentMessage(
ChatMessageRole.USER.value(),
Arrays.asList(AgentContent.ofText("The two figures in the painting gradually approach each other, then kissing passionately, alternating deep and determined intensity")
, AgentContent.ofImageUrl("https://img-repo-intl.imdr.cn/dr/sample-182141/xoJteuReBtNCdoMoH.jpg!w1080.jpg"))
);
messages.add(userMessage);
// Create agent completion request
AgentsCompletionRequest request = AgentsCompletionRequest.builder()
.agentId("vidu_template_agent") // Using translation agent
.stream(false) // Non-streaming mode
.messages(messages)
.customVariables(JsonNodeFactory.instance.objectNode().put("template", "french_kiss"))
.requestId("agent-example-" + System.currentTimeMillis())
.build();
try {
// Execute request
ChatCompletionResponse response = client.agents().createAgentCompletion(request);
if (response.isSuccess()) {
System.out.println("Agent completion successful!");
System.out.println("\nResponse: " + new ObjectMapper().writeValueAsString(response.getData()));
} else {
System.err.println("Error: " + response.getMsg());
}
} catch (Exception e) {
System.err.println("Exception occurred: " + e.getMessage());
e.printStackTrace();
} finally {
client.close();
}
}
}