Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ FIX: 공식문서에 따른 요청형식 반영 #61

Merged
merged 1 commit into from
Dec 29, 2024
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
36 changes: 18 additions & 18 deletions src/main/java/shop/catchmind/gpt/application/GptService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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<GptMessage> messages = createGptMessages(dto, pictureType);
public InterpretDto interpretPicture(final NaturalLanguageDto dto, final PictureType pictureType, final String imageUrl) {
List<GptMessage> messages = createGptMessages(dto, pictureType, imageUrl);
log.info("Request Messages: {}", messages);

HashMap<String, Object> 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<GptMessage> createGptMessages(final NaturalLanguageDto dto, final PictureType pictureType) {
private static List<GptMessage> createGptMessages(final NaturalLanguageDto dto, final PictureType pictureType, final String imageUrl) {
List<GptMessage> messages = new ArrayList<>();

// gpt 역할(프롬프트) 설정
Expand All @@ -63,7 +64,14 @@ private static List<GptMessage> createGptMessages(final NaturalLanguageDto dto,
}

// 실제 요청
messages.add(GptMessage.of(USER, dto.value()));
if (pictureType == PictureType.GENERAL) {
messages.add(GptMessage.of(USER, dto.value()));
} else {
List<Object> 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;
}
Expand All @@ -79,25 +87,17 @@ private static HashMap<String, Object> createRequestBody(final List<GptMessage>
}

// api 호출에 필요한 Http Header를 만들고 HTTP 객체를 만드는 메서드
public HttpEntity<MultiValueMap<String, Object>> createHttpEntity(final HashMap<String, Object> chatGptRequest, final MultipartFile file) {
public HttpEntity<HashMap<String, Object>> createHttpEntity(final HashMap<String, Object> chatGptRequest) {

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
httpHeaders.setContentType(MediaType.parseMediaType(MEDIA_TYPE));
httpHeaders.add(AUTHORIZATION, BEARER + apiKey);

// 파일을 MultiValueMap에 담아서 전송
MultiValueMap<String, Object> 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<MultiValueMap<String, Object>> httpEntity) {
public GptResponse getResponse(final HttpEntity<HashMap<String, Object>> httpEntity) {

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
// 답변이 길어질 경우 TimeOut Error 발생하므로 time 설정
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/shop/catchmind/gpt/dto/GptMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public InterpretResponse inspect(final Long authId, final List<MultipartFile> 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 객체 생성 및 저장
Expand Down