forked from Chat_things/NeuroDock
Added a lot of things, but i guse bigest part is the external_tools.py generator
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package me.zacharias.chat.core;
|
||||
|
||||
import me.zacharias.chat.core.memory.AddMemoryFunction;
|
||||
import me.zacharias.chat.core.memory.GetMemoryFunction;
|
||||
import me.zacharias.chat.core.memory.RemoveMemoryFunction;
|
||||
import me.zacharias.chat.ollama.*;
|
||||
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
|
||||
import org.json.JSONArray;
|
||||
@@ -21,7 +24,7 @@ public class Core {
|
||||
private ScheduledExecutorService scheduler;
|
||||
|
||||
private OllamaObject ollamaObject;
|
||||
private ArrayList<OllamaFuntionTool> funtionTools = new ArrayList<>();
|
||||
private ArrayList<Pair<OllamaFuntionTool, String>> funtionTools = new ArrayList<>();
|
||||
|
||||
private String ollamaIP = "localhost";//"192.168.5.184";
|
||||
private int ollamaPort = 11434;
|
||||
@@ -106,10 +109,13 @@ public class Core {
|
||||
messagesWriter.close();
|
||||
|
||||
File f = new File("./cache/messages.json");
|
||||
messagesWriter = new BufferedWriter(new FileWriter(messagesFile));
|
||||
for(OllamaMessage message : ollamaObject.getMessages()) {
|
||||
messages.put(new JSONObject(message.toString()));
|
||||
if(f.exists())
|
||||
{
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
|
||||
messagesWriter = new BufferedWriter(new FileWriter(f));
|
||||
|
||||
messagesWriter.write(messages.toString());
|
||||
messagesWriter.close();
|
||||
@@ -125,8 +131,45 @@ public class Core {
|
||||
supportColor = printMessageHandler.color();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link ollamaObject} object to the provided argument,
|
||||
* Aslo adds the memory base system. see {@link Core#setOllamaObjectNoMemory} if you don't want to add memory functions
|
||||
* @param ollamaObject
|
||||
*/
|
||||
public void setOllamaObject(OllamaObject ollamaObject) {
|
||||
this.ollamaObject = ollamaObject;
|
||||
if(this.ollamaObject == null) {
|
||||
this.ollamaObject = ollamaObject;
|
||||
|
||||
addTool(new AddMemoryFunction(), "Core");
|
||||
addTool(new RemoveMemoryFunction(), "Core");
|
||||
addTool(new GetMemoryFunction(), "Core");
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Ollama object is already set");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link Core#ollamaObject} object to the provided argument,
|
||||
* Dose not add the base system for memory. see {@link Core#setOllamaObject} if you want to add memory function
|
||||
* @param ollamaObject
|
||||
*/
|
||||
public void setOllamaObjectNoMemory(OllamaObject ollamaObject) {
|
||||
if(this.ollamaObject == null) {
|
||||
this.ollamaObject = ollamaObject;
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Ollama object is already set");
|
||||
}
|
||||
}
|
||||
|
||||
public void addTool(OllamaFuntionTool funtionTool, String source) {
|
||||
funtionTools.add(new Pair<>(funtionTool, source));
|
||||
ollamaObject.addTool(funtionTool);
|
||||
}
|
||||
|
||||
public ArrayList<Pair<OllamaFuntionTool, String>> getFuntionTools() {
|
||||
return funtionTools;
|
||||
}
|
||||
|
||||
public OllamaObject getOllamaObject() {
|
||||
@@ -134,7 +177,7 @@ public class Core {
|
||||
}
|
||||
|
||||
public void addFuntionTool(OllamaFuntionTool funtionTool) {
|
||||
funtionTools.add(funtionTool);
|
||||
funtionTools.add(new Pair<>(funtionTool, "External"));
|
||||
}
|
||||
|
||||
public static void flushLog() {
|
||||
@@ -205,7 +248,7 @@ public class Core {
|
||||
if(jsonObject.has("function"))
|
||||
{
|
||||
JSONObject function = jsonObject.getJSONObject("function");
|
||||
List<OllamaFuntionTool> functions = funtionTools.stream().filter(func -> func.name().equalsIgnoreCase(function.getString("name"))).toList();
|
||||
List<Pair<OllamaFuntionTool, String>> functions = funtionTools.stream().filter(func -> func.getKey().name().equalsIgnoreCase(function.getString("name"))).toList();
|
||||
|
||||
if(functions.isEmpty()) {
|
||||
ollamaObject.addMessage(new OllamaToolError("Function '"+function.getString("name")+"' does not exist"));
|
||||
@@ -214,7 +257,7 @@ public class Core {
|
||||
}
|
||||
else {
|
||||
|
||||
OllamaFuntionTool func = functions.getFirst();
|
||||
OllamaFuntionTool func = functions.getFirst().getKey();
|
||||
|
||||
ArrayList<OllamaFunctionArgument> argumentArrayList = new ArrayList<>();
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ public class LaunchOptions {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private boolean loadOld;
|
||||
private boolean loadOld = true;
|
||||
private boolean autoAccept;
|
||||
private boolean serverMode;
|
||||
private int port = 39075;
|
||||
|
||||
27
Core/src/main/java/me/zacharias/chat/core/Pair.java
Normal file
27
Core/src/main/java/me/zacharias/chat/core/Pair.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package me.zacharias.chat.core;
|
||||
|
||||
public class Pair<K, V> {
|
||||
private K key;
|
||||
private V value;
|
||||
|
||||
public Pair(K key, V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setKey(K key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public void setValue(V value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package me.zacharias.chat.core.memory;
|
||||
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFuntionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
|
||||
import org.json.JSONArray;
|
||||
|
||||
public class AddMemoryFunction extends OllamaFuntionTool {
|
||||
CoreMemory memory = CoreMemory.getInstance();
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "add_memory";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Add to the memory";
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaPerameter parameters() {
|
||||
return OllamaPerameter.builder()
|
||||
.addProperty("memory", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The memory to store", true)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
if (args.length == 0) {
|
||||
throw new OllamaToolErrorException(name(), "Missing memory argument");
|
||||
}
|
||||
String value = (String) args[0].getValue();
|
||||
memory.addMemory(value);
|
||||
return new OllamaToolRespnce(name(), "Added "+value+" to the memory");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package me.zacharias.chat.core.memory;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CoreMemory {
|
||||
private static CoreMemory instance = new CoreMemory("./cache/CoreMemory.json");
|
||||
|
||||
public static CoreMemory getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public CoreMemory(String memoryFile) {
|
||||
File f = new File(memoryFile);
|
||||
if (f.exists()) {
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(f));
|
||||
StringBuilder data = new StringBuilder();
|
||||
String buffer = null;
|
||||
while ((buffer = br.readLine()) != null) {
|
||||
data.append(buffer);
|
||||
}
|
||||
JSONArray jsonArray = new JSONArray(data.toString());
|
||||
for(Object obj : jsonArray) {
|
||||
if(obj instanceof String str) {
|
||||
memory.add(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
memory.add(obj.toString());
|
||||
}
|
||||
}
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
this.memoryFile = memoryFile;
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
public void run() {
|
||||
try{
|
||||
File f = new File(memoryFile);
|
||||
if(f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
|
||||
for(String str : memory) {
|
||||
jsonArray.put(str);
|
||||
}
|
||||
|
||||
bw.write(jsonArray.toString());
|
||||
|
||||
bw.close();
|
||||
}catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private ArrayList<String> memory = new ArrayList<>();
|
||||
private final String memoryFile;
|
||||
|
||||
public ArrayList<String> getMemory() {
|
||||
return memory;
|
||||
}
|
||||
|
||||
public void addMemory(String memory) {
|
||||
this.memory.add(memory);
|
||||
}
|
||||
|
||||
public void removeMemory(String memory) {
|
||||
this.memory.remove(memory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package me.zacharias.chat.core.memory;
|
||||
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFuntionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import org.json.JSONArray;
|
||||
|
||||
public class GetMemoryFunction extends OllamaFuntionTool {
|
||||
CoreMemory memory = CoreMemory.getInstance();
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "get_memory";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Retrives all the memory";
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaPerameter parameters() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
return new OllamaToolRespnce(name(), memory.getMemory().toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package me.zacharias.chat.core.memory;
|
||||
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFuntionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
|
||||
|
||||
public class RemoveMemoryFunction extends OllamaFuntionTool {
|
||||
CoreMemory memory = CoreMemory.getInstance();
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "remove_memory";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Remove from the memory";
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaPerameter parameters() {
|
||||
return OllamaPerameter.builder()
|
||||
.addProperty("memory", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The memory to remove", true)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
if (args.length == 0) {
|
||||
throw new OllamaToolErrorException(name(), "Missing memory argument");
|
||||
}
|
||||
String value = (String) args[0].getValue();
|
||||
memory.removeMemory(value);
|
||||
return new OllamaToolRespnce(name(), "Removed "+value+" to the memory");
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ public enum OllamaMessageRole {
|
||||
|
||||
public static OllamaMessageRole fromRole(String role) {
|
||||
for(OllamaMessageRole roleRole : values()) {
|
||||
if(roleRole.role.equals(role))
|
||||
if(roleRole.role.equals(role.toLowerCase()))
|
||||
return roleRole;
|
||||
}
|
||||
throw new IllegalArgumentException("Invalid role: " + role);
|
||||
|
||||
@@ -15,13 +15,13 @@ import java.util.Map;
|
||||
public class OllamaObject {
|
||||
String model;
|
||||
ArrayList<OllamaMessage> messages;
|
||||
OllamaTool[] tools;
|
||||
ArrayList<OllamaTool> tools;
|
||||
JSONObject format;
|
||||
Map<String, Object> options;
|
||||
boolean stream;
|
||||
String keep_alive;
|
||||
|
||||
private OllamaObject(String model, ArrayList<OllamaMessage> messages, OllamaTool[] tools, JSONObject format, Map<String, Object> options, boolean stream, String keep_alive) {
|
||||
private OllamaObject(String model, ArrayList<OllamaMessage> messages, ArrayList<OllamaTool> tools, JSONObject format, Map<String, Object> options, boolean stream, String keep_alive) {
|
||||
this.model = model;
|
||||
this.messages = messages;
|
||||
this.tools = tools;
|
||||
@@ -46,9 +46,9 @@ public class OllamaObject {
|
||||
JSONObject obj = jsonArray.getJSONObject(i);
|
||||
OllamaMessage message;
|
||||
if (!obj.has("tool_calls")) {
|
||||
message = new OllamaMessage(obj.getEnum(OllamaMessageRole.class, "role"), obj.getString("content"));
|
||||
message = new OllamaMessage(OllamaMessageRole.fromRole(obj.getString("role")), obj.getString("content"));
|
||||
} else {
|
||||
message = new OllamaMessageToolCall(obj.getEnum(OllamaMessageRole.class, "role"), obj.getString("content"), obj.getJSONArray("tool_calls"));
|
||||
message = new OllamaMessageToolCall(OllamaMessageRole.fromRole(obj.getString("role")), obj.getString("content"), obj.getJSONArray("tool_calls"));
|
||||
}
|
||||
messages.add(message);
|
||||
}
|
||||
@@ -68,10 +68,14 @@ public class OllamaObject {
|
||||
return messages;
|
||||
}
|
||||
|
||||
public OllamaTool[] getTools() {
|
||||
public ArrayList<OllamaTool> getTools() {
|
||||
return tools;
|
||||
}
|
||||
|
||||
public void addTool(OllamaTool tool) {
|
||||
tools.add(tool);
|
||||
}
|
||||
|
||||
public JSONObject getFormat() {
|
||||
return format;
|
||||
}
|
||||
@@ -193,7 +197,7 @@ public class OllamaObject {
|
||||
}
|
||||
|
||||
public OllamaObject build() {
|
||||
return new OllamaObject(model, messages, tools.toArray(new OllamaTool[0]), format, options, stream, keep_alive);
|
||||
return new OllamaObject(model, messages, tools, format, options, stream, keep_alive);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user