forked from openai/openai-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructuredOutputsRawAsyncExample.java
More file actions
46 lines (41 loc) · 1.96 KB
/
Copy pathStructuredOutputsRawAsyncExample.java
File metadata and controls
46 lines (41 loc) · 1.96 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
package com.openai.example;
import com.openai.client.OpenAIClientAsync;
import com.openai.client.okhttp.OpenAIOkHttpClientAsync;
import com.openai.core.JsonValue;
import com.openai.models.ChatModel;
import com.openai.models.ResponseFormatJsonSchema;
import com.openai.models.ResponseFormatJsonSchema.JsonSchema;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import java.util.Map;
public final class StructuredOutputsRawAsyncExample {
private StructuredOutputsRawAsyncExample() {}
public static void main(String[] args) {
// Configures using one of:
// - The `OPENAI_API_KEY` environment variable
// - The `OPENAI_BASE_URL` and `AZURE_OPENAI_KEY` environment variables
OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv();
JsonSchema.Schema schema = JsonSchema.Schema.builder()
.putAdditionalProperty("type", JsonValue.from("object"))
.putAdditionalProperty(
"properties", JsonValue.from(Map.of("employees", Map.of("items", Map.of("type", "string")))))
.build();
ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
.model(ChatModel.GPT_4O_MINI)
.maxCompletionTokens(2048)
.responseFormat(ResponseFormatJsonSchema.builder()
.jsonSchema(JsonSchema.builder()
.name("employee-list")
.schema(schema)
.build())
.build())
.addUserMessage("Who works at OpenAI?")
.build();
client.chat()
.completions()
.create(createParams)
.thenAccept(completion -> completion.choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println))
.join();
}
}