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,37 @@ 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. You may use markdown syntax for the response"
144+ .formatted (matchingTag .getName ());
145+ chatGptAnswer = chatGptService .ask (question , context );
143146
144- if (chatGPTAnswer .isEmpty ()) {
147+ if (chatGptAnswer .isEmpty ()) {
145148 return useChatGptFallbackMessage (threadChannel );
146149 }
147150
148- StringBuilder idForDismissButton = new StringBuilder ( );
149- RestAction <Message > message =
151+ AtomicReference < String > messageId = new AtomicReference <>( "" );
152+ RestAction <Message > post =
150153 mentionGuildSlashCommand (threadChannel .getGuild (), ChatGptCommand .COMMAND_NAME )
151154 .map ("""
152155 Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! \
153156 In any case, a human is on the way 👍. To continue talking to the AI, you can use \
154157 %s.
155158 """ ::formatted )
156159 .flatMap (threadChannel ::sendMessage )
157- .onSuccess (m -> idForDismissButton . append ( m .getId ()));
160+ .onSuccess (message -> messageId . set ( message .getId ()));
158161
159- String answer = chatGPTAnswer .orElseThrow ();
162+ String answer = chatGptAnswer .orElseThrow ();
160163 SelfUser selfUser = threadChannel .getJDA ().getSelfUser ();
161164
162165 int responseCharLimit = MessageEmbed .DESCRIPTION_MAX_LENGTH ;
@@ -165,9 +168,8 @@ RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
165168 }
166169
167170 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 ())));
171173 }
172174
173175 /**
@@ -204,24 +206,21 @@ private Button generateDismissButton(ComponentIdInteractor componentIdInteractor
204206
205207 private Optional <String > prepareChatGptQuestion (ThreadChannel threadChannel ,
206208 String originalQuestion ) {
209+ StringJoiner question = new StringJoiner (" - " );
210+
207211 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 )));
209215
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 ) {
212218 return Optional .empty ();
213219 }
214220
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 ());
225224 }
226225
227226 private RestAction <Message > useChatGptFallbackMessage (ThreadChannel threadChannel ) {
0 commit comments