-
-
Notifications
You must be signed in to change notification settings - Fork 104
ChatGPT Intialization #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
241c504
Initial setup for ChatGPT integration. Add ChatGPT Service to communi…
0ab420c
Update ChatGPTCommand to update message when response is ready. Updat…
a9115d6
Turn ChatGPTService to be statically accessed and thus a utility class.
a6927ad
Changes required to bring code up to standard. Use of Config to get O…
178ba09
Clean up changes to ChatGPTService|Command to bring up to standard.
520505c
Change ChatGptService.ask to return Optional. Clearer user experience…
18d673d
Update config.json.template to include OpenAI Key instructions.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
37 changes: 37 additions & 0 deletions
37
application/src/main/java/org/togetherjava/tjbot/features/chaptgpt/ChatGptCommand.java
This file contains hidden or 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,37 @@ | ||
| package org.togetherjava.tjbot.features.chaptgpt; | ||
|
|
||
| import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
|
|
||
| import org.togetherjava.tjbot.features.CommandVisibility; | ||
| import org.togetherjava.tjbot.features.SlashCommandAdapter; | ||
|
|
||
| /** | ||
| * The implemented command is {@code /chatgpt}, which allows users to ask ChatGPT a question, upon | ||
| * which it will respond with an AI generated answer. | ||
| */ | ||
| public final class ChatGptCommand extends SlashCommandAdapter { | ||
| private static final String QUESTION_OPTION = "question"; | ||
| private final ChatGptService chatGptService; | ||
|
|
||
| /** | ||
| * Creates an instance of the chatgpt command. | ||
| * | ||
| * @param chatGptService ChatGptService - Needed to make calls to ChatGPT API | ||
| */ | ||
| public ChatGptCommand(ChatGptService chatGptService) { | ||
| super("chatgpt", "Ask the ChatGPT AI a question!", CommandVisibility.GUILD); | ||
|
|
||
| this.chatGptService = chatGptService; | ||
|
|
||
| getData().addOption(OptionType.STRING, QUESTION_OPTION, "What do you want to ask?", true); | ||
| } | ||
|
|
||
| @Override | ||
| public void onSlashCommand(SlashCommandInteractionEvent event) { | ||
| event.deferReply().queue(); | ||
| String response = chatGptService.ask(event.getOption(QUESTION_OPTION).getAsString()) | ||
| .orElse("An error has occurred while trying to communication with ChatGPT. Please try again later"); | ||
| event.getHook().sendMessage(response).queue(); | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
application/src/main/java/org/togetherjava/tjbot/features/chaptgpt/ChatGptService.java
This file contains hidden or 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,79 @@ | ||
| package org.togetherjava.tjbot.features.chaptgpt; | ||
|
|
||
| import com.theokanning.openai.OpenAiHttpException; | ||
| import com.theokanning.openai.completion.chat.ChatCompletionRequest; | ||
| import com.theokanning.openai.completion.chat.ChatMessage; | ||
| import com.theokanning.openai.completion.chat.ChatMessageRole; | ||
| import com.theokanning.openai.service.OpenAiService; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import org.togetherjava.tjbot.config.Config; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Service used to communicate to OpenAI API to generate responses. | ||
| */ | ||
| public class ChatGptService { | ||
| private static final Logger logger = LoggerFactory.getLogger(ChatGptService.class); | ||
| private static final Duration TIMEOUT = Duration.ofSeconds(10); | ||
| private static final int MAX_TOKENS = 3_000; | ||
| private boolean isDisabled = false; | ||
| private final OpenAiService openAiService; | ||
|
|
||
| /** | ||
| * Creates instance of ChatGPTService | ||
| * | ||
| * @param config needed for token to OpenAI API. | ||
| */ | ||
| public ChatGptService(Config config) { | ||
| String apiKey = config.getOpenaiApiKey(); | ||
| if (apiKey.isBlank()) { | ||
| isDisabled = true; | ||
| } | ||
|
|
||
| openAiService = new OpenAiService(apiKey, TIMEOUT); | ||
| } | ||
|
|
||
| /** | ||
| * Prompt ChatGPT with a question and receive a response. | ||
| * | ||
| * @param question The question being asked of ChatGPT. Max is {@value MAX_TOKENS} tokens. | ||
| * @see <a href="https://platform.openai.com/docs/guides/chat/managing-tokens">ChatGPT | ||
| * Tokens</a>. | ||
| * @return response from ChatGPT as a String. | ||
| */ | ||
| public Optional<String> ask(String question) { | ||
| if (isDisabled) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| try { | ||
| ChatMessage chatMessage = | ||
| new ChatMessage(ChatMessageRole.USER.value(), Objects.requireNonNull(question)); | ||
| ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() | ||
| .model("gpt-3.5-turbo") | ||
| .messages(List.of(chatMessage)) | ||
| .frequencyPenalty(0.5) | ||
| .temperature(0.7) | ||
| .maxTokens(MAX_TOKENS) | ||
| .n(1) | ||
| .build(); | ||
| return Optional.ofNullable(openAiService.createChatCompletion(chatCompletionRequest) | ||
| .getChoices() | ||
| .get(0) | ||
| .getMessage() | ||
| .getContent()); | ||
| } catch (OpenAiHttpException openAiHttpException) { | ||
| logger.warn( | ||
| "There was an error using the OpenAI API: {} Code: {} Type: {} Status Code: {}", | ||
| openAiHttpException.getMessage(), openAiHttpException.code, | ||
| openAiHttpException.type, openAiHttpException.statusCode); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
application/src/main/java/org/togetherjava/tjbot/features/chaptgpt/package-info.java
This file contains hidden or 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 @@ | ||
| /** | ||
| * This package contains the functionality to connect with ChatGPT via API calls. | ||
| */ | ||
| @MethodsReturnNonnullByDefault | ||
| @ParametersAreNonnullByDefault | ||
| package org.togetherjava.tjbot.features.chaptgpt; | ||
|
|
||
| import org.togetherjava.tjbot.annotations.MethodsReturnNonnullByDefault; | ||
|
|
||
| import javax.annotation.ParametersAreNonnullByDefault; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.