Process a single sample and return results.
(sample_data: tuple, model: str, backend: str, retrieve_k: int,
temperature_c5: float, sglang_host: str, sglang_port: int,
memories_dir: str, allow_categories: list)
| 228 | return logger |
| 229 | |
| 230 | def process_single_sample(sample_data: tuple, model: str, backend: str, retrieve_k: int, |
| 231 | temperature_c5: float, sglang_host: str, sglang_port: int, |
| 232 | memories_dir: str, allow_categories: list) -> dict: |
| 233 | """Process a single sample and return results.""" |
| 234 | sample_idx, sample = sample_data |
| 235 | |
| 236 | # Create agent for this sample |
| 237 | agent = advancedMemAgent(model, backend, retrieve_k, temperature_c5, sglang_host, sglang_port) |
| 238 | |
| 239 | # Create memory cache filename based on sample index |
| 240 | memory_cache_file = os.path.join( |
| 241 | memories_dir, |
| 242 | f"memory_cache_sample_{sample_idx}.pkl" |
| 243 | ) |
| 244 | retriever_cache_file = os.path.join( |
| 245 | memories_dir, |
| 246 | f"retriever_cache_sample_{sample_idx}.pkl" |
| 247 | ) |
| 248 | retriever_cache_embeddings_file = os.path.join( |
| 249 | memories_dir, |
| 250 | f"retriever_cache_embeddings_sample_{sample_idx}.npy" |
| 251 | ) |
| 252 | |
| 253 | # Check if cached memories exist |
| 254 | if os.path.exists(memory_cache_file): |
| 255 | print(f"[Sample {sample_idx}] Loading cached memories") |
| 256 | with open(memory_cache_file, 'rb') as f: |
| 257 | cached_memories = pickle.load(f) |
| 258 | # Restore memories to agent |
| 259 | agent.memory_system.memories = cached_memories |
| 260 | if os.path.exists(retriever_cache_file): |
| 261 | print(f"[Sample {sample_idx}] Found retriever cache files") |
| 262 | agent.memory_system.retriever = agent.memory_system.retriever.load( |
| 263 | retriever_cache_file, retriever_cache_embeddings_file |
| 264 | ) |
| 265 | else: |
| 266 | print(f"[Sample {sample_idx}] No retriever cache found, loading from memory") |
| 267 | agent.memory_system.retriever = agent.memory_system.retriever.load_from_local_memory( |
| 268 | cached_memories, |
| 269 | 'all-MiniLM-L6-v2' |
| 270 | ) |
| 271 | print(f"[Sample {sample_idx}] Successfully loaded {len(cached_memories)} memories") |
| 272 | else: |
| 273 | print(f"[Sample {sample_idx}] No cached memories found. Creating new memories.") |
| 274 | |
| 275 | for _, turns in sample.conversation.sessions.items(): |
| 276 | for turn in turns.turns: |
| 277 | turn_datatime = turns.date_time |
| 278 | conversation_tmp = "Speaker " + turn.speaker + "says : " + turn.text |
| 279 | agent.add_memory(conversation_tmp, time=turn_datatime) |
| 280 | |
| 281 | memories_to_cache = agent.memory_system.memories |
| 282 | with open(memory_cache_file, 'wb') as f: |
| 283 | pickle.dump(memories_to_cache, f) |
| 284 | agent.memory_system.retriever.save(retriever_cache_file, retriever_cache_embeddings_file) |
| 285 | print(f"[Sample {sample_idx}] Successfully cached {len(memories_to_cache)} memories") |
| 286 | |
| 287 | # Process questions for this sample |
nothing calls this directly
no test coverage detected