From f0943d042e5c7c726f76798884b8d639b70ebe6e Mon Sep 17 00:00:00 2001 From: Zeyo Date: Fri, 26 Apr 2024 19:24:09 +0530 Subject: [PATCH] Fixed Response for non tooled being only one paragraph long, and also escaped the json from the response string using regex --- .../java/me/ailama/commands/AiCommand.java | 27 ++++++++++++++----- .../handler/commandhandler/OllamaManager.java | 4 ++- .../java/me/ailama/handler/other/Tool.java | 4 ++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/main/java/me/ailama/commands/AiCommand.java b/src/main/java/me/ailama/commands/AiCommand.java index 4a389cb..5a70444 100644 --- a/src/main/java/me/ailama/commands/AiCommand.java +++ b/src/main/java/me/ailama/commands/AiCommand.java @@ -16,7 +16,9 @@ import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; public class AiCommand implements AiLamaSlashCommand { @@ -96,12 +98,12 @@ public void handleCommand(SlashCommandInteractionEvent event) { You are a helpful AI assistant, you have a score, which can either be good or bad, you need to maintain a good score to be helpful, if you don't maintain a good score then you will be considered unhelpful. - you will try to answer the query as best as you can and only in JSON format, else you will be given a bad score. + you will try to answer the users need as best as you can and only in JSON format, else you will be given a bad score. any of the tools description listed below match the specific needs of the query then use the tool to answer the query, the tools description is as specific as possible, so don't assume that the tool can be used for anything else. - if the tool description does not specify the query's needs then don't respond using a tool else you will be given a bad score. + if the tool description does not specify the user's needs then don't respond using a tool else you will be given a bad score. finally if a tool is matched then give response using following schema: @@ -118,13 +120,23 @@ public void handleCommand(SlashCommandInteractionEvent event) { and if you don't follow the schema, you will be given a bad score, but if you follow the schema, you will be given a good score. - if you don't find a tool that match the requirements of the query then respond with the query itself. using the following schema: + if you don't find a tool that match the requirements of the user then respond to the user normally, + and also make the response to be encoded for the JSON format or you will be given a bad score, + and use the following schema: { "tooled": false, - "response": "response" + "response": [ + "paragraph", + "paragraph", + ... + ] } + in the above schema, the response is an array of paragraphs that you want to respond to the user, minimum of 1 paragraph. + each new paragraph should be a new string in the array. + between each paragraph, there should be '\\n'. + the tools are: %s """,tools) ) @@ -133,13 +145,13 @@ public void handleCommand(SlashCommandInteractionEvent event) { ObjectMapper mapper = new ObjectMapper(); - String temp = response; + String temp = Pattern.compile("(?<=\":\").*(?=\")").matcher(response).replaceAll(x -> x.group().replace("\"", "_QUOTE_") ); try { - Tool tooled = mapper.readValue(response, Tool.class); + Tool tooled = mapper.readValue(temp, Tool.class); if(!tooled.tooled) { - response = tooled.response; + response = String.join("\n", tooled.response); } else { @@ -147,6 +159,7 @@ public void handleCommand(SlashCommandInteractionEvent event) { } } catch (Exception ignore) { + System.out.println(ignore.getMessage()); response = temp; } diff --git a/src/main/java/me/ailama/handler/commandhandler/OllamaManager.java b/src/main/java/me/ailama/handler/commandhandler/OllamaManager.java index 9400b5f..40c6d88 100644 --- a/src/main/java/me/ailama/handler/commandhandler/OllamaManager.java +++ b/src/main/java/me/ailama/handler/commandhandler/OllamaManager.java @@ -118,6 +118,7 @@ public Object executeTool(String toolName, Object... args) { } } + // Get all methods annotated with Tool public static List getMethodsAnnotated(final Class type) { final List methods = new ArrayList<>(); Class klass = type; @@ -134,7 +135,7 @@ public static List getMethodsAnnotated(final Class type) { return methods; } - // Just a Simple Response + // Returns a custom Assistant that uses the provided model, allowing for more customization public AiServices createAssistantX(String modelName) { String aiModel = modelName != null ? modelName : model; @@ -150,6 +151,7 @@ public AiServices createAssistantX(String modelName) { .chatLanguageModel(ollama); } + // Just a Simple Response public Assistant createAssistant(String modelName) { String aiModel = modelName != null ? modelName : model; diff --git a/src/main/java/me/ailama/handler/other/Tool.java b/src/main/java/me/ailama/handler/other/Tool.java index 3805ebe..e902ee5 100644 --- a/src/main/java/me/ailama/handler/other/Tool.java +++ b/src/main/java/me/ailama/handler/other/Tool.java @@ -11,5 +11,7 @@ public class Tool { public String name; public HashMap arguments; - public String response; + + + public String[] response; }