- 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:
Generated
+1
-1
@@ -7,7 +7,7 @@
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="corretto-21" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="26" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -34,7 +34,7 @@ public class Tool {
|
||||
}
|
||||
return ResponseEntity.ok(new NewToolResponse(request.getName(), request.getDescription(), arguments));
|
||||
} else {
|
||||
return ResponseEntity.status(400).body("Tool already exists");
|
||||
return ResponseEntity.badRequest().body("Tool already exists");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
package me.zacharias.chat.core.files;
|
||||
|
||||
import me.zacharias.chat.core.Core;
|
||||
import me.zacharias.chat.core.Pair;
|
||||
import me.zacharias.chat.core.files.tools.ReadFileTool;
|
||||
import me.zacharias.chat.ollama.OllamaTool;
|
||||
import org.intellij.lang.annotations.MagicConstant;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
@@ -20,7 +25,7 @@ public class FileHandler {
|
||||
/**
|
||||
* The directory used as base for this instance of {@link FileHandler}. This is where all files that can be read or writen will be located
|
||||
*/
|
||||
private final File/*System*/ directory;
|
||||
private final Path/*System*/ root;
|
||||
|
||||
/**
|
||||
* Creates a new instance as well as setting the {@link #instance} to this new one
|
||||
@@ -28,19 +33,41 @@ public class FileHandler {
|
||||
*/
|
||||
public FileHandler(@MagicConstant(valuesFromClass = FileHandlerLocation.class) String baseDirectory) {
|
||||
try {
|
||||
FileSystem fs = FileSystems.newFileSystem(new File(baseDirectory).toPath());
|
||||
//fs.getPath()
|
||||
directory = new File(baseDirectory);
|
||||
if (!directory.exists())
|
||||
directory.mkdirs();
|
||||
root = Path.of(baseDirectory).toAbsolutePath().normalize();
|
||||
if (!root.toFile().exists()) {
|
||||
root.toFile().mkdirs();
|
||||
}
|
||||
|
||||
instance = this;
|
||||
}catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
throw new FileHandlerException("Failed to create FileHandler instance with base directory \"" + baseDirectory + "\"");
|
||||
}
|
||||
}
|
||||
|
||||
public static FileHandler getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all {@link me.zacharias.chat.ollama.OllamaTool}'s this module adds
|
||||
* @return
|
||||
*/
|
||||
public static ArrayList<Pair<? extends OllamaTool, String>> getTools() {
|
||||
ArrayList<Pair<? extends OllamaTool, String>> tools = new ArrayList<>();
|
||||
|
||||
tools.add(new Pair<>(new ReadFileTool(), Core.Source.INTERNAL));
|
||||
|
||||
return tools;
|
||||
}
|
||||
|
||||
public Path resolve(String relative) throws IOException{
|
||||
Path p = root.resolve(relative).normalize();
|
||||
if (!p.startsWith(root)) throw new IOException("Access denied");
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param filename
|
||||
@@ -48,11 +75,12 @@ public class FileHandler {
|
||||
* @throws FileHandlerException
|
||||
*/
|
||||
public String readFile(String filename) throws FileHandlerException {
|
||||
if(filename.contains(".."))
|
||||
{
|
||||
throw new FileHandlerException("File \"" + filename + "\" tries to retrace path");
|
||||
File file = null;
|
||||
try {
|
||||
file = resolve(filename).toFile();
|
||||
} catch (IOException e) {
|
||||
throw new FileHandlerException("Illegal restricted path");
|
||||
}
|
||||
File file = new File(directory, filename);
|
||||
if(file.exists())
|
||||
{
|
||||
try{
|
||||
@@ -82,4 +110,13 @@ public class FileHandler {
|
||||
}
|
||||
throw new FileHandlerException("Cant find file \""+filename+"\"");
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a relative path from {@link FileHandler#root} to file
|
||||
* @param file the file to be relativised
|
||||
* @return the relative path from {@link FileHandler#root} to file
|
||||
*/
|
||||
public String path(File file) {
|
||||
return root.relativize(file.toPath()).toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package me.zacharias.chat.core.files.tools;
|
||||
|
||||
import me.zacharias.chat.core.files.FileHandler;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class ReadFileTool extends OllamaFunctionTool {
|
||||
FileHandler fs = FileHandler.getInstance();
|
||||
@Override
|
||||
public String name() {
|
||||
return "read_file";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Reads the file of path or null if that file dosent exists";
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaPerameter parameters() {
|
||||
return OllamaPerameter.builder()
|
||||
.addProperty("file_path", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The path to the file to be read")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
String orgPath = null;
|
||||
for (OllamaFunctionArgument arg : args) {
|
||||
if(arg.argument().equals("file_path")) {
|
||||
orgPath = (String) arg.value();
|
||||
}
|
||||
}
|
||||
|
||||
if(orgPath == null) {
|
||||
throw new OllamaToolErrorException(this.name(), "Missing required argument 'file_path'");
|
||||
}
|
||||
|
||||
Path filePath = null;
|
||||
|
||||
try{
|
||||
filePath = fs.resolve(orgPath);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new OllamaToolErrorException(this.name(), ex);
|
||||
}
|
||||
|
||||
File file = filePath.toFile();
|
||||
if(!file.exists()) {
|
||||
throw new OllamaToolErrorException(this.name(), "File does not exist: " + fs.path(file));
|
||||
}
|
||||
|
||||
BufferedReader br = null;
|
||||
try{
|
||||
br = new BufferedReader(new FileReader(file));
|
||||
}catch (FileNotFoundException ex) {
|
||||
throw new OllamaToolErrorException(this.name(), "File not found: " + fs.path(file));
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String tmp = null;
|
||||
try {
|
||||
while ((tmp = br.readLine()) != null) {
|
||||
sb.append(tmp).append("\n");
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new OllamaToolErrorException(this.name(), "Error reading file: " + fs.path(file));
|
||||
}
|
||||
return new OllamaToolRespnce(this.name(), sb.toString());
|
||||
}
|
||||
}
|
||||
@@ -416,7 +416,8 @@ public class OllamaObject {
|
||||
* @param tools The tools to add
|
||||
* @return The {@link OllamaObjectBuilder}
|
||||
*/
|
||||
public OllamaObjectBuilder addTools(Pair<OllamaTool, String>... tools) {
|
||||
@SafeVarargs
|
||||
public final OllamaObjectBuilder addTools(Pair<OllamaTool, String>... tools) {
|
||||
this.tools.addAll(List.of(tools));
|
||||
return this;
|
||||
}
|
||||
@@ -453,11 +454,10 @@ public class OllamaObject {
|
||||
|
||||
public OllamaObjectBuilder addFileTools(@MagicConstant(valuesFromClass = FileHandlerLocation.class) String baseDirectory)
|
||||
{
|
||||
FileHandler fileHandler = new FileHandler(baseDirectory);
|
||||
new FileHandler(baseDirectory);
|
||||
|
||||
if(false);
|
||||
|
||||
throw new IllegalArgumentException("FileHandler is not supported yet!");
|
||||
//throw new IllegalArgumentException("FileHandler is not supported yet!");
|
||||
return addTools(FileHandler.getTools());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,6 +28,10 @@ public class OllamaToolErrorException extends RuntimeException {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public OllamaToolErrorException(String tool, Exception ex) {
|
||||
this(tool, ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tool that caused the error.
|
||||
* @return The tool that caused the error
|
||||
|
||||
@@ -50,10 +50,10 @@ public class Display {
|
||||
{
|
||||
|
||||
core.setOllamaObject/*NoMemory*/(OllamaObject.builder()
|
||||
.setModel("llama3.2")
|
||||
.setModel("llama3.1:8b")
|
||||
.keep_alive(10)
|
||||
//.stream(false)
|
||||
//.addFileTools(FileHandlerLocation.DATA_FILES)
|
||||
.addFileTools(FileHandlerLocation.DATA_FILES)
|
||||
.addMessages(new SystemMessage("You need to emulate feelings and emotions."))
|
||||
.build());
|
||||
|
||||
@@ -61,13 +61,12 @@ public class Display {
|
||||
|
||||
core.addTool(new TimeTool(), Core.Source.INTERNAL);
|
||||
// TODO: Well Docker failes when luanched.... Fuck
|
||||
PythonRunner pythonRunner = new PythonRunner(core);
|
||||
core.addTool(pythonRunner, Core.Source.INTERNAL);
|
||||
//core.addTools(new MALAPITool().getOllamaTools());
|
||||
//core.addTools(new GeniusTools().getGeniusTools());
|
||||
//core.addTools(new WikipediaTool().getWikipediaToolsInstance());
|
||||
core.addTool(new PythonRunner(core), Core.Source.INTERNAL);
|
||||
core.addTools(new MALAPITool().getOllamaTools());
|
||||
core.addTools(new GeniusTools().getGeniusTools());
|
||||
core.addTools(new WikipediaTool().getWikipediaToolsInstance());
|
||||
|
||||
APIApplication.start();
|
||||
//APIApplication.start();
|
||||
|
||||
//core.getOllamaObject().addMessage(new OllamaMessage(OllamaMessageRole.SYSTEM, "Have a nice tone and use formal wording"));
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ dependencies {
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
|
||||
implementation "org.jsoup:jsoup:1.20.1"
|
||||
implementation 'io.github.classgraph:classgraph:4.8.158'
|
||||
implementation 'io.github.classgraph:classgraph:4.8.184'
|
||||
|
||||
implementation project(":Core")
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ dependencies {
|
||||
|
||||
implementation project(":Core")
|
||||
|
||||
implementation 'io.github.classgraph:classgraph:4.8.158'
|
||||
implementation 'io.github.classgraph:classgraph:4.8.184'
|
||||
}
|
||||
|
||||
test {
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# This is used in sub-project that dosent explicitly require the API sub-project to only include if thay shuld be included project wide.
|
||||
useAPI = true
|
||||
@@ -7,7 +7,6 @@ version = '1.0-SNAPSHOT'
|
||||
|
||||
dependencies {
|
||||
implementation project(":Display")
|
||||
implementation project(":API")
|
||||
implementation project(":Core")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package me.zacharias.chat.launcher;
|
||||
|
||||
//import me.zacharias.chat.api.APIApplication;
|
||||
import me.zacharias.chat.api.APIApplication;
|
||||
import me.zacharias.chat.core.LaunchOptions;
|
||||
import me.zacharias.chat.display.Display;
|
||||
|
||||
@@ -93,7 +92,8 @@ public class Launcher {
|
||||
if (options.isServerMode()) {
|
||||
System.out.println("Starting in API mode...");
|
||||
try {
|
||||
APIApplication.start();
|
||||
Class<?> clazz = Class.forName("me.zacharias.chat.api.APIApplication");
|
||||
clazz.getMethod("start").invoke(null);
|
||||
}catch (Exception e)
|
||||
{
|
||||
System.out.println("Failed to start API server: " + e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user