Skip to content

Commit

Permalink
feat: SpringAi gpt 우선 연결만
Browse files Browse the repository at this point in the history
  • Loading branch information
ez23re authored and kanguk01 committed Oct 25, 2024
1 parent e42264a commit 2336237
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ configurations {

repositories {
mavenCentral()
maven {
url 'https://repo.spring.io/milestone'
}
maven {
url 'https://repo.spring.io/snapshot'
}
}

dependencies {
Expand Down Expand Up @@ -61,6 +67,14 @@ dependencies {
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

//gpt
// implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'com.fasterxml.jackson.core:jackson-databind'
// implementation 'org.springframework.boot:spring-boot-starter'
implementation platform("org.springframework.ai:spring-ai-bom:0.8.0")
implementation 'org.springframework.ai:spring-ai-openai'


}

tasks.named('test') {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/splanet/splanet/config/OpenAIConfig.java
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;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/splanet/splanet/gpt/ChatResponse.java
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 객체 리스트
}
8 changes: 8 additions & 0 deletions src/main/java/com/splanet/splanet/gpt/Choice.java
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 객체
}
12 changes: 12 additions & 0 deletions src/main/java/com/splanet/splanet/gpt/Message.java
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 src/main/java/com/splanet/splanet/gpt/OpenAiChatClient.java
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
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/splanet/splanet/gpt/Prompt.java
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; // 메시지 목록
}

0 comments on commit 2336237

Please sign in to comment.