-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNovaPAIExample.java
More file actions
140 lines (124 loc) · 6.48 KB
/
NovaPAIExample.java
File metadata and controls
140 lines (124 loc) · 6.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// NovaPAI Java SDK Example
// Maven: add openai-java dependency
// Docs: https://novapai.ai
// pom.xml dependency:
// <dependency>
// <groupId>com.openai</groupId>
// <artifactId>openai-java</artifactId>
// <version>2.4.0</version>
// </dependency>
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.*;
import java.util.List;
import java.util.Map;
public class NovaPAIExample {
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.builder()
.apiKey("your-api-key")
.baseUrl("https://api.novapai.ai/router/v1")
.build();
// ── Basic Chat ──────────────────────────────────────
basicChat(client);
// ── Streaming ───────────────────────────────────────
streamChat(client);
// ── Function Calling ────────────────────────────────
functionCalling(client);
// ── JSON Mode ───────────────────────────────────────
jsonMode(client);
}
static void basicChat(OpenAIClient client) {
ChatCompletion response = client.chat().completions().create(
ChatCompletionCreateParams.builder()
.model("deepseek-v4-pro")
.messages(List.of(
ChatCompletionMessageParam.ofSystem(
ChatCompletionSystemMessageParam.builder()
.content("You are a helpful assistant.")
.build()
),
ChatCompletionMessageParam.ofUser(
ChatCompletionUserMessageParam.builder()
.content("Hello!")
.build()
)
))
.build()
);
System.out.println(response.choices().get(0).message().content().orElse(""));
}
static void streamChat(OpenAIClient client) {
client.chat().completions().createStreaming(
ChatCompletionCreateParams.builder()
.model("deepseek-v4-pro")
.messages(List.of(
ChatCompletionMessageParam.ofUser(
ChatCompletionUserMessageParam.builder()
.content("Tell me a joke")
.build()
)
))
.build()
).stream().forEach(chunk ->
chunk.choices().stream()
.map(c -> c.delta().content().orElse(""))
.forEach(System.out::print)
);
System.out.println();
}
static void functionCalling(OpenAIClient client) {
ChatCompletion response = client.chat().completions().create(
ChatCompletionCreateParams.builder()
.model("deepseek-v4-pro")
.messages(List.of(
ChatCompletionMessageParam.ofUser(
ChatCompletionUserMessageParam.builder()
.content("What's the weather in Tokyo?")
.build()
)
))
.tools(List.of(
ChatCompletionTool.builder()
.function(
FunctionDefinition.builder()
.name("get_weather")
.description("Get current weather for a city")
.parameters(Map.of(
"type", "object",
"properties", Map.of(
"city", Map.of("type", "string", "description", "City name")
),
"required", List.of("city")
))
.build()
)
.build()
))
.build()
);
ChatCompletionMessageToolCall tc = response.choices().get(0).message().toolCalls().get().get(0);
System.out.println("Function: " + tc.function().name());
System.out.println("Args: " + tc.function().arguments());
}
static void jsonMode(OpenAIClient client) {
ChatCompletion response = client.chat().completions().create(
ChatCompletionCreateParams.builder()
.model("deepseek-v4-pro")
.messages(List.of(
ChatCompletionMessageParam.ofSystem(
ChatCompletionSystemMessageParam.builder()
.content("Extract company info as JSON.")
.build()
),
ChatCompletionMessageParam.ofUser(
ChatCompletionUserMessageParam.builder()
.content("Apple Inc. is based in Cupertino, founded in 1976.")
.build()
)
))
.responseFormat(ChatCompletionResponseFormat.ofJsonObject())
.build()
);
System.out.println(response.choices().get(0).message().content().orElse(""));
}
}