Skip to content

Commit

Permalink
Merge pull request #69 from tukcomCD2024/backend/61-backend-feat-gpt-…
Browse files Browse the repository at this point in the history
…40-turbo-api

[Backend : Feat] Gpt-4.0 Turbo API 호출 및 응답 개수 변경
  • Loading branch information
yo0oni authored May 12, 2024
2 parents 50e1d5f + cbb8911 commit 8e0e342
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
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-16k";
public static final Integer MAX_TOKEN = 3000;
public static final String CHAT_MODEL = "gpt-4-turbo";
public static final Integer MAX_TOKEN = 4095;
public static final Boolean STREAM = false;
public static final String ROLE = "user";
public static final Double TEMPERATURE = 0.6;
public static final Double TEMPERATURE = 1.5;
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.
I would like you to plan a package tour program to plan your travel itinerary.
Also, plan your itinerary based on the travel distance and famous tourist destinations.
If possible, please also provide the cost.
Also, be sure to include the name of the landmark or location.
I want to visit a maximum of three attractions per day.
Make sure to plan for at least 10 hours of activity each day.
Please write the itinerary without using any special characters, only in plain text.
I would like the format to be as follows:
For example
Expand Down Expand Up @@ -60,5 +62,8 @@ public class GptConfig {
한국에 귀국하는 날짜: %s
No need to say anything else, just plan your schedule right away.
Please create the result in Korean.""";
Please create the result in Korean.
And only include schedule-related content in the schedule you're creating.
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
@@ -1,7 +1,7 @@
package com.isp.backend.domain.gpt.controller;

import com.isp.backend.domain.gpt.dto.request.GptScheduleRequest;
import com.isp.backend.domain.gpt.dto.response.GptScheduleResponse;
import com.isp.backend.domain.gpt.dto.response.GptSchedulesResponse;
import com.isp.backend.domain.gpt.service.GptService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -15,7 +15,7 @@ public class GptController {
private final GptService gptService;

@PostMapping("/schedules")
public GptScheduleResponse sendQuestion(@RequestBody GptScheduleRequest gptScheduleRequest) {
public GptSchedulesResponse sendQuestion(@RequestBody GptScheduleRequest gptScheduleRequest) {
return gptService.askQuestion(gptScheduleRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
@NoArgsConstructor
@AllArgsConstructor
public class GptScheduleResponse {
private String countryImage;
private List<GptSchedule> schedules;
private List<GptSchedule> schedule;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.isp.backend.domain.gpt.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class GptSchedulesResponse {
private String countryImage;
private List<GptScheduleResponse> schedules;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.isp.backend.domain.gpt.config.GptConfig;
import com.isp.backend.domain.gpt.constant.ParsingConstants;
import com.isp.backend.domain.gpt.dto.request.GptRequest;
import com.isp.backend.domain.gpt.dto.response.GptResponse;
import com.isp.backend.domain.gpt.dto.request.GptScheduleRequest;
import com.isp.backend.domain.gpt.dto.response.GptResponse;
import com.isp.backend.domain.gpt.dto.response.GptScheduleResponse;
import com.isp.backend.domain.gpt.dto.response.GptSchedulesResponse;
import com.isp.backend.domain.gpt.entity.GptMessage;
import com.isp.backend.domain.gpt.entity.GptSchedule;
import com.isp.backend.domain.gpt.entity.GptScheduleParser;
Expand All @@ -21,6 +22,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand All @@ -42,7 +44,7 @@ public HttpEntity<GptRequest> buildHttpEntity(GptRequest gptRequest) {
return new HttpEntity<>(gptRequest, httpHeaders);
}

public GptScheduleResponse getResponse(String destination, HttpEntity<GptRequest> chatGptRequestHttpEntity) {
public GptScheduleResponse getResponse(HttpEntity<GptRequest> chatGptRequestHttpEntity) {

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(60000);
Expand All @@ -53,10 +55,9 @@ public GptScheduleResponse getResponse(String destination, HttpEntity<GptRequest
GptConfig.CHAT_URL,
chatGptRequestHttpEntity,
GptResponse.class);
String countryImage = s3ImageService.get(destination);
List<GptSchedule> gptSchedules = gptScheduleParser.parseScheduleText(getScheduleText(responseEntity));

return new GptScheduleResponse(countryImage, gptSchedules);
return new GptScheduleResponse(gptSchedules);
}

private String getScheduleText(ResponseEntity<GptResponse> responseEntity) {
Expand All @@ -67,7 +68,7 @@ private GptMessage getGptMessage(ResponseEntity<GptResponse> responseEntity) {
return responseEntity.getBody().getChoices().get(0).getMessage();
}

public GptScheduleResponse askQuestion(GptScheduleRequest questionRequestDTO) {
public GptSchedulesResponse askQuestion(GptScheduleRequest questionRequestDTO) {
String question = makeQuestion(questionRequestDTO);
List<GptMessage> messages = Collections.singletonList(
GptMessage.builder()
Expand All @@ -76,19 +77,27 @@ public GptScheduleResponse askQuestion(GptScheduleRequest questionRequestDTO) {
.build()
);


return this.getResponse(
questionRequestDTO.getDestination(),
this.buildHttpEntity(
new GptRequest(
GptConfig.CHAT_MODEL,
GptConfig.MAX_TOKEN,
GptConfig.TEMPERATURE,
GptConfig.STREAM,
messages
String countryImage = s3ImageService.get(questionRequestDTO.getDestination());
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);
}

private String makeQuestion(GptScheduleRequest questionRequestDTO) {
Expand Down

0 comments on commit 8e0e342

Please sign in to comment.