From 970f23ca644a05475cf322cc687af574324519e2 Mon Sep 17 00:00:00 2001 From: UU_jeong <80961726+oU-Ua@users.noreply.github.com> Date: Sat, 20 Jul 2024 22:57:17 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20dalle-3=EB=A5=BC=20=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=ED=95=9C=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/gpt/business/GptBusiness.java | 5 ++ .../domain/gpt/controller/GptController.java | 6 +++ .../domain/gpt/dto/DalleRequestDTO.java | 22 +++++++++ .../easylead/domain/gpt/dto/DalleResData.java | 18 +++++++ .../domain/gpt/dto/DalleResponseDTO.java | 23 +++++++++ .../domain/gpt/service/GptService.java | 47 +++++++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 src/main/java/com/easylead/easylead/domain/gpt/dto/DalleRequestDTO.java create mode 100644 src/main/java/com/easylead/easylead/domain/gpt/dto/DalleResData.java create mode 100644 src/main/java/com/easylead/easylead/domain/gpt/dto/DalleResponseDTO.java diff --git a/src/main/java/com/easylead/easylead/domain/gpt/business/GptBusiness.java b/src/main/java/com/easylead/easylead/domain/gpt/business/GptBusiness.java index 7c0bf02..41ff931 100644 --- a/src/main/java/com/easylead/easylead/domain/gpt/business/GptBusiness.java +++ b/src/main/java/com/easylead/easylead/domain/gpt/business/GptBusiness.java @@ -30,4 +30,9 @@ public ResponseDTO getEasyToReadCustom(String text) throws JsonProcessingExcepti HttpRequest request = gptService.requestGPTCustom(text); return gptConverter.toResponseDTO(gptService.responseGPT(request)); } + + public ResponseDTO getImage(String keyword) throws JsonProcessingException { + HttpRequest request = gptService.requestGPTImage(keyword); + return gptConverter.toResponseDTO(gptService.responseDalle(request)); + } } diff --git a/src/main/java/com/easylead/easylead/domain/gpt/controller/GptController.java b/src/main/java/com/easylead/easylead/domain/gpt/controller/GptController.java index c7bcaa6..83753b9 100644 --- a/src/main/java/com/easylead/easylead/domain/gpt/controller/GptController.java +++ b/src/main/java/com/easylead/easylead/domain/gpt/controller/GptController.java @@ -47,5 +47,11 @@ public ResponseEntity easyLeadCustom(@RequestParam String text) } + @GetMapping("/image") + public ResponseEntity ImageGenerate(@RequestParam String keyword) + throws JsonProcessingException { + return ResponseEntity.ok(gptBusiness.getImage(keyword)); + } + } diff --git a/src/main/java/com/easylead/easylead/domain/gpt/dto/DalleRequestDTO.java b/src/main/java/com/easylead/easylead/domain/gpt/dto/DalleRequestDTO.java new file mode 100644 index 0000000..f246961 --- /dev/null +++ b/src/main/java/com/easylead/easylead/domain/gpt/dto/DalleRequestDTO.java @@ -0,0 +1,22 @@ +package com.easylead.easylead.domain.gpt.dto; + +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@Builder +@JsonPropertyOrder({"model", "prompt", "n","size"}) +public class DalleRequestDTO { + String model; + String prompt; + int n; + String size; + +} diff --git a/src/main/java/com/easylead/easylead/domain/gpt/dto/DalleResData.java b/src/main/java/com/easylead/easylead/domain/gpt/dto/DalleResData.java new file mode 100644 index 0000000..bdded2a --- /dev/null +++ b/src/main/java/com/easylead/easylead/domain/gpt/dto/DalleResData.java @@ -0,0 +1,18 @@ +package com.easylead.easylead.domain.gpt.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@Builder +@JsonIgnoreProperties(ignoreUnknown = true) +public class DalleResData { + String url; +} diff --git a/src/main/java/com/easylead/easylead/domain/gpt/dto/DalleResponseDTO.java b/src/main/java/com/easylead/easylead/domain/gpt/dto/DalleResponseDTO.java new file mode 100644 index 0000000..e3885ec --- /dev/null +++ b/src/main/java/com/easylead/easylead/domain/gpt/dto/DalleResponseDTO.java @@ -0,0 +1,23 @@ +package com.easylead.easylead.domain.gpt.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@Builder +@JsonIgnoreProperties(ignoreUnknown = true) +public class DalleResponseDTO { + int created; + + + List data; + +} diff --git a/src/main/java/com/easylead/easylead/domain/gpt/service/GptService.java b/src/main/java/com/easylead/easylead/domain/gpt/service/GptService.java index c7a59a0..23a38c4 100644 --- a/src/main/java/com/easylead/easylead/domain/gpt/service/GptService.java +++ b/src/main/java/com/easylead/easylead/domain/gpt/service/GptService.java @@ -221,4 +221,51 @@ public HttpRequest requestGPTCustom(String text) throws JsonProcessingException } + + public HttpRequest requestGPTImage(String keyword) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + List messages = new ArrayList<>(); + + messages.add(new Message(keyword, "user")); + + DalleRequestDTO dalleRequest = new DalleRequestDTO("dall-e-3", keyword, 1,"1024x1024"); + String input = null; + input = mapper.writeValueAsString(dalleRequest); + System.out.println(input); + System.out.println("apikey : " + gptApiCustomkey); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://api.openai.com/v1/images/generations")) + .header("Content-Type", "application/json") + .header("Authorization", "Bearer " + gptApiCustomkey) + .POST(HttpRequest.BodyPublishers.ofString(input)) + .build(); + + return request; + + } + + public String responseDalle(HttpRequest request) throws JsonProcessingException { + HttpClient client = HttpClient.newHttpClient(); + HttpResponse response = null; + try { + response = client.send(request, HttpResponse.BodyHandlers.ofString()); + } catch (IOException | InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println(response.body()); + ObjectMapper objectMapper = new ObjectMapper(); + DalleResponseDTO dalleResponseDTO = objectMapper.readValue(response.body(), DalleResponseDTO.class); + if(dalleResponseDTO.getData() ==null){ + throw new ApiException(ErrorCode.SERVER_ERROR); + } + List data = dalleResponseDTO.getData(); + + String url = data.get(0).getUrl(); + String subject = ""; + + System.out.println("content = " + url); + return url; + + } }