-
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
10 changed files
with
220 additions
and
7 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/com/moneygement/lyf/jarvis/recommendation/config/RAGConfig.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,13 @@ | ||
package com.moneygement.lyf.jarvis.recommendation.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
@Configuration | ||
public class RAGConfig { | ||
@Value("${url.rag}") | ||
private String ragUrl; | ||
} |
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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/moneygement/lyf/jarvis/recommendation/domain/RagResponse.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,55 @@ | ||
package com.moneygement.lyf.jarvis.recommendation.domain; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.*; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class RagResponse { | ||
|
||
@JsonProperty("startPoint") | ||
private Point startPoint; // 출발지 정보 | ||
|
||
@JsonProperty("endPoint") | ||
private Point endPoint; // 도착지 정보 | ||
|
||
@JsonProperty("pointList") | ||
private List<Point> pointList; // 추천 명소 리스트 | ||
|
||
@JsonProperty("message") | ||
private String message; // 전체 메시지 | ||
|
||
/** | ||
* Point 클래스: 출발지, 도착지, 명소 정보를 담는 내부 클래스 | ||
*/ | ||
@Getter | ||
@Setter | ||
@ToString | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class Point { | ||
|
||
@JsonProperty("name") | ||
private String name; // 장소 이름 | ||
|
||
@JsonProperty("latitude") | ||
private double latitude; // 위도 | ||
|
||
@JsonProperty("longitude") | ||
private double longitude; // 경도 | ||
|
||
@JsonProperty("message") | ||
private String message; // 장소 설명 (명소일 경우) | ||
|
||
@JsonProperty("status") | ||
private String status; // 장소 상태 (예: 맛집, 관광 등) | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
...java/com/moneygement/lyf/jarvis/recommendation/domain/RecommendationCreateRAGRequest.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,12 @@ | ||
package com.moneygement.lyf.jarvis.recommendation.domain; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record RecommendationCreateRAGRequest(@NotBlank(message = "user id can't be empty") String userId, | ||
@NotBlank(message = "command can't be empty") String command, | ||
@NotNull(message = "latitude can't be empty") Double latitude, | ||
@NotNull(message = "latitude can't be empty") Double longitude, | ||
String weather | ||
) { | ||
} |
10 changes: 10 additions & 0 deletions
10
...in/java/com/moneygement/lyf/jarvis/recommendation/domain/RecommendationCreateRequest.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,10 @@ | ||
package com.moneygement.lyf.jarvis.recommendation.domain; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record RecommendationCreateRequest(@NotBlank(message = "user id can't be empty") String userId, | ||
@NotBlank(message = "request can't be empty") String request, | ||
@NotNull(message = "latitude can't be empty") Double latitude, | ||
@NotNull(message = "latitude can't be empty") Double longitude) { | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/moneygement/lyf/jarvis/recommendation/service/RAGService.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,36 @@ | ||
package com.moneygement.lyf.jarvis.recommendation.service; | ||
|
||
import static com.moneygement.lyf.jarvis.common.util.JsonUtil.*; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.moneygement.lyf.jarvis.common.util.HttpClientUtil; | ||
import com.moneygement.lyf.jarvis.recommendation.config.RAGConfig; | ||
import com.moneygement.lyf.jarvis.recommendation.domain.RagResponse; | ||
import com.moneygement.lyf.jarvis.recommendation.domain.RecommendationCreateRAGRequest; | ||
import com.moneygement.lyf.jarvis.route.domain.OptimalRouteRequest; | ||
import com.moneygement.lyf.jarvis.route.domain.OptimalRouteResponse; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class RAGService { | ||
private final RAGConfig ragConfig; | ||
|
||
public RagResponse getRecommendations(RecommendationCreateRAGRequest request) throws IOException { | ||
ResponseEntity<Object> response = HttpClientUtil.post(null, ragConfig.getRagUrl(), request); | ||
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { | ||
return convertObject(response.getBody(), RagResponse.class); | ||
} else { | ||
log.error("Failed to call RAG API: " + response.getStatusCode()); | ||
throw new IOException("Failed to call RAG API: " + response.getStatusCode()); | ||
} | ||
} | ||
|
||
} |
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
74 changes: 73 additions & 1 deletion
74
...ain/java/com/moneygement/lyf/jarvis/recommendation/service/RecommendationServiceImpl.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 |
---|---|---|
@@ -1,21 +1,93 @@ | ||
package com.moneygement.lyf.jarvis.recommendation.service; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.moneygement.lyf.jarvis.common.error.ExternalApiFailedException; | ||
import com.moneygement.lyf.jarvis.history.domain.HistoryCreateRequest; | ||
import com.moneygement.lyf.jarvis.history.service.HistoryService; | ||
import com.moneygement.lyf.jarvis.recommendation.domain.GroupRecommendation; | ||
import com.moneygement.lyf.jarvis.recommendation.domain.RagResponse; | ||
import com.moneygement.lyf.jarvis.recommendation.domain.Recommendation; | ||
import com.moneygement.lyf.jarvis.recommendation.domain.RecommendationCreateRAGRequest; | ||
import com.moneygement.lyf.jarvis.recommendation.domain.RecommendationCreateRequest; | ||
import com.moneygement.lyf.jarvis.recommendation.persistence.GroupRecommendationRepository; | ||
import com.moneygement.lyf.jarvis.recommendation.persistence.RecommendationRepository; | ||
import com.moneygement.lyf.jarvis.weather.service.WeatherReportService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RecommendationServiceImpl implements RecommendationService{ | ||
public class RecommendationServiceImpl implements RecommendationService { | ||
private final GroupRecommendationRepository groupRecommendationRepository; | ||
private final RecommendationRepository recommendationRepository; | ||
private final HistoryService historyService; | ||
private final WeatherReportService weatherReportService; | ||
private final RAGService ragService; | ||
|
||
@Override | ||
public List<Recommendation> getRecommendationsByGroupId(Long groupId) { | ||
return recommendationRepository.findByGroupId(groupId); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public String recommend(RecommendationCreateRequest request) { | ||
// 1. weather service summary 서비스 호출 -> weather 값에 포함 | ||
String weather = | ||
weatherReportService.getWeatherSummary(request.latitude(), request.longitude()); | ||
// 2. RAG 호출 | ||
RagResponse ragResponse; | ||
try { | ||
ragResponse = ragService.getRecommendations(new RecommendationCreateRAGRequest( | ||
request.userId(), | ||
request.request(), | ||
request.latitude(), | ||
request.longitude(), | ||
weather | ||
)); | ||
} catch (IOException e) { | ||
throw new ExternalApiFailedException(e.getMessage()); | ||
} | ||
// 3. group_recommendation 저장 | ||
if (ragResponse == null) { | ||
throw new ExternalApiFailedException("Can't retrieve data from RAG API"); | ||
} | ||
|
||
GroupRecommendation group = new GroupRecommendation(ragResponse.getMessage(), request.userId()); | ||
group.setSessionName("자동 생성된 여행 코스"); | ||
GroupRecommendation savedGroup = groupRecommendationRepository.save(group); | ||
|
||
// 4. recommendation 저장 | ||
recommendationRepository.save(new Recommendation( | ||
savedGroup.getGroupId(), | ||
ragResponse.getStartPoint().getName(), | ||
ragResponse.getMessage(), | ||
ragResponse.getStartPoint().getStatus(), | ||
ragResponse.getStartPoint().getLatitude(), | ||
ragResponse.getStartPoint().getLongitude())); | ||
|
||
for (RagResponse.Point point : ragResponse.getPointList()) { | ||
Recommendation recommendation = new Recommendation( | ||
savedGroup.getGroupId(), | ||
point.getName(), | ||
point.getMessage(), | ||
point.getStatus(), | ||
point.getLatitude(), | ||
point.getLongitude()); | ||
recommendationRepository.save(recommendation); | ||
} | ||
|
||
// 5. history에 저장 | ||
historyService.save(new HistoryCreateRequest( | ||
request.userId(), | ||
savedGroup.getGroupId(), | ||
request.request(), | ||
ragResponse.getMessage())); | ||
return ragResponse.getMessage(); | ||
} | ||
} |
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