Skip to content

Commit

Permalink
Fixed Response for non tooled being only one paragraph long, and also…
Browse files Browse the repository at this point in the history
… escaped the json from the response string using regex
  • Loading branch information
ZeyoYT committed Apr 26, 2024
1 parent 3a4643a commit f0943d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
27 changes: 20 additions & 7 deletions src/main/java/me/ailama/commands/AiCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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:
Expand All @@ -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)
)
Expand All @@ -133,20 +145,21 @@ 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
{
response = OllamaManager.getInstance().executeTool(tooled.name, tooled.arguments.values().toArray()).toString();
}
}
catch (Exception ignore) {
System.out.println(ignore.getMessage());
response = temp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public Object executeTool(String toolName, Object... args) {
}
}

// Get all methods annotated with Tool
public static List<Method> getMethodsAnnotated(final Class<?> type) {
final List<Method> methods = new ArrayList<>();
Class<?> klass = type;
Expand All @@ -134,7 +135,7 @@ public static List<Method> 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<Assistant> createAssistantX(String modelName) {

String aiModel = modelName != null ? modelName : model;
Expand All @@ -150,6 +151,7 @@ public AiServices<Assistant> createAssistantX(String modelName) {
.chatLanguageModel(ollama);
}

// Just a Simple Response
public Assistant createAssistant(String modelName) {

String aiModel = modelName != null ? modelName : model;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/me/ailama/handler/other/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public class Tool {
public String name;

public HashMap<String, Object> arguments;
public String response;


public String[] response;
}

0 comments on commit f0943d0

Please sign in to comment.