- 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:
@@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,6 +27,10 @@ public class OllamaToolErrorException extends RuntimeException {
|
||||
this.tool = tool;
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public OllamaToolErrorException(String tool, Exception ex) {
|
||||
this(tool, ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tool that caused the error.
|
||||
|
||||
Reference in New Issue
Block a user