Spaces:
Sleeping
Sleeping
EMMA commited on
Commit ·
00f82e4
1
Parent(s): cd73026
update code
Browse files- utils/memory_utils.py +216 -26
utils/memory_utils.py
CHANGED
|
@@ -6,6 +6,8 @@ import datetime
|
|
| 6 |
import shutil
|
| 7 |
import gradio as gr
|
| 8 |
from pprint import pprint
|
|
|
|
|
|
|
| 9 |
import traceback
|
| 10 |
|
| 11 |
# LlamaIndex Imports
|
|
@@ -107,13 +109,75 @@ def enter_name(name, memory, local_memory_qa, data_args, update_memory_index=Tru
|
|
| 107 |
memory[name].update({"name": name})
|
| 108 |
msg = f"Welcome, new user {name}! I will remember your name, so next time we meet, I'll be able to call you by your name!"
|
| 109 |
return msg, memory[name], memory, name, user_memory_index
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
|
|
|
| 111 |
|
| 112 |
-
def enter_name_llamaindex(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
"""
|
| 114 |
Load the user's session, episodic, and semantic memory indices.
|
| 115 |
Compatible with LlamaIndex v0.10+.
|
| 116 |
"""
|
|
|
|
| 117 |
sessions_memory = None
|
| 118 |
episodic_memory = None
|
| 119 |
semantic_memory = None
|
|
@@ -125,12 +189,25 @@ def enter_name_llamaindex(name, memory, data_args, update_memory_index=True):
|
|
| 125 |
return "User not found.", None, None, None, None
|
| 126 |
|
| 127 |
user_memory = memory[name]
|
|
|
|
| 128 |
# مسیر دقیق کاربر در پوشه llamaindex
|
| 129 |
base_path = os.path.join(_LLAMAINDEX_BASE_DIR, name)
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
print(f"[DEBUG] base_path: {base_path}")
|
| 135 |
print(f"[DEBUG] sessions_path: {sessions_path}")
|
| 136 |
print(f"[DEBUG] episodic_path: {episodic_path}")
|
|
@@ -143,49 +220,164 @@ def enter_name_llamaindex(name, memory, data_args, update_memory_index=True):
|
|
| 143 |
)
|
| 144 |
|
| 145 |
print(f"[DEBUG] indices_exist: {indices_exist}")
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
if update_memory_index or not indices_exist:
|
| 149 |
-
print(f"[DEBUG] Initializing memory indices for {name}...")
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
print("[DEBUG] After build:")
|
| 155 |
-
|
| 156 |
-
print(
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
def load_index_safe(index_name, index_path):
|
|
|
|
| 160 |
if not os.path.isdir(index_path):
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
return None
|
| 163 |
|
| 164 |
try:
|
|
|
|
| 165 |
storage_context = StorageContext.from_defaults(
|
| 166 |
persist_dir=index_path
|
| 167 |
)
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
print(
|
| 170 |
f"[DEBUG] {index_name} loaded: "
|
| 171 |
-
f"{type(index).__name__},
|
|
|
|
| 172 |
)
|
|
|
|
| 173 |
return index
|
|
|
|
| 174 |
except Exception as exc:
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
traceback.print_exc()
|
|
|
|
| 177 |
return None
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
print("[DEBUG] RETURN VALUES:")
|
| 184 |
-
|
| 185 |
-
print(
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
return (
|
| 191 |
f"Welcome back, {name}!",
|
|
@@ -194,8 +386,6 @@ def enter_name_llamaindex(name, memory, data_args, update_memory_index=True):
|
|
| 194 |
episodic_memory,
|
| 195 |
semantic_memory,
|
| 196 |
)
|
| 197 |
-
|
| 198 |
-
|
| 199 |
def summarize_memory_event_personality(data_args, memory, user_name):
|
| 200 |
"""
|
| 201 |
Summarizes the memory and returns the user-specific memory dict.
|
|
|
|
| 6 |
import shutil
|
| 7 |
import gradio as gr
|
| 8 |
from pprint import pprint
|
| 9 |
+
from huggingface_hub import HfApi
|
| 10 |
+
|
| 11 |
import traceback
|
| 12 |
|
| 13 |
# LlamaIndex Imports
|
|
|
|
| 109 |
memory[name].update({"name": name})
|
| 110 |
msg = f"Welcome, new user {name}! I will remember your name, so next time we meet, I'll be able to call you by your name!"
|
| 111 |
return msg, memory[name], memory, name, user_memory_index
|
| 112 |
+
def upload_user_indices_to_hf(name):
|
| 113 |
+
local_dir = os.path.join(_LLAMAINDEX_BASE_DIR, name)
|
| 114 |
+
remote_dir = f"memory_index/llamaindex/{name}"
|
| 115 |
+
|
| 116 |
+
if not os.path.isdir(local_dir):
|
| 117 |
+
raise FileNotFoundError(
|
| 118 |
+
f"Index directory does not exist: {local_dir}"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
files_count = sum(
|
| 122 |
+
len(files)
|
| 123 |
+
for _, _, files in os.walk(local_dir)
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
if files_count == 0:
|
| 127 |
+
raise RuntimeError(
|
| 128 |
+
f"No index files found for upload: {local_dir}"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
token = os.environ.get("HF_TOKEN")
|
| 132 |
+
if not token:
|
| 133 |
+
raise RuntimeError(
|
| 134 |
+
"Hugging Face token is missing; index upload cannot run."
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
print(
|
| 138 |
+
f"[HF] Uploading {files_count} index files\n"
|
| 139 |
+
f"[HF] Local: {local_dir}\n"
|
| 140 |
+
f"[HF] Dataset: {REPO_ID}\n"
|
| 141 |
+
f"[HF] Remote: {remote_dir}",
|
| 142 |
+
flush=True,
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
api = HfApi(token=token)
|
| 146 |
+
|
| 147 |
+
try:
|
| 148 |
+
commit_info = api.upload_folder(
|
| 149 |
+
repo_id=REPO_ID,
|
| 150 |
+
repo_type="dataset",
|
| 151 |
+
folder_path=local_dir,
|
| 152 |
+
path_in_repo=remote_dir,
|
| 153 |
+
commit_message=f"Upload memory indices for {name}",
|
| 154 |
+
)
|
| 155 |
+
except Exception:
|
| 156 |
+
print(
|
| 157 |
+
f"[HF] Index upload FAILED for {name}",
|
| 158 |
+
flush=True,
|
| 159 |
+
)
|
| 160 |
+
raise
|
| 161 |
+
|
| 162 |
+
print(
|
| 163 |
+
f"[HF] Index upload completed for {name}. "
|
| 164 |
+
f"Commit: {commit_info.oid}",
|
| 165 |
+
flush=True,
|
| 166 |
+
)
|
| 167 |
|
| 168 |
+
return commit_info
|
| 169 |
|
| 170 |
+
def enter_name_llamaindex(
|
| 171 |
+
name,
|
| 172 |
+
memory,
|
| 173 |
+
data_args,
|
| 174 |
+
update_memory_index=True
|
| 175 |
+
):
|
| 176 |
"""
|
| 177 |
Load the user's session, episodic, and semantic memory indices.
|
| 178 |
Compatible with LlamaIndex v0.10+.
|
| 179 |
"""
|
| 180 |
+
|
| 181 |
sessions_memory = None
|
| 182 |
episodic_memory = None
|
| 183 |
semantic_memory = None
|
|
|
|
| 189 |
return "User not found.", None, None, None, None
|
| 190 |
|
| 191 |
user_memory = memory[name]
|
| 192 |
+
|
| 193 |
# مسیر دقیق کاربر در پوشه llamaindex
|
| 194 |
base_path = os.path.join(_LLAMAINDEX_BASE_DIR, name)
|
| 195 |
+
|
| 196 |
+
sessions_path = os.path.join(
|
| 197 |
+
base_path,
|
| 198 |
+
"sessions"
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
episodic_path = os.path.join(
|
| 202 |
+
base_path,
|
| 203 |
+
"episodic_memory"
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
semantic_path = os.path.join(
|
| 207 |
+
base_path,
|
| 208 |
+
"semantic_memory"
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
print(f"[DEBUG] base_path: {base_path}")
|
| 212 |
print(f"[DEBUG] sessions_path: {sessions_path}")
|
| 213 |
print(f"[DEBUG] episodic_path: {episodic_path}")
|
|
|
|
| 220 |
)
|
| 221 |
|
| 222 |
print(f"[DEBUG] indices_exist: {indices_exist}")
|
| 223 |
+
|
| 224 |
+
# ============================================================
|
| 225 |
+
# Build / Update indices
|
| 226 |
+
# ============================================================
|
| 227 |
|
| 228 |
if update_memory_index or not indices_exist:
|
|
|
|
| 229 |
|
| 230 |
+
print(
|
| 231 |
+
f"[DEBUG] Initializing memory indices "
|
| 232 |
+
f"for {name!r}..."
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
# 1. Build and persist LlamaIndex indices
|
| 236 |
+
build_memory_index(
|
| 237 |
+
memory,
|
| 238 |
+
data_args,
|
| 239 |
+
name=name
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
# ========================================================
|
| 243 |
+
# 2. Upload persisted indices to Hugging Face Dataset
|
| 244 |
+
# ========================================================
|
| 245 |
+
|
| 246 |
+
try:
|
| 247 |
+
print(
|
| 248 |
+
f"[DEBUG] Uploading persisted indices "
|
| 249 |
+
f"for user {name!r} to Hugging Face..."
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
upload_user_indices_to_hf(name)
|
| 253 |
+
|
| 254 |
+
print(
|
| 255 |
+
f"[DEBUG] Successfully uploaded indices "
|
| 256 |
+
f"for {name!r}."
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
except Exception as exc:
|
| 260 |
+
|
| 261 |
+
print(
|
| 262 |
+
f"[ERROR] Failed to upload indices "
|
| 263 |
+
f"for {name!r}: {exc}"
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
traceback.print_exc()
|
| 267 |
+
|
| 268 |
+
# ========================================================
|
| 269 |
+
# 3. Verify local persisted directories
|
| 270 |
+
# ========================================================
|
| 271 |
|
| 272 |
print("[DEBUG] After build:")
|
| 273 |
+
|
| 274 |
+
print(
|
| 275 |
+
f"[DEBUG] sessions_path exists: "
|
| 276 |
+
f"{os.path.isdir(sessions_path)}"
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
print(
|
| 280 |
+
f"[DEBUG] episodic_path exists: "
|
| 281 |
+
f"{os.path.isdir(episodic_path)}"
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
print(
|
| 285 |
+
f"[DEBUG] semantic_path exists: "
|
| 286 |
+
f"{os.path.isdir(semantic_path)}"
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
# ============================================================
|
| 290 |
+
# Safe index loading
|
| 291 |
+
# ============================================================
|
| 292 |
|
| 293 |
def load_index_safe(index_name, index_path):
|
| 294 |
+
|
| 295 |
if not os.path.isdir(index_path):
|
| 296 |
+
|
| 297 |
+
print(
|
| 298 |
+
f"[DEBUG] {index_name} path does not exist: "
|
| 299 |
+
f"{index_path}"
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
return None
|
| 303 |
|
| 304 |
try:
|
| 305 |
+
|
| 306 |
storage_context = StorageContext.from_defaults(
|
| 307 |
persist_dir=index_path
|
| 308 |
)
|
| 309 |
+
|
| 310 |
+
index = load_index_from_storage(
|
| 311 |
+
storage_context
|
| 312 |
+
)
|
| 313 |
+
|
| 314 |
print(
|
| 315 |
f"[DEBUG] {index_name} loaded: "
|
| 316 |
+
f"{type(index).__name__}, "
|
| 317 |
+
f"is_none={index is None}"
|
| 318 |
)
|
| 319 |
+
|
| 320 |
return index
|
| 321 |
+
|
| 322 |
except Exception as exc:
|
| 323 |
+
|
| 324 |
+
print(
|
| 325 |
+
f"[ERROR] Could not load "
|
| 326 |
+
f"{index_name}: {exc}"
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
traceback.print_exc()
|
| 330 |
+
|
| 331 |
return None
|
| 332 |
|
| 333 |
+
# ============================================================
|
| 334 |
+
# Load all three indices
|
| 335 |
+
# ============================================================
|
| 336 |
+
|
| 337 |
+
sessions_memory = load_index_safe(
|
| 338 |
+
"sessions_memory",
|
| 339 |
+
sessions_path
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
episodic_memory = load_index_safe(
|
| 343 |
+
"episodic_memory",
|
| 344 |
+
episodic_path
|
| 345 |
+
)
|
| 346 |
+
|
| 347 |
+
semantic_memory = load_index_safe(
|
| 348 |
+
"semantic_memory",
|
| 349 |
+
semantic_path
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
# ============================================================
|
| 353 |
+
# Debug information
|
| 354 |
+
# ============================================================
|
| 355 |
|
| 356 |
print("[DEBUG] RETURN VALUES:")
|
| 357 |
+
|
| 358 |
+
print(
|
| 359 |
+
f" hello_msg: Welcome back, {name}!"
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
print(
|
| 363 |
+
f" user_memory type: "
|
| 364 |
+
f"{type(user_memory).__name__}"
|
| 365 |
+
)
|
| 366 |
+
|
| 367 |
+
print(
|
| 368 |
+
f" sessions_memory type: "
|
| 369 |
+
f"{type(sessions_memory).__name__}"
|
| 370 |
+
)
|
| 371 |
+
|
| 372 |
+
print(
|
| 373 |
+
f" episodic_memory type: "
|
| 374 |
+
f"{type(episodic_memory).__name__}"
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
print(
|
| 378 |
+
f" semantic_memory type: "
|
| 379 |
+
f"{type(semantic_memory).__name__}"
|
| 380 |
+
)
|
| 381 |
|
| 382 |
return (
|
| 383 |
f"Welcome back, {name}!",
|
|
|
|
| 386 |
episodic_memory,
|
| 387 |
semantic_memory,
|
| 388 |
)
|
|
|
|
|
|
|
| 389 |
def summarize_memory_event_personality(data_args, memory, user_name):
|
| 390 |
"""
|
| 391 |
Summarizes the memory and returns the user-specific memory dict.
|