From 7918d04b686eac60e46bc7716e188e5c243a1489 Mon Sep 17 00:00:00 2001 From: vishv843 <201901453@daiict.ac.in> Date: Mon, 18 Mar 2024 22:23:05 -0500 Subject: [PATCH 1/2] title generation by ChatGPT for transfer-questions command refactoring reviewer comments addressed prompt improved Update prompt Co-authored-by: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> removed tags --- .../togetherjava/tjbot/features/Features.java | 2 +- .../moderation/TransferQuestionCommand.java | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/Features.java b/application/src/main/java/org/togetherjava/tjbot/features/Features.java index 5cbfb7cea7..bc9e94cd6f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/Features.java @@ -133,7 +133,7 @@ public static Collection createFeatures(JDA jda, Database database, Con features.add(new HelpThreadCreatedListener(helpSystemHelper)); // Message context commands - features.add(new TransferQuestionCommand(config)); + features.add(new TransferQuestionCommand(config, chatGptService)); // User context commands diff --git a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java index aadf9ab61a..425de70ddd 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java @@ -32,6 +32,7 @@ import org.togetherjava.tjbot.features.BotCommandAdapter; import org.togetherjava.tjbot.features.CommandVisibility; import org.togetherjava.tjbot.features.MessageContextCommand; +import org.togetherjava.tjbot.features.chatgpt.ChatGptService; import org.togetherjava.tjbot.features.utils.StringDistances; import java.awt.Color; @@ -64,20 +65,23 @@ public final class TransferQuestionCommand extends BotCommandAdapter private static final int INPUT_MIN_LENGTH = 3; private final Predicate isHelpForumName; private final List tags; + private final ChatGptService chatGptService; /** * Creates a new instance. * * @param config to get the helper forum and tags + * @param chatGptService the service used to ask ChatGPT questions via the API. */ - public TransferQuestionCommand(Config config) { + public TransferQuestionCommand(Config config, ChatGptService chatGptService) { super(Commands.message(COMMAND_NAME), CommandVisibility.GUILD); isHelpForumName = Pattern.compile(config.getHelpSystem().getHelpForumPattern()).asMatchPredicate(); tags = config.getHelpSystem().getCategories(); + this.chatGptService = chatGptService; } @Override @@ -92,12 +96,20 @@ public void onMessageContext(MessageContextInteractionEvent event) { String originalChannelId = event.getTarget().getChannel().getId(); String authorId = event.getTarget().getAuthor().getId(); String mostCommonTag = tags.get(0); + String chatGptPrompt = + "generate title for this question %s, it shouldn't be longer than 4-5 words with no header" + .formatted(originalMessage); + Optional chatGptResponse = chatGptService.ask(chatGptPrompt, ""); + String title = chatGptResponse.orElse(createTitle(originalMessage)); + if (title.length() > TITLE_MAX_LENGTH) { + title = title.substring(0, TITLE_MAX_LENGTH); + } TextInput modalTitle = TextInput.create(MODAL_TITLE_ID, "Title", TextInputStyle.SHORT) .setMaxLength(TITLE_MAX_LENGTH) .setMinLength(TITLE_MIN_LENGTH) .setPlaceholder("Describe the question in short") - .setValue(createTitle(originalMessage)) + .setValue(title) .build(); Builder modalInputBuilder = From 90a15f62f6d66d23bd4286a7cf4a6e7ddc5ccf31 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Sun, 12 May 2024 18:31:59 +0530 Subject: [PATCH 2/2] refactor prompt message for better titles --- .../tjbot/features/moderation/TransferQuestionCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java index 425de70ddd..59924023a2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java @@ -97,7 +97,7 @@ public void onMessageContext(MessageContextInteractionEvent event) { String authorId = event.getTarget().getAuthor().getId(); String mostCommonTag = tags.get(0); String chatGptPrompt = - "generate title for this question %s, it shouldn't be longer than 4-5 words with no header" + "Summarize the following text into a concise title or heading not more than 4-5 words, remove quotations if any: %s" .formatted(originalMessage); Optional chatGptResponse = chatGptService.ask(chatGptPrompt, ""); String title = chatGptResponse.orElse(createTitle(originalMessage));