From b566b1a2403f4e2619f827204e3cb282a85930fc Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Wed, 11 Sep 2024 19:56:25 +0100 Subject: [PATCH] fixed github ai context issue --- .../commands/admin/AICommand.java | 11 +++--- .../mystiguardian/github/GithubAIModel.java | 36 +++++++------------ .../utils/MystiGuardianUtils.java | 18 ++++++---- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/commands/admin/AICommand.java b/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/commands/admin/AICommand.java index dc16cf0..437a1ee 100644 --- a/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/commands/admin/AICommand.java +++ b/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/commands/admin/AICommand.java @@ -24,6 +24,7 @@ import io.github.yusufsdiscordbot.mystiguardian.utils.PermChecker; import java.util.EnumSet; import java.util.List; +import java.util.Optional; import lombok.val; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; @@ -41,19 +42,17 @@ public void onSlashCommandInteractionEvent( PermChecker permChecker) { val question = event.getOption("question", OptionMapping::getAsString); - var newChat = event.getOption("new-chat", OptionMapping::getAsBoolean); - if (newChat == null) { - newChat = false; - } + var newChat = Optional.ofNullable(event.getOption("new-chat", OptionMapping::getAsBoolean)); + val model = Optional.ofNullable(event.getOption("model", OptionMapping::getAsString)); val githubAIModel = MystiGuardianUtils.getGithubAIModel( - event.getGuild().getIdLong(), event.getMember().getIdLong()); + event.getGuild().getIdLong(), event.getMember().getIdLong(), model); event.deferReply().queue(); githubAIModel - .askQuestion(question, event.getMember().getIdLong(), newChat) + .askQuestion(question, event.getMember().getIdLong(), newChat.orElse(Boolean.FALSE)) .thenAccept((answer) -> event.getHook().editOriginal(answer).queue()) .exceptionally( throwable -> { diff --git a/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/github/GithubAIModel.java b/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/github/GithubAIModel.java index 0c3921e..de298f8 100644 --- a/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/github/GithubAIModel.java +++ b/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/github/GithubAIModel.java @@ -33,7 +33,7 @@ import org.jetbrains.annotations.NotNull; public class GithubAIModel { - private final String model; + private String model; private final String token; private final Map> context = new HashMap<>(); private final List initialMessages = new ArrayList<>(); @@ -47,14 +47,16 @@ public GithubAIModel(String model, String initialPrompt, Long memberId) { this.client = new OkHttpClient(); this.mapper = new ObjectMapper(); initialMessages.add(new Message("system", initialPrompt)); - context.put(memberId, initialMessages); + context.put(memberId, new ArrayList<>(initialMessages)); } public CompletableFuture askQuestion(String question, Long memberId, boolean newChat) { + if (!context.containsKey(memberId)) { + context.put(memberId, new ArrayList<>(initialMessages)); + } - if (newChat && context.containsKey(memberId)) { - context.put(memberId, new ArrayList<>()); - context.get(memberId).addAll(initialMessages); + if (newChat) { + context.put(memberId, new ArrayList<>(initialMessages)); } context.get(memberId).add(new Message("user", question)); @@ -82,7 +84,6 @@ private CompletableFuture sendRequest(long memberId) { new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { - MystiGuardianUtils.logger.error("Error while sending request to AI model", e); future.completeExceptionally(e); } @@ -91,13 +92,8 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { if (response.isSuccessful()) { String responseBody = response.body().string(); - MystiGuardianUtils.logger.debug("Request sent to AI model successfully"); future.complete(parseResponse(responseBody, memberId)); } else { - MystiGuardianUtils.logger.error( - "Error while sending request to AI model. Response code: {}, Response body: {}", - response.code(), - response.body().string()); future.completeExceptionally( new RuntimeException( "Request failed with status code: " + response.code())); @@ -105,7 +101,6 @@ public void onResponse(@NotNull Call call, @NotNull Response response) } }); } catch (JsonProcessingException e) { - MystiGuardianUtils.logger.error("Error while processing JSON for AI model request", e); future.completeExceptionally(e); } return future; @@ -115,7 +110,6 @@ public void onResponse(@NotNull Call call, @NotNull Response response) ObjectNode payload = mapper.createObjectNode(); ArrayNode messages = payload.putArray("messages"); - // Send the full context (message history) for the user for (Message message : context.get(userId)) { ObjectNode messageNode = messages.addObject(); messageNode.put("role", message.role()); @@ -123,29 +117,25 @@ public void onResponse(@NotNull Call call, @NotNull Response response) } payload.put("model", model); - - String jsonInputString = mapper.writeValueAsString(payload); - - MystiGuardianUtils.logger.debug("Request to AI model: {}", jsonInputString); - return RequestBody.create(jsonInputString, MediaType.get("application/json")); + return RequestBody.create( + mapper.writeValueAsString(payload), MediaType.get("application/json")); } private String parseResponse(String responseBody, long memberId) { - MystiGuardianUtils.logger.debug("Response from AI model: {}", responseBody); try { ObjectNode responseJson = (ObjectNode) mapper.readTree(responseBody); String assistantResponse = responseJson.get("choices").get(0).get("message").get("content").asText(); - - // Append assistant's response to the context context.get(memberId).add(new Message("assistant", assistantResponse)); - return assistantResponse; } catch (JsonProcessingException e) { - MystiGuardianUtils.logger.error("Error while parsing AI model response", e); return null; } } + public void setNewModel(String model) { + this.model = model; + } + private record Message(String role, String content) {} } diff --git a/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/utils/MystiGuardianUtils.java b/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/utils/MystiGuardianUtils.java index cfbde09..0182100 100644 --- a/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/utils/MystiGuardianUtils.java +++ b/DiscordBot/src/main/java/io/github/yusufsdiscordbot/mystiguardian/utils/MystiGuardianUtils.java @@ -75,6 +75,8 @@ public class MystiGuardianUtils { private static final SystemInfo systemInfo = new SystemInfo(); private static final CentralProcessor processor = systemInfo.getHardware().getProcessor(); private static final Map githubAIModel = new HashMap<>(); + private static final String AI_PROMPT = + "You are MystiGuardian, your server’s mystical protector and entertainment extraordinaire, created by RealYusufIsmail. As an experienced Java developer active on Discord, your mission is to unite moderation with fun, ensuring a secure and delightful experience for all. You provide helpful, accurate, and timely assistance to users, solving their programming challenges while offering valuable insights to improve their skills. Beyond your technical expertise, you strive to foster a positive and supportive environment, making every interaction productive and uplifting. With your unique combination of wisdom and charm, you guide the server with balance, ensuring both order and entertainment for everyone."; @Getter private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3); @@ -242,15 +244,17 @@ public static String getJavaVendor() { } @NotNull - public static GithubAIModel getGithubAIModel(long id, long userId) { - if (!githubAIModel.containsKey(id)) { - return new GithubAIModel( - "meta-llama-3-8b-instruct", - "You are a java developer, existing on discord. You aim to help others with their problems and make their day better.", - userId); + public static GithubAIModel getGithubAIModel(long guildId, long userId, Optional model) { + if (!githubAIModel.containsKey(guildId)) { + githubAIModel.put( + guildId, new GithubAIModel(model.orElse("meta-llama-3-8b-instruct"), AI_PROMPT, userId)); } - return getGithubAIModel(id, userId); + val githubMode = githubAIModel.get(guildId); + + model.ifPresent(githubMode::setNewModel); + + return githubMode; } public static synchronized void clearGithubAIModel() {