Add files via upload

This commit is contained in:
motongt 2025-12-10 16:33:14 +08:00 committed by GitHub
parent 60fb1ffc5e
commit 900044fdb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 67641 additions and 0 deletions

9
config.yaml.example Normal file
View file

@ -0,0 +1,9 @@
# Memory Chain Configuration Example
# ===================================
# Copy this file to config.yaml and fill in your settings
model:
llm_model: "model_name" # LLM model name
openai_api_key: "your-openai-api-key-here" # Your OpenAI API key
openai_base_url: "https://api.openai.com/v1" # OpenAI API base URL (change if using proxy)

480
example_usage.py Normal file
View file

@ -0,0 +1,480 @@
#!/usr/bin/env python
# coding=utf-8
# Copyright 2025 The OPPO Personal AI team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import argparse
import os
import yaml
import torch
from openai import AsyncOpenAI
from sentence_transformers import SentenceTransformer
from memory_chain import MemoryChain, MemoryManager
# ==================== Configuration Class ====================
class MemoryConfig:
"""Memory system configuration"""
def __init__(
self,
working_memory_max_size: int = 20,
episodic_memory_refresh_rate: int = 5,
output_dir: str = "./memory_storage",
number_of_retrieval_pieces: int = 15, # Must be >= 10 (internal calculation uses top_k-9)
drop_threshold: float = 0.1,
):
self.working_memory_max_size = working_memory_max_size
self.episodic_memory_refresh_rate = episodic_memory_refresh_rate
self.output_dir = output_dir
self.number_of_retrieval_pieces = max(number_of_retrieval_pieces, 10) # Ensure at least 10
self.drop_threshold = drop_threshold
# ==================== Main Usage Example ====================
async def main():
"""
Memory Chain Usage Example
"""
# ========== 1. Configuration Parameters ==========
# Read config file (or set directly)
try:
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
except FileNotFoundError:
# If no config file, use default configuration
config = {
"model": {
"llm_model": "gpt-4o-mini",
"openai_api_key": "your-api-key-here",
"openai_base_url": "https://api.openai.com/v1"
}
}
# Initialize OpenAI client
client = AsyncOpenAI(
base_url=config["model"]["openai_base_url"],
api_key=config["model"]["openai_api_key"]
)
# Load Embedding model
print("Loading Embedding model...")
device = "cuda" if torch.cuda.is_available() else "cpu"
embedding_model = SentenceTransformer("all-MiniLM-L6-v2").to(device)
print(f"Embedding model loaded, using device: {device}")
# Memory system configuration
# Note: working_memory_max_size determines the working memory capacity
# When message count reaches this value, old messages are transferred to episodic memory
memory_config = MemoryConfig(
working_memory_max_size=5, # Max working memory capacity (small value to trigger memory transfer quickly)
episodic_memory_refresh_rate=3, # Episodic memory refresh rate
output_dir="./memory_storage", # Memory storage directory
number_of_retrieval_pieces=15, # Number of retrieval pieces (must be >= 10, internal uses top_k-9)
drop_threshold=0.1 # Similarity threshold
)
# Ensure storage directory exists
os.makedirs(memory_config.output_dir, exist_ok=True)
# ========== 2. Initialize Memory System ==========
user_name = "Alice" # User name
agent_name = "Bob" # Agent/assistant name
memory_index = 0 # Memory index (for multi-session scenarios)
# Create MemoryChain (core memory system)
memory_system = MemoryChain(
memory_index=memory_index,
llm_model=config["model"]["llm_model"],
llm_client=client,
embedding_model=embedding_model,
user_name=user_name,
agent_name=agent_name,
cmd_args=memory_config,
args=config,
memory_dir=memory_config.output_dir
)
# Create MemoryManager (memory manager, provides high-level API)
memory_manager = MemoryManager(
memory_index=memory_index,
memory_system=memory_system,
llm_model=config["model"]["llm_model"],
llm_client=client,
embedding_model=embedding_model,
user_name=user_name,
agent_name=agent_name,
cmd_args=memory_config,
args=config,
memory_dir=memory_config.output_dir
)
print(f"\n{'='*50}")
print("Memory system initialization complete!")
print(f"User: {user_name}, Agent: {agent_name}")
print(f"{'='*50}\n")
# ========== 3. Simulate Conversation and Store Memory ==========
# Simulated conversation data
# Note: Number of conversations must exceed working_memory_max_size to trigger memory transfer
conversations = [
{"speaker": user_name, "message": "I love playing basketball with my friends every weekend.", "timestamp": "2024-01-01 10:00:00"},
{"speaker": agent_name, "message": "That sounds great! Basketball is a wonderful way to stay active.", "timestamp": "2024-01-01 10:01:00"},
{"speaker": user_name, "message": "Yes! I've been playing for 5 years. My favorite team is the Lakers.", "timestamp": "2024-01-01 10:02:00"},
{"speaker": agent_name, "message": "The Lakers have a great history. Do you play any position specifically?", "timestamp": "2024-01-01 10:03:00"},
{"speaker": user_name, "message": "I usually play as a point guard. I also started learning guitar last month.", "timestamp": "2024-01-01 10:04:00"},
{"speaker": agent_name, "message": "Guitar is a lovely instrument! What kind of music do you want to play?", "timestamp": "2024-01-01 10:05:00"},
{"speaker": user_name, "message": "I'm into rock music. My favorite band is Coldplay.", "timestamp": "2024-01-01 10:06:00"},
{"speaker": agent_name, "message": "Coldplay has some amazing songs. Do you have a favorite album?", "timestamp": "2024-01-01 10:07:00"},
{"speaker": user_name, "message": "I really love 'A Rush of Blood to the Head'. The song 'The Scientist' is my favorite.", "timestamp": "2024-01-01 10:08:00"},
{"speaker": agent_name, "message": "That's a classic! Do you also enjoy attending live concerts?", "timestamp": "2024-01-01 10:09:00"},
{"speaker": user_name, "message": "Yes! I went to a Coldplay concert last year. It was an amazing experience.", "timestamp": "2024-01-01 10:10:00"},
{"speaker": agent_name, "message": "That must have been incredible! Live music is always so energizing.", "timestamp": "2024-01-01 10:11:00"},
{"speaker": user_name, "message": "Absolutely! I also enjoy cooking Italian food on weekends.", "timestamp": "2024-01-01 10:12:00"},
{"speaker": agent_name, "message": "Italian cuisine is delicious! What's your specialty dish?", "timestamp": "2024-01-01 10:13:00"},
{"speaker": user_name, "message": "I make a great homemade pasta with tomato sauce. My family loves it.", "timestamp": "2024-01-01 10:14:00"},
{"speaker": agent_name, "message": "That sounds delicious! Cooking for family is always special.", "timestamp": "2024-01-01 10:15:00"},
]
print("Processing conversations and building memory...")
print("-" * 50)
for index, conv in enumerate(conversations):
speaker = conv["speaker"]
message = conv["message"]
timestamp = conv["timestamp"]
user_speak = (speaker == user_name)
print(f"[{index}] {speaker}: {message}")
# Store message in memory system
await memory_manager.receive_message(
message=message,
client=client,
index=index,
timestamp=timestamp,
user_speak=user_speak
)
print("-" * 50)
print("Conversation processing complete!\n")
# ========== 4. Sync Memory Mappings (Important! Must be called before retrieval) ==========
print("Syncing memory mappings...")
# Sync messages from working_memory and episodic_memory to topic_message_dict
for message in list(memory_system.user_working_memory.working_memory_queue.queue) + memory_system.user_episodic_memory.episodic_memory_cache_list:
memory_system.user_topic_message_dict[message["topics"]] = [message["raw_message"], message["timestamp"]]
for message in list(memory_system.agent_working_memory.working_memory_queue.queue) + memory_system.agent_episodic_memory.episodic_memory_cache_list:
memory_system.agent_topic_message_dict[message["topics"]] = [message["raw_message"], message["timestamp"]]
# Generate memory detail map (for keyword retrieval)
memory_system.generate_memory_detail_map()
print("Memory mapping sync complete!\n")
# ========== 5. Update Persona Memory (Optional, call after conversation ends) ==========
print("Updating persona memory...")
await memory_system.user_persona_memory.update_preference_persona(
memory_system.user_episodic_memory.topic_episodic_memory_list
)
await memory_system.user_persona_memory.update_attribute_persona(
memory_system.user_episodic_memory.attribute_episodic_memory_dict
)
print("Persona memory update complete!\n")
# ========== 6. Memory Retrieval ==========
print("=" * 50)
print("Memory Retrieval Example")
print("=" * 50)
# Test questions
test_questions = [
f"What sport does {user_name} like?",
f"What instrument is {user_name} learning?",
f"What is {user_name}'s favorite band?",
]
for question in test_questions:
print(f"\nQuestion: {question}")
print("-" * 40)
# Retrieve relevant information from memory system
retrieval_result, retrieved_user, retrieved_agent, peak_mem, peak_mem_inc = \
memory_manager.retrieve_from_memory_soft_segmentation(
question=question,
topn=memory_config.number_of_retrieval_pieces,
drop_threshold=memory_config.drop_threshold
)
# Display retrieval results
print(f"Retrieved persona attributes: {retrieval_result.get('persona attributes', [])}")
print(f"Retrieved context messages: {retrieval_result.get('retrieved context messages', [])[:3]}")
# Generate answer
answer, tokens = await memory_manager.generate_system_response(
query=question,
restrieval_result=retrieval_result,
client=client,
speaker_a=retrieved_user,
speaker_b=retrieved_agent,
llm_model=config["model"]["llm_model"]
)
print(f"Generated answer: {answer}")
print(f"Tokens consumed: {tokens}")
print("\n" + "=" * 50)
print("Example run complete!")
print("=" * 50)
# ==================== Simplified API Wrapper ====================
class SimpleMemory:
"""
Simplified memory system wrapper class
Provides simpler API for everyday use
"""
def __init__(
self,
user_name: str,
agent_name: str,
llm_model: str = "gpt-4o-mini",
api_key: str = None,
base_url: str = "https://api.openai.com/v1",
embedding_model_name: str = "all-MiniLM-L6-v2",
memory_dir: str = "./memory_storage",
):
self.user_name = user_name
self.agent_name = agent_name
self.llm_model = llm_model
self.memory_dir = memory_dir
self.message_index = 0
# Initialize client
self.client = AsyncOpenAI(base_url=base_url, api_key=api_key)
# Load Embedding model
device = "cuda" if torch.cuda.is_available() else "cpu"
self.embedding_model = SentenceTransformer(embedding_model_name).to(device)
# Configuration
self.config = {"model": {"llm_model": llm_model}}
self.cmd_args = MemoryConfig(
working_memory_max_size=5, # Same as main() for consistent behavior
episodic_memory_refresh_rate=3, # Same as main()
output_dir=memory_dir
)
os.makedirs(memory_dir, exist_ok=True)
# Initialize memory system
self.memory_system = MemoryChain(
memory_index=0,
llm_model=llm_model,
llm_client=self.client,
embedding_model=self.embedding_model,
user_name=user_name,
agent_name=agent_name,
cmd_args=self.cmd_args,
args=self.config,
memory_dir=memory_dir
)
self.memory_manager = MemoryManager(
memory_index=0,
memory_system=self.memory_system,
llm_model=llm_model,
llm_client=self.client,
embedding_model=self.embedding_model,
user_name=user_name,
agent_name=agent_name,
cmd_args=self.cmd_args,
args=self.config,
memory_dir=memory_dir
)
async def add_message(self, message: str, is_user: bool = True, timestamp: str = None):
"""Add a message to memory"""
from datetime import datetime
if timestamp is None:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
await self.memory_manager.receive_message(
message=message,
client=self.client,
index=self.message_index,
timestamp=timestamp,
user_speak=is_user
)
self.message_index += 1
async def query(self, question: str) -> str:
"""Query memory and generate answer"""
# Sync memory mappings
self._sync_memory_mappings()
result, user, agent, _, _ = self.memory_manager.retrieve_from_memory_soft_segmentation(
question=question,
topn=self.cmd_args.number_of_retrieval_pieces,
drop_threshold=self.cmd_args.drop_threshold
)
answer, _ = await self.memory_manager.generate_system_response(
query=question,
restrieval_result=result,
client=self.client,
speaker_a=user,
speaker_b=agent,
llm_model=self.llm_model
)
return answer
def _sync_memory_mappings(self):
"""Sync memory mappings (must be called before retrieval)"""
for message in list(self.memory_system.user_working_memory.working_memory_queue.queue) + self.memory_system.user_episodic_memory.episodic_memory_cache_list:
self.memory_system.user_topic_message_dict[message["topics"]] = [message["raw_message"], message["timestamp"]]
for message in list(self.memory_system.agent_working_memory.working_memory_queue.queue) + self.memory_system.agent_episodic_memory.episodic_memory_cache_list:
self.memory_system.agent_topic_message_dict[message["topics"]] = [message["raw_message"], message["timestamp"]]
self.memory_system.generate_memory_detail_map()
async def update_persona(self):
"""Update persona memory"""
# Sync memory mappings first
self._sync_memory_mappings()
await self.memory_system.user_persona_memory.update_preference_persona(
self.memory_system.user_episodic_memory.topic_episodic_memory_list
)
await self.memory_system.user_persona_memory.update_attribute_persona(
self.memory_system.user_episodic_memory.attribute_episodic_memory_dict
)
# ==================== Simplified Usage Example ====================
async def simple_example():
"""Simplified API usage example - processes the same conversation as main()"""
print("\n" + "=" * 50)
print("Simplified API Usage Example")
print("=" * 50 + "\n")
# Read configuration
with open('config.yaml', 'r') as f:
config = yaml.safe_load(f)
user_name = "Alice"
agent_name = "Bob"
# Create simplified memory system
memory = SimpleMemory(
user_name=user_name,
agent_name=agent_name,
llm_model=config["model"]["llm_model"],
api_key=config["model"]["openai_api_key"],
base_url=config["model"]["openai_base_url"],
memory_dir="./memory_storage_simple", # Use different storage directory
)
# Simulated conversation data (same as main())
conversations = [
{"speaker": user_name, "message": "I love playing basketball with my friends every weekend.", "timestamp": "2024-01-01 10:00:00"},
{"speaker": agent_name, "message": "That sounds great! Basketball is a wonderful way to stay active.", "timestamp": "2024-01-01 10:01:00"},
{"speaker": user_name, "message": "Yes! I've been playing for 5 years. My favorite team is the Lakers.", "timestamp": "2024-01-01 10:02:00"},
{"speaker": agent_name, "message": "The Lakers have a great history. Do you play any position specifically?", "timestamp": "2024-01-01 10:03:00"},
{"speaker": user_name, "message": "I usually play as a point guard. I also started learning guitar last month.", "timestamp": "2024-01-01 10:04:00"},
{"speaker": agent_name, "message": "Guitar is a lovely instrument! What kind of music do you want to play?", "timestamp": "2024-01-01 10:05:00"},
{"speaker": user_name, "message": "I'm into rock music. My favorite band is Coldplay.", "timestamp": "2024-01-01 10:06:00"},
{"speaker": agent_name, "message": "Coldplay has some amazing songs. Do you have a favorite album?", "timestamp": "2024-01-01 10:07:00"},
{"speaker": user_name, "message": "I really love 'A Rush of Blood to the Head'. The song 'The Scientist' is my favorite.", "timestamp": "2024-01-01 10:08:00"},
{"speaker": agent_name, "message": "That's a classic! Do you also enjoy attending live concerts?", "timestamp": "2024-01-01 10:09:00"},
{"speaker": user_name, "message": "Yes! I went to a Coldplay concert last year. It was an amazing experience.", "timestamp": "2024-01-01 10:10:00"},
{"speaker": agent_name, "message": "That must have been incredible! Live music is always so energizing.", "timestamp": "2024-01-01 10:11:00"},
{"speaker": user_name, "message": "Absolutely! I also enjoy cooking Italian food on weekends.", "timestamp": "2024-01-01 10:12:00"},
{"speaker": agent_name, "message": "Italian cuisine is delicious! What's your specialty dish?", "timestamp": "2024-01-01 10:13:00"},
{"speaker": user_name, "message": "I make a great homemade pasta with tomato sauce. My family loves it.", "timestamp": "2024-01-01 10:14:00"},
{"speaker": agent_name, "message": "That sounds delicious! Cooking for family is always special.", "timestamp": "2024-01-01 10:15:00"},
]
# Add all conversations
print("Processing conversations and building memory...")
print("-" * 50)
for conv in conversations:
speaker = conv["speaker"]
message = conv["message"]
timestamp = conv["timestamp"]
is_user = (speaker == user_name)
print(f"{'[User]' if is_user else '[Agent]'} {speaker}: {message[:50]}...")
await memory.add_message(message, is_user=is_user, timestamp=timestamp)
print("-" * 50)
print("Conversation processing complete!\n")
# Update persona memory
print("Updating persona memory...")
await memory.update_persona()
print("Persona memory update complete!\n")
# Test questions
print("=" * 50)
print("Memory Retrieval Example")
print("=" * 50)
test_questions = [
f"What sport does {user_name} like?",
f"What instrument is {user_name} learning?",
f"What is {user_name}'s favorite band?",
]
for question in test_questions:
print(f"\nQuestion: {question}")
print("-" * 40)
answer = await memory.query(question)
print(f"Answer: {answer}")
print("\n" + "=" * 50)
print("Simplified example run complete!")
print("=" * 50)
# ==================== Entry Point ====================
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == "simple":
# Run simplified example
print("Running simplified example...")
asyncio.run(simple_example())
else:
# Run full example (default)
print("Running full example...")
asyncio.run(main())
# Usage:
# python example_usage.py # Run full example
# python example_usage.py simple # Run simplified example

