-
-
Notifications
You must be signed in to change notification settings - Fork 109
Added /rewrite for improving a message using AI #1378
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 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e4f888c
Feature: Implement /rewrite command for message improvement using Cha…
firasrg ef2626f
Merge remote-tracking branch 'origin/develop' into firasrg/feature-sl…
firasrg 5707f50
feature "rewrite-msg command": applies changes due to Zabu's first re…
firasrg a8a761e
feature "rewrite-msg command": applies changes due to Wazei's first r…
firasrg 311d060
feature "rewrite-msg command": applies changes due to Wazei's first r…
firasrg 0604f9b
feature "rewrite-msg command": applies changes due to Wazei's first r…
firasrg aebfa42
feature "rewrite-msg command": removing Optional and other adjustments
firasrg 2b55087
feature "rewrite-msg command" - reply to taz 2nd review;
firasrg 5eea861
ChatGptService: MAX_TOKENS value updated to 1000;
firasrg 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
153 changes: 153 additions & 0 deletions
153
application/src/main/java/org/togetherjava/tjbot/features/messages/RewriteMsgCommand.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,153 @@ | ||
| package org.togetherjava.tjbot.features.messages; | ||
|
|
||
| import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionMapping; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
| import net.dv8tion.jda.api.interactions.commands.build.OptionData; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import org.togetherjava.tjbot.features.CommandVisibility; | ||
| import org.togetherjava.tjbot.features.SlashCommandAdapter; | ||
| import org.togetherjava.tjbot.features.chatgpt.ChatGptModel; | ||
| import org.togetherjava.tjbot.features.chatgpt.ChatGptService; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * The implemented command is {@code /rewrite-msg}, which allows users to have their message | ||
| * rewritten in a clearer, more professional, or better structured form using ChatGPT AI. | ||
| * <p> | ||
| * The rewritten message is shown as an ephemeral message visible only to the user who triggered the | ||
| * command, making it perfect for getting quick writing improvements without cluttering the channel. | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
| * <p> | ||
| * Users can optionally specify a tone/style for the rewrite. If not provided, defaults to CLEAR. | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
| */ | ||
| public final class RewriteMsgCommand extends SlashCommandAdapter { | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
| private static final Logger logger = LoggerFactory.getLogger(RewriteMsgCommand.class); | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
| public static final String COMMAND_NAME = "rewrite-msg"; | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
| private static final String MESSAGE_OPTION = "message"; | ||
| private static final String TONE_OPTION = "tone"; | ||
| private static final int MAX_MESSAGE_LENGTH = 500; | ||
| private static final int MIN_MESSAGE_LENGTH = 3; | ||
| private static final ChatGptModel CHAT_GPT_MODEL = ChatGptModel.HIGH_QUALITY; | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
|
|
||
| private final ChatGptService chatGptService; | ||
|
|
||
| private static String buildResponse(String userMessage, String rewrittenMessage, MsgTone tone) { | ||
| final String toneLabel = tone.displayName; | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
|
|
||
| return """ | ||
| **Rewritten message (%s)** | ||
|
Taz03 marked this conversation as resolved.
Outdated
|
||
|
|
||
| **Original:** | ||
| %s | ||
|
|
||
| **Rewritten:** | ||
|
Taz03 marked this conversation as resolved.
Outdated
|
||
| %s""".formatted(toneLabel, userMessage, rewrittenMessage); | ||
|
Taz03 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private static String buildChatGptPrompt(String userMessage, MsgTone tone) { | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
| return """ | ||
| Please rewrite the following message to make it clearer, more professional, \ | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
| and better structured. Maintain the original meaning while improving the quality \ | ||
| of the writing. Do NOT use em-dashes (—). %s | ||
|
|
||
| If the message is already well-written, provide minor improvements. | ||
|
Taz03 marked this conversation as resolved.
Outdated
|
||
|
|
||
| Original message: | ||
|
Taz03 marked this conversation as resolved.
Outdated
|
||
| %s""".formatted(tone.description, userMessage); | ||
|
Taz03 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Creates the slash command definition and configures available options for rewriting messages. | ||
| * | ||
| * @param chatGptService service for interacting with ChatGPT | ||
| */ | ||
| public RewriteMsgCommand(ChatGptService chatGptService) { | ||
| super(COMMAND_NAME, "Let AI rephrase and improve your message", CommandVisibility.GUILD); | ||
|
|
||
| this.chatGptService = chatGptService; | ||
|
|
||
| final OptionData messageOption = | ||
| new OptionData(OptionType.STRING, MESSAGE_OPTION, "The message you want to rewrite", | ||
| true) | ||
| .setMinLength(MIN_MESSAGE_LENGTH) | ||
| .setMaxLength(MAX_MESSAGE_LENGTH); | ||
|
|
||
| final OptionData toneOption = new OptionData(OptionType.STRING, TONE_OPTION, | ||
| "The tone/style for the rewritten message (default: " + MsgTone.CLEAR.displayName | ||
| + ")", | ||
| false); | ||
|
|
||
| Arrays.stream(MsgTone.values()) | ||
| .forEach(tone -> toneOption.addChoice(tone.displayName, tone.name())); | ||
|
|
||
| getData().addOptions(messageOption, toneOption); | ||
| } | ||
|
|
||
| @Override | ||
| public void onSlashCommand(SlashCommandInteractionEvent event) { | ||
| final String userMessage = | ||
| Objects.requireNonNull(event.getOption(MESSAGE_OPTION)).getAsString(); | ||
|
|
||
| final MsgTone tone = parseTone(event.getOption(TONE_OPTION), event.getUser().getId()); | ||
|
|
||
| event.deferReply(true).queue(); | ||
|
|
||
| final Optional<String> rewrittenMessage = this.rewrite(userMessage, tone); | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (rewrittenMessage.isEmpty()) { | ||
| logger.debug("Failed to obtain a response for /rewrite-msg, original message: '{}'", | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
| userMessage); | ||
| event.getHook() | ||
| .editOriginal( | ||
| "An error occurred while processing your request. Please try again later.") | ||
| .queue(); | ||
| return; | ||
| } | ||
|
|
||
| final String response = buildResponse(userMessage, rewrittenMessage.orElseThrow(), tone); | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
|
|
||
| event.getHook().editOriginal(response).queue(); | ||
| } | ||
|
|
||
| private MsgTone parseTone(@Nullable OptionMapping toneOption, String userId) | ||
|
Taz03 marked this conversation as resolved.
Outdated
|
||
| throws IllegalArgumentException { | ||
| if (toneOption == null) { | ||
| logger.debug("Tone option not provided for user: {}, using default CLEAR", userId); | ||
| return MsgTone.CLEAR; | ||
| } | ||
|
|
||
| final String toneValue = toneOption.getAsString(); | ||
| final MsgTone tone = MsgTone.valueOf(toneValue); | ||
|
|
||
| logger.debug("Parsed tone '{}' for user: {}", tone.displayName, userId); | ||
|
|
||
| return tone; | ||
| } | ||
|
|
||
| private Optional<String> rewrite(String userMessage, MsgTone tone) { | ||
| final String rewritePrompt = buildChatGptPrompt(userMessage, tone); | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
|
|
||
| return chatGptService.ask(rewritePrompt, tone.displayName, CHAT_GPT_MODEL); | ||
|
tj-wazei marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private enum MsgTone { | ||
| CLEAR("Clear", "Make it clear and easy to understand."), | ||
| PRO("Pro", "Use a professional and polished tone."), | ||
| DETAILED("Detailed", "Expand with more detail and explanation."), | ||
| TECHNICAL("Technical", "Use technical and specialized language where appropriate."); | ||
|
|
||
| private final String displayName; | ||
| private final String description; | ||
|
|
||
| MsgTone(String displayName, String description) { | ||
| this.displayName = displayName; | ||
| this.description = description; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.