3838import java .util .Map ;
3939import java .util .Optional ;
4040import java .util .Set ;
41+ import java .util .StringJoiner ;
42+ import java .util .concurrent .atomic .AtomicReference ;
4143import java .util .function .Consumer ;
4244import java .util .function .Function ;
4345import java .util .function .Predicate ;
@@ -127,36 +129,36 @@ public HelpSystemHelper(Config config, Database database, ChatGptService chatGpt
127129 RestAction <Message > constructChatGptAttempt (ThreadChannel threadChannel ,
128130 String originalQuestion , ComponentIdInteractor componentIdInteractor ) {
129131 Optional <String > questionOptional = prepareChatGptQuestion (threadChannel , originalQuestion );
130- Optional <String > chatGPTAnswer ;
132+ Optional <String > chatGptAnswer ;
131133
132134 if (questionOptional .isEmpty ()) {
133135 return useChatGptFallbackMessage (threadChannel );
134136 }
135137 String question = questionOptional .get ();
136- logger .debug ("The final question sent to chatGPT: {}" , question );
137138
138139 ForumTag defaultTag = threadChannel .getAppliedTags ().getFirst ();
139140 ForumTag matchingTag = getCategoryTagOfChannel (threadChannel ).orElse (defaultTag );
140141
141- String context = matchingTag .getName ();
142- chatGPTAnswer = chatGptService .ask (question , context );
142+ String context =
143+ "Category %s on a Java Q&A discord server" .formatted (matchingTag .getName ());
144+ chatGptAnswer = chatGptService .ask (question , context );
143145
144- if (chatGPTAnswer .isEmpty ()) {
146+ if (chatGptAnswer .isEmpty ()) {
145147 return useChatGptFallbackMessage (threadChannel );
146148 }
147149
148- StringBuilder idForDismissButton = new StringBuilder ( );
149- RestAction <Message > message =
150+ AtomicReference < String > messageId = new AtomicReference <>( "" );
151+ RestAction <Message > post =
150152 mentionGuildSlashCommand (threadChannel .getGuild (), ChatGptCommand .COMMAND_NAME )
151153 .map ("""
152154 Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! \
153155 In any case, a human is on the way 👍. To continue talking to the AI, you can use \
154156 %s.
155157 """ ::formatted )
156158 .flatMap (threadChannel ::sendMessage )
157- .onSuccess (m -> idForDismissButton . append ( m .getId ()));
159+ .onSuccess (message -> messageId . set ( message .getId ()));
158160
159- String answer = chatGPTAnswer .orElseThrow ();
161+ String answer = chatGptAnswer .orElseThrow ();
160162 SelfUser selfUser = threadChannel .getJDA ().getSelfUser ();
161163
162164 int responseCharLimit = MessageEmbed .DESCRIPTION_MAX_LENGTH ;
@@ -165,9 +167,8 @@ RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
165167 }
166168
167169 MessageEmbed responseEmbed = generateGptResponseEmbed (answer , selfUser , originalQuestion );
168- return message .flatMap (any -> threadChannel .sendMessageEmbeds (responseEmbed )
169- .addActionRow (
170- generateDismissButton (componentIdInteractor , idForDismissButton .toString ())));
170+ return post .flatMap (any -> threadChannel .sendMessageEmbeds (responseEmbed )
171+ .addActionRow (generateDismissButton (componentIdInteractor , messageId .get ())));
171172 }
172173
173174 /**
@@ -204,24 +205,21 @@ private Button generateDismissButton(ComponentIdInteractor componentIdInteractor
204205
205206 private Optional <String > prepareChatGptQuestion (ThreadChannel threadChannel ,
206207 String originalQuestion ) {
208+ StringJoiner question = new StringJoiner (" - " );
209+
207210 String questionTitle = threadChannel .getName ();
208- StringBuilder questionBuilder = new StringBuilder (MAX_QUESTION_LENGTH );
211+ question .add (questionTitle );
212+ question .add (originalQuestion .substring (0 ,
213+ Math .min (originalQuestion .length (), MAX_QUESTION_LENGTH )));
209214
210- if ( originalQuestion . length () < MIN_QUESTION_LENGTH
211- && questionTitle .length () < MIN_QUESTION_LENGTH ) {
215+ // Not enough content for meaningful responses
216+ if ( question .length () < MIN_QUESTION_LENGTH ) {
212217 return Optional .empty ();
213218 }
214219
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 ());
220+ question .add (
221+ "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." );
222+ return Optional .of (question .toString ());
225223 }
226224
227225 private RestAction <Message > useChatGptFallbackMessage (ThreadChannel threadChannel ) {
0 commit comments