Skip to content

Commit 286b7d5

Browse files
docs: add ImageGenerationExample (#457)
* ImageGenerationExample * docs: slight example tweaks --------- Co-authored-by: Tomer Aberbach <tomer@stainless.com>
1 parent 3c78e3a commit 286b7d5

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.openai.example;
2+
3+
import com.openai.client.OpenAIClient;
4+
import com.openai.client.okhttp.OpenAIOkHttpClient;
5+
import com.openai.models.images.ImageGenerateParams;
6+
import com.openai.models.images.ImageModel;
7+
import java.io.IOException;
8+
9+
public final class ImageGenerationExample {
10+
private ImageGenerationExample() {}
11+
12+
public static void main(String[] args) throws IOException {
13+
// Configures using one of:
14+
// - The `OPENAI_API_KEY` environment variable
15+
// - The `OPENAI_BASE_URL` and `AZURE_OPENAI_KEY` environment variables
16+
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
17+
18+
ImageGenerateParams imageGenerateParams = ImageGenerateParams.builder()
19+
.responseFormat(ImageGenerateParams.ResponseFormat.URL)
20+
.prompt("Two cats playing ping-pong")
21+
.model(ImageModel.DALL_E_2)
22+
.size(ImageGenerateParams.Size._512X512)
23+
.n(1)
24+
.build();
25+
26+
client.images().generate(imageGenerateParams).data().orElseThrow().stream()
27+
.flatMap(image -> image.url().stream())
28+
.forEach(System.out::println);
29+
}
30+
}

0 commit comments

Comments
 (0)