Skip to content

Commit ff1b0d1

Browse files
authored
♻️ Improvement: Enhance processing of ES index names in memory banks. (#3196)
[Specification Details] 1. Replace all symbols in the index name that do not meet the rules with "_". 2. Modify test cases.
1 parent 119af70 commit ff1b0d1

2 files changed

Lines changed: 264 additions & 322 deletions

File tree

backend/utils/memory_utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import re
23
from typing import Dict, Any
34
from urllib.parse import urlparse
45

@@ -9,6 +10,11 @@
910
logger = logging.getLogger("memory_utils")
1011

1112

13+
def _sanitize_index_component(value: str) -> str:
14+
"""Convert arbitrary text into an Elasticsearch-safe index component."""
15+
return re.sub(r"[^a-z0-9_.-]", "_", value.lower())
16+
17+
1218
def build_memory_config(tenant_id: str) -> Dict[str, Any]:
1319
"""Return a fully-validated configuration dictionary for *mem0* ``Memory``.
1420
"""
@@ -30,9 +36,8 @@ def build_memory_config(tenant_id: str) -> Dict[str, Any]:
3036
es_host = f"{parsed.scheme}://{parsed.hostname}"
3137
es_port = parsed.port
3238
# Normalize repo/name to avoid problematic characters in index names
33-
safe_repo = embed_raw["model_repo"].lower().replace(
34-
"/", "_") if embed_raw["model_repo"] else ""
35-
safe_name = embed_raw["model_name"].lower().replace("/", "_")
39+
safe_repo = _sanitize_index_component(embed_raw["model_repo"]) if embed_raw["model_repo"] else ""
40+
safe_name = _sanitize_index_component(embed_raw["model_name"])
3641
index_name = (
3742
f"mem0_{safe_repo}_{safe_name}_{embed_raw['max_tokens']}"
3843
if embed_raw["model_repo"]
@@ -73,4 +78,4 @@ def build_memory_config(tenant_id: str) -> Dict[str, Any]:
7378
},
7479
"telemetry": {"enabled": False},
7580
}
76-
return memory_config
81+
return memory_config

0 commit comments

Comments
 (0)