From ce595d89bd01162b1ce27830ceb5817f55669ef0 Mon Sep 17 00:00:00 2001 From: Frank <29271979+FrankyHollywood@users.noreply.github.com> Date: Tue, 13 Aug 2024 16:59:50 +0200 Subject: [PATCH] Small refactor of conversation cache --- .../saturn/services/llm/AiSearchApp.java | 89 ++------------- .../services/llm/ConversationCache.java | 106 ++++++++++++++++++ .../{responseUtil.java => ResponseUtil.java} | 2 +- 3 files changed, 115 insertions(+), 82 deletions(-) create mode 100644 projects/saturn/src/main/java/io/fairspace/saturn/services/llm/ConversationCache.java rename projects/saturn/src/main/java/io/fairspace/saturn/services/llm/{responseUtil.java => ResponseUtil.java} (99%) diff --git a/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/AiSearchApp.java b/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/AiSearchApp.java index 073fea880..3e2010d89 100644 --- a/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/AiSearchApp.java +++ b/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/AiSearchApp.java @@ -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; @@ -13,7 +9,6 @@ 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; @@ -21,7 +16,6 @@ @Log4j2 public class AiSearchApp extends BaseApp { - final String CACHE_DIR = CONFIG.llmConversationCachePath; public AiSearchApp(String basePath) { super(basePath); @@ -39,49 +33,9 @@ protected void initApp() { }); get("/allconversations", "application/json", (req, res) -> { - if (!Files.exists(Paths.get(CACHE_DIR + getUserKey()))) { - return new ArrayList(); - } - - 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(); - 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); } @@ -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); } @@ -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); } }); @@ -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); diff --git a/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/ConversationCache.java b/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/ConversationCache.java new file mode 100644 index 000000000..becd48d7c --- /dev/null +++ b/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/ConversationCache.java @@ -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 getAllConversations(String userKey) throws IOException { + if (!Files.exists(Paths.get(CACHE_DIR + userKey))) { + return new ArrayList(); + } + + 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(); + 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()); + } + } +} diff --git a/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/responseUtil.java b/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/ResponseUtil.java similarity index 99% rename from projects/saturn/src/main/java/io/fairspace/saturn/services/llm/responseUtil.java rename to projects/saturn/src/main/java/io/fairspace/saturn/services/llm/ResponseUtil.java index 6bc2191c6..0800573ed 100644 --- a/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/responseUtil.java +++ b/projects/saturn/src/main/java/io/fairspace/saturn/services/llm/ResponseUtil.java @@ -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)); }