mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-04-08 03:00:28 -04:00
Implement loading MemoryItems from file in JSONFileMemory (#4703)
Further changes: * remove `init` param from `get_memory()`, replace usages by `memory.clear()` * make token length calculation optional in `MemoryItem.dump()`
This commit is contained in:
committed by
GitHub
parent
6e6e7fcc9a
commit
f0a5250da5
@@ -94,7 +94,8 @@ def agent(config: Config, workspace: Workspace) -> Agent:
|
||||
ai_config.command_registry = command_registry
|
||||
|
||||
config.set_memory_backend("json_file")
|
||||
memory_json_file = get_memory(config, init=True)
|
||||
memory_json_file = get_memory(config)
|
||||
memory_json_file.clear()
|
||||
|
||||
system_prompt = ai_config.construct_full_prompt()
|
||||
|
||||
|
||||
@@ -28,7 +28,9 @@ def memory_json_file(agent_test_config: Config):
|
||||
was_memory_backend = agent_test_config.memory_backend
|
||||
|
||||
agent_test_config.set_memory_backend("json_file")
|
||||
yield get_memory(agent_test_config, init=True)
|
||||
memory = get_memory(agent_test_config)
|
||||
memory.clear()
|
||||
yield memory
|
||||
|
||||
agent_test_config.set_memory_backend(was_memory_backend)
|
||||
|
||||
|
||||
@@ -34,7 +34,9 @@ def test_json_memory_init_with_backing_empty_file(config: Config, workspace: Wor
|
||||
assert index_file.read_text() == "[]"
|
||||
|
||||
|
||||
def test_json_memory_init_with_backing_file(config: Config, workspace: Workspace):
|
||||
def test_json_memory_init_with_backing_invalid_file(
|
||||
config: Config, workspace: Workspace
|
||||
):
|
||||
index_file = workspace.root / f"{config.memory_index}.json"
|
||||
index_file.touch()
|
||||
|
||||
@@ -78,6 +80,24 @@ def test_json_memory_get(config: Config, memory_item: MemoryItem, mock_get_embed
|
||||
assert retrieved.memory_item == memory_item
|
||||
|
||||
|
||||
def test_json_memory_load_index(config: Config, memory_item: MemoryItem):
|
||||
index = JSONFileMemory(config)
|
||||
index.add(memory_item)
|
||||
|
||||
try:
|
||||
assert index.file_path.exists(), "index was not saved to file"
|
||||
assert len(index) == 1, f"index constains {len(index)} items instead of 1"
|
||||
assert index.memories[0] == memory_item, "item in index != added mock item"
|
||||
except AssertionError as e:
|
||||
raise ValueError(f"Setting up for load_index test failed: {e}")
|
||||
|
||||
index.memories = []
|
||||
index.load_index()
|
||||
|
||||
assert len(index) == 1
|
||||
assert index.memories[0] == memory_item
|
||||
|
||||
|
||||
@pytest.mark.vcr
|
||||
@requires_api_key("OPENAI_API_KEY")
|
||||
def test_json_memory_get_relevant(config: Config, patched_api_requestor: None) -> None:
|
||||
|
||||
Reference in New Issue
Block a user