forked from openai/openai-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponsesStructuredOutputsStreamingExample.java
More file actions
87 lines (68 loc) · 3.03 KB
/
ResponsesStructuredOutputsStreamingExample.java
File metadata and controls
87 lines (68 loc) · 3.03 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
package com.openai.example;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.helpers.ResponseAccumulator;
import com.openai.models.ChatModel;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseStreamEvent;
import com.openai.models.responses.StructuredResponseCreateParams;
import java.util.List;
public final class ResponsesStructuredOutputsStreamingExample {
public static class Person {
@JsonPropertyDescription("The first name and surname of the person.")
public String name;
public int birthYear;
@JsonPropertyDescription("The year the person died, or 'present' if the person is living.")
public String deathYear;
@Override
public String toString() {
return name + " (" + birthYear + '-' + deathYear + ')';
}
}
public static class Book {
public String title;
public Person author;
@JsonPropertyDescription("The year in which the book was first published.")
public int publicationYear;
public String genre;
@JsonIgnore
public String isbn;
@Override
public String toString() {
return '"' + title + "\" (" + publicationYear + ") [" + genre + "] by " + author;
}
}
public static class BookList {
public List<Book> books;
}
private ResponsesStructuredOutputsStreamingExample() {}
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
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
StructuredResponseCreateParams<BookList> createParams = ResponseCreateParams.builder()
.input("List some famous late twentieth century novels.")
.text(BookList.class)
.model(ChatModel.GPT_4O)
.build();
ResponseAccumulator accumulator = ResponseAccumulator.create();
try (StreamResponse<ResponseStreamEvent> streamResponse =
client.responses().createStreaming(createParams)) {
streamResponse.stream()
.peek(accumulator::accumulate)
.flatMap(event -> event.outputTextDelta().stream())
.forEach(textEvent -> System.out.print(textEvent.delta()));
System.out.println();
}
accumulator.response(BookList.class).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.flatMap(bookList -> bookList.books.stream())
.forEach(book -> System.out.println(" - " + book));
}
}