|
| 1 | +from memu.llm import OpenAIClient |
| 2 | +from memu.memory import MemoryAgent |
| 3 | +import json |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +import re |
| 7 | +import os |
| 8 | +import shutil |
| 9 | + |
| 10 | +character_test = None |
| 11 | + |
| 12 | +def load_locomo_conversation(sample_id: int, session_id: int|str|list, add_image_caption: bool = False, perserve_role: bool = True): |
| 13 | + |
| 14 | + try: |
| 15 | + with open(f"locomo/locomo10.json", "r") as f: |
| 16 | + data = json.load(f) |
| 17 | + |
| 18 | + sample = data[sample_id] |
| 19 | + conversation = sample["conversation"] |
| 20 | + |
| 21 | + speaker_a = conversation.get("speaker_a", "Speaker A") |
| 22 | + speaker_b = conversation.get("speaker_b", "Speaker B") |
| 23 | + speakers = [speaker_a, speaker_b] |
| 24 | + |
| 25 | + if not perserve_role: |
| 26 | + remap_role = { |
| 27 | + speaker_a: "user", |
| 28 | + speaker_b: "assistant" |
| 29 | + } |
| 30 | + |
| 31 | + if isinstance(session_id, int): |
| 32 | + session_ids = [session_id] |
| 33 | + elif isinstance(session_id, str): |
| 34 | + session_ids = [int(session_id)] |
| 35 | + elif isinstance(session_id, list): |
| 36 | + session_ids = [int(sid) for sid in session_id] |
| 37 | + |
| 38 | + conversation_messages = [] |
| 39 | + for session_id in session_ids: |
| 40 | + conversation_single = [] |
| 41 | + if f"session_{session_id+1}" in conversation: |
| 42 | + session = conversation[f"session_{session_id+1}"] |
| 43 | + |
| 44 | + for message in session: |
| 45 | + speaker = message["speaker"] |
| 46 | + content = message["text"] |
| 47 | + |
| 48 | + if not perserve_role: |
| 49 | + speaker = remap_role.get(speaker, "Unknown") |
| 50 | + |
| 51 | + if add_image_caption and "blip_caption" in message: |
| 52 | + content = f"({speaker} shares {message['blip_caption']}.) {content}" |
| 53 | + |
| 54 | + conversation_single.append({ |
| 55 | + "role": speaker, |
| 56 | + "content": content |
| 57 | + }) |
| 58 | + conversation_messages.append(conversation_single) |
| 59 | + |
| 60 | + return conversation_messages, speakers |
| 61 | + |
| 62 | + except Exception as e: |
| 63 | + raise |
| 64 | + |
| 65 | +def load_debug_conversation(file_name: str): |
| 66 | + if '.' not in file_name: |
| 67 | + file_name = f"{file_name}.txt" |
| 68 | + with open(f"debug/{file_name}", "r") as f: |
| 69 | + raw = f.readlines() |
| 70 | + |
| 71 | + conversation = [] |
| 72 | + speakers = set() |
| 73 | + for line in raw: |
| 74 | + if ": " in line: |
| 75 | + role, content = line.split(": ", 1) |
| 76 | + speakers.add(role) |
| 77 | + conversation.append({"role": role, "content": content.strip()}) |
| 78 | + |
| 79 | + return conversation, list(speakers) |
| 80 | + |
| 81 | +def process_conversation(conversation: list[dict] = []): |
| 82 | + """Process conversation with memory agent""" |
| 83 | + |
| 84 | + # Initialize LLM client |
| 85 | + llm_client = OpenAIClient(model="gpt-4o-mini") |
| 86 | + memory_agent = MemoryAgent(llm_client=llm_client, memory_dir="memory") |
| 87 | + |
| 88 | + # Process conversation |
| 89 | + result = memory_agent.run( |
| 90 | + conversation=conversation, |
| 91 | + character_name=character_test or conversation[0]["role"], |
| 92 | + max_iterations=20 |
| 93 | + ) |
| 94 | + |
| 95 | + if result.get("success"): |
| 96 | + print(f"✅ Processing completed - Iterations: {result.get('iterations', 0)}") |
| 97 | + print(f"📁 Files generated: {len(result.get('files_generated', []))}") |
| 98 | + else: |
| 99 | + print(f"❌ Processing failed: {result.get('error')}") |
| 100 | + |
| 101 | + return result |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + print("🌟 MEMORY AGENT DEMONSTRATION") |
| 105 | + print("Loading and processing conversations...") |
| 106 | + |
| 107 | + conversations, speakers = load_locomo_conversation(sample_id=2, session_id=[0,1], add_image_caption=True, perserve_role=True) |
| 108 | + character_test = speakers[1] |
| 109 | + |
| 110 | + print(f"Loaded {len(conversations)} conversations for character: {character_test}") |
| 111 | + |
| 112 | + # Process each conversation |
| 113 | + for i, conversation in enumerate(conversations): |
| 114 | + print(f"\n🔄 Processing conversation {i+1} of {len(conversations)}") |
| 115 | + process_conversation(conversation) |
| 116 | + |
| 117 | + print("\n✅ All conversations processed.") |
0 commit comments