-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/backend/feature/42-gpt-api-respo…
…nse-dto' into dev_backend
- Loading branch information
Showing
13 changed files
with
185 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
backend/src/main/java/com/isp/backend/domain/gpt/controller/ChatGptController.java
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/com/isp/backend/domain/gpt/controller/GptController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("/chat-gpt") | ||
@RestController | ||
public class GptController { | ||
private final GptService gptService; | ||
|
||
@PostMapping("/question") | ||
public ResponseEntity<GptMessage> sendQuestion(@RequestBody GptScheduleRequestDto gptScheduleRequestDto) { | ||
GptResponseDTO gptResponseDTO = gptService.askQuestion(gptScheduleRequestDto); | ||
return ResponseEntity.ok(gptResponseDTO.getChoices().get(0).getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/com/isp/backend/domain/gpt/dto/GptScheduleRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
13 changes: 13 additions & 0 deletions
13
backend/src/main/java/com/isp/backend/domain/gpt/dto/GptScheduleResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/com/isp/backend/domain/gpt/entity/GptSchedule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GptScheduleDetail> gptScheduleDetails; | ||
|
||
@Builder | ||
public GptSchedule(List<GptScheduleDetail> gptScheduleDetails) { | ||
this.gptScheduleDetails = gptScheduleDetails; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/com/isp/backend/domain/gpt/entity/GptScheduleDetail.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> details; | ||
|
||
@Builder | ||
public GptScheduleDetail(LocalDateTime day, List<String> details) { | ||
this.day = day; | ||
this.details = details; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/isp/backend/domain/gpt/mapper/GptMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.isp.backend.domain.gpt.mapper; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class GptMapper { | ||
|
||
} |
72 changes: 0 additions & 72 deletions
72
backend/src/main/java/com/isp/backend/domain/gpt/service/ChatGptService.java
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
backend/src/main/java/com/isp/backend/domain/gpt/service/GptService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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<GptRequestDTO> 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<GptRequestDTO> chatGptRequestHttpEntity) { | ||
|
||
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); | ||
requestFactory.setConnectTimeout(60000); | ||
requestFactory.setReadTimeout(60 * 1000); | ||
restTemplate.setRequestFactory(requestFactory); | ||
|
||
ResponseEntity<GptResponseDTO> responseEntity = restTemplate.postForEntity( | ||
GptConfig.CHAT_URL, | ||
chatGptRequestHttpEntity, | ||
GptResponseDTO.class); | ||
|
||
return responseEntity.getBody(); | ||
} | ||
|
||
public GptResponseDTO askQuestion(GptScheduleRequestDto questionRequestDTO) { | ||
List<GptMessage> 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 | ||
) | ||
) | ||
); | ||
} | ||
} |