From b7e0fd3dc804efee883226c5521b4d2360d7e4ac Mon Sep 17 00:00:00 2001 From: parshvadaftari Date: Fri, 4 Oct 2024 01:17:31 +0530 Subject: [PATCH 1/4] [Add] error handling for update method in OSS & platform code. --- mem0/client/main.py | 1 + mem0/memory/main.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/mem0/client/main.py b/mem0/client/main.py index 11bfa0e8ad..2e79eef2ae 100644 --- a/mem0/client/main.py +++ b/mem0/client/main.py @@ -197,6 +197,7 @@ def update(self, memory_id: str, data: str) -> Dict[str, Any]: """ capture_client_event("client.update", self) response = self.client.put(f"/v1/memories/{memory_id}/", json={"text": data}) + response.raise_for_status() return response.json() @api_error_handler diff --git a/mem0/memory/main.py b/mem0/memory/main.py index 1aecb467f1..f6a9423cb3 100644 --- a/mem0/memory/main.py +++ b/mem0/memory/main.py @@ -453,7 +453,10 @@ def update(self, memory_id, data): dict: Updated memory. """ capture_event("mem0.update", self, {"memory_id": memory_id}) - self._update_memory(memory_id, data) + + existing_embeddings = {data: self.embedding_model.embed(data)} + + self._update_memory(memory_id, data, existing_embeddings) return {"message": "Memory updated successfully!"} def delete(self, memory_id): @@ -536,7 +539,11 @@ def _create_memory(self, data, existing_embeddings, metadata=None): def _update_memory(self, memory_id, data, existing_embeddings, metadata=None): logger.info(f"Updating memory with {data=}") - existing_memory = self.vector_store.get(vector_id=memory_id) + + try: + existing_memory = self.vector_store.get(vector_id=memory_id) + except Exception as e: + raise ValueError(f"Error getting memory with ID {memory_id}. Please provide a valid 'memory_id'") prev_value = existing_memory.payload.get("data") new_metadata = metadata or {} From 0afa0913c917897e6976f6caa06bec67de2d198f Mon Sep 17 00:00:00 2001 From: parshvadaftari Date: Fri, 4 Oct 2024 01:33:58 +0530 Subject: [PATCH 2/4] Update test_main.py --- tests/test_main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_main.py b/tests/test_main.py index fd667b4f94..ed348ec3d9 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -127,11 +127,15 @@ def test_search(memory_instance, version, enable_graph): def test_update(memory_instance): + memory_instance.embedding_model = Mock() + memory_instance.embedding_model.embed = Mock(return_value=[0.1, 0.2, 0.3]) + memory_instance._update_memory = Mock() result = memory_instance.update("test_id", "Updated memory") - memory_instance._update_memory.assert_called_once_with("test_id", "Updated memory") + memory_instance._update_memory.assert_called_once_with("test_id", "Updated memory", {"Updated memory": [0.1, 0.2, 0.3]}) + assert result["message"] == "Memory updated successfully!" From 6ad08414e93150c280279e2330e6afbffee9f7c9 Mon Sep 17 00:00:00 2001 From: parshvadaftari Date: Tue, 8 Oct 2024 12:20:46 +0530 Subject: [PATCH 3/4] Properly enclose key data in quotes. --- mem0/memory/main.py | 2 +- tests/test_main.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mem0/memory/main.py b/mem0/memory/main.py index f6a9423cb3..dcfa257e79 100644 --- a/mem0/memory/main.py +++ b/mem0/memory/main.py @@ -454,7 +454,7 @@ def update(self, memory_id, data): """ capture_event("mem0.update", self, {"memory_id": memory_id}) - existing_embeddings = {data: self.embedding_model.embed(data)} + existing_embeddings = {"data": self.embedding_model.embed(data)} self._update_memory(memory_id, data, existing_embeddings) return {"message": "Memory updated successfully!"} diff --git a/tests/test_main.py b/tests/test_main.py index ed348ec3d9..fa0014d6fc 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -134,7 +134,7 @@ def test_update(memory_instance): result = memory_instance.update("test_id", "Updated memory") - memory_instance._update_memory.assert_called_once_with("test_id", "Updated memory", {"Updated memory": [0.1, 0.2, 0.3]}) + memory_instance._update_memory.assert_called_once_with("test_id", "Updated memory", {"data": [0.1, 0.2, 0.3]}) assert result["message"] == "Memory updated successfully!" From f0c909ceaf81c0d9c0ff6242c9ece43a5f7c3b33 Mon Sep 17 00:00:00 2001 From: parshvadaftari Date: Tue, 8 Oct 2024 12:42:51 +0530 Subject: [PATCH 4/4] Reverting quotes. --- mem0/memory/main.py | 2 +- tests/test_main.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mem0/memory/main.py b/mem0/memory/main.py index dcfa257e79..f6a9423cb3 100644 --- a/mem0/memory/main.py +++ b/mem0/memory/main.py @@ -454,7 +454,7 @@ def update(self, memory_id, data): """ capture_event("mem0.update", self, {"memory_id": memory_id}) - existing_embeddings = {"data": self.embedding_model.embed(data)} + existing_embeddings = {data: self.embedding_model.embed(data)} self._update_memory(memory_id, data, existing_embeddings) return {"message": "Memory updated successfully!"} diff --git a/tests/test_main.py b/tests/test_main.py index fa0014d6fc..ed348ec3d9 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -134,7 +134,7 @@ def test_update(memory_instance): result = memory_instance.update("test_id", "Updated memory") - memory_instance._update_memory.assert_called_once_with("test_id", "Updated memory", {"data": [0.1, 0.2, 0.3]}) + memory_instance._update_memory.assert_called_once_with("test_id", "Updated memory", {"Updated memory": [0.1, 0.2, 0.3]}) assert result["message"] == "Memory updated successfully!"