https://platform.openai.com/account/api-keys
사이트 들어가서 open api key를 발급받는다.
key의 유효시간은 하루라고 알고 있음.
https://velog.io/@shinyounseob/Spring-Boot-ChatGPT-Tutorial
위 블로그 내용을 참고함.
//Config
public static final String AUTHORIZATION = "Authorization";
public static final String BEARER = "Bearer ";
public static final String API_KEY = "발급받은 key 적어주기";
public static final String MODEL = "text-davinci-003";
public static final Integer MAX_TOKEN = 300;
public static final Double TEMPERATURE = 0.0;
public static final Double TOP_P = 1.0;
public static final String MEDIA_TYPE = "application/json; charset=UTF-8";
public static final String URL = "https://api.openai.com/v1/completions";
//Controller
@RestController
@RequestMapping("/chat-gpt")
public class openAIController {
private final ChatGptService chatGptService;
public openAIController(ChatGptService chatGptService) {
this.chatGptService = chatGptService;
}
@PostMapping("/question")
public ChatGptResponseDto sendQuestion(@RequestBody QuestionRequestDto requestDto) {
return chatGptService.askQuestion(requestDto);
}
}
//DTO
//ChatGptRequestDto
@Getter
@NoArgsConstructor
public class ChatGptRequestDto implements Serializable {
private String model;
private String prompt;
@JsonProperty("max_tokens")
private Integer maxTokens;
private Double temperature;
@JsonProperty("top_p")
private Double topP;
@Builder
public ChatGptRequestDto(String model, String prompt,
Integer maxTokens, Double temperature,
Double topP) {
this.model = model;
this.prompt = prompt;
this.maxTokens = maxTokens;
this.temperature = temperature;
this.topP = topP;
}
}
//ChatGptResponseDto
@Getter
@NoArgsConstructor
public class ChatGptResponseDto implements Serializable {
private String id;
private String object;
private LocalDate created;
private String model;
private List<Choice> choices;
@Builder
public ChatGptResponseDto(String id, String object,
LocalDate created, String model,
java.util.List<Choice> choices) {
this.id = id;
this.object = object;
this.created = created;
this.model = model;
this.choices = choices;
}
}
//Choice
@Getter
@NoArgsConstructor
public class Choice implements Serializable {
private String text;
private Integer index;
@JsonProperty("finish_reason")
private String finishReason;
@Builder
public Choice(String text, Integer index, String finishReason) {
this.text = text;
this.index = index;
this.finishReason = finishReason;
}
}
//QuestionRequestDto
@Getter
public class QuestionRequestDto implements Serializable {
private String question;
}
//Service
@Service
public class ChatGptService {
private static RestTemplate restTemplate = new RestTemplate();
public HttpEntity<ChatGptRequestDto> buildHttpEntity(ChatGptRequestDto requestDto) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType(ChatGptConfig.MEDIA_TYPE));
headers.add(ChatGptConfig.AUTHORIZATION, ChatGptConfig.BEARER + ChatGptConfig.API_KEY);
return new HttpEntity<>(requestDto, headers);
}
public ChatGptResponseDto getResponse(HttpEntity<ChatGptRequestDto> chatGptRequestDtoHttpEntity) {
ResponseEntity<ChatGptResponseDto> responseEntity = restTemplate.postForEntity(
ChatGptConfig.URL,
chatGptRequestDtoHttpEntity,
ChatGptResponseDto.class);
return responseEntity.getBody();
}
public ChatGptResponseDto askQuestion(QuestionRequestDto requestDto) {
return this.getResponse(
this.buildHttpEntity(
new ChatGptRequestDto(
ChatGptConfig.MODEL,
requestDto.getQuestion(),
ChatGptConfig.MAX_TOKEN,
ChatGptConfig.TEMPERATURE,
ChatGptConfig.TOP_P
)
)
);
}
}
잘 나온다.
AI주제에... 비웃다니..