| 317 | |
| 318 | |
| 319 | class MemuClient: |
| 320 | def __init__(self): |
| 321 | from memu import MemuClient |
| 322 | |
| 323 | self.memu_client = MemuClient( |
| 324 | base_url="https://api.memu.so", api_key=os.getenv("MEMU_API_KEY") |
| 325 | ) |
| 326 | self.agent_id = "assistant_001" |
| 327 | |
| 328 | def add(self, messages, user_id, iso_date): |
| 329 | try: |
| 330 | response = self.memu_client.memorize_conversation( |
| 331 | conversation=messages, |
| 332 | user_id=user_id, |
| 333 | user_name=user_id, |
| 334 | agent_id=self.agent_id, |
| 335 | agent_name=self.agent_id, |
| 336 | session_date=iso_date, |
| 337 | ) |
| 338 | self.wait_for_completion(response.item_id) |
| 339 | except Exception as error: |
| 340 | print("❌ Error saving conversation:", error) |
| 341 | |
| 342 | def search(self, query, user_id, top_k): |
| 343 | user_memories = self.memu_client.retrieve_related_memory_items( |
| 344 | user_id=user_id, agent_id=self.agent_id, query=query, top_k=top_k, min_similarity=0.1 |
| 345 | ) |
| 346 | res = [m.memory.content for m in user_memories.related_memories] |
| 347 | return res |
| 348 | |
| 349 | def wait_for_completion(self, task_id): |
| 350 | while True: |
| 351 | status = self.memu_client.get_task_status(task_id) |
| 352 | if status.status in ["SUCCESS", "FAILURE", "REVOKED"]: |
| 353 | break |
| 354 | time.sleep(2) |
| 355 | |
| 356 | |
| 357 | if __name__ == "__main__": |
no outgoing calls
no test coverage detected