Basicly refactord the entire project into a modular nature
This commit is contained in:
20
Display/build.gradle
Normal file
20
Display/build.gradle
Normal file
@@ -0,0 +1,20 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group = 'me.zacharias'
|
||||
version = '1.0-SNAPSHOT'
|
||||
|
||||
dependencies {
|
||||
implementation project(":Core")
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
jar{
|
||||
manifest {
|
||||
attributes 'Main-Class': 'me.zacharias.chat.display.Main'
|
||||
}
|
||||
}
|
||||
106
Display/src/main/java/me/zacharias/chat/display/Display.java
Normal file
106
Display/src/main/java/me/zacharias/chat/display/Display.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package me.zacharias.chat.display;
|
||||
|
||||
import me.zacharias.chat.core.Core;
|
||||
import me.zacharias.chat.core.PrintMessageHandler;
|
||||
import me.zacharias.chat.ollama.*;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import static me.zacharias.chat.core.Core.writeLog;
|
||||
|
||||
public class Display {
|
||||
|
||||
Core core = new Core(new PrintMessageHandler() {
|
||||
@Override
|
||||
public void printMessage(String message) {
|
||||
System.out.println(">> "+message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean color() {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
public Display()
|
||||
{
|
||||
|
||||
core.setOllamaObject(OllamaObject.builder()
|
||||
.setModel("llama3-AI")
|
||||
.keep_alive(10)
|
||||
.addTool(new TimeTool())
|
||||
.addTool(new PythonRunner())
|
||||
.stream(false)
|
||||
.build());
|
||||
|
||||
writeLog("Creating base OllamaObject with model: "+core.getOllamaObject().getModel());
|
||||
|
||||
System.out.println("Installed tools");
|
||||
writeLog("Tools installed in this instance");
|
||||
|
||||
for(OllamaTool tool : core.getOllamaObject().getTools())
|
||||
{
|
||||
if(tool instanceof OllamaFuntionTool funtion)
|
||||
{
|
||||
StringBuilder args = new StringBuilder();
|
||||
OllamaPerameter perameter = funtion.parameters();
|
||||
if(perameter != null) {
|
||||
JSONObject obj = perameter.getProperties();
|
||||
for (String name : obj.keySet()) {
|
||||
args.append(args.toString().isBlank() ? "" : ", ").append(obj.getJSONObject(name).getString("type")).append(Arrays.stream(perameter.getRequired()).anyMatch(str -> str.equalsIgnoreCase(name)) ? "" : "?").append(" ").append(name);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("> Function: "+funtion.name()+"("+args+")");
|
||||
writeLog("Function: "+funtion.name()+"("+args+")");
|
||||
core.addFuntionTool(funtion);
|
||||
}
|
||||
}
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
System.out.println("Message Trsnscription:");
|
||||
try {
|
||||
while (true) {
|
||||
System.out.print("> ");
|
||||
StringBuilder message = new StringBuilder(br.readLine());
|
||||
while (br.ready()) {
|
||||
message.append("\n").append(br.readLine());
|
||||
}
|
||||
if (message.toString().startsWith("/")) {
|
||||
switch (message.substring(1)) {
|
||||
case "help":
|
||||
System.out.print("""
|
||||
Available commands:
|
||||
/help Prints this help message.
|
||||
/bye Exits the program.
|
||||
/write Flushes the current log stream to file.
|
||||
""");
|
||||
break;
|
||||
case "bye":
|
||||
writeLog("Exiting program...");
|
||||
System.out.println("Bye!");
|
||||
System.exit(0);
|
||||
return;
|
||||
case "write":
|
||||
core.flushLog();
|
||||
break;
|
||||
default:
|
||||
System.out.println("Unknown command: " + message);
|
||||
}
|
||||
} else {
|
||||
writeLog("User: " + message);
|
||||
core.getOllamaObject().addMessage(new OllamaMessage(OllamaMessageRole.USER, message.toString()));
|
||||
//System.out.println(ollamaObject.toString());
|
||||
core.handleResponce(core.qurryOllama());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("Exiting due to exception");
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package me.zacharias.chat.display;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.async.ResultCallback;
|
||||
import com.github.dockerjava.api.command.LogContainerCmd;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
||||
import com.github.dockerjava.core.DockerClientBuilder;
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFuntionTool;
|
||||
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.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static me.zacharias.chat.core.Core.writeLog;
|
||||
|
||||
public class PythonRunner extends OllamaFuntionTool {
|
||||
DockerClient dockerClient;
|
||||
public PythonRunner() {
|
||||
|
||||
DefaultDockerClientConfig.Builder config
|
||||
= DefaultDockerClientConfig.createDefaultConfigBuilder()
|
||||
.withDockerHost("tcp://localhost:2375")
|
||||
.withDockerTlsVerify(false);
|
||||
dockerClient = DockerClientBuilder
|
||||
.getInstance(config)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "python_runner";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Runs python code";
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaPerameter parameters() {
|
||||
return OllamaPerameter.builder()
|
||||
.addProperty("code", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The code to be executed", true)
|
||||
.addProperty("name", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The name of the python code")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
|
||||
if(args.length == 0)
|
||||
{
|
||||
throw new OllamaToolErrorException(name(), "Missing code argument");
|
||||
}
|
||||
|
||||
String name = null;
|
||||
String code = null;
|
||||
|
||||
for(OllamaFunctionArgument arg : args)
|
||||
{
|
||||
if(arg.getArgument().equals("name"))
|
||||
{
|
||||
name = (String) arg.getValue();
|
||||
if(!name.endsWith(".py"))
|
||||
{
|
||||
name += ".py";
|
||||
}
|
||||
} else if (arg.getArgument().equals("code")) {
|
||||
code = (String) arg.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
if(name == null)
|
||||
{
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
byte[] encodedhash = digest.digest(String.valueOf(args[0].getValue()).getBytes(StandardCharsets.UTF_8));
|
||||
StringBuffer hexString = new StringBuffer();
|
||||
for(byte b : encodedhash)
|
||||
{
|
||||
hexString.append(String.format("%02x", b));
|
||||
}
|
||||
name = hexString.toString()+".py";
|
||||
}catch (Exception e) {}
|
||||
}
|
||||
|
||||
name = name.replace(" ", "_");
|
||||
|
||||
writeLog("Running python code `" + name + "`");
|
||||
|
||||
File pythonFile = new File("./pythonFiles", name);
|
||||
|
||||
if(!pythonFile.exists())
|
||||
{
|
||||
try {
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(pythonFile));
|
||||
writer.write(code);
|
||||
writer.close();
|
||||
}catch(IOException e) {}
|
||||
}
|
||||
|
||||
String containerId = dockerClient.createContainerCmd("python").withCmd("python", name).exec().getId();
|
||||
dockerClient.copyArchiveToContainerCmd(containerId)
|
||||
.withHostResource(pythonFile.getPath())
|
||||
//.withRemotePath("~/")
|
||||
.exec();
|
||||
|
||||
dockerClient.startContainerCmd(containerId).exec();
|
||||
|
||||
GetContainerLog log = new GetContainerLog(dockerClient, containerId);
|
||||
|
||||
List<String> logs = new ArrayList<>();
|
||||
|
||||
do {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
logs.addAll(log.getDockerLogs());
|
||||
}
|
||||
while (logs.isEmpty());
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
public GetContainerLog(DockerClient dockerClient, String containerId) {
|
||||
this.dockerClient = dockerClient;
|
||||
this.containerId = containerId;
|
||||
this.lastLogTime = (int) (System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package me.zacharias.chat.display;
|
||||
|
||||
import me.zacharias.chat.ollama.OllamaFunctionArgument;
|
||||
import me.zacharias.chat.ollama.OllamaFuntionTool;
|
||||
import me.zacharias.chat.ollama.OllamaPerameter;
|
||||
import me.zacharias.chat.ollama.OllamaToolRespnce;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class TimeTool extends OllamaFuntionTool {
|
||||
|
||||
@Override
|
||||
public OllamaToolRespnce function(OllamaFunctionArgument... arguments) {
|
||||
Date date = new Date();
|
||||
return new OllamaToolRespnce(name(), date.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "get_current_date";
|
||||
}
|
||||
|
||||
@Override
|
||||
public OllamaPerameter parameters() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Get the current date";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user