Make local json cache when it doesn't exist

This commit is contained in:
James Collins
2023-04-23 15:43:04 -07:00
parent d6ef9d1b5d
commit 680c7b5aaa

View File

@@ -1,7 +1,7 @@
from __future__ import annotations
import dataclasses
import os
from pathlib import Path
from typing import Any, List
import numpy as np
@@ -38,26 +38,22 @@ class LocalCache(MemoryProviderSingleton):
Returns:
None
"""
self.filename = f"{cfg.memory_index}.json"
if os.path.exists(self.filename):
try:
with open(self.filename, "w+b") as f:
file_content = f.read()
if not file_content.strip():
file_content = b"{}"
f.write(file_content)
workspace_path = Path(cfg.workspace_path)
self.filename = workspace_path / f"{cfg.memory_index}.json"
self.filename.touch(exist_ok=True)
loaded = orjson.loads(file_content)
self.data = CacheContent(**loaded)
except orjson.JSONDecodeError:
print(f"Error: The file '{self.filename}' is not in JSON format.")
self.data = CacheContent()
else:
print(
f"Warning: The file '{self.filename}' does not exist. "
"Local memory would not be saved to a file."
)
self.data = CacheContent()
try:
with self.filename.open("w+b") as f:
file_content = f.read()
if not file_content.strip():
file_content = b"{}"
f.write(file_content)
loaded = orjson.loads(file_content)
except orjson.JSONDecodeError:
print(f"Error: The file '{self.filename}' is not in JSON format.")
loaded = {}
self.data = CacheContent(**loaded)
def add(self, text: str):
"""