Skip to content

Commit 589ea32

Browse files
committed
docs: responses examples
1 parent 2d98165 commit 589ea32

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ This library requires Java 8 or later.
4848

4949
See the [`openai-java-example`](openai-java-example/src/main/java/com/openai/example) directory for complete and runnable examples.
5050

51+
The primary API for interacting with OpenAI models is the [Responses API](https://platform.openai.com/docs/api-reference/responses). You can generate text from the model with the code below.
52+
53+
```java
54+
import com.openai.client.OpenAIClient;
55+
import com.openai.client.okhttp.OpenAIOkHttpClient;
56+
import com.openai.models.ChatModel;
57+
import com.openai.models.responses.Response;
58+
import com.openai.models.responses.ResponseCreateParams;
59+
60+
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID` environment variables
61+
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
62+
63+
ResponseCreateParams params = ResponseCreateParams.builder()
64+
.input("Say this is a test")
65+
.model(ChatModel.GPT_4O)
66+
.build();
67+
Response response = client.responses().create(params);
68+
```
69+
70+
The previous standard (supported indefinitely) for generating text is the [Chat Completions API](https://platform.openai.com/docs/api-reference/chat). You can use that API to generate text from the model with the code below.
71+
5172
```java
5273
import com.openai.client.OpenAIClient;
5374
import com.openai.client.okhttp.OpenAIOkHttpClient;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.openai.example;
2+
3+
import com.openai.client.OpenAIClientAsync;
4+
import com.openai.client.okhttp.OpenAIOkHttpClientAsync;
5+
import com.openai.models.ChatModel;
6+
import com.openai.models.responses.ResponseCreateParams;
7+
8+
public final class ResponsesAsyncExample {
9+
private ResponsesAsyncExample() {}
10+
11+
public static void main(String[] args) {
12+
// Configures using one of:
13+
// - The `OPENAI_API_KEY` environment variable
14+
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables
15+
OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv();
16+
17+
ResponseCreateParams createParams = ResponseCreateParams.builder()
18+
.input("Tell me a story about building the best SDK!")
19+
.model(ChatModel.GPT_4O)
20+
.build();
21+
22+
client.responses()
23+
.create(createParams)
24+
.thenAccept(response -> response.output().stream()
25+
.flatMap(item -> item.message().stream())
26+
.flatMap(message -> message.content().stream())
27+
.flatMap(content -> content.outputText().stream())
28+
.forEach(outputText -> System.out.println(outputText.text())))
29+
.join();
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.openai.example;
2+
3+
import com.openai.client.OpenAIClient;
4+
import com.openai.client.okhttp.OpenAIOkHttpClient;
5+
import com.openai.models.ChatModel;
6+
import com.openai.models.responses.ResponseCreateParams;
7+
8+
public final class ResponsesExample {
9+
private ResponsesExample() {}
10+
11+
public static void main(String[] args) {
12+
// Configures using one of:
13+
// - The `OPENAI_API_KEY` environment variable
14+
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables
15+
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
16+
17+
ResponseCreateParams createParams = ResponseCreateParams.builder()
18+
.input("Tell me a story about building the best SDK!")
19+
.model(ChatModel.GPT_4O)
20+
.build();
21+
22+
client.responses().create(createParams).output().stream()
23+
.flatMap(item -> item.message().stream())
24+
.flatMap(message -> message.content().stream())
25+
.flatMap(content -> content.outputText().stream())
26+
.forEach(outputText -> System.out.println(outputText.text()));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.openai.example;
2+
3+
import com.openai.client.OpenAIClientAsync;
4+
import com.openai.client.okhttp.OpenAIOkHttpClientAsync;
5+
import com.openai.models.ChatModel;
6+
import com.openai.models.responses.ResponseCreateParams;
7+
8+
public final class ResponsesStreamingAsyncExample {
9+
private ResponsesStreamingAsyncExample() {}
10+
11+
public static void main(String[] args) {
12+
// Configures using one of:
13+
// - The `OPENAI_API_KEY` environment variable
14+
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables
15+
OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv();
16+
17+
ResponseCreateParams createParams = ResponseCreateParams.builder()
18+
.input("Tell me a story about building the best SDK!")
19+
.model(ChatModel.GPT_4O)
20+
.build();
21+
22+
client.responses()
23+
.createStreaming(createParams)
24+
.subscribe(event -> event.outputTextDelta().ifPresent(textEvent -> System.out.print(textEvent.delta())))
25+
.onCompleteFuture()
26+
.join();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.openai.example;
2+
3+
import com.openai.client.OpenAIClient;
4+
import com.openai.client.okhttp.OpenAIOkHttpClient;
5+
import com.openai.core.http.StreamResponse;
6+
import com.openai.models.ChatModel;
7+
import com.openai.models.responses.ResponseCreateParams;
8+
import com.openai.models.responses.ResponseStreamEvent;
9+
10+
public final class ResponsesStreamingExample {
11+
private ResponsesStreamingExample() {}
12+
13+
public static void main(String[] args) {
14+
// Configures using one of:
15+
// - The `OPENAI_API_KEY` environment variable
16+
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables
17+
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
18+
19+
ResponseCreateParams createParams = ResponseCreateParams.builder()
20+
.input("Tell me a story about building the best SDK!")
21+
.model(ChatModel.GPT_4O)
22+
.build();
23+
24+
try (StreamResponse<ResponseStreamEvent> streamResponse =
25+
client.responses().createStreaming(createParams)) {
26+
streamResponse.stream()
27+
.flatMap(event -> event.outputTextDelta().stream())
28+
.forEach(textEvent -> System.out.print(textEvent.delta()));
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)