diff --git a/backend/build.gradle b/backend/build.gradle index 96e4b9a1..cafd748d 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -40,6 +40,8 @@ dependencies { implementation platform("io.awspring.cloud:spring-cloud-aws-dependencies:3.0.2") implementation 'io.awspring.cloud:spring-cloud-aws-starter-s3' + implementation 'org.springframework.boot:spring-boot-starter-webflux' + implementation 'io.netty:netty-resolver-dns-native-macos:4.1.68.Final:osx-aarch_64' // Open API implementation "com.amadeus:amadeus-java:8.0.0" @@ -53,4 +55,4 @@ dependencies { tasks.named('test') { useJUnitPlatform() -} +} \ No newline at end of file diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/config/GptConfig.java b/backend/src/main/java/com/isp/backend/domain/gpt/config/GptConfig.java index f8098ae2..f249989f 100644 --- a/backend/src/main/java/com/isp/backend/domain/gpt/config/GptConfig.java +++ b/backend/src/main/java/com/isp/backend/domain/gpt/config/GptConfig.java @@ -11,7 +11,6 @@ public class GptConfig { public static final Boolean STREAM = false; public static final String ROLE = "user"; public static final Double TEMPERATURE = 1.0; - public static final String MEDIA_TYPE = "application/json; charset=UTF-8"; public static final String CHAT_URL = "https://api.openai.com/v1/chat/completions"; public static final String PROMPT = """ You're a great travel agency staff member. @@ -27,31 +26,31 @@ public class GptConfig { For example --- 2024-02-06 - 1. Go to location - 2. See the place - 3. Eat the Lunch - 4. Go to location - 5. See the place - 6. Eat the dinner - 7. shopping + 1. Go to location 26.69549000, 127.87642000 + 2. See the place 26.69549000, 127.87642000 + 3. Eat the Lunch 26.69549000, 127.87642000 + 4. Go to location 26.69549000, 127.87642000 + 5. See the place 26.69549000, 127.87642000 + 6. Eat the dinner 26.69549000, 127.87642000 + 7. shopping 26.69549000, 127.87642000 2024-02-07 - 1. Go to location - 2. See the place - 3. Eat the Lunch - 4. Go to location - 5. See the place - 6. Eat the dinner - 7. shopping + 1. Go to location 26.69549000, 127.87642000 + 2. See the place 26.69549000, 127.87642000 + 3. Eat the Lunch 26.69549000, 127.87642000 + 4. Go to location 26.69549000, 127.87642000 + 5. See the place 26.69549000, 127.87642000 + 6. Eat the dinner 26.69549000, 127.87642000 + 7. shopping 26.69549000, 127.87642000 2024-02-08 - 1. Go to location - 2. See the place - 3. Eat the Lunch - 4. Go to location - 5. See the place - 6. Eat the dinner - 7. shopping + 1. Go to location 26.69549000, 127.87642000 + 2. See the place 26.69549000, 127.87642000 + 3. Eat the Lunch 26.69549000, 127.87642000 + 4. Go to location 26.69549000, 127.87642000 + 5. See the place 26.69549000, 127.87642000 + 6. Eat the dinner 26.69549000, 127.87642000 + 7. shopping 26.69549000, 127.87642000 --- 여행지: %s @@ -67,5 +66,6 @@ public class GptConfig { Do not add any information I haven't provided to you. Under no circumstances should you include any activities other than traveling. Absolutely not. 우리는 매일 점심과 저녁은 항상 식당에 가서 먹을거야. 반드시 실제로 존재하는 맛있고 유명한 식당으로 추천해줘 - 금액 정보는 알려주지마. 대신 다양한 경험을 할 수 있게 일정을 만들어줘"""; + 금액 정보는 알려주지마. 대신 다양한 경험을 할 수 있게 일정을 만들어줘. + 그리고 일정 옆에 장소의 위도 경도 좌표값 소수점 여덟자리까지 함께 작성해줘."""; } \ No newline at end of file diff --git a/backend/src/main/java/com/isp/backend/domain/gpt/config/WebClientConfig.java b/backend/src/main/java/com/isp/backend/domain/gpt/config/WebClientConfig.java new file mode 100644 index 00000000..f6500b65 --- /dev/null +++ b/backend/src/main/java/com/isp/backend/domain/gpt/config/WebClientConfig.java @@ -0,0 +1,26 @@ +package com.isp.backend.domain.gpt.config; + +import io.netty.channel.ChannelOption; +import io.netty.handler.timeout.ReadTimeoutHandler; +import io.netty.handler.timeout.WriteTimeoutHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.netty.http.client.HttpClient; + + +@Configuration +public class WebClientConfig { + @Bean + public WebClient webClient() { + HttpClient httpClient = HttpClient.create() + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000) + .doOnConnected(conn -> conn + .addHandlerLast(new ReadTimeoutHandler(10)) + .addHandlerLast(new WriteTimeoutHandler(10))); + return WebClient.builder() + .clientConnector(new ReactorClientHttpConnector(httpClient)) + .build(); + } +} \ 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 index 12f6d6f8..04dade00 100644 --- 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 @@ -7,6 +7,8 @@ import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; +import java.util.concurrent.CompletableFuture; + @RequiredArgsConstructor @RequestMapping("/gpt") @RestController @@ -15,7 +17,7 @@ public class GptController { private final GptService gptService; @PostMapping("/schedules") - public GptSchedulesResponse sendQuestion(@RequestBody GptScheduleRequest gptScheduleRequest) { + public CompletableFuture sendQuestion(@RequestBody GptScheduleRequest gptScheduleRequest) { return gptService.askQuestion(gptScheduleRequest); } } \ 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 index f170e67e..56104236 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 @@ -15,62 +15,57 @@ 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.scheduling.annotation.Async; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; @Slf4j @RequiredArgsConstructor @Service public class GptService { - private final RestTemplate restTemplate; private final GptScheduleParser gptScheduleParser; private final ScheduleService scheduleService; + private final WebClient webClient; - @Value("${api-key.chat-gpt}") + @Value("${api-key.gpt-trip}") private String apiKey; - public HttpEntity buildHttpEntity(GptRequest gptRequest) { - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.parseMediaType(GptConfig.MEDIA_TYPE)); - httpHeaders.add(GptConfig.AUTHORIZATION, GptConfig.BEARER + apiKey); - return new HttpEntity<>(gptRequest, httpHeaders); - } - - public GptScheduleResponse 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, - GptResponse.class); + public GptScheduleResponse getResponse(HttpHeaders headers, GptRequest gptRequest) { - List gptSchedules = gptScheduleParser.parseScheduleText(getScheduleText(responseEntity)); + GptResponse response = webClient.post() + .uri(GptConfig.CHAT_URL) + .headers(h -> h.addAll(headers)) + .contentType(MediaType.APPLICATION_JSON) + .body(BodyInserters.fromValue(gptRequest)) + .retrieve() + .bodyToMono(GptResponse.class) + .block(); + List gptSchedules = gptScheduleParser.parseScheduleText(getScheduleText(response)); return new GptScheduleResponse(gptSchedules); } - private String getScheduleText(ResponseEntity responseEntity) { - return getGptMessage(responseEntity).toString(); + private String getScheduleText(GptResponse gptResponse) { + return getGptMessage(gptResponse).toString(); } - private GptMessage getGptMessage(ResponseEntity responseEntity) { - return responseEntity.getBody().getChoices().get(0).getMessage(); + private GptMessage getGptMessage(GptResponse gptResponse) { + return gptResponse.getChoices().get(0).getMessage(); } - public GptSchedulesResponse askQuestion(GptScheduleRequest questionRequestDTO) { + @Async + public CompletableFuture askQuestion(GptScheduleRequest questionRequestDTO) { String question = makeQuestion(questionRequestDTO); List messages = Collections.singletonList( GptMessage.builder() @@ -81,26 +76,43 @@ public GptSchedulesResponse askQuestion(GptScheduleRequest questionRequestDTO) { Country country = scheduleService.validateCountry(questionRequestDTO.getDestination()); String countryImage = country.getImageUrl(); - List schedules = new ArrayList<>(); - for(int i = 0; i < 3; i++) { - schedules.add( - this.getResponse( - this.buildHttpEntity( - new GptRequest( - GptConfig.CHAT_MODEL, - GptConfig.MAX_TOKEN, - GptConfig.TEMPERATURE, - GptConfig.STREAM, - messages - ) - ) - )); - } - - - - - return new GptSchedulesResponse(countryImage, schedules); + + ExecutorService executorService = Executors.newFixedThreadPool(5); + List> futures = Arrays.asList( + sendRequestAsync(apiKey, messages, executorService), + sendRequestAsync(apiKey, messages, executorService), + sendRequestAsync(apiKey, messages, executorService) + ); + + CompletableFuture> combinedFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) + .thenApply(v -> futures.stream() + .map(CompletableFuture::join) + .collect(Collectors.toList())); + + return combinedFuture.thenApply(schedules -> new GptSchedulesResponse(countryImage, schedules)); + } + + private CompletableFuture sendRequestAsync(String apiKey, List messages, ExecutorService executor) { + HttpHeaders headers = buildHttpHeaders(apiKey); + GptRequest request = getGptRequest(messages); + return CompletableFuture.supplyAsync(() -> getResponse(headers, request), executor); + } + + private GptRequest getGptRequest(List messages) { + return new GptRequest( + GptConfig.CHAT_MODEL, + GptConfig.MAX_TOKEN, + GptConfig.TEMPERATURE, + GptConfig.STREAM, + messages + ); + } + + private HttpHeaders buildHttpHeaders(String key) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + httpHeaders.add(GptConfig.AUTHORIZATION, GptConfig.BEARER + key); + return httpHeaders; } private String makeQuestion(GptScheduleRequest questionRequestDTO) { @@ -111,7 +123,7 @@ private String makeQuestion(GptScheduleRequest questionRequestDTO) { questionRequestDTO.getExcludedActivities(), questionRequestDTO.getDepartureDate(), questionRequestDTO.getReturnDate(), - String.join(ParsingConstants.COMMA, questionRequestDTO.getPurpose()) + String.join(ParsingConstants.COMMA, questionRequestDTO.getPurpose()) ); } } \ No newline at end of file diff --git a/backend/src/main/resources/application.yml b/backend/src/main/resources/application.yml index df76f102..87d1c333 100644 --- a/backend/src/main/resources/application.yml +++ b/backend/src/main/resources/application.yml @@ -34,7 +34,7 @@ jwt: secret: ${JWT_SECRET_KEY} api-key: - chat-gpt: ${GPT_API_KEY} + gpt-trip: ${GPT_API_KEY} amadeus: accessKey: ${AMADEUS_ACCESS_KEY} secretKey: ${AMADEUS_SECRET_KEY}