Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gradle/buildOutputCleanup/cache.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Fri Feb 07 20:37:40 KST 2025
#Sat Feb 15 02:43:43 KST 2025
gradle.version=8.10
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
48 changes: 37 additions & 11 deletions src/main/java/com/team4/giftidea/controller/GptController.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public GptController(RestTemplate restTemplate, GptConfig gptConfig, ProductServ
@ApiResponse(responseCode = "500", description = "서버 내부 오류 발생")
})
@PostMapping(value = "/process", consumes = "multipart/form-data", produces = "application/json")
public List<Product> processFileAndRecommend(
public List<Object> processFileAndRecommend(
@RequestParam("file") @Parameter(description = "카카오톡 대화 파일 (.txt)", required = true) MultipartFile file,
@RequestParam("targetName") @Parameter(description = "분석 대상 이름 (예: '여자친구')", required = true) String targetName,
@RequestParam("relation") @Parameter(description = "대상과의 관계 (couple, friend, parent 등)", required = true) String relation,
Expand Down Expand Up @@ -116,13 +116,21 @@ public List<Product> processFileAndRecommend(
processedMessages.add(finalChunk.toString());

// 2. GPT API 호출: 전처리된 메시지로 키워드 반환
String categories = generatePrompt(processedMessages, relation, sex, theme);
String gptResponse = generatePrompt(processedMessages, relation, sex, theme);

// 3. 키워드 리스트 변환 및 상품 검색
List<String> keywords = Arrays.asList(categories.split(","));
// 3. 키워드, 근거 리스트 변환 및 상품 검색
String[] responseLines = gptResponse.split("\n");
String categories = responseLines[0].replace("Categories: ", "").trim();
String reasons = responseLines.length > 1 ? responseLines[1].trim() : "";

List<String> keywords = Arrays.asList(categories.split(", "));
keywords.replaceAll(String::trim);

List<Product> products = productService.searchByKeywords(keywords);
List<String> reasonList = Arrays.asList(reasons.split("\n"));

List<Product> products_No_reason = productService.searchByKeywords(keywords);
List<Object> products = new ArrayList<>(products_No_reason);
products.add(reasonList);

return products;
}
Expand Down Expand Up @@ -193,7 +201,6 @@ private String generatePrompt(List<String> processedMessages, String relation, S
private String generateText(String prompt) {
GptRequestDTO request = new GptRequestDTO(gptConfig.getModel(), prompt);
try {

// HTTP 요청 전에 request 객체 로깅
ObjectMapper mapper = new ObjectMapper();

Expand All @@ -203,21 +210,39 @@ private String generateText(String prompt) {
if (response != null) {
log.debug("GPT 응답 수신: {}", mapper.writeValueAsString(response));

// 응답에 'choices'가 있고, 그 중 첫 번째 항목이 존재하는지 확인
if (response.getChoices() != null && !response.getChoices().isEmpty()) {
String content = response.getChoices().get(0).getMessage().getContent();

// 필요한 형태로 카테고리 추출 (예: "1. [무선이어폰, 스마트워치, 향수]" 형태)
if (content.contains("1.")) {
String categories = content.split("1.")[1].split("\n")[0]; // 첫 번째 카테고리 라인 추출
// 첫 번째 줄: 카테고리 리스트 추출
String categories = content.split("1.")[1].split("\n")[0];

// 괄호 안의 항목들을 추출하고, 쉼표로 구분하여 키워드 리스트 만들기
// 카테고리 리스트 (괄호 안의 항목들)
String[] categoryArray = categories.split("\\[|\\]")[1].split(",");

List<String> keywords = new ArrayList<>();
for (String category : categoryArray) {
keywords.add(category.trim());
}
return String.join(", ", keywords); // 최종적으로 카테고리들을 반환

// 두 번째 줄 이후: 카테고리별 설명(reason) 추출
List<String> reasons = new ArrayList<>();
String[] lines = content.split("\n");

for (String line : lines) {
line = line.trim();
if (line.startsWith("- ")) { // 설명 부분인지 확인
int startIndex = line.indexOf(": [");
if (startIndex != -1) {
String reason = line.substring(startIndex + 3, line.length() - 1).trim();
reasons.add(reason);
}
}
}

// 카테고리와 설명을 조합하여 반환
return "Categories: " + String.join(", ", keywords) + "\n" +
"Reasons: " + String.join("\n", reasons);
} else {
log.warn("GPT 응답에서 카테고리 정보가 올바르지 않습니다.");
}
Expand All @@ -237,6 +262,7 @@ private String generateText(String prompt) {
}
}


private String extractKeywordsAndReasonsCoupleMan(String theme, String message) {
String prompt = String.format("""
다음 텍스트를 참고하여 남자 애인이 %s에 선물로 받으면 좋아할 카테고리 3개와 판단에 참고한 대화를 제공해주세요.
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/team4/giftidea/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ public class Product {
*/
@Column(nullable = false)
private String keyword;

}