From 8c2a76c25768127ca08772533c6f23884c3efc8c Mon Sep 17 00:00:00 2001 From: AddYu Date: Sat, 13 Apr 2024 00:59:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81dall-e=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chatgpt/entity/images/Generations.java | 52 +++++++++++++++++-- .../entity/images/ImagesRensponse.java | 15 ++++++ .../java/com/plexpt/chatgpt/ImagesTest.java | 33 ++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/plexpt/chatgpt/ImagesTest.java diff --git a/src/main/java/com/plexpt/chatgpt/entity/images/Generations.java b/src/main/java/com/plexpt/chatgpt/entity/images/Generations.java index 0c3b00d..a2edcd8 100644 --- a/src/main/java/com/plexpt/chatgpt/entity/images/Generations.java +++ b/src/main/java/com/plexpt/chatgpt/entity/images/Generations.java @@ -6,7 +6,9 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; /** * @Author matoooo @@ -21,15 +23,57 @@ @JsonIgnoreProperties(ignoreUnknown = true) public class Generations { private String prompt; + /** + * Optional Defaults to dall-e-2 + *
+ * The model to use for image generation. + */ + private String model; private int n; + /** + * Optional Defaults to standard + *
+ * dall-e-3可以使用hd + */ + private String quality; + /** + * The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024 for dall-e-2. Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 + */ private String size; private String response_format; + /** + * Optional + * Defaults to vivid + *
+ * The style of the generated images. Must be one of vivid or natural. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images. This param is only supported for dall-e-3. + */ + private String style; - public static Generations ofURL(String prompt,int n,String size) { - return new Generations(prompt,n,size, ResponseFormat.URL.getValue()); + + @Getter + @AllArgsConstructor + public enum Model { + DALL_E_2("dall-e-2"), + DALL_E_3("dall-e-3"), + ; + private String name; + } + + public static Generations ofURL(String prompt, int n, String size) { + return Generations.builder() + .prompt(prompt) + .n(n) + .size(size) + .response_format(ResponseFormat.URL.getValue()) + .build(); } - public static Generations ofB64_JSON(String prompt,int n,String size) { - return new Generations(prompt,n,size, ResponseFormat.B64_JSON.getValue()); + public static Generations ofB64_JSON(String prompt, int n, String size) { + return Generations.builder() + .prompt(prompt) + .n(n) + .size(size) + .response_format(ResponseFormat.B64_JSON.getValue()) + .build(); } } diff --git a/src/main/java/com/plexpt/chatgpt/entity/images/ImagesRensponse.java b/src/main/java/com/plexpt/chatgpt/entity/images/ImagesRensponse.java index 7598292..160f0b5 100644 --- a/src/main/java/com/plexpt/chatgpt/entity/images/ImagesRensponse.java +++ b/src/main/java/com/plexpt/chatgpt/entity/images/ImagesRensponse.java @@ -1,9 +1,12 @@ package com.plexpt.chatgpt.entity.images; +import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; +import java.util.ArrayList; import java.util.List; +import java.util.Map; /** * @Author matoooo @@ -19,4 +22,16 @@ public class ImagesRensponse { private List data; private long created; + public List getUrls() { + List urls = new ArrayList<>(); + for (Object datum : data) { + if (datum instanceof Map) { + Object url = ((Map) datum).get("url"); + if (url != null) { + urls.add(url.toString()); + } + } + } + return urls; + } } diff --git a/src/test/java/com/plexpt/chatgpt/ImagesTest.java b/src/test/java/com/plexpt/chatgpt/ImagesTest.java new file mode 100644 index 0000000..04ab010 --- /dev/null +++ b/src/test/java/com/plexpt/chatgpt/ImagesTest.java @@ -0,0 +1,33 @@ +package com.plexpt.chatgpt; + +import com.plexpt.chatgpt.entity.images.Generations; +import com.plexpt.chatgpt.entity.images.ImagesRensponse; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +/** + * @author xiejiay (^_−)☆ + */ +public class ImagesTest { + + @Test + public void generations() { + Images images = Images.builder() + .apiHost("https://api.openai.com/") + .apiKey("***") + .build().init(); + ImagesRensponse generations = images.generations(Generations.builder() + .prompt("黑白摄影,一位中年人手持一张空白的日历,表情深思,背景为朦胧的城市街景,光圈f/2.8,ISO 100,焦距50mm。\n" + + "关键词:黑白摄影、空白日历、深思表情、朦胧城市背景") + .model(Generations.Model.DALL_E_3.getName()) + .size("1792x1024") + .style("natural") + .quality("hd") + .build()); + List data = generations.getUrls(); + System.out.println(data); + Assert.assertNotNull(data); + } +} \ No newline at end of file From 70d6e4c61e1a465b894d6cda7b5a117f2c934f02 Mon Sep 17 00:00:00 2001 From: AddYu Date: Sat, 13 Apr 2024 00:59:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=94=AF=E6=8C=81dall-e=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chatgpt/entity/images/Generations.java | 52 +++++++++++++++++-- .../entity/images/ImagesRensponse.java | 14 +++++ .../java/com/plexpt/chatgpt/ImagesTest.java | 33 ++++++++++++ 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/plexpt/chatgpt/ImagesTest.java diff --git a/src/main/java/com/plexpt/chatgpt/entity/images/Generations.java b/src/main/java/com/plexpt/chatgpt/entity/images/Generations.java index 0c3b00d..a2edcd8 100644 --- a/src/main/java/com/plexpt/chatgpt/entity/images/Generations.java +++ b/src/main/java/com/plexpt/chatgpt/entity/images/Generations.java @@ -6,7 +6,9 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; /** * @Author matoooo @@ -21,15 +23,57 @@ @JsonIgnoreProperties(ignoreUnknown = true) public class Generations { private String prompt; + /** + * Optional Defaults to dall-e-2 + *
+ * The model to use for image generation. + */ + private String model; private int n; + /** + * Optional Defaults to standard + *
+ * dall-e-3可以使用hd + */ + private String quality; + /** + * The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024 for dall-e-2. Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 + */ private String size; private String response_format; + /** + * Optional + * Defaults to vivid + *
+ * The style of the generated images. Must be one of vivid or natural. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images. This param is only supported for dall-e-3. + */ + private String style; - public static Generations ofURL(String prompt,int n,String size) { - return new Generations(prompt,n,size, ResponseFormat.URL.getValue()); + + @Getter + @AllArgsConstructor + public enum Model { + DALL_E_2("dall-e-2"), + DALL_E_3("dall-e-3"), + ; + private String name; + } + + public static Generations ofURL(String prompt, int n, String size) { + return Generations.builder() + .prompt(prompt) + .n(n) + .size(size) + .response_format(ResponseFormat.URL.getValue()) + .build(); } - public static Generations ofB64_JSON(String prompt,int n,String size) { - return new Generations(prompt,n,size, ResponseFormat.B64_JSON.getValue()); + public static Generations ofB64_JSON(String prompt, int n, String size) { + return Generations.builder() + .prompt(prompt) + .n(n) + .size(size) + .response_format(ResponseFormat.B64_JSON.getValue()) + .build(); } } diff --git a/src/main/java/com/plexpt/chatgpt/entity/images/ImagesRensponse.java b/src/main/java/com/plexpt/chatgpt/entity/images/ImagesRensponse.java index 7598292..291069a 100644 --- a/src/main/java/com/plexpt/chatgpt/entity/images/ImagesRensponse.java +++ b/src/main/java/com/plexpt/chatgpt/entity/images/ImagesRensponse.java @@ -3,7 +3,9 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; +import java.util.ArrayList; import java.util.List; +import java.util.Map; /** * @Author matoooo @@ -19,4 +21,16 @@ public class ImagesRensponse { private List data; private long created; + public List getUrls() { + List urls = new ArrayList<>(); + for (Object datum : data) { + if (datum instanceof Map) { + Object url = ((Map) datum).get("url"); + if (url != null) { + urls.add(url.toString()); + } + } + } + return urls; + } } diff --git a/src/test/java/com/plexpt/chatgpt/ImagesTest.java b/src/test/java/com/plexpt/chatgpt/ImagesTest.java new file mode 100644 index 0000000..04ab010 --- /dev/null +++ b/src/test/java/com/plexpt/chatgpt/ImagesTest.java @@ -0,0 +1,33 @@ +package com.plexpt.chatgpt; + +import com.plexpt.chatgpt.entity.images.Generations; +import com.plexpt.chatgpt.entity.images.ImagesRensponse; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +/** + * @author xiejiay (^_−)☆ + */ +public class ImagesTest { + + @Test + public void generations() { + Images images = Images.builder() + .apiHost("https://api.openai.com/") + .apiKey("***") + .build().init(); + ImagesRensponse generations = images.generations(Generations.builder() + .prompt("黑白摄影,一位中年人手持一张空白的日历,表情深思,背景为朦胧的城市街景,光圈f/2.8,ISO 100,焦距50mm。\n" + + "关键词:黑白摄影、空白日历、深思表情、朦胧城市背景") + .model(Generations.Model.DALL_E_3.getName()) + .size("1792x1024") + .style("natural") + .quality("hd") + .build()); + List data = generations.getUrls(); + System.out.println(data); + Assert.assertNotNull(data); + } +} \ No newline at end of file