-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy patheval_supermemory.py
More file actions
349 lines (270 loc) · 9.92 KB
/
eval_supermemory.py
File metadata and controls
349 lines (270 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import os
import re
import time
import math
import json
import copy
import requests
import traceback
from datetime import datetime, timezone
from dotenv import load_dotenv
from concurrent.futures import ProcessPoolExecutor, as_completed
from tqdm import tqdm
from supermemory import Supermemory, SupermemoryError, NotFoundError
from tenacity import (
retry,
retry_if_exception_type,
stop_after_attempt,
wait_fixed
)
from llms import llm_request
from prompts import PROMPT_MEMOS
TEMPLATE_SUPERMEMORY = """Memories for user {user_id}:
{memories}
"""
load_dotenv()
RETRY_TIMES = 10
WAIT_TIME = 30
client = Supermemory(
api_key=os.getenv("SUPERMEMORY_API_KEY")
)
@retry(
retry=retry_if_exception_type(Exception),
wait=wait_fixed(WAIT_TIME),
stop=stop_after_attempt(RETRY_TIMES),
reraise=True
)
def add_memory(client, user_id, dialogues, conv_id, batch_size=20):
start = time.time()
response_id_ls = []
dialogue_len = len(dialogues)
for i in range(0, dialogue_len, batch_size):
content = "\n".join(
[
f"[{turn['timestamp']}]{turn['role']}: {turn['content']}" for turn in dialogues[i:i+batch_size]
]
)
response = client.memories.add(
content=content,
container_tag=user_id,
metadata={
"conv_id": conv_id
}
)
response_id_ls.append(response.id)
duration_ms = (time.time() - start) * 1000
return response_id_ls, duration_ms
@retry(
retry=retry_if_exception_type((Exception, SupermemoryError, NotFoundError)),
wait=wait_fixed(WAIT_TIME),
stop=stop_after_attempt(RETRY_TIMES),
reraise=True
)
def get_memory_from_response_id(client, response_id):
start = time.time()
response = client.memories.get(response_id)
while response.status != 'done':
time.sleep(5)
response = client.memories.get(response.id)
if response.status == 'failed':
return (time.time() - start) * 1000, []
duration_ms_1 = (time.time() - start) * 1000
max_retries = 10
for attempt in range(max_retries + 1):
time.sleep(6)
response = client.memories.get(response.id)
memories=[
item["memory"] for item in response.model_dump().get("memories", [])
]
if memories:
break
duration_ms_2 = (time.time() - start) * 1000
return (duration_ms_1, memories) if memories else (duration_ms_2, [])
@retry(
retry=retry_if_exception_type(Exception),
wait=wait_fixed(WAIT_TIME),
stop=stop_after_attempt(RETRY_TIMES),
reraise=True
)
def search_memory(
client,
query,
user_id,
top_k=20
):
start = time.time()
memories = client.search.memories(
q=query,
limit=top_k,
rerank=True,
rewrite_query=True,
container_tag=user_id,
threshold=0.7,
)
memories = [
item["memory"] for item in memories.model_dump().get("results")
]
context = TEMPLATE_SUPERMEMORY.format(
user_id=user_id,
memories="\n".join(memories)
)
duration_ms = (time.time() - start) * 1000
return context, memories, duration_ms
def extract_user_name(persona_info: str):
match = re.search(r'Name:\s*(.*?); Gender:', persona_info)
if match:
username = match.group(1).strip()
return username
else:
raise ValueError("No name found.")
def process_user(user_data, top_k, save_path, version):
user_name = extract_user_name(user_data["persona_info"]) + f"_{version}"
user_name = user_name.replace(" ", "_")
sessions = user_data["sessions"]
tmp_dir = os.path.join(save_path, "tmp")
os.makedirs(tmp_dir, exist_ok=True)
tmp_file = os.path.join(tmp_dir, f"{user_data['uuid']}.json")
new_user_data = {
"uuid": user_data["uuid"],
"user_name": user_name,
"sessions": []
}
try:
session_id = 0
for session in tqdm(sessions, total=len(sessions), desc=f"Processing user {user_name}"):
session_id += 1
new_session = {
"memory_points": session["memory_points"],
"dialogue": session["dialogue"]
}
# add messages
dialogue = session["dialogue"]
conv_id = f"{session_id}_{user_name}"
response_id_ls, duration_ms = add_memory(
client=client,
user_id=user_name,
dialogues=dialogue,
conv_id=conv_id,
batch_size=20
)
if session.get('is_generated_qa_session', False):
new_session["add_dialogue_duration_ms"] = duration_ms
new_session["is_generated_qa_session"] = True
del new_session["dialogue"]
del new_session["memory_points"]
new_user_data["sessions"].append(new_session)
continue
extracted_memories = []
for response_id in response_id_ls:
try:
get_memory_time, memories = get_memory_from_response_id(client, response_id)
except Exception as e:
traceback.print_exc()
get_memory_time, memories = 0, []
duration_ms += get_memory_time
extracted_memories.extend(memories)
new_session["response_id_ls"] = response_id_ls
new_session["extracted_memories"] = extracted_memories
new_session["add_dialogue_duration_ms"] = duration_ms
# search updated memories
for memory in new_session["memory_points"]:
if memory["is_update"] == "False" or not memory["original_memories"]:
continue
_, memories_from_system, duration_ms = search_memory(
client=client,
query=memory["memory_content"],
user_id=user_name,
top_k=10
)
memory["memories_from_system"] = memories_from_system
# search and query
if "questions" not in session:
new_user_data["sessions"].append(new_session)
continue
new_session["questions"] = []
for qa in session["questions"]:
context, _, duration_ms = search_memory(
client=client,
query=qa["question"],
user_id=user_name,
top_k=top_k
)
new_qa = copy.deepcopy(qa)
new_qa["context"] = context
new_qa["search_duration_ms"] = duration_ms
prompt = PROMPT_MEMOS.format(
context=context,
question=qa["question"]
)
start_time = time.time()
response = llm_request(prompt)
new_qa["system_response"] = response
new_qa["response_duration_ms"] = (time.time() - start_time) * 1000
new_session["questions"].append(new_qa)
new_user_data["sessions"].append(new_session)
with open(tmp_file, "w", encoding="utf-8") as f:
json.dump(new_user_data, f, ensure_ascii=False)
print(f"✅ Saved user {user_name} to {tmp_file}")
return {"uuid": user_data["uuid"], "status": "ok", "path": tmp_file}
except Exception as e:
error_path = os.path.join(tmp_dir, f"{user_data['uuid']}_error.log")
with open(error_path, "w", encoding="utf-8") as f:
f.write(traceback.format_exc())
print(f"❌ Error in user {user_name}: {e}")
return {"uuid": user_data["uuid"], "status": "error", "path": error_path}
def iter_jsonl(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line:
yield json.loads(line)
def main(
data_path: str,
version: str = "default",
top_k: int = 20,
max_workers: int = 2
):
frame = "supermemory"
save_path = f"results/{frame}-{version}/"
os.makedirs(save_path, exist_ok=True)
output_file = os.path.join(save_path, f"{frame}_eval_results.jsonl")
tmp_dir = os.path.join(save_path, "tmp")
os.makedirs(tmp_dir, exist_ok=True)
start_time = time.time()
with ProcessPoolExecutor(max_workers=max_workers) as executor:
futures = {}
for idx, user_data in enumerate(iter_jsonl(data_path), 1):
uuid = user_data["uuid"]
future = executor.submit(process_user, user_data, top_k, save_path, version)
futures[future] = uuid
total_users = idx
for i, future in enumerate(as_completed(futures), 1):
uuid = futures[future]
try:
result = future.result()
print(f"[{i}/{total_users}] ✅ Finished {uuid} ({result['status']})")
except Exception as e:
print(f"[{i}/{total_users}] ❌ Error processing {uuid}: {e}")
traceback.print_exc()
with open(output_file, "a", encoding="utf-8") as f_out:
for file in os.listdir(tmp_dir):
if file.endswith(".json"):
file_path = os.path.join(tmp_dir, file)
try:
with open(file_path, "r", encoding="utf-8") as f_in:
data = json.load(f_in)
f_out.write(json.dumps(data, ensure_ascii=False) + "\n")
except Exception as e:
print(f"⚠️ Skipped {file}: {e}")
elapsed = time.time() - start_time
print(f"✅ All done in {elapsed:.2f}s")
print(f"✅ Final results saved to: {output_file}")
if __name__ == "__main__":
data_path = "../data/HaluMem-Medium.jsonl"
version = "default"
top_k = 20
main(
data_path=data_path,
version=version,
top_k=top_k
)