added a global cache directory, not fully properly implemented, and dose not respect t.ex the FreeDesktop specifications for cache directories and files OPS this is a partial commit! things might not add up on this commit.
This commit is contained in:
@@ -77,6 +77,7 @@ public class Core {
|
|||||||
public static String DATA;
|
public static String DATA;
|
||||||
public static File DATA_DIR;
|
public static File DATA_DIR;
|
||||||
public static File PLUGIN_DIRECTORY;
|
public static File PLUGIN_DIRECTORY;
|
||||||
|
public static File CACHE_DIRECTORY;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
setDataDirectory("AI-Chat");
|
setDataDirectory("AI-Chat");
|
||||||
@@ -117,6 +118,11 @@ public class Core {
|
|||||||
if(!PLUGIN_DIRECTORY.exists()) {
|
if(!PLUGIN_DIRECTORY.exists()) {
|
||||||
PLUGIN_DIRECTORY.mkdirs();
|
PLUGIN_DIRECTORY.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CACHE_DIRECTORY = new File(DATA + "/cache");
|
||||||
|
if(!CACHE_DIRECTORY.exists()) {
|
||||||
|
CACHE_DIRECTORY.mkdirs();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -261,6 +267,13 @@ public class Core {
|
|||||||
if(this.ollamaObject == null) {
|
if(this.ollamaObject == null) {
|
||||||
this.ollamaObject = ollamaObject;
|
this.ollamaObject = ollamaObject;
|
||||||
|
|
||||||
|
for(Pair<OllamaTool, String> tool : ollamaObject.getTools()) {
|
||||||
|
if(tool.getKey() instanceof OllamaFunctionTool functionTool)
|
||||||
|
{
|
||||||
|
funtionTools.add(new Pair<>(functionTool, tool.getValue()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
addTool(new AddMemoryFunction(), Source.CORE);
|
addTool(new AddMemoryFunction(), Source.CORE);
|
||||||
addTool(new RemoveMemoryFunction(), Source.CORE);
|
addTool(new RemoveMemoryFunction(), Source.CORE);
|
||||||
addTool(new GetMemoryFunction(), Source.CORE);
|
addTool(new GetMemoryFunction(), Source.CORE);
|
||||||
@@ -280,6 +293,12 @@ public class Core {
|
|||||||
public void setOllamaObjectNoMemory(OllamaObject ollamaObject) {
|
public void setOllamaObjectNoMemory(OllamaObject ollamaObject) {
|
||||||
if(this.ollamaObject == null) {
|
if(this.ollamaObject == null) {
|
||||||
this.ollamaObject = ollamaObject;
|
this.ollamaObject = ollamaObject;
|
||||||
|
for(Pair<OllamaTool, String> tool : ollamaObject.getTools()) {
|
||||||
|
if(tool.getKey() instanceof OllamaFunctionTool functionTool)
|
||||||
|
{
|
||||||
|
funtionTools.add(new Pair<>(functionTool, tool.getValue()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalArgumentException("Ollama object is already set");
|
throw new IllegalArgumentException("Ollama object is already set");
|
||||||
@@ -434,7 +453,9 @@ public class Core {
|
|||||||
if(jsonObject.has("function"))
|
if(jsonObject.has("function"))
|
||||||
{
|
{
|
||||||
JSONObject function = jsonObject.getJSONObject("function");
|
JSONObject function = jsonObject.getJSONObject("function");
|
||||||
List<Pair<OllamaFunctionTool, String>> functions = funtionTools.stream().filter(func -> (func.getKey().name()+func.getValue()).equalsIgnoreCase(function.getString("name"))).toList();
|
List<Pair<OllamaFunctionTool, String>> functions = funtionTools.stream()
|
||||||
|
.filter(func -> (
|
||||||
|
func.getKey().name()+func.getValue()).equalsIgnoreCase(function.getString("name"))).toList();
|
||||||
|
|
||||||
ArrayList<OllamaFunctionArgument> argumentArrayList = new ArrayList<>();
|
ArrayList<OllamaFunctionArgument> argumentArrayList = new ArrayList<>();
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ dependencies {
|
|||||||
implementation project(":API")
|
implementation project(":API")
|
||||||
implementation project(":WikipediaTool")
|
implementation project(":WikipediaTool")
|
||||||
|
|
||||||
implementation("com.github.docker-java:docker-java-core:3.6.0")
|
implementation("com.github.docker-java:docker-java-core:3.7.1")
|
||||||
implementation("com.github.docker-java:docker-java-transport-httpclient5:3.6.0")
|
implementation("com.github.docker-java:docker-java-transport-httpclient5:3.7.1")
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ package me.zacharias.chat.display;
|
|||||||
|
|
||||||
import com.github.dockerjava.api.DockerClient;
|
import com.github.dockerjava.api.DockerClient;
|
||||||
import com.github.dockerjava.api.async.ResultCallback;
|
import com.github.dockerjava.api.async.ResultCallback;
|
||||||
import com.github.dockerjava.api.async.ResultCallbackTemplate;
|
|
||||||
import com.github.dockerjava.api.command.*;
|
import com.github.dockerjava.api.command.*;
|
||||||
import com.github.dockerjava.api.model.BuildResponseItem;
|
|
||||||
import com.github.dockerjava.api.model.Frame;
|
import com.github.dockerjava.api.model.Frame;
|
||||||
import com.github.dockerjava.api.model.Statistics;
|
import com.github.dockerjava.api.model.HostConfig;
|
||||||
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
||||||
|
import com.github.dockerjava.core.DockerClientImpl;
|
||||||
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
|
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
|
||||||
import com.github.dockerjava.transport.DockerHttpClient;
|
import com.github.dockerjava.transport.DockerHttpClient;
|
||||||
import me.zacharias.chat.core.Core;
|
import me.zacharias.chat.core.Core;
|
||||||
@@ -28,6 +27,7 @@ import java.time.Duration;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static me.zacharias.chat.core.Core.writeLog;
|
import static me.zacharias.chat.core.Core.writeLog;
|
||||||
@@ -52,6 +52,16 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
*/
|
*/
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A hash map of all python runners, this is to make it thread safe in a theoretical case of multiple python runners running at once
|
||||||
|
*/
|
||||||
|
private ConcurrentHashMap<String, StringBuilder> outputBuffers = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The docker client to be used throughout this tool
|
||||||
|
*/
|
||||||
|
DockerHttpClient dockerHttpClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of PythonRunner.
|
* Creates a new instance of PythonRunner.
|
||||||
* @param core The Core instance
|
* @param core The Core instance
|
||||||
@@ -59,8 +69,6 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
public PythonRunner(Core core) {
|
public PythonRunner(Core core) {
|
||||||
this.core = core;
|
this.core = core;
|
||||||
|
|
||||||
generateExternalTools();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
serverSocket = new ServerSocket(6050);
|
serverSocket = new ServerSocket(6050);
|
||||||
Thread thread = new Thread(() -> {
|
Thread thread = new Thread(() -> {
|
||||||
@@ -76,7 +84,26 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
JSONObject data = new JSONObject(inputLine);
|
JSONObject data = new JSONObject(inputLine);
|
||||||
List<Pair<OllamaFunctionTool, String>> list = core.getFuntionTools().stream().filter(funtionTool -> funtionTool.getKey().name().equalsIgnoreCase(data.optString("function", ""))).toList();
|
|
||||||
|
String toolName = data.optString("function", "");
|
||||||
|
|
||||||
|
if(toolName.equals("print_output")) {
|
||||||
|
// This is a "special" "tool" that is used to hijack the output of the python program so logs can be put else where since the LLM dose'nt need the entirety of the log from pacman and what not.
|
||||||
|
String containerId = data.optString("container_id", "");
|
||||||
|
if(containerId.isEmpty()) {
|
||||||
|
out.write(new JSONObject().put("error", "Missing container id").toString());
|
||||||
|
out.newLine();
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
in.close();
|
||||||
|
socket.close();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
outputBuffers.getOrDefault(containerId, new StringBuilder()).append(data.optString("text", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Pair<OllamaFunctionTool, String>> list = core.getFuntionTools().stream().filter(funtionTool -> funtionTool.getKey().name().equalsIgnoreCase(toolName)).toList();
|
||||||
|
|
||||||
if (list.isEmpty()) {
|
if (list.isEmpty()) {
|
||||||
out.write(new JSONObject().put("error", "Function don't exist").toString());
|
out.write(new JSONObject().put("error", "Function don't exist").toString());
|
||||||
@@ -120,12 +147,11 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
|
|
||||||
DefaultDockerClientConfig config
|
DefaultDockerClientConfig config
|
||||||
= DefaultDockerClientConfig.createDefaultConfigBuilder()
|
= DefaultDockerClientConfig.createDefaultConfigBuilder()
|
||||||
.withDockerHost("tcp://localhost:2375")
|
|
||||||
.withDockerTlsVerify(false)
|
|
||||||
.withDockerCertPath("~/.docker")
|
|
||||||
.build();
|
.build();
|
||||||
DockerHttpClient dockerHttpClient = new ApacheDockerHttpClient.Builder()
|
|
||||||
|
dockerHttpClient = new ApacheDockerHttpClient.Builder()
|
||||||
.dockerHost(config.getDockerHost())
|
.dockerHost(config.getDockerHost())
|
||||||
|
.sslConfig(config.getSSLConfig())
|
||||||
.maxConnections(10)
|
.maxConnections(10)
|
||||||
.connectionTimeout(Duration.ofSeconds(100))
|
.connectionTimeout(Duration.ofSeconds(100))
|
||||||
.responseTimeout(Duration.ofSeconds(100))
|
.responseTimeout(Duration.ofSeconds(100))
|
||||||
@@ -141,14 +167,14 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
{
|
{
|
||||||
if(!(response.getStatusCode() == 200))
|
if(!(response.getStatusCode() == 200))
|
||||||
{
|
{
|
||||||
writeLog("Failed to ping docker");
|
writeLog("Failed to ping docker: " + response.getStatusCode());
|
||||||
System.out.println("Failed to ping docker. Docker components is disabled.");
|
System.out.println("Failed to ping docker (" + response.getStatusCode() + "). Docker components is disabled.");
|
||||||
dockerClient = null;
|
dockerClient = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(!(IOUtils.toString(response.getBody(), Charset.defaultCharset()).equals("OK")))
|
if(!(IOUtils.toString(response.getBody(), Charset.defaultCharset()).equals("OK")))
|
||||||
{
|
{
|
||||||
writeLog("Failed to ping docker");
|
writeLog("Failed to ping docker: body not OK");
|
||||||
System.out.println("Failed to ping docker. Docker components is disabled.");
|
System.out.println("Failed to ping docker. Docker components is disabled.");
|
||||||
dockerClient = null;
|
dockerClient = null;
|
||||||
return;
|
return;
|
||||||
@@ -156,9 +182,73 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
writeLog("Failed to ping docker");
|
writeLog("Failed to ping docker: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
System.out.println("Failed to ping docker. Docker components is disabled.");
|
System.out.println("Failed to ping docker. Docker components is disabled.");
|
||||||
dockerClient = null;
|
dockerClient = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dockerClient = DockerClientImpl.getInstance(config, dockerHttpClient);
|
||||||
|
|
||||||
|
initializingImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A small function to pull the archlinux:latest image from Docker Hub. See <a href="https://hub.docker.com/_/archlinux/">ArchLinux Docker Hub</a>
|
||||||
|
* <br>
|
||||||
|
* I want to note here that the reason for using an ArchLinux image and not somthing like python or Ubuntu. Is because I'm more
|
||||||
|
* comfortable with how to do this on an Arch host, and I could not manage to pull libraries for the python image. So those who despise Arch you're welcome to re-write the arch specific parts.
|
||||||
|
*/
|
||||||
|
private void initializingImage() {
|
||||||
|
// Check if archlinux:latest is pulled already
|
||||||
|
DockerHttpClient.Request checkArchlinux_latest = DockerHttpClient.Request.builder()
|
||||||
|
.method(DockerHttpClient.Request.Method.GET)
|
||||||
|
.path("/images/archlinux:latest/json")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
boolean archlinux_latestAvailable = false;
|
||||||
|
|
||||||
|
try(DockerHttpClient.Response response = dockerHttpClient.execute(checkArchlinux_latest))
|
||||||
|
{
|
||||||
|
if(response.getStatusCode() == 200)
|
||||||
|
{
|
||||||
|
archlinux_latestAvailable = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
writeLog("Failed to fetch archlinux:latest: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
System.out.println("Failed to fetch archlinux:latest.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!archlinux_latestAvailable) {
|
||||||
|
|
||||||
|
// If it's not already downloaded, we will download it.
|
||||||
|
DockerHttpClient.Request downloadArchlinux_latest = DockerHttpClient.Request.builder()
|
||||||
|
.method(DockerHttpClient.Request.Method.POST)
|
||||||
|
.path("/images/create?fromImage=archlinux&tag=latest")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (DockerHttpClient.Response response = dockerHttpClient.execute(downloadArchlinux_latest)) {
|
||||||
|
if (response.getStatusCode() == 200) {
|
||||||
|
writeLog("Successfully downloaded archlinux:latest");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
writeLog("Failed to download archlinux:latest: " + response.getStatusCode() + "\n" + response.getBody());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
writeLog("Failed to fetch archlinux:latest: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
System.out.println("Failed to fetch archlinux:latest.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
writeLog("archlinux:latest already exists");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,13 +259,13 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String description() {
|
public String description() {
|
||||||
return "Runs python code";
|
return "Runs python code.\nuse python's print method for output";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OllamaPerameter parameters() {
|
public OllamaPerameter parameters() {
|
||||||
return OllamaPerameter.builder()
|
return OllamaPerameter.builder()
|
||||||
.addProperty("code", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The code to be executed", true)
|
.addProperty("code", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The code to be executed, optional if a file with the same name exists.")
|
||||||
.addProperty("name", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The name of the python code")
|
.addProperty("name", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The name of the python code")
|
||||||
.addProperty("libs", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "A space separated list of pip packages needed")
|
.addProperty("libs", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "A space separated list of pip packages needed")
|
||||||
.build();
|
.build();
|
||||||
@@ -214,6 +304,11 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(name == null && code == null)
|
||||||
|
{
|
||||||
|
throw new OllamaToolErrorException(name(), "Missing code or name");
|
||||||
|
}
|
||||||
|
|
||||||
if(name == null)
|
if(name == null)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@@ -247,8 +342,14 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
|
|
||||||
File f = new File("./pythonFiles", "external_tools.py");
|
File f = new File("./pythonFiles", "external_tools.py");
|
||||||
|
|
||||||
|
String containerId = dockerClient.createContainerCmd("archlinux")
|
||||||
|
.withCmd("/bin/bash","./cmd.sh")
|
||||||
|
.withHostConfig(HostConfig.newHostConfig()
|
||||||
|
.withExtraHosts("host.docker.internal:host-gateway"))
|
||||||
|
.exec().getId();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String external_tools = generateExternalTools();
|
String external_tools = generateExternalTools(containerId);
|
||||||
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
|
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
|
||||||
bw.write(external_tools.replace("\\n", "\n"));
|
bw.write(external_tools.replace("\\n", "\n"));
|
||||||
bw.flush();
|
bw.flush();
|
||||||
@@ -258,10 +359,23 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
try {
|
try {
|
||||||
ArrayList<String> pythonArgs = new ArrayList<>();
|
ArrayList<String> pythonArgs = new ArrayList<>();
|
||||||
|
|
||||||
pythonArgs.add("/bin/bash");
|
pythonArgs.add("#!/bin/env bash\n");
|
||||||
|
|
||||||
StringBuilder cmd = new StringBuilder();
|
StringBuilder cmd = new StringBuilder();
|
||||||
cmd.append("set -e\n\npacman --noconfirm -Sy > /dev/null\npacman --noconfirm -S python python-pip > /dev/null\nmkdir pythonRun > /dev/null\npython -m venv ./pythonRun > /dev/null\ncp external_tools.py ./pythonRun > /dev/null\ncp ").append(name).append(" ./pythonRun > /dev/null\ncd pythonRun > /dev/null\nsource ./bin/activate > /dev/null\n");
|
cmd.append("""
|
||||||
|
set -e
|
||||||
|
|
||||||
|
pacman --noconfirm -Sy > /dev/null
|
||||||
|
pacman --noconfirm -S python python-pip > /dev/null
|
||||||
|
mkdir pythonRun > /dev/null
|
||||||
|
python -m venv ./pythonRun > /dev/null
|
||||||
|
cp external_tools.py ./pythonRun > /dev/null
|
||||||
|
cp\s""")
|
||||||
|
.append(name).append("""
|
||||||
|
\s./pythonRun > /dev/null
|
||||||
|
cd pythonRun > /dev/null
|
||||||
|
source ./bin/activate > /dev/null
|
||||||
|
""");
|
||||||
|
|
||||||
if(libs != null && !libs.isEmpty())
|
if(libs != null && !libs.isEmpty())
|
||||||
{
|
{
|
||||||
@@ -285,20 +399,17 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
fullCmd.append(arg).append(" ");
|
fullCmd.append(arg).append(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
File program = new File("./cache", "cmd.sh");
|
File program = new File(Core.CACHE_DIRECTORY, "cmd.sh");
|
||||||
if(program.exists()){
|
if(program.exists()){
|
||||||
program.delete();
|
program.delete();
|
||||||
}
|
}
|
||||||
program.createNewFile();
|
program.createNewFile();
|
||||||
|
|
||||||
BufferedWriter bw = new BufferedWriter(new FileWriter(program));
|
BufferedWriter bw = new BufferedWriter(new FileWriter(program));
|
||||||
bw.write(cmd.toString());
|
bw.write(fullCmd.toString());
|
||||||
bw.flush();
|
bw.flush();
|
||||||
bw.close();
|
bw.close();
|
||||||
|
|
||||||
|
|
||||||
String containerId = dockerClient.createContainerCmd("archlinux").withCmd("/bin/bash","./cmd.sh").exec().getId();
|
|
||||||
|
|
||||||
dockerClient.copyArchiveToContainerCmd(containerId)
|
dockerClient.copyArchiveToContainerCmd(containerId)
|
||||||
.withHostResource(pythonFile.getPath())
|
.withHostResource(pythonFile.getPath())
|
||||||
.exec();
|
.exec();
|
||||||
@@ -311,14 +422,8 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
.withHostResource(program.getPath())
|
.withHostResource(program.getPath())
|
||||||
.exec();
|
.exec();
|
||||||
|
|
||||||
//InputStream stdin = new ByteArrayInputStream(fullCmd.toString().getBytes(StandardCharsets.UTF_8));
|
|
||||||
|
|
||||||
dockerClient.startContainerCmd(containerId).exec();
|
dockerClient.startContainerCmd(containerId).exec();
|
||||||
|
|
||||||
//dockerClient.attachContainerCmd(containerId).withStdIn(stdin).exec(null);
|
|
||||||
|
|
||||||
//dockerClient.execCreateCmd(containerId).withCmd(fullCmd.toString()).exec();
|
|
||||||
|
|
||||||
GetContainerLog log = new GetContainerLog(dockerClient, containerId);
|
GetContainerLog log = new GetContainerLog(dockerClient, containerId);
|
||||||
|
|
||||||
List<String> logs = new ArrayList<>();
|
List<String> logs = new ArrayList<>();
|
||||||
@@ -354,9 +459,11 @@ public class PythonRunner extends OllamaFunctionTool {
|
|||||||
* @return The generated external_tools.py file
|
* @return The generated external_tools.py file
|
||||||
* @throws IllegalStateException This is thrown when the external_tool_base.py can not be read, this should be handled by disabling the python runner.
|
* @throws IllegalStateException This is thrown when the external_tool_base.py can not be read, this should be handled by disabling the python runner.
|
||||||
*/
|
*/
|
||||||
private String generateExternalTools() throws IllegalStateException{
|
private String generateExternalTools(String containerId) throws IllegalStateException{
|
||||||
StringBuilder code = new StringBuilder();
|
StringBuilder code = new StringBuilder();
|
||||||
|
|
||||||
|
code.append("CONTAINER_ID = \"").append(containerId).append("\"\n\n");
|
||||||
|
|
||||||
try (InputStream in = getClass().getResourceAsStream("/external_tool_base.py");
|
try (InputStream in = getClass().getResourceAsStream("/external_tool_base.py");
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
|
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
|
||||||
String tmp = null;
|
String tmp = null;
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ def connect(data):
|
|||||||
return responce.decode("utf-8")
|
return responce.decode("utf-8")
|
||||||
|
|
||||||
def print_output(text):
|
def print_output(text):
|
||||||
data = {"function": "print_output", "arguments": [{"name": "text", "value": text}]}
|
data = {"function": "print_output", "container_id": CONTAINER_ID, "text": text}
|
||||||
connect(json.dumps(data))
|
connect(json.dumps(data))
|
||||||
|
|
||||||
def _neurodock_print(*args, **kwargs):
|
def _neurodock_print(*args, **kwargs):
|
||||||
@@ -22,3 +22,5 @@ def _neurodock_print(*args, **kwargs):
|
|||||||
print_output(text)
|
print_output(text)
|
||||||
|
|
||||||
builtins.print = _neurodock_print
|
builtins.print = _neurodock_print
|
||||||
|
|
||||||
|
## Following is the generated
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
CONTAINER_ID = "c25df4d642bdeff0e0b4c4e72138c7b17db09cf174c7e5448538c6811ee10064"
|
||||||
|
|
||||||
|
import socket
|
||||||
|
import json
|
||||||
|
import builtins
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
def print_output(text):
|
||||||
|
data = {"function": "print_output", "container_id": CONTAINER_ID, "text": text}
|
||||||
|
connect(json.dumps(data))
|
||||||
|
|
||||||
|
def _neurodock_print(*args, **kwargs):
|
||||||
|
text = " ".join(str(a) for a in args)
|
||||||
|
print_output(text)
|
||||||
|
|
||||||
|
builtins.print = _neurodock_print
|
||||||
|
|
||||||
|
## Following is the generated
|
||||||
|
def read_file(file_path=None):
|
||||||
|
data = {"function":"read_file","arguments":[{"name": "file_path", "value": file_path}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def add_memory(memory, identity):
|
||||||
|
data = {"function":"add_memory","arguments":[{"name": "memory", "value": memory}, {"name": "identity", "value": identity}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def remove_memory(identity):
|
||||||
|
data = {"function":"remove_memory","arguments":[{"name": "identity", "value": identity}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def get_memory(identity):
|
||||||
|
data = {"function":"get_memory","arguments":[{"name": "identity", "value": identity}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def get_memories():
|
||||||
|
data = {"function":"get_memories"}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def get_memory_identities():
|
||||||
|
data = {"function":"get_memory_identities"}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def get_current_date():
|
||||||
|
data = {"function":"get_current_date"}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def python_runner(code=None, name=None, libs=None):
|
||||||
|
data = {"function":"python_runner","arguments":[{"name": "code", "value": code}, {"name": "name", "value": name}, {"name": "libs", "value": libs}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def get_anime_details(id, fields=None):
|
||||||
|
data = {"function":"get_anime_details","arguments":[{"name": "id", "value": id}, {"name": "fields", "value": fields}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def get_anime_list(offset=None, query, fields=None):
|
||||||
|
data = {"function":"get_anime_list","arguments":[{"name": "offset", "value": offset}, {"name": "query", "value": query}, {"name": "fields", "value": fields}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def get_manag_details(id, fields=None):
|
||||||
|
data = {"function":"get_manag_details","arguments":[{"name": "id", "value": id}, {"name": "fields", "value": fields}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def get_manga_list(offset=None, query, fields=None):
|
||||||
|
data = {"function":"get_manga_list","arguments":[{"name": "offset", "value": offset}, {"name": "query", "value": query}, {"name": "fields", "value": fields}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def findsong(title):
|
||||||
|
data = {"function":"findsong","arguments":[{"name": "title", "value": title}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def get_lyrics(song_id):
|
||||||
|
data = {"function":"get_lyrics","arguments":[{"name": "song_id", "value": song_id}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
|
def get_wiki_page_text(title):
|
||||||
|
data = {"function":"get_wiki_page_text","arguments":[{"name": "title", "value": title}]}
|
||||||
|
return json.loads(connect(json.dumps(data)))
|
||||||
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
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")
|
|
||||||
|
|
||||||
|
|
||||||
def add_memory(memory):
|
|
||||||
data = {"function":"add_memory","arguments":[{"name": "memory", "value": memory}]}
|
|
||||||
return connect(json.dumps(data))
|
|
||||||
|
|
||||||
def remove_memory(memory):
|
|
||||||
data = {"function":"remove_memory","arguments":[{"name": "memory", "value": memory}]}
|
|
||||||
return connect(json.dumps(data))
|
|
||||||
|
|
||||||
def get_memory():
|
|
||||||
data = {"function":"get_memory"}
|
|
||||||
return connect(json.dumps(data))
|
|
||||||
|
|
||||||
def get_current_date():
|
|
||||||
data = {"function":"get_current_date"}
|
|
||||||
return connect(json.dumps(data))
|
|
||||||
|
|
||||||
def python_runner(code, name=None):
|
|
||||||
data = {"function":"python_runner","arguments":[{"name": "code", "value": code}, {"name": "name", "value": name}]}
|
|
||||||
return connect(json.dumps(data))
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user