From 6b07d94b7c2b7e064d8a3da413461cc4f536739e Mon Sep 17 00:00:00 2001 From: UU_jeong <80961726+oU-Ua@users.noreply.github.com> Date: Sun, 14 Jul 2024 13:54:48 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20gpt=20custom=20=EA=B8=B0=EB=8A=A5=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 | 7 +++++ .../domain/gpt/service/GptService.java | 28 +++++++++++++++++++ 3 files changed, 40 insertions(+) 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 9909e7f..7c0bf02 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 @@ -25,4 +25,9 @@ public ResponseDTO getEasyToRead(String text) throws JsonProcessingException { 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)); + } } 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 6418deb..c7bcaa6 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 @@ -40,5 +40,12 @@ public Flux ask(Locale locale, } } + @GetMapping("/custom") + public ResponseEntity easyLeadCustom(@RequestParam String text) + throws JsonProcessingException { + return ResponseEntity.ok(gptBusiness.getEasyToReadCustom(text)); + + } + } 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 8b35731..c7a59a0 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 @@ -32,6 +32,9 @@ public class GptService { @Value("${gpt.api_key}") private String gptApiKey; + + @Value("${gpt.api_key_custom}") + private String gptApiCustomkey; private final ObjectMapper objectMapper = new ObjectMapper() .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE ); @@ -193,4 +196,29 @@ public Flux askStream(String text) throws JsonProcessingException { .bodyToFlux(String.class); return eventStream; } + + public HttpRequest requestGPTCustom(String text) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + List messages = new ArrayList<>(); + // Assistant API 사용할지 Prompt를 변경할지 선택하기 + // 시스템 역할 설정 + messages.add(new Message(text, "user")); + + ChatGPTRequestDTO chatGptRequest = new ChatGPTRequestDTO("ft:gpt-3.5-turbo-0125:personal::9klL6p0E", messages, 0.3,false); + String input = null; + input = mapper.writeValueAsString(chatGptRequest); + System.out.println(input); + System.out.println("apikey : " + gptApiCustomkey); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://api.openai.com/v1/chat/completions")) + .header("Content-Type", "application/json") + .header("Authorization", "Bearer " + gptApiCustomkey) + .POST(HttpRequest.BodyPublishers.ofString(input)) + .build(); + + return request; + + + } }