refactor: update CoreMemory to v2 and rename OllamaToolResponse
build / build (push) Has been cancelled
build / build (push) Has been cancelled
- Refactored `CoreMemory` to support versioning (v2) and dual-memory types: `MAPPED_MEMORY` (key-value) and `ARRAYED_MEMORY` (sequential). - Added automatic migration logic for v1 memory files to v2. - Introduced new memory tools: `AddArrayMemory`, `GetArrayMemory`, and `GetArrayedMemories`. - Renamed `OllamaToolRespnce` to `OllamaToolResponse` and added `empty()` factory methods for better error handling. - Updated multiple function tools (e.g., `GetMemoryFunction`, `GetMemoriesFunction`, `APITool`, `GetWikiPageText`) to use the new `OllamaToolResponse` and `Optional`-based memory retrieval. - Improved null safety by adding `@NotNull` annotations to tool implementations.
This commit is contained in:
@@ -3,7 +3,6 @@ package me.zacharias.chat.core;
|
||||
import me.zacharias.chat.core.memory.*;
|
||||
import me.zacharias.chat.ollama.*;
|
||||
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
|
||||
import me.zacharias.chat.plugin.Plugin;
|
||||
import me.zacharias.chat.plugin.loader.Loader;
|
||||
import me.zacharias.chat.plugin.exceptions.PluginLoadingException;
|
||||
import org.intellij.lang.annotations.MagicConstant;
|
||||
@@ -472,7 +471,7 @@ public class Core {
|
||||
// TODO: Check so all arguments is the correct type, else error out in a safe mannet.
|
||||
|
||||
try {
|
||||
OllamaToolRespnce function1 = func.function(argumentArrayList.toArray(new OllamaFunctionArgument[0]));
|
||||
OllamaToolResponse function1 = func.function(argumentArrayList.toArray(new OllamaFunctionArgument[0]));
|
||||
ollamaObject.addMessage(function1);
|
||||
printMessageHandler.printMessage(function1);
|
||||
writeLog("Successfully function call " + func.name() + " output: " + function1.getResponse());
|
||||
|
||||
@@ -4,7 +4,7 @@ import me.zacharias.chat.core.files.FileHandler;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import me.zacharias.chat.ollama.OllamaToolResponse;
|
||||
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -31,7 +31,7 @@ public class ReadFileTool extends OllamaFunctionTool {
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
String orgPath = null;
|
||||
for (OllamaFunctionArgument arg : args) {
|
||||
if(arg.argument().equals("file_path")) {
|
||||
@@ -73,6 +73,6 @@ public class ReadFileTool extends OllamaFunctionTool {
|
||||
catch (IOException e) {
|
||||
throw new OllamaToolErrorException(this.name(), "Error reading file: " + fs.path(file));
|
||||
}
|
||||
return new OllamaToolRespnce(this.name(), sb.toString());
|
||||
return new OllamaToolResponse(this.name(), sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package me.zacharias.chat.core.memory;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import me.zacharias.chat.ollama.OllamaToolResponse;
|
||||
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -36,7 +36,7 @@ public class AddMemoryFunction extends OllamaFunctionTool {
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
if (args.length == 0) {
|
||||
throw new OllamaToolErrorException(name(), "Missing memory argument");
|
||||
}
|
||||
@@ -59,6 +59,6 @@ public class AddMemoryFunction extends OllamaFunctionTool {
|
||||
}
|
||||
|
||||
this.memory.addMemory(identity, memory);
|
||||
return new OllamaToolRespnce(name(), "Added "+identity+" to the memory");
|
||||
return new OllamaToolResponse(name(), "Added "+identity+" to the memory");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,29 @@ import org.json.JSONObject;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* CoreMemory is a class that provides a way to store and retrieve strings from a file.<br>
|
||||
* This is meant to be used as a way to store and retrieve strings from a file.
|
||||
*/
|
||||
public class CoreMemory {
|
||||
public static final int VERSION = 2;
|
||||
/**
|
||||
* The singleton instance of CoreMemory.
|
||||
*/
|
||||
private static final CoreMemory instance = new CoreMemory(Core.DATA + "/CoreMemory.json");
|
||||
|
||||
|
||||
/**
|
||||
* Memory type identifier for key-value mapped memory storage.
|
||||
*/
|
||||
public static final String MAPPED_MEMORY = "mapped_memory";
|
||||
|
||||
/**
|
||||
* Memory type identifier for sequential/array-based memory storage.
|
||||
*/
|
||||
public static final String ARRAYED_MEMORY = "arrayed_memory";
|
||||
|
||||
/**
|
||||
* Gets the singleton instance of CoreMemory.
|
||||
* @return The singleton instance of CoreMemory
|
||||
@@ -30,6 +42,9 @@ public class CoreMemory {
|
||||
* @param memoryFile The file to store the memory in
|
||||
*/
|
||||
public CoreMemory(String memoryFile) {
|
||||
|
||||
// Since version 2, version 1(un versiond) memory files are incompadible, and will be refectored
|
||||
|
||||
File f = new File(memoryFile);
|
||||
if (f.exists()) {
|
||||
try {
|
||||
@@ -39,7 +54,18 @@ public class CoreMemory {
|
||||
while ((buffer = br.readLine()) != null) {
|
||||
data.append(buffer);
|
||||
}
|
||||
memory = new JSONObject(data.toString());
|
||||
JSONObject tmpMemory = new JSONObject(data.toString());
|
||||
if(tmpMemory.optInt("VERSION", 0) == 0)
|
||||
{
|
||||
memory = new JSONObject();
|
||||
memory.put("VERSION", VERSION);
|
||||
memory.put(MAPPED_MEMORY, tmpMemory);
|
||||
memory.put(ARRAYED_MEMORY, new JSONArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
memory = tmpMemory;
|
||||
}
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -82,11 +108,11 @@ public class CoreMemory {
|
||||
* @return A list of memory identifies/names
|
||||
*/
|
||||
public String[] getMemoriesIdentity() {
|
||||
return memory.keySet().toArray(new String[0]);
|
||||
return memory.getJSONObject(MAPPED_MEMORY).keySet().toArray(new String[0]);
|
||||
}
|
||||
|
||||
public String getMemory(String name) {
|
||||
return memory.optString(name, null);
|
||||
public Optional<String> getMemory(String name) {
|
||||
return Optional.ofNullable(memory.getJSONObject(MAPPED_MEMORY).optString(name, null));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,7 +121,7 @@ public class CoreMemory {
|
||||
* @param memory The memory
|
||||
*/
|
||||
public void addMemory(String name, String memory) {
|
||||
this.memory.put(name, memory);
|
||||
this.memory.getJSONObject(MAPPED_MEMORY).put(name, memory);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,14 +129,53 @@ public class CoreMemory {
|
||||
* @param name The memory to remove
|
||||
*/
|
||||
public void removeMemory(String name) {
|
||||
this.memory.remove(name);
|
||||
this.memory.getJSONObject(MAPPED_MEMORY).remove(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an array-based memory
|
||||
* @param memory The memory to remember
|
||||
*/
|
||||
public void addMemory(String memory)
|
||||
{
|
||||
this.memory.getJSONArray(ARRAYED_MEMORY).put(memory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an array-based memory
|
||||
* @param memory The memory to remember
|
||||
* @param index The index to put it at
|
||||
* @apiNote Prefer {@link #addMemory(String)} unless there is a reason to use this one.
|
||||
*/
|
||||
public void addMemory(String memory, int index)
|
||||
{
|
||||
this.memory.getJSONArray(ARRAYED_MEMORY).put(index, memory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all array-stored memories
|
||||
* @return An array of all array-based memories
|
||||
*/
|
||||
public JSONArray getArrayMemories()
|
||||
{
|
||||
return memory.getJSONArray(ARRAYED_MEMORY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array-based memory at index
|
||||
* @param index The memory to retrieve
|
||||
* @return The memory stored at index
|
||||
*/
|
||||
public Optional<String> getMemory(int index)
|
||||
{
|
||||
return Optional.ofNullable(memory.getJSONArray(ARRAYED_MEMORY).optString(index, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all memories as a JSON string.
|
||||
* @return A JSON string of all memories
|
||||
*/
|
||||
public String getMemories() {
|
||||
public String getMappedMemories() {
|
||||
ArrayList<String> memories = new ArrayList<>();
|
||||
for (String key : memory.keySet()) {
|
||||
memories.add(key + ": " + memory.getString(key));
|
||||
|
||||
@@ -3,7 +3,7 @@ package me.zacharias.chat.core.memory;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import me.zacharias.chat.ollama.OllamaToolResponse;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class GetMemoriesFunction extends OllamaFunctionTool {
|
||||
@@ -23,7 +23,7 @@ public class GetMemoriesFunction extends OllamaFunctionTool {
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
return new OllamaToolRespnce(name(), CoreMemory.getInstance().getMemories());
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
return new OllamaToolResponse(name(), CoreMemory.getInstance().getMappedMemories());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package me.zacharias.chat.core.memory;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import me.zacharias.chat.ollama.OllamaToolResponse;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
@@ -34,7 +34,9 @@ public class GetMemoryFunction extends OllamaFunctionTool {
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
return new OllamaToolRespnce(name(), memory.getMemory((String) (args[0].value())));
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
return memory.getMemory((String) (args[0].value()))
|
||||
.map(value -> new OllamaToolResponse(name(), value))
|
||||
.orElse(OllamaToolResponse.empty("No memory found for key: " + args[0].value()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package me.zacharias.chat.core.memory;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import me.zacharias.chat.ollama.OllamaToolResponse;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.json.JSONArray;
|
||||
|
||||
@@ -26,7 +26,7 @@ public class GetMemoryIdentitiesFunction extends OllamaFunctionTool {
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
return new OllamaToolRespnce(this.name(), new JSONArray(memory.getMemoriesIdentity()).toString());
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
return new OllamaToolResponse(this.name(), new JSONArray(memory.getMemoriesIdentity()).toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package me.zacharias.chat.core.memory;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import me.zacharias.chat.ollama.OllamaToolResponse;
|
||||
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -35,12 +35,12 @@ public class RemoveMemoryFunction extends OllamaFunctionTool {
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
if (args.length == 0) {
|
||||
throw new OllamaToolErrorException(name(), "Missing memory argument");
|
||||
}
|
||||
String value = (String) args[0].value();
|
||||
memory.removeMemory(value);
|
||||
return new OllamaToolRespnce(name(), "Removed "+value+" to the memory");
|
||||
return new OllamaToolResponse(name(), "Removed "+value+" to the memory");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package me.zacharias.chat.core.memory.array;
|
||||
|
||||
import me.zacharias.chat.core.memory.CoreMemory;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter.OllamaPerameterBuilder.Type;
|
||||
import me.zacharias.chat.ollama.OllamaToolResponse;
|
||||
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AddArrayMemory extends OllamaFunctionTool {
|
||||
|
||||
private CoreMemory memory = CoreMemory.getInstance();
|
||||
|
||||
@Override
|
||||
public @NotNull String name() {
|
||||
return "add_arrayed_memory";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull OllamaPerameter parameters() {
|
||||
return OllamaPerameter.builder()
|
||||
.addProperty("memory", Type.STRING, "The memory to remember", true)
|
||||
.addProperty("index", Type.INT, "The index to put it at. Should be avoided, unless overwriting", false)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
if (args.length > 1) {
|
||||
String memory = null;
|
||||
int index = -1;
|
||||
for (OllamaFunctionArgument arg : args) {
|
||||
if(arg.argument().equals("memory")) {
|
||||
memory = (String) arg.value();
|
||||
}
|
||||
else if(arg.argument().equals("index")) {
|
||||
index = (Integer) arg.value();
|
||||
}
|
||||
}
|
||||
if (memory == null || index < 0) {
|
||||
throw new OllamaToolErrorException(name(), "no memory or index provided");
|
||||
}
|
||||
this.memory.addMemory(memory, index);
|
||||
}
|
||||
else {
|
||||
if(!args[0].argument().equals("memory")) {
|
||||
throw new OllamaToolErrorException(name(), "no memory provided");
|
||||
}
|
||||
this.memory.addMemory(name(), args[0].argument());
|
||||
}
|
||||
return new OllamaToolResponse(name(), "Added arrayed memory");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package me.zacharias.chat.core.memory.array;
|
||||
|
||||
import me.zacharias.chat.core.memory.CoreMemory;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolResponse;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter.OllamaPerameterBuilder.Type;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class GetArrayMemory extends OllamaFunctionTool {
|
||||
|
||||
CoreMemory memory = CoreMemory.getInstance();
|
||||
|
||||
@Override
|
||||
public @NotNull String name() {
|
||||
return "get_array_memory";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull OllamaPerameter parameters() {
|
||||
return OllamaPerameter.builder()
|
||||
.addProperty("index", Type.STRING, "The index to retrieve memory from", false)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
if (args.length != 1) {
|
||||
return new OllamaToolResponse(name(), memory.getArrayMemories().toString());
|
||||
}
|
||||
return memory.getMemory((Integer) args[0].value())
|
||||
.map(value -> new OllamaToolResponse(name(), value))
|
||||
.orElse(OllamaToolResponse.empty("No memory found for key: " + args[0].value()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package me.zacharias.chat.core.memory.array;
|
||||
|
||||
import me.zacharias.chat.core.memory.CoreMemory;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolResponse;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class GetArrayedMemories extends OllamaFunctionTool {
|
||||
private CoreMemory memory = CoreMemory.getInstance();
|
||||
@Override
|
||||
public @NotNull String name() {
|
||||
return "get_arrayed_memories";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull OllamaPerameter parameters() {
|
||||
return OllamaPerameter.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
return new OllamaToolResponse(name(), memory.getArrayMemories().toString());
|
||||
}
|
||||
}
|
||||
@@ -66,8 +66,8 @@ public abstract class OllamaFunctionTool implements OllamaTool {
|
||||
* This is used by Ollama to call the tool.<br>
|
||||
* Throw {@link OllamaToolErrorException} if the tool encounters an error instead of normal exceptions. The {@link OllamaToolErrorException} gets handled more gracefully by {@link Core#handleResponce(JSONObject)}
|
||||
* @param args The arguments to pass to the tool, if any
|
||||
* @return The response from the tool
|
||||
* @return The response from the tool, if null return {@link OllamaToolResponse}
|
||||
* @throws OllamaToolErrorException If the tool encounters an error
|
||||
*/
|
||||
abstract public OllamaToolRespnce function(OllamaFunctionArgument... args);
|
||||
abstract public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args);
|
||||
}
|
||||
|
||||
@@ -65,6 +65,18 @@ public class OllamaPerameter {
|
||||
public static OllamaPerameterBuilder builder() {
|
||||
return new OllamaPerameterBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an empty {@link OllamaPerameter}
|
||||
* @return an empty {@link OllamaPerameter}
|
||||
* @apiNote This is equvalent to
|
||||
* <pre>{@code
|
||||
* OllamaPerameter.builder().build();
|
||||
* }</pre>
|
||||
*/
|
||||
public static OllamaPerameter empty() {
|
||||
return builder().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a builder for {@link OllamaPerameter}.
|
||||
@@ -85,13 +97,10 @@ public class OllamaPerameter {
|
||||
* @param type The type of the parameter
|
||||
* @param description The description of the parameter
|
||||
* @return The {@link OllamaPerameterBuilder}
|
||||
* @apiNote Prefer {@link #addProperty(String, Type, String, boolean)} to be explicit about required state
|
||||
*/
|
||||
public OllamaPerameterBuilder addProperty(String name, Type type, String description) {
|
||||
if(name == null || type == null || description == null) {
|
||||
return this;
|
||||
}
|
||||
propertyMap.put(name, new Property(type.getType(), description));
|
||||
return this;
|
||||
return OllamaPerameterBuilder.this.addProperty(name, type, description, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package me.zacharias.chat.ollama;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Represents a response from a tool.
|
||||
*/
|
||||
public class OllamaToolRespnce extends OllamaMessage {
|
||||
/**
|
||||
* The tool that responded.
|
||||
*/
|
||||
private final String tool;
|
||||
/**
|
||||
* The response from the tool.
|
||||
*/
|
||||
private final String response;
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link OllamaToolRespnce}.
|
||||
* @param tool The tool that responded
|
||||
* @param response The response from the tool
|
||||
*/
|
||||
public OllamaToolRespnce(String tool, String response) {
|
||||
super(OllamaMessageRole.TOOL, new JSONObject().put("tool", tool).put("result", response).toString());
|
||||
this.tool = tool;
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tool that responded.
|
||||
* @return The tool that responded
|
||||
*/
|
||||
public String getTool() {
|
||||
return tool;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the response from the tool.
|
||||
* @return The response from the tool
|
||||
*/
|
||||
public String getResponse() {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package me.zacharias.chat.ollama;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Represents a response from a tool.
|
||||
*/
|
||||
public class OllamaToolResponse extends OllamaMessage {
|
||||
|
||||
/**
|
||||
* Returns an empty tool response
|
||||
* See {@link OllamaToolResponse#empty(String, String)} for a reasoned/described response
|
||||
* @param tool The tool that responded
|
||||
* @return an empty tool response
|
||||
*/
|
||||
public static OllamaToolResponse empty(String tool)
|
||||
{
|
||||
return empty(tool, "No reason provided");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an empty tool response with a reason/description.
|
||||
* See {@link OllamaToolResponse#empty(String)} for reason/description less response
|
||||
* @param tool The tool that responded
|
||||
* @param description A description for why this is empty
|
||||
* @return an empty tool response with a reason
|
||||
*/
|
||||
public static OllamaToolResponse empty(String tool, String description)
|
||||
{
|
||||
return new OllamaToolResponse(tool, "Empty! reason: " + description);
|
||||
}
|
||||
|
||||
/**
|
||||
* The tool that responded.
|
||||
*/
|
||||
private final String tool;
|
||||
/**
|
||||
* The response from the tool.
|
||||
*/
|
||||
private final String response;
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link OllamaToolResponse}.
|
||||
* @param tool The tool that responded
|
||||
* @param response The response from the tool
|
||||
*/
|
||||
public OllamaToolResponse(String tool, String response) {
|
||||
super(OllamaMessageRole.TOOL, new JSONObject().put("tool", tool).put("result", response).toString());
|
||||
this.tool = tool;
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tool that responded.
|
||||
* @return The tool that responded
|
||||
*/
|
||||
public String getTool() {
|
||||
return tool;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the response from the tool.
|
||||
* @return The response from the tool
|
||||
*/
|
||||
public String getResponse() {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class Loader {
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
// Wrap OllamaFunctionArguments[] to ether ToolArgument[] or ToolArguments(current implementation)
|
||||
try {
|
||||
ToolResponse toolResponse = tool.callTool(null);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package plugin;
|
||||
|
||||
//// This class is broken due to the current refactoring on the RPCP part of this project.
|
||||
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import me.zacharias.chat.ollama.OllamaToolResponse;
|
||||
import me.zacharias.chat.plugin.annotation.OllamaTool;
|
||||
import me.zacharias.chat.plugin.annotation.injectons.InjectPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -29,7 +31,7 @@ public class Tool extends OllamaFunctionTool {
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
public @NotNull OllamaToolResponse function(OllamaFunctionArgument... args) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user