From 585a61eeb27d9144796d35e84fe8b2311fdeff67 Mon Sep 17 00:00:00 2001 From: Gyehwan Baek Date: Mon, 30 Dec 2024 01:32:48 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20FIX:=20=EA=B3=B5=EC=8B=9D=EB=AC=B8?= =?UTF-8?q?=EC=84=9C=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EC=9A=94=EC=B2=AD?= =?UTF-8?q?=ED=98=95=EC=8B=9D=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../catchmind/gpt/application/GptService.java | 36 +++++++++---------- .../shop/catchmind/gpt/dto/GptMessage.java | 4 +-- .../picture/application/PictureService.java | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/shop/catchmind/gpt/application/GptService.java b/src/main/java/shop/catchmind/gpt/application/GptService.java index 270b0df..0fc4337 100644 --- a/src/main/java/shop/catchmind/gpt/application/GptService.java +++ b/src/main/java/shop/catchmind/gpt/application/GptService.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import static shop.catchmind.gpt.constant.GptConstant.*; @@ -34,22 +35,22 @@ public class GptService { @Value(value = "${GPT_API_KEY}") private String apiKey; - public InterpretDto interpretPicture(final NaturalLanguageDto dto, final PictureType pictureType, final MultipartFile file) { - List messages = createGptMessages(dto, pictureType); + public InterpretDto interpretPicture(final NaturalLanguageDto dto, final PictureType pictureType, final String imageUrl) { + List messages = createGptMessages(dto, pictureType, imageUrl); log.info("Request Messages: {}", messages); HashMap requestBody = createRequestBody(messages); - GptResponse chatGptRes = getResponse(createHttpEntity(requestBody, file)); + GptResponse chatGptRes = getResponse(createHttpEntity(requestBody)); - String response = chatGptRes.choices().get(0).message().content(); + String response = (String) chatGptRes.choices().get(0).message().content(); log.info("Response: {}", response); return InterpretDto.builder().data(response).build(); } // GPT 에 요청할 메시지를 만드는 메서드 - private static List createGptMessages(final NaturalLanguageDto dto, final PictureType pictureType) { + private static List createGptMessages(final NaturalLanguageDto dto, final PictureType pictureType, final String imageUrl) { List messages = new ArrayList<>(); // gpt 역할(프롬프트) 설정 @@ -63,7 +64,14 @@ private static List createGptMessages(final NaturalLanguageDto dto, } // 실제 요청 - messages.add(GptMessage.of(USER, dto.value())); + if (pictureType == PictureType.GENERAL) { + messages.add(GptMessage.of(USER, dto.value())); + } else { + List content = new ArrayList<>(); + content.add(Map.of("type", "text", "text", dto.value())); + content.add(Map.of("type", "image_url", "image_url", Map.of("url", imageUrl))); + messages.add(GptMessage.of(USER, content)); + } return messages; } @@ -79,25 +87,17 @@ private static HashMap createRequestBody(final List } // api 호출에 필요한 Http Header를 만들고 HTTP 객체를 만드는 메서드 - public HttpEntity> createHttpEntity(final HashMap chatGptRequest, final MultipartFile file) { + public HttpEntity> createHttpEntity(final HashMap chatGptRequest) { HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); + httpHeaders.setContentType(MediaType.parseMediaType(MEDIA_TYPE)); httpHeaders.add(AUTHORIZATION, BEARER + apiKey); - // 파일을 MultiValueMap에 담아서 전송 - MultiValueMap body = new LinkedMultiValueMap<>(); - body.add("request", chatGptRequest); - try { - body.add("file", new MultipartInputStreamFileResource(file.getInputStream(), file.getOriginalFilename())); - } catch (IOException e) { - throw new RuntimeException("Failed to read the file", e); - } - return new HttpEntity<>(body, httpHeaders); + return new HttpEntity<>(chatGptRequest, httpHeaders); } // GPT API 요청후 response body를 받아오는 메서드 - public GptResponse getResponse(final HttpEntity> httpEntity) { + public GptResponse getResponse(final HttpEntity> httpEntity) { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); // 답변이 길어질 경우 TimeOut Error 발생하므로 time 설정 diff --git a/src/main/java/shop/catchmind/gpt/dto/GptMessage.java b/src/main/java/shop/catchmind/gpt/dto/GptMessage.java index 7737cb8..a2a5825 100644 --- a/src/main/java/shop/catchmind/gpt/dto/GptMessage.java +++ b/src/main/java/shop/catchmind/gpt/dto/GptMessage.java @@ -5,9 +5,9 @@ @Builder public record GptMessage( String role, - String content + Object content ) { - public static GptMessage of(final String role, final String content) { + public static GptMessage of(final String role, final Object content) { return GptMessage.builder() .role(role) .content(content) diff --git a/src/main/java/shop/catchmind/picture/application/PictureService.java b/src/main/java/shop/catchmind/picture/application/PictureService.java index 6f8bb3e..e0d012b 100644 --- a/src/main/java/shop/catchmind/picture/application/PictureService.java +++ b/src/main/java/shop/catchmind/picture/application/PictureService.java @@ -87,7 +87,7 @@ public InterpretResponse inspect(final Long authId, final List im } // GPT 서비스로부터 해석 결과 받기 - InterpretDto interpretDto = gptService.interpretPicture(NaturalLanguageDto.of(value), pictureType, image); + InterpretDto interpretDto = gptService.interpretPicture(NaturalLanguageDto.of(value), pictureType, imageUrl); String interpretedContent = removeNumbersInParentheses(interpretDto.data()); // Picture 객체 생성 및 저장