-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
576 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/kim3ho1/yourprotein/gpt/common/BaseEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.kim3ho1.yourprotein.gpt.common; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import java.time.LocalDate; | ||
|
||
@EntityListeners(AuditingEntityListener.class) | ||
@MappedSuperclass | ||
@Getter | ||
public class BaseEntity { | ||
|
||
@CreatedDate | ||
@Column(updatable = false) | ||
private LocalDate createdDate; | ||
|
||
@LastModifiedDate | ||
private LocalDate lastModifiedDate; | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/kim3ho1/yourprotein/gpt/config/ChatGPTConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.kim3ho1.yourprotein.gpt.config; | ||
|
||
import com.theokanning.openai.service.OpenAiService; | ||
import java.time.Duration; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/* | ||
* Chat GPT 라이브러리를 사용하기전, 해당 서비스에 토큰 주입을 하기위한 Config | ||
* API Key 발급은 https://platform.openai.com/account/api-keys 에서 발급받을 수 있습니다. | ||
*/ | ||
@Slf4j | ||
@Configuration | ||
public class ChatGPTConfig { | ||
|
||
@Value("${gpt.token}") | ||
private String token; | ||
|
||
@Bean | ||
public OpenAiService openAiService() { | ||
log.info("token : {}을 활용한 OpenAiService 을 생성합니다.", token); | ||
return new OpenAiService(token, Duration.ofSeconds(60)); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/kim3ho1/yourprotein/gpt/config/QueryDslConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//package com.kim3ho1.yourprotein.gpt.config; | ||
// | ||
//import com.querydsl.jpa.impl.JPAQueryFactory; | ||
//import org.springframework.context.annotation.Bean; | ||
//import org.springframework.context.annotation.Configuration; | ||
// | ||
//import javax.persistence.EntityManager; | ||
//import javax.persistence.PersistenceContext; | ||
// | ||
//@Configuration | ||
//public class QueryDslConfig { | ||
// | ||
// @PersistenceContext | ||
// private EntityManager entityManager; | ||
// | ||
// @Bean | ||
// public JPAQueryFactory jpaQueryFactory() { | ||
// return new JPAQueryFactory(entityManager); | ||
// } | ||
//} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/kim3ho1/yourprotein/gpt/config/WebsocketConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.kim3ho1.yourprotein.gpt.config; | ||
|
||
|
||
import com.kim3ho1.yourprotein.gpt.service.StreamCompletionHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocket; | ||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; | ||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; | ||
|
||
@Configuration | ||
@EnableWebSocket | ||
@RequiredArgsConstructor | ||
public class WebsocketConfig implements WebSocketConfigurer { | ||
|
||
private final StreamCompletionHandler streamCompletionHandler; | ||
|
||
@Override | ||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { | ||
registry.addHandler(streamCompletionHandler, "/chat/stream").setAllowedOrigins("*"); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/kim3ho1/yourprotein/gpt/controller/ChatGPTRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.kim3ho1.yourprotein.gpt.controller; | ||
|
||
|
||
import com.kim3ho1.yourprotein.gpt.dto.request.GPTCompletionChatRequest; | ||
import com.kim3ho1.yourprotein.gpt.dto.request.GPTCompletionRequest; | ||
import com.kim3ho1.yourprotein.gpt.dto.response.CompletionChatResponse; | ||
import com.kim3ho1.yourprotein.gpt.dto.response.CompletionResponse; | ||
import com.kim3ho1.yourprotein.gpt.service.GPTChatRestService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.HashMap; | ||
|
||
@RestController | ||
@RequestMapping("/api/chatgpt/rest") | ||
@RequiredArgsConstructor | ||
public class ChatGPTRestController { | ||
|
||
private final GPTChatRestService gptChatRestService; | ||
|
||
@PostMapping("/completion/chat") | ||
public CompletionChatResponse chat(final @RequestBody HashMap<String, String> prompt) { | ||
return completionChat(new GPTCompletionChatRequest("gpt-3.5-turbo", "user", prompt.get("prompt"), 1000)); | ||
} | ||
|
||
|
||
public CompletionChatResponse completionChat(final GPTCompletionChatRequest gptCompletionChatRequest) { | ||
|
||
return gptChatRestService.completionChat(gptCompletionChatRequest); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/kim3ho1/yourprotein/gpt/dto/request/GPTCompletionChatRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.kim3ho1.yourprotein.gpt.dto.request; | ||
|
||
import com.theokanning.openai.completion.chat.ChatCompletionRequest; | ||
import com.theokanning.openai.completion.chat.ChatMessage; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class GPTCompletionChatRequest { | ||
|
||
private String model; | ||
|
||
private String role; | ||
|
||
private String message; | ||
|
||
private Integer maxTokens; | ||
|
||
|
||
public static ChatCompletionRequest of(GPTCompletionChatRequest request) { | ||
return ChatCompletionRequest.builder() | ||
.model(request.getModel()) | ||
.messages(convertChatMessage(request)) | ||
.maxTokens(request.getMaxTokens()) | ||
.build(); | ||
} | ||
|
||
private static List<ChatMessage> convertChatMessage(GPTCompletionChatRequest request) { | ||
return List.of(new ChatMessage(request.getRole(), request.getMessage())); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/kim3ho1/yourprotein/gpt/dto/request/GPTCompletionRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.kim3ho1.yourprotein.gpt.dto.request; | ||
|
||
import com.theokanning.openai.completion.CompletionRequest; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class GPTCompletionRequest { | ||
|
||
private String model; | ||
|
||
private String prompt; | ||
|
||
private Integer maxToken; | ||
|
||
|
||
public static CompletionRequest of(GPTCompletionRequest restRequest) { | ||
return CompletionRequest.builder() | ||
.model(restRequest.getModel()) | ||
.prompt(restRequest.getPrompt()) | ||
.maxTokens(restRequest.getMaxToken()) | ||
.build(); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/com/kim3ho1/yourprotein/gpt/dto/response/CompletionChatResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.kim3ho1.yourprotein.gpt.dto.response; | ||
|
||
|
||
import com.theokanning.openai.completion.chat.ChatCompletionChoice; | ||
import com.theokanning.openai.completion.chat.ChatCompletionResult; | ||
import com.theokanning.openai.completion.chat.ChatMessage; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class CompletionChatResponse { | ||
|
||
private String id; | ||
|
||
private String object; | ||
|
||
private Long created; | ||
|
||
private String model; | ||
|
||
private List<Message> messages; | ||
|
||
private Usage usage; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class Message { | ||
|
||
private String role; | ||
|
||
private String message; | ||
|
||
public static Message of(ChatMessage chatMessage) { | ||
return new Message( | ||
chatMessage.getRole(), | ||
chatMessage.getContent() | ||
); | ||
} | ||
} | ||
|
||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class Usage { | ||
|
||
private Long promptTokens; | ||
|
||
private Long completionTokens; | ||
|
||
private Long totalTokens; | ||
|
||
public static Usage of(com.theokanning.openai.Usage usage) { | ||
return new Usage( | ||
usage.getPromptTokens(), | ||
usage.getCompletionTokens(), | ||
usage.getTotalTokens() | ||
); | ||
} | ||
} | ||
|
||
public static List<CompletionChatResponse.Message> toResponseListBy(List<ChatCompletionChoice> choices) { | ||
return choices.stream() | ||
.map(completionChoice -> Message.of(completionChoice.getMessage())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static CompletionChatResponse of(ChatCompletionResult result) { | ||
return new CompletionChatResponse( | ||
result.getId(), | ||
result.getObject(), | ||
result.getCreated(), | ||
result.getModel(), | ||
toResponseListBy(result.getChoices()), | ||
Usage.of(result.getUsage()) | ||
); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/com/kim3ho1/yourprotein/gpt/dto/response/CompletionResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.kim3ho1.yourprotein.gpt.dto.response; | ||
|
||
import com.theokanning.openai.completion.CompletionChoice; | ||
import com.theokanning.openai.completion.CompletionResult; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class CompletionResponse { | ||
|
||
private String id; | ||
|
||
private String object; | ||
|
||
private Long created; | ||
|
||
private String model; | ||
|
||
private List<Message> messages; | ||
|
||
private Usage usage; | ||
|
||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class Message { | ||
|
||
private String text; | ||
|
||
private Integer index; | ||
|
||
private String finishReason; | ||
|
||
public static Message of(CompletionChoice choice) { | ||
return new Message( | ||
choice.getText(), | ||
choice.getIndex(), | ||
choice.getFinish_reason() | ||
); | ||
} | ||
} | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class Usage { | ||
|
||
private Long promptTokens; | ||
|
||
private Long completionTokens; | ||
|
||
private Long totalTokens; | ||
|
||
public static Usage of(com.theokanning.openai.Usage usage) { | ||
return new Usage( | ||
usage.getPromptTokens(), | ||
usage.getCompletionTokens(), | ||
usage.getTotalTokens() | ||
); | ||
} | ||
} | ||
|
||
public static List<Message> toResponseListBy(List<CompletionChoice> choices) { | ||
return choices.stream() | ||
.map(Message::of) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static CompletionResponse of(CompletionResult result) { | ||
return new CompletionResponse( | ||
result.getId(), | ||
result.getObject(), | ||
result.getCreated(), | ||
result.getModel(), | ||
toResponseListBy(result.getChoices()), | ||
Usage.of(result.getUsage()) | ||
); | ||
} | ||
} |
Oops, something went wrong.