Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 25 additions & 3 deletions memory/embeddings/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,34 @@ def object_to_text(obj: Any) -> str:
parts = []
for key, value in obj.items():
if isinstance(value, dict):
formatted = f"{key}: " + object_to_text(value)
# Special case for position
if key == "position":
position_parts = []
if "room" in value:
position_parts.append(f"room is {value['room']}")
if "x" in value and "y" in value:
position_parts.append(f"coordinates: {value['x']}, {value['y']}")
if "facing" in value:
position_parts.append(f"facing {value['facing']}")
parts.append(" | ".join(position_parts))
else:
formatted = f"{key}: " + object_to_text(value)
parts.append(formatted)
elif isinstance(value, list):
formatted = f"{key}: " + ", ".join(str(item) for item in value)
# Special case for inventory
if key == "inventory":
if not value:
parts.append("empty inventory")
else:
# Add individual "has" statements for each item
for item in value:
parts.append(f"has {item}")
else:
formatted = f"{key}: " + ", ".join(str(item) for item in value)
parts.append(formatted)
else:
formatted = f"{key}: {value}"
parts.append(formatted)
parts.append(formatted)
return " | ".join(parts)
elif isinstance(obj, list):
if not obj:
Expand Down
3 changes: 0 additions & 3 deletions memory/search/strategies/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ def search(
if tier == "im" and fields_mask:
# Field masking with IM tier needs lower threshold
adjusted_min_score = 0.4
elif tier == "ltm":
# LTM tier has more compressed vectors, lower threshold
adjusted_min_score = 0.4

# Find similar memories
search_limit = limit * 2 # Get more results for post-filtering
Expand Down
9 changes: 5 additions & 4 deletions tests/embeddings/test_embedding_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,16 @@ def test_text_processing_for_embeddings():

# Dictionary with inventory
inventory_dict = {"inventory": ["sword", "shield", "potion"]}
assert "has sword, shield, potion" in object_to_text(inventory_dict)
text = object_to_text(inventory_dict)
assert "has sword" in text
assert "has shield" in text
assert "has potion" in text

# Dictionary with position
position_dict = {"position": {"room": "kitchen", "x": 5, "y": 10}}
position_text = object_to_text(position_dict)
assert "room is kitchen" in position_text
assert "coordinates" in position_text
assert "5" in position_text
assert "10" in position_text
assert "coordinates: 5, 10" in position_text

def test_object_to_text_realistic_agent_state():
"""Test object_to_text with realistic agent state information."""
Expand Down
20 changes: 10 additions & 10 deletions tests/search/test_match_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_search_with_example_object(self):
]

# Mock memory retrieval
self.mock_stm_store.get.side_effect = lambda id: {
self.mock_stm_store.get.side_effect = lambda agent_id, memory_id: {
"mem1": {
"id": "mem1",
"content": {"type": "task", "priority": "high", "status": "pending"},
Expand All @@ -70,7 +70,7 @@ def test_search_with_example_object(self):
"content": {"type": "note", "priority": "low"},
"metadata": {"tags": ["reference"]},
},
}.get(id)
}.get(memory_id)

# Perform search
results = self.strategy.search(
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_search_with_fields_mask(self):
]

# Mock memory retrieval
self.mock_im_store.get.side_effect = lambda id: {
self.mock_im_store.get.side_effect = lambda agent_id, memory_id: {
"mem1": {
"id": "mem1",
"content": {
Expand All @@ -125,7 +125,7 @@ def test_search_with_fields_mask(self):
},
"metadata": {},
},
}.get(id)
}.get(memory_id)

# Perform search with fields mask
results = self.strategy.search(
Expand Down Expand Up @@ -158,11 +158,11 @@ def test_search_with_min_score(self):
]

# Mock memory retrieval
self.mock_ltm_store.get.side_effect = lambda id: {
self.mock_ltm_store.get.side_effect = lambda agent_id, memory_id: {
"mem1": {"id": "mem1", "content": "matching content 1", "metadata": {}},
"mem2": {"id": "mem2", "content": "matching content 2", "metadata": {}},
"mem3": {"id": "mem3", "content": "matching content 3", "metadata": {}},
}.get(id)
}.get(memory_id)

# Perform search with min_score
results = self.strategy.search(
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_search_with_metadata_filter(self):
]

# Mock memory retrieval
self.mock_stm_store.get.side_effect = lambda id: {
self.mock_stm_store.get.side_effect = lambda agent_id, memory_id: {
"mem1": {
"id": "mem1",
"content": "content 1",
Expand All @@ -204,7 +204,7 @@ def test_search_with_metadata_filter(self):
"content": "content 2",
"metadata": {"type": "note"},
},
}.get(id)
}.get(memory_id)

# Perform search with metadata filter
metadata_filter = {"type": "task"}
Expand Down Expand Up @@ -349,11 +349,11 @@ def test_search_with_none_memory(self):
]

# Mock memory retrieval with one memory missing
self.mock_stm_store.get.side_effect = lambda id: {
self.mock_stm_store.get.side_effect = lambda agent_id, memory_id: {
"mem1": {"id": "mem1", "content": "content 1", "metadata": {}},
"mem2": None, # Missing memory
"mem3": {"id": "mem3", "content": "content 3", "metadata": {}},
}.get(id)
}.get(memory_id)

# Perform search
results = self.strategy.search(
Expand Down
Loading
Loading