-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/com/splanet/splanet/config/OpenAIConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.splanet.splanet.gpt; | ||
|
||
import lombok.Getter; | ||
import java.util.List; | ||
|
||
@Getter | ||
public class ChatResponse { | ||
private List<Choice> choices; // Choice 객체 리스트 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.splanet.splanet.gpt; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Choice { | ||
private Message message; // Message 객체 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.splanet.splanet.gpt; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Message { | ||
private String content; // 메시지 내용 | ||
|
||
public Message(String content) { | ||
this.content = content; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/splanet/splanet/gpt/OpenAiChatClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class OpenAiChatClient { | ||
|
||
private final WebClient webClient; | ||
|
||
@Value("${spring.ai.gpt.api-key}") | ||
private String apiKey; | ||
|
||
public OpenAiChatClient(WebClient.Builder webClientBuilder) { | ||
this.webClient = webClientBuilder.baseUrl("https://api.openai.com/v1").build(); | ||
} | ||
|
||
public String call(String message) { | ||
Message userMessage = new Message(message); // 메시지 객체 생성 | ||
Prompt prompt = new Prompt(List.of(userMessage)); // Message 객체를 리스트로 감싸서 Prompt 생성 | ||
|
||
ChatResponse response = webClient.post() | ||
.uri("/chat/completions") | ||
.header("Authorization", "Bearer " + apiKey) // 프로퍼티에서 가져온 API 키 사용 | ||
.bodyValue(prompt) | ||
.retrieve() | ||
.bodyToMono(ChatResponse.class) | ||
.block(); // 블록하여 응답을 기다림 | ||
|
||
return response != null && response.getChoices() != null && !response.getChoices().isEmpty() | ||
? response.getChoices().get(0).getMessage().getContent() | ||
: "No response"; | ||
} | ||
|
||
public Flux<ChatResponse> stream(Prompt prompt) { | ||
return webClient.post() | ||
.uri("/chat/completions") | ||
.header("Authorization", "Bearer " + apiKey) // 프로퍼티에서 가져온 API 키 사용 | ||
.bodyValue(prompt) | ||
.retrieve() | ||
.bodyToFlux(ChatResponse.class); // Stream the response | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.splanet.splanet.gpt; | ||
|
||
import lombok.Getter; | ||
import lombok.AllArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@AllArgsConstructor // 모든 필드를 사용하는 생성자를 자동 생성 | ||
public class Prompt { | ||
private List<Message> messages; // 메시지 목록 | ||
} |