Skip to content

Commit 10718cb

Browse files
Title generation by ChatGPT for transfer-questions command (#1059)
* 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 * refactor prompt message for better titles --------- Co-authored-by: alphaBEE <61616007+ankitsmt211@users.noreply.github.com>
1 parent f8c91a9 commit 10718cb

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

application/src/main/java/org/togetherjava/tjbot/features/Features.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
153153
features.add(new HelpThreadCreatedListener(helpSystemHelper));
154154

155155
// Message context commands
156-
features.add(new TransferQuestionCommand(config));
156+
features.add(new TransferQuestionCommand(config, chatGptService));
157157

158158
// User context commands
159159

application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.togetherjava.tjbot.features.BotCommandAdapter;
3333
import org.togetherjava.tjbot.features.CommandVisibility;
3434
import org.togetherjava.tjbot.features.MessageContextCommand;
35+
import org.togetherjava.tjbot.features.chatgpt.ChatGptService;
3536
import org.togetherjava.tjbot.features.utils.StringDistances;
3637

3738
import java.awt.Color;
@@ -64,20 +65,23 @@ public final class TransferQuestionCommand extends BotCommandAdapter
6465
private static final int INPUT_MIN_LENGTH = 3;
6566
private final Predicate<String> isHelpForumName;
6667
private final List<String> tags;
68+
private final ChatGptService chatGptService;
6769

6870

6971
/**
7072
* Creates a new instance.
7173
*
7274
* @param config to get the helper forum and tags
75+
* @param chatGptService the service used to ask ChatGPT questions via the API.
7376
*/
74-
public TransferQuestionCommand(Config config) {
77+
public TransferQuestionCommand(Config config, ChatGptService chatGptService) {
7578
super(Commands.message(COMMAND_NAME), CommandVisibility.GUILD);
7679

7780
isHelpForumName =
7881
Pattern.compile(config.getHelpSystem().getHelpForumPattern()).asMatchPredicate();
7982

8083
tags = config.getHelpSystem().getCategories();
84+
this.chatGptService = chatGptService;
8185
}
8286

8387
@Override
@@ -92,12 +96,20 @@ public void onMessageContext(MessageContextInteractionEvent event) {
9296
String originalChannelId = event.getTarget().getChannel().getId();
9397
String authorId = event.getTarget().getAuthor().getId();
9498
String mostCommonTag = tags.get(0);
99+
String chatGptPrompt =
100+
"Summarize the following text into a concise title or heading not more than 4-5 words, remove quotations if any: %s"
101+
.formatted(originalMessage);
102+
Optional<String> chatGptResponse = chatGptService.ask(chatGptPrompt, "");
103+
String title = chatGptResponse.orElse(createTitle(originalMessage));
104+
if (title.length() > TITLE_MAX_LENGTH) {
105+
title = title.substring(0, TITLE_MAX_LENGTH);
106+
}
95107

96108
TextInput modalTitle = TextInput.create(MODAL_TITLE_ID, "Title", TextInputStyle.SHORT)
97109
.setMaxLength(TITLE_MAX_LENGTH)
98110
.setMinLength(TITLE_MIN_LENGTH)
99111
.setPlaceholder("Describe the question in short")
100-
.setValue(createTitle(originalMessage))
112+
.setValue(title)
101113
.build();
102114

103115
Builder modalInputBuilder =

0 commit comments

Comments
 (0)