66751
locomo10.json Normal file

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,381 @@
#!/usr/bin/env python
# coding=utf-8
# Copyright 2025 The OPPO Personal AI team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from datetime import datetime, timedelta
import re
import openai
import time
import tiktoken
import os
import asyncio
import aiofiles
from tqdm.asyncio import tqdm_asyncio
from memory_chain import *
from openai import AsyncOpenAI
import yaml
import argparse
import torch
import time
from typing import List, Union, Dict
from concurrent.futures import ProcessPoolExecutor
try:
with open('config.yaml', 'r') as file:
args = yaml.safe_load(file)
except Exception as e:
print(e)
print('Error reading the config file')
client = AsyncOpenAI(base_url=args["model"]["openai_base_url"],api_key=args["model"]["openai_api_key"])
def get_timestamp():
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
class ParallelSentenceTransformer:
def __init__(self, model: SentenceTransformer):
self.model = model
# Enable DataParallel only in multi-GPU environment
if torch.cuda.device_count() > 1:
self.model = torch.nn.DataParallel(model)
print(f"Using {torch.cuda.device_count()} GPUs!")
else:
print("Using single GPU or CPU")
def encode(self, texts: Union[str, List[str]],
batch_size: int = 32,
convert_to_tensor: bool = True,
**kwargs) -> torch.Tensor:
"""
Multi-GPU friendly encoding method with automatic batching and cross-device processing.
Args:
texts: Input text (single string or list)
batch_size: Batch size per GPU
convert_to_tensor: Whether to return tensor
"""
# Normalize input format
if isinstance(texts, str):
texts = [texts]
# Get current device (primary GPU)
device = self.model.device_ids[0] if isinstance(self.model, torch.nn.DataParallel) \
else next(self.model.parameters()).device
# Batch processing (avoid single GPU memory overflow)
all_embeddings = []
for i in range(0, len(texts), batch_size):
batch = texts[i:i + batch_size]
# Tokenization (must be executed on primary GPU)
features = self.model.module.tokenize(batch) if hasattr(self.model, 'module') \
else self.model.tokenize(batch)
features = {k: v.to(device) for k, v in features.items()}
# Multi-GPU forward propagation
with torch.no_grad():
outputs = self.model(features)
embeddings = outputs['sentence_embedding']
all_embeddings.append(embeddings if convert_to_tensor else embeddings.cpu().numpy())
# Merge results
return torch.cat(all_embeddings, dim=0) if convert_to_tensor \
else np.concatenate(all_embeddings, axis=0)
print("Loading embedding model...")
device = "cuda" if torch.cuda.is_available() else "cpu"
embedding_model = ParallelSentenceTransformer(SentenceTransformer("all-MiniLM-L6-v2").to(device))
# path = 'your model'
# embedding_model = ParallelSentenceTransformer(SentenceTransformer(path).to(device))
async def update_user_profile_from_top_segment(mid_mem, long_mem, sample_id, client):
print("update the profiles of the users.")
async def generate_system_response_with_memx(query, short_mem, long_mem, retrieval_queue, long_konwledge, client, sample_id, speaker_a, speaker_b, meta_data,no_mtm,no_ltm,no_wm):
"""
These codes are referenced from memory os.
"""
if no_wm == True:
history_text = ""
else:
history = short_mem.get_all()
history_text_list = [
f"{speaker_a}: {qa.get('user_input', '')}\n{speaker_b}: {qa.get('agent_response', '')}\nTime: ({qa.get('timestamp', '')})"
for qa in history]
if no_mtm == True:
retrieval_text = ""
else:
retrieval_text = "\n".join([
f"【Historical Memory】 {speaker_a}: {page.get('user_input', '')}\n{speaker_b}: {page.get('agent_response', '')}\nTime:({page.get('timestamp', '')})\nConversation chain overview:({page.get('meta_info', '')})\n"
for page in retrieval_queue])
if no_ltm == True:
user_profile_text = ""
else:
profile_obj = long_mem.get_user_profile(sample_id)
user_profile_text = str(profile_obj.get("data", "None")) if profile_obj else "None"
background = f"【User Profile】\n{user_profile_text}\n\n"
for kn in long_konwledge:
background += f"{kn['knowledge']}\n"
background = re.sub(r'(?i)\buser\b', speaker_a, background)
background= re.sub(r'(?i)\bassistant\b', speaker_b, background)
assistant_knowledge = long_mem.get_assistant_knowledge()
assistant_knowledge_text = "【Assistant Knowledge】\n"
for ak in assistant_knowledge:
assistant_knowledge_text += f"- {ak['knowledge']} ({ak['timestamp']})\n"
assistant_knowledge_text = re.sub(r'\bI\b', speaker_b, assistant_knowledge_text)
system_prompt = (
f"You are role-playing as {speaker_b} in a conversation with the user is playing is {speaker_a}. "
f"Here are some of your character traits and knowledge:\n{assistant_knowledge_text}\n"
f"Any content referring to 'User' in the prompt refers to {speaker_a}'s content, and any content referring to 'AI'or 'assiant' refers to {speaker_b}'s content."
f"Your task is to answer questions about {speaker_a} or {speaker_b} in an extremely concise manner.\n"
f"When the question is: \"What did the charity race raise awareness for?\", you should not answer in the form of: \"The charity race raised awareness for mental health.\" Instead, it should be: \"mental health\", as this is more concise."
)
user_prompt = (
f"<CONTEXT>\n"
f"Recent conversation between {speaker_a} and {speaker_b}:\n"
f"{history_text}\n\n"
f"<MEMORY>\n"
f"Relevant past conversations:\n"
f"{retrieval_text}\n\n"
f"<CHARACTER TRAITS>\n"
f"Characteristics of {speaker_a}:\n"
f"{background}\n\n"
f"the question is: {query}\n"
f"Your task is to answer questions about {speaker_a} or {speaker_b} in an extremely concise manner.\n"
f"Please only provide the content of the answer, without including 'answer:'\n"
f"For questions that require answering a date or time, strictly follow the format \"15 July 2023\" and provide a specific date whenever possible. For example, if you need to answer \"last year,\" give the specific year of last year rather than just saying \"last year.\" Only provide one year, date, or time, without any extra responses.\n"
f"If the question is about the duration, answer in the form of several years, months, or days.\n"
f"Generate answers primarily composed of concrete entities, such as Mentoring program, school speech, etc"
)
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
response = await client.chat_completion(model="gpt-4o-mini", messages=messages, temperature=0.7, max_tokens=2000)
return response, system_prompt, user_prompt
def process_conversation(conversation_data):
"""
These codes are referenced from memory os.
Process conversation data from locomo10 format into memory system format.
Handles both text-only and image-containing messages.
"""
processed = []
speaker_a = conversation_data["speaker_a"]
speaker_b = conversation_data["speaker_b"]
session_keys = [key for key in conversation_data.keys() if key.startswith("session_") and not key.endswith("_date_time")]
for session_key in session_keys:
timestamp_key = f"{session_key}_date_time"
timestamp = conversation_data.get(timestamp_key, "")
for dialog in conversation_data[session_key]:
speaker = dialog["speaker"]
text = dialog["text"]
if "blip_caption" in dialog and dialog["blip_caption"]:
text = f"{text} (image description: {dialog['blip_caption']})"
if speaker == speaker_a:
processed.append({speaker_a: text,"timestamp": timestamp})
else:
processed.append({speaker_b: text, "timestamp": timestamp})
return processed
import os
import re
def find_agent_memory_file(directory, idx):
pattern = re.compile(rf'^.*_agent_working_memory_{idx}\.json$')
for filename in os.listdir(directory):
if pattern.match(filename):
return os.path.join(directory,filename)
return None
def find_user_memory_file(directory, idx):
pattern = re.compile(rf'^.*_user_working_memory_{idx}\.json$')
for filename in os.listdir(directory):
if pattern.match(filename):
return os.path.join(directory,filename)
return None
async def run_locomo_experiment(idx,args,cmd_args,client):
os.makedirs("mem_tmp_loco_final", exist_ok=True)
try:
with open("locomo10.json", "r", encoding="utf-8") as f:
dataset = json.load(f)
except FileNotFoundError:
print("Error: locomo10.json file not found, please ensure it is in the current directory")
return
except Exception as e:
print(f"Error loading dataset: {e}")
return
output_file = cmd_args.response_output_dir+"/HippoIndex_results_"+str(idx)+".json"
results = []
total_samples = len(dataset)
sample = dataset[idx]
print(f"Processing sample {idx + 1}/{total_samples}: {sample.get('sample_id', 'unknown')}")
sample_id = sample.get("sample_id", "unknown_sample")
conversation_data = sample["conversation"]
qa_pairs = sample["qa"]
qa_count = len(qa_pairs)
processed_dialogs = process_conversation(conversation_data)
print(f"This sample has {len(processed_dialogs)} dialog turns")
print(f"This sample has {len(qa_pairs)} questions")
if not processed_dialogs:
print(f"Sample {sample_id} has no valid conversation data, skipping")
return
speaker_a = conversation_data["speaker_a"]
speaker_b = conversation_data["speaker_b"]
memory_dir = cmd_args.output_dir
memory_system = MemoryChain(memory_index=idx, llm_model=args["model"]["llm_model"],llm_client = client,embedding_model = embedding_model,
user_name= speaker_a, agent_name = speaker_b, cmd_args=cmd_args, args=args, memory_dir = memory_dir)
memory_manager = MemoryManager(memory_index=idx, memory_system=memory_system,llm_model=args["model"]["llm_model"],llm_client = client,
embedding_model = embedding_model,user_name= speaker_a, agent_name = speaker_b, cmd_args=cmd_args, args=args, memory_dir = memory_dir)
if cmd_args.use_all_memory_cache:
await memory_system.read_cache_memory(cmd_args.memory_cache_path)
memory_system.generate_memory_detail_map()
print("Memory system load cache memory finish")
# qa_count = len(qa_pairs)
else:
print("Stage 1: We indexing the memory base.")
for index,dialog in enumerate(processed_dialogs):
timestamp = get_timestamp()
if speaker_a in dialog:
user_speak = True
start_time = time.time()
await memory_manager.receive_message(message = dialog[speaker_a], client=client,index=index,timestamp=dialog["timestamp"], user_speak=user_speak)
end_time = time.time() # Record end time
elapsed = end_time - start_time # Calculate elapsed time (seconds)
print(f"Receive one new message took: {elapsed:.4f} seconds")
else:
user_speak = False
start_time = time.time()
await memory_manager.receive_message(message = dialog[speaker_b],client=client,index=index,timestamp=dialog["timestamp"],user_speak = user_speak)
end_time = time.time() # Record end time
elapsed = end_time - start_time # Calculate elapsed time (seconds)
print(f"Receive one new message took: {elapsed:.4f} seconds")
await memory_system.agent_persona_memory.update_preference_persona(memory_system.agent_episodic_memory.topic_episodic_memory_list)
await memory_system.agent_persona_memory.update_attribute_persona(memory_system.agent_episodic_memory.attribute_episodic_memory_dict)
await memory_system.user_persona_memory.update_preference_persona(memory_system.user_episodic_memory.topic_episodic_memory_list)
await memory_system.user_persona_memory.update_attribute_persona(memory_system.user_episodic_memory.attribute_episodic_memory_dict)
return
for qa_idx, qa in enumerate(qa_pairs):
start_time = time.time()
print(f"Processing QA {qa_idx + 1}/{qa_count}")
question = qa["question"]
original_answer = qa.get("answer", "")
category = qa["category"]
evidence = qa.get("evidence", "")
if(original_answer == ""):
# continue
original_answer = qa.get("adversarial_answer", "")
# retrieval_result, user_name, agent_name = memory_manager.retrieve_from_memory_soft_segmentation_v5(question,cmd_args.number_of_retrieval_pieces,cmd_args.drop_threshold)
retrieval_result, user_name, agent_name,peak_memory_number,peak_memory_number_increase = memory_manager.retrieve_from_memory_soft_segmentation(question,cmd_args.number_of_retrieval_pieces,cmd_args.drop_threshold)
# retrieval_result, user_name, agent_name,peak_memory_number,peak_memory_number_increase = memory_manager.retrieve_from_memory_soft_segmentation_v9_ablation_study(question,cmd_args.number_of_retrieval_pieces,cmd_args.drop_threshold)
answer,tokens_consumption = await memory_manager.generate_system_response(question,retrieval_result,client,user_name,agent_name,args["model"]["llm_model"])
end_time = time.time()
elapsed_time = end_time - start_time
results.append({"time":elapsed_time,"sample_id": sample_id,"user": user_name,"agent": agent_name,"question": question,"answer":answer,"reference_answer": original_answer,"category": category,
"evidence": evidence,"persona attributes":retrieval_result["persona attributes"],"fact scores":retrieval_result["fact scores"],"persona facts": retrieval_result["persona facts"],
"retrieved context messages":retrieval_result["retrieved context messages"],"fact recovery num":retrieval_result["recover facts num"],"ignored details keywords": retrieval_result["ignored_details_keywords"],
"ignored facts":retrieval_result["ignored_facts"],"keyword distribution dict":retrieval_result["keyword distribution dict"],"keyword message":retrieval_result["keyword message"],"original retrievals":retrieval_result["original context message"],"token consumption":tokens_consumption,
"peak memory":peak_memory_number,"peak_memory_number_increase":peak_memory_number_increase})
try:
with open(output_file, "w", encoding="utf-8") as f:
json.dump(results, f, ensure_ascii=False, indent=2)
print(f"Sample {idx + 1} processing complete, results saved to {output_file}")
except Exception as e:
print(f"Error saving results: {e}")
import traceback
traceback.print_exc()
print(f"Final error saving results: {e}")
async def run_one(idx,args,cmd_args,client):
async with semaphore:
result = await run_locomo_experiment(idx=idx,args=args,cmd_args=cmd_args,client=client)
return result
semaphore = asyncio.Semaphore(10)
async def main():
parser = argparse.ArgumentParser(description='Command line arguments')
parser.add_argument('--working_memory_max_size',type=int, default=20, help = 'the maximum size of the working memory')
parser.add_argument('--episodic_memory_refresh_rate',type=int, default=5, help = 'the rate ti refresh episodic memory')
parser.add_argument('--output_dir',type=str, default="./test_locomo") # 1. Memory generation directory
parser.add_argument('--response_output_dir',type=str, default="./responses_locomo") # 2. Results output
parser.add_argument('--use_all_memory_cache',type=bool, default=False) # 1.False(start generate mem) 2. True (start retireval)
parser.add_argument('--memory_cache_path',type=str, default="./test_locomo")
parser.add_argument('--message_understanding_cache',type=bool, default=False) # This is to save money and keep efficiency
parser.add_argument('--message_understanding_cache_path',type=str, default="./memory_cache")
parser.add_argument('--number_of_retrieval_pieces',type=int, default=10)
parser.add_argument('--drop_threshold',type=float, default=0.1)
cmd_args = parser.parse_args()
tasks = [run_one(idx=p_id, args = args, cmd_args=cmd_args, client=client) for p_id in range(10)]
import shutil
dir_path = cmd_args.output_dir
try:
shutil.rmtree(cmd_args.response_output_dir)
except:
print("No target output folder.")
os.mkdir(cmd_args.response_output_dir)
results = await tqdm_asyncio.gather(*tasks, total=len(tasks))
if __name__ == "__main__":
asyncio.run(main())

20
requirements.txt Normal file
View file

@ -0,0 +1,20 @@
# Memory Chain - Dependencies
# ===========================
# Install with: pip install -r requirements.txt
# Core Dependencies
openai>=1.0.0 # OpenAI API client
torch>=2.8.0 # PyTorch deep learning framework
sentence-transformers>=2.2.0 # Sentence embedding models
numpy>=1.22.0 # Numerical computing
# Configuration & Utilities
pyyaml>=6.0 # YAML config file parsing
tqdm>=4.66.3 # Progress bars
aiofiles>=23.0.0 # Async file operations
# Optional Dependencies (for experiments)
tiktoken>=0.5.0 # OpenAI tokenizer
FlagEmbedding>=1.2.0 # BGE embedding models (optional, for custom embeddings)
scikit-learn>=1.0.0 # Machine learning utilities (cosine similarity)