Skip to content
Open
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
4 changes: 4 additions & 0 deletions mem0/memory/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,13 @@ def _add_to_vector_store(self, messages, metadata, filters, infer, prompt=None):
system_prompt += AGENT_CONTEXT_SUFFIX

custom_instr = prompt or self.custom_instructions
observation_timestamp = metadata.get("timestamp") or metadata.get("created_at")

user_prompt = generate_additive_extraction_prompt(
existing_memories=existing_memories,
new_messages=parsed_messages,
last_k_messages=last_messages,
timestamp=observation_timestamp,
custom_instructions=custom_instr,
)

Expand Down Expand Up @@ -2143,11 +2145,13 @@ async def _add_to_vector_store(
system_prompt += AGENT_CONTEXT_SUFFIX

custom_instr = prompt or self.custom_instructions
observation_timestamp = metadata.get("timestamp") or metadata.get("created_at")

user_prompt = generate_additive_extraction_prompt(
existing_memories=existing_memories,
new_messages=parsed_messages,
last_k_messages=last_messages,
timestamp=observation_timestamp,
custom_instructions=custom_instr,
)

Expand Down
41 changes: 40 additions & 1 deletion tests/memory/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,32 @@ def test_falls_back_to_custom_instructions_when_no_prompt(self, mock_memory):
user_prompt = mock_memory.llm.generate_response.call_args[1]["messages"][1]["content"]
assert "config-level instructions" in user_prompt

def test_metadata_timestamp_sets_observation_date(self, mock_memory):
timestamp = "2023-05-24T10:00:00+00:00"

mock_memory._add_to_vector_store(
messages=[{"role": "user", "content": "I went to Paris last week"}],
metadata={"timestamp": timestamp},
filters={},
infer=True,
)

user_prompt = mock_memory.llm.generate_response.call_args[1]["messages"][1]["content"]
assert f"## Observation Date\n{timestamp}" in user_prompt

def test_created_at_sets_observation_date_when_timestamp_missing(self, mock_memory):
created_at = "2023-05-24T10:00:00+00:00"

mock_memory._add_to_vector_store(
messages=[{"role": "user", "content": "I went to Paris last week"}],
metadata={"created_at": created_at},
filters={},
infer=True,
)

user_prompt = mock_memory.llm.generate_response.call_args[1]["messages"][1]["content"]
assert f"## Observation Date\n{created_at}" in user_prompt


class TestAsyncUpdate:
@pytest.fixture
Expand Down Expand Up @@ -216,6 +242,20 @@ async def test_async_empty_llm_response_memory_actions(self, mock_async_memory,
assert result == []
assert mock_async_memory.llm.generate_response.call_count == 1

async def test_async_metadata_timestamp_sets_observation_date(self, mock_async_memory):
timestamp = "2023-05-24T10:00:00+00:00"
mock_async_memory.llm.generate_response.return_value = '{"memory": []}'

await mock_async_memory._add_to_vector_store(
messages=[{"role": "user", "content": "I went to Paris last week"}],
metadata={"timestamp": timestamp},
effective_filters={},
infer=True,
)

user_prompt = mock_async_memory.llm.generate_response.call_args[1]["messages"][1]["content"]
assert f"## Observation Date\n{timestamp}" in user_prompt


def _build_memory_instance(mocker, memory_cls):
_setup_mocks(mocker)
Expand Down Expand Up @@ -662,4 +702,3 @@ async def test_async_update_preserves_actor_id_when_different_actor_updates(mock
stored = memory.vector_store.update.call_args.kwargs["payload"]
assert stored["actor_id"] == "Alice"