From f70ddb5b4a6437ca644461facae46e5b7a11595a Mon Sep 17 00:00:00 2001 From: christolis Date: Tue, 12 Mar 2024 14:23:18 +0200 Subject: [PATCH 01/11] wip: base code for gpt-formatter --- .../togetherjava/tjbot/features/Features.java | 4 +- .../features/basic/ChatGPTFormatter.java | 17 ++++ .../features/chatgpt/ChatGptService.java | 81 ++++++++++++++----- .../features/code/CodeMessageHandler.java | 10 ++- .../features/code/FormatCodeCommand.java | 9 ++- 5 files changed, 94 insertions(+), 27 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java diff --git a/application/src/main/java/org/togetherjava/tjbot/features/Features.java b/application/src/main/java/org/togetherjava/tjbot/features/Features.java index 5cbfb7cea7..a2bfe00580 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/Features.java @@ -88,9 +88,9 @@ public static Collection createFeatures(JDA jda, Database database, Con ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config); ScamHistoryStore scamHistoryStore = new ScamHistoryStore(database); GitHubReference githubReference = new GitHubReference(config); - CodeMessageHandler codeMessageHandler = - new CodeMessageHandler(blacklistConfig.special(), jshellEval); ChatGptService chatGptService = new ChatGptService(config); + CodeMessageHandler codeMessageHandler = + new CodeMessageHandler(blacklistConfig.special(), jshellEval, chatGptService); HelpSystemHelper helpSystemHelper = new HelpSystemHelper(config, database, chatGptService); // NOTE The system can add special system relevant commands also by itself, diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java new file mode 100644 index 0000000000..a04cbbb1f0 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java @@ -0,0 +1,17 @@ +package org.togetherjava.tjbot.features.basic; + +import org.togetherjava.tjbot.features.chatgpt.ChatGptService; + +import java.util.Optional; + +public final class ChatGPTFormatter { + private final ChatGptService chatGptService; + + public ChatGPTFormatter(ChatGptService chatGptService) { + this.chatGptService = chatGptService; + } + + public Optional format(CharSequence code) { + return chatGptService.formatCode(code); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java index e8b02d04bb..3c3376b265 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java @@ -100,28 +100,8 @@ public Optional ask(String question, String context) { String instructions = "KEEP IT CONCISE, NOT MORE THAN 280 WORDS"; String questionWithContext = "context: Category %s on a Java Q&A discord server. %s %s" .formatted(context, instructions, question); - ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), - Objects.requireNonNull(questionWithContext)); - ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() - .model(AI_MODEL) - .messages(List.of(chatMessage)) - .frequencyPenalty(FREQUENCY_PENALTY) - .temperature(TEMPERATURE) - .maxTokens(MAX_TOKENS) - .n(MAX_NUMBER_OF_RESPONSES) - .build(); - - String response = openAiService.createChatCompletion(chatCompletionRequest) - .getChoices() - .getFirst() - .getMessage() - .getContent(); - - if (response == null) { - return Optional.empty(); - } - - return Optional.of(response); + + return getMessageResponse(questionWithContext); } catch (OpenAiHttpException openAiHttpException) { logger.warn( "There was an error using the OpenAI API: {} Code: {} Type: {} Status Code: {}", @@ -133,4 +113,61 @@ public Optional ask(String question, String context) { } return Optional.empty(); } + + public Optional formatCode(CharSequence code) { + if (isDisabled) { + return Optional.empty(); + } + + String payload = String.format( + """ + If you happen to find any code in the container below, FORMAT \ + IT regardless of the programming language you find. MAKE IT HUMANLY READABLE. \ + If you don't find any, then your only answer should say empty. Output with no \ + introduction, no explanation, no ``` stuff, only code. Double check that \ + your response is correct. The code provided might not be readable. + + --- BEGIN CODE --- + %s + --- END CODE --- + """, + code); + Optional response = getMessageResponse(payload); + + if (response.isEmpty() || response.get().equalsIgnoreCase("empty")) { + return Optional.empty(); + } + + return response; + } + + private Optional getMessageResponse(String message) { + ChatMessage chatMessage = + new ChatMessage(ChatMessageRole.USER.value(), Objects.requireNonNull(message)); + ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() + .model(AI_MODEL) + .messages(List.of(chatMessage)) + .frequencyPenalty(FREQUENCY_PENALTY) + .temperature(TEMPERATURE) + .maxTokens(MAX_TOKENS) + .n(MAX_NUMBER_OF_RESPONSES) + .build(); + + logger.debug("GPT tx payload: {}", message); + + String response = openAiService.createChatCompletion(chatCompletionRequest) + .getChoices() + .getFirst() + .getMessage() + .getContent(); + + if (response == null) { + logger.debug("Got empty response"); + return Optional.empty(); + } + + logger.debug("GPT rx: {}", response); + + return Optional.of(response); + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java b/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java index bf9e8f54cd..de5bc1bcc4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java @@ -17,6 +17,7 @@ import org.togetherjava.tjbot.features.MessageReceiverAdapter; import org.togetherjava.tjbot.features.UserInteractionType; import org.togetherjava.tjbot.features.UserInteractor; +import org.togetherjava.tjbot.features.chatgpt.ChatGptService; import org.togetherjava.tjbot.features.componentids.ComponentIdGenerator; import org.togetherjava.tjbot.features.componentids.ComponentIdInteractor; import org.togetherjava.tjbot.features.jshell.JShellEval; @@ -49,6 +50,7 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements private final ComponentIdInteractor componentIdInteractor; private final Map labelToCodeAction; + private final ChatGptService chatGptService; /** * Memorizes the ID of the bots code-reply message that a message belongs to. That way, the @@ -68,11 +70,15 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements * disabled * @param jshellEval used to execute java code and build visual result */ - public CodeMessageHandler(FeatureBlacklist blacklist, JShellEval jshellEval) { + public CodeMessageHandler(FeatureBlacklist blacklist, JShellEval jshellEval, + ChatGptService chatGptService) { componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName()); + this.chatGptService = chatGptService; List codeActions = blacklist - .filterStream(Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)), + .filterStream( + Stream.of(new FormatCodeCommand(chatGptService), + new EvalCodeCommand(jshellEval)), codeAction -> codeAction.getClass().getName()) .toList(); diff --git a/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java index 2072daf6ba..06745c8170 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java @@ -3,6 +3,8 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.MessageEmbed; +import org.togetherjava.tjbot.features.basic.ChatGPTFormatter; +import org.togetherjava.tjbot.features.chatgpt.ChatGptService; import org.togetherjava.tjbot.features.utils.CodeFence; import org.togetherjava.tjbot.formatter.Formatter; @@ -13,6 +15,11 @@ */ final class FormatCodeCommand implements CodeAction { private final Formatter formatter = new Formatter(); + private final ChatGPTFormatter chatGPTFormatter; + + public FormatCodeCommand(ChatGptService service) { + this.chatGPTFormatter = new ChatGPTFormatter(service); + } @Override public String getLabel() { @@ -34,6 +41,6 @@ public MessageEmbed apply(CodeFence codeFence) { } private String formatCode(CharSequence code) { - return formatter.format(code); + return chatGPTFormatter.format(code).orElse(formatter.format(code)); } } From 4440992da385e90420b4b138b6fe1d95ceae12fe Mon Sep 17 00:00:00 2001 From: christolis Date: Tue, 12 Mar 2024 14:36:35 +0200 Subject: [PATCH 02/11] feat: send empty optional if length exceeds threshold --- .../togetherjava/tjbot/features/chatgpt/ChatGptService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java index 3c3376b265..9b89d4f8f8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java @@ -45,6 +45,7 @@ public class ChatGptService { * a separate iteration based on the input. */ private static final int MAX_NUMBER_OF_RESPONSES = 1; + private static final int MAX_CODE_LENGTH = 2000; private static final String AI_MODEL = "gpt-3.5-turbo"; private boolean isDisabled = false; @@ -115,7 +116,7 @@ public Optional ask(String question, String context) { } public Optional formatCode(CharSequence code) { - if (isDisabled) { + if (isDisabled || code.length() > MAX_CODE_LENGTH) { return Optional.empty(); } From 0e8f304117559ec283993a65a3a065f5f118f9a9 Mon Sep 17 00:00:00 2001 From: christolis Date: Tue, 12 Mar 2024 14:38:50 +0200 Subject: [PATCH 03/11] fix: remove unused field --- .../togetherjava/tjbot/features/code/CodeMessageHandler.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java b/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java index de5bc1bcc4..8637447bc5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java @@ -50,7 +50,6 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements private final ComponentIdInteractor componentIdInteractor; private final Map labelToCodeAction; - private final ChatGptService chatGptService; /** * Memorizes the ID of the bots code-reply message that a message belongs to. That way, the @@ -73,7 +72,6 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements public CodeMessageHandler(FeatureBlacklist blacklist, JShellEval jshellEval, ChatGptService chatGptService) { componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName()); - this.chatGptService = chatGptService; List codeActions = blacklist .filterStream( From 41f6e946ac43f58e37092c796194004df76a4e77 Mon Sep 17 00:00:00 2001 From: christolis Date: Tue, 12 Mar 2024 14:53:51 +0200 Subject: [PATCH 04/11] refactor: move try-catch and fix JavaDocs --- .../features/chatgpt/ChatGptService.java | 95 +++++++++++-------- 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java index 9b89d4f8f8..acc21d0326 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java @@ -97,24 +97,20 @@ public Optional ask(String question, String context) { return Optional.empty(); } - try { - String instructions = "KEEP IT CONCISE, NOT MORE THAN 280 WORDS"; - String questionWithContext = "context: Category %s on a Java Q&A discord server. %s %s" - .formatted(context, instructions, question); + String instructions = "KEEP IT CONCISE, NOT MORE THAN 280 WORDS"; + String questionWithContext = "context: Category %s on a Java Q&A discord server. %s %s" + .formatted(context, instructions, question); - return getMessageResponse(questionWithContext); - } catch (OpenAiHttpException openAiHttpException) { - logger.warn( - "There was an error using the OpenAI API: {} Code: {} Type: {} Status Code: {}", - openAiHttpException.getMessage(), openAiHttpException.code, - openAiHttpException.type, openAiHttpException.statusCode); - } catch (RuntimeException runtimeException) { - logger.warn("There was an error using the OpenAI API: {}", - runtimeException.getMessage()); - } - return Optional.empty(); + return getMessageResponse(questionWithContext); } + /** + * Prompt ChatGPT with code for it to format it. + * + * @param code the code to be formatted by ChatGPT. If code exceeds {@value MAX_CODE_LENGTH} + * characters then this method returns an empty {@link Optional} + * @return an optional response from ChatGPT as a String + */ public Optional formatCode(CharSequence code) { if (isDisabled || code.length() > MAX_CODE_LENGTH) { return Optional.empty(); @@ -133,6 +129,7 @@ public Optional formatCode(CharSequence code) { --- END CODE --- """, code); + Optional response = getMessageResponse(payload); if (response.isEmpty() || response.get().equalsIgnoreCase("empty")) { @@ -142,33 +139,49 @@ public Optional formatCode(CharSequence code) { return response; } + /** + * Prompt ChatGPT with a message and get its response. + * + * @param message the message to send to ChatGPT + * @return an optional response from ChatGPT as a String + */ private Optional getMessageResponse(String message) { - ChatMessage chatMessage = - new ChatMessage(ChatMessageRole.USER.value(), Objects.requireNonNull(message)); - ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() - .model(AI_MODEL) - .messages(List.of(chatMessage)) - .frequencyPenalty(FREQUENCY_PENALTY) - .temperature(TEMPERATURE) - .maxTokens(MAX_TOKENS) - .n(MAX_NUMBER_OF_RESPONSES) - .build(); - - logger.debug("GPT tx payload: {}", message); - - String response = openAiService.createChatCompletion(chatCompletionRequest) - .getChoices() - .getFirst() - .getMessage() - .getContent(); - - if (response == null) { - logger.debug("Got empty response"); - return Optional.empty(); + try { + ChatMessage chatMessage = + new ChatMessage(ChatMessageRole.USER.value(), Objects.requireNonNull(message)); + ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() + .model(AI_MODEL) + .messages(List.of(chatMessage)) + .frequencyPenalty(FREQUENCY_PENALTY) + .temperature(TEMPERATURE) + .maxTokens(MAX_TOKENS) + .n(MAX_NUMBER_OF_RESPONSES) + .build(); + + logger.debug("GPT tx payload: {}", message); + + String response = openAiService.createChatCompletion(chatCompletionRequest) + .getChoices() + .getFirst() + .getMessage() + .getContent(); + + if (response == null) { + logger.debug("Got empty response"); + return Optional.empty(); + } + + logger.debug("GPT rx: {}", response); + + return Optional.of(response); + } catch (OpenAiHttpException openAiHttpException) { + logger.warn( + "There was an error using the OpenAI API: {} Code: {} Type: {} Status Code: {}", + openAiHttpException.getMessage(), openAiHttpException.code, + openAiHttpException.type, openAiHttpException.statusCode); + } catch (RuntimeException runtimeException) { + logger.warn("There was an error using the OpenAI API: {}", + runtimeException.getMessage()); } - - logger.debug("GPT rx: {}", response); - - return Optional.of(response); } } From 88ebdd77f83a23c2e1e9805b3668bcc8ac9ced20 Mon Sep 17 00:00:00 2001 From: christolis Date: Tue, 12 Mar 2024 14:55:01 +0200 Subject: [PATCH 05/11] feat: remove excess debugging --- .../togetherjava/tjbot/features/chatgpt/ChatGptService.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java index acc21d0326..2ce0753cb8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java @@ -158,8 +158,6 @@ private Optional getMessageResponse(String message) { .n(MAX_NUMBER_OF_RESPONSES) .build(); - logger.debug("GPT tx payload: {}", message); - String response = openAiService.createChatCompletion(chatCompletionRequest) .getChoices() .getFirst() @@ -167,12 +165,9 @@ private Optional getMessageResponse(String message) { .getContent(); if (response == null) { - logger.debug("Got empty response"); return Optional.empty(); } - logger.debug("GPT rx: {}", response); - return Optional.of(response); } catch (OpenAiHttpException openAiHttpException) { logger.warn( From 9c9b6d44343d650e89c922895ac4d4c97b2a737b Mon Sep 17 00:00:00 2001 From: christolis Date: Tue, 12 Mar 2024 15:04:11 +0200 Subject: [PATCH 06/11] docs: add more JavaDocs --- .../tjbot/features/basic/ChatGPTFormatter.java | 14 ++++++++++++++ .../tjbot/features/code/FormatCodeCommand.java | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java index a04cbbb1f0..2c25956d1e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java @@ -4,13 +4,27 @@ import java.util.Optional; +/** + * A utility class for formatting code using ChatGPT. + */ public final class ChatGPTFormatter { private final ChatGptService chatGptService; + /** + * Constructs a new {@link ChatGPTFormatter} with the provided {@link ChatGptService}. + * + * @param chatGptService the {@link ChatGptService} used for formatting code + */ public ChatGPTFormatter(ChatGptService chatGptService) { this.chatGptService = chatGptService; } + /** + * Formats the provided code using ChatGPT. + * + * @param code the code to be formatted + * @return an Optional containing the formatted code if successful, empty otherwise + */ public Optional format(CharSequence code) { return chatGptService.formatCode(code); } diff --git a/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java index 06745c8170..dbad39bea9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java @@ -40,6 +40,12 @@ public MessageEmbed apply(CodeFence codeFence) { .build(); } + /** + * Formats the provided code using either the ChatGPT formatter or the generic formatter. + * + * @param code the code to be formatted + * @return the formatted code + */ private String formatCode(CharSequence code) { return chatGPTFormatter.format(code).orElse(formatter.format(code)); } From 0e00a0597c3e82c2903722ea1b0402571eaac095 Mon Sep 17 00:00:00 2001 From: christolis Date: Tue, 12 Mar 2024 15:07:00 +0200 Subject: [PATCH 07/11] fix: add missing return statement --- .../org/togetherjava/tjbot/features/chatgpt/ChatGptService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java index 2ce0753cb8..98852a59f4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java @@ -178,5 +178,7 @@ private Optional getMessageResponse(String message) { logger.warn("There was an error using the OpenAI API: {}", runtimeException.getMessage()); } + + return Optional.empty(); } } From c5768d2cf615b61cb368fd9af2fe947ee94f16b5 Mon Sep 17 00:00:00 2001 From: christolis Date: Sat, 16 Mar 2024 12:04:31 +0200 Subject: [PATCH 08/11] refactor(gpt-formatter): move from basic to code pkg --- .../tjbot/features/{basic => code}/ChatGPTFormatter.java | 2 +- .../org/togetherjava/tjbot/features/code/FormatCodeCommand.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) rename application/src/main/java/org/togetherjava/tjbot/features/{basic => code}/ChatGPTFormatter.java (94%) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java b/application/src/main/java/org/togetherjava/tjbot/features/code/ChatGPTFormatter.java similarity index 94% rename from application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java rename to application/src/main/java/org/togetherjava/tjbot/features/code/ChatGPTFormatter.java index 2c25956d1e..2463f2e57b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/ChatGPTFormatter.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/code/ChatGPTFormatter.java @@ -1,4 +1,4 @@ -package org.togetherjava.tjbot.features.basic; +package org.togetherjava.tjbot.features.code; import org.togetherjava.tjbot.features.chatgpt.ChatGptService; diff --git a/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java index dbad39bea9..66785be970 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java @@ -3,7 +3,6 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.MessageEmbed; -import org.togetherjava.tjbot.features.basic.ChatGPTFormatter; import org.togetherjava.tjbot.features.chatgpt.ChatGptService; import org.togetherjava.tjbot.features.utils.CodeFence; import org.togetherjava.tjbot.formatter.Formatter; From 775699c124383387885deca6cb2b451a4cc1f531 Mon Sep 17 00:00:00 2001 From: christolis Date: Sat, 16 Mar 2024 12:06:14 +0200 Subject: [PATCH 09/11] docs: rewrite method header --- .../org/togetherjava/tjbot/features/chatgpt/ChatGptService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java index 98852a59f4..a099b74b07 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java @@ -105,7 +105,7 @@ public Optional ask(String question, String context) { } /** - * Prompt ChatGPT with code for it to format it. + * Provide ChatGPT with code to format. * * @param code the code to be formatted by ChatGPT. If code exceeds {@value MAX_CODE_LENGTH} * characters then this method returns an empty {@link Optional} From 5aa116be6fda3384c6719fecb6b9423b4a345d7e Mon Sep 17 00:00:00 2001 From: christolis Date: Sat, 16 Mar 2024 12:10:07 +0200 Subject: [PATCH 10/11] feat(gpt-formatter): remove unnecessary checks --- .../tjbot/features/chatgpt/ChatGptService.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java index a099b74b07..96191b35db 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java @@ -120,9 +120,8 @@ public Optional formatCode(CharSequence code) { """ If you happen to find any code in the container below, FORMAT \ IT regardless of the programming language you find. MAKE IT HUMANLY READABLE. \ - If you don't find any, then your only answer should say empty. Output with no \ - introduction, no explanation, no ``` stuff, only code. Double check that \ - your response is correct. The code provided might not be readable. + Output with no introduction, no explanation, no ``` stuff, only code. Double \ + check that your response is correct. The code provided might not be readable. --- BEGIN CODE --- %s @@ -130,13 +129,7 @@ public Optional formatCode(CharSequence code) { """, code); - Optional response = getMessageResponse(payload); - - if (response.isEmpty() || response.get().equalsIgnoreCase("empty")) { - return Optional.empty(); - } - - return response; + return getMessageResponse(payload); } /** From 4548a6be74c0d2581a990f2edaaec8c5f5759ce3 Mon Sep 17 00:00:00 2001 From: christolis Date: Tue, 19 Mar 2024 22:39:29 +0200 Subject: [PATCH 11/11] docs: create constructor docs --- .../togetherjava/tjbot/features/code/FormatCodeCommand.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java index 66785be970..a2849312d7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/code/FormatCodeCommand.java @@ -16,6 +16,11 @@ final class FormatCodeCommand implements CodeAction { private final Formatter formatter = new Formatter(); private final ChatGPTFormatter chatGPTFormatter; + /** + * Initializes a {@link FormatCodeCommand} which formats code. + * + * @param service the {@link ChatGptService} instance to be used for code formatting + */ public FormatCodeCommand(ChatGptService service) { this.chatGPTFormatter = new ChatGPTFormatter(service); }