-
-
Notifications
You must be signed in to change notification settings - Fork 105
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 4 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
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
| 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.config.Config; | ||
| import org.togetherjava.tjbot.features.CommandVisibility; | ||
| import org.togetherjava.tjbot.features.SlashCommandAdapter; | ||
|
|
||
| /** | ||
| * The implemented command is {@code /chatgpt}, upon which the bot will respond with a response | ||
| * generated by ChatGPT. | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public final class ChatGPTCommand extends SlashCommandAdapter { | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private static final String PROMPT = "prompt"; | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private final ChatGPTService chatGPTService; | ||
|
|
||
| /** | ||
| * Creates an instance of the chatgpt command. | ||
| * | ||
| * @param config Config - Required to get token needed for ChatGPT API | ||
| */ | ||
| public ChatGPTCommand(Config config) { | ||
| super("chatgpt", "User enters a prompt and then gets a response generated by ChatGPT", | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CommandVisibility.GUILD); | ||
|
|
||
| chatGPTService = new ChatGPTService(config); | ||
|
|
||
| getData().addOption(OptionType.STRING, PROMPT, "Prompt sent to ChatGPT", true); | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public void onSlashCommand(SlashCommandInteractionEvent event) { | ||
| event.deferReply().queue(); | ||
| String response = chatGPTService.ask(event.getOption(PROMPT).getAsString()); | ||
| event.getHook().sendMessage(response).queue(); | ||
Zabuzard marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
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,71 @@ | ||
| 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; | ||
|
|
||
| /** | ||
| * 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 long TIMEOUT_DURATION = 10000L; // In milliseconds | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private static final int MAX_TOKENS = 3000; | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private final OpenAiService openAiService; | ||
|
|
||
| /** | ||
| * Creates instance of ChatGPTService | ||
| * | ||
| * @param config Config - needed for token to OpenAI API. | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public ChatGPTService(Config config) { | ||
| String token = config.getOpenaiToken(); | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| openAiService = new OpenAiService(token, Duration.ofMillis(TIMEOUT_DURATION)); | ||
Zabuzard marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Prompt ChatGPT with a question and receive a response. | ||
| * | ||
| * @param question String - The question being asked of ChatGPT. Max is 4000 tokens. | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @see <a href="https://platform.openai.com/docs/guides/chat/managing-tokens">ChatGPT | ||
| * Tokens</a>. | ||
| * @return response from ChatGPT as a String. | ||
| * @throws OpenAiHttpException - Thrown when an error occurs with the API such as a timeout or | ||
| * token error such as it being expired or revoked. | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public String ask(String question) { | ||
| 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) | ||
Zabuzard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .build(); | ||
| return openAiService.createChatCompletion(chatCompletionRequest) | ||
| .getChoices() | ||
| .get(0) | ||
| .getMessage() | ||
| .getContent(); | ||
| } catch (OpenAiHttpException openAiHttpException) { | ||
| logger.error(String.format( | ||
| "There was an error using the OpenAI API: %s Code: %s Type: %s Status Code:%s", | ||
| openAiHttpException.getMessage(), openAiHttpException.code, | ||
| openAiHttpException.type, openAiHttpException.statusCode)); | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return "An error has occurred while trying to communication with ChatGPT. Please try again later"; | ||
tmcdonnell2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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.