-
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.
- Loading branch information
Showing
20 changed files
with
746 additions
and
6 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
whaple-domain/src/main/java/com/irostub/domain/entity/standard/Music.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,31 @@ | ||
package com.irostub.domain.entity.standard; | ||
|
||
import com.irostub.domain.entity.BaseEntity; | ||
import lombok.Getter; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Entity | ||
public class Music extends BaseEntity { | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String title; | ||
private String description; | ||
private String url; | ||
private String videoId; | ||
|
||
protected Music() { | ||
} | ||
|
||
public Music(String title, String description, String url, String videoId) { | ||
this.title = title; | ||
this.description = description; | ||
this.url = url; | ||
this.videoId = videoId; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
whaple-domain/src/main/java/com/irostub/domain/repository/MusicRepository.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,9 @@ | ||
package com.irostub.domain.repository; | ||
|
||
|
||
import com.irostub.domain.entity.standard.Music; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface MusicRepository extends JpaRepository<Music, Long> { | ||
Boolean existsByVideoId(String videoId); | ||
} |
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
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
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
118 changes: 118 additions & 0 deletions
118
whaple-standard/src/main/java/com/irostub/standard/bot/gpt/GptCommand.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,118 @@ | ||
package com.irostub.standard.bot.gpt; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.irostub.standard.bot.DefaultBotCommand; | ||
import com.irostub.standard.bot.IManCommand; | ||
import com.irostub.standard.bot.config.AppProperties; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; | ||
import org.telegram.telegrambots.meta.api.objects.Chat; | ||
import org.telegram.telegrambots.meta.api.objects.User; | ||
import org.telegram.telegrambots.meta.bots.AbsSender; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.List; | ||
|
||
@Service | ||
@Slf4j | ||
public class GptCommand extends DefaultBotCommand implements IManCommand { | ||
private final AppProperties appProperties; | ||
|
||
public GptCommand(AppProperties appProperties) { | ||
super("gpt", "AI 를 통해 질문에 답변합니다."); | ||
this.appProperties = appProperties; | ||
} | ||
|
||
@Override | ||
public String getExtendedDescription() { | ||
return "!gpt [질문]\n" + | ||
"\n" + | ||
"---예시---\n" + | ||
"!gpt 오늘은 날씨가 좋아. 뭘 해야 할까?"; | ||
} | ||
|
||
@Override | ||
public void execute(AbsSender absSender, User user, Chat chat, Integer messageId, String[] arguments) { | ||
if (arguments.length < 1) { | ||
SendMessage message = SendMessage.builder() | ||
.chatId(chat.getId()) | ||
.text("질문이 없는 것 같습니다..") | ||
.build(); | ||
sendMessage(absSender, message); | ||
return; | ||
} | ||
|
||
String prompt = String.join(COMMAND_PARAMETER_SEPARATOR, arguments); | ||
|
||
GptMessage gptSystem = new GptMessage(); | ||
gptSystem.setRole("system"); | ||
gptSystem.setContent("당신은 프로그래밍의 전문가입니다. " + | ||
"JAVA, GO, Javascript 등의 각종 언어에 능숙 합니다. " + | ||
"또한, React, Spring 과 같은 개발 프레임워크에 대해서도 잘 알고 있습니다. " + | ||
"당신은 Kubernetes 또한 매우 잘 알고 있습니다. " + | ||
"데이터 서치하고 이를 기반으로 사용자가 질문한 내용에 사실만 답변을 합니다." + | ||
"유저를 반드시 선생님 이라고 부르십시오. "); | ||
|
||
GptMessage gptUser = new GptMessage(); | ||
gptUser.setRole("user"); | ||
gptUser.setContent(prompt); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
GptRequest chatGptRequest = new GptRequest("gpt-3.5-turbo", List.of(gptSystem, gptUser), 1, null); | ||
String input; | ||
try { | ||
input = mapper.writeValueAsString(chatGptRequest); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create("https://api.openai.com/v1/chat/completions")) | ||
.header("Content-Type", "application/json") | ||
.header("Authorization", appProperties.getChatgptToken()) | ||
.POST(HttpRequest.BodyPublishers.ofString(input)) | ||
.build(); | ||
|
||
HttpClient client = HttpClient.newHttpClient(); | ||
HttpResponse<String> response = null; | ||
try { | ||
response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
} catch (Throwable e) { | ||
log.error("http communication error, e=",e); | ||
} | ||
|
||
if (response.statusCode() == 200) { | ||
GptResponse chatGptResponse = null; | ||
try { | ||
chatGptResponse = mapper.readValue(response.body(), GptResponse.class); | ||
} catch (JsonProcessingException e) { | ||
log.error("parse error, e=",e); | ||
} | ||
String answer = chatGptResponse.getChoices()[chatGptResponse.getChoices().length-1].getMessage().getContent(); | ||
|
||
if (!answer.isEmpty()) { | ||
SendMessage message = SendMessage.builder() | ||
.chatId(chat.getId()) | ||
.text(answer) | ||
.parseMode("Markdown") | ||
.build(); | ||
sendMessage(absSender, message); | ||
} | ||
} else { | ||
log.error("status code={}, error={}",response.statusCode(),response.body()); | ||
} | ||
} | ||
private void sendMessage(AbsSender absSender, SendMessage send) { | ||
try { | ||
absSender.execute(send); | ||
} catch (TelegramApiException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
whaple-standard/src/main/java/com/irostub/standard/bot/gpt/GptMessage.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,9 @@ | ||
package com.irostub.standard.bot.gpt; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class GptMessage { | ||
private String role; | ||
private String content; | ||
} |
18 changes: 18 additions & 0 deletions
18
whaple-standard/src/main/java/com/irostub/standard/bot/gpt/GptRequest.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,18 @@ | ||
package com.irostub.standard.bot.gpt; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Data | ||
public class GptRequest { | ||
private String model; | ||
private List<GptMessage> messages = new ArrayList<>(); | ||
private int temperature; | ||
private Integer max_tokens; | ||
} |
14 changes: 14 additions & 0 deletions
14
whaple-standard/src/main/java/com/irostub/standard/bot/gpt/GptResponse.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,14 @@ | ||
package com.irostub.standard.bot.gpt; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class GptResponse { | ||
private String id; | ||
private String object; | ||
private int created; | ||
private String model; | ||
private GptResponseChoice[] choices; | ||
private GptResponseUsage usage; | ||
private Object system_fingerprint; | ||
} |
11 changes: 11 additions & 0 deletions
11
whaple-standard/src/main/java/com/irostub/standard/bot/gpt/GptResponseChoice.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,11 @@ | ||
package com.irostub.standard.bot.gpt; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class GptResponseChoice { | ||
private int index; | ||
private GptMessage message; | ||
private Object logprobs; | ||
private String finish_reason; | ||
} |
10 changes: 10 additions & 0 deletions
10
whaple-standard/src/main/java/com/irostub/standard/bot/gpt/GptResponseUsage.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,10 @@ | ||
package com.irostub.standard.bot.gpt; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class GptResponseUsage { | ||
private int prompt_tokens; | ||
private int completion_tokens; | ||
private int total_tokens; | ||
} |
Oops, something went wrong.