|
| 1 | +""" |
| 2 | +Example demonstrating how to use MOSProduct for multi-user scenarios. |
| 3 | +""" |
| 4 | + |
| 5 | +from memos.configs.mem_cube import GeneralMemCubeConfig |
| 6 | +from memos.configs.mem_os import MOSConfig |
| 7 | +from memos.mem_cube.general import GeneralMemCube |
| 8 | +from memos.mem_os.product import MOSProduct |
| 9 | + |
| 10 | + |
| 11 | +def get_config(user_name): |
| 12 | + openapi_config = { |
| 13 | + "model_name_or_path": "gpt-4o-mini", |
| 14 | + "temperature": 0.8, |
| 15 | + "max_tokens": 1024, |
| 16 | + "top_p": 0.9, |
| 17 | + "top_k": 50, |
| 18 | + "remove_think_prefix": True, |
| 19 | + "api_key": "your-api-key-here", |
| 20 | + "api_base": "https://api.openai.com/v1", |
| 21 | + } |
| 22 | + # Create a default configuration |
| 23 | + default_config = MOSConfig( |
| 24 | + user_id="root", |
| 25 | + chat_model={"backend": "openai", "config": openapi_config}, |
| 26 | + mem_reader={ |
| 27 | + "backend": "naive", |
| 28 | + "config": { |
| 29 | + "llm": { |
| 30 | + "backend": "openai", |
| 31 | + "config": openapi_config, |
| 32 | + }, |
| 33 | + "embedder": { |
| 34 | + "backend": "ollama", |
| 35 | + "config": { |
| 36 | + "model_name_or_path": "nomic-embed-text:latest", |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + enable_textual_memory=True, |
| 42 | + enable_activation_memory=False, |
| 43 | + top_k=5, |
| 44 | + max_turns_window=20, |
| 45 | + ) |
| 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, |
| 63 | + }, |
| 64 | + }, |
| 65 | + "embedder": { |
| 66 | + "backend": "ollama", |
| 67 | + "config": { |
| 68 | + "model_name_or_path": "nomic-embed-text:latest", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + "act_mem": {}, |
| 74 | + "para_mem": {}, |
| 75 | + } |
| 76 | + ) |
| 77 | + default_mem_cube = GeneralMemCube(default_cube_config) |
| 78 | + return default_config, default_mem_cube |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + default_config, default_mem_cube = get_config(user_name="alice") |
| 83 | + # Initialize MOSProduct with default config |
| 84 | + mos_product = MOSProduct(default_config=default_config) |
| 85 | + |
| 86 | + # Register first user with default config |
| 87 | + result1 = mos_product.user_register( |
| 88 | + user_id="alice", |
| 89 | + user_name="alice", |
| 90 | + interests="I'm interested in machine learning and AI research.", |
| 91 | + default_mem_cube=default_mem_cube, |
| 92 | + ) |
| 93 | + print(f"User registration result: {result1}") |
| 94 | + |
| 95 | + # Chat with Alice |
| 96 | + print("\n=== Chatting with Alice ===") |
| 97 | + for response_chunk in mos_product.chat(query="What are my interests?", user_id="alice"): |
| 98 | + print(response_chunk, end="") |
| 99 | + |
| 100 | + # Add memory for Alice |
| 101 | + mos_product.add( |
| 102 | + user_id="alice", |
| 103 | + memory_content="I attended a machine learning conference last week.", |
| 104 | + mem_cube_id=result1["default_cube_id"], |
| 105 | + ) |
| 106 | + |
| 107 | + # Search memories for Alice |
| 108 | + search_result = mos_product.search(query="conference", user_id="alice") |
| 109 | + print(f"\nSearch result for Alice: {search_result}") |
| 110 | + |
| 111 | + # Search memories for Alice |
| 112 | + search_result = mos_product.get_all(query="conference", user_id="alice", memory_type="text_mem") |
| 113 | + print(f"\nSearch result for Alice: {search_result}") |
| 114 | + |
| 115 | + # List all users |
| 116 | + users = mos_product.list_users() |
| 117 | + print(f"\nAll registered users: {users}") |
| 118 | + |
| 119 | + # Get user info |
| 120 | + alice_info = mos_product.get_user_info("alice") |
| 121 | + print(f"\nAlice's info: {alice_info}") |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + main() |
0 commit comments