1๏ธโฃ API Key ์์ฑ
โก๏ธ API ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ API Key ๋ฅผ ๋ฐ๊ธ๋ฐ์์ผํ๋ค.
OpenAI API
An API for accessing new AI models developed by OpenAI
platform.openai.com

โก๏ธ ์์ฑ๋ ํค๋ Client ์ฝ๋ ์ชฝ์์ ๋ ธ์ถ๋๋ฉด ์๋๋ค.
2๏ธโฃ API Parameter ์ Authentication
โ API Parameter
โ๏ธ model
OpenAI API
An API for accessing new AI models developed by OpenAI
platform.openai.com

์ต๊ทผ์ gpt-4 ๊ฐ ๋์์ง๋ง ๋๋ gpt-3.5-turbo ๋ฅผ ์ฌ์ฉํ๊ธฐ๋ก ํ๋ค.
โ๏ธ Message
- role : message ์ก์์ ์
- user : message ์ก์ ์
- assistant : message ์์ ์
- content : ChatGPT ์ ๋ณด๋ด๋ ์ค์ ๋ด์ฉ
โก Authentication
โก๏ธ ์์ ๋ฐ๊ธํ API Key ๋ฅผ ํค๋์ ๋ฃ์ด์ ์์ฒญ์ ๋ณด๋ธ๋ค.
โข ChatGPT ์ ๋ณด๋ด๋ ์์ฒญ ์์
$ curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'
โฃ API ์ ์ถ๊ฐ์ ์ผ๋ก ๋ณด๋ผ ์ ์๋ ํ๋ผ๋ฏธํฐ (Optional Parameters)
- n : ์์ฑํ ์๋ต์ ๊ฐ์ (default : 1)
- temperature : ์๋ต์ ๋ฌด์์์ฑ (default : 1)
- max_tokens : ์๋ต ํ ํฐ ์์ ์ ํ (default : infinity)
3๏ธโฃ API ์๋ต ํํ
โก๏ธ json ํํ๋ก ๋์ด์จ๋ค. Object ์ ๋ฐฐ์ด ํํ๋ก ์ค๋๋ฐ, n ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ๋ก ์ ํด๋์ง ์์ผ๋ฉด 1๊ฐ์ Object ๋ง ์จ๋ค.
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
4๏ธโฃ ํ๋ก์ ํธ ์ ์ฉ _ (1) Request & Response DTO ์์ฑ
โ Message
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Message {
private String role;
private String content;
}
โก ChatReq
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
@Getter
public class ChatReq {
private String model;
private List<Message> messages;
public ChatReq (String model, String prompt) {
this.model = model;
this.messages = new ArrayList<>();
this.messages.add(new Message("user", prompt));
}
}
โข ChatRes
import com.inha.server.chatGPT.dto.Message;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ChatRes {
private List<Choice> choices;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static class Choice {
private int index;
private Message message;
}
}
5๏ธโฃ ํ๋ก์ ํธ ์ ์ฉ _ (2) : application.yml ํ์ผ ์์ฑ
โ application.yml
openai :
model : gpt-3.5-turbo
api :
url : {์ ํํ model ์ endpoint}
key : {์์ ๋ฐ๊ธ๋ฐ์ API Key}
6๏ธโฃ ํ๋ก์ ํธ ์ ์ฉ _ (3) : RestTemplate ์์ฑ
โ OpenAIRestTemplateConfig
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class OpenAIRestTemplateConfig {
@Value("${openai.api.key}")
private String openaiApiKey;
@Bean
@Qualifier("openaiRestTemplate")
public RestTemplate openaiRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(
(request, body, execution)
-> {
request.getHeaders().add("Authorization", "Bearer " + openaiApiKey);
return execution.execute(request, body);
});
return restTemplate;
}
}
7๏ธโฃ ํ๋ก์ ํธ ์ ์ฉ _ (4) : Controller ์์ฑ
โ ChatController
import com.inha.server.chatGPT.dto.request.ChatReq;
import com.inha.server.chatGPT.dto.response.ChatRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ChatController {
@Qualifier("openaiRestTemplate")
@Autowired
private RestTemplate restTemplate;
@Value("${openai.model}")
private String model;
@Value("${openai.api.url}")
private String apiUrl;
@GetMapping("/chat")
public String chat(@RequestParam String prompt) {
ChatReq request = new ChatReq(model, prompt);
// api ํธ์ถ
ChatRes response = restTemplate.postForObject(apiUrl, request, ChatRes.class);
if (response == null || response.getChoices() == null || response.getChoices().isEmpty()) {
return "No response";
}
return response.getChoices().get(0).getMessage().getContent();
}
}
8๏ธโฃ ์๋ต ํ์ธ
โก๏ธ Postman ์ ํตํด ์๋ต์ ํ์ธํ๋ค.

๐ ์ฐธ๊ณ
https://www.baeldung.com/spring-boot-chatgpt-api-openai