Skip to content

Commit

Permalink
Changed Arguments to Parameters
Browse files Browse the repository at this point in the history
Changed Api Tool
Changed How Parameters are flagged

New System Prompt for tool calling
  • Loading branch information
ZeyoYT committed May 29, 2024
1 parent 5e66cd1 commit 06ca3e2
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 145 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ dependencies {
implementation group: 'io.github.freya022', name: 'JDA', version: '5905e6316b'
implementation group: 'io.github.cdimascio', name: 'java-dotenv', version: '5.2.2'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.5.6'
implementation group: 'dev.langchain4j', name: 'langchain4j-ollama', version: '0.30.0'
implementation group: 'dev.langchain4j', name: 'langchain4j-ollama', version: '0.31.0'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '5.0.0-alpha.14'
implementation group: 'joda-time', name: 'joda-time', version: '2.12.7'
implementation group: 'org.jsoup', name: 'jsoup', version: '1.17.2'

implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0'

implementation 'dev.langchain4j:langchain4j-easy-rag:0.30.0'
implementation 'dev.langchain4j:langchain4j-easy-rag:0.31.0'

}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.ailama.config.Config;
import me.ailama.handler.commandhandler.CommandRegister;
import me.ailama.main.AiLama;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/ailama/commands/AiCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void handleCommand(SlashCommandInteractionEvent event) {
}
else
{
response = OllamaManager.getInstance().executeTool(tooled.name, tooled.arguments.values().toArray()).toString();
response = OllamaManager.getInstance().executeTool(tooled.name, tooled.parameters.values().toArray()).toString();
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/me/ailama/handler/JsonBuilder/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ public JsonArray objects(JsonObject... jsonObjects) {
return this;
}

public JsonArray objects(List<JsonObject> jsonObjects) {
for(JsonObject jsonObject : jsonObjects) {
jsonBuilder.append(jsonObject.build()).append(",");
public JsonArray objects(List<JsonObject> jsonObjects) {
for(JsonObject jsonObject : jsonObjects) {
jsonBuilder.append(jsonObject.build()).append(",");
}
return this;
}

public JsonArray addString(String value) {
jsonBuilder.append("\"").append(value).append("\",");
return this;
}
return this;
}

public String build() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package me.ailama.handler.annotations;

public @interface Args {
public @interface Parameter {
String name();
String Type();
String description() default "";

boolean noNull() default false;
boolean required() default true;
}
2 changes: 1 addition & 1 deletion src/main/java/me/ailama/handler/annotations/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
String name();
String description();

Args[] arguments() default {};
Parameter[] parameters() default {};

boolean rawResponse() default false;

Expand Down
212 changes: 141 additions & 71 deletions src/main/java/me/ailama/handler/commandhandler/OllamaManager.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/java/me/ailama/handler/models/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Tool {
public boolean rawResponse;
public String name;

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

public String[] response;
}
35 changes: 3 additions & 32 deletions src/main/java/me/ailama/main/AiLama.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package me.ailama.main;

import me.ailama.handler.annotations.Tool;
import me.ailama.handler.annotations.Args;
import net.dv8tion.jda.api.utils.data.DataObject;
import okhttp3.*;
import me.ailama.handler.annotations.Parameter;
import org.joda.time.DateTime;

import java.time.LocalDateTime;
Expand All @@ -20,8 +18,8 @@ public AiLama() {

}

@Tool(name = "fixUrl", description = "Fix a URL if it doesn't start with 'http://' or 'https://'", arguments = {
@Args(name = "url", Type = "string")
@Tool(name = "fixUrl", description = "Fix a URL if it doesn't start with 'http://' or 'https://'", parameters = {
@Parameter(name = "url", Type = "string")
})
public String fixUrl(String url) {
if (!url.startsWith("http://") && !url.startsWith("https://")) {
Expand All @@ -30,33 +28,6 @@ public String fixUrl(String url) {
return url;
}

public String getRates(String currency,String currency2) {
final OkHttpClient okHttpClient = new OkHttpClient();
String rate = "";

final Request request = new Request.Builder().url("https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/" + currency + ".json").build();

try {
final Response response = okHttpClient.newCall(request).execute();
final ResponseBody responseBody = response.body();

if (responseBody != null) {

final DataObject dataObject = DataObject.fromJson(responseBody.string());
rate = dataObject.getObject(currency).getString(currency2);

responseBody.close();
}

response.close();
return rate;
}
catch (Exception e) {
Main.LOGGER.error("Error while getting currency rate: " + e.getMessage());
return "0";
}
}

public List<String> getParts(final String string, final int partitionSize) {
final List<String> parts = new ArrayList<>();
for (int len = string.length(), i = 0; i < len; i += partitionSize) {
Expand Down
50 changes: 37 additions & 13 deletions src/main/java/me/ailama/tools/ApiTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,60 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import me.ailama.handler.annotations.Args;
import me.ailama.handler.annotations.Parameter;
import me.ailama.handler.annotations.Tool;
import me.ailama.main.AiLama;
import me.ailama.main.Main;
import net.dv8tion.jda.api.utils.data.DataObject;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;

public class ApiTools {
@Tool(name = "currencyRate", description = "Converts a currency rate to another", arguments = {
@Args(name = "amount", Type = "double", description = "Amount to convert", noNull = true),
@Args(name = "currency1", Type = "string", description = "Currency to convert from, Like INR"),
@Args(name = "currency2", Type = "string", description = "Currency to convert to, Like USD")
@Tool(name = "currencyRate", description = "Converts a currency rate to another", parameters = {
@Parameter(name = "amount", Type = "double", description = "Amount to convert"),
@Parameter(name = "currency1", Type = "string", description = "Currency to convert from, Like INR"),
@Parameter(name = "currency2", Type = "string", description = "Currency to convert to, Like USD")
})
public String currencyRate(Double amount, String currency1, String currency2) {
final double finalRate = Double.parseDouble(AiLama.getInstance().getRates(currency1.toLowerCase(),currency2.toLowerCase()));
final double finalRate = Double.parseDouble(getRates(currency1.toLowerCase(),currency2.toLowerCase()));
final double conv = finalRate * amount;
return String.format("%.4f", conv);
}

@Tool(name = "getApi", description = "used for calling api", arguments = {
@Args(name = "api_url", Type = "string", description = "full api url, like https://api.example.com"),
@Args(name = "headers", Type = "json_object", description = "headers to pass for the api call"),
@Args(name = "call_type", Type = "STRING{GET,POST}", description = "define the api call type"),
@Args(name = "body", Type = "string", description = "body to pass with the api call"),
public String getRates(String currency,String currency2) {
final OkHttpClient okHttpClient = new OkHttpClient();
String rate = "";

final Request request = new Request.Builder().url("https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/" + currency + ".json").build();

try {
final Response response = okHttpClient.newCall(request).execute();
final ResponseBody responseBody = response.body();

final DataObject dataObject = DataObject.fromJson(responseBody.string());
rate = dataObject.getObject(currency).getString(currency2);

responseBody.close();

response.close();
return rate;
}
catch (Exception e) {
Main.LOGGER.error("Error while getting currency rate: " + e.getMessage());
return "0";
}
}

@Tool(name = "callApi", description = "used for calling restful api", parameters = {
@Parameter(name = "api_url", Type = "string", description = "full api url, like https://api.example.com"),
@Parameter(name = "headers", Type = "json_object", description = "headers to pass for the api call"),
@Parameter(name = "call_type", Type = "STRING{GET,POST}", description = "define the api call type"),
@Parameter(name = "body", Type = "string", description = "body to pass with the api call"),
}, rawResponse = true)
public String callGetApi(String rawResponse) {

DataObject dataObject = DataObject.fromJson(rawResponse).getObject("arguments");
DataObject dataObject = DataObject.fromJson(rawResponse).getObject("parameters");
String url = dataObject.getString("api_url");
String callType = dataObject.getString("call_type");
String body = dataObject.getString("body", null);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/me/ailama/tools/MathTools.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package me.ailama.tools;

import me.ailama.handler.annotations.Args;
import me.ailama.handler.annotations.Parameter;
import me.ailama.handler.annotations.Tool;

public class MathTools {

@Tool(name = "basicMathOperations", description = "only does Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%), Power (^)", arguments = {
@Args(name = "a", Type = "number", noNull = true),
@Args(name = "b", Type = "number", noNull = true),
@Args(name = "operation_symbol", Type = "string", noNull = true)
@Tool(name = "basicMathOperations", description = "only does Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%), Power (^)", parameters = {
@Parameter(name = "a", Type = "number"),
@Parameter(name = "b", Type = "number"),
@Parameter(name = "operation_symbol", Type = "string")
})
public String basicMathOperations(Number a, Number b, String operation) {
return switch (operation) {
Expand Down Expand Up @@ -46,9 +46,9 @@ public String power(Number a, Number b) {
return String.valueOf(Math.pow(a.doubleValue(), b.doubleValue()));
}

@Tool(name = "squareOrCube", description = "Square or cube of a number, symbols are square_root and cube_root", arguments = {
@Args(name = "a", Type = "number"),
@Args(name = "operation_symbol", Type = "string")
@Tool(name = "squareOrCube", description = "Square or cube of a number, symbols are square_root and cube_root", parameters = {
@Parameter(name = "a", Type = "number"),
@Parameter(name = "operation_symbol", Type = "string")
})
public String squareOrCube(Number a, String operation) {
return switch (operation) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/me/ailama/tools/TimeTools.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package me.ailama.tools;

import me.ailama.handler.annotations.Args;
import me.ailama.handler.annotations.Parameter;
import me.ailama.handler.annotations.Tool;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class TimeTools {

@Tool(name = "formatTime", description = "Format time from milliseconds to a readable format like formatTime(N1)", arguments = {
@Args(name = "timeInMillis", Type = "number")
@Tool(name = "formatTime", description = "Format time from milliseconds to a readable format like formatTime(N1)", parameters = {
@Parameter(name = "timeInMillis", Type = "number")
})
public String formatTime(final long timeInMillis) {

Expand All @@ -23,9 +23,9 @@ public String formatTime(final long timeInMillis) {
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}

@Tool(name = "time", description = "Get the current time", arguments = {
@Args(name = "is24Hour", Type = "boolean", description = "true for 24-hour format, false for 12-hour format"),
@Args(name = "timeZone", Type = "string", description = "Timezone in which you want to get the time like 'Asia/Kolkata'")
@Tool(name = "time", description = "Get the current time", parameters = {
@Parameter(name = "is24Hour", Type = "boolean", description = "true for 24-hour format, false for 12-hour format"),
@Parameter(name = "timeZone", Type = "string", description = "Timezone in which you want to get the time like 'Asia/Kolkata'")
})
public String time(boolean is24Hour, String timeZone) {
DateTime dateTime = new DateTime();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/ailama/tools/UtilityTools.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package me.ailama.tools;

import me.ailama.handler.annotations.Args;
import me.ailama.handler.annotations.Parameter;
import me.ailama.handler.annotations.Tool;
import me.ailama.handler.commandhandler.OllamaManager;

import java.net.URI;

public class UtilityTools {
@Tool(name = "isValidURL", description = "Check if a URL is valid like isValidURL(url)", arguments = {
@Args(name = "url", Type = "string")
@Tool(name = "isValidURL", description = "Check if a URL is valid like isValidURL(url)", parameters = {
@Parameter(name = "url", Type = "string")
})
public boolean isValidURL(String url) {
try {
Expand Down

0 comments on commit 06ca3e2

Please sign in to comment.