fix: keep query embedding for fine search#1478
Open
fancyboi999 wants to merge 1 commit intoMemTensor:mainfrom
Open
fix: keep query embedding for fine search#1478fancyboi999 wants to merge 1 commit intoMemTensor:mainfrom
fancyboi999 wants to merge 1 commit intoMemTensor:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
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_queryfallback) 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_typeinTreeNodeTextualMemoryMetadata.
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.
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_taskonly called the embedder whenparsed_goal.memorieswas non-empty. That made fine search fragile for prompts like "我喜欢什么": the parser can produce useful keys/tags while leavingmemories=[], and the retrieval path then falls back to exact metadata filters. For memories added through the fast path, those filters often miss because the storedkeyis the original message andtagsmay only containmode: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
UserMemoryintopref_mem. The current response formatter treatsPreferenceMemoryas preference memory and returnsUserMemorythroughtext_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.pyuv run ruff check src/memos/memories/textual/tree_text_memory/retrieve/searcher.py tests/memories/textual/test_tree_searcher.pygit diff --checkI 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/addwrote我喜欢草莓asUserMemory.memory_type=UserMemory,tags=["mode:fast"], and the expected cube id asuser_name.Memorynode.我喜欢什么, using the same embedding model and filtering byuser_nameplusmemory_type=UserMemory, hit the stored strawberry memory./product/searchwithmode=fineandsearch_memory_type=UserMemoryreturned the relevant memory undertext_mem.Closes #1448