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:
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
@@ -1,5 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.13 (pythonFiles)" />
|
||||
</component>
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.zacharias.chat.display;
|
||||
|
||||
import me.zacharias.chat.core.Core;
|
||||
import me.zacharias.chat.core.Pair;
|
||||
import me.zacharias.chat.core.PrintMessageHandler;
|
||||
import me.zacharias.chat.ollama.*;
|
||||
import org.json.JSONObject;
|
||||
@@ -30,38 +31,35 @@ public class Display {
|
||||
core.setOllamaObject(OllamaObject.builder()
|
||||
.setModel("llama3-AI")
|
||||
.keep_alive(10)
|
||||
.addTool(new TimeTool())
|
||||
.addTool(new PythonRunner())
|
||||
.stream(false)
|
||||
.build());
|
||||
|
||||
core.addTool(new TimeTool(), "Internal");
|
||||
core.addTool(new PythonRunner(core), "Internal");
|
||||
|
||||
writeLog("Creating base OllamaObject with model: "+core.getOllamaObject().getModel());
|
||||
|
||||
System.out.println("Installed tools");
|
||||
writeLog("Tools installed in this instance");
|
||||
|
||||
for(OllamaTool tool : core.getOllamaObject().getTools())
|
||||
for(Pair<OllamaFuntionTool, String> funtion : core.getFuntionTools())
|
||||
{
|
||||
if(tool instanceof OllamaFuntionTool funtion)
|
||||
{
|
||||
StringBuilder args = new StringBuilder();
|
||||
OllamaPerameter perameter = funtion.parameters();
|
||||
if(perameter != null) {
|
||||
JSONObject obj = perameter.getProperties();
|
||||
for (String name : obj.keySet()) {
|
||||
args.append(args.toString().isBlank() ? "" : ", ").append(obj.getJSONObject(name).getString("type")).append(Arrays.stream(perameter.getRequired()).anyMatch(str -> str.equalsIgnoreCase(name)) ? "" : "?").append(" ").append(name);
|
||||
}
|
||||
StringBuilder args = new StringBuilder();
|
||||
OllamaPerameter perameter = funtion.getKey().parameters();
|
||||
if(perameter != null) {
|
||||
JSONObject obj = perameter.getProperties();
|
||||
for (String name : obj.keySet()) {
|
||||
args.append(args.toString().isBlank() ? "" : ", ").append(obj.getJSONObject(name).getString("type")).append(Arrays.stream(perameter.getRequired()).anyMatch(str -> str.equalsIgnoreCase(name)) ? "" : "?").append(" ").append(name);
|
||||
}
|
||||
|
||||
System.out.println("> Function: "+funtion.name()+"("+args+")");
|
||||
writeLog("Function: "+funtion.name()+"("+args+")");
|
||||
core.addFuntionTool(funtion);
|
||||
}
|
||||
|
||||
System.out.println("> Function: "+funtion.getKey().name()+"("+args+") ["+funtion.getValue()+"]");
|
||||
writeLog("Function: "+funtion.getKey().name()+"("+args+") ["+funtion.getValue()+"]");
|
||||
}
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
System.out.println("Message Trsnscription:");
|
||||
System.out.println("Message Transcription:");
|
||||
try {
|
||||
while (true) {
|
||||
System.out.print("> ");
|
||||
|
||||
@@ -6,14 +6,21 @@ import com.github.dockerjava.api.command.LogContainerCmd;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
||||
import com.github.dockerjava.core.DockerClientBuilder;
|
||||
import me.zacharias.chat.core.Core;
|
||||
import me.zacharias.chat.core.Pair;
|
||||
import me.zacharias.chat.ollama.*;
|
||||
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
|
||||
import org.apache.http.conn.HttpHostConnectException;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -21,7 +28,66 @@ import static me.zacharias.chat.core.Core.writeLog;
|
||||
|
||||
public class PythonRunner extends OllamaFuntionTool {
|
||||
DockerClient dockerClient;
|
||||
public PythonRunner() {
|
||||
Core core;
|
||||
|
||||
ServerSocket serverSocket;
|
||||
public PythonRunner(Core core) {
|
||||
this.core = core;
|
||||
|
||||
try {
|
||||
serverSocket = new ServerSocket(6050);
|
||||
Thread thread = new Thread(() -> {
|
||||
while (true) {
|
||||
try {
|
||||
Socket socket = serverSocket.accept();
|
||||
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
|
||||
String inputLine = in.readLine();
|
||||
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
|
||||
try {
|
||||
JSONObject data = new JSONObject(inputLine);
|
||||
List<Pair<OllamaFuntionTool, String>> list = core.getFuntionTools().stream().filter(funtionTool -> funtionTool.getKey().name().equalsIgnoreCase(data.optString("function", ""))).toList();
|
||||
|
||||
if (list.isEmpty()) {
|
||||
out.write(new JSONObject().put("error", "Function dose't exist").toString());
|
||||
out.newLine();
|
||||
out.flush();
|
||||
out.close();
|
||||
in.close();
|
||||
socket.close();
|
||||
continue;
|
||||
}
|
||||
|
||||
ArrayList<OllamaFunctionArgument> args = new ArrayList<>();
|
||||
|
||||
for (Object o : data.optJSONArray("arguments", new JSONArray())) {
|
||||
if (o instanceof JSONObject obj) {
|
||||
OllamaFunctionArgument arg = new OllamaFunctionArgument(obj.getString("name"), obj.getString("value"));
|
||||
args.add(arg);
|
||||
}
|
||||
}
|
||||
|
||||
out.write(list.getFirst().getKey().function(args.toArray(new OllamaFunctionArgument[0])).getResponse());
|
||||
out.newLine();
|
||||
out.flush();
|
||||
out.close();
|
||||
in.close();
|
||||
socket.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
thread.start();
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
DefaultDockerClientConfig.Builder config
|
||||
= DefaultDockerClientConfig.createDefaultConfigBuilder()
|
||||
@@ -94,6 +160,8 @@ public class PythonRunner extends OllamaFuntionTool {
|
||||
|
||||
File pythonFile = new File("./pythonFiles", name);
|
||||
|
||||
code = "from external_tools import *\n\n"+code;
|
||||
|
||||
if(!pythonFile.exists())
|
||||
{
|
||||
try {
|
||||
@@ -103,12 +171,24 @@ public class PythonRunner extends OllamaFuntionTool {
|
||||
}catch(IOException e) {}
|
||||
}
|
||||
|
||||
File f = new File("./pythonFiles", "external_tools.py");
|
||||
|
||||
try {
|
||||
String external_tools = generateExternalTools();
|
||||
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
|
||||
bw.write(external_tools);
|
||||
bw.flush();
|
||||
bw.close();
|
||||
}catch(IOException e) {}
|
||||
|
||||
try {
|
||||
String containerId = dockerClient.createContainerCmd("python").withCmd("python", name).exec().getId();
|
||||
dockerClient.copyArchiveToContainerCmd(containerId)
|
||||
.withHostResource(pythonFile.getPath())
|
||||
//.withRemotePath("~/")
|
||||
.exec();
|
||||
|
||||
dockerClient.copyArchiveToContainerCmd(containerId)
|
||||
.withHostResource(f.getPath())
|
||||
.exec();
|
||||
|
||||
dockerClient.startContainerCmd(containerId).exec();
|
||||
@@ -142,6 +222,78 @@ public class PythonRunner extends OllamaFuntionTool {
|
||||
}
|
||||
}
|
||||
|
||||
private String generateExternalTools() {
|
||||
StringBuilder code = new StringBuilder();
|
||||
|
||||
code.append("""
|
||||
import socket
|
||||
import json
|
||||
|
||||
HOST = "host.docker.internal"
|
||||
PORT = 6050
|
||||
|
||||
def connect(data):
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
s.connect((HOST, PORT))
|
||||
data = data + "\\n"
|
||||
s.sendall(data.encode("utf-8"))
|
||||
responce = s.recv(4096)
|
||||
return responce.decode("utf-8")
|
||||
|
||||
|
||||
""");
|
||||
|
||||
for(Pair<OllamaFuntionTool, String> funtionTool : core.getFuntionTools())
|
||||
{
|
||||
OllamaFuntionTool tool = funtionTool.getKey();
|
||||
|
||||
String name = tool.name();
|
||||
|
||||
code.append("def ").append(name).append("(");
|
||||
|
||||
ArrayList<String> args = new ArrayList<>();
|
||||
|
||||
boolean first = true;
|
||||
try {
|
||||
for (String argName : tool.parameters().getProperties().keySet()) {
|
||||
args.add(argName);
|
||||
if (!first) {
|
||||
code.append(", ");
|
||||
}
|
||||
code.append(argName);
|
||||
if (Arrays.stream(tool.parameters().getRequired()).noneMatch(required -> required.equals(argName))) {
|
||||
code.append("=None");
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
}catch (Exception e) {}
|
||||
code.append("):\n");
|
||||
code.append(" data = {\"function\":\"").append(tool.name()).append("\"");
|
||||
if(args.size() > 0)
|
||||
{
|
||||
code.append(",\"arguments\":[");
|
||||
}
|
||||
first = true;
|
||||
for(String str : args)
|
||||
{
|
||||
if(!first)
|
||||
{
|
||||
code.append(", ");
|
||||
}
|
||||
code.append("{\"name\": \"").append(str).append("\", \"value\": ").append(str).append("}");
|
||||
first = false;
|
||||
}
|
||||
if(args.size() > 0)
|
||||
{
|
||||
code.append("]");
|
||||
}
|
||||
code.append("}\n");
|
||||
code.append(" return connect(json.dumps(data))\n\n");
|
||||
}
|
||||
|
||||
return code.toString();
|
||||
}
|
||||
|
||||
public class GetContainerLog {
|
||||
private DockerClient dockerClient;
|
||||
private String containerId;
|
||||
|
||||
@@ -40,13 +40,13 @@ public class Launcher {
|
||||
case "--help", "-h" -> {
|
||||
System.out.println("""
|
||||
Launch options for AI_chat
|
||||
-h --help Provides this help message
|
||||
-s --server Starts the application as API server
|
||||
-p --port Provides the port number that the API server shuld use, defaults to 39075
|
||||
-o --output Redirects the API Server output to another file
|
||||
-y Auto accepts to prompts, used for a seamless run. Not recomended when running as Display
|
||||
-l --loadOld Loads the old message history. [Default]
|
||||
--api Provides API docs
|
||||
-h --help Provides this help message
|
||||
-s --server Starts the application as API server
|
||||
-p --port Provides the port number that the API server shuld use, defaults to 39075
|
||||
-o --output Redirects the API Server output to another file
|
||||
-y Auto accepts to prompts, used for a seamless run. Not recomended when running as Display
|
||||
-d --dontloadOld Don't load old messages
|
||||
--api Provides API docs
|
||||
""");
|
||||
return;
|
||||
}
|
||||
@@ -54,9 +54,9 @@ public class Launcher {
|
||||
{
|
||||
options.setAutoAccept(true);
|
||||
}
|
||||
case "-l", "--loadold" ->
|
||||
case "-d", "--dontloadold" ->
|
||||
{
|
||||
options.setLoadOld(true);
|
||||
options.setLoadOld(false);
|
||||
}
|
||||
|
||||
default -> {
|
||||
|
||||
Reference in New Issue
Block a user