From 92081194f3a2753a9fccb2649dc28de428fa9446 Mon Sep 17 00:00:00 2001 From: xdnw Date: Thu, 13 Jul 2023 04:31:50 +0100 Subject: [PATCH] update response --- .../commands/manager/v2/command/ICommand.java | 2 +- .../v2/command/ParametricCallable.java | 59 ++++++++++--------- .../manager/v2/impl/pw/CommandManager2.java | 2 +- .../v2/impl/pw/commands/HelpCommands.java | 2 +- .../discord/gpt/imps/ProcessSummarizer.java | 1 + .../discord/gpt/imps/ProcessText2Text.java | 1 + .../discord/gpt/pwembed/CommandEmbedding.java | 12 +++- .../gpt/pwembed/NationMetricEmbedding.java | 2 +- .../discord/gpt/pwembed/PWEmbedding.java | 6 +- .../discord/gpt/pwembed/PWGPTHandler.java | 28 +++++---- .../discord/gpt/pwembed/SettingEmbedding.java | 2 +- .../locutus/discord/util/DocPrinter2.java | 2 +- 12 files changed, 71 insertions(+), 48 deletions(-) diff --git a/src/main/java/link/locutus/discord/commands/manager/v2/command/ICommand.java b/src/main/java/link/locutus/discord/commands/manager/v2/command/ICommand.java index 5c70a211..534584a1 100644 --- a/src/main/java/link/locutus/discord/commands/manager/v2/command/ICommand.java +++ b/src/main/java/link/locutus/discord/commands/manager/v2/command/ICommand.java @@ -23,5 +23,5 @@ default Map getUserParameterMap() { return result; } - String toBasicMarkdown(ValueStore store, PermissionHandler permisser, String prefix, boolean spoiler); + String toBasicMarkdown(ValueStore store, PermissionHandler permisser, String prefix, boolean spoiler, boolean links); } diff --git a/src/main/java/link/locutus/discord/commands/manager/v2/command/ParametricCallable.java b/src/main/java/link/locutus/discord/commands/manager/v2/command/ParametricCallable.java index 4c933a81..d925b7ad 100644 --- a/src/main/java/link/locutus/discord/commands/manager/v2/command/ParametricCallable.java +++ b/src/main/java/link/locutus/discord/commands/manager/v2/command/ParametricCallable.java @@ -443,38 +443,39 @@ public Object call(Object instance, ValueStore store, Object[] paramVals) { } @Override - public String toBasicMarkdown(ValueStore store, PermissionHandler permisser, String prefix, boolean spoiler) { + public String toBasicMarkdown(ValueStore store, PermissionHandler permisser, String prefix, boolean spoiler, boolean links) { StringBuilder result = new StringBuilder(); Map permissionInfo = new LinkedHashMap<>(); Method method = getMethod(); - for (Annotation permAnnotation : method.getDeclaredAnnotations()) { - Key permKey = Key.of(boolean.class, permAnnotation); - Parser parser = permisser.get(permKey); - if (parser != null) { - List permValues = new ArrayList<>(); - - for (Method permMeth : permAnnotation.annotationType().getDeclaredMethods()) { - Object def = permMeth.getDefaultValue(); - Object current = null; - try { - current = permMeth.invoke(permAnnotation); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } - if (!Objects.equals(def, current)) { - permValues.add(permMeth.getName() + ": " + StringMan.getString(current)); + if (permisser != null) { + for (Annotation permAnnotation : method.getDeclaredAnnotations()) { + Key permKey = Key.of(boolean.class, permAnnotation); + Parser parser = permisser.get(permKey); + if (parser != null) { + List permValues = new ArrayList<>(); + + for (Method permMeth : permAnnotation.annotationType().getDeclaredMethods()) { + Object def = permMeth.getDefaultValue(); + Object current = null; + try { + current = permMeth.invoke(permAnnotation); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + if (!Objects.equals(def, current)) { + permValues.add(permMeth.getName() + ": " + StringMan.getString(current)); + } } - } - String title = permAnnotation.annotationType().getSimpleName() + "(" + String.join(", ", permValues) + ")"; - String body = parser.getDescription(); - permissionInfo.put(title, body); + String title = permAnnotation.annotationType().getSimpleName() + "(" + String.join(", ", permValues) + ")"; + String body = parser.getDescription(); + permissionInfo.put(title, body); + } + } + if (permissionInfo.isEmpty()) { + result.append("`This command is public`\n\n"); } - } - - if (permissionInfo.isEmpty()) { - result.append("`This command is public`\n\n"); } if (simpleDesc().isEmpty()) { @@ -518,8 +519,12 @@ else if (defDesc != null && !defDesc.isEmpty() && !Objects.equals(desc, defDesc) String keyName = key.toSimpleString(); if (spoiler) keyName = StringEscapeUtils.escapeHtml4(keyName.replace("[", "\\[").replace("]", "\\]")); - String typeLink = MarkupUtil.markdownUrl(keyName, typeUrlBase + "#" + MarkupUtil.pathName(key.toSimpleString())); - result.append("`" + argFormat + "`").append(" - ").append(typeLink); + if (links) { + String typeLink = MarkupUtil.markdownUrl(keyName, typeUrlBase + "#" + MarkupUtil.pathName(key.toSimpleString())); + result.append("`" + argFormat + "`").append(" - ").append(typeLink); + } else { + result.append("`" + argFormat + "`").append(" - ").append(keyName); + } result.append("\n\n"); diff --git a/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/CommandManager2.java b/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/CommandManager2.java index bce87f89..c89ef80e 100644 --- a/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/CommandManager2.java +++ b/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/CommandManager2.java @@ -529,7 +529,7 @@ private void handleCall(IMessageIO io, Supplier call) { CommandCallable command = e.getCommand(); String title = "Error Running: /" + command.getFullPath(" "); if (command instanceof ICommand icmd) { - body.append(icmd.toBasicMarkdown(store, permisser, "/", false)); + body.append(icmd.toBasicMarkdown(store, permisser, "/", false, true)); } else { String help = command.help(store); String desc = command.desc(store); diff --git a/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/commands/HelpCommands.java b/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/commands/HelpCommands.java index bd1db3a7..7e8c44e4 100644 --- a/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/commands/HelpCommands.java +++ b/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/commands/HelpCommands.java @@ -72,7 +72,7 @@ public void moderation_check(@Me IMessageIO io, String input) throws IOException @Command public String command(@Me IMessageIO io, ValueStore store, PermissionHandler permisser, ICommand command) { - String body = command.toBasicMarkdown(store, permisser, "/", false); + String body = command.toBasicMarkdown(store, permisser, "/", false, true); // todo spoilers String title = "/" + command.getFullPath(); if (body.length() > 4096) { diff --git a/src/main/java/link/locutus/discord/gpt/imps/ProcessSummarizer.java b/src/main/java/link/locutus/discord/gpt/imps/ProcessSummarizer.java index 1261fbab..9519b6e9 100644 --- a/src/main/java/link/locutus/discord/gpt/imps/ProcessSummarizer.java +++ b/src/main/java/link/locutus/discord/gpt/imps/ProcessSummarizer.java @@ -67,6 +67,7 @@ public String summarizeChunk(String chunk) { String result = StringMan.join(lines, "\n"); if (result.contains("result:")) { result = result.substring(result.indexOf("result:") + 7); + result = result.replace("\\n", "\n"); return result; } else { System.err.println(result); diff --git a/src/main/java/link/locutus/discord/gpt/imps/ProcessText2Text.java b/src/main/java/link/locutus/discord/gpt/imps/ProcessText2Text.java index a57c1165..2a70d445 100644 --- a/src/main/java/link/locutus/discord/gpt/imps/ProcessText2Text.java +++ b/src/main/java/link/locutus/discord/gpt/imps/ProcessText2Text.java @@ -46,6 +46,7 @@ public String generate(String text) { String result = StringMan.join(lines, "\n"); if (result.contains("result:")) { result = result.substring(result.indexOf("result:") + 7); + result = result.replace("\\n", "\n"); return result; } else { System.err.println(result); diff --git a/src/main/java/link/locutus/discord/gpt/pwembed/CommandEmbedding.java b/src/main/java/link/locutus/discord/gpt/pwembed/CommandEmbedding.java index a4b2f1b6..2750232f 100644 --- a/src/main/java/link/locutus/discord/gpt/pwembed/CommandEmbedding.java +++ b/src/main/java/link/locutus/discord/gpt/pwembed/CommandEmbedding.java @@ -1,5 +1,6 @@ package link.locutus.discord.gpt.pwembed; +import link.locutus.discord.Locutus; import link.locutus.discord.commands.manager.v2.binding.ValueStore; import link.locutus.discord.commands.manager.v2.command.ParametricCallable; import link.locutus.discord.commands.manager.v2.impl.pw.CommandManager2; @@ -18,13 +19,18 @@ public String apply(String query, GptHandler handler) { } @Override - public String getContent() { + public String getSummary() { return getObj().simpleDesc(); } + @Override + public String getFull() { + ValueStore store = Locutus.imp().getCommandManager().getV2().getStore(); + return getObj().toBasicMarkdown(store, null, "/", false, false);//simpleDesc(); + } + @Override public boolean hasPermission(ValueStore store, CommandManager2 manager) { -// return getObj().hasPermission(store, manager.getPermisser()); - return true; + return getObj().hasPermission(store, manager.getPermisser()); } } diff --git a/src/main/java/link/locutus/discord/gpt/pwembed/NationMetricEmbedding.java b/src/main/java/link/locutus/discord/gpt/pwembed/NationMetricEmbedding.java index 0b2c90b1..fc9bedfb 100644 --- a/src/main/java/link/locutus/discord/gpt/pwembed/NationMetricEmbedding.java +++ b/src/main/java/link/locutus/discord/gpt/pwembed/NationMetricEmbedding.java @@ -18,7 +18,7 @@ public String apply(String query, GptHandler handler) { } @Override - public String getContent() { + public String getSummary() { return StringMan.classNameToSimple(getObj().getType().toString()) + " " + getObj().getDesc().replaceAll("\n", " "); } diff --git a/src/main/java/link/locutus/discord/gpt/pwembed/PWEmbedding.java b/src/main/java/link/locutus/discord/gpt/pwembed/PWEmbedding.java index 4ec08875..f5d43b7c 100644 --- a/src/main/java/link/locutus/discord/gpt/pwembed/PWEmbedding.java +++ b/src/main/java/link/locutus/discord/gpt/pwembed/PWEmbedding.java @@ -32,7 +32,11 @@ public T getObj() { public abstract String apply(String query, GptHandler handler); - public abstract String getContent(); + public abstract String getSummary(); + + public String getFull() { + return getSummary(); + } public boolean shouldSaveConent() { return shouldSave; diff --git a/src/main/java/link/locutus/discord/gpt/pwembed/PWGPTHandler.java b/src/main/java/link/locutus/discord/gpt/pwembed/PWGPTHandler.java index 1bed73a6..ed6e25bb 100644 --- a/src/main/java/link/locutus/discord/gpt/pwembed/PWGPTHandler.java +++ b/src/main/java/link/locutus/discord/gpt/pwembed/PWGPTHandler.java @@ -63,22 +63,23 @@ public String generateSolution(ValueStore store, GuildDB db, User user, String u // get prompt String prompt = """ - You are `Locutus` a discord bot assistant of a player who is the leader of a nation in the game Politics And War. - Use the information below and your own knowledge to respond. + You are `Locutus` a discord bot for providing documentation to a player who is the leader of a nation in the game Politics And War. + Locutus wiki: + Use the information to reply with comprehensive documentation including appropriate syntax. Player conversation: ``` {user_input} ``` - Top results from searching the game database: + Top results from searching the game database (might not be relevant): {search_results}"""; // 2000 int promptLength = prompt.replace("{user_input}", "").replace("{search_results}", "").length(); int userInputLength = userInput.length(); - int max = 2000; + int max = 2000 - 31; int remaining = max - promptLength - userInputLength; if (store == null) { @@ -90,11 +91,12 @@ public String generateSolution(ValueStore store, GuildDB db, User user, String u // get the closest results List embeddings = new ArrayList<>(); - HashSet allowedTypes = new HashSet<>(Arrays.asList(EmbeddingType.values())); + Set allowedTypes = new HashSet<>(Arrays.asList(EmbeddingType.values())); List> closest = this.getClosest(store, userInput, 50, allowedTypes); for (Map.Entry entry : closest) { PWEmbedding embedding = entry.getKey(); - String text = embedding.getType() + "." + embedding.getId() + "=" + embedding.getContent(); + String text = embedding.getType() + "." + embedding.getId() + "=" + embedding.getFull(); + text = text.replace("\n\n", "\n"); if (text.length() + 1 > remaining) continue; embeddings.add(text); remaining -= text.length() + 1; @@ -102,11 +104,15 @@ public String generateSolution(ValueStore store, GuildDB db, User user, String u String formatted = prompt.replace("{user_input}", userInput).replace("{search_results}", String.join("\n", embeddings)); - System.out.println("Prompt\n```\n" + formatted + "\n```"); + System.out.println("Prompt\n\n" + formatted + "\n"); - String result = this.handler.getText2text().generate(formatted); - - return result; + try { + String result = this.handler.getText2text().generate(formatted); + return result; + } catch (Throwable e) { + e.printStackTrace(); + return "Error (see console)"; + } } private void registerCommandEmbeddings() { @@ -141,7 +147,7 @@ private void registerEmbedding(PWEmbedding embedding, boolean forceDownload, Str return; } } - String full = prefix + embedding.getType().name() + " " + embedding.getId() + ": " + embedding.getContent(); + String full = prefix + embedding.getType().name() + " " + embedding.getId() + ": " + embedding.getSummary(); handler.getEmbedding(embedding.getType().ordinal(), embedding.getId(), full, embedding.shouldSaveConent()); } diff --git a/src/main/java/link/locutus/discord/gpt/pwembed/SettingEmbedding.java b/src/main/java/link/locutus/discord/gpt/pwembed/SettingEmbedding.java index a9517ec5..73c09548 100644 --- a/src/main/java/link/locutus/discord/gpt/pwembed/SettingEmbedding.java +++ b/src/main/java/link/locutus/discord/gpt/pwembed/SettingEmbedding.java @@ -21,7 +21,7 @@ public String apply(String query, GptHandler handler) { } @Override - public String getContent() { + public String getSummary() { return getObj().help(); } diff --git a/src/main/java/link/locutus/discord/util/DocPrinter2.java b/src/main/java/link/locutus/discord/util/DocPrinter2.java index 7e2993ff..4ffed1d2 100644 --- a/src/main/java/link/locutus/discord/util/DocPrinter2.java +++ b/src/main/java/link/locutus/discord/util/DocPrinter2.java @@ -136,7 +136,7 @@ public static String printCommands(CommandGroup group, ValueStore store, Permiss for (ParametricCallable command : commands) { result.append("## ").append(prefix).append(command.getFullPath()).append("\n"); - result.append(command.toBasicMarkdown(store, permisser, prefix, true)); + result.append(command.toBasicMarkdown(store, permisser, prefix, true, true)); result.append("\n---\n\n"); }