Skip to content

Commit

Permalink
Chat gpt attempt questions (#873)
Browse files Browse the repository at this point in the history
* * Refactor folder name
* Add ChatGPT command/service code and utils.

* * Fix Features.java import statements and add ChatGPTService being passed in to helper.
* Add parsing of question to helper.

* * Add AI Help message to when user asks question. (Either direction to the command if the service fails or an explanation that the next response is AI generated)

* * Add test files and test suite for AIResponseParser.java
* Add logger.debug() to catch responses before being parsed in case of failing to parse correctly to generate new tests [AIResponseParser.java].

* * Refactor how AIResponseParserTest parameterizes file names (ints instead of full file names).
* Remove ChatGPTServiceTest.java as it was an older way to generate long responses from ChatGPT for testing.
* Add another test file and include in parameters for AIResponseParserTest.java
  • Loading branch information
tmcdonnell2 authored Aug 27, 2023
1 parent 03b1112 commit ae006d2
Show file tree
Hide file tree
Showing 12 changed files with 566 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,46 @@

import org.togetherjava.tjbot.config.Config;
import org.togetherjava.tjbot.db.Database;
import org.togetherjava.tjbot.features.basic.*;
import org.togetherjava.tjbot.features.bookmarks.*;
import org.togetherjava.tjbot.features.chaptgpt.ChatGptCommand;
import org.togetherjava.tjbot.features.chaptgpt.ChatGptService;
import org.togetherjava.tjbot.features.basic.PingCommand;
import org.togetherjava.tjbot.features.basic.RoleSelectCommand;
import org.togetherjava.tjbot.features.basic.SlashCommandEducator;
import org.togetherjava.tjbot.features.basic.SuggestionsUpDownVoter;
import org.togetherjava.tjbot.features.bookmarks.BookmarksCommand;
import org.togetherjava.tjbot.features.bookmarks.BookmarksSystem;
import org.togetherjava.tjbot.features.bookmarks.LeftoverBookmarksCleanupRoutine;
import org.togetherjava.tjbot.features.bookmarks.LeftoverBookmarksListener;
import org.togetherjava.tjbot.features.chatgpt.ChatGptCommand;
import org.togetherjava.tjbot.features.chatgpt.ChatGptService;
import org.togetherjava.tjbot.features.code.CodeMessageAutoDetection;
import org.togetherjava.tjbot.features.code.CodeMessageHandler;
import org.togetherjava.tjbot.features.code.CodeMessageManualDetection;
import org.togetherjava.tjbot.features.filesharing.FileSharingMessageListener;
import org.togetherjava.tjbot.features.help.*;
import org.togetherjava.tjbot.features.help.AutoPruneHelperRoutine;
import org.togetherjava.tjbot.features.help.GuildLeaveCloseThreadListener;
import org.togetherjava.tjbot.features.help.HelpSystemHelper;
import org.togetherjava.tjbot.features.help.HelpThreadActivityUpdater;
import org.togetherjava.tjbot.features.help.HelpThreadAutoArchiver;
import org.togetherjava.tjbot.features.help.HelpThreadCommand;
import org.togetherjava.tjbot.features.help.HelpThreadCreatedListener;
import org.togetherjava.tjbot.features.help.HelpThreadMetadataPurger;
import org.togetherjava.tjbot.features.jshell.JShellCommand;
import org.togetherjava.tjbot.features.jshell.JShellEval;
import org.togetherjava.tjbot.features.mathcommands.TeXCommand;
import org.togetherjava.tjbot.features.mathcommands.wolframalpha.WolframAlphaCommand;
import org.togetherjava.tjbot.features.mediaonly.MediaOnlyChannelListener;
import org.togetherjava.tjbot.features.moderation.*;
import org.togetherjava.tjbot.features.moderation.BanCommand;
import org.togetherjava.tjbot.features.moderation.KickCommand;
import org.togetherjava.tjbot.features.moderation.ModerationActionsStore;
import org.togetherjava.tjbot.features.moderation.MuteCommand;
import org.togetherjava.tjbot.features.moderation.NoteCommand;
import org.togetherjava.tjbot.features.moderation.QuarantineCommand;
import org.togetherjava.tjbot.features.moderation.RejoinModerationRoleListener;
import org.togetherjava.tjbot.features.moderation.ReportCommand;
import org.togetherjava.tjbot.features.moderation.UnbanCommand;
import org.togetherjava.tjbot.features.moderation.UnmuteCommand;
import org.togetherjava.tjbot.features.moderation.UnquarantineCommand;
import org.togetherjava.tjbot.features.moderation.WarnCommand;
import org.togetherjava.tjbot.features.moderation.WhoIsCommand;
import org.togetherjava.tjbot.features.moderation.attachment.BlacklistedAttachmentListener;
import org.togetherjava.tjbot.features.moderation.audit.AuditCommand;
import org.togetherjava.tjbot.features.moderation.audit.ModAuditLogRoutine;
Expand Down Expand Up @@ -76,9 +100,9 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
ModerationActionsStore actionsStore = new ModerationActionsStore(database);
ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config);
ScamHistoryStore scamHistoryStore = new ScamHistoryStore(database);
HelpSystemHelper helpSystemHelper = new HelpSystemHelper(config, database);
CodeMessageHandler codeMessageHandler = new CodeMessageHandler(jshellEval);
ChatGptService chatGptService = new ChatGptService(config);
HelpSystemHelper helpSystemHelper = new HelpSystemHelper(config, database, chatGptService);

