forked from openai/openai-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructuredOutputsExample.java
More file actions
41 lines (36 loc) · 1.77 KB
/
Copy pathStructuredOutputsExample.java
File metadata and controls
41 lines (36 loc) · 1.77 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
package com.openai.example;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.JsonValue;
import com.openai.models.*;
import com.openai.models.ResponseFormatJsonSchema.JsonSchema;
import java.util.Map;
public final class StructuredOutputsExample {
private StructuredOutputsExample() {}
public static void main(String[] args) {
// Configures using one of:
// - The `OPENAI_API_KEY` environment variable
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
// TODO: Update this once we support extracting JSON schemas from Java classes
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).choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println);
}
}