11import concurrent .futures
22import json
3+ import os
34
45from typing import Any
56
67from memos .configs .mem_os import MOSConfig
78from memos .llms .factory import LLMFactory
89from memos .log import get_logger
910from memos .mem_os .core import MOSCore
11+ from memos .mem_os .utils .default_config import get_default
1012from memos .memories .textual .base import BaseTextMemory
1113from 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.
0 commit comments