Add configurable case-insensitive search
- Add MEM0_CASE_INSENSITIVE config option (default: false) - When enabled, searches with both original and lowercase query - Merges results, keeping highest score for each memory - Fixes case sensitivity issues with Qdrant embeddings - Generalize after-install.md with placeholders instead of personal values
This commit is contained in:
@@ -65,13 +65,52 @@ class LocalMem0Client:
|
||||
query: str,
|
||||
user_id: Optional[str] = None,
|
||||
limit: int = 5,
|
||||
case_insensitive: bool = False,
|
||||
) -> List[Dict]:
|
||||
"""Search memories by semantic similarity.
|
||||
|
||||
API: POST /search
|
||||
Request: {query, user_id, limit}
|
||||
Response: {results: [{id, text, user_id, score, metadata}]}
|
||||
|
||||
Args:
|
||||
query: Search query
|
||||
user_id: User identifier
|
||||
limit: Max results
|
||||
case_insensitive: If True, search with both original and lowercase query
|
||||
"""
|
||||
if not case_insensitive:
|
||||
payload = {"query": query, "limit": limit}
|
||||
if user_id:
|
||||
payload["user_id"] = user_id
|
||||
result = self._request("POST", "/search", json=payload)
|
||||
return result.get("results", [])
|
||||
|
||||
# Case-insensitive mode: search with both original and lowercase
|
||||
# Fetch 2x limit to ensure we get top N after merging
|
||||
results_original = self._search_with_query(query, user_id, limit * 2)
|
||||
results_lower = self._search_with_query(query.lower(), user_id, limit * 2)
|
||||
|
||||
# Merge and deduplicate, keeping highest score
|
||||
merged = {}
|
||||
for result in results_original + results_lower:
|
||||
mem_id = result.get("id")
|
||||
if mem_id not in merged or result.get("score", 0) > merged[mem_id].get(
|
||||
"score", 0
|
||||
):
|
||||
merged[mem_id] = result
|
||||
|
||||
return sorted(merged.values(), key=lambda x: x.get("score", 0), reverse=True)[
|
||||
:limit
|
||||
]
|
||||
|
||||
def _search_with_query(
|
||||
self,
|
||||
query: str,
|
||||
user_id: Optional[str] = None,
|
||||
limit: int = 5,
|
||||
) -> List[Dict]:
|
||||
"""Internal search helper for case-insensitive mode."""
|
||||
payload = {"query": query, "limit": limit}
|
||||
if user_id:
|
||||
payload["user_id"] = user_id
|
||||
|
||||
Reference in New Issue
Block a user