38
38
import java.util.Map;
39
39
import java.util.Optional;
40
40
import java.util.Set;
41
+ import java.util.StringJoiner;
42
+ import java.util.concurrent.atomic.AtomicReference;
41
43
import java.util.function.Consumer;
42
44
import java.util.function.Function;
43
45
import java.util.function.Predicate;
@@ -127,36 +129,37 @@ public HelpSystemHelper(Config config, Database database, ChatGptService chatGpt
127
129
RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
128
130
String originalQuestion, ComponentIdInteractor componentIdInteractor) {
129
131
Optional<String> questionOptional = prepareChatGptQuestion(threadChannel, originalQuestion);
130
- Optional<String> chatGPTAnswer ;
132
+ Optional<String> chatGptAnswer ;
131
133
132
134
if (questionOptional.isEmpty()) {
133
135
return useChatGptFallbackMessage(threadChannel);
134
136
}
135
137
String question = questionOptional.get();
136
- logger.debug("The final question sent to chatGPT: {}", question);
137
138
138
139
ForumTag defaultTag = threadChannel.getAppliedTags().getFirst();
139
140
ForumTag matchingTag = getCategoryTagOfChannel(threadChannel).orElse(defaultTag);
140
141
141
- String context = matchingTag.getName();
142
- chatGPTAnswer = chatGptService.ask(question, context);
142
+ String context =
143
+ "Category %s on a Java Q&A discord server. You may use markdown syntax for the response"
144
+ .formatted(matchingTag.getName());
145
+ chatGptAnswer = chatGptService.ask(question, context);
143
146
144
- if (chatGPTAnswer .isEmpty()) {
147
+ if (chatGptAnswer .isEmpty()) {
145
148
return useChatGptFallbackMessage(threadChannel);
146
149
}
147
150
148
- StringBuilder idForDismissButton = new StringBuilder( );
149
- RestAction<Message> message =
151
+ AtomicReference<String> messageId = new AtomicReference<>("" );
152
+ RestAction<Message> post =
150
153
mentionGuildSlashCommand(threadChannel.getGuild(), ChatGptCommand.COMMAND_NAME)
151
154
.map("""
152
155
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! \
153
156
In any case, a human is on the way 👍. To continue talking to the AI, you can use \
154
157
%s.
155
158
"""::formatted)
156
159
.flatMap(threadChannel::sendMessage)
157
- .onSuccess(m -> idForDismissButton.append(m .getId()));
160
+ .onSuccess(message -> messageId.set(message .getId()));
158
161
159
- String answer = chatGPTAnswer .orElseThrow();
162
+ String answer = chatGptAnswer .orElseThrow();
160
163
SelfUser selfUser = threadChannel.getJDA().getSelfUser();
161
164
162
165
int responseCharLimit = MessageEmbed.DESCRIPTION_MAX_LENGTH;
@@ -165,9 +168,8 @@ RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
165
168
}
166
169
167
170
MessageEmbed responseEmbed = generateGptResponseEmbed(answer, selfUser, originalQuestion);
168
- return message.flatMap(any -> threadChannel.sendMessageEmbeds(responseEmbed)
169
- .addActionRow(
170
- generateDismissButton(componentIdInteractor, idForDismissButton.toString())));
171
+ return post.flatMap(any -> threadChannel.sendMessageEmbeds(responseEmbed)
172
+ .addActionRow(generateDismissButton(componentIdInteractor, messageId.get())));
171
173
}
172
174
173
175
/**
@@ -204,24 +206,21 @@ private Button generateDismissButton(ComponentIdInteractor componentIdInteractor
204
206
205
207
private Optional<String> prepareChatGptQuestion(ThreadChannel threadChannel,
206
208
String originalQuestion) {
209
+ StringJoiner question = new StringJoiner(" - ");
210
+
207
211
String questionTitle = threadChannel.getName();
208
- StringBuilder questionBuilder = new StringBuilder(MAX_QUESTION_LENGTH);
212
+ question.add(questionTitle);
213
+ question.add(originalQuestion.substring(0,
214
+ Math.min(originalQuestion.length(), MAX_QUESTION_LENGTH)));
209
215
210
- if (originalQuestion.length() < MIN_QUESTION_LENGTH
211
- && questionTitle .length() < MIN_QUESTION_LENGTH) {
216
+ // Not enough content for meaningful responses
217
+ if (question .length() < MIN_QUESTION_LENGTH) {
212
218
return Optional.empty();
213
219
}
214
220
215
- questionBuilder.append(questionTitle).append(" ");
216
- originalQuestion = originalQuestion.substring(0, Math
217
- .min(MAX_QUESTION_LENGTH - questionBuilder.length(), originalQuestion.length()));
218
-
219
- questionBuilder.append(originalQuestion);
220
-
221
- questionBuilder.append(
222
- ". If possible, get, maximum, 5 top links from reliable websites as references in markdown syntax. Put this message on top of the links list \"Here are some links that may help :\".");
223
-
224
- return Optional.of(questionBuilder.toString());
221
+ question.add(
222
+ "Additionally to answering the question, provide 3 useful links (as markdown list) from reliable websites on the topic. Write \"Useful links:\" as title for this list.");
223
+ return Optional.of(question.toString());
225
224
}
226
225
227
226
private RestAction<Message> useChatGptFallbackMessage(ThreadChannel threadChannel) {
0 commit comments