File size: 23,396 Bytes
80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 29048d9 80a97c8 |
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 |
# context_manager.py
import sqlite3
import json
import logging
import uuid
from datetime import datetime, timedelta
logger = logging.getLogger(__name__)
class EfficientContextManager:
def __init__(self, llm_router=None):
self.session_cache = {} # In-memory for active sessions
self.cache_config = {
"max_session_size": 10, # MB per session
"ttl": 3600, # 1 hour
"compression": "gzip",
"eviction_policy": "LRU"
}
self.db_path = "sessions.db"
self.llm_router = llm_router # For generating context summaries
logger.info(f"Initializing ContextManager with DB path: {self.db_path}")
self._init_database()
def _init_database(self):
"""Initialize database and create tables"""
try:
logger.info("Initializing database...")
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Create sessions table if not exists
cursor.execute("""
CREATE TABLE IF NOT EXISTS sessions (
session_id TEXT PRIMARY KEY,
user_id TEXT DEFAULT 'Test_Any',
created_at TIMESTAMP,
last_activity TIMESTAMP,
context_data TEXT,
user_metadata TEXT
)
""")
# Add user_id column to existing sessions table if it doesn't exist
try:
cursor.execute("ALTER TABLE sessions ADD COLUMN user_id TEXT DEFAULT 'Test_Any'")
logger.info("β Added user_id column to sessions table")
except sqlite3.OperationalError:
# Column already exists
pass
logger.info("β Sessions table ready")
# Create interactions table
cursor.execute("""
CREATE TABLE IF NOT EXISTS interactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT REFERENCES sessions(session_id),
user_input TEXT,
context_snapshot TEXT,
created_at TIMESTAMP,
FOREIGN KEY(session_id) REFERENCES sessions(session_id)
)
""")
logger.info("β Interactions table ready")
# Create user_contexts table (persistent user persona summaries)
cursor.execute("""
CREATE TABLE IF NOT EXISTS user_contexts (
user_id TEXT PRIMARY KEY,
persona_summary TEXT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
logger.info("β User contexts table ready")
# Create session_contexts table (session summaries)
cursor.execute("""
CREATE TABLE IF NOT EXISTS session_contexts (
session_id TEXT PRIMARY KEY,
user_id TEXT,
session_summary TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(session_id) REFERENCES sessions(session_id),
FOREIGN KEY(user_id) REFERENCES user_contexts(user_id)
)
""")
logger.info("β Session contexts table ready")
# Create interaction_contexts table (individual interaction summaries)
cursor.execute("""
CREATE TABLE IF NOT EXISTS interaction_contexts (
interaction_id TEXT PRIMARY KEY,
session_id TEXT,
user_input TEXT,
system_response TEXT,
interaction_summary TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(session_id) REFERENCES sessions(session_id)
)
""")
logger.info("β Interaction contexts table ready")
conn.commit()
conn.close()
logger.info("Database initialization complete")
except Exception as e:
logger.error(f"Database initialization error: {e}", exc_info=True)
async def manage_context(self, session_id: str, user_input: str, user_id: str = "Test_Any") -> dict:
"""
Efficient context management with user-based context system
STEP 1: Fetch User Context (if available)
STEP 2: Get Previous Interaction Contexts
STEP 3: Combine for workflow use
"""
# Level 1: In-memory session cache
cache_key = f"{session_id}_{user_id}"
context = self._get_from_memory_cache(cache_key)
if not context:
# Level 2: Database retrieval with user context
context = await self._retrieve_from_db(session_id, user_input, user_id)
# STEP 1: Fetch or generate User Context at session start (if first interaction in session)
if not context.get("user_context_loaded"):
user_context = await self.get_user_context(user_id)
context["user_context"] = user_context
context["user_context_loaded"] = True
# Cache warming
self._warm_memory_cache(cache_key, context)
# Update context with new interaction
updated_context = self._update_context(context, user_input, user_id=user_id)
return self._optimize_context(updated_context)
async def get_user_context(self, user_id: str) -> str:
"""
STEP 1: Fetch or generate User Context (500-token persona summary)
Available for all interactions except first time per user
"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Check if user context exists
cursor.execute("""
SELECT persona_summary FROM user_contexts WHERE user_id = ?
""", (user_id,))
row = cursor.fetchone()
if row and row[0]:
# Existing user context found
conn.close()
logger.info(f"β User context loaded for {user_id}")
return row[0]
# Generate new user context from all historical data
logger.info(f"Generating new user context for {user_id}")
# Fetch all historical Session and Interaction contexts for this user
all_session_summaries = []
all_interaction_summaries = []
# Get all session contexts
cursor.execute("""
SELECT session_summary FROM session_contexts WHERE user_id = ?
ORDER BY created_at DESC LIMIT 50
""", (user_id,))
for row in cursor.fetchall():
if row[0]:
all_session_summaries.append(row[0])
# Get all interaction contexts
cursor.execute("""
SELECT ic.interaction_summary
FROM interaction_contexts ic
JOIN sessions s ON ic.session_id = s.session_id
WHERE s.user_id = ?
ORDER BY ic.created_at DESC LIMIT 100
""", (user_id,))
for row in cursor.fetchall():
if row[0]:
all_interaction_summaries.append(row[0])
conn.close()
if not all_session_summaries and not all_interaction_summaries:
# First time user - no context to generate
logger.info(f"No historical data for {user_id} - first time user")
return ""
# Generate persona summary using LLM (500 tokens)
historical_data = "\n\n".join(all_session_summaries + all_interaction_summaries[:20])
if self.llm_router:
prompt = f"""Generate a concise 500-token persona summary for user {user_id} based on their interaction history:
Historical Context:
{historical_data}
Create a persona summary that captures:
- Communication style and preferences
- Common topics and interests
- Interaction patterns
- Key information shared across sessions
Keep the summary concise and focused (approximately 500 tokens)."""
try:
persona_summary = await self.llm_router.route_inference(
task_type="general_reasoning",
prompt=prompt,
max_tokens=500,
temperature=0.7
)
if persona_summary and isinstance(persona_summary, str) and persona_summary.strip():
# Store in database
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
INSERT OR REPLACE INTO user_contexts (user_id, persona_summary, updated_at)
VALUES (?, ?, ?)
""", (user_id, persona_summary.strip(), datetime.now().isoformat()))
conn.commit()
conn.close()
logger.info(f"β Generated and stored user context for {user_id}")
return persona_summary.strip()
except Exception as e:
logger.error(f"Error generating user context: {e}", exc_info=True)
# Fallback: Return empty if LLM fails
logger.warning(f"Could not generate user context for {user_id} - using empty")
return ""
except Exception as e:
logger.error(f"Error getting user context: {e}", exc_info=True)
return ""
async def generate_interaction_context(self, interaction_id: str, session_id: str,
user_input: str, system_response: str,
user_id: str = "Test_Any") -> str:
"""
STEP 2: Generate Interaction Context (50-token summary)
Called after each response
"""
try:
if not self.llm_router:
return ""
prompt = f"""Summarize this interaction in approximately 50 tokens:
User Input: {user_input[:200]}
System Response: {system_response[:300]}
Provide a brief summary capturing the key exchange."""
try:
summary = await self.llm_router.route_inference(
task_type="general_reasoning",
prompt=prompt,
max_tokens=50,
temperature=0.7
)
if summary and isinstance(summary, str) and summary.strip():
# Store in database
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
INSERT OR REPLACE INTO interaction_contexts
(interaction_id, session_id, user_input, system_response, interaction_summary, created_at)
VALUES (?, ?, ?, ?, ?, ?)
""", (
interaction_id,
session_id,
user_input[:500],
system_response[:1000],
summary.strip(),
datetime.now().isoformat()
))
conn.commit()
conn.close()
logger.info(f"β Generated interaction context for {interaction_id}")
return summary.strip()
except Exception as e:
logger.error(f"Error generating interaction context: {e}", exc_info=True)
# Fallback on LLM failure
return ""
except Exception as e:
logger.error(f"Error in generate_interaction_context: {e}", exc_info=True)
return ""
async def generate_session_context(self, session_id: str, user_id: str = "Test_Any") -> str:
"""
FINAL STEP: Generate Session Context (100-token summary)
Called at session end
"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Get all interaction contexts for this session
cursor.execute("""
SELECT interaction_summary FROM interaction_contexts
WHERE session_id = ?
ORDER BY created_at ASC
""", (session_id,))
interaction_summaries = [row[0] for row in cursor.fetchall() if row[0]]
conn.close()
if not interaction_summaries:
logger.info(f"No interactions to summarize for session {session_id}")
return ""
# Generate session summary using LLM (100 tokens)
if self.llm_router:
combined_context = "\n".join(interaction_summaries)
prompt = f"""Summarize this session's interactions in approximately 100 tokens:
Interaction Summaries:
{combined_context}
Create a concise session summary capturing:
- Main topics discussed
- Key outcomes or information shared
- User's focus areas
Keep the summary concise (approximately 100 tokens)."""
try:
session_summary = await self.llm_router.route_inference(
task_type="general_reasoning",
prompt=prompt,
max_tokens=100,
temperature=0.7
)
if session_summary and isinstance(session_summary, str) and session_summary.strip():
# Store in database
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
INSERT OR REPLACE INTO session_contexts
(session_id, user_id, session_summary, created_at)
VALUES (?, ?, ?, ?)
""", (session_id, user_id, session_summary.strip(), datetime.now().isoformat()))
conn.commit()
conn.close()
logger.info(f"β Generated session context for {session_id}")
return session_summary.strip()
except Exception as e:
logger.error(f"Error generating session context: {e}", exc_info=True)
# Fallback on LLM failure
return ""
except Exception as e:
logger.error(f"Error in generate_session_context: {e}", exc_info=True)
return ""
async def end_session(self, session_id: str, user_id: str = "Test_Any"):
"""
FINAL STEP: Generate Session Context and clear cache
"""
try:
# Generate session context
await self.generate_session_context(session_id, user_id)
# Clear in-memory cache for this session
cache_key = f"{session_id}_{user_id}"
if cache_key in self.session_cache:
del self.session_cache[cache_key]
logger.info(f"β Cleared cache for session {session_id}")
except Exception as e:
logger.error(f"Error ending session: {e}", exc_info=True)
def _optimize_context(self, context: dict) -> dict:
"""
Optimize context for LLM consumption
Format: [Interaction Context #N, #N-1, ...] + User Context
"""
user_context = context.get("user_context", "")
interaction_contexts = context.get("interaction_contexts", [])
# Format interaction contexts as requested
formatted_interactions = []
for idx, ic in enumerate(interaction_contexts[:10]): # Last 10 interactions
formatted_interactions.append(f"[Interaction Context #{len(interaction_contexts) - idx}]\n{ic.get('summary', '')}")
# Combine User Context + Interaction Contexts
combined_context = ""
if user_context:
combined_context += f"[User Context]\n{user_context}\n\n"
if formatted_interactions:
combined_context += "\n\n".join(formatted_interactions)
return {
"session_id": context.get("session_id"),
"user_id": context.get("user_id", "Test_Any"),
"user_context": user_context,
"interaction_contexts": interaction_contexts,
"combined_context": combined_context, # For direct use in prompts
"preferences": context.get("preferences", {}),
"active_tasks": context.get("active_tasks", []),
"last_activity": context.get("last_activity")
}
def _get_from_memory_cache(self, cache_key: str) -> dict:
"""
Retrieve context from in-memory session cache
"""
return self.session_cache.get(cache_key)
async def _retrieve_from_db(self, session_id: str, user_input: str, user_id: str = "Test_Any") -> dict:
"""
Retrieve context from database with semantic search
"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Get session data
cursor.execute("""
SELECT context_data, user_metadata, last_activity, user_id
FROM sessions
WHERE session_id = ?
""", (session_id,))
row = cursor.fetchone()
if row:
context_data = json.loads(row[0]) if row[0] else {}
user_metadata = json.loads(row[1]) if row[1] else {}
last_activity = row[2]
session_user_id = row[3] if len(row) > 3 else user_id
# Update user_id if it changed
if session_user_id != user_id:
cursor.execute("""
UPDATE sessions SET user_id = ? WHERE session_id = ?
""", (user_id, session_id))
conn.commit()
# Get previous interaction contexts for this session
cursor.execute("""
SELECT interaction_summary, created_at
FROM interaction_contexts
WHERE session_id = ?
ORDER BY created_at DESC
LIMIT 20
""", (session_id,))
interaction_contexts = []
for ic_row in cursor.fetchall():
if ic_row[0]:
interaction_contexts.append({
"summary": ic_row[0],
"timestamp": ic_row[1]
})
context = {
"session_id": session_id,
"user_id": user_id,
"interaction_contexts": interaction_contexts,
"preferences": user_metadata.get("preferences", {}),
"active_tasks": user_metadata.get("active_tasks", []),
"last_activity": last_activity,
"user_context_loaded": False # Will be loaded in manage_context
}
conn.close()
return context
else:
# Create new session
cursor.execute("""
INSERT INTO sessions (session_id, user_id, created_at, last_activity, context_data, user_metadata)
VALUES (?, ?, ?, ?, ?, ?)
""", (session_id, user_id, datetime.now().isoformat(), datetime.now().isoformat(), "{}", "{}"))
conn.commit()
conn.close()
return {
"session_id": session_id,
"user_id": user_id,
"interaction_contexts": [],
"preferences": {},
"active_tasks": [],
"user_context_loaded": False
}
except Exception as e:
logger.error(f"Database retrieval error: {e}", exc_info=True)
# Fallback to empty context
return {
"session_id": session_id,
"user_id": user_id,
"interaction_contexts": [],
"preferences": {},
"active_tasks": [],
"user_context_loaded": False
}
def _warm_memory_cache(self, cache_key: str, context: dict):
"""
Warm the in-memory cache with retrieved context
"""
self.session_cache[cache_key] = context
def _update_context(self, context: dict, user_input: str, response: str = None, user_id: str = "Test_Any") -> dict:
"""
Update context with new user interaction and persist to database
Note: Interaction context generation happens separately after response is generated
"""
try:
# Update session activity
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Update session last_activity
cursor.execute("""
UPDATE sessions
SET last_activity = ?, user_id = ?
WHERE session_id = ?
""", (datetime.now().isoformat(), user_id, context["session_id"]))
# Insert basic interaction record (for backward compatibility)
session_context = {
"preferences": context.get("preferences", {}),
"active_tasks": context.get("active_tasks", [])
}
cursor.execute("""
INSERT INTO interactions (session_id, user_input, context_snapshot, created_at)
VALUES (?, ?, ?, ?)
""", (context["session_id"], user_input, json.dumps(session_context), datetime.now().isoformat()))
conn.commit()
conn.close()
except Exception as e:
logger.error(f"Context update error: {e}", exc_info=True)
return context
def _extract_entities(self, context: dict) -> list:
"""
Extract essential entities from context
"""
# TODO: Implement entity extraction
return []
def _generate_summary(self, context: dict) -> str:
"""
Generate conversation summary
"""
# TODO: Implement summary generation
return ""
|