@@ -97,24 +97,20 @@ public Optional<String> ask(String question, String context) {
9797 return Optional .empty ();
9898 }
9999
100- try {
101- String instructions = "KEEP IT CONCISE, NOT MORE THAN 280 WORDS" ;
102- String questionWithContext = "context: Category %s on a Java Q&A discord server. %s %s"
103- .formatted (context , instructions , question );
100+ String instructions = "KEEP IT CONCISE, NOT MORE THAN 280 WORDS" ;
101+ String questionWithContext = "context: Category %s on a Java Q&A discord server. %s %s"
102+ .formatted (context , instructions , question );
104103
105- return getMessageResponse (questionWithContext );
106- } catch (OpenAiHttpException openAiHttpException ) {
107- logger .warn (
108- "There was an error using the OpenAI API: {} Code: {} Type: {} Status Code: {}" ,
109- openAiHttpException .getMessage (), openAiHttpException .code ,
110- openAiHttpException .type , openAiHttpException .statusCode );
111- } catch (RuntimeException runtimeException ) {
112- logger .warn ("There was an error using the OpenAI API: {}" ,
113- runtimeException .getMessage ());
114- }
115- return Optional .empty ();
104+ return getMessageResponse (questionWithContext );
116105 }
117106
107+ /**
108+ * Prompt ChatGPT with code for it to format it.
109+ *
110+ * @param code the code to be formatted by ChatGPT. If code exceeds {@value MAX_CODE_LENGTH}
111+ * characters then this method returns an empty {@link Optional}
112+ * @return an optional response from ChatGPT as a String
113+ */
118114 public Optional <String > formatCode (CharSequence code ) {
119115 if (isDisabled || code .length () > MAX_CODE_LENGTH ) {
120116 return Optional .empty ();
@@ -133,6 +129,7 @@ public Optional<String> formatCode(CharSequence code) {
133129 --- END CODE ---
134130 """ ,
135131 code );
132+
136133 Optional <String > response = getMessageResponse (payload );
137134
138135 if (response .isEmpty () || response .get ().equalsIgnoreCase ("empty" )) {
@@ -142,33 +139,49 @@ public Optional<String> formatCode(CharSequence code) {
142139 return response ;
143140 }
144141
142+ /**
143+ * Prompt ChatGPT with a message and get its response.
144+ *
145+ * @param message the message to send to ChatGPT
146+ * @return an optional response from ChatGPT as a String
147+ */
145148 private Optional <String > getMessageResponse (String message ) {
146- ChatMessage chatMessage =
147- new ChatMessage (ChatMessageRole .USER .value (), Objects .requireNonNull (message ));
148- ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder ()
149- .model (AI_MODEL )
150- .messages (List .of (chatMessage ))
151- .frequencyPenalty (FREQUENCY_PENALTY )
152- .temperature (TEMPERATURE )
153- .maxTokens (MAX_TOKENS )
154- .n (MAX_NUMBER_OF_RESPONSES )
155- .build ();
156-
157- logger .debug ("GPT tx payload: {}" , message );
158-
159- String response = openAiService .createChatCompletion (chatCompletionRequest )
160- .getChoices ()
161- .getFirst ()
162- .getMessage ()
163- .getContent ();
164-
165- if (response == null ) {
166- logger .debug ("Got empty response" );
167- return Optional .empty ();
149+ try {
150+ ChatMessage chatMessage =
151+ new ChatMessage (ChatMessageRole .USER .value (), Objects .requireNonNull (message ));
152+ ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder ()
153+ .model (AI_MODEL )
154+ .messages (List .of (chatMessage ))
155+ .frequencyPenalty (FREQUENCY_PENALTY )
156+ .temperature (TEMPERATURE )
157+ .maxTokens (MAX_TOKENS )
158+ .n (MAX_NUMBER_OF_RESPONSES )
159+ .build ();
160+
161+ logger .debug ("GPT tx payload: {}" , message );
162+
163+ String response = openAiService .createChatCompletion (chatCompletionRequest )
164+ .getChoices ()
165+ .getFirst ()
166+ .getMessage ()
167+ .getContent ();
168+
169+ if (response == null ) {
170+ logger .debug ("Got empty response" );
171+ return Optional .empty ();
172+ }
173+
174+ logger .debug ("GPT rx: {}" , response );
175+
176+ return Optional .of (response );
177+ } catch (OpenAiHttpException openAiHttpException ) {
178+ logger .warn (
179+ "There was an error using the OpenAI API: {} Code: {} Type: {} Status Code: {}" ,
180+ openAiHttpException .getMessage (), openAiHttpException .code ,
181+ openAiHttpException .type , openAiHttpException .statusCode );
182+ } catch (RuntimeException runtimeException ) {
183+ logger .warn ("There was an error using the OpenAI API: {}" ,
184+ runtimeException .getMessage ());
168185 }
169-
170- logger .debug ("GPT rx: {}" , response );
171-
172- return Optional .of (response );
173186 }
174187}
0 commit comments