Mem0 1.0.0 (#3545)

This commit is contained in:
Parshva Daftari
2025-10-16 15:50:20 +05:30
committed by GitHub
parent 8f5151c344
commit 394203d1b5
77 changed files with 3445 additions and 990 deletions
+6 -4
View File
@@ -43,7 +43,7 @@ class TestAddToVectorStoreErrors:
def test_empty_llm_response_fact_extraction(self, mocker, mock_memory, caplog):
"""Test empty response from LLM during fact extraction"""
# Setup
mock_memory.llm.generate_response.return_value = ""
mock_memory.llm.generate_response.return_value = "invalid json" # This will trigger a JSON decode error
mock_capture_event = mocker.MagicMock()
mocker.patch("mem0.memory.main.capture_event", mock_capture_event)
@@ -56,7 +56,8 @@ class TestAddToVectorStoreErrors:
# Verify
assert mock_memory.llm.generate_response.call_count == 1
assert result == [] # Should return empty list when no memories processed
assert "Error in new_retrieved_facts" in caplog.text
# Check for error message in any of the log records
assert any("Error in new_retrieved_facts" in record.msg for record in caplog.records), "Expected error message not found in logs"
assert mock_capture_event.call_count == 1
def test_empty_llm_response_memory_actions(self, mock_memory, caplog):
@@ -96,7 +97,7 @@ class TestAsyncAddToVectorStoreErrors:
async def test_async_empty_llm_response_fact_extraction(self, mock_async_memory, caplog, mocker):
"""Test empty response in AsyncMemory._add_to_vector_store"""
mocker.patch("mem0.utils.factory.EmbedderFactory.create", return_value=MagicMock())
mock_async_memory.llm.generate_response.return_value = ""
mock_async_memory.llm.generate_response.return_value = "invalid json" # This will trigger a JSON decode error
mock_capture_event = mocker.MagicMock()
mocker.patch("mem0.memory.main.capture_event", mock_capture_event)
@@ -106,7 +107,8 @@ class TestAsyncAddToVectorStoreErrors:
)
assert mock_async_memory.llm.generate_response.call_count == 1
assert result == []
assert "Error in new_retrieved_facts" in caplog.text
# Check for error message in any of the log records
assert any("Error in new_retrieved_facts" in record.msg for record in caplog.records), "Expected error message not found in logs"
assert mock_capture_event.call_count == 1
@pytest.mark.asyncio
+15 -11
View File
@@ -62,7 +62,7 @@ def test_search_vectors_with_filters(chromadb_instance, mock_chromadb_client):
results = chromadb_instance.search(query="", vectors=vectors, limit=2, filters=filters)
# Verify that _generate_where_clause was called with the filters
expected_where = {"$and": [{"user_id": "alice"}, {"agent_id": "agent1"}, {"run_id": "run1"}]}
expected_where = {"$and": [{"user_id": {"$eq": "alice"}}, {"agent_id": {"$eq": "agent1"}}, {"run_id": {"$eq": "run1"}}]}
chromadb_instance.collection.query.assert_called_once_with(
query_embeddings=vectors, where=expected_where, n_results=2
)
@@ -87,9 +87,10 @@ def test_search_vectors_with_single_filter(chromadb_instance, mock_chromadb_clie
filters = {"user_id": "alice"}
results = chromadb_instance.search(query="", vectors=vectors, limit=2, filters=filters)
# Verify that single filter is passed as-is (no $and wrapper)
# Verify that single filter is passed with $eq operator
expected_where = {"user_id": {"$eq": "alice"}}
chromadb_instance.collection.query.assert_called_once_with(
query_embeddings=vectors, where=filters, n_results=2
query_embeddings=vectors, where=expected_where, n_results=2
)
assert len(results) == 1
@@ -182,7 +183,7 @@ def test_list_vectors_with_filters(chromadb_instance):
results = chromadb_instance.list(filters=filters, limit=2)
# Verify that _generate_where_clause was called with the filters
expected_where = {"$and": [{"user_id": "alice"}, {"agent_id": "agent1"}, {"run_id": "run1"}]}
expected_where = {"$and": [{"user_id": {"$eq": "alice"}}, {"agent_id": {"$eq": "agent1"}}, {"run_id": {"$eq": "run1"}}]}
chromadb_instance.collection.get.assert_called_once_with(where=expected_where, limit=2)
assert len(results[0]) == 1
@@ -203,8 +204,9 @@ def test_list_vectors_with_single_filter(chromadb_instance):
filters = {"user_id": "alice"}
results = chromadb_instance.list(filters=filters, limit=2)
# Verify that single filter is passed as-is (no $and wrapper)
chromadb_instance.collection.get.assert_called_once_with(where=filters, limit=2)
# Verify that single filter is passed with $eq operator
expected_where = {"user_id": {"$eq": "alice"}}
chromadb_instance.collection.get.assert_called_once_with(where=expected_where, limit=2)
assert len(results[0]) == 1
assert results[0][0].payload["user_id"] == "alice"
@@ -215,7 +217,8 @@ def test_generate_where_clause_multiple_filters():
filters = {"user_id": "alice", "agent_id": "agent1", "run_id": "run1"}
result = ChromaDB._generate_where_clause(filters)
expected = {"$and": [{"user_id": "alice"}, {"agent_id": "agent1"}, {"run_id": "run1"}]}
# ChromaDB accepts filters in {"$and": [{"field": {"$eq": "value"}}, ...]} format
expected = {"$and": [{"user_id": {"$eq": "alice"}}, {"agent_id": {"$eq": "agent1"}}, {"run_id": {"$eq": "run1"}}]}
assert result == expected
@@ -224,8 +227,9 @@ def test_generate_where_clause_single_filter():
filters = {"user_id": "alice"}
result = ChromaDB._generate_where_clause(filters)
# Single filter should be returned as-is
assert result == filters
# ChromaDB accepts single filters in {"field": {"$eq": "value"}} format
expected = {"user_id": {"$eq": "alice"}}
assert result == expected
def test_generate_where_clause_no_filters():
@@ -242,6 +246,6 @@ def test_generate_where_clause_non_string_values():
filters = {"user_id": "alice", "count": 5, "active": True}
result = ChromaDB._generate_where_clause(filters)
# Only string values should be included in $and array
expected = {"$and": [{"user_id": "alice"}]}
# ChromaDB accepts non-string values in filters
expected = {"$and": [{"user_id": {"$eq": "alice"}}, {"count": {"$eq": 5}}, {"active": {"$eq": True}}]}
assert result == expected
+2 -3
View File
@@ -4,7 +4,6 @@ import unittest
from unittest.mock import MagicMock, patch
import dotenv
import pytest
try:
from opensearchpy import AWSV4SignerAuth, OpenSearch
@@ -102,10 +101,12 @@ class TestOpenSearchDB(unittest.TestCase):
self.client_mock.indices.create = MagicMock()
self.client_mock.indices.delete = MagicMock()
self.client_mock.indices.get_alias = MagicMock()
self.client_mock.indices.refresh = MagicMock()
self.client_mock.get = MagicMock()
self.client_mock.update = MagicMock()
self.client_mock.delete = MagicMock()
self.client_mock.search = MagicMock()
self.client_mock.index = MagicMock(return_value={"_id": "doc1"})
patcher = patch("mem0.vector_stores.opensearch.OpenSearch", return_value=self.client_mock)
self.mock_os = patcher.start()
@@ -148,7 +149,6 @@ class TestOpenSearchDB(unittest.TestCase):
self.os_db.create_index()
self.client_mock.indices.create.assert_not_called()
@pytest.mark.skip(reason="This test is not working as expected")
def test_insert(self):
vectors = [[0.1] * 1536, [0.2] * 1536]
payloads = [{"key1": "value1"}, {"key2": "value2"}]
@@ -183,7 +183,6 @@ class TestOpenSearchDB(unittest.TestCase):
self.assertEqual(results[1].id, "id2")
self.assertEqual(results[1].payload, payloads[1])
@pytest.mark.skip(reason="This test is not working as expected")
def test_get(self):
mock_response = {"hits": {"hits": [{"_id": "doc1", "_source": {"id": "id1", "payload": {"key1": "value1"}}}]}}
self.client_mock.search.return_value = mock_response