// NOTE The system can add special system relevant commands also by itself,
// hence this list may not necessarily represent the full list of all commands actually
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.togetherjava.tjbot.features.chatgpt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Represents a class to partition long text blocks into smaller blocks which work with Discord's
* API. Initially constructed to partition text from AI text generation APIs.
*/
public class AIResponseParser {
private AIResponseParser() {
throw new UnsupportedOperationException("Utility class, construction not supported");
}

private static final Logger logger = LoggerFactory.getLogger(AIResponseParser.class);
private static final int RESPONSE_LENGTH_LIMIT = 2_000;

/**
* Parses the response generated by AI. If response is longer than
* {@value RESPONSE_LENGTH_LIMIT}, then breaks apart the response into suitable lengths for
* Discords API.
*
* @param response The response from the AI which we want to send over Discord.
* @return An array potentially holding the original response split up into shorter than
* {@value RESPONSE_LENGTH_LIMIT} length pieces.
*/
public static String[] parse(String response) {
String[] partedResponse = new String[] {response};
if (response.length() > RESPONSE_LENGTH_LIMIT) {
logger.debug("Response to parse:\n{}", response);
partedResponse = partitionAiResponse(response);
}

return partedResponse;
}

private static String[] partitionAiResponse(String response) {
List<String> responseChunks = new ArrayList<>();
String[] splitResponseOnMarks = response.split("```");

for (int i = 0; i < splitResponseOnMarks.length; i++) {
String split = splitResponseOnMarks[i];
List<String> chunks = new ArrayList<>();
chunks.add(split);

// Check each chunk for correct length. If over the length, split in two and check
// again.
while (!chunks.stream().allMatch(s -> s.length() < RESPONSE_LENGTH_LIMIT)) {
for (int j = 0; j < chunks.size(); j++) {
String chunk = chunks.get(j);
if (chunk.length() > RESPONSE_LENGTH_LIMIT) {
int midpointNewline = chunk.lastIndexOf("\n", chunk.length() / 2);
chunks.set(j, chunk.substring(0, midpointNewline));
chunks.add(j + 1, chunk.substring(midpointNewline));
}
}
}

// Given the splitting on ```, the odd numbered entries need to have code marks
// restored.
if (i % 2 != 0) {
// We assume that everything after the ``` on the same line is the language
// declaration. Could be empty.
String lang = split.substring(0, split.indexOf(System.lineSeparator()));
chunks = chunks.stream()
.map(s -> ("```" + lang).concat(s).concat("```"))
// Handle case of doubling language declaration
.map(s -> s.replaceFirst("```" + lang + lang, "```" + lang))
.collect(Collectors.toList());
}

List<String> list = chunks.stream().filter(string -> !string.equals("")).toList();
responseChunks.addAll(list);
} // end of for loop.

return responseChunks.toArray(new String[0]);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.togetherjava.tjbot.features.chaptgpt;
package org.togetherjava.tjbot.features.chatgpt;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
Expand All @@ -22,6 +22,7 @@
* which it will respond with an AI generated answer.
*/
public final class ChatGptCommand extends SlashCommandAdapter {
public static final String COMMAND_NAME = "chatgpt";
private static final String QUESTION_INPUT = "question";
private static final int MAX_MESSAGE_INPUT_LENGTH = 200;
private static final int MIN_MESSAGE_INPUT_LENGTH = 4;
Expand All @@ -37,7 +38,7 @@ public final class ChatGptCommand extends SlashCommandAdapter {
* @param chatGptService ChatGptService - Needed to make calls to ChatGPT API
*/
public ChatGptCommand(ChatGptService chatGptService) {
super("chatgpt", "Ask the ChatGPT AI a question!", CommandVisibility.GUILD);
super(COMMAND_NAME, "Ask the ChatGPT AI a question!", CommandVisibility.GUILD);

this.chatGptService = chatGptService;
}
Expand Down Expand Up @@ -73,14 +74,20 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
event.deferReply().queue();

Optional<String> optional =
Optional<String[]> optional =
chatGptService.ask(event.getValue(QUESTION_INPUT).getAsString());
if (optional.isPresent()) {
userIdToAskedAtCache.put(event.getMember().getId(), Instant.now());
}

String response = optional.orElse(
"An error has occurred while trying to communicate with ChatGPT. Please try again later");
event.getHook().sendMessage(response).queue();
String[] errorResponse = {"""
An error has occurred while trying to communicate with ChatGPT.
Please try again later.
"""};

String[] response = optional.orElse(errorResponse);
for (String message : response) {
event.getHook().sendMessage(message).queue();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.togetherjava.tjbot.features.chaptgpt;
package org.togetherjava.tjbot.features.chatgpt;

import com.theokanning.openai.OpenAiHttpException;
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
Expand All @@ -22,12 +22,13 @@ public class ChatGptService {
private static final Logger logger = LoggerFactory.getLogger(ChatGptService.class);
private static final Duration TIMEOUT = Duration.ofSeconds(90);
private static final int MAX_TOKENS = 3_000;
private static final String AI_MODEL = "gpt-3.5-turbo";
private boolean isDisabled = false;
private final OpenAiService openAiService;

/**
* Creates instance of ChatGPTService
*
*
* @param config needed for token to OpenAI API.
*/
public ChatGptService(Config config) {
Expand All @@ -37,17 +38,34 @@ public ChatGptService(Config config) {
}

openAiService = new OpenAiService(apiKey, TIMEOUT);

ChatMessage setupMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(),
"""
Please answer questions in 1500 characters or less. Remember to count spaces in the
character limit. For code supplied for review, refer to the old code supplied rather than
rewriting the code. Don't supply a corrected version of the code.\s""");
ChatCompletionRequest systemSetupRequest = ChatCompletionRequest.builder()
.model(AI_MODEL)
.messages(List.of(setupMessage))
.frequencyPenalty(0.5)
.temperature(0.3)
.maxTokens(50)
.n(1)
.build();

// Sending the system setup message to ChatGPT.
openAiService.createChatCompletion(systemSetupRequest);
}

/**
* Prompt ChatGPT with a question and receive a response.
*
*
* @param question The question being asked of ChatGPT. Max is {@value MAX_TOKENS} tokens.
* @return partitioned response from ChatGPT as a String array.
* @see <a href="https://platform.openai.com/docs/guides/chat/managing-tokens">ChatGPT
* Tokens</a>.
* @return response from ChatGPT as a String.
*/
public Optional<String> ask(String question) {
public Optional<String[]> ask(String question) {
if (isDisabled) {
return Optional.empty();
}
Expand All @@ -56,18 +74,25 @@ public Optional<String> ask(String question) {
ChatMessage chatMessage =
new ChatMessage(ChatMessageRole.USER.value(), Objects.requireNonNull(question));
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
.model("gpt-3.5-turbo")
.model(AI_MODEL)
.messages(List.of(chatMessage))
.frequencyPenalty(0.5)
.temperature(0.7)
.temperature(0.3)
.maxTokens(MAX_TOKENS)
.n(1)
.build();
return Optional.ofNullable(openAiService.createChatCompletion(chatCompletionRequest)

String response = openAiService.createChatCompletion(chatCompletionRequest)
.getChoices()
.get(0)
.getMessage()
.getContent());
.getContent();

if (response == null) {
return Optional.empty();
}

return Optional.of(AIResponseParser.parse(response));
} catch (OpenAiHttpException openAiHttpException) {
logger.warn(
"There was an error using the OpenAI API: {} Code: {} Type: {} Status Code: {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
package org.togetherjava.tjbot.features.chaptgpt;
package org.togetherjava.tjbot.features.chatgpt;

import org.togetherjava.tjbot.annotations.MethodsReturnNonnullByDefault;

Expand Down
Loading

0 comments on commit ae006d2

Please sign in to comment.