Skip to content

Commit

Permalink
feat: 문자열 응답을 JSON으로 변환하는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yo0oni committed Mar 9, 2024
1 parent 0d95d03 commit df048b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.isp.backend.domain.gpt.entity;

import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

Expand All @@ -9,10 +8,11 @@
@Data
@NoArgsConstructor
public class GptSchedule {
private List<GptScheduleDetail> gptScheduleDetails;
private String date;
private List<String> scheduleDetail;

@Builder
public GptSchedule(List<GptScheduleDetail> gptScheduleDetails) {
this.gptScheduleDetails = gptScheduleDetails;
public GptSchedule(String date, List<String> scheduleDetail) {
this.date = date;
this.scheduleDetail = scheduleDetail;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
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.dto.GptScheduleResponseDto;
import com.isp.backend.domain.gpt.entity.GptMessage;
import com.isp.backend.domain.gpt.mapper.GptMapper;
import com.isp.backend.domain.gpt.entity.GptSchedule;
import com.isp.backend.domain.gpt.mapper.JsonMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -25,7 +27,7 @@
@Service
public class GptService {
private final RestTemplate restTemplate;
private final GptMapper gptMapper;
private final JsonMapper jsonMapper;

@Value("${api-key.chat-gpt}")
private String apiKey;
Expand All @@ -37,7 +39,7 @@ public HttpEntity<GptRequestDTO> buildHttpEntity(GptRequestDTO gptRequestDTO) {
return new HttpEntity<>(gptRequestDTO, httpHeaders);
}

public GptResponseDTO getResponse(HttpEntity<GptRequestDTO> chatGptRequestHttpEntity) {
public GptScheduleResponseDto getResponse(HttpEntity<GptRequestDTO> chatGptRequestHttpEntity) {

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(60000);
Expand All @@ -48,11 +50,27 @@ public GptResponseDTO getResponse(HttpEntity<GptRequestDTO> chatGptRequestHttpEn
GptConfig.CHAT_URL,
chatGptRequestHttpEntity,
GptResponseDTO.class);
List<GptSchedule> gptSchedules = jsonMapper.parseScheduleText(getScheduleText(responseEntity));
return new GptScheduleResponseDto(gptSchedules);
}

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

private GptMessage getGptMessage(ResponseEntity<GptResponseDTO> responseEntity) {
return getChoices(responseEntity).get(0).getMessage();
}

private List<GptResponseDTO.Choice> getChoices(ResponseEntity<GptResponseDTO> responseEntity) {
return getBody(responseEntity).getChoices();
}

private GptResponseDTO getBody(ResponseEntity<GptResponseDTO> responseEntity) {
return responseEntity.getBody();
}

public GptResponseDTO askQuestion(GptScheduleRequestDto questionRequestDTO) {
public GptScheduleResponseDto askQuestion(GptScheduleRequestDto questionRequestDTO) {
List<GptMessage> messages = new ArrayList<>();
String question = makeQuestion(questionRequestDTO);
messages.add(GptMessage.builder()
Expand Down

0 comments on commit df048b3

Please sign in to comment.