From 0b4e881438fbc92ac35f28270275d3d886a16b20 Mon Sep 17 00:00:00 2001 From: Kim Jiyoon Date: Sat, 9 Mar 2024 22:28:16 +0900 Subject: [PATCH 1/4] =?UTF-8?q?modify:=20gpt=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=9D=B4=EB=A6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ChatGptConfig.java => GptConfig.java} | 2 +- .../gpt/controller/ChatGptController.java | 26 ------- .../domain/gpt/controller/GptController.java | 25 +++++++ ...tGptRequestDTO.java => GptRequestDTO.java} | 6 +- ...ptResponseDTO.java => GptResponseDTO.java} | 6 +- .../{ChatGptMessage.java => GptMessage.java} | 4 +- .../domain/gpt/service/ChatGptService.java | 72 ------------------ .../domain/gpt/service/GptService.java | 73 +++++++++++++++++++ 8 files changed, 107 insertions(+), 107 deletions(-) rename backend/src/main/java/com/isp/backend/domain/gpt/config/{ChatGptConfig.java => GptConfig.java} (95%) delete mode 100644 backend/src/main/java/com/isp/backend/domain/gpt/controller/ChatGptController.java create mode 100644 backend/src/main/java/com/isp/backend/domain/gpt/controller/GptController.java rename backend/src/main/java/com/isp/backend/domain/gpt/dto/{ChatGptRequestDTO.java => GptRequestDTO.java} (75%) rename backend/src/main/java/com/isp/backend/domain/gpt/dto/{ChatGptResponseDTO.java => GptResponseDTO.java} (87%) rename backend/src/main/java/com/isp/backend/domain/gpt/entity/{ChatGptMessage.java => GptMessage.java} (73%) delete mode 100644 backend/src/main/java/com/isp/backend/domain/gpt/service/ChatGptService.java create mode 100644 backend/src/main/java/com/isp/backend/domain/gpt/service/GptService.java diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/config/ChatGptConfig.java b/backend/src/main/java/com/isp/backend/domain/gpt/config/GptConfig.java similarity index 95% rename from backend/src/main/java/com/isp/backend/domain/gpt/config/ChatGptConfig.java rename to backend/src/main/java/com/isp/backend/domain/gpt/config/GptConfig.java index a14e9f90..99d7e11e 100644 --- a/backend/src/main/java/com/isp/backend/domain/gpt/config/ChatGptConfig.java +++ b/backend/src/main/java/com/isp/backend/domain/gpt/config/GptConfig.java @@ -3,7 +3,7 @@ import org.springframework.context.annotation.Configuration; @Configuration -public class ChatGptConfig { +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"; diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/controller/ChatGptController.java b/backend/src/main/java/com/isp/backend/domain/gpt/controller/ChatGptController.java deleted file mode 100644 index c801952d..00000000 --- a/backend/src/main/java/com/isp/backend/domain/gpt/controller/ChatGptController.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.isp.backend.domain.gpt.controller; - -import com.isp.backend.domain.gpt.dto.ChatGptResponseDTO; -import com.isp.backend.domain.gpt.dto.QuestionRequestDTO; -import com.isp.backend.domain.gpt.entity.ChatGptMessage; -import com.isp.backend.domain.gpt.service.ChatGptService; -import lombok.RequiredArgsConstructor; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RequiredArgsConstructor -@RequestMapping("api/chat-gpt") -@RestController -public class ChatGptController { - private final ChatGptService chatGptService; - - @PostMapping("/question") - public ResponseEntity sendQuestion(@RequestBody QuestionRequestDTO questionRequestDTO) { - ChatGptResponseDTO chatGptResponseDTO = new ChatGptResponseDTO(); - chatGptResponseDTO = chatGptService.askQuestion(questionRequestDTO); - return ResponseEntity.ok(chatGptResponseDTO.getChoices().get(0).getMessage()); - } -} \ No newline at end of file diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/controller/GptController.java b/backend/src/main/java/com/isp/backend/domain/gpt/controller/GptController.java new file mode 100644 index 00000000..261f6bbe --- /dev/null +++ b/backend/src/main/java/com/isp/backend/domain/gpt/controller/GptController.java @@ -0,0 +1,25 @@ +package com.isp.backend.domain.gpt.controller; + +import com.isp.backend.domain.gpt.dto.GptResponseDTO; +import com.isp.backend.domain.gpt.dto.GptScheduleRequestDto; +import com.isp.backend.domain.gpt.entity.GptMessage; +import com.isp.backend.domain.gpt.service.GptService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RequestMapping("api/chat-gpt") +@RestController +public class GptController { + private final GptService gptService; + + @PostMapping("/question") + public ResponseEntity sendQuestion(@RequestBody GptScheduleRequestDto gptScheduleRequestDto) { + GptResponseDTO gptResponseDTO = gptService.askQuestion(gptScheduleRequestDto); + return ResponseEntity.ok(gptResponseDTO.getChoices().get(0).getMessage()); + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/dto/ChatGptRequestDTO.java b/backend/src/main/java/com/isp/backend/domain/gpt/dto/GptRequestDTO.java similarity index 75% rename from backend/src/main/java/com/isp/backend/domain/gpt/dto/ChatGptRequestDTO.java rename to backend/src/main/java/com/isp/backend/domain/gpt/dto/GptRequestDTO.java index 749117ce..56327a30 100644 --- a/backend/src/main/java/com/isp/backend/domain/gpt/dto/ChatGptRequestDTO.java +++ b/backend/src/main/java/com/isp/backend/domain/gpt/dto/GptRequestDTO.java @@ -1,7 +1,7 @@ package com.isp.backend.domain.gpt.dto; import com.fasterxml.jackson.annotation.JsonProperty; -import com.isp.backend.domain.gpt.entity.ChatGptMessage; +import com.isp.backend.domain.gpt.entity.GptMessage; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -11,12 +11,12 @@ @Getter @NoArgsConstructor @AllArgsConstructor -public class ChatGptRequestDTO { +public class GptRequestDTO { private String model; @JsonProperty("max_tokens") private Integer maxTokens; private Double temperature; private Boolean stream; - private List messages; + private List messages; } diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/dto/ChatGptResponseDTO.java b/backend/src/main/java/com/isp/backend/domain/gpt/dto/GptResponseDTO.java similarity index 87% rename from backend/src/main/java/com/isp/backend/domain/gpt/dto/ChatGptResponseDTO.java rename to backend/src/main/java/com/isp/backend/domain/gpt/dto/GptResponseDTO.java index a6ce2778..880669ca 100644 --- a/backend/src/main/java/com/isp/backend/domain/gpt/dto/ChatGptResponseDTO.java +++ b/backend/src/main/java/com/isp/backend/domain/gpt/dto/GptResponseDTO.java @@ -1,7 +1,7 @@ package com.isp.backend.domain.gpt.dto; import com.fasterxml.jackson.annotation.JsonProperty; -import com.isp.backend.domain.gpt.entity.ChatGptMessage; +import com.isp.backend.domain.gpt.entity.GptMessage; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -12,7 +12,7 @@ @Getter @NoArgsConstructor @AllArgsConstructor -public class ChatGptResponseDTO { +public class GptResponseDTO { private String id; private String object; private long created; @@ -34,7 +34,7 @@ public static class Usage { @Getter @Setter public static class Choice { - private ChatGptMessage message; + private GptMessage message; @JsonProperty("finish_reason") private String finishReason; private int index; diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/entity/ChatGptMessage.java b/backend/src/main/java/com/isp/backend/domain/gpt/entity/GptMessage.java similarity index 73% rename from backend/src/main/java/com/isp/backend/domain/gpt/entity/ChatGptMessage.java rename to backend/src/main/java/com/isp/backend/domain/gpt/entity/GptMessage.java index 86edf5b0..beaac34b 100644 --- a/backend/src/main/java/com/isp/backend/domain/gpt/entity/ChatGptMessage.java +++ b/backend/src/main/java/com/isp/backend/domain/gpt/entity/GptMessage.java @@ -8,12 +8,12 @@ @Data @NoArgsConstructor -public class ChatGptMessage implements Serializable { +public class GptMessage implements Serializable { private String role; private String content; @Builder - public ChatGptMessage(String role, String content) { + public GptMessage(String role, String content) { this.role = role; this.content = content; } diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/service/ChatGptService.java b/backend/src/main/java/com/isp/backend/domain/gpt/service/ChatGptService.java deleted file mode 100644 index abbc69ee..00000000 --- a/backend/src/main/java/com/isp/backend/domain/gpt/service/ChatGptService.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.isp.backend.domain.gpt.service; - -import com.isp.backend.domain.gpt.config.ChatGptConfig; -import com.isp.backend.domain.gpt.dto.ChatGptRequestDTO; -import com.isp.backend.domain.gpt.dto.ChatGptResponseDTO; -import com.isp.backend.domain.gpt.dto.QuestionRequestDTO; -import com.isp.backend.domain.gpt.entity.ChatGptMessage; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.http.client.SimpleClientHttpRequestFactory; -import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; - -import java.util.ArrayList; -import java.util.List; - -@Slf4j -@RequiredArgsConstructor -@Service -public class ChatGptService { - private final RestTemplate restTemplate; - - @Value("${api-key.chat-gpt}") - private String apiKey; - - public HttpEntity buildHttpEntity(ChatGptRequestDTO chatGptRequestDTO) { - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.parseMediaType(ChatGptConfig.MEDIA_TYPE)); - httpHeaders.add(ChatGptConfig.AUTHORIZATION, ChatGptConfig.BEARER + apiKey); - return new HttpEntity<>(chatGptRequestDTO, httpHeaders); - } - - public ChatGptResponseDTO getResponse(HttpEntity chatGptRequestHttpEntity) { - - SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); - requestFactory.setConnectTimeout(60000); - requestFactory.setReadTimeout(60 * 1000); - restTemplate.setRequestFactory(requestFactory); - - ResponseEntity responseEntity = restTemplate.postForEntity( - ChatGptConfig.CHAT_URL, - chatGptRequestHttpEntity, - ChatGptResponseDTO.class); - - return responseEntity.getBody(); - } - - public ChatGptResponseDTO askQuestion(QuestionRequestDTO questionRequestDTO) { - List messages = new ArrayList<>(); - messages.add(ChatGptMessage.builder() - .role(ChatGptConfig.ROLE) - .content(questionRequestDTO.getQuestion()) - .build()); - System.out.println(messages); - return this.getResponse( - this.buildHttpEntity( - new ChatGptRequestDTO( - ChatGptConfig.CHAT_MODEL, - ChatGptConfig.MAX_TOKEN, - ChatGptConfig.TEMPERATURE, - ChatGptConfig.STREAM, - messages - ) - ) - ); - } -} \ No newline at end of file diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/service/GptService.java b/backend/src/main/java/com/isp/backend/domain/gpt/service/GptService.java new file mode 100644 index 00000000..a8c333ce --- /dev/null +++ b/backend/src/main/java/com/isp/backend/domain/gpt/service/GptService.java @@ -0,0 +1,73 @@ +package com.isp.backend.domain.gpt.service; + +import com.isp.backend.domain.gpt.config.GptConfig; +import com.isp.backend.domain.gpt.dto.GptRequestDTO; +import com.isp.backend.domain.gpt.dto.GptResponseDTO; +import com.isp.backend.domain.gpt.dto.GptScheduleRequestDto; +import com.isp.backend.domain.gpt.entity.GptMessage;import com.isp.backend.domain.gpt.mapper.GptMapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.ArrayList; +import java.util.List; + +@Slf4j +@RequiredArgsConstructor +@Service +public class GptService { + private final RestTemplate restTemplate; + private final GptMapper gptMapper; + + @Value("${api-key.chat-gpt}") + private String apiKey; + + public HttpEntity buildHttpEntity(GptRequestDTO gptRequestDTO) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.parseMediaType(GptConfig.MEDIA_TYPE)); + httpHeaders.add(GptConfig.AUTHORIZATION, GptConfig.BEARER + apiKey); + return new HttpEntity<>(gptRequestDTO, httpHeaders); + } + + public GptResponseDTO getResponse(HttpEntity chatGptRequestHttpEntity) { + + SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); + requestFactory.setConnectTimeout(60000); + requestFactory.setReadTimeout(60 * 1000); + restTemplate.setRequestFactory(requestFactory); + + ResponseEntity responseEntity = restTemplate.postForEntity( + GptConfig.CHAT_URL, + chatGptRequestHttpEntity, + GptResponseDTO.class); + + return responseEntity.getBody(); + } + + public GptResponseDTO askQuestion(GptScheduleRequestDto questionRequestDTO) { + List messages = new ArrayList<>(); + String question = questionRequestDTO.getDepartureDate(); + messages.add(GptMessage.builder() + .role(GptConfig.ROLE) + .content(question) + .build()); + return this.getResponse( + this.buildHttpEntity( + new GptRequestDTO( + GptConfig.CHAT_MODEL, + GptConfig.MAX_TOKEN, + GptConfig.TEMPERATURE, + GptConfig.STREAM, + messages + ) + ) + ); + } +} \ No newline at end of file From 1b752943a6187c4928d56d8b7d279938ee6e8bf5 Mon Sep 17 00:00:00 2001 From: Kim Jiyoon Date: Sat, 9 Mar 2024 22:28:54 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=EC=83=9D=EC=84=B1=ED=98=95=20?= =?UTF-8?q?=EC=9D=BC=EC=A0=95=20API=20=EC=9A=94=EC=B2=AD=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20DTO=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/gpt/dto/GptScheduleRequestDto.java | 15 +++++++++++++++ .../domain/gpt/dto/GptScheduleResponseDto.java | 13 +++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 backend/src/main/java/com/isp/backend/domain/gpt/dto/GptScheduleRequestDto.java create mode 100644 backend/src/main/java/com/isp/backend/domain/gpt/dto/GptScheduleResponseDto.java diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/dto/GptScheduleRequestDto.java b/backend/src/main/java/com/isp/backend/domain/gpt/dto/GptScheduleRequestDto.java new file mode 100644 index 00000000..3081f270 --- /dev/null +++ b/backend/src/main/java/com/isp/backend/domain/gpt/dto/GptScheduleRequestDto.java @@ -0,0 +1,15 @@ +package com.isp.backend.domain.gpt.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class GptScheduleRequestDto { + private String destination; + private String purpose; + private String departureDate; + private String returnDate; +} diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/dto/GptScheduleResponseDto.java b/backend/src/main/java/com/isp/backend/domain/gpt/dto/GptScheduleResponseDto.java new file mode 100644 index 00000000..78465c44 --- /dev/null +++ b/backend/src/main/java/com/isp/backend/domain/gpt/dto/GptScheduleResponseDto.java @@ -0,0 +1,13 @@ +package com.isp.backend.domain.gpt.dto; + +import com.isp.backend.domain.gpt.entity.GptSchedule; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class GptScheduleResponseDto { + private GptSchedule gptSchedule; +} From a58c31ca33f7b074eb39f31686c34e6634298c25 Mon Sep 17 00:00:00 2001 From: Kim Jiyoon Date: Sat, 9 Mar 2024 22:29:03 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=EC=9D=BC=EC=A0=95=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EA=B4=80=EB=A0=A8=20=EC=97=94=ED=8B=B0=ED=8B=B0=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/gpt/entity/GptSchedule.java | 18 ++++++++++++++++ .../domain/gpt/entity/GptScheduleDetail.java | 21 +++++++++++++++++++ .../backend/domain/gpt/mapper/GptMapper.java | 10 +++++++++ 3 files changed, 49 insertions(+) create mode 100644 backend/src/main/java/com/isp/backend/domain/gpt/entity/GptSchedule.java create mode 100644 backend/src/main/java/com/isp/backend/domain/gpt/entity/GptScheduleDetail.java create mode 100644 backend/src/main/java/com/isp/backend/domain/gpt/mapper/GptMapper.java diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/entity/GptSchedule.java b/backend/src/main/java/com/isp/backend/domain/gpt/entity/GptSchedule.java new file mode 100644 index 00000000..2d354829 --- /dev/null +++ b/backend/src/main/java/com/isp/backend/domain/gpt/entity/GptSchedule.java @@ -0,0 +1,18 @@ +package com.isp.backend.domain.gpt.entity; + +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@NoArgsConstructor +public class GptSchedule { + private List gptScheduleDetails; + + @Builder + public GptSchedule(List gptScheduleDetails) { + this.gptScheduleDetails = gptScheduleDetails; + } +} diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/entity/GptScheduleDetail.java b/backend/src/main/java/com/isp/backend/domain/gpt/entity/GptScheduleDetail.java new file mode 100644 index 00000000..cadd633b --- /dev/null +++ b/backend/src/main/java/com/isp/backend/domain/gpt/entity/GptScheduleDetail.java @@ -0,0 +1,21 @@ +package com.isp.backend.domain.gpt.entity; + +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; +import java.util.List; + +@Data +@NoArgsConstructor +public class GptScheduleDetail { + private LocalDateTime day; + private List details; + + @Builder + public GptScheduleDetail(LocalDateTime day, List details) { + this.day = day; + this.details = details; + } +} diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/mapper/GptMapper.java b/backend/src/main/java/com/isp/backend/domain/gpt/mapper/GptMapper.java new file mode 100644 index 00000000..7f504092 --- /dev/null +++ b/backend/src/main/java/com/isp/backend/domain/gpt/mapper/GptMapper.java @@ -0,0 +1,10 @@ +package com.isp.backend.domain.gpt.mapper; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class GptMapper { + +} From 407c7fb4d7b9e30a392b90f4c7d71247a46cbda6 Mon Sep 17 00:00:00 2001 From: Kim Jiyoon Date: Sat, 9 Mar 2024 22:30:14 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20import=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=98=A4=ED=83=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/isp/backend/domain/gpt/service/GptService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/service/GptService.java b/backend/src/main/java/com/isp/backend/domain/gpt/service/GptService.java index a8c333ce..f02c70fb 100644 --- a/backend/src/main/java/com/isp/backend/domain/gpt/service/GptService.java +++ b/backend/src/main/java/com/isp/backend/domain/gpt/service/GptService.java @@ -4,7 +4,8 @@ import com.isp.backend.domain.gpt.dto.GptRequestDTO; import com.isp.backend.domain.gpt.dto.GptResponseDTO; import com.isp.backend.domain.gpt.dto.GptScheduleRequestDto; -import com.isp.backend.domain.gpt.entity.GptMessage;import com.isp.backend.domain.gpt.mapper.GptMapper; +import com.isp.backend.domain.gpt.entity.GptMessage; +import com.isp.backend.domain.gpt.mapper.GptMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value;