Skip to content

fix: keep query embedding for fine search#1478

Open
fancyboi999 wants to merge 1 commit intoMemTensor:mainfrom
fancyboi999:fancy/fix-1448-product-search-memory
Open

fix: keep query embedding for fine search#1478
fancyboi999 wants to merge 1 commit intoMemTensor:mainfrom
fancyboi999:fancy/fix-1448-product-search-memory

Conversation

@fancyboi999
Copy link
Copy Markdown
Contributor

What changed

Fine search now embeds the user query even when the task parser returns keys or tags but no memory expansions.

Before this change, _parse_task only called the embedder when parsed_goal.memories was non-empty. That made fine search fragile for prompts like "我喜欢什么": the parser can produce useful keys/tags while leaving memories=[], and the retrieval path then falls back to exact metadata filters. For memories added through the fast path, those filters often miss because the stored key is the original message and tags may only contain mode:fast.

This patch keeps the existing metadata lookup behavior, but makes semantic recall available by always embedding the normalized query plus any parser-provided memory expansions. Empty strings are ignored and duplicates are removed.

Why this shape

I did not map UserMemory into pref_mem. The current response formatter treats PreferenceMemory as preference memory and returns UserMemory through text_mem; changing that here would mix two different memory types and could break callers that expect preference metadata.

Validation

  • uv run pytest tests/memories/textual/test_tree_searcher.py tests/memories/textual/test_tree_retriever.py
  • uv run ruff check src/memos/memories/textual/tree_text_memory/retrieve/searcher.py tests/memories/textual/test_tree_searcher.py
  • git diff --check

I also ran a local end-to-end check with the product API, qwen-flash, az-ut-on/text-embedding-3-small, Docker Qdrant, and Docker Neo4j:

  • /product/add wrote 我喜欢草莓 as UserMemory.
  • Qdrant contained the point with memory_type=UserMemory, tags=["mode:fast"], and the expected cube id as user_name.
  • Neo4j contained the same Memory node.
  • A direct Qdrant vector search for 我喜欢什么, using the same embedding model and filtering by user_name plus memory_type=UserMemory, hit the stored strawberry memory.
  • /product/search with mode=fine and search_memory_type=UserMemory returned the relevant memory under text_mem.

Closes #1448

Copilot AI review requested due to automatic review settings April 13, 2026 09:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a fine-search retrieval gap where the query embedding was only computed when the task parser returned non-empty memories, causing fine mode to fall back to brittle exact-metadata filters for some prompts (e.g., “我喜欢什么”) and miss relevant UserMemory items.

Changes:

  • Always build an embedding input set from the normalized query (rephrased_query fallback) plus any parser-provided memory expansions, trimming whitespace, ignoring empty strings, and de-duplicating before embedding.
  • Add a regression test ensuring fine mode embeds the query even when the parser returns keys/tags but memories=[].
  • Extend the test helper to allow setting memory_type in TreeNodeTextualMemoryMetadata.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/memos/memories/textual/tree_text_memory/retrieve/searcher.py Ensures query embeddings are computed even without memory expansions (with trimming + de-dupe).
tests/memories/textual/test_tree_searcher.py Adds regression coverage for the “no memory expansions but still embed query” fine-search path.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: 添加包含偏好信息的记忆后,使用搜索接口POST /product/search无法检索到任何记忆

2 participants