Skip to content

Commit

Permalink
Merge pull request #42 from EWHA-LUX/feature/#41
Browse files Browse the repository at this point in the history
  • Loading branch information
Haewonny authored Feb 16, 2024
2 parents 26201c2 + 9d05c50 commit 1ed2270
Show file tree
Hide file tree
Showing 13 changed files with 431 additions and 26 deletions.
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;
}
}
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;
}
}
19 changes: 6 additions & 13 deletions src/main/java/ewha/lux/once/domain/home/dto/ChatDto.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ewha.lux.once.domain.home.dto;

import lombok.Getter;
import lombok.Setter;
import lombok.*;

@Getter
@Setter
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ChatDto {

private String nickname;
private int ownedCardCount;
private Long chatId;
Expand All @@ -14,13 +16,4 @@ public class ChatDto {
private String benefit;
private int discount;

public ChatDto(String nickname, int ownedCardCount, Long chatId, String cardName, String cardImg, String benefit, int discount){
this.nickname = nickname;
this.ownedCardCount = ownedCardCount;
this.chatId = chatId;
this.cardName = cardName;
this.cardImg = cardImg;
this.benefit = benefit;
this.discount = discount;
}
}
50 changes: 50 additions & 0 deletions src/main/java/ewha/lux/once/domain/home/dto/GeminiChatRequest.java
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);
}
}
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;

}
}


12 changes: 12 additions & 0 deletions src/main/java/ewha/lux/once/domain/home/dto/Message.java
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 src/main/java/ewha/lux/once/domain/home/dto/OpenaiChatRequest.java
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));
}

}
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 src/main/java/ewha/lux/once/domain/home/service/GeminiService.java
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;
}

}
Loading

0 comments on commit 1ed2270

Please sign in to comment.