forked from bojieli/ai-agent-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
580 lines (475 loc) · 21.5 KB
/
Copy pathagent.py
File metadata and controls
580 lines (475 loc) · 21.5 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
"""
Memobase Agent Implementation with Kimi K3 Model
Advanced memory management for LOCOMO benchmark
"""
import json
import logging
import time
import hashlib
import pickle
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass, field
from datetime import datetime
from pathlib import Path
from collections import defaultdict
from openai import OpenAI
from config import (
KIMI_API_KEY, KIMI_BASE_URL, KIMI_MODEL,
MODEL_TEMPERATURE, MODEL_MAX_TOKENS, MODEL_TOP_P,
MEMOBASE_CONFIG, MEMORY_DB_PATH, AGENT_CONFIG,
MAX_MEMORY_ENTRIES, MEMORY_COMPRESSION_THRESHOLD,
LOG_LEVEL, LOG_FORMAT
)
def _reasoning_safe_temperature(model, requested=1.0):
"""Reasoning models (Kimi K3, GPT-5, ...) only accept temperature=1.
Return 1 for those; otherwise the requested value so non-reasoning
providers (Doubao, DeepSeek, older Moonshot) are unchanged."""
m = str(model or "").lower().replace("/", "-")
return 1 if ("kimi-k3" in m or "gpt-5" in m) else requested
# Configure logging
logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT)
logger = logging.getLogger(__name__)
@dataclass
class Memory:
"""Represents a single memory entry"""
id: str
type: str # episodic, semantic, procedural, working
content: Any
embedding: Optional[List[float]] = None
metadata: Dict[str, Any] = field(default_factory=dict)
created_at: datetime = field(default_factory=datetime.now)
accessed_at: datetime = field(default_factory=datetime.now)
access_count: int = 0
importance_score: float = 1.0
decay_rate: float = 0.1
def __post_init__(self):
if not self.id:
# Generate unique ID based on content
content_str = json.dumps(self.content, sort_keys=True)
self.id = hashlib.md5(content_str.encode()).hexdigest()[:12]
def access(self):
"""Update access statistics"""
self.accessed_at = datetime.now()
self.access_count += 1
# Increase importance with access
self.importance_score = min(10.0, self.importance_score * 1.1)
def decay(self):
"""Apply time-based decay to importance"""
time_since_access = (datetime.now() - self.accessed_at).total_seconds() / 3600
self.importance_score *= (1 - self.decay_rate * min(1, time_since_access / 24))
self.importance_score = max(0.1, self.importance_score)
@dataclass
class MemoryCluster:
"""Represents a cluster of related memories"""
id: str
memories: List[Memory]
summary: Optional[str] = None
centroid_embedding: Optional[List[float]] = None
created_at: datetime = field(default_factory=datetime.now)
def add_memory(self, memory: Memory):
"""Add a memory to the cluster"""
self.memories.append(memory)
# TODO: Update centroid embedding
def compress(self) -> str:
"""Compress cluster into a summary"""
if not self.summary:
# Create summary from memories
contents = [m.content for m in self.memories]
self.summary = f"Cluster of {len(self.memories)} related memories: {contents[:3]}..."
return self.summary
class MemoryStore:
"""Manages different types of memories with persistence"""
def __init__(self, db_path: Path = MEMORY_DB_PATH):
self.db_path = db_path
self.memories: Dict[str, List[Memory]] = defaultdict(list)
self.clusters: List[MemoryCluster] = []
self.embeddings_cache: Dict[str, List[float]] = {}
self._load_memories()
logger.info(f"Initialized MemoryStore at {db_path}")
def _load_memories(self):
"""Load memories from persistent storage"""
memory_file = self.db_path / "memories.pkl"
if memory_file.exists():
try:
with open(memory_file, 'rb') as f:
data = pickle.load(f)
self.memories = data.get('memories', defaultdict(list))
self.clusters = data.get('clusters', [])
logger.info(f"Loaded {sum(len(m) for m in self.memories.values())} memories")
except Exception as e:
logger.error(f"Failed to load memories: {e}")
def _save_memories(self):
"""Save memories to persistent storage"""
memory_file = self.db_path / "memories.pkl"
try:
with open(memory_file, 'wb') as f:
pickle.dump({
'memories': dict(self.memories),
'clusters': self.clusters
}, f)
logger.debug("Saved memories to disk")
except Exception as e:
logger.error(f"Failed to save memories: {e}")
def add_memory(self, memory_type: str, content: Any,
metadata: Optional[Dict] = None,
importance: float = 1.0) -> Memory:
"""Add a new memory"""
memory = Memory(
id="", # Will be auto-generated
type=memory_type,
content=content,
metadata=metadata or {},
importance_score=importance
)
self.memories[memory_type].append(memory)
# Apply compression if needed
if len(self.memories[memory_type]) > MEMORY_COMPRESSION_THRESHOLD:
self._compress_memories(memory_type)
self._save_memories()
logger.debug(f"Added {memory_type} memory: {memory.id}")
return memory
def get_memories(self, memory_type: Optional[str] = None,
limit: int = 10,
min_importance: float = 0.5) -> List[Memory]:
"""Retrieve memories, optionally filtered by type and importance"""
if memory_type:
memories = self.memories.get(memory_type, [])
else:
memories = [m for mlist in self.memories.values() for m in mlist]
# Filter by importance and sort by relevance
memories = [m for m in memories if m.importance_score >= min_importance]
memories.sort(key=lambda m: (m.importance_score, m.access_count), reverse=True)
# Update access stats
for memory in memories[:limit]:
memory.access()
return memories[:limit]
def search_memories(self, query: str, limit: int = 5) -> List[Memory]:
"""Search memories by content similarity"""
results = []
query_lower = query.lower()
for memory_list in self.memories.values():
for memory in memory_list:
# Simple text matching (would use embeddings in production)
content_str = str(memory.content).lower()
if query_lower in content_str:
score = content_str.count(query_lower) * memory.importance_score
results.append((score, memory))
results.sort(key=lambda x: x[0], reverse=True)
# Update access stats
for _, memory in results[:limit]:
memory.access()
return [m for _, m in results[:limit]]
def _compress_memories(self, memory_type: str):
"""Compress old memories to save space"""
memories = self.memories[memory_type]
if len(memories) <= MEMORY_COMPRESSION_THRESHOLD:
return
# Sort by importance and recency
memories.sort(key=lambda m: (m.importance_score, m.accessed_at.timestamp()))
# Keep top memories, compress others
to_keep = memories[-MAX_MEMORY_ENTRIES//2:]
to_compress = memories[:-MAX_MEMORY_ENTRIES//2]
if to_compress:
# Create a cluster from compressed memories
cluster = MemoryCluster(
id=hashlib.md5(f"{memory_type}_{datetime.now()}".encode()).hexdigest()[:12],
memories=to_compress
)
cluster.compress()
self.clusters.append(cluster)
# Replace with compressed version
compressed_memory = Memory(
id=cluster.id,
type=memory_type,
content=cluster.summary,
metadata={"cluster_id": cluster.id, "compressed_count": len(to_compress)},
importance_score=sum(m.importance_score for m in to_compress) / len(to_compress)
)
self.memories[memory_type] = [compressed_memory] + to_keep
logger.info(f"Compressed {len(to_compress)} {memory_type} memories into cluster {cluster.id}")
def consolidate_memories(self):
"""Consolidate and reorganize memories for efficiency"""
for memory_type in self.memories:
memories = self.memories[memory_type]
# Apply decay to all memories
for memory in memories:
memory.decay()
# Remove very low importance memories
self.memories[memory_type] = [
m for m in memories if m.importance_score > 0.1
]
self._save_memories()
logger.info("Consolidated memories")
def clear_working_memory(self):
"""Clear working memory (short-term)"""
self.memories['working'] = []
logger.debug("Cleared working memory")
class MemobaseAgent:
"""
Advanced agent with Memobase memory management for LOCOMO benchmark
"""
def __init__(self, api_key: str = KIMI_API_KEY):
"""Initialize the Memobase agent"""
self.client = OpenAI(
api_key=api_key,
base_url=KIMI_BASE_URL
)
self.model = KIMI_MODEL
self.memory_store = MemoryStore()
self.conversation_history = []
self.current_task = None
self.task_context = {}
# Initialize system prompt
self._init_system_prompt()
logger.info(f"Initialized MemobaseAgent with model {self.model}")
def _init_system_prompt(self):
"""Initialize the system prompt with memory capabilities"""
self.system_prompt = """You are an advanced AI agent with sophisticated memory management capabilities.
You have access to multiple types of memory:
1. **Episodic Memory**: Specific experiences and events from tasks
2. **Semantic Memory**: General knowledge and facts
3. **Procedural Memory**: Learned procedures and problem-solving patterns
4. **Working Memory**: Current task context and temporary information
Memory Management Guidelines:
- Store important information for future reference
- Retrieve relevant memories when solving new problems
- Learn from past experiences to improve performance
- Compress and consolidate memories to maintain efficiency
- Use procedural memories to apply learned strategies
Your goal is to complete tasks efficiently while learning and adapting from experience.
When you encounter similar problems, use your memories to solve them more effectively.
Always think step-by-step and use your memory system strategically."""
def _store_interaction(self, role: str, content: str, memory_type: str = "episodic"):
"""Store an interaction in memory"""
self.memory_store.add_memory(
memory_type=memory_type,
content={
"role": role,
"content": content,
"task": self.current_task,
"timestamp": datetime.now().isoformat()
},
metadata={
"task_id": self.current_task,
"turn": len(self.conversation_history)
}
)
def _retrieve_relevant_memories(self, query: str, limit: int = 5) -> List[Memory]:
"""Retrieve memories relevant to current query"""
# Search across all memory types
relevant_memories = []
# Get recent episodic memories
episodic = self.memory_store.get_memories("episodic", limit=limit//2)
relevant_memories.extend(episodic)
# Search for similar content
searched = self.memory_store.search_memories(query, limit=limit//2)
relevant_memories.extend(searched)
# Get procedural memories if task-related
if "solve" in query.lower() or "how" in query.lower():
procedural = self.memory_store.get_memories("procedural", limit=2)
relevant_memories.extend(procedural)
# Remove duplicates
seen = set()
unique_memories = []
for memory in relevant_memories:
if memory.id not in seen:
seen.add(memory.id)
unique_memories.append(memory)
return unique_memories[:limit]
def _format_memories_for_context(self, memories: List[Memory]) -> str:
"""Format memories for inclusion in context"""
if not memories:
return ""
formatted = "\n=== Relevant Memories ===\n"
for memory in memories:
formatted += f"[{memory.type.upper()}] (importance: {memory.importance_score:.2f})\n"
if isinstance(memory.content, dict):
formatted += json.dumps(memory.content, indent=2)
else:
formatted += str(memory.content)
formatted += "\n---\n"
return formatted
def _learn_from_outcome(self, task: str, approach: str, outcome: str, success: bool):
"""Learn from task outcomes and store procedural knowledge"""
# Store the learning as procedural memory
self.memory_store.add_memory(
memory_type="procedural",
content={
"task_pattern": task,
"approach": approach,
"outcome": outcome,
"success": success,
"learned_at": datetime.now().isoformat()
},
importance=2.0 if success else 1.0,
metadata={"task_id": self.current_task}
)
if success:
logger.info(f"Learned successful approach for task type: {task}")
else:
logger.info(f"Learned from failure in task type: {task}")
def process_message(self, message: str, task_id: Optional[str] = None) -> str:
"""
Process a message with memory-aware reasoning
Args:
message: User message to process
task_id: Optional task identifier for context
Returns:
Agent's response
"""
self.current_task = task_id or f"task_{int(time.time())}"
# Store the query in working memory
self.memory_store.add_memory(
memory_type="working",
content=message,
metadata={"task_id": self.current_task}
)
# Retrieve relevant memories
relevant_memories = self._retrieve_relevant_memories(message)
memory_context = self._format_memories_for_context(relevant_memories)
# Build messages with memory context
messages = [
{"role": "system", "content": self.system_prompt}
]
if memory_context:
messages.append({
"role": "system",
"content": memory_context
})
# Add conversation history
messages.extend(self.conversation_history)
messages.append({"role": "user", "content": message})
try:
# Call Kimi K3 model
response = self.client.chat.completions.create(
model=self.model,
messages=messages,
temperature=_reasoning_safe_temperature(self.model, MODEL_TEMPERATURE),
max_tokens=MODEL_MAX_TOKENS,
top_p=MODEL_TOP_P
)
assistant_response = response.choices[0].message.content
# Store the interaction in episodic memory
self._store_interaction("user", message)
self._store_interaction("assistant", assistant_response)
# Update conversation history
self.conversation_history.append({"role": "user", "content": message})
self.conversation_history.append({"role": "assistant", "content": assistant_response})
# Keep conversation history manageable
if len(self.conversation_history) > 20:
# Move old conversations to episodic memory and compress
old_convs = self.conversation_history[:10]
for conv in old_convs:
self.memory_store.add_memory(
memory_type="episodic",
content=conv,
importance=0.5
)
self.conversation_history = self.conversation_history[10:]
return assistant_response
except Exception as e:
logger.error(f"Error processing message: {e}")
return f"Error: {str(e)}"
def execute_task(self, task: Dict[str, Any]) -> Dict[str, Any]:
"""
Execute a LOCOMO benchmark task
Args:
task: Task dictionary with 'id', 'type', 'query', and optional 'context'
Returns:
Result dictionary with 'response', 'memories_used', 'execution_time'
"""
start_time = time.time()
task_id = task.get('id', f"task_{int(time.time())}")
task_type = task.get('type', 'unknown')
query = task['query']
context = task.get('context', '')
self.current_task = task_id
# Check for similar past tasks in procedural memory
similar_tasks = self.memory_store.search_memories(f"{task_type} {query[:50]}", limit=3)
# Build enhanced query with context
enhanced_query = query
if context:
enhanced_query = f"Context: {context}\n\nTask: {query}"
# Process the task
response = self.process_message(enhanced_query, task_id)
# Extract approach and outcome for learning
approach = f"Used {len(similar_tasks)} similar memories"
outcome = response[:100] # First 100 chars as outcome summary
# Learn from this task
self._learn_from_outcome(
task=task_type,
approach=approach,
outcome=outcome,
success=True # Would be determined by evaluation
)
execution_time = time.time() - start_time
return {
"task_id": task_id,
"response": response,
"memories_used": len(similar_tasks),
"execution_time": execution_time,
"memory_stats": {
"episodic": len(self.memory_store.memories.get('episodic', [])),
"semantic": len(self.memory_store.memories.get('semantic', [])),
"procedural": len(self.memory_store.memories.get('procedural', [])),
"working": len(self.memory_store.memories.get('working', []))
}
}
def consolidate_and_learn(self):
"""Consolidate memories and extract learnings"""
logger.info("Starting memory consolidation...")
# Consolidate memories
self.memory_store.consolidate_memories()
# Extract patterns from episodic memories
episodic_memories = self.memory_store.get_memories('episodic', limit=50)
# Group by task type and extract patterns
task_patterns = defaultdict(list)
for memory in episodic_memories:
if isinstance(memory.content, dict):
task = memory.content.get('task', 'unknown')
task_patterns[task].append(memory)
# Create procedural memories from patterns
for task_type, memories in task_patterns.items():
if len(memories) >= 3: # Need multiple examples to learn
# Extract common approach
pattern = {
"task_type": task_type,
"successful_approaches": [],
"common_challenges": [],
"learned_from": len(memories)
}
self.memory_store.add_memory(
memory_type="procedural",
content=pattern,
importance=2.0,
metadata={"consolidation_run": datetime.now().isoformat()}
)
# Clear working memory
self.memory_store.clear_working_memory()
logger.info("Memory consolidation complete")
def reset(self, keep_memories: bool = True):
"""Reset the agent state"""
self.conversation_history = []
self.current_task = None
self.task_context = {}
if not keep_memories:
self.memory_store = MemoryStore()
else:
# Only clear working memory
self.memory_store.clear_working_memory()
logger.info(f"Agent reset (memories kept: {keep_memories})")
def get_performance_metrics(self) -> Dict[str, Any]:
"""Get agent performance metrics"""
memory_stats = {
memory_type: len(memories)
for memory_type, memories in self.memory_store.memories.items()
}
total_memories = sum(memory_stats.values())
cluster_count = len(self.memory_store.clusters)
return {
"total_memories": total_memories,
"memory_distribution": memory_stats,
"clusters_created": cluster_count,
"conversation_length": len(self.conversation_history),
"current_task": self.current_task
}