Switching out toString returing a string JSON to toJSON that returns a JSONObject instead... and toString is not ment for this purpose

This commit is contained in:
2026-07-19 23:05:48 +02:00
parent 2defa32cb8
commit 4cd4367a19
13 changed files with 35 additions and 48 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ plugins {
id 'java-library'
}
version = '1.8.13'
version = '1.9.0'
dependencies {
implementation project(":Plugin-API")
@@ -5,7 +5,6 @@ import me.neurodock.ollama.*;
import me.neurodock.ollama.exceptions.OllamaToolErrorException;
import me.neurodock.plugin.Data;
import me.neurodock.plugin.LoadedPlugin;
import me.neurodock.plugin.Plugin;
import me.neurodock.plugin.loader.Loader;
import me.neurodock.plugin.exceptions.PluginLoadingException;
import org.intellij.lang.annotations.MagicConstant;
@@ -16,10 +15,6 @@ import org.json.JSONObject;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
@@ -32,7 +27,6 @@ import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import static me.neurodock.ollama.OllamaFunctionArgument.deconstructOllamaFunctionArguments;
import static me.neurodock.plugin.Plugin.UNKNOWN_PLUGIN;
import static me.neurodock.plugin.tool.Tool.RPCP_SOURCE;
/**
@@ -328,7 +322,7 @@ public class Core {
private JSONArray buildMessagesArray() {
JSONArray messages = new JSONArray();
for (OllamaMessage message : ollamaObject.getMessages()) {
messages.put(new JSONObject(message.toString()));
messages.put(message.toJSON());
}
return messages;
}
@@ -434,7 +428,7 @@ public class Core {
JSONArray messages = new JSONArray();
for(OllamaMessage message : ollamaObject.getMessages()) {
messages.put(new JSONObject(message.toString()));
messages.put(message.toJSON());
}
messagesWriter.write(messages.toString());
@@ -614,7 +608,7 @@ public class Core {
connection.setDoOutput(true);
connection.setConnectTimeout(80 * 1000);
String ollamaObjectString = ollamaObject.toString();
String ollamaObjectString = ollamaObject.toJSON().toString();
ollamaObjectString = ollamaObjectString.replace("\n", "\\n");
@@ -28,8 +28,7 @@ public abstract class OllamaFunctionTool implements OllamaTool {
return source;
}
@Override
public String toString() {
public JSONObject toJSON() {
JSONObject ret = new JSONObject();
ret.put("type", "function");
@@ -39,11 +38,11 @@ public abstract class OllamaFunctionTool implements OllamaTool {
function.put("description", description());
}
function.put("parameters", (parameters() == null?
new JSONObject() : new JSONObject(parameters().toString())));
new JSONObject() : parameters().toJSON()));
ret.put("function", function);
return ret.toString();
return ret;
}
/**
@@ -39,11 +39,10 @@ public class OllamaMessage {
return content;
}
@Override
public String toString() {
public JSONObject toJSON() {
JSONObject json = new JSONObject();
json.put("role", role.getRole());
json.put("content", content.replace("\n", "\\n"));
return json.toString();
return json;
}
}
@@ -25,13 +25,13 @@ public class OllamaMessageToolCall extends OllamaMessage{
}
@Override
public String toString() {
public JSONObject toJSON() {
JSONObject json = new JSONObject();
json.put("role", role);
json.put("content", content);
json.put("tool_calls", tool_calls);
return json.toString();
return json;
}
}
@@ -311,21 +311,21 @@ public class OllamaObject {
}
}
@Override
public String toString() {
public JSONObject toJSON()
{
JSONObject json = new JSONObject();
JSONArray tools = new JSONArray();
for (Pair<OllamaTool, String> tool : this.tools) {
if(tool.getKey().getClass().isInterface()) continue;
JSONObject obj = new JSONObject(tool.getKey().toString());
JSONObject obj = tool.getKey().toJSON();
//obj.put("name", obj.getString("name") + tool.getValue()); // Injects the source of the tool into the name
tools.put(obj);
}
JSONArray messages = new JSONArray();
for (OllamaMessage message : this.messages) {
messages.put(new JSONObject(message.toString()));
messages.put(message.toJSON());
}
json.put("model", model);
@@ -335,7 +335,7 @@ public class OllamaObject {
json.put("options", options);
json.put("stream", stream);
json.put("keep_alive", keep_alive);
return json.toString();
return json;
}
/**
@@ -4,7 +4,6 @@ import me.neurodock.plugin.tool.ToolParameters;
import org.json.JSONObject;
import java.util.*;
import java.util.stream.Stream;
/**
* Represents the parameters of a tool.
@@ -22,15 +21,14 @@ public class OllamaPerameter {
this.required = required;
};
@Override
public String toString() {
public JSONObject toJSON() {
JSONObject json = new JSONObject();
json.put("type", "object");
json.put("properties", properties);
json.put("required", required);
return json.toString();
return json;
}
/**
@@ -195,7 +193,7 @@ public class OllamaPerameter {
public OllamaPerameter build() {
JSONObject properties = new JSONObject();
for(String name : propertyMap.keySet()) {
properties.put(name, new JSONObject(propertyMap.get(name).toString()));
properties.put(name, propertyMap.get(name).toJSON());
}
return new OllamaPerameter(properties, required.toArray(new String[0]));
}
@@ -223,14 +221,13 @@ public class OllamaPerameter {
this.description = description;
}
@Override
public String toString() {
public JSONObject toJSON() {
JSONObject json = new JSONObject();
json.put("type", type);
json.put("description", description);
return json.toString();
return json;
}
}
@@ -1,7 +1,10 @@
package me.neurodock.ollama;
import org.json.JSONObject;
/**
* Represents a tool.
*/
public interface OllamaTool {
public JSONObject toJSON();
}
@@ -79,10 +79,7 @@ public class Loader {
}
@Override
public String toString() {
// Replaces the toString which is used throughout the Core and OllamaObject to get the JSON representation of a Function Tool
// Ideally the toString method SHULD NOT be used like this, while yes a JSON representation is better than a Java Hash representation
// the toString should not be utilized or overwritten in this manner
public JSONObject toJSON() {
return tool.getToolJSON();
}
});
@@ -5,7 +5,6 @@ import me.neurodock.core.Pair;
import me.neurodock.core.PrintMessageHandler;
import me.neurodock.core.files.FileHandlerLocation;
import me.neurodock.core.memory.CoreMemory;
import me.neurodock.genius.GeniusTools;
import me.neurodock.ollama.*;
import me.neurodock.ollama.utils.SystemMessage;
import org.json.JSONObject;
@@ -153,8 +152,8 @@ public class Display {
writeLog("Tools installed in this instance acording to the coire OllamaObject");
for(Pair<OllamaTool, String> funtion : core.getOllamaObject().getTools()) {
System.out.println("> Function: " + funtion.getKey().toString());
writeLog("Function: " + funtion.getKey().toString());
System.out.println("> Function: " + funtion.getKey().toJSON());
writeLog("Function: " + funtion.getKey().toJSON());
}
break;
case "working":
+2 -2
View File
@@ -15,8 +15,8 @@ public class LyricsFetch {
@Test
public void testFetchLyrics() throws Exception {
Document doc = Jsoup.connect("https://genius.com/Neuro-sama-life-lyrics")
.userAgent("Mozilla/5.0")
Document doc = Jsoup.connect("https://genius.com/Ellie-minibot-same-moon-lyrics")
.userAgent("Mozilla/6.0")
.get();
Elements containers = doc.select("div[data-lyrics-container=true]");
@@ -30,7 +30,7 @@ public abstract class Tool {
* The core uses this method when generating the list of available functions to the LLM
* @return a string formated JSON containing the representation of the tool definitions
*/
public final String getToolJSON() {
public final JSONObject getToolJSON() {
JSONObject ret = new JSONObject();
ret.put("type", "function");
@@ -39,11 +39,11 @@ public abstract class Tool {
if(description() != null) {
function.put("description", description());
}
function.put("parameters", (parameters() == null? new JSONObject() : new JSONObject(parameters().getJSON())));
function.put("parameters", (parameters() == null? new JSONObject() : parameters().getJSON()));
ret.put("function", function);
return ret.toString();
return ret;
}
/**
@@ -149,7 +149,7 @@ public class ToolParameters {
public ToolParameters build() {
JSONObject properties = new JSONObject();
for(String name : propertyMap.keySet()) {
properties.put(name, new JSONObject(propertyMap.get(name).toString()));
properties.put(name, propertyMap.get(name).toJSON());
}
return new ToolParameters(properties, required.toArray(new String[0]));
}
@@ -177,14 +177,13 @@ public class ToolParameters {
this.description = description;
}
@Override
public String toString() {
public JSONObject toJSON() {
JSONObject json = new JSONObject();
json.put("type", type);
json.put("description", description);
return json.toString();
return json;
}
}