-
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.
Merge pull request #22 from olmangjolmang/OMJM-82-recommend-posts
생성형 AI Gemini 도입, 함께 읽으면 좋은 아티클 기능 구현
- Loading branch information
Showing
11 changed files
with
310 additions
and
76 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
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
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
56 changes: 56 additions & 0 deletions
56
src/main/java/com/ticle/server/post/dto/GeminiRequest.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,56 @@ | ||
package com.ticle.server.post.dto; | ||
|
||
import lombok.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@Getter | ||
@Setter | ||
|
||
public class GeminiRequest { | ||
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 GeminiRequest(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); | ||
} | ||
} | ||
|
84 changes: 84 additions & 0 deletions
84
src/main/java/com/ticle/server/post/dto/GeminiResponse.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,84 @@ | ||
package com.ticle.server.post.dto; | ||
|
||
import lombok.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class GeminiResponse { | ||
|
||
private List<Candidate> candidates; | ||
private PromptFeedback promptFeedback; | ||
|
||
@Data | ||
public static class Candidate { | ||
private Content content; | ||
private String finishReason; | ||
private int index; | ||
private List<SafetyRating> safetyRatings; | ||
} | ||
|
||
@Data | ||
public static class Content { | ||
private List<Parts> parts; | ||
private String role; | ||
} | ||
|
||
@Data | ||
public static class Parts { | ||
private String text; | ||
} | ||
|
||
@Data | ||
public static class SafetyRating { | ||
private String category; | ||
private String probability; | ||
} | ||
|
||
@Data | ||
public static class PromptFeedback { | ||
private List<SafetyRating> safetyRatings; | ||
} | ||
|
||
// Gemini 리턴 | ||
// postDetail의 recommenPosts 결과를 List 형태로 만들어주는 함수 | ||
public List<Map<String, Object>> formatRecommendPost() { | ||
List<Map<String, Object>> recommendPosts = new ArrayList<>(); | ||
|
||
if (candidates != null) { | ||
for (Candidate candidate : candidates) { | ||
Content content = candidate.getContent(); | ||
if (content != null && content.getParts() != null && content.getParts().size() > 0) { | ||
String combinedString = content.getParts().get(0).getText(); | ||
|
||
String[] parts = combinedString.split("postTitle="); | ||
|
||
if (parts.length == 2) { | ||
String postId = parts[0].substring(parts[0].indexOf("[") + 1, parts[0].indexOf("]")); | ||
String postTitle = parts[1].substring(parts[1].indexOf("[") + 1, parts[1].indexOf("]")); | ||
|
||
//postid, posttitle를 각각 list화 | ||
String[] postIds = postId.split(", "); | ||
String[] postTitles = postTitle.split(", "); | ||
|
||
for (int i = 0; i < postIds.length; i++) { | ||
Map<String, Object> postMap = new LinkedHashMap<>(); | ||
postMap.put("postId", Long.parseLong(postIds[i])); | ||
postMap.put("postTitle", postTitles[i].replaceAll("'", "")); | ||
recommendPosts.add(postMap); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return recommendPosts; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ticle/server/post/dto/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,22 @@ | ||
package com.ticle.server.post.dto; | ||
|
||
|
||
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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/ticle/server/post/dto/PostIdTitleDto.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.ticle.server.post.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
public class PostIdTitleDto { | ||
private Long postId; | ||
private String title; | ||
|
||
@Override | ||
public String toString() { | ||
return "{" + | ||
"postId=" + postId + | ||
", title='" + title + '\'' + | ||
'}'; | ||
} | ||
} |
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
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
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
Oops, something went wrong.