Skip to content

Commit 8c2a76c

Browse files
committed
支持dall-e 3
1 parent 3367ae7 commit 8c2a76c

File tree

3 files changed

+96
-4
lines changed

3 files changed

+96
-4
lines changed

src/main/java/com/plexpt/chatgpt/entity/images/Generations.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import lombok.AllArgsConstructor;
77
import lombok.Builder;
88
import lombok.Data;
9+
import lombok.Getter;
910
import lombok.NoArgsConstructor;
11+
import lombok.RequiredArgsConstructor;
1012

1113
/**
1214
* @Author matoooo
@@ -21,15 +23,57 @@
2123
@JsonIgnoreProperties(ignoreUnknown = true)
2224
public class Generations {
2325
private String prompt;
26+
/**
27+
* Optional Defaults to dall-e-2
28+
* <br/>
29+
* The model to use for image generation.
30+
*/
31+
private String model;
2432
private int n;
33+
/**
34+
* Optional Defaults to standard
35+
* <br/>
36+
* dall-e-3可以使用hd
37+
*/
38+
private String quality;
39+
/**
40+
* 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
41+
*/
2542
private String size;
2643
private String response_format;
44+
/**
45+
* Optional
46+
* Defaults to vivid
47+
* <br/>
48+
* 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.
49+
*/
50+
private String style;
2751

28-
public static Generations ofURL(String prompt,int n,String size) {
29-
return new Generations(prompt,n,size, ResponseFormat.URL.getValue());
52+
53+
@Getter
54+
@AllArgsConstructor
55+
public enum Model {
56+
DALL_E_2("dall-e-2"),
57+
DALL_E_3("dall-e-3"),
58+
;
59+
private String name;
60+
}
61+
62+
public static Generations ofURL(String prompt, int n, String size) {
63+
return Generations.builder()
64+
.prompt(prompt)
65+
.n(n)
66+
.size(size)
67+
.response_format(ResponseFormat.URL.getValue())
68+
.build();
3069
}
3170

32-
public static Generations ofB64_JSON(String prompt,int n,String size) {
33-
return new Generations(prompt,n,size, ResponseFormat.B64_JSON.getValue());
71+
public static Generations ofB64_JSON(String prompt, int n, String size) {
72+
return Generations.builder()
73+
.prompt(prompt)
74+
.n(n)
75+
.size(size)
76+
.response_format(ResponseFormat.B64_JSON.getValue())
77+
.build();
3478
}
3579
}

src/main/java/com/plexpt/chatgpt/entity/images/ImagesRensponse.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.plexpt.chatgpt.entity.images;
22

3+
import com.alibaba.fastjson.JSON;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import lombok.Data;
56

7+
import java.util.ArrayList;
68
import java.util.List;
9+
import java.util.Map;
710

811
/**
912
* @Author matoooo
@@ -19,4 +22,16 @@ public class ImagesRensponse {
1922
private List<Object> data;
2023
private long created;
2124

25+
public List<String> getUrls() {
26+
List<String> urls = new ArrayList<>();
27+
for (Object datum : data) {
28+
if (datum instanceof Map) {
29+
Object url = ((Map<?, ?>) datum).get("url");
30+
if (url != null) {
31+
urls.add(url.toString());
32+
}
33+
}
34+
}
35+
return urls;
36+
}
2237
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.plexpt.chatgpt;
2+
3+
import com.plexpt.chatgpt.entity.images.Generations;
4+
import com.plexpt.chatgpt.entity.images.ImagesRensponse;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.util.List;
9+
10+
/**
11+
* @author xiejiay (^_−)☆
12+
*/
13+
public class ImagesTest {
14+
15+
@Test
16+
public void generations() {
17+
Images images = Images.builder()
18+
.apiHost("https://api.openai.com/")
19+
.apiKey("***")
20+
.build().init();
21+
ImagesRensponse generations = images.generations(Generations.builder()
22+
.prompt("黑白摄影,一位中年人手持一张空白的日历,表情深思,背景为朦胧的城市街景,光圈f/2.8,ISO 100,焦距50mm。\n" +
23+
"关键词:黑白摄影、空白日历、深思表情、朦胧城市背景")
24+
.model(Generations.Model.DALL_E_3.getName())
25+
.size("1792x1024")
26+
.style("natural")
27+
.quality("hd")
28+
.build());
29+
List<String> data = generations.getUrls();
30+
System.out.println(data);
31+
Assert.assertNotNull(data);
32+
}
33+
}

0 commit comments

Comments
 (0)