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 1/3] =?UTF-8?q?feat:=20dalle-3=EB=A5=BC=20=EC=9D=B4?= =?UTF-8?q?=EC=9A=A9=ED=95=9C=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=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; + + } } From 477e3ba1a5256ae24af72eabc85418f84569ce69 Mon Sep 17 00:00:00 2001 From: UU_jeong <80961726+oU-Ua@users.noreply.github.com> Date: Sun, 28 Jul 2024 15:26:22 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20custom=20stream=EC=9C=BC=EB=A1=9C?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../easylead/easylead/config/GptConfig.java | 1 + .../domain/gpt/business/GptBusiness.java | 5 ++-- .../domain/gpt/controller/GptController.java | 22 +++++++++----- .../domain/gpt/service/GptService.java | 30 ++++++++++++++++++- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/easylead/easylead/config/GptConfig.java b/src/main/java/com/easylead/easylead/config/GptConfig.java index aafdfd5..d51d721 100644 --- a/src/main/java/com/easylead/easylead/config/GptConfig.java +++ b/src/main/java/com/easylead/easylead/config/GptConfig.java @@ -7,6 +7,7 @@ public class GptConfig { public static final String AUTHORIZATION = "Authorization"; public static final String BEARER = "Bearer "; public static final String CHAT_MODEL = "gpt-3.5-turbo"; + public static final String CHAT_MODEL_CUSTOM = "ft:gpt-3.5-turbo-0125:personal::9ldfWO0p"; public static final Integer MAX_TOKEN = 300; public static final Boolean STREAM = true; public static final String ROLE = "user"; 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 41ff931..0c9873f 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 @@ -26,9 +26,8 @@ public Flux ask(String text) throws JsonProcessingException { return gptService.askStream(text); } - public ResponseDTO getEasyToReadCustom(String text) throws JsonProcessingException { - HttpRequest request = gptService.requestGPTCustom(text); - return gptConverter.toResponseDTO(gptService.responseGPT(request)); + public Flux getEasyToReadCustom(String text) throws JsonProcessingException { + return gptService.askCustomStream(text); } public ResponseDTO getImage(String keyword) throws JsonProcessingException { 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 83753b9..8124531 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 @@ -5,15 +5,18 @@ import com.fasterxml.jackson.core.JsonProcessingException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.util.Locale; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Flux; -import java.util.Locale; - @Slf4j @RestController @RequiredArgsConstructor @@ -40,10 +43,15 @@ public Flux ask(Locale locale, } } - @GetMapping("/custom") - public ResponseEntity easyLeadCustom(@RequestParam String text) - throws JsonProcessingException { - return ResponseEntity.ok(gptBusiness.getEasyToReadCustom(text)); + @GetMapping(value="/custom", produces = MediaType.TEXT_EVENT_STREAM_VALUE) + public Flux easyLeadCustom(@RequestParam String text) + { + try { + return gptBusiness.getEasyToReadCustom(text); + }catch (JsonProcessingException je){ + log.error(je.getMessage()); + return Flux.empty(); + } } 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 23a38c4..35323bd 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 @@ -196,6 +196,32 @@ public Flux askStream(String text) throws JsonProcessingException { .bodyToFlux(String.class); return eventStream; } + public Flux askCustomStream(String text) throws JsonProcessingException { + WebClient client = WebClient.builder() + .baseUrl(GptConfig.CHAT_URL) + .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .defaultHeader(GptConfig.AUTHORIZATION, GptConfig.BEARER + gptApiCustomkey) + .build(); + + List messages = new ArrayList<>(); + + messages.add(new Message(text,"user")); + + ChatGPTRequestDTO chatGptRequest = new ChatGPTRequestDTO( + GptConfig.CHAT_MODEL_CUSTOM, + messages, + GptConfig.TEMPERATURE, + GptConfig.STREAM + ); + String requestValue = objectMapper.writeValueAsString(chatGptRequest); + + Flux eventStream = client.post() + .bodyValue(requestValue) + .accept(MediaType.TEXT_EVENT_STREAM) + .retrieve() + .bodyToFlux(String.class); + return eventStream; + } public HttpRequest requestGPTCustom(String text) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); @@ -204,7 +230,7 @@ public HttpRequest requestGPTCustom(String text) throws JsonProcessingException // 시스템 역할 설정 messages.add(new Message(text, "user")); - ChatGPTRequestDTO chatGptRequest = new ChatGPTRequestDTO("ft:gpt-3.5-turbo-0125:personal::9klL6p0E", messages, 0.3,false); + ChatGPTRequestDTO chatGptRequest = new ChatGPTRequestDTO("ft:gpt-3.5-turbo-0125:personal::9ldfWO0p", messages, 0.3,false); String input = null; input = mapper.writeValueAsString(chatGptRequest); System.out.println(input); @@ -268,4 +294,6 @@ public String responseDalle(HttpRequest request) throws JsonProcessingException return url; } + + } From 26df8acdf2eb141b80ad9cba79de6aee6f450930 Mon Sep 17 00:00:00 2001 From: oU-Ua Date: Sun, 28 Jul 2024 16:15:29 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20gpt=20=ED=95=99=EC=8A=B5=20?= =?UTF-8?q?=EB=AA=A8=EB=8D=B8=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/easylead/easylead/config/GptConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/easylead/easylead/config/GptConfig.java b/src/main/java/com/easylead/easylead/config/GptConfig.java index d51d721..4a38457 100644 --- a/src/main/java/com/easylead/easylead/config/GptConfig.java +++ b/src/main/java/com/easylead/easylead/config/GptConfig.java @@ -7,7 +7,7 @@ public class GptConfig { public static final String AUTHORIZATION = "Authorization"; public static final String BEARER = "Bearer "; public static final String CHAT_MODEL = "gpt-3.5-turbo"; - public static final String CHAT_MODEL_CUSTOM = "ft:gpt-3.5-turbo-0125:personal::9ldfWO0p"; + public static final String CHAT_MODEL_CUSTOM = "ft:gpt-3.5-turbo-0613:personal::9prSIgJ8"; public static final Integer MAX_TOKEN = 300; public static final Boolean STREAM = true; public static final String ROLE = "user";