openai-java
openai-java copied to clipboard
Error creating image editing and image change
When creating image editing and image changes, the interface will report a NullPointerException if the parameters that are not required are not written. After filling, the interface will report an HTTP 400 error
Version is 0.9.0
This doesn't answer your exact question, but I've been calling the v1 DALLE2 api directly. It returns a URL to the generated image which you can stream. FWIW, I haven't gotten the chatGPT endpoint to work for me for images.
public final static String openai_image = "https://api.openai.com/v1/images/generations";
public final static String json_request = "{ \"prompt\": ${PROMPT}, \"n\": 1, \"size\": \"1024x1024\" }";
GeneratedImage.java
@Data public class GeneratedImage {
Integer created = 0;
ArrayList<ImageLocation> data = null;
}
Call
public static InputStream dalle2(String prompt) {
System.out.println("DAlle prompt: " + prompt);
String json = json_request.replace("${PROMPT}", prompt);
System.out.println("json_request to dalle: " + json);
HttpRequest request =
HttpRequest.newBuilder()
.uri(URI.create(openai_image))
.header("Content-Type","application/json")
.header("Authorization", Ridges.OPENAI_API_KEY)
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = null;
try {
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
GeneratedImage gen = null;
if ((response.statusCode() / 100) != 2) {
System.out.println("response code: " + response.statusCode());
System.out.println("response body: " + response.body());
} else {
gen = gs.fromJson(response.body(), GeneratedImage.class);
}
if (gen != null && gen.getData() != null) {
return new URL(gen.getData().get(0).getUrl()).openStream();
}
} catch (IOException | InterruptedException e) {
System.out.println("response code: " + response.statusCode());
e.printStackTrace();
}
return null;
}
Any new info on this one, I got the same issue.

Workaround by setting value for responseFormat
.
CreateImageEditRequest request = CreateImageEditRequest.builder()
.prompt(resolvedPrompt)
.n(n)
.size(size)
.responseFormat("url")
.build();
Sorry, I thought ImageLocation was a standard class, but I realized I just wrote something to capture the result location which is a string of a URL.
import lombok.Data;
@Data
public class ImageLocation {
String url;
}
Strange, the official docs say that response_format is optional and defaults to "url", I'll keep an eye on this