MCPcopy
hub / github.com/NevaMind-AI/memU / _embedding_based_retrieve

Method _embedding_based_retrieve

src/memu/app/retrieve.py:867–941  ·  view source on GitHub ↗

Embedding-based retrieval with query rewriting and judging at each tier

(
        self,
        query: str,
        top_k: int,
        context_queries: list[dict[str, Any]] | None,
        ctx: Context,
        store: Database,
        llm_client: Any | None = None,
        where: Mapping[str, Any] | None = None,
    )

Source from the content-addressed store, hash-verified

865 return None
866
867 async def _embedding_based_retrieve(
868 self,
869 query: str,
870 top_k: int,
871 context_queries: list[dict[str, Any]] | None,
872 ctx: Context,
873 store: Database,
874 llm_client: Any | None = None,
875 where: Mapping[str, Any] | None = None,
876 ) -> dict[str, Any]:
877 """Embedding-based retrieval with query rewriting and judging at each tier"""
878 where_filters = self._normalize_where(where)
879 category_pool = store.memory_category_repo.list_categories(where_filters)
880 items_pool = store.memory_item_repo.list_items(where_filters)
881 resource_pool = store.resource_repo.list_resources(where_filters)
882 client = llm_client or self._get_llm_client()
883 current_query = query
884 qvec = (await client.embed([current_query]))[0]
885 response: dict[str, Any] = {"resources": [], "items": [], "categories": [], "next_step_query": None}
886 content_sections: list[str] = []
887
888 # Tier 1: Categories
889 cat_hits, summary_lookup = await self._rank_categories_by_summary(
890 qvec,
891 top_k,
892 ctx,
893 store,
894 embed_client=client,
895 categories=category_pool,
896 )
897 if cat_hits:
898 response["categories"] = self._materialize_hits(cat_hits, category_pool)
899 content_sections.append(
900 self._format_category_content(cat_hits, summary_lookup, store, categories=category_pool)
901 )
902
903 needs_more, current_query = await self._decide_if_retrieval_needed(
904 current_query,
905 context_queries,
906 retrieved_content="\n\n".join(content_sections),
907 llm_client=client,
908 )
909 response["next_step_query"] = current_query
910 if not needs_more:
911 return response
912 # Re-embed with rewritten query
913 qvec = (await client.embed([current_query]))[0]
914
915 # Tier 2: Items
916 item_hits = store.memory_item_repo.vector_search_items(qvec, top_k, where=where_filters)
917 if item_hits:
918 response["items"] = self._materialize_hits(item_hits, items_pool)
919 content_sections.append(self._format_item_content(item_hits, store, items=items_pool))
920
921 needs_more, current_query = await self._decide_if_retrieval_needed(
922 current_query,
923 context_queries,
924 retrieved_content="\n\n".join(content_sections),

Callers

nothing calls this directly

Calls 15

_normalize_whereMethod · 0.95
_materialize_hitsMethod · 0.95
_format_item_contentMethod · 0.95
cosine_topkFunction · 0.90
_get_llm_clientMethod · 0.80
list_categoriesMethod · 0.45
list_itemsMethod · 0.45

Tested by

no test coverage detected