openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

Error creating image editing and image change

Open 1999LonelyCHEN opened this issue 2 years ago • 6 comments

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

1999LonelyCHEN avatar Feb 22 '23 09:02 1999LonelyCHEN

image image

1999LonelyCHEN avatar Feb 22 '23 09:02 1999LonelyCHEN

Version is 0.9.0

1999LonelyCHEN avatar Feb 22 '23 09:02 1999LonelyCHEN

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;
	}

cryptoapebot avatar Feb 22 '23 14:02 cryptoapebot

Any new info on this one, I got the same issue.

Screenshot 2023-02-28 at 4 39 49 PM

hntan avatar Feb 28 '23 09:02 hntan

Workaround by setting value for responseFormat.

CreateImageEditRequest request = CreateImageEditRequest.builder()
            .prompt(resolvedPrompt)
            .n(n)
            .size(size)
            .responseFormat("url")
            .build();

hntan avatar Feb 28 '23 10:02 hntan

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;
}

cryptoapebot avatar Feb 28 '23 14:02 cryptoapebot

Strange, the official docs say that response_format is optional and defaults to "url", I'll keep an eye on this

TheoKanning avatar Apr 01 '23 19:04 TheoKanning