-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
13 changed files
with
431 additions
and
26 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/ewha/lux/once/domain/home/config/GeminiRestTemplateConfig.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,20 @@ | ||
package ewha.lux.once.domain.home.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class GeminiRestTemplateConfig { | ||
@Bean | ||
@Qualifier("geminiRestTemplate") | ||
public RestTemplate geminiRestTemplate() { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
restTemplate.getInterceptors().add((request, body, execution) -> execution.execute(request, body)); | ||
|
||
return restTemplate; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/ewha/lux/once/domain/home/config/OpenaiRestTemplateConfig.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,25 @@ | ||
package ewha.lux.once.domain.home.config; | ||
|
||
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; | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
src/main/java/ewha/lux/once/domain/home/dto/GeminiChatRequest.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,50 @@ | ||
package ewha.lux.once.domain.home.dto; | ||
|
||
import lombok.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter @Setter | ||
@Builder | ||
public class GeminiChatRequest { | ||
private List<Content> contents; | ||
private GenerationConfig generationConfig; | ||
|
||
@Getter @Setter | ||
public static class Content { | ||
private Parts parts; | ||
} | ||
|
||
@Getter @Setter | ||
public static class Parts { | ||
private String text; | ||
|
||
} | ||
|
||
@Getter @Setter | ||
public static class GenerationConfig { | ||
private int candidate_count; | ||
private int max_output_tokens; | ||
private double temperature; | ||
|
||
} | ||
|
||
public GeminiChatRequest(String prompt) { | ||
this.contents = new ArrayList<>(); | ||
Content content = new Content(); | ||
Parts parts = new Parts(); | ||
|
||
parts.setText(prompt); | ||
content.setParts(parts); | ||
|
||
this.contents.add(content); | ||
this.generationConfig = new GenerationConfig(); | ||
this.generationConfig.setCandidate_count(1); | ||
this.generationConfig.setMax_output_tokens(1000); | ||
this.generationConfig.setTemperature(0.7); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/ewha/lux/once/domain/home/dto/GeminiChatResponse.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,54 @@ | ||
package ewha.lux.once.domain.home.dto; | ||
|
||
import lombok.*; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class GeminiChatResponse { | ||
|
||
private List<Candidate> candidates; | ||
private PromptFeedback promptFeedback; | ||
|
||
|
||
@Getter @Setter | ||
public static class Candidate { | ||
private Content content; | ||
private String finishReason; | ||
private int index; | ||
private List<SafetyRating> safetyRatings; | ||
|
||
} | ||
|
||
@Getter @Setter | ||
@ToString | ||
public static class Content { | ||
private List<Parts> parts; | ||
private String role; | ||
|
||
} | ||
|
||
@Getter @Setter | ||
@ToString | ||
public static class Parts { | ||
private String text; | ||
|
||
} | ||
|
||
@Getter @Setter | ||
public static class SafetyRating { | ||
private String category; | ||
private String probability; | ||
} | ||
|
||
@Getter @Setter | ||
public static class PromptFeedback { | ||
private List<SafetyRating> safetyRatings; | ||
|
||
} | ||
} | ||
|
||
|
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 ewha.lux.once.domain.home.dto; | ||
|
||
import lombok.*; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter @Setter | ||
public class Message { | ||
private String role; | ||
private String content; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/ewha/lux/once/domain/home/dto/OpenaiChatRequest.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,25 @@ | ||
package ewha.lux.once.domain.home.dto; | ||
|
||
import lombok.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Data | ||
@Getter @Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class OpenaiChatRequest { | ||
private String model; | ||
private List<Message> messages; | ||
private final int n = 1; | ||
private double temperature; | ||
|
||
public OpenaiChatRequest(String model, String prompt, String userInput) { | ||
this.model = model; | ||
this.messages = new ArrayList<>(); | ||
this.messages.add(new Message("system", prompt)); | ||
this.messages.add(new Message("user", userInput)); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/ewha/lux/once/domain/home/dto/OpenaiChatResponse.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,24 @@ | ||
package ewha.lux.once.domain.home.dto; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class OpenaiChatResponse { | ||
private List<Choice> choices; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public static class Choice { | ||
private int idx; | ||
private Message message; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/ewha/lux/once/domain/home/service/GeminiService.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,59 @@ | ||
package ewha.lux.once.domain.home.service; | ||
|
||
import ewha.lux.once.domain.card.entity.OwnedCard; | ||
import ewha.lux.once.domain.home.dto.GeminiChatRequest; | ||
import ewha.lux.once.domain.home.dto.GeminiChatResponse; | ||
import ewha.lux.once.domain.user.entity.Users; | ||
import ewha.lux.once.global.repository.OwnedCardRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
|
||
@Service @Slf4j | ||
@RequiredArgsConstructor | ||
public class GeminiService { | ||
|
||
@Qualifier("geminiRestTemplate") | ||
@Autowired | ||
private RestTemplate restTemplate; | ||
|
||
private final OwnedCardRepository ownedCardRepository; | ||
|
||
@Value("${gemini.api.url}") | ||
private String apiUrl; | ||
|
||
@Value("${gemini.api.key}") | ||
private String geminiApiKey; | ||
|
||
String prompt = "결제처, 결제금액, 카드들의 혜택 정보를 Input으로 하여 결제처에서 최적의 혜택을 누릴 수 있는 카드 번호, 혜택 정보, 할인 금액을 알려주어야 함.\n" + | ||
"카드들의 혜택 정보에서 각 카드는 ///// 로 구분되고, 각 카드가 입력된 결제처에 해당되는 혜택을 가지고 있다면 할인 금액을 계산하고, 여러 카드 중 가장 할인 금액이 큰 카드의 고유번호 숫자, 결제처에 해당되는 혜택 정보 요약 텍스트(특수문자 없어야 함), 해당 혜택 적용 시 받게되는 할인 금액 숫자를 쉼표로 구분하여 제공해야 함. \n"; | ||
public String gemini(Users nowUser, String keyword, int paymentAmount) { | ||
List<OwnedCard> ownedCards = ownedCardRepository.findOwnedCardByUsers(nowUser); | ||
prompt = prompt + "결제 금액: " + paymentAmount + ", 결제처: " + keyword + ", 카드들의 혜택 정보: "; | ||
for (OwnedCard ownedCard : ownedCards) { | ||
String name = ownedCard.getCard().getName(); | ||
String id = ownedCard.getCard().getId().toString(); | ||
String benefits = ownedCard.getCard().getBenefits(); | ||
prompt = prompt + name + ", " + "카드 고유 번호 : " + id + ", " + benefits + "/////"; | ||
} | ||
|
||
// Gemini 요청 보내는 부분 | ||
String requestUrl = apiUrl + "?key=" + geminiApiKey; | ||
GeminiChatRequest request = new GeminiChatRequest(prompt); | ||
|
||
GeminiChatResponse response = restTemplate.postForObject(requestUrl, request, GeminiChatResponse.class); | ||
|
||
String result = response.getCandidates().get(0).getContent().getParts().get(0).getText().toString(); // 응답만 추출 | ||
|
||
log.info(result); | ||
|
||
return result; | ||
} | ||
|
||
} |
Oops, something went wrong.