-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample_usage.py
More file actions
481 lines (400 loc) · 20.8 KB
/
example_usage.py
File metadata and controls
481 lines (400 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/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
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