Spaces:
Running
Running
File size: 8,082 Bytes
7eb32cf |
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 |
import json
from pathlib import Path
from typing import List, Dict, Any, Optional
from datetime import datetime
import sqlite3
class MemoryStore:
"""Persistent memory store for agent context and user preferences"""
def __init__(self, db_path: str = "data/memory.db"):
"""Initialize memory store with SQLite"""
Path(db_path).parent.mkdir(parents=True, exist_ok=True)
self.db_path = db_path
self._init_db()
def _init_db(self):
"""Initialize database schema"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Create memories table
cursor.execute('''
CREATE TABLE IF NOT EXISTS memories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
memory_type TEXT,
metadata TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
importance INTEGER DEFAULT 5
)
''')
# Create user preferences table
cursor.execute('''
CREATE TABLE IF NOT EXISTS preferences (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Create context table for short-term memory
cursor.execute('''
CREATE TABLE IF NOT EXISTS context (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
conn.close()
def add_memory(
self,
content: str,
memory_type: str = 'general',
metadata: Dict[str, Any] = None,
importance: int = 5
) -> int:
"""
Add a memory to long-term storage
Args:
content: Memory content
memory_type: Type of memory (general, task, preference, etc.)
metadata: Additional metadata
importance: Importance score (1-10)
Returns:
Memory ID
"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
metadata_json = json.dumps(metadata) if metadata else '{}'
cursor.execute('''
INSERT INTO memories (content, memory_type, metadata, importance)
VALUES (?, ?, ?, ?)
''', (content, memory_type, metadata_json, importance))
memory_id = cursor.lastrowid
conn.commit()
conn.close()
return memory_id
def get_memories(
self,
memory_type: Optional[str] = None,
limit: int = 10,
min_importance: int = 0
) -> List[Dict[str, Any]]:
"""
Retrieve memories
Args:
memory_type: Filter by memory type
limit: Maximum number of memories to return
min_importance: Minimum importance score
Returns:
List of memories
"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
query = '''
SELECT id, content, memory_type, metadata, created_at, importance
FROM memories
WHERE importance >= ?
'''
params = [min_importance]
if memory_type:
query += ' AND memory_type = ?'
params.append(memory_type)
query += ' ORDER BY importance DESC, created_at DESC LIMIT ?'
params.append(limit)
cursor.execute(query, params)
rows = cursor.fetchall()
conn.close()
memories = []
for row in rows:
memories.append({
'id': row[0],
'content': row[1],
'memory_type': row[2],
'metadata': json.loads(row[3]),
'created_at': row[4],
'importance': row[5]
})
return memories
def get_relevant_memories(self, query: str, k: int = 5) -> str:
"""
Get memories relevant to a query
Args:
query: Search query
k: Number of memories to return
Returns:
Formatted string of relevant memories
"""
# Simple keyword-based search (can be enhanced with embeddings)
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Search for memories containing query keywords
keywords = query.lower().split()
memories = []
for keyword in keywords[:3]: # Limit to 3 keywords
cursor.execute('''
SELECT content, memory_type, importance
FROM memories
WHERE LOWER(content) LIKE ?
ORDER BY importance DESC
LIMIT ?
''', (f'%{keyword}%', k))
memories.extend(cursor.fetchall())
conn.close()
if not memories:
return "No relevant memories found."
# Format memories
unique_memories = list({m[0]: m for m in memories}.values())[:k]
formatted = []
for content, mem_type, importance in unique_memories:
formatted.append(f"[{mem_type}] {content}")
return "\n".join(formatted)
def set_preference(self, key: str, value: Any):
"""Set a user preference"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
value_json = json.dumps(value)
cursor.execute('''
INSERT OR REPLACE INTO preferences (key, value, updated_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
''', (key, value_json))
conn.commit()
conn.close()
def get_preference(self, key: str, default: Any = None) -> Any:
"""Get a user preference"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('SELECT value FROM preferences WHERE key = ?', (key,))
row = cursor.fetchone()
conn.close()
if row:
return json.loads(row[0])
return default
def get_all_preferences(self) -> Dict[str, Any]:
"""Get all user preferences"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('SELECT key, value FROM preferences')
rows = cursor.fetchall()
conn.close()
return {key: json.loads(value) for key, value in rows}
def add_context(self, session_id: str, content: str):
"""Add to short-term context"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO context (session_id, content)
VALUES (?, ?)
''', (session_id, content))
conn.commit()
conn.close()
def get_context(self, session_id: str, limit: int = 10) -> List[str]:
"""Get recent context for a session"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT content FROM context
WHERE session_id = ?
ORDER BY created_at DESC
LIMIT ?
''', (session_id, limit))
rows = cursor.fetchall()
conn.close()
return [row[0] for row in reversed(rows)]
def clear_old_context(self, days: int = 7):
"""Clear context older than specified days"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
DELETE FROM context
WHERE created_at < datetime('now', ? || ' days')
''', (f'-{days}',))
conn.commit()
conn.close() |