-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from Princess-in-silvertown/feat/43
Feat: ν€μλ μλ μμ± κΈ°λ₯
- Loading branch information
Showing
10 changed files
with
211 additions
and
5 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
47 changes: 47 additions & 0 deletions
47
src/main/java/slvtwn/khu/toyouserver/agent/gpt/ChatGptAgent.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,47 @@ | ||
package slvtwn.khu.toyouserver.agent.gpt; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
@AllArgsConstructor | ||
@Component | ||
public class ChatGptAgent { | ||
|
||
private ChatGptConfiguration configuration; | ||
|
||
public ChatGptResponse requestWithPrompt(String prompt) { | ||
HashMap<String, Object> body = setupBody(prompt); | ||
|
||
return WebClient.create() | ||
.post() | ||
.uri(configuration.getTextModel()) | ||
.header("Content-Type", "application/json") | ||
.header("Authorization", "Bearer " + configuration.getOpenaiKey()) | ||
.bodyValue(body) | ||
.retrieve() | ||
.onStatus(HttpStatusCode::is4xxClientError, | ||
clientResponse -> clientResponse.bodyToMono(String.class).map(Exception::new)) | ||
.bodyToMono(ChatGptResponse.class) | ||
.block(); | ||
} | ||
|
||
|
||
private HashMap<String, Object> setupBody(String prompt) { | ||
HashMap<String, Object> body = new HashMap<>(); | ||
body.put("model", configuration.getTextModel()); | ||
|
||
HashMap<String, String> userMessage = new HashMap<>(); | ||
userMessage.put("role", "user"); | ||
userMessage.put("content", prompt); | ||
|
||
List<HashMap<String, String>> messages = new ArrayList<>(); | ||
messages.add(userMessage); | ||
body.put("messages", messages); | ||
return body; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/slvtwn/khu/toyouserver/agent/gpt/ChatGptConfiguration.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,17 @@ | ||
package slvtwn.khu.toyouserver.agent.gpt; | ||
|
||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Getter | ||
@Component | ||
public class ChatGptConfiguration { | ||
|
||
@Value("${openai.key}") | ||
private String openaiKey; | ||
@Value("${openai.text-generation.endpoint}") | ||
private String textModelEndpoint; | ||
@Value("${openai.text-generation.model}") | ||
private String textModel; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/slvtwn/khu/toyouserver/agent/gpt/ChatGptResponse.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,21 @@ | ||
package slvtwn.khu.toyouserver.agent.gpt; | ||
|
||
import java.util.List; | ||
|
||
public record ChatGptResponse(String id, String object, Long created, List<Choice> choices, Usage usage) { | ||
|
||
public record Choice( | ||
String text, | ||
int index, | ||
String logprobs, | ||
String finish_reason | ||
) { | ||
} | ||
|
||
public record Usage( | ||
int prompt_tokens, | ||
int completion_tokens, | ||
int total_tokens | ||
) { | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/slvtwn/khu/toyouserver/application/AgentService.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,62 @@ | ||
package slvtwn.khu.toyouserver.application; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import slvtwn.khu.toyouserver.agent.gpt.ChatGptAgent; | ||
import slvtwn.khu.toyouserver.agent.gpt.ChatGptResponse; | ||
import slvtwn.khu.toyouserver.dto.KeywordRequest; | ||
import slvtwn.khu.toyouserver.dto.KeywordResponse; | ||
|
||
@AllArgsConstructor | ||
@Service | ||
public class AgentService { | ||
|
||
private final ChatGptAgent chatGptAgent; | ||
|
||
public KeywordResponse generateKeywords(KeywordRequest request) { | ||
String content = request.content(); | ||
String prompt = String.format(""" | ||
Suggest 3 keywords that could represent emotions or characteristics in the content. | ||
<example> | ||
<request> | ||
content: | ||
μλ , μ λ² νκΈ°μ λ§λμ μ λ§ λ°κ°μ μ΄! μμΌλ‘ λ λ§λ μ μμκ±°λΌ κΈ°λνκ³ μμ΄. | ||
μ λ§ μμ½μ§λ§ 건κ°νκ² μ μ§λ΄! | ||
keywords: | ||
</request> | ||
<response> | ||
λ°κ°μ, κΈ°λ, μμ¬μ | ||
</response> | ||
</example> | ||
<example> | ||
<request> | ||
content: | ||
μμΌ μΆνν΄! μμΌ μΆν νν°μ μ§μ μ°Έμ¬νμ§ λͺ»ν΄μ μμ½μ§λ§, μ¦κ²κ² ν루λ₯Ό 보λμΌλ©΄ μ’κ² μ΄. | ||
keywords: | ||
</request> | ||
<response> | ||
μΆν, μμ¬μ, μ¦κ±°μ | ||
</response> | ||
</example> | ||
content: %s | ||
keywords: | ||
""", content); | ||
|
||
ChatGptResponse chatGptResponse = chatGptAgent.requestWithPrompt(prompt); | ||
ChatGptResponse.Choice firstChoice = chatGptResponse.choices().get(0); | ||
List<String> keywords = parseKeywordFromChoice(firstChoice); | ||
|
||
return new KeywordResponse(keywords); | ||
} | ||
|
||
private List<String> parseKeywordFromChoice(ChatGptResponse.Choice choice) { | ||
String text = choice.text(); | ||
return Arrays.stream(text.split(",")) | ||
.toList(); | ||
} | ||
} |
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,5 @@ | ||
package slvtwn.khu.toyouserver.dto; | ||
|
||
public record KeywordRequest(String content) { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/slvtwn/khu/toyouserver/dto/KeywordResponse.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,7 @@ | ||
package slvtwn.khu.toyouserver.dto; | ||
|
||
import java.util.List; | ||
|
||
public record KeywordResponse(List<String> keywords) { | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/slvtwn/khu/toyouserver/presentation/AgentController.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,23 @@ | ||
package slvtwn.khu.toyouserver.presentation; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import slvtwn.khu.toyouserver.application.AgentService; | ||
import slvtwn.khu.toyouserver.common.response.ToyouResponse; | ||
import slvtwn.khu.toyouserver.dto.KeywordRequest; | ||
import slvtwn.khu.toyouserver.dto.KeywordResponse; | ||
|
||
@AllArgsConstructor | ||
@RestController | ||
public class AgentController { | ||
|
||
private final AgentService agentService; | ||
|
||
@PostMapping("/generate-keywords") | ||
public ToyouResponse<KeywordResponse> generateKeywords(@RequestBody KeywordRequest keywordRequest) { | ||
KeywordResponse response = agentService.generateKeywords(keywordRequest); | ||
return ToyouResponse.from(response); | ||
} | ||
} |
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
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,21 @@ | ||
spring: | ||
jpa: | ||
open-in-view: false | ||
generate-ddl: true | ||
show-sql: true | ||
properties: | ||
hibernate: | ||
format_sql: true | ||
|
||
datasource: | ||
url: jdbc:h2:mem:testdb | ||
driver-class-name: org.h2.Driver | ||
username: sa | ||
password: | ||
|
||
openai: | ||
key: test-key | ||
|
||
text-generation: | ||
endpoint: https://api.openai.com/v1/chat/completions | ||
model: gpt-4o-mini |