Skip to content

Commit

Permalink
update response
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Jul 13, 2023
1 parent 2ec647f commit 9208119
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ default Map<String, ParameterData> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> permissionInfo = new LinkedHashMap<>();

Method method = getMethod();
for (Annotation permAnnotation : method.getDeclaredAnnotations()) {
Key<Object> permKey = Key.of(boolean.class, permAnnotation);
Parser parser = permisser.get(permKey);
if (parser != null) {
List<String> 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<Object> permKey = Key.of(boolean.class, permAnnotation);
Parser parser = permisser.get(permKey);
if (parser != null) {
List<String> 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()) {
Expand Down Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ private void handleCall(IMessageIO io, Supplier<Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Object> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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", " ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/link/locutus/discord/gpt/pwembed/PWGPTHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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: <https://github.com/xdnw/locutus/wiki/Commands>
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) {
Expand All @@ -90,23 +91,28 @@ public String generateSolution(ValueStore store, GuildDB db, User user, String u

// get the closest results
List<String> embeddings = new ArrayList<>();
HashSet<EmbeddingType> allowedTypes = new HashSet<>(Arrays.asList(EmbeddingType.values()));
Set<EmbeddingType> allowedTypes = new HashSet<>(Arrays.asList(EmbeddingType.values()));
List<Map.Entry<PWEmbedding, Double>> closest = this.getClosest(store, userInput, 50, allowedTypes);
for (Map.Entry<PWEmbedding, Double> 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;
}

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() {
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public String apply(String query, GptHandler handler) {
}

@Override
public String getContent() {
public String getSummary() {
return getObj().help();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/link/locutus/discord/util/DocPrinter2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down

0 comments on commit 9208119

Please sign in to comment.