Skip to content

Commit 0ca7b67

Browse files
committed
feat: add product_api examples
1 parent afec8b6 commit 0ca7b67

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

examples/api/__init__.py

Whitespace-only changes.

examples/api/product_api.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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:8004/product"
16+
HEADERS = {"Content-Type": "application/json"}
17+
18+
USER_ID = "memos_user_id"
19+
USER_NAME = "memos_user_alice"
20+
MEM_CUBE_ID = "memos_cube_id_01"
21+
SESSION_ID = "memos_session_id_01"
22+
23+
24+
def register_user():
25+
url = f"{BASE_URL}/users/register"
26+
data = {
27+
"user_id": USER_ID,
28+
"user_name": USER_NAME,
29+
"interests": "memory,retrieval,test",
30+
"mem_cube_id": MEM_CUBE_ID,
31+
}
32+
print(f"[*] Registering user {USER_ID} ...")
33+
resp = requests.post(url, headers=HEADERS, data=json.dumps(data), timeout=30)
34+
print(resp.status_code, resp.text)
35+
return resp.json()
36+
37+
38+
def add_memory():
39+
url = f"{BASE_URL}/add"
40+
data = {
41+
"user_id": USER_ID,
42+
"memory_content": "今天我在测试 MemOS 的记忆添加与检索流程。",
43+
"messages": [{"role": "user", "content": "我今天在做系统测试"}],
44+
"doc_path": None,
45+
"mem_cube_id": MEM_CUBE_ID,
46+
"source": "test_script",
47+
"user_profile": False,
48+
"session_id": SESSION_ID,
49+
}
50+
print("[*] Adding memory ...")
51+
resp = requests.post(url, headers=HEADERS, data=json.dumps(data), timeout=30)
52+
print(resp.status_code, resp.text)
53+
return resp.json()
54+
55+
56+
def search_memory(query="系统测试"):
57+
url = f"{BASE_URL}/search"
58+
data = {
59+
"user_id": USER_ID,
60+
"query": query,
61+
"mem_cube_id": MEM_CUBE_ID,
62+
"top_k": 5,
63+
"session_id": SESSION_ID,
64+
}
65+
print("[*] Searching memory ...")
66+
resp = requests.post(url, headers=HEADERS, data=json.dumps(data), timeout=30)
67+
print(resp.status_code, resp.text)
68+
return resp.json()
69+
70+
71+
def chat_stream(query="总结一下我今天做的事"):
72+
url = f"{BASE_URL}/chat"
73+
data = {
74+
"user_id": USER_ID,
75+
"query": query,
76+
"mem_cube_id": MEM_CUBE_ID,
77+
"history": [],
78+
"internet_search": False,
79+
"moscube": False,
80+
"session_id": SESSION_ID,
81+
}
82+
83+
print("[*] Starting streaming chat ...")
84+
85+
with requests.post(url, headers=HEADERS, data=json.dumps(data), stream=True) as resp:
86+
for raw_line in resp.iter_lines():
87+
if not raw_line:
88+
continue
89+
line = raw_line.decode("utf-8", errors="ignore")
90+
91+
payload = line.removeprefix("data: ").strip()
92+
if payload == "[DONE]":
93+
print("[done]")
94+
break
95+
96+
try:
97+
msg = json.loads(payload)
98+
msg_type = msg.get("type")
99+
msg_data = msg.get("data") or msg.get("content")
100+
101+
if msg_type == "text":
102+
print(msg_data, end="", flush=True)
103+
elif msg_type == "reference":
104+
print(f"\n[参考记忆] {msg_data}")
105+
elif msg_type == "status":
106+
pass
107+
elif msg_type == "suggestion":
108+
print(f"\n[建议] {msg_data}")
109+
elif msg_type == "end":
110+
print("\n[✅ Chat End]")
111+
else:
112+
print(f"\n[{msg_type}] {msg_data}")
113+
except Exception:
114+
try:
115+
print(payload.encode("latin-1").decode("utf-8"), end="")
116+
except Exception:
117+
print(payload)
118+
119+
120+
if __name__ == "__main__":
121+
print("===== STEP 1: Register User =====")
122+
register_user()
123+
124+
print("\n===== STEP 2: Add Memory =====")
125+
add_memory()
126+
127+
print("\n===== STEP 3: Search Memory =====")
128+
search_memory()
129+
130+
print("\n===== STEP 4: Stream Chat =====")
131+
chat_stream()

0 commit comments

Comments
 (0)