diff --git a/src/main/java/com/splanet/splanet/config/OpenAIConfig.java b/src/main/java/com/splanet/splanet/config/OpenAIConfig.java deleted file mode 100644 index 974a3739..00000000 --- a/src/main/java/com/splanet/splanet/config/OpenAIConfig.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.splanet.splanet.config; - -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 OpenAIConfig { - @Value("${spring.ai.gpt.api-key}") - private String apiKey; - @Bean - public RestTemplate template(){ - RestTemplate restTemplate = new RestTemplate(); - restTemplate.getInterceptors().add((request, body, execution) -> { - request.getHeaders().add("Authorization", "Bearer " + apiKey); - return execution.execute(request, body); - }); - return restTemplate; - } -} \ No newline at end of file diff --git a/src/main/java/com/splanet/splanet/gpt/OpenAiChatClient.java b/src/main/java/com/splanet/splanet/gpt/OpenAiChatClient.java index 27da861e..363e30d7 100644 --- a/src/main/java/com/splanet/splanet/gpt/OpenAiChatClient.java +++ b/src/main/java/com/splanet/splanet/gpt/OpenAiChatClient.java @@ -1,6 +1,5 @@ package com.splanet.splanet.gpt; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Flux; @@ -11,25 +10,24 @@ public class OpenAiChatClient { private final WebClient webClient; + private final OpenAiProperties openAiProperties; - @Value("${spring.ai.gpt.api-key}") - private String apiKey; - - public OpenAiChatClient(WebClient.Builder webClientBuilder) { + public OpenAiChatClient(WebClient.Builder webClientBuilder, OpenAiProperties openAiProperties) { this.webClient = webClientBuilder.baseUrl("https://api.openai.com/v1").build(); + this.openAiProperties = openAiProperties; } public String call(String message) { - Message userMessage = new Message(message); // 메시지 객체 생성 - Prompt prompt = new Prompt(List.of(userMessage)); // Message 객체를 리스트로 감싸서 Prompt 생성 + Message userMessage = new Message(message); + Prompt prompt = new Prompt(List.of(userMessage)); ChatResponse response = webClient.post() .uri("/chat/completions") - .header("Authorization", "Bearer " + apiKey) // 프로퍼티에서 가져온 API 키 사용 + .header("Authorization", "Bearer " + openAiProperties.getApiKey()) .bodyValue(prompt) .retrieve() .bodyToMono(ChatResponse.class) - .block(); // 블록하여 응답을 기다림 + .block(); return response != null && response.getChoices() != null && !response.getChoices().isEmpty() ? response.getChoices().get(0).getMessage().getContent() @@ -39,9 +37,9 @@ public String call(String message) { public Flux stream(Prompt prompt) { return webClient.post() .uri("/chat/completions") - .header("Authorization", "Bearer " + apiKey) // 프로퍼티에서 가져온 API 키 사용 + .header("Authorization", "Bearer " + openAiProperties.getApiKey()) .bodyValue(prompt) .retrieve() - .bodyToFlux(ChatResponse.class); // Stream the response + .bodyToFlux(ChatResponse.class); } } \ No newline at end of file diff --git a/src/main/java/com/splanet/splanet/gpt/OpenAiProperties.java b/src/main/java/com/splanet/splanet/gpt/OpenAiProperties.java new file mode 100644 index 00000000..282fa863 --- /dev/null +++ b/src/main/java/com/splanet/splanet/gpt/OpenAiProperties.java @@ -0,0 +1,14 @@ +package com.splanet.splanet.gpt; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Getter +@Setter +@ConfigurationProperties(prefix = "gpt-api-key") +@Configuration +public class OpenAiProperties { + private String apiKey; +} \ No newline at end of file