Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Add] Error handling for update method in OSS & platform code. #1939

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mem0/client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions mem0/memory/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Dev-Khant marked this conversation as resolved.
Show resolved Hide resolved

self._update_memory(memory_id, data, existing_embeddings)
return {"message": "Memory updated successfully!"}

def delete(self, memory_id):
Expand Down Expand Up @@ -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 {}
Expand Down
6 changes: 5 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!"


Expand Down
Loading