Skip to content

Commit

Permalink
Merge pull request #268 from scutken/main
Browse files Browse the repository at this point in the history
支持使用dall-e 3
  • Loading branch information
PlexPt authored May 23, 2024
2 parents 227c0ef + 184aaa0 commit 3d04257
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/main/java/com/plexpt/chatgpt/entity/images/Generations.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

/**
* @Author matoooo
Expand All @@ -21,15 +23,57 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class Generations {
private String prompt;
/**
* Optional Defaults to dall-e-2
* <br/>
* The model to use for image generation.
*/
private String model;
private int n;
/**
* Optional Defaults to standard
* <br/>
* 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
* <br/>
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,4 +22,16 @@ public class ImagesRensponse {
private List<Object> data;
private long created;

public List<String> getUrls() {
List<String> 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;
}
}
33 changes: 33 additions & 0 deletions src/test/java/com/plexpt/chatgpt/ImagesTest.java
Original file line number Diff line number Diff line change
@@ -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<String> data = generations.getUrls();
System.out.println(data);
Assert.assertNotNull(data);
}
}

0 comments on commit 3d04257

Please sign in to comment.