- GradleAPI - Launcher - MALAPITool Started finishing up the File handeling module in Core - Added ReadFileTool.java which allowes to read files from this contained "file system" modulized dependency for the API Updated for Launcher.java to use class and method refrence to entry point to remove hard dependency Added a @SafeVarargs to OllamaObject.java made OllamaObject.OllamaObjectBuilder#addFileTools not throw IllegalArgumentException and instead add the tooling and return the builder Added overloaded constructor OllamaToolErrorException#OllamaToolErrorException(String, Exception) to allow for forwarding of exceptions, although this is not recomended PythonRunner - Fixed bugs with it under Linux host(this MUST be refractord to support Windows and Mac hosts) - Added a overwrite for default python print method to send prints over the external_tools socket to be put into output log for the runner - Cleaned up some stuff - Updated to properly refer to files - Fixed cmd.sh generation - Removed old "-c" addition to the command - Removed old code related to how logs pulled from containers standerd output's - Updated the generation of external_tools.py to respect pythons requirment of optinal arguments being after all required Updated Tool#addTool to respect the standerd of spring boot's return model
This commit is contained in:
@@ -100,7 +100,21 @@ public class PythonRunner extends OllamaFunctionTool {
|
||||
continue;
|
||||
}
|
||||
|
||||
outputBuffers.getOrDefault(containerId, new StringBuilder()).append(data.optString("text", ""));
|
||||
outputBuffers.compute(containerId, (k, buf) -> {
|
||||
if(buf == null) {
|
||||
buf = new StringBuilder();
|
||||
}
|
||||
buf.append(data.optString("text", ""));
|
||||
buf.append(System.lineSeparator());
|
||||
return buf;
|
||||
});
|
||||
out.write(new JSONObject().put("container_id", containerId).toString());
|
||||
out.newLine();
|
||||
out.flush();
|
||||
out.close();
|
||||
in.close();
|
||||
socket.close();
|
||||
continue;
|
||||
}
|
||||
|
||||
List<Pair<OllamaFunctionTool, String>> list = core.getFuntionTools().stream().filter(funtionTool -> funtionTool.getKey().name().equalsIgnoreCase(toolName)).toList();
|
||||
@@ -155,7 +169,6 @@ public class PythonRunner extends OllamaFunctionTool {
|
||||
.maxConnections(10)
|
||||
.connectionTimeout(Duration.ofSeconds(100))
|
||||
.responseTimeout(Duration.ofSeconds(100))
|
||||
.sslConfig(config.getSSLConfig())
|
||||
.build();
|
||||
|
||||
DockerHttpClient.Request ping = DockerHttpClient.Request.builder()
|
||||
@@ -343,15 +356,17 @@ public class PythonRunner extends OllamaFunctionTool {
|
||||
File f = new File("./pythonFiles", "external_tools.py");
|
||||
|
||||
String containerId = dockerClient.createContainerCmd("archlinux")
|
||||
.withCmd("/bin/bash","./cmd.sh")
|
||||
.withCmd("/bin/bash","/cmd.sh")
|
||||
.withHostConfig(HostConfig.newHostConfig()
|
||||
.withExtraHosts("host.docker.internal:host-gateway"))
|
||||
.exec().getId();
|
||||
|
||||
outputBuffers.put(containerId, new StringBuilder());
|
||||
|
||||
try {
|
||||
String external_tools = generateExternalTools(containerId);
|
||||
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
|
||||
bw.write(external_tools.replace("\\n", "\n"));
|
||||
bw.write(external_tools/*.replace("\\n", "\n").replace("\\!n", "\\n")*/);
|
||||
bw.flush();
|
||||
bw.close();
|
||||
}catch(IOException e) {}
|
||||
@@ -369,8 +384,8 @@ public class PythonRunner extends OllamaFunctionTool {
|
||||
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""")
|
||||
cp /external_tools.py ./pythonRun > /dev/null
|
||||
cp\s/""")
|
||||
.append(name).append("""
|
||||
\s./pythonRun > /dev/null
|
||||
cd pythonRun > /dev/null
|
||||
@@ -389,7 +404,6 @@ public class PythonRunner extends OllamaFunctionTool {
|
||||
|
||||
cmd.append("python ").append(name);//.append(" exit");
|
||||
|
||||
pythonArgs.add("-c");
|
||||
pythonArgs.add(cmd.toString());
|
||||
|
||||
StringBuilder fullCmd = new StringBuilder();
|
||||
@@ -424,32 +438,16 @@ public class PythonRunner extends OllamaFunctionTool {
|
||||
|
||||
dockerClient.startContainerCmd(containerId).exec();
|
||||
|
||||
GetContainerLog log = new GetContainerLog(dockerClient, containerId);
|
||||
dockerClient.waitContainerCmd(containerId).start().awaitCompletion();
|
||||
|
||||
List<String> logs = new ArrayList<>();
|
||||
String output = outputBuffers.remove(containerId).toString();
|
||||
|
||||
do {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
logs.addAll(log.getDockerLogs());
|
||||
}
|
||||
while (isRunning(containerId));
|
||||
dockerClient.removeContainerCmd(containerId).exec();
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
for (String s : logs) {
|
||||
output.append(s).append("\n");
|
||||
}
|
||||
|
||||
//writeLog("Result from python: " + output.toString());
|
||||
|
||||
return new OllamaToolRespnce(name(), output.toString());
|
||||
return new OllamaToolRespnce(name(), output.isEmpty() ? "Code ran successfully with no output" : output);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new OllamaToolErrorException(name(), "Docker unavalible");
|
||||
throw new OllamaToolErrorException(name(), "Docker unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -488,15 +486,24 @@ public class PythonRunner extends OllamaFunctionTool {
|
||||
|
||||
boolean first = true;
|
||||
try {
|
||||
for (String argName : tool.parameters().getProperties().keySet()) {
|
||||
for(String argName : tool.parameters().getRequired())
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
for (String argName : tool.parameters().getProperties().keySet()) {
|
||||
if(Arrays.asList(tool.parameters().getRequired()).contains(argName)) continue;
|
||||
|
||||
args.add(argName);
|
||||
if (!first) {
|
||||
code.append(", ");
|
||||
}
|
||||
code.append(argName).append("=None");
|
||||
first = false;
|
||||
}
|
||||
}catch (Exception e) {}
|
||||
@@ -526,69 +533,4 @@ public class PythonRunner extends OllamaFunctionTool {
|
||||
|
||||
return code.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a Docker Container is running
|
||||
* @param containerId The ID of the Container
|
||||
* @return a boolean weather it's running or not
|
||||
*/
|
||||
public boolean isRunning(String containerId)
|
||||
{
|
||||
InspectContainerResponse cmd = dockerClient.inspectContainerCmd(containerId).exec();
|
||||
return Boolean.TRUE.equals(cmd.getState().getRunning());
|
||||
}
|
||||
|
||||
/**
|
||||
* A Helper class to get the logs from a docker container.
|
||||
*/
|
||||
public class GetContainerLog {
|
||||
private DockerClient dockerClient;
|
||||
private String containerId;
|
||||
private int lastLogTime;
|
||||
|
||||
private static String nameOfLogger = "dockertest.PrintContainerLog";
|
||||
private static Logger myLogger = Logger.getLogger(nameOfLogger);
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link GetContainerLog}
|
||||
* @param dockerClient The DockerClient instance
|
||||
* @param containerId The container id
|
||||
*/
|
||||
public GetContainerLog(DockerClient dockerClient, String containerId) {
|
||||
this.dockerClient = dockerClient;
|
||||
this.containerId = containerId;
|
||||
this.lastLogTime = (int) (System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the logs of the container.
|
||||
* @return The logs of the container
|
||||
*/
|
||||
public List<String> getDockerLogs() {
|
||||
|
||||
final List<String> logs = new ArrayList<>();
|
||||
|
||||
LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId);
|
||||
logContainerCmd.withStdOut(true).withStdErr(true);
|
||||
logContainerCmd.withSince(lastLogTime); // UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries since that timestamp.
|
||||
// logContainerCmd.withTail(4); // get only the last 4 log entries
|
||||
|
||||
logContainerCmd.withTimestamps(true);
|
||||
|
||||
try {
|
||||
logContainerCmd.exec(new ResultCallback.Adapter<Frame>() {
|
||||
@Override
|
||||
public void onNext(Frame item) {
|
||||
logs.add(new String(item.getPayload()).trim());
|
||||
}
|
||||
}).awaitCompletion();
|
||||
} catch (InterruptedException e) {
|
||||
myLogger.severe("Interrupted Exception!" + e.getMessage());
|
||||
}
|
||||
|
||||
lastLogTime = (int) (System.currentTimeMillis() / 1000) + 5; // assumes at least a 5 second wait between calls to getDockerLogs
|
||||
|
||||
return logs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user