Skip to content

Commit

Permalink
Merge pull request #20 from easy-lead/feat/#13
Browse files Browse the repository at this point in the history
Feat/#13
  • Loading branch information
oU-Ua authored Jul 28, 2024
2 parents 548d04c + 26df8ac commit 9b00918
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/easylead/easylead/config/GptConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class GptConfig {
public static final String AUTHORIZATION = "Authorization";
public static final String BEARER = "Bearer ";
public static final String CHAT_MODEL = "gpt-3.5-turbo";
public static final String CHAT_MODEL_CUSTOM = "ft:gpt-3.5-turbo-0613:personal::9prSIgJ8";
public static final Integer MAX_TOKEN = 300;
public static final Boolean STREAM = true;
public static final String ROLE = "user";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ public Flux<String> ask(String text) throws JsonProcessingException {
return gptService.askStream(text);
}

public ResponseDTO getEasyToReadCustom(String text) throws JsonProcessingException {
HttpRequest request = gptService.requestGPTCustom(text);
return gptConverter.toResponseDTO(gptService.responseGPT(request));
public Flux<String> getEasyToReadCustom(String text) throws JsonProcessingException {
return gptService.askCustomStream(text);
}

public ResponseDTO getImage(String keyword) throws JsonProcessingException {
HttpRequest request = gptService.requestGPTImage(keyword);
return gptConverter.toResponseDTO(gptService.responseDalle(request));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Locale;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Locale;

@Slf4j
@RestController
@RequiredArgsConstructor
Expand All @@ -40,12 +43,23 @@ public Flux<String> ask(Locale locale,
}
}

@GetMapping("/custom")
public ResponseEntity<ResponseDTO> easyLeadCustom(@RequestParam String text)
throws JsonProcessingException {
return ResponseEntity.ok(gptBusiness.getEasyToReadCustom(text));
@GetMapping(value="/custom", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> easyLeadCustom(@RequestParam String text)
{
try {
return gptBusiness.getEasyToReadCustom(text);
}catch (JsonProcessingException je){
log.error(je.getMessage());
return Flux.empty();
}

}

@GetMapping("/image")
public ResponseEntity<ResponseDTO> ImageGenerate(@RequestParam String keyword)
throws JsonProcessingException {
return ResponseEntity.ok(gptBusiness.getImage(keyword));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.easylead.easylead.domain.gpt.dto;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@JsonPropertyOrder({"model", "prompt", "n","size"})
public class DalleRequestDTO {
String model;
String prompt;
int n;
String size;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.easylead.easylead.domain.gpt.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class DalleResData {
String url;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.easylead.easylead.domain.gpt.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class DalleResponseDTO {
int created;


List<DalleResData> data;

}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ public Flux<String> askStream(String text) throws JsonProcessingException {
.bodyToFlux(String.class);
return eventStream;
}
public Flux<String> askCustomStream(String text) throws JsonProcessingException {
WebClient client = WebClient.builder()
.baseUrl(GptConfig.CHAT_URL)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(GptConfig.AUTHORIZATION, GptConfig.BEARER + gptApiCustomkey)
.build();

List<Message> messages = new ArrayList<>();

messages.add(new Message(text,"user"));

ChatGPTRequestDTO chatGptRequest = new ChatGPTRequestDTO(
GptConfig.CHAT_MODEL_CUSTOM,
messages,
GptConfig.TEMPERATURE,
GptConfig.STREAM
);
String requestValue = objectMapper.writeValueAsString(chatGptRequest);

Flux<String> eventStream = client.post()
.bodyValue(requestValue)
.accept(MediaType.TEXT_EVENT_STREAM)
.retrieve()
.bodyToFlux(String.class);
return eventStream;
}

public HttpRequest requestGPTCustom(String text) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Expand All @@ -204,7 +230,7 @@ public HttpRequest requestGPTCustom(String text) throws JsonProcessingException
// 시스템 역할 설정
messages.add(new Message(text, "user"));

ChatGPTRequestDTO chatGptRequest = new ChatGPTRequestDTO("ft:gpt-3.5-turbo-0125:personal::9klL6p0E", messages, 0.3,false);
ChatGPTRequestDTO chatGptRequest = new ChatGPTRequestDTO("ft:gpt-3.5-turbo-0125:personal::9ldfWO0p", messages, 0.3,false);
String input = null;
input = mapper.writeValueAsString(chatGptRequest);
System.out.println(input);
Expand All @@ -221,4 +247,53 @@ public HttpRequest requestGPTCustom(String text) throws JsonProcessingException


}

public HttpRequest requestGPTImage(String keyword) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
List<Message> messages = new ArrayList<>();

messages.add(new Message(keyword, "user"));

DalleRequestDTO dalleRequest = new DalleRequestDTO("dall-e-3", keyword, 1,"1024x1024");
String input = null;
input = mapper.writeValueAsString(dalleRequest);
System.out.println(input);
System.out.println("apikey : " + gptApiCustomkey);

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.openai.com/v1/images/generations"))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + gptApiCustomkey)
.POST(HttpRequest.BodyPublishers.ofString(input))
.build();

return request;

}

public String responseDalle(HttpRequest request) throws JsonProcessingException {
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = null;
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(response.body());
ObjectMapper objectMapper = new ObjectMapper();
DalleResponseDTO dalleResponseDTO = objectMapper.readValue(response.body(), DalleResponseDTO.class);
if(dalleResponseDTO.getData() ==null){
throw new ApiException(ErrorCode.SERVER_ERROR);
}
List<DalleResData> data = dalleResponseDTO.getData();

String url = data.get(0).getUrl();
String subject = "";

System.out.println("content = " + url);
return url;

}


}

0 comments on commit 9b00918

Please sign in to comment.