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:
@@ -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();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user