-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory_proxy.py
More file actions
329 lines (260 loc) · 13 KB
/
Copy pathmemory_proxy.py
File metadata and controls
329 lines (260 loc) · 13 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
"""
Passive Memory Proxy
A transparent proxy that sits between you and the LLM API.
Automatically injects relevant memories into context and extracts new memories from responses.
The LLM never knows memory is happening - it just sees enhanced context.
Usage:
proxy = PassiveMemoryProxy()
response = proxy.chat("Your message here")
"""
import os
from datetime import datetime
from typing import Dict, List, Any, Optional
from dataclasses import dataclass
from engram_pkg import VectorMemory
from memory_integration import MemoryIntegration
from memory_extractor import MemoryExtractor
from token_tracker import tracker
# Check for Gemini SDK
_gemini_available = False
try:
from google import genai
from google.genai import types
_gemini_available = True
except ImportError:
pass
@dataclass
class ProxyConfig:
"""Configuration for the memory proxy"""
# Memory settings
memory_path: str = "vector_memory"
max_memories_to_inject: int = 5
min_memory_importance: float = 0.2
# LLM settings
model: str = "gemini-2.0-flash"
extraction_model: str = "gemini-2.0-flash-lite"
max_tokens: int = 4096
# Context settings
memory_token_budget: int = 1000 # Max tokens for memory context
include_timestamps: bool = False
# Behavior
extraction_enabled: bool = True
verbose: bool = False
class PassiveMemoryProxy:
"""
Transparent memory-enhanced LLM proxy.
Synchronous path (blocking):
1. User message → Search memories → Inject context → Call LLM → Return response
Asynchronous path (background):
2. After response → Extract insights → Store new memories
"""
def __init__(self, config: ProxyConfig = None, api_key: str = None):
"""
Initialize the memory proxy.
Args:
config: Proxy configuration
api_key: Gemini API key (or uses GEMINI_API_KEY env var)
"""
self.config = config or ProxyConfig()
# Initialize memory system
self.memory = MemoryIntegration(
memory_path=self.config.memory_path,
auto_save=True
)
# Initialize extractor for async memory creation
self.extractor = MemoryExtractor(
memory_system=self.memory.memory_system,
model=self.config.extraction_model
)
# Start extraction worker
if self.config.extraction_enabled:
self.extractor.start_worker()
# Initialize Gemini client
self.client = None
if _gemini_available:
try:
key = api_key or os.environ.get("GEMINI_API_KEY")
if key:
self.client = genai.Client(api_key=key)
print(f"✅ Gemini client initialized ({self.config.model})")
else:
print("❌ GEMINI_API_KEY not found in environment")
except Exception as e:
print(f"❌ Failed to initialize Gemini client: {e}")
else:
print("❌ google-genai SDK not installed. Install with: pip install google-genai")
# Conversation state
self.conversation_history = []
self.system_prompt = """You are a helpful AI assistant with persistent memory. You have the ability to remember important information from previous conversations and use it to provide more personalized and context-aware responses.
When you receive context from previous conversations, treat this as YOUR memory - information you genuinely remember about the user and past interactions. Use this knowledge naturally in your responses."""
# Statistics
self.stats = {
"messages_processed": 0,
"memories_injected": 0,
"tokens_used_for_memory": 0,
"session_start": datetime.now()
}
print(f"🧠 Passive Memory Proxy initialized")
print(f" Memory store: {len(self.memory.memory_system.memories)} memories")
print(f" Extraction: {'enabled' if self.config.extraction_enabled else 'disabled'}")
def set_system_prompt(self, prompt: str):
"""Set the base system prompt (memory context will be prepended)"""
self.system_prompt = prompt
def _get_memory_context(self, user_message: str) -> str:
"""
Get relevant memories formatted for context injection.
This is the SYNCHRONOUS retrieval step - must complete before LLM call.
"""
# Search for relevant memories
memories = self.memory.memory_system.retrieve_memory(
query=user_message,
limit=self.config.max_memories_to_inject,
min_importance=self.config.min_memory_importance
)
if not memories:
return ""
# Format memories for injection
memory_lines = []
total_chars = 0
char_budget = self.config.memory_token_budget * 4 # Rough chars per token
for mem in memories:
line = f"• {mem.content}"
if self.config.include_timestamps:
line += f" (from {mem.timestamp.strftime('%Y-%m-%d')})"
if total_chars + len(line) > char_budget:
break
memory_lines.append(line)
total_chars += len(line)
self.stats["memories_injected"] += 1
if not memory_lines:
return ""
self.stats["tokens_used_for_memory"] += total_chars // 4
context = "## Your memories:\n"
context += "\n".join(memory_lines)
context += "\n\nThese are things you remember. Use them naturally when relevant to help the user."
return context
def _build_system_with_memory(self, memory_context: str) -> str:
"""Build the system prompt with memory context"""
if memory_context:
return f"{memory_context}\n\n---\n\n{self.system_prompt}"
return self.system_prompt
def chat(self, user_message: str) -> str:
"""
Send a message and get a response with transparent memory enhancement.
Args:
user_message: The user's message
Returns:
The assistant's response
"""
if not self.client:
return "Error: Gemini client not initialized. Check your API key."
self.stats["messages_processed"] += 1
# ═══════════════════════════════════════════════════════════
# STEP 1: RETRIEVE MEMORIES (synchronous, ~10-50ms)
# ═══════════════════════════════════════════════════════════
memory_context = self._get_memory_context(user_message)
if self.config.verbose and memory_context:
print(f"📚 Injecting {memory_context.count('•')} memories into context")
# ═══════════════════════════════════════════════════════════
# STEP 2: BUILD SYSTEM PROMPT WITH MEMORY
# ═══════════════════════════════════════════════════════════
system_with_memory = self._build_system_with_memory(memory_context)
# ═══════════════════════════════════════════════════════════
# STEP 3: BUILD CONVERSATION CONTENTS
# ═══════════════════════════════════════════════════════════
contents = []
for msg in self.conversation_history:
contents.append(types.Content(
role=msg["role"],
parts=[types.Part(text=msg["content"])]
))
# Add current user message
contents.append(types.Content(
role="user",
parts=[types.Part(text=user_message)]
))
# ═══════════════════════════════════════════════════════════
# STEP 4: CALL LLM (synchronous, blocking)
# ═══════════════════════════════════════════════════════════
try:
response = self.client.models.generate_content(
model=self.config.model,
contents=contents,
config=types.GenerateContentConfig(
system_instruction=system_with_memory,
max_output_tokens=self.config.max_tokens
)
)
assistant_message = response.text
# Track token usage for Brain
tracker.record_brain_usage(response.usage_metadata, self.config.model)
except Exception as e:
error_msg = f"Error calling LLM: {e}"
print(f"❌ {error_msg}")
return error_msg
# ═══════════════════════════════════════════════════════════
# STEP 5: TRACK CONVERSATION
# ═══════════════════════════════════════════════════════════
self.conversation_history.append({
"role": "user",
"content": user_message
})
self.conversation_history.append({
"role": "model", # Gemini uses "model" instead of "assistant"
"content": assistant_message
})
# Keep history manageable
if len(self.conversation_history) > 40:
self.conversation_history = self.conversation_history[-40:]
# ═══════════════════════════════════════════════════════════
# STEP 6: EXTRACT MEMORIES (async, non-blocking)
# ═══════════════════════════════════════════════════════════
if self.config.extraction_enabled:
self.extractor.extract_async(user_message, assistant_message)
return assistant_message
def add_memory(self, content: str, importance: float = 0.7,
tags: List[str] = None) -> str:
"""Manually add a memory"""
return self.memory.add_memory(content, importance, tags or [])
def search_memories(self, query: str, limit: int = 5) -> List[Dict[str, Any]]:
"""Search memories"""
return self.memory.search_memories(query, limit)
def get_stats(self) -> Dict[str, Any]:
"""Get proxy statistics"""
session_duration = (datetime.now() - self.stats["session_start"]).total_seconds()
return {
**self.stats,
"session_duration_seconds": session_duration,
"memory_count": len(self.memory.memory_system.memories),
"extraction_stats": self.extractor.get_stats(),
"conversation_length": len(self.conversation_history)
}
def clear_conversation(self):
"""Clear conversation history (memories persist)"""
self.conversation_history = []
print("🗑️ Conversation cleared (memories preserved)")
def shutdown(self, show_stats: bool = False):
"""Graceful shutdown"""
self.extractor.stop_worker()
self.memory.memory_system.force_save()
print("👋 Memory proxy shut down")
if show_stats:
print()
print(tracker.format_stats())
def get_token_stats(self) -> dict:
"""Get current token usage stats"""
return tracker.get_stats()
def print_token_stats(self):
"""Print token usage stats"""
print(tracker.format_stats())
def create_proxy(verbose: bool = False, **kwargs) -> PassiveMemoryProxy:
"""Factory function to create a memory proxy"""
config = ProxyConfig(verbose=verbose, **kwargs)
return PassiveMemoryProxy(config=config)
if __name__ == "__main__":
# Quick test
print("Testing Passive Memory Proxy")
print("=" * 40)
proxy = create_proxy(verbose=True)
print(f"\nStats: {proxy.get_stats()}")
print("\nProxy ready! Use proxy.chat('message') to send messages.")