Skip to content
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
24 changes: 12 additions & 12 deletions src/memos/api/handlers/chat_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,8 @@ def generate_chat_response() -> Generator[str, None, None]:

yield f"data: {json.dumps({'type': 'status', 'data': '1'})}\n\n"

# Extract memories from search results (first search)
memories_list = []
if search_response.data and search_response.data.get("text_mem"):
text_mem_results = search_response.data["text_mem"]
if text_mem_results and text_mem_results[0].get("memories"):
memories_list = text_mem_results[0]["memories"]
# Extract memories from every searched cube (first search)
memories_list = self._memories_from_cube_results(search_response.data)

# Filter memories by threshold
filtered_memories = self._filter_memories_by_threshold(memories_list)[:5]
Expand Down Expand Up @@ -577,12 +573,8 @@ def generate_chat_response() -> Generator[str, None, None]:
project_id=chat_req.project_id,
)

# Extract memories from search results (second search)
memories_list = []
if search_response.data and search_response.data.get("text_mem"):
text_mem_results = search_response.data["text_mem"]
if text_mem_results and text_mem_results[0].get("memories"):
memories_list = text_mem_results[0]["memories"]
# Extract memories from every searched cube (second search)
memories_list = self._memories_from_cube_results(search_response.data)

# Filter memories by threshold, min_num is the min number of memories for playground
second_filtered_memories = self._filter_memories_by_threshold(
Expand Down Expand Up @@ -1161,6 +1153,14 @@ def _format_mem_block(

return "\n".join(lines_o), "\n".join(lines_p)

@staticmethod
def _memories_from_cube_results(search_data: dict[str, Any] | None) -> list[Any]:
"""Collect textual memories from every cube in a search response."""
memories_list: list[Any] = []
for cube_result in (search_data or {}).get("text_mem") or []:
memories_list.extend(cube_result.get("memories") or [])
return memories_list

def _filter_memories_by_threshold(
self,
memories: list,
Expand Down
24 changes: 24 additions & 0 deletions tests/api/test_playground_cube_recall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Playground recall must keep memories from every searched cube."""

from memos.api.handlers.chat_handler import ChatHandler


def test_playground_recall_keeps_memories_from_later_cubes():
search_data = {
"text_mem": [
{"cube_id": "knowledge-base", "memories": []},
{
"cube_id": "personal",
"memories": [{"id": "mem-1", "memory": "use event_id as the idempotency key"}],
},
]
}

memories = ChatHandler._memories_from_cube_results(search_data)

assert [item["id"] for item in memories] == ["mem-1"]


def test_playground_recall_returns_empty_when_search_data_missing():
assert ChatHandler._memories_from_cube_results(None) == []
assert ChatHandler._memories_from_cube_results({}) == []
Loading