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,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;
|
||||
|
||||
Reference in New Issue
Block a user