Store user_id in vectordb (#2466)
This commit is contained in:
@@ -3,6 +3,7 @@ import os
|
||||
import uuid
|
||||
|
||||
# Set up the directory path
|
||||
VECTOR_ID = str(uuid.uuid4())
|
||||
home_dir = os.path.expanduser("~")
|
||||
mem0_dir = os.environ.get("MEM0_DIR") or os.path.join(home_dir, ".mem0")
|
||||
os.makedirs(mem0_dir, exist_ok=True)
|
||||
@@ -29,3 +30,29 @@ def get_user_id():
|
||||
return user_id
|
||||
except Exception:
|
||||
return "anonymous_user"
|
||||
|
||||
|
||||
def get_or_create_user_id(vector_store):
|
||||
"""Store user_id in vector store and return it."""
|
||||
user_id = get_user_id()
|
||||
|
||||
# Try to get existing user_id from vector store
|
||||
try:
|
||||
existing = vector_store.get(vector_id=VECTOR_ID)
|
||||
if existing and hasattr(existing, "payload") and existing.payload and "user_id" in existing.payload:
|
||||
return existing.payload["user_id"]
|
||||
except:
|
||||
pass
|
||||
|
||||
# If we get here, we need to insert the user_id
|
||||
try:
|
||||
dims = getattr(vector_store, "embedding_model_dims", 1)
|
||||
vector_store.insert(
|
||||
vectors=[[0.0] * dims],
|
||||
payloads=[{"user_id": user_id, "type": "user_identity"}],
|
||||
ids=[VECTOR_ID]
|
||||
)
|
||||
except:
|
||||
pass
|
||||
|
||||
return user_id
|
||||
|
||||
Reference in New Issue
Block a user