Skip to content

Commit a3ac092

Browse files
committed
fix:ci code
1 parent 2f3b0fc commit a3ac092

File tree

13 files changed

+1304
-1187
lines changed

13 files changed

+1304
-1187
lines changed

examples/mem_os/multi_user_memos_example.py

Lines changed: 49 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
Example demonstrating how to use MOSProduct for multi-user scenarios.
33
"""
44

5-
from memos.configs.mem_os import MOSConfig
6-
from memos.configs.llm import LLMConfigFactory
7-
from memos.configs.mem_reader import MemReaderConfigFactory
85
from memos.configs.mem_cube import GeneralMemCubeConfig
9-
from memos.mem_os.product import MOSProduct
6+
from memos.configs.mem_os import MOSConfig
107
from memos.mem_cube.general import GeneralMemCube
11-
from memos.configs.mem_cube import GeneralMemCubeConfig
12-
from memos.configs.memory import MemoryConfigFactory
13-
from memos.memories.factory import MemoryFactory
8+
from memos.mem_os.product import MOSProduct
9+
1410

1511
def get_config(user_name):
1612
openapi_config = {
@@ -23,25 +19,13 @@ def get_config(user_name):
2319
"api_key": "your-api-key-here",
2420
"api_base": "https://api.openai.com/v1",
2521
}
26-
huggingface_config = {
27-
"model_name_or_path": "Qwen/Qwen3-1.7B",
28-
"temperature": 0.1,
29-
"remove_think_prefix": True,
30-
"max_tokens": 4096,
31-
"top_p": 0.9,
32-
"top_k": 50,
33-
"add_generation_prompt": True,
34-
}
3522
# Create a default configuration
3623
default_config = MOSConfig(
3724
user_id="root",
38-
chat_model={
39-
"backend":"openai",
40-
"config":openapi_config
41-
},
25+
chat_model={"backend": "openai", "config": openapi_config},
4226
mem_reader={
43-
"backend":"naive",
44-
"config":{
27+
"backend": "naive",
28+
"config": {
4529
"llm": {
4630
"backend": "openai",
4731
"config": openapi_config,
@@ -52,104 +36,90 @@ def get_config(user_name):
5236
"model_name_or_path": "nomic-embed-text:latest",
5337
},
5438
},
55-
}
39+
},
5640
},
5741
enable_textual_memory=True,
5842
enable_activation_memory=False,
5943
top_k=5,
60-
max_turns_window=20
44+
max_turns_window=20,
6145
)
62-
default_cube_config = GeneralMemCubeConfig.model_validate({
63-
"user_id": user_name,
64-
"cube_id": f"{user_name}_default_cube",
65-
"text_mem": {
66-
"backend": "tree_text",
67-
"config": {
68-
"extractor_llm": {
69-
"backend": "openai",
70-
"config": openapi_config
71-
},
72-
"dispatcher_llm": {
73-
"backend": "openai",
74-
"config": openapi_config
46+
default_cube_config = GeneralMemCubeConfig.model_validate(
47+
{
48+
"user_id": user_name,
49+
"cube_id": f"{user_name}_default_cube",
50+
"text_mem": {
51+
"backend": "tree_text",
52+
"config": {
53+
"extractor_llm": {"backend": "openai", "config": openapi_config},
54+
"dispatcher_llm": {"backend": "openai", "config": openapi_config},
55+
"graph_db": {
56+
"backend": "neo4j",
57+
"config": {
58+
"uri": "bolt://localhost:7687",
59+
"user": "neo4j",
60+
"password": "12345678",
61+
"db_name": user_name,
62+
"auto_create": True,
7563
},
76-
"graph_db": {
77-
"backend": "neo4j",
78-
"config": {
79-
"uri": "bolt://localhost:7687",
80-
"user": "neo4j",
81-
"password": "12345678",
82-
"db_name": user_name,
83-
"auto_create": True,
84-
},
64+
},
65+
"embedder": {
66+
"backend": "ollama",
67+
"config": {
68+
"model_name_or_path": "nomic-embed-text:latest",
8569
},
86-
"embedder": {
87-
"backend": "ollama",
88-
"config": {
89-
"model_name_or_path": "nomic-embed-text:latest",
90-
},
9170
},
92-
}
93-
},
94-
"act_mem": {},
95-
"para_mem": {},
96-
})
71+
},
72+
},
73+
"act_mem": {},
74+
"para_mem": {},
75+
}
76+
)
9777
default_mem_cube = GeneralMemCube(default_cube_config)
9878
return default_config, default_mem_cube
99-
79+
80+
10081
def main():
10182
default_config, default_mem_cube = get_config(user_name="alice")
10283
# Initialize MOSProduct with default config
10384
mos_product = MOSProduct(default_config=default_config)
104-
10585

10686
# Register first user with default config
10787
result1 = mos_product.user_register(
10888
user_id="alice",
10989
user_name="alice",
11090
interests="I'm interested in machine learning and AI research.",
111-
default_mem_cube=default_mem_cube
91+
default_mem_cube=default_mem_cube,
11292
)
11393
print(f"User registration result: {result1}")
114-
94+
11595
# Chat with Alice
11696
print("\n=== Chatting with Alice ===")
117-
for response_chunk in mos_product.chat(
118-
query="What are my interests?",
119-
user_id="alice"
120-
):
97+
for response_chunk in mos_product.chat(query="What are my interests?", user_id="alice"):
12198
print(response_chunk, end="")
122-
99+
123100
# Add memory for Alice
124101
mos_product.add(
125102
user_id="alice",
126103
memory_content="I attended a machine learning conference last week.",
127-
mem_cube_id=result1["default_cube_id"]
104+
mem_cube_id=result1["default_cube_id"],
128105
)
129-
106+
130107
# Search memories for Alice
131-
search_result = mos_product.search(
132-
query="conference",
133-
user_id="alice"
134-
)
108+
search_result = mos_product.search(query="conference", user_id="alice")
135109
print(f"\nSearch result for Alice: {search_result}")
136110

137111
# Search memories for Alice
138-
search_result = mos_product.get_all(
139-
query="conference",
140-
user_id="alice",
141-
memory_type="text_mem"
142-
)
112+
search_result = mos_product.get_all(query="conference", user_id="alice", memory_type="text_mem")
143113
print(f"\nSearch result for Alice: {search_result}")
144-
114+
145115
# List all users
146116
users = mos_product.list_users()
147117
print(f"\nAll registered users: {users}")
148-
118+
149119
# Get user info
150120
alice_info = mos_product.get_user_info("alice")
151121
print(f"\nAlice's info: {alice_info}")
152122

153123

154124
if __name__ == "__main__":
155-
main()
125+
main()

examples/mem_os/persistent_memos_example.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import os
99
import tempfile
10-
from pathlib import Path
1110

1211
from memos.configs.mem_os import MOSConfig
1312
from 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

5453
def 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("\n2. 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("\n3. 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("\n4. 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("\n5. 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("\n6. 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("\n7. 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("\n8. 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

189190
if __name__ == "__main__":
190191
demonstrate_persistence()
191-
demonstrate_api_usage()
192+
demonstrate_api_usage()

0 commit comments

Comments
 (0)