Skip to content

Commit

Permalink
Merge pull request #130 from My-Own-Weapon/feat/#85-auto
Browse files Browse the repository at this point in the history
리뷰 자동화 API 분리
  • Loading branch information
moonxxpower authored Jul 24, 2024
2 parents c8da364 + 001637e commit fcd24d5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.chimaera.wagubook.controller;

import com.chimaera.wagubook.dto.request.PostAIRequest;
import com.chimaera.wagubook.dto.request.PostCreateRequest;
import com.chimaera.wagubook.dto.request.PostUpdateRequest;
import com.chimaera.wagubook.dto.response.PostAIResponse;
import com.chimaera.wagubook.dto.response.PostResponse;
import com.chimaera.wagubook.dto.response.StorePostResponse;
import com.chimaera.wagubook.exception.CustomException;
Expand Down Expand Up @@ -42,6 +44,12 @@ public ResponseEntity<PostResponse> createPost(@RequestPart List<MultipartFile>
return new ResponseEntity<>(postService.createPost(images, data, memberId), HttpStatus.CREATED);
}

@PostMapping(value = "/posts/auto")
@Operation(summary = "AI 자동 생성")
public ResponseEntity<PostAIResponse> createContent(@RequestBody PostAIRequest postAIRequest) throws IOException {
return new ResponseEntity<>(postService.createContent(postAIRequest), HttpStatus.OK);
}

/**
* 포스트 조회 (전체)
* Method : GET
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/chimaera/wagubook/dto/request/PostAIRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.chimaera.wagubook.dto.request;

import com.chimaera.wagubook.entity.Category;
import lombok.Data;

@Data
public class PostAIRequest {
private Category postCategory;
private String menuName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.chimaera.wagubook.dto.response;

import lombok.Data;

@Data
public class PostAIResponse {
private String menuContent;

public PostAIResponse(String menuContent) {
this.menuContent = menuContent;
}
}
16 changes: 8 additions & 8 deletions src/main/java/com/chimaera/wagubook/service/OpenAiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public String requestImageAnalysis(BufferedImage resizedImage, String menuName,
// 요청할 데이터 설정
Map<String, Object> requestData = new HashMap<>();
requestData.put("model", apiModel);
requestData.put("max_tokens", 100);
requestData.put("max_tokens", 150);
requestData.put("messages", Arrays.asList(
Map.of("role", "user", "content", String.format("주어진 음식 이미지를 분석해서 이에 대한 긍정적인 리뷰를 남겨줘" +
"해당 음식은 메뉴 이름이 %s이고, 음식 카테고리가 %s인 음식이야." +
Map.of("role", "user", "content", String.format("주어진 음식 이미지를 분석해서 이에 대한 긍정적인 리뷰를 100글자 내외로 남겨줘" +
"해당 음식은 이름이 %s이고, 카테고리가 %s인 음식이야." +
"이때 숫자, 시간, 날짜 관련 표현은 제외해줘." +
"또한 사용자가 기록을 남기는 어투로 작성해줘야 하고, 완성형 문장이어야 해!" +
"또한 사용자가 기록을 남기는 어투로 작성해줘야 하고, 반드시 완성형 문장이어야 해!" +
"마지막으로 출력은 리뷰만 해줘.", imageUrl, menuName, categoryName))
));

Expand All @@ -62,12 +62,12 @@ public String requestText(String menuName, String categoryName) throws IOExcepti
// 요청할 데이터 설정
Map<String, Object> requestData = new HashMap<>();
requestData.put("model", apiModel);
requestData.put("max_tokens", 100);
requestData.put("max_tokens", 150);
requestData.put("messages", Arrays.asList(
Map.of("role", "user", "content", String.format("주어진 음식 이미지를 분석해서 이에 대한 긍정적인 리뷰를 남겨줘" +
"해당 음식은 메뉴 이름이 %s이고, 음식 카테고리가 %s인 음식이야." +
Map.of("role", "user", "content", String.format("주어진 음식 이미지를 분석해서 이에 대한 긍정적인 리뷰를 100글자 내외로 남겨줘" +
"해당 음식은 이름이 %s이고, 카테고리가 %s인 음식이야." +
"이때 숫자, 시간, 날짜 관련 표현은 제외해줘." +
"또한 사용자가 기록을 남기는 어투로 작성해줘야 하고, 완성형 문장이어야 해!" +
"또한 사용자가 기록을 남기는 어투로 작성해줘야 하고, 반드시 완성형 문장이어야 해!" +
"마지막으로 출력은 리뷰만 해줘.", menuName, categoryName))
));

Expand Down
21 changes: 14 additions & 7 deletions src/main/java/com/chimaera/wagubook/service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.chimaera.wagubook.service;

import com.chimaera.wagubook.dto.request.PostAIRequest;
import com.chimaera.wagubook.dto.request.PostCreateRequest;
import com.chimaera.wagubook.dto.request.PostUpdateRequest;
import com.chimaera.wagubook.dto.response.PostAIResponse;
import com.chimaera.wagubook.dto.response.PostResponse;
import com.chimaera.wagubook.dto.response.StorePostResponse;
import com.chimaera.wagubook.entity.*;
Expand Down Expand Up @@ -113,13 +115,13 @@ public PostResponse createPost(List<MultipartFile> images, PostCreateRequest pos
MultipartFile image = images.get(i);
BufferedImage resizedImage = s3ImageService.resizeImageWithAspectRatio(image, 512, 512);

if (postCreateRequest.isAuto()) {
// String review = openAiService.requestImageAnalysis(resizedImage, menu.getMenuName(), postCreateRequest.getPostCategory().toString());
String review = openAiService.requestText(menu.getMenuName(), postCreateRequest.getPostCategory().toString());

menu.setMenuContent(review);
menuRepository.save(menu);
}
// if (postCreateRequest.isAuto()) {
//// String review = openAiService.requestImageAnalysis(resizedImage, menu.getMenuName(), postCreateRequest.getPostCategory().toString());
// String review = openAiService.requestText(menu.getMenuName(), postCreateRequest.getPostCategory().toString());
//
// menu.setMenuContent(review);
// menuRepository.save(menu);
// }

String url = s3ImageService.uploadImage(resizedImage, image.getOriginalFilename());

Expand All @@ -137,6 +139,11 @@ public PostResponse createPost(List<MultipartFile> images, PostCreateRequest pos
return new PostResponse(post);
}

public PostAIResponse createContent(PostAIRequest postAIRequest) throws IOException {
String review = openAiService.requestText(postAIRequest.getMenuName(), postAIRequest.getPostCategory().toString());
return new PostAIResponse(review);
}

// 포스트 조회 (전체)
public List<StorePostResponse> getAllPosts(Long memberId, PageRequest pageRequest) {
Member member = memberRepository.findById(memberId).orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_MEMBER));
Expand Down

0 comments on commit fcd24d5

Please sign in to comment.