Skip to content

Commit 8eb0738

Browse files
authored
Feat: add simple memos for user could easy start (#118)
* feat: add neo and simple memos * fix: remove mock data
1 parent 9cbed6d commit 8eb0738

File tree

6 files changed

+470
-20
lines changed

6 files changed

+470
-20
lines changed

examples/mem_os/easy_memos.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Simple test script for MOS.simple() functionality.
4+
"""
5+
6+
import os
7+
8+
from memos.mem_os.main import MOS
9+
10+
11+
# Set environment variables for testing
12+
os.environ["OPENAI_API_BASE"] = "http://xxxxxxxxx"
13+
os.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxx"
14+
os.environ["MOS_TEXT_MEM_TYPE"] = "general_text" # "tree_text" need set neo4j
15+
16+
17+
memory = MOS.simple()
18+
print("MOS.simple() works!")
19+
memory.add(memory_content="my favorite color is blue")
20+
print(memory.chat("what is my favorite color?"))
21+
# Your favorite color is blue!

src/memos/mem_os/core.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,10 @@ def create_cube_for_user(
411411
return self.user_manager.create_cube(cube_name, owner_id, cube_path, cube_id)
412412

413413
def register_mem_cube(
414-
self, mem_cube_name_or_path: str, mem_cube_id: str | None = None, user_id: str | None = None
414+
self,
415+
mem_cube_name_or_path: str | GeneralMemCube,
416+
mem_cube_id: str | None = None,
417+
user_id: str | None = None,
415418
) -> None:
416419
"""
417420
Register a MemCube with the MOS.
@@ -424,12 +427,18 @@ def register_mem_cube(
424427
self._validate_user_exists(target_user_id)
425428

426429
if mem_cube_id is None:
427-
mem_cube_id = mem_cube_name_or_path
430+
if isinstance(mem_cube_name_or_path, GeneralMemCube):
431+
mem_cube_id = f"cube_{target_user_id}"
432+
else:
433+
mem_cube_id = mem_cube_name_or_path
428434

429435
if mem_cube_id in self.mem_cubes:
430436
logger.info(f"MemCube with ID {mem_cube_id} already in MOS, skip install.")
431437
else:
432-
if os.path.exists(mem_cube_name_or_path):
438+
if isinstance(mem_cube_name_or_path, GeneralMemCube):
439+
self.mem_cubes[mem_cube_id] = mem_cube_name_or_path
440+
logger.info(f"register new cube {mem_cube_id} for user {target_user_id}")
441+
elif os.path.exists(mem_cube_name_or_path):
433442
self.mem_cubes[mem_cube_id] = GeneralMemCube.init_from_dir(mem_cube_name_or_path)
434443
else:
435444
logger.warning(
@@ -462,10 +471,14 @@ def register_mem_cube(
462471
else:
463472
# Cube doesn't exist, create it
464473
self.create_cube_for_user(
465-
cube_name=mem_cube_name_or_path,
474+
cube_name=mem_cube_name_or_path
475+
if not isinstance(mem_cube_name_or_path, GeneralMemCube)
476+
else mem_cube_id,
466477
owner_id=target_user_id,
467478
cube_id=mem_cube_id,
468-
cube_path=mem_cube_name_or_path,
479+
cube_path=mem_cube_name_or_path
480+
if not isinstance(mem_cube_name_or_path, GeneralMemCube)
481+
else "init",
469482
)
470483
logger.info(f"register new cube {mem_cube_id} for user {target_user_id}")
471484

src/memos/mem_os/main.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import concurrent.futures
22
import json
3+
import os
34

45
from typing import Any
56

67
from memos.configs.mem_os import MOSConfig
78
from memos.llms.factory import LLMFactory
89
from memos.log import get_logger
910
from memos.mem_os.core import MOSCore
11+
from memos.mem_os.utils.default_config import get_default
1012
from memos.memories.textual.base import BaseTextMemory
1113
from memos.templates.mos_prompts import (
1214
COT_DECOMPOSE_PROMPT,
@@ -24,13 +26,84 @@ class MOS(MOSCore):
2426
This class maintains backward compatibility with the original MOS interface.
2527
"""
2628

27-
def __init__(self, config: MOSConfig):
29+
def __init__(self, config: MOSConfig | None = None):
30+
"""
31+
Initialize MOS with optional automatic configuration.
32+
33+
Args:
34+
config (MOSConfig, optional): MOS configuration. If None, will use automatic configuration from environment variables.
35+
"""
36+
if config is None:
37+
# Auto-configure if no config provided
38+
config, default_cube = self._auto_configure()
39+
self._auto_registered_cube = default_cube
40+
else:
41+
self._auto_registered_cube = None
42+
2843
self.enable_cot = config.PRO_MODE
2944
if config.PRO_MODE:
3045
print(PRO_MODE_WELCOME_MESSAGE)
3146
logger.info(PRO_MODE_WELCOME_MESSAGE)
3247
super().__init__(config)
3348

49+
# Auto-register cube if one was created
50+
if self._auto_registered_cube is not None:
51+
self.register_mem_cube(self._auto_registered_cube)
52+
logger.info(
53+
f"Auto-registered default cube: {self._auto_registered_cube.config.cube_id}"
54+
)
55+
56+
def _auto_configure(self, **kwargs) -> tuple[MOSConfig, Any]:
57+
"""
58+
Automatically configure MOS with default settings.
59+
60+
Returns:
61+
tuple[MOSConfig, Any]: MOS configuration and default MemCube
62+
"""
63+
# Get configuration from environment variables
64+
openai_api_key = os.getenv("OPENAI_API_KEY")
65+
openai_api_base = os.getenv("OPENAI_API_BASE", "https://api.openai.com/v1")
66+
text_mem_type = os.getenv("MOS_TEXT_MEM_TYPE", "general_text")
67+
68+
if not openai_api_key:
69+
raise ValueError("OPENAI_API_KEY environment variable is required")
70+
71+
logger.info(f"Auto-configuring MOS with text_mem_type: {text_mem_type}")
72+
return get_default(
73+
openai_api_key=openai_api_key,
74+
openai_api_base=openai_api_base,
75+
text_mem_type=text_mem_type,
76+
)
77+
78+
@classmethod
79+
def simple(cls) -> "MOS":
80+
"""
81+
Create a MOS instance with automatic configuration from environment variables.
82+
83+
This is the simplest way to get started with MemOS.
84+
85+
Environment variables needed:
86+
- OPENAI_API_KEY: Your OpenAI API key
87+
- OPENAI_API_BASE: OpenAI API base URL (optional, defaults to "https://api.openai.com/v1")
88+
- MOS_TEXT_MEM_TYPE: Text memory type (optional, defaults to "general_text")
89+
90+
Returns:
91+
MOS: Configured MOS instance with auto-registered default cube
92+
93+
Example:
94+
```python
95+
# Set environment variables
96+
export OPENAI_API_KEY="your-api-key"
97+
export MOS_TEXT_MEM_TYPE="general_text"
98+
99+
# Then use
100+
memory = MOS.simple()
101+
memory.add_memory("Hello world!")
102+
response = memory.chat("What did I just say?")
103+
```
104+
"""
105+
return cls()
106+
34107
def chat(self, query: str, user_id: str | None = None, base_prompt: str | None = None) -> str:
35108
"""
36109
Enhanced chat method with optional CoT (Chain of Thought) enhancement.

src/memos/mem_os/product.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
load_dotenv()
3636

3737
CUBE_PATH = os.getenv("MOS_CUBE_PATH", "/tmp/data/")
38-
with open("./tmp/fake_data.json") as f:
39-
MOCK_DATA = json.loads(f.read())
4038

4139

4240
class MOSProduct(MOSCore):

0 commit comments

Comments
 (0)