Started work on API and remembrance fuction

This commit is contained in:
2025-02-21 13:31:13 +01:00
parent 9daae9211f
commit da2cb69b1b
11 changed files with 331 additions and 42 deletions

View File

@@ -92,7 +92,7 @@ public class Core {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH-mm-ss");
File messagesFile = new File("./messages/"+now.format(formatter)+".txt");
File messagesFile = new File("./messages/"+now.format(formatter)+".json");
BufferedWriter messagesWriter = new BufferedWriter(new FileWriter(messagesFile));
@@ -105,6 +105,15 @@ public class Core {
messagesWriter.write(messages.toString());
messagesWriter.close();
File f = new File("./cache/messages.json");
messagesWriter = new BufferedWriter(new FileWriter(messagesFile));
for(OllamaMessage message : ollamaObject.getMessages()) {
messages.put(new JSONObject(message.toString()));
}
messagesWriter.write(messages.toString());
messagesWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -184,9 +193,11 @@ public class Core {
//System.out.println("Responce: "+responce);
if(responce != null) {
writeLog("Raw responce: "+responce.toString());
if(responce.getJSONObject("message").has("tool_calls"))
JSONObject message = responce.getJSONObject("message");
if(message.has("tool_calls"))
{
JSONArray calls = responce.getJSONObject("message").getJSONArray("tool_calls");
ollamaObject.addMessage(new OllamaMessageToolCall(OllamaMessageRole.fromRole(message.optString("role")), message.getString("content"), message.getJSONArray("tool_calls")));
JSONArray calls = message.getJSONArray("tool_calls");
for(Object call : calls)
{
if(call instanceof JSONObject jsonObject)

View File

@@ -0,0 +1,59 @@
package me.zacharias.chat.core;
public class LaunchOptions {
private static LaunchOptions instance = new LaunchOptions();
public static LaunchOptions getInstance() {
return instance;
}
private boolean loadOld;
private boolean autoAccept;
private boolean serverMode;
private int port = 39075;
private String redirectOutput;
public static void setInstance(LaunchOptions instance) {
LaunchOptions.instance = instance;
}
public boolean isLoadOld() {
return loadOld;
}
public void setLoadOld(boolean loadOld) {
this.loadOld = loadOld;
}
public boolean isAutoAccept() {
return autoAccept;
}
public void setAutoAccept(boolean autoAccept) {
this.autoAccept = autoAccept;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getRedirectOutput() {
return redirectOutput;
}
public void setRedirectOutput(String redirectOutput) {
this.redirectOutput = redirectOutput;
}
public boolean isServerMode() {
return serverMode;
}
public void setServerMode(boolean serverMode) {
this.serverMode = serverMode;
}
}

View File

@@ -15,4 +15,12 @@ public enum OllamaMessageRole {
public String getRole() {
return role;
}
public static OllamaMessageRole fromRole(String role) {
for(OllamaMessageRole roleRole : values()) {
if(roleRole.role.equals(role))
return roleRole;
}
throw new IllegalArgumentException("Invalid role: " + role);
}
}

View File

@@ -0,0 +1,25 @@
package me.zacharias.chat.ollama;
import org.json.JSONArray;
import org.json.JSONObject;
public class OllamaMessageToolCall extends OllamaMessage{
private JSONArray tool_calls;
public OllamaMessageToolCall(OllamaMessageRole role, String content, JSONArray tool_calls) {
super(role, content);
this.tool_calls = tool_calls;
}
@Override
public String toString() {
JSONObject json = new JSONObject();
json.put("role", role);
json.put("content", content);
json.put("tool_calls", tool_calls);
return json.toString();
}
}

View File

@@ -1,8 +1,12 @@
package me.zacharias.chat.ollama;
import me.zacharias.chat.core.LaunchOptions;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -25,6 +29,35 @@ public class OllamaObject {
this.options = options;
this.stream = stream;
this.keep_alive = keep_alive;
LaunchOptions launchOptions = new LaunchOptions();
if(launchOptions.isLoadOld()) {
System.out.println("Loading old data...");
File f = new File("./cache/messages.json");
if(f.exists()) {
try {
BufferedReader br = new BufferedReader(new FileReader(f));
StringBuilder data = new StringBuilder();
String buffer = null;
while ((buffer = br.readLine()) != null) {
data.append(buffer).append("\n");
}
JSONArray jsonArray = new JSONArray(data.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
OllamaMessage message;
if (!obj.has("tool_calls")) {
message = new OllamaMessage(obj.getEnum(OllamaMessageRole.class, "role"), obj.getString("content"));
} else {
message = new OllamaMessageToolCall(obj.getEnum(OllamaMessageRole.class, "role"), obj.getString("content"), obj.getJSONArray("tool_calls"));
}
messages.add(message);
}
}catch (Exception e) {
System.out.println("Error loading old data");
e.printStackTrace();
}
}
}
}
public String getModel() {