Skip to content

Commit

Permalink
Merge pull request #92 from tukcomCD2024/backend/feature/91-add-locat…
Browse files Browse the repository at this point in the history
…ion-coordinate

[Backend : Feat] 비동기 처리를 통한 GPT 응답 시간 단축
  • Loading branch information
yo0oni authored Jun 16, 2024
2 parents c2ae832 + df86669 commit 16a6820
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 78 deletions.
4 changes: 3 additions & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -53,4 +55,4 @@ dependencies {

tasks.named('test') {
useJUnitPlatform()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
우리는 매일 점심과 저녁은 항상 식당에 가서 먹을거야. 반드시 실제로 존재하는 맛있고 유명한 식당으로 추천해줘
금액 정보는 알려주지마. 대신 다양한 경험을 할 수 있게 일정을 만들어줘""";
금액 정보는 알려주지마. 대신 다양한 경험을 할 수 있게 일정을 만들어줘.
그리고 일정 옆에 장소의 위도 경도 좌표값 소수점 여덟자리까지 함께 작성해줘.""";
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletableFuture;

@RequiredArgsConstructor
@RequestMapping("/gpt")
@RestController
Expand All @@ -15,7 +17,7 @@ public class GptController {
private final GptService gptService;

@PostMapping("/schedules")
public GptSchedulesResponse sendQuestion(@RequestBody GptScheduleRequest gptScheduleRequest) {
public CompletableFuture<GptSchedulesResponse> sendQuestion(@RequestBody GptScheduleRequest gptScheduleRequest) {
return gptService.askQuestion(gptScheduleRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GptRequest> 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<GptRequest> chatGptRequestHttpEntity) {

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(60000);
requestFactory.setReadTimeout(60 * 1000);
restTemplate.setRequestFactory(requestFactory);

ResponseEntity<GptResponse> responseEntity = restTemplate.postForEntity(
GptConfig.CHAT_URL,
chatGptRequestHttpEntity,
GptResponse.class);
public GptScheduleResponse getResponse(HttpHeaders headers, GptRequest gptRequest) {

List<GptSchedule> 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<GptSchedule> gptSchedules = gptScheduleParser.parseScheduleText(getScheduleText(response));
return new GptScheduleResponse(gptSchedules);
}

private String getScheduleText(ResponseEntity<GptResponse> responseEntity) {
return getGptMessage(responseEntity).toString();
private String getScheduleText(GptResponse gptResponse) {
return getGptMessage(gptResponse).toString();
}

private GptMessage getGptMessage(ResponseEntity<GptResponse> 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<GptSchedulesResponse> askQuestion(GptScheduleRequest questionRequestDTO) {
String question = makeQuestion(questionRequestDTO);
List<GptMessage> messages = Collections.singletonList(
GptMessage.builder()
Expand All @@ -81,26 +76,43 @@ public GptSchedulesResponse askQuestion(GptScheduleRequest questionRequestDTO) {

Country country = scheduleService.validateCountry(questionRequestDTO.getDestination());
String countryImage = country.getImageUrl();
List<GptScheduleResponse> 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<CompletableFuture<GptScheduleResponse>> futures = Arrays.asList(
sendRequestAsync(apiKey, messages, executorService),
sendRequestAsync(apiKey, messages, executorService),
sendRequestAsync(apiKey, messages, executorService)
);

CompletableFuture<List<GptScheduleResponse>> 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<GptScheduleResponse> sendRequestAsync(String apiKey, List<GptMessage> messages, ExecutorService executor) {
HttpHeaders headers = buildHttpHeaders(apiKey);
GptRequest request = getGptRequest(messages);
return CompletableFuture.supplyAsync(() -> getResponse(headers, request), executor);
}

private GptRequest getGptRequest(List<GptMessage> 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) {
Expand All @@ -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())
);
}
}
2 changes: 1 addition & 1 deletion backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 16a6820

Please sign in to comment.