77
88import os
99import tempfile
10- from pathlib import Path
1110
1211from memos .configs .mem_os import MOSConfig
1312from memos .mem_os .product import MOSProduct
@@ -24,7 +23,7 @@ def create_sample_config(user_id: str) -> MOSConfig:
2423 "model_name_or_path" : "gpt-3.5-turbo" ,
2524 "api_key" : "your-api-key-here" ,
2625 "temperature" : 0.7 ,
27- }
26+ },
2827 },
2928 mem_reader = {
3029 "backend" : "naive" ,
@@ -34,52 +33,52 @@ def create_sample_config(user_id: str) -> MOSConfig:
3433 "config" : {
3534 "model_name_or_path" : "gpt-3.5-turbo" ,
3635 "api_key" : "your-api-key-here" ,
37- }
36+ },
3837 },
3938 "embedder" : {
4039 "backend" : "ollama" ,
4140 "config" : {
4241 "model_name_or_path" : "nomic-embed-text:latest" ,
43- }
44- }
45- }
42+ },
43+ },
44+ },
4645 },
4746 enable_textual_memory = True ,
4847 enable_activation_memory = False ,
4948 top_k = 5 ,
50- max_turns_window = 20
49+ max_turns_window = 20 ,
5150 )
5251
5352
5453def demonstrate_persistence ():
5554 """Demonstrate the persistence functionality."""
5655 print ("=== MemOS Persistent User Management Demo ===\n " )
57-
56+
5857 # Create a temporary database for this demo
5958 temp_dir = tempfile .mkdtemp ()
6059 db_path = os .path .join (temp_dir , "demo_memos.db" )
61-
60+
6261 try :
6362 # Step 1: Create a persistent user manager
6463 print ("1. Creating PersistentUserManager..." )
6564 user_manager = PersistentUserManager (db_path = db_path )
6665 print (f" Database created at: { db_path } " )
67-
66+
6867 # Step 2: Create some sample configurations
6968 print ("\n 2. Creating sample user configurations..." )
7069 user_configs = {}
7170 for i in range (3 ):
72- user_id = f"user_{ i + 1 } "
73- user_name = f"User { i + 1 } "
71+ user_id = f"user_{ i + 1 } "
72+ user_name = f"User { i + 1 } "
7473 config = create_sample_config (user_id )
7574 user_configs [user_id ] = config
76-
75+
7776 # Create user with configuration
7877 created_id = user_manager .create_user_with_config (
7978 user_name , config , UserRole .USER , user_id
8079 )
8180 print (f" Created user: { user_name } (ID: { created_id } )" )
82-
81+
8382 # Step 3: Verify configurations are saved
8483 print ("\n 3. Verifying configurations are saved..." )
8584 for user_id in user_configs :
@@ -90,12 +89,12 @@ def demonstrate_persistence():
9089 print (f" - Top-k: { config .top_k } " )
9190 else :
9291 print (f" ✗ Configuration not found for { user_id } " )
93-
92+
9493 # Step 4: Simulate service restart by creating a new manager instance
9594 print ("\n 4. Simulating service restart..." )
9695 print (" Creating new PersistentUserManager instance..." )
9796 new_user_manager = PersistentUserManager (db_path = db_path )
98-
97+
9998 # Step 5: Verify configurations are restored
10099 print ("\n 5. Verifying configurations are restored after restart..." )
101100 for user_id in user_configs :
@@ -104,17 +103,17 @@ def demonstrate_persistence():
104103 print (f" ✓ Configuration restored for { user_id } " )
105104 else :
106105 print (f" ✗ Configuration not restored for { user_id } " )
107-
106+
108107 # Step 6: Create MOSProduct and demonstrate restoration
109108 print ("\n 6. Creating MOSProduct with persistent user manager..." )
110109 default_config = create_sample_config ("default_user" )
111110 mos_product = MOSProduct (default_config = default_config )
112-
111+
113112 # The MOSProduct should automatically restore user instances
114113 print (f" Active user instances: { len (mos_product .user_instances )} " )
115114 for user_id in mos_product .user_instances :
116115 print (f" - { user_id } " )
117-
116+
118117 # Step 7: Demonstrate configuration update
119118 print ("\n 7. Demonstrating configuration update..." )
120119 user_id = "user_1"
@@ -124,26 +123,28 @@ def demonstrate_persistence():
124123 updated_config = original_config .model_copy (deep = True )
125124 updated_config .top_k = 10
126125 updated_config .enable_activation_memory = True
127-
126+
128127 success = user_manager .save_user_config (user_id , updated_config )
129128 if success :
130129 print (f" ✓ Updated configuration for { user_id } " )
131130 print (f" - New top-k: { updated_config .top_k } " )
132131 print (f" - Activation memory: { updated_config .enable_activation_memory } " )
133132 else :
134133 print (f" ✗ Failed to update configuration for { user_id } " )
135-
134+
136135 # Step 8: List all configurations
137136 print ("\n 8. Listing all user configurations..." )
138137 all_configs = user_manager .list_user_configs ()
139138 print (f" Total configurations: { len (all_configs )} " )
140139 for user_id , config in all_configs .items ():
141- print (f" - { user_id } : top_k={ config .top_k } , textual_memory={ config .enable_textual_memory } " )
142-
140+ print (
141+ f" - { user_id } : top_k={ config .top_k } , textual_memory={ config .enable_textual_memory } "
142+ )
143+
143144 print ("\n === Demo completed successfully! ===" )
144145 print (f"Database file: { db_path } " )
145146 print ("You can inspect this file to see the persistent data." )
146-
147+
147148 except Exception as e :
148149 print (f"Error during demo: { e } " )
149150 raise
@@ -160,18 +161,18 @@ def demonstrate_api_usage():
160161 print ("\n === API Usage Example ===" )
161162 print ("""
162163 With the new persistent system, your API calls would work like this:
163-
164+
164165 1. Register a user (configuration is automatically saved):
165166 POST /product/users/register
166167 {
167168 "user_id": "john_doe",
168169 "user_name": "John Doe",
169170 "interests": "AI, machine learning, programming"
170171 }
171-
172+
172173 2. Get user configuration:
173174 GET /product/users/john_doe/config
174-
175+
175176 3. Update user configuration:
176177 PUT /product/users/john_doe/config
177178 {
@@ -180,12 +181,12 @@ def demonstrate_api_usage():
180181 "top_k": 10,
181182 ...
182183 }
183-
184+
184185 4. After service restart, all user instances are automatically restored
185186 and the user can immediately use the system without re-registration.
186187 """ )
187188
188189
189190if __name__ == "__main__" :
190191 demonstrate_persistence ()
191- demonstrate_api_usage ()
192+ demonstrate_api_usage ()
0 commit comments