Skip to content

Commit

Permalink
Small refactor of conversation cache
Browse files Browse the repository at this point in the history
  • Loading branch information
frankyhollywood committed Aug 13, 2024
1 parent cfacd57 commit b3f349e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package io.fairspace.saturn.services.llm;

import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.io.IOException;

import lombok.extern.log4j.*;
import org.json.JSONObject;
Expand All @@ -13,15 +9,13 @@
import io.fairspace.saturn.services.BaseApp;

import static io.fairspace.saturn.auth.RequestContext.getAccessToken;
import static io.fairspace.saturn.config.ConfigLoader.CONFIG;

import static org.eclipse.jetty.http.MimeTypes.Type.APPLICATION_JSON;
import static spark.Spark.get;
import static spark.Spark.post;

@Log4j2
public class AiSearchApp extends BaseApp {
final String CACHE_DIR = CONFIG.llmConversationCachePath;

public AiSearchApp(String basePath) {
super(basePath);
Expand All @@ -39,49 +33,9 @@ protected void initApp() {
});

get("/allconversations", "application/json", (req, res) -> {
if (!Files.exists(Paths.get(CACHE_DIR + getUserKey()))) {
return new ArrayList<JSONObject>();
}

try (var conversations = Files.list(Paths.get(CACHE_DIR + getUserKey()))) {
var content = conversations
.map(path -> {
try {
return new String(Files.readAllBytes(path));
} catch (Exception e) {
return "{}";
}
})
.collect(Collectors.toList());

var result = new ArrayList<JSONObject>();
for (var c : content) {
try {
var obj = new JSONObject(c);
var conversation = new JSONObject();
conversation.put("id", obj.getJSONObject("conversation").getString("conversationId"));
conversation.put(
"topic",
obj.getJSONObject("conversation")
.getJSONArray("messages")
.getJSONObject(0)
.getJSONObject("userInput")
.getString("input"));
conversation.put(
"start",
new LlmConversation()
.getStartTime(obj.getJSONObject("conversation")
.getString("startTime")));

result.add(conversation);
} catch (Exception e) {
System.out.println(
"Error extracting conversation id, input message, and start time: " + e.getMessage());
}
}

res.type(APPLICATION_JSON.asString());
return result;
res.type(APPLICATION_JSON.asString());
try {
return new ConversationCache().getAllConversations(getUserKey());
} catch (Exception e) {
return handleError(req, e);
}
Expand Down Expand Up @@ -113,24 +67,10 @@ protected void initApp() {
}

var conversationId = req.params(":id");
var filename = CACHE_DIR + getUserKey() + "/" + conversationId + ".json";

res.type(APPLICATION_JSON.asString());

if (Files.exists(Paths.get(filename))) {
try {
var content = new String(Files.readAllBytes(Paths.get(filename)));
return content;
} catch (Exception e) {
System.out.println("Error reading conversation history file: " + e.getMessage());
var content = new String();
return content;
}
} else {
System.out.println("Conversation history file " + filename + " does not exist.");
return "{}";
}

return new ConversationCache().GetConversation(conversationId, getUserKey());
} catch (Exception e) {
return handleError(req, e);
}
Expand All @@ -150,20 +90,12 @@ protected void initApp() {
var conversationId = body.getString("conversationId");

var result = new LlmConversation().continueChat(conversationId, query);
var filepath = CACHE_DIR + getUserKey();
var file = new java.io.File(filepath + "/" + conversationId + ".json");

Files.createDirectories(Paths.get(filepath));

try (FileWriter writer = new FileWriter(file)) {
writer.write(result.toString());
} catch (Exception e) {
System.out.println("Error writing conversation history file: " + e.getMessage());
}
new ConversationCache().saveConversation(conversationId, getUserKey(), result);

res.type(APPLICATION_JSON.asString());
return result;
} catch (Exception e) {
} catch (IOException e) {
return handleError(req, e);
}
});
Expand All @@ -177,13 +109,8 @@ protected void initApp() {
}

var conversationId = req.params(":id");
var filename = CACHE_DIR + getUserKey() + "/" + conversationId + ".json";

try {
Files.deleteIfExists(Paths.get(filename));
} catch (Exception e) {
System.out.println("Error deleting conversation history file: " + e.getMessage());
}
new ConversationCache().deleteChat(conversationId, getUserKey());

var result = new LlmConversation().deleteChat(conversationId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package io.fairspace.saturn.services.llm;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.stream.Collectors;

import lombok.extern.log4j.Log4j2;
import org.json.JSONObject;

import static io.fairspace.saturn.config.ConfigLoader.CONFIG;

@Log4j2
public class ConversationCache {
final String CACHE_DIR = CONFIG.llmConversationCachePath;

public ArrayList<JSONObject> getAllConversations(String userKey) throws IOException {
if (!Files.exists(Paths.get(CACHE_DIR + userKey))) {
return new ArrayList<JSONObject>();
}

try (var conversations = Files.list(Paths.get(CACHE_DIR + userKey))) {
var content = conversations
.map(path -> {
try {
return new String(Files.readAllBytes(path));
} catch (Exception e) {
return "{}";
}
})
.collect(Collectors.toList());

var result = new ArrayList<JSONObject>();
for (var c : content) {
try {
var obj = new JSONObject(c);
var conversation = new JSONObject();
conversation.put("id", obj.getJSONObject("conversation").getString("conversationId"));
conversation.put(
"topic",
obj.getJSONObject("conversation")
.getJSONArray("messages")
.getJSONObject(0)
.getJSONObject("userInput")
.getString("input"));
conversation.put(
"start",
new LlmConversation()
.getStartTime(
obj.getJSONObject("conversation").getString("startTime")));

result.add(conversation);
} catch (Exception e) {
log.error("Error extracting conversation id, input message, and start time: " + e.getMessage());
throw e;
}
}

return result;
} catch (IOException e) {
log.error(e);
throw e;
}
}

public String GetConversation(String conversationId, String userKey) {
var filename = CACHE_DIR + userKey + "/" + conversationId + ".json";
if (Files.exists(Paths.get(filename))) {
try {
var content = new String(Files.readAllBytes(Paths.get(filename)));
return content;
} catch (Exception e) {
log.error("Error reading conversation history file: " + e.getMessage(), e);
return "{}";
}
} else {
log.error("Conversation history file " + filename + " does not exist.");
return "{}";
}
}

public void saveConversation(String conversationId, String userKey, String result) throws IOException {
var filepath = CACHE_DIR + userKey;
var file = new java.io.File(filepath + "/" + conversationId + ".json");

Files.createDirectories(Paths.get(filepath));

try (FileWriter writer = new FileWriter(file)) {
writer.write(result.toString());
} catch (Exception e) {
log.error("Error writing conversation history file: " + e.getMessage());
}
}

public void deleteChat(String conversationId, String userKey) {
var filename = CACHE_DIR + userKey + "/" + conversationId + ".json";

try {
Files.deleteIfExists(Paths.get(filename));
} catch (Exception e) {
log.error("Error deleting conversation history file: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.json.JSONArray;
import org.json.JSONObject;

public class responseUtil {
public class ResponseUtil {
public static String cleanupJson(String input) {
return cleanupJson(new JSONObject(input));
}
Expand Down

0 comments on commit b3f349e

Please sign in to comment.