Skip to content

Commit

Permalink
refactor: @Value->@ConfigurationProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
ez23re authored and kanguk01 committed Oct 25, 2024
1 parent 2336237 commit 87fc348
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
21 changes: 0 additions & 21 deletions src/main/java/com/splanet/splanet/config/OpenAIConfig.java

This file was deleted.

20 changes: 9 additions & 11 deletions src/main/java/com/splanet/splanet/gpt/OpenAiChatClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()
Expand All @@ -39,9 +37,9 @@ public String call(String message) {
public Flux<ChatResponse> 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);
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/splanet/splanet/gpt/OpenAiProperties.java
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 87fc348

Please sign in to comment.