Skip to content

Commit

Permalink
Fix :: 이미지랑 message 데이터 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
yeseong0412 committed Jan 6, 2024
1 parent 4af4b46 commit a75e7e4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/miracle/azure/ChatGPTController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.miracle.azure;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -14,7 +15,12 @@ public class ChatGPTController {
private final ChatGPTService chatGPTService;

@PostMapping("/data")
public Object createData(@RequestPart("data") MessageRequest massageRequest , @RequestPart("file") MultipartFile multipartFile) throws IOException {
return chatGPTService.createData(massageRequest, multipartFile);
public Object createFile(@RequestPart("file") MultipartFile multipartFile) throws IOException {
return chatGPTService.createFile(multipartFile);
}

@PostMapping("/file")
public Object createData(@RequestBody MessageRequest massageRequest) throws JsonProcessingException {
return chatGPTService.createData(massageRequest);
}
}
48 changes: 30 additions & 18 deletions src/main/java/com/miracle/azure/ChatGPTService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.miracle.azure;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -29,35 +30,46 @@ public class ChatGPTService {

private final RestTemplate restTemplate;

public Object createData(MessageRequest massageRequest, MultipartFile multipartFile) throws IOException {
String responseBody = "";
public Object createData(MessageRequest massageRequest) throws JsonProcessingException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("api-key", subscriptionKey);

HttpEntity<String> requestEntity = new HttpEntity<>(postGPT(null,massageRequest), headers);
//System.out.println(requestBody); //로그 찍기
ResponseEntity<String> responseEntity = restTemplate.exchange(apiUrl, HttpMethod.POST, requestEntity, String.class);


ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(responseEntity.getBody());
System.out.println(jsonNode);
String textValue = jsonNode.get("choices").get(0).get("message").get("content").asText();

Map<String, String> map = new HashMap<>();
map.put("message" , textValue);

return map;
}

public Object createFile(MultipartFile multipartFile) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("api-key", subscriptionKey);

if (multipartFile.isEmpty()) {
HttpEntity<String> requestEntity = new HttpEntity<>(postGPT(null,massageRequest), headers);
//System.out.println(requestBody); //로그 찍기
ResponseEntity<String> responseEntity = restTemplate.exchange(apiUrl, HttpMethod.POST, requestEntity, String.class);
responseBody = responseEntity.getBody();
// byte[] bytes = multipartFile.getBytes();
// String base64Encoded = Base64.getEncoder().encodeToString(bytes);
//String url = s3Uploader.upload(multipartFile, "fikess");

} else {
// byte[] bytes = multipartFile.getBytes();
// String base64Encoded = Base64.getEncoder().encodeToString(bytes);
//String url = s3Uploader.upload(multipartFile, "fikess");
String encodedFileContent = generateDataUrl(multipartFile);

String encodedFileContent = generateDataUrl(multipartFile);
HttpEntity<String> requestEntity = new HttpEntity<>(postGPT(encodedFileContent,null), headers);
//System.out.println(requestBody); //로그 찍기
ResponseEntity<String> responseEntity = restTemplate.exchange(apiUrl, HttpMethod.POST, requestEntity, String.class);

HttpEntity<String> requestEntity = new HttpEntity<>(postGPT(encodedFileContent,null), headers);
//System.out.println(requestBody); //로그 찍기
ResponseEntity<String> responseEntity = restTemplate.exchange(apiUrl, HttpMethod.POST, requestEntity, String.class);
responseBody = responseEntity.getBody();

}

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(responseBody);
JsonNode jsonNode = objectMapper.readTree(responseEntity.getBody());
System.out.println(jsonNode);
String textValue = jsonNode.get("choices").get(0).get("message").get("content").asText();

Expand Down

0 comments on commit a75e7e4

Please sign in to comment.