Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/backend/feature/42-gpt-api-respo…
Browse files Browse the repository at this point in the history
…nse-dto' into dev_backend
  • Loading branch information
Dayon-Hong committed Mar 9, 2024
2 parents 9157b59 + 407c7fb commit deeb535
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChatGptConfig {
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";
Expand Down

This file was deleted.

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());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.isp.backend.domain.gpt.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.isp.backend.domain.gpt.entity.ChatGptMessage;
import com.isp.backend.domain.gpt.entity.GptMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -11,12 +11,12 @@
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ChatGptRequestDTO {
public class GptRequestDTO {
private String model;

@JsonProperty("max_tokens")
private Integer maxTokens;
private Double temperature;
private Boolean stream;
private List<ChatGptMessage> messages;
private List<GptMessage> messages;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.isp.backend.domain.gpt.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.isp.backend.domain.gpt.entity.ChatGptMessage;
import com.isp.backend.domain.gpt.entity.GptMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -12,7 +12,7 @@
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ChatGptResponseDTO {
public class GptResponseDTO {
private String id;
private String object;
private long created;
Expand All @@ -34,7 +34,7 @@ public static class Usage {
@Getter
@Setter
public static class Choice {
private ChatGptMessage message;
private GptMessage message;
@JsonProperty("finish_reason")
private String finishReason;
private int index;
Expand Down
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;
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

@Data
@NoArgsConstructor
public class ChatGptMessage implements Serializable {
public class GptMessage implements Serializable {
private String role;
private String content;

@Builder
public ChatGptMessage(String role, String content) {
public GptMessage(String role, String content) {
this.role = role;
this.content = content;
}
Expand Down
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;
}
}
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;
}
}
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 {

}

This file was deleted.

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
)
)
);
}
}

0 comments on commit deeb535

Please sign in to comment.