Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions examples/mem_os/multi_user_memos_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Example demonstrating how to use MOSProduct for multi-user scenarios.
"""

import os

from memos.configs.mem_cube import GeneralMemCubeConfig
from memos.configs.mem_os import MOSConfig
from memos.mem_cube.general import GeneralMemCube
Expand All @@ -16,28 +18,53 @@ def get_config(user_name):
"top_p": 0.9,
"top_k": 50,
"remove_think_prefix": True,
"api_key": "your-api-key-here",
"api_base": "https://api.openai.com/v1",
"api_key": os.getenv("OPENAI_API_KEY"),
"api_base": os.getenv("OPENAI_API_BASE"),
}
# Create a default configuration
default_config = MOSConfig(
user_id="root",
chat_model={"backend": "openai", "config": openapi_config},
mem_reader={
"backend": "naive",
"backend": "simple_struct",
"config": {
"llm": {
"backend": "openai",
"config": openapi_config,
},
"embedder": {
"backend": "ollama",
"backend": "universal_api",
"config": {
"provider": os.getenv("MOS_EMBEDDER_PROVIDER", "openai"),
"api_key": os.getenv("MOS_EMBEDDER_API_KEY", "sk-xxxx"),
"model_name_or_path": os.getenv(
"MOS_EMBEDDER_MODEL", "text-embedding-3-large"
),
"base_url": os.getenv("MOS_EMBEDDER_API_BASE", "http://openai.com"),
},
},
"chunker": {
"backend": "sentence",
"config": {
"model_name_or_path": "nomic-embed-text:latest",
"tokenizer_or_token_counter": "gpt2",
"chunk_size": 512,
"chunk_overlap": 128,
"min_sentences_per_chunk": 1,
},
},
},
},
user_manager={
"backend": "mysql",
"config": {
"host": os.getenv("MYSQL_HOST", "localhost"),
"port": int(os.getenv("MYSQL_PORT", "3306")),
"username": os.getenv("MYSQL_USERNAME", "root"),
"password": os.getenv("MYSQL_PASSWORD", "12345678"),
"database": os.getenv("MYSQL_DATABASE", "memos_users"),
"charset": os.getenv("MYSQL_CHARSET", "utf8mb4"),
},
},
enable_textual_memory=True,
enable_activation_memory=False,
top_k=5,
Expand All @@ -55,17 +82,27 @@ def get_config(user_name):
"graph_db": {
"backend": "neo4j",
"config": {
"uri": "bolt://localhost:7687",
"user": "neo4j",
"password": "12345678",
"db_name": user_name,
"uri": os.getenv("NEO4J_URI", "bolt://localhost:7687"),
"user": os.getenv("NEO4J_USER", "neo4j"),
"password": os.getenv("NEO4J_PASSWORD", "12345678"),
"db_name": os.getenv(
"NEO4J_DB_NAME", "shared-tree-textual-memory-test"
),
"user_name": f"memos{user_name.replace('-', '')}",
"embedding_dimension": int(os.getenv("EMBEDDING_DIMENSION", 768)),
"use_multi_db": False,
"auto_create": True,
},
},
"embedder": {
"backend": "ollama",
"backend": "universal_api",
"config": {
"model_name_or_path": "nomic-embed-text:latest",
"provider": os.getenv("MOS_EMBEDDER_PROVIDER", "openai"),
"api_key": os.getenv("MOS_EMBEDDER_API_KEY", "sk-xxxx"),
"model_name_or_path": os.getenv(
"MOS_EMBEDDER_MODEL", "text-embedding-3-large"
),
"base_url": os.getenv("MOS_EMBEDDER_API_BASE", "http://openai.com"),
},
},
},
Expand Down Expand Up @@ -109,7 +146,7 @@ def main():
print(f"\nSearch result for Alice: {search_result}")

# Search memories for Alice
search_result = mos_product.get_all(query="conference", user_id="alice", memory_type="text_mem")
search_result = mos_product.get_all(user_id="alice", memory_type="text_mem")
print(f"\nSearch result for Alice: {search_result}")

# List all users
Expand Down
2 changes: 1 addition & 1 deletion src/memos/mem_os/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ def get_user_info(self) -> dict[str, Any]:
return {
"user_id": user.user_id,
"user_name": user.user_name,
"role": user.role.value,
"role": user.role.value if hasattr(user.role, "value") else user.role,
"created_at": user.created_at.isoformat(),
"accessible_cubes": [
{
Expand Down
6 changes: 4 additions & 2 deletions src/memos/mem_user/mysql_user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class User(Base):

user_id = Column(String(255), primary_key=True, default=lambda: str(uuid.uuid4()))
user_name = Column(String(255), unique=True, nullable=False)
role = Column(String(20), default=UserRole.USER.value, nullable=False)
role = Column(
String(20), default=UserRole.USER.value, nullable=False
) # for sqlite backend this is SQLEnum
created_at = Column(DateTime, default=datetime.now, nullable=False)
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, nullable=False)
is_active = Column(Boolean, default=True, nullable=False)
Expand All @@ -65,7 +67,7 @@ class User(Base):
owned_cubes = relationship("Cube", back_populates="owner", cascade="all, delete-orphan")

def __repr__(self):
return f"<User(user_id='{self.user_id}', user_name='{self.user_name}', role='{self.role.value}')>"
return f"<User(user_id='{self.user_id}', user_name='{self.user_name}', role='{self.role}')>"


class Cube(Base):
Expand Down
Loading