Added javadoc, and fixed spelling errors.

This commit is contained in:
2025-03-14 14:32:38 +01:00
parent e7e8b445bf
commit 907f2e42e5
30 changed files with 1177 additions and 194 deletions

View File

@@ -11,8 +11,16 @@ import java.util.*;
import static me.zacharias.chat.core.Core.writeLog;
/**
* The main class of the Display.<br>
* This is the main class for running as a Terminal application.<br>
* Somewhat meant to be used as a Debug tool for testing your API.
*/
public class Display {
/**
* The Core instance.
*/
Core core = new Core(new PrintMessageHandler() {
@Override
public void printMessage(String message) {
@@ -24,25 +32,29 @@ public class Display {
return true;
}
});
/**
* Creates a new instance of Display.<br>
* This Creates the OllamaObject and adds the tools to it. as well as handles the display of the messages. and the input from the user.
*/
public Display()
{
core.setOllamaObject(OllamaObject.builder()
.setModel("llama3-AI")
.keep_alive(10)
.stream(false)
//.stream(false)
.build());
core.addTool(new TimeTool(), "Internal");
core.addTool(new PythonRunner(core), "Internal");
core.addTool(new TimeTool(), Core.Source.EXTERNAL);
core.addTool(new PythonRunner(core), Core.Source.INTERNAL);
writeLog("Creating base OllamaObject with model: "+core.getOllamaObject().getModel());
System.out.println("Installed tools");
writeLog("Tools installed in this instance");
for(Pair<OllamaFuntionTool, String> funtion : core.getFuntionTools())
for(Pair<OllamaFunctionTool, String> funtion : core.getFuntionTools())
{
StringBuilder args = new StringBuilder();
OllamaPerameter perameter = funtion.getKey().parameters();

View File

@@ -10,7 +10,6 @@ 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;
@@ -26,11 +25,30 @@ import java.util.logging.Logger;
import static me.zacharias.chat.core.Core.writeLog;
public class PythonRunner extends OllamaFuntionTool {
DockerClient dockerClient;
Core core;
ServerSocket serverSocket;
/**
* A tool that runs python code.
* This is a wrapper around a docker container.
* This is partly meant as a proof of concept, but also as a way to run python code while keeping the executed code in a secure environment.
*/
public class PythonRunner extends OllamaFunctionTool {
/**
* The DockerClient instance.
*/
private DockerClient dockerClient;
/**
* The Core instance.
*/
private Core core;
/**
* The ServerSocket instance.
*/
private ServerSocket serverSocket;
/**
* Creates a new instance of PythonRunner.
* @param core The Core instance
*/
public PythonRunner(Core core) {
this.core = core;
@@ -49,7 +67,7 @@ public class PythonRunner extends OllamaFuntionTool {
try {
JSONObject data = new JSONObject(inputLine);
List<Pair<OllamaFuntionTool, String>> list = core.getFuntionTools().stream().filter(funtionTool -> funtionTool.getKey().name().equalsIgnoreCase(data.optString("function", ""))).toList();
List<Pair<OllamaFunctionTool, 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());
@@ -79,7 +97,7 @@ public class PythonRunner extends OllamaFuntionTool {
} catch (Exception e) {
}
} catch (Exception e) {
}
}
});
@@ -128,15 +146,15 @@ public class PythonRunner extends OllamaFuntionTool {
for(OllamaFunctionArgument arg : args)
{
if(arg.getArgument().equals("name"))
if(arg.argument().equals("name"))
{
name = (String) arg.getValue();
name = (String) arg.value();
if(!name.endsWith(".py"))
{
name += ".py";
}
} else if (arg.getArgument().equals("code")) {
code = (String) arg.getValue();
} else if (arg.argument().equals("code")) {
code = (String) arg.value();
}
}
@@ -144,7 +162,7 @@ public class PythonRunner extends OllamaFuntionTool {
{
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encodedhash = digest.digest(String.valueOf(args[0].getValue()).getBytes(StandardCharsets.UTF_8));
byte[] encodedhash = digest.digest(String.valueOf(args[0].value()).getBytes(StandardCharsets.UTF_8));
StringBuffer hexString = new StringBuffer();
for(byte b : encodedhash)
{
@@ -221,7 +239,12 @@ public class PythonRunner extends OllamaFuntionTool {
throw new OllamaToolErrorException(name(), "Docker unavalible");
}
}
/**
* Generates the external_tools.py file.<br>
* This is meant to provide the python code with all ExternalTools defined in the OllamaObject.
* @return The generated external_tools.py file
*/
private String generateExternalTools() {
StringBuilder code = new StringBuilder();
@@ -243,9 +266,9 @@ public class PythonRunner extends OllamaFuntionTool {
""");
for(Pair<OllamaFuntionTool, String> funtionTool : core.getFuntionTools())
for(Pair<OllamaFunctionTool, String> funtionTool : core.getFuntionTools())
{
OllamaFuntionTool tool = funtionTool.getKey();
OllamaFunctionTool tool = funtionTool.getKey();
String name = tool.name();
@@ -293,7 +316,10 @@ public class PythonRunner extends OllamaFuntionTool {
return code.toString();
}
/**
* A Helper class to get the logs from a docker container.
*/
public class GetContainerLog {
private DockerClient dockerClient;
private String containerId;
@@ -301,13 +327,22 @@ public class PythonRunner extends OllamaFuntionTool {
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<>();

View File

@@ -1,13 +1,17 @@
package me.zacharias.chat.display;
import me.zacharias.chat.ollama.OllamaFunctionArgument;
import me.zacharias.chat.ollama.OllamaFuntionTool;
import me.zacharias.chat.ollama.OllamaFunctionTool;
import me.zacharias.chat.ollama.OllamaPerameter;
import me.zacharias.chat.ollama.OllamaToolRespnce;
import java.util.Date;
public class TimeTool extends OllamaFuntionTool {
/**
* A tool that returns the current date.
* This gives Ollama the ability to get the current date.
*/
public class TimeTool extends OllamaFunctionTool {
@Override
public OllamaToolRespnce function(OllamaFunctionArgument... arguments) {