|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Simulate full MemOS Product API workflow: |
| 4 | +1. Register user |
| 5 | +2. Add memory |
| 6 | +3. Search memory |
| 7 | +4. Chat (stream) |
| 8 | +""" |
| 9 | + |
| 10 | +import json |
| 11 | + |
| 12 | +import requests |
| 13 | + |
| 14 | + |
| 15 | +BASE_URL = "http://0.0.0.0:8001/product" |
| 16 | +HEADERS = {"Content-Type": "application/json"} |
| 17 | + |
| 18 | +index = "24" |
| 19 | +USER_ID = f"memos_user_id_{index}" |
| 20 | +USER_NAME = f"memos_user_alice_{index}" |
| 21 | +MEM_CUBE_ID = f"memos_cube_id_{index}" |
| 22 | +SESSION_ID = f"memos_session_id_{index}" |
| 23 | +SESSION_ID2 = f"memos_session_id_{index}_s2" |
| 24 | + |
| 25 | + |
| 26 | +def register_user(): |
| 27 | + url = f"{BASE_URL}/users/register" |
| 28 | + data = { |
| 29 | + "user_id": USER_ID, |
| 30 | + "user_name": USER_NAME, |
| 31 | + "interests": "memory,retrieval,test", |
| 32 | + "mem_cube_id": MEM_CUBE_ID, |
| 33 | + } |
| 34 | + print(f"[*] Registering user {USER_ID} ...") |
| 35 | + resp = requests.post(url, headers=HEADERS, data=json.dumps(data), timeout=30) |
| 36 | + print(resp.status_code, resp.text) |
| 37 | + return resp.json() |
| 38 | + |
| 39 | + |
| 40 | +def add_memory(): |
| 41 | + url = f"{BASE_URL}/add" |
| 42 | + data = { |
| 43 | + "user_id": USER_ID, |
| 44 | + "memory_content": "今天我在测试 MemOS 的记忆添加与检索流程。", |
| 45 | + "messages": [{"role": "user", "content": "我今天在做系统测试"}], |
| 46 | + "doc_path": None, |
| 47 | + "mem_cube_id": MEM_CUBE_ID, |
| 48 | + "source": "test_script", |
| 49 | + "user_profile": False, |
| 50 | + "session_id": SESSION_ID, |
| 51 | + } |
| 52 | + print("[*] Adding memory ...") |
| 53 | + resp = requests.post(url, headers=HEADERS, data=json.dumps(data), timeout=30) |
| 54 | + print(resp.status_code, resp.text) |
| 55 | + return resp.json() |
| 56 | + |
| 57 | + |
| 58 | +def search_memory(query="系统测试"): |
| 59 | + url = f"{BASE_URL}/search" |
| 60 | + data = { |
| 61 | + "user_id": USER_ID, |
| 62 | + "query": query, |
| 63 | + "mem_cube_id": MEM_CUBE_ID, |
| 64 | + "top_k": 5, |
| 65 | + "session_id": SESSION_ID, |
| 66 | + } |
| 67 | + print("[*] Searching memory ...") |
| 68 | + resp = requests.post(url, headers=HEADERS, data=json.dumps(data), timeout=30) |
| 69 | + print(resp.status_code, resp.text) |
| 70 | + return resp.json() |
| 71 | + |
| 72 | + |
| 73 | +def chat_stream(query: str, session_id: str, history: list | None = None): |
| 74 | + url = f"{BASE_URL}/chat" |
| 75 | + data = { |
| 76 | + "user_id": USER_ID, |
| 77 | + "query": query, |
| 78 | + "mem_cube_id": MEM_CUBE_ID, |
| 79 | + "history": history, |
| 80 | + "internet_search": False, |
| 81 | + "moscube": False, |
| 82 | + "session_id": session_id, |
| 83 | + } |
| 84 | + |
| 85 | + print("[*] Starting streaming chat ...") |
| 86 | + |
| 87 | + with requests.post(url, headers=HEADERS, data=json.dumps(data), stream=True) as resp: |
| 88 | + for raw_line in resp.iter_lines(): |
| 89 | + if not raw_line: |
| 90 | + continue |
| 91 | + line = raw_line.decode("utf-8", errors="ignore") |
| 92 | + |
| 93 | + payload = line.removeprefix("data: ").strip() |
| 94 | + if payload == "[DONE]": |
| 95 | + print("[done]") |
| 96 | + break |
| 97 | + |
| 98 | + try: |
| 99 | + msg = json.loads(payload) |
| 100 | + msg_type = msg.get("type") |
| 101 | + msg_data = msg.get("data") or msg.get("content") |
| 102 | + |
| 103 | + if msg_type == "text": |
| 104 | + print(msg_data, end="", flush=True) |
| 105 | + elif msg_type == "reference": |
| 106 | + print(f"\n[参考记忆] {msg_data}") |
| 107 | + elif msg_type == "status": |
| 108 | + pass |
| 109 | + elif msg_type == "suggestion": |
| 110 | + print(f"\n[建议] {msg_data}") |
| 111 | + elif msg_type == "end": |
| 112 | + print("\n[✅ Chat End]") |
| 113 | + else: |
| 114 | + print(f"\n[{msg_type}] {msg_data}") |
| 115 | + except Exception: |
| 116 | + try: |
| 117 | + print(payload.encode("latin-1").decode("utf-8"), end="") |
| 118 | + except Exception: |
| 119 | + print(payload) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + print("===== STEP 1: Register User =====") |
| 124 | + register_user() |
| 125 | + |
| 126 | + print("\n===== STEP 2: Add Memory =====") |
| 127 | + add_memory() |
| 128 | + |
| 129 | + print("\n===== STEP 3: Search Memory =====") |
| 130 | + search_memory() |
| 131 | + |
| 132 | + print("\n===== STEP 4: Stream Chat =====") |
| 133 | + chat_stream("我很开心,我今天吃了好吃的拉面", SESSION_ID, history=[]) |
| 134 | + chat_stream( |
| 135 | + "我刚和你说什么", |
| 136 | + SESSION_ID, |
| 137 | + history=[ |
| 138 | + {"role": "user", "content": "我很开心,我今天吃了好吃的拉面"}, |
| 139 | + {"role": "assistant", "content": "🉑"}, |
| 140 | + ], |
| 141 | + ) |
| 142 | + |
| 143 | + print("\n===== STEP 4: Stream Chat =====") |
| 144 | + chat_stream("我刚和你说什么了呢", SESSION_ID2, history=[]) |
0 commit comments