forked from Chat_things/NeuroDock
Started work on API and remembrance fuction
This commit is contained in:
@@ -1,13 +1,127 @@
|
||||
package me.zacharias.chat.api;
|
||||
|
||||
import me.zacharias.chat.core.Core;
|
||||
import me.zacharias.chat.core.LaunchOptions;
|
||||
import me.zacharias.chat.core.PrintMessageHandler;
|
||||
import me.zacharias.chat.ollama.OllamaObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class APIServer {
|
||||
ArrayList<Client> clientsList = new ArrayList<>();
|
||||
ServerSocket serverSocket;
|
||||
PrintStream dataOut;
|
||||
|
||||
public APIServer(int port, String redirectedOutput) {
|
||||
PrintMessageHandler printMessageHandler = new PrintMessageHandler() {
|
||||
@Override
|
||||
public void printMessage(String message) {
|
||||
|
||||
synchronized (clientsList) {
|
||||
for (Client client : clientsList) {
|
||||
boolean success = client.sendMessage(message);
|
||||
if (!success) {
|
||||
clientsList.remove(client);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean color() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
Core core = new Core(printMessageHandler);
|
||||
|
||||
/**
|
||||
* Options foir this is expected to be passed thru the {@link LaunchOptions#instance} object.<br>
|
||||
* Used objects:<br>
|
||||
* - {@link LaunchOptions#autoAccept}<br>
|
||||
* - {@link LaunchOptions#redirectOutput}<br>
|
||||
* - {@link LaunchOptions#port}<br>
|
||||
*/
|
||||
public APIServer() {
|
||||
LaunchOptions options = LaunchOptions.getInstance();
|
||||
String redirectedOutput = options.getRedirectOutput();
|
||||
int port = options.getPort();
|
||||
|
||||
core.setOllamaObject(OllamaObject.builder()
|
||||
.setModel("llama3-AI")
|
||||
.keep_alive(10)
|
||||
.stream(false)
|
||||
.build());
|
||||
|
||||
if (!Paths.get(redirectedOutput).toFile().getParentFile().exists()) {
|
||||
System.out.println("Failed to be able to open the redirected output file due to missing directory");
|
||||
}
|
||||
|
||||
File f = new File(redirectedOutput);
|
||||
|
||||
try {
|
||||
if (f.exists()) {
|
||||
System.out.println("Output already exists");
|
||||
System.out.print("Overwrite the existing output file? [y/N]: ");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
char c;
|
||||
|
||||
if (options.isAutoAccept()) {
|
||||
c = 'y';
|
||||
} else {
|
||||
String s = sc.nextLine();
|
||||
c = s.isBlank() ? 'n' : s.charAt(0);
|
||||
}
|
||||
|
||||
if (Character.toLowerCase(c) == 'y') {
|
||||
f.delete();
|
||||
f.createNewFile();
|
||||
dataOut = new PrintStream(new FileOutputStream(f), true);
|
||||
} else {
|
||||
System.out.print("Rename existing output file? [y/N]: ");
|
||||
String s = sc.nextLine();
|
||||
c = s.isBlank() ? 'n' : s.charAt(0);
|
||||
if (c == 'y') {
|
||||
System.out.println("New file name for [" + f.getName() + "]: ");
|
||||
File newFile = new File(f.getParentFile(), sc.nextLine().trim());
|
||||
if (f.renameTo(newFile)) {
|
||||
System.out.println("Old file placed in [" + newFile.getPath() + "]");
|
||||
f = new File(redirectedOutput);
|
||||
} else {
|
||||
System.out.println("Failed to rename file. Proceeding with appending data.");
|
||||
}
|
||||
if (f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
dataOut = new PrintStream(new FileOutputStream(f), true);
|
||||
} else {
|
||||
System.out.print("Appending new data to [" + f.getPath() + "]");
|
||||
dataOut = new PrintStream(new FileOutputStream(f, true), true);
|
||||
Date date = new Date();
|
||||
dataOut.println("\n\nNew instance started at: " + date.toString() + "\n");
|
||||
}
|
||||
}
|
||||
sc.close();
|
||||
} else {
|
||||
f.createNewFile();
|
||||
dataOut = new PrintStream(new FileOutputStream(f, true), true);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
//serverSocket = new ServerSocket(port);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,26 @@
|
||||
package me.zacharias.chat.api;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Client {
|
||||
|
||||
Socket socket;
|
||||
|
||||
public Client(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returnes true unless if the method failes
|
||||
*/
|
||||
public boolean sendMessage(String message) {
|
||||
try(BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) {
|
||||
out.write(message+"\r\n");
|
||||
return true;
|
||||
}catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user