Commit
Β·
207f9f7
1
Parent(s):
93f44e2
cache key error when user id changes -fixed task 1 31_10_2025 v7
Browse files- app.py +67 -0
- cache_implementation.py +50 -1
- config.py +20 -0
- src/context_manager.py +120 -0
- src/llm_router.py +99 -0
- src/orchestrator_engine.py +145 -32
- test_context_flow.py +163 -0
app.py
CHANGED
|
@@ -525,6 +525,73 @@ def _update_skills_display(skills_html: str) -> Tuple[str, bool]:
|
|
| 525 |
else:
|
| 526 |
return "", False # Hide skills display
|
| 527 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
async def process_message_async(message: str, history: Optional[List], session_id: str, user_id: str = "Test_Any") -> Tuple[List, str, dict, dict, dict, str, str]:
|
| 529 |
"""
|
| 530 |
Process message with full orchestration system
|
|
|
|
| 525 |
else:
|
| 526 |
return "", False # Hide skills display
|
| 527 |
|
| 528 |
+
def process_with_metrics(message, session_id, user_id):
|
| 529 |
+
"""Process message with detailed metrics"""
|
| 530 |
+
import json
|
| 531 |
+
import time
|
| 532 |
+
|
| 533 |
+
metrics = {
|
| 534 |
+
'start_time': time.time(),
|
| 535 |
+
'context_size_before': 0,
|
| 536 |
+
'context_size_after': 0,
|
| 537 |
+
'llm_calls': [],
|
| 538 |
+
'cache_hits': 0,
|
| 539 |
+
'cache_misses': 0
|
| 540 |
+
}
|
| 541 |
+
|
| 542 |
+
try:
|
| 543 |
+
global orchestrator
|
| 544 |
+
if orchestrator is None:
|
| 545 |
+
return None
|
| 546 |
+
|
| 547 |
+
# Get initial context size
|
| 548 |
+
context = orchestrator.context_manager.get_or_create_session_context(session_id, user_id)
|
| 549 |
+
metrics['context_size_before'] = len(str(context))
|
| 550 |
+
|
| 551 |
+
# Track LLM calls by wrapping the route_inference method
|
| 552 |
+
original_route = orchestrator.llm_router.route_inference
|
| 553 |
+
|
| 554 |
+
async def tracked_route(*args, **kwargs):
|
| 555 |
+
start = time.time()
|
| 556 |
+
result = await original_route(*args, **kwargs)
|
| 557 |
+
metrics['llm_calls'].append({
|
| 558 |
+
'task': kwargs.get('task_type', 'unknown'),
|
| 559 |
+
'duration': time.time() - start,
|
| 560 |
+
'model': result.get('model', 'unknown') if isinstance(result, dict) else 'unknown'
|
| 561 |
+
})
|
| 562 |
+
return result
|
| 563 |
+
|
| 564 |
+
# Temporarily replace route_inference (would need to restore after)
|
| 565 |
+
# Note: This is a simplified version - in production you'd use a decorator or wrapper
|
| 566 |
+
|
| 567 |
+
# Process message
|
| 568 |
+
import asyncio
|
| 569 |
+
loop = asyncio.new_event_loop()
|
| 570 |
+
asyncio.set_event_loop(loop)
|
| 571 |
+
response = loop.run_until_complete(
|
| 572 |
+
orchestrator.process_request(
|
| 573 |
+
session_id=session_id,
|
| 574 |
+
user_input=message
|
| 575 |
+
)
|
| 576 |
+
)
|
| 577 |
+
|
| 578 |
+
# Get final context size
|
| 579 |
+
context = orchestrator.context_manager.get_or_create_session_context(session_id, user_id)
|
| 580 |
+
metrics['context_size_after'] = len(str(context))
|
| 581 |
+
|
| 582 |
+
# Calculate metrics
|
| 583 |
+
metrics['total_duration'] = time.time() - metrics['start_time']
|
| 584 |
+
metrics['context_growth'] = metrics['context_size_after'] - metrics['context_size_before']
|
| 585 |
+
|
| 586 |
+
# Log metrics
|
| 587 |
+
logger.info(f"Processing Metrics: {json.dumps(metrics, indent=2)}")
|
| 588 |
+
|
| 589 |
+
return response
|
| 590 |
+
|
| 591 |
+
except Exception as e:
|
| 592 |
+
logger.error(f"Error in process_with_metrics: {e}", exc_info=True)
|
| 593 |
+
return None
|
| 594 |
+
|
| 595 |
async def process_message_async(message: str, history: Optional[List], session_id: str, user_id: str = "Test_Any") -> Tuple[List, str, dict, dict, dict, str, str]:
|
| 596 |
"""
|
| 597 |
Process message with full orchestration system
|
cache_implementation.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# cache_implementation.py
|
| 2 |
import time
|
| 3 |
-
from typing import Optional
|
|
|
|
| 4 |
|
| 5 |
class SessionCache:
|
| 6 |
def __init__(self):
|
|
@@ -77,3 +78,51 @@ class SessionCache:
|
|
| 77 |
# For now, return as-is
|
| 78 |
return data
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# cache_implementation.py
|
| 2 |
import time
|
| 3 |
+
from typing import Optional, Dict
|
| 4 |
+
from collections import OrderedDict
|
| 5 |
|
| 6 |
class SessionCache:
|
| 7 |
def __init__(self):
|
|
|
|
| 78 |
# For now, return as-is
|
| 79 |
return data
|
| 80 |
|
| 81 |
+
class ContextCache:
|
| 82 |
+
"""LRU cache for context management"""
|
| 83 |
+
|
| 84 |
+
def __init__(self, max_size: int = 100):
|
| 85 |
+
self.cache = OrderedDict()
|
| 86 |
+
self.max_size = max_size
|
| 87 |
+
self.hit_count = 0
|
| 88 |
+
self.miss_count = 0
|
| 89 |
+
|
| 90 |
+
def get(self, key: str) -> Optional[Dict]:
|
| 91 |
+
"""Get from cache with LRU update"""
|
| 92 |
+
if key in self.cache:
|
| 93 |
+
self.hit_count += 1
|
| 94 |
+
# Move to end (most recently used)
|
| 95 |
+
self.cache.move_to_end(key)
|
| 96 |
+
return self.cache[key]
|
| 97 |
+
|
| 98 |
+
self.miss_count += 1
|
| 99 |
+
return None
|
| 100 |
+
|
| 101 |
+
def put(self, key: str, value: Dict):
|
| 102 |
+
"""Add to cache with size management"""
|
| 103 |
+
if key in self.cache:
|
| 104 |
+
# Update existing
|
| 105 |
+
self.cache.move_to_end(key)
|
| 106 |
+
|
| 107 |
+
self.cache[key] = value
|
| 108 |
+
|
| 109 |
+
# Evict oldest if needed
|
| 110 |
+
if len(self.cache) > self.max_size:
|
| 111 |
+
self.cache.popitem(last=False)
|
| 112 |
+
|
| 113 |
+
@property
|
| 114 |
+
def hit_rate(self) -> float:
|
| 115 |
+
"""Calculate cache hit rate"""
|
| 116 |
+
total = self.hit_count + self.miss_count
|
| 117 |
+
return self.hit_count / total if total > 0 else 0.0
|
| 118 |
+
|
| 119 |
+
def clear(self):
|
| 120 |
+
"""Clear all cache entries"""
|
| 121 |
+
self.cache.clear()
|
| 122 |
+
self.hit_count = 0
|
| 123 |
+
self.miss_count = 0
|
| 124 |
+
|
| 125 |
+
def size(self) -> int:
|
| 126 |
+
"""Get current cache size"""
|
| 127 |
+
return len(self.cache)
|
| 128 |
+
|
config.py
CHANGED
|
@@ -41,3 +41,23 @@ class Settings(BaseSettings):
|
|
| 41 |
|
| 42 |
settings = Settings()
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
settings = Settings()
|
| 43 |
|
| 44 |
+
# Context configuration
|
| 45 |
+
CONTEXT_CONFIG = {
|
| 46 |
+
'max_context_tokens': int(os.getenv("MAX_CONTEXT_TOKENS", "4000")),
|
| 47 |
+
'cache_ttl_seconds': int(os.getenv("CACHE_TTL_SECONDS", "300")),
|
| 48 |
+
'max_cache_size': int(os.getenv("MAX_CACHE_SIZE", "100")),
|
| 49 |
+
'parallel_processing': os.getenv("PARALLEL_PROCESSING", "True").lower() == "true",
|
| 50 |
+
'context_decay_factor': float(os.getenv("CONTEXT_DECAY_FACTOR", "0.8")),
|
| 51 |
+
'max_interactions_to_keep': int(os.getenv("MAX_INTERACTIONS_TO_KEEP", "10")),
|
| 52 |
+
'enable_metrics': os.getenv("ENABLE_METRICS", "True").lower() == "true",
|
| 53 |
+
'compression_enabled': os.getenv("COMPRESSION_ENABLED", "True").lower() == "true",
|
| 54 |
+
'summarization_threshold': int(os.getenv("SUMMARIZATION_THRESHOLD", "2000")) # tokens
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# Model selection for context operations
|
| 58 |
+
CONTEXT_MODELS = {
|
| 59 |
+
'summarization': os.getenv("CONTEXT_SUMMARIZATION_MODEL", "Qwen/Qwen2.5-7B-Instruct"),
|
| 60 |
+
'intent': os.getenv("CONTEXT_INTENT_MODEL", "Qwen/Qwen2.5-7B-Instruct"),
|
| 61 |
+
'synthesis': os.getenv("CONTEXT_SYNTHESIS_MODEL", "Qwen/Qwen2.5-72B-Instruct")
|
| 62 |
+
}
|
| 63 |
+
|
src/context_manager.py
CHANGED
|
@@ -7,6 +7,7 @@ import hashlib
|
|
| 7 |
import threading
|
| 8 |
from contextlib import contextmanager
|
| 9 |
from datetime import datetime, timedelta
|
|
|
|
| 10 |
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
|
@@ -49,6 +50,7 @@ class TransactionManager:
|
|
| 49 |
class EfficientContextManager:
|
| 50 |
def __init__(self, llm_router=None):
|
| 51 |
self.session_cache = {} # In-memory for active sessions
|
|
|
|
| 52 |
self.cache_config = {
|
| 53 |
"max_session_size": 10, # MB per session
|
| 54 |
"ttl": 3600, # 1 hour
|
|
@@ -1228,3 +1230,121 @@ Keep the summary concise (approximately 100 tokens)."""
|
|
| 1228 |
"""
|
| 1229 |
# TODO: Implement summary generation
|
| 1230 |
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import threading
|
| 8 |
from contextlib import contextmanager
|
| 9 |
from datetime import datetime, timedelta
|
| 10 |
+
from typing import Dict, Optional, List
|
| 11 |
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
|
|
| 50 |
class EfficientContextManager:
|
| 51 |
def __init__(self, llm_router=None):
|
| 52 |
self.session_cache = {} # In-memory for active sessions
|
| 53 |
+
self._session_cache = {} # Enhanced in-memory cache with timestamps
|
| 54 |
self.cache_config = {
|
| 55 |
"max_session_size": 10, # MB per session
|
| 56 |
"ttl": 3600, # 1 hour
|
|
|
|
| 1230 |
"""
|
| 1231 |
# TODO: Implement summary generation
|
| 1232 |
return ""
|
| 1233 |
+
|
| 1234 |
+
def get_or_create_session_context(self, session_id: str, user_id: Optional[str] = None) -> Dict:
|
| 1235 |
+
"""Enhanced context retrieval with caching"""
|
| 1236 |
+
import time
|
| 1237 |
+
|
| 1238 |
+
# In-memory cache check first
|
| 1239 |
+
if session_id in self._session_cache:
|
| 1240 |
+
cache_entry = self._session_cache[session_id]
|
| 1241 |
+
if time.time() - cache_entry['timestamp'] < 300: # 5 min cache
|
| 1242 |
+
logger.debug(f"Cache hit for session {session_id}")
|
| 1243 |
+
return cache_entry['context']
|
| 1244 |
+
|
| 1245 |
+
# Batch database queries
|
| 1246 |
+
conn = None
|
| 1247 |
+
try:
|
| 1248 |
+
conn = sqlite3.connect(self.db_path)
|
| 1249 |
+
cursor = conn.cursor()
|
| 1250 |
+
|
| 1251 |
+
# Single query for all context data
|
| 1252 |
+
query = """
|
| 1253 |
+
SELECT
|
| 1254 |
+
s.session_data,
|
| 1255 |
+
s.user_metadata,
|
| 1256 |
+
s.last_activity,
|
| 1257 |
+
u.user_profile,
|
| 1258 |
+
i.interaction_data,
|
| 1259 |
+
ic.context_summary
|
| 1260 |
+
FROM sessions s
|
| 1261 |
+
LEFT JOIN user_contexts u ON s.user_id = u.user_id
|
| 1262 |
+
LEFT JOIN interactions i ON s.session_id = i.session_id
|
| 1263 |
+
LEFT JOIN interaction_contexts ic ON i.session_id = ic.session_id
|
| 1264 |
+
WHERE s.session_id = ?
|
| 1265 |
+
ORDER BY i.created_at DESC
|
| 1266 |
+
LIMIT 10
|
| 1267 |
+
"""
|
| 1268 |
+
|
| 1269 |
+
cursor.execute(query, (session_id,))
|
| 1270 |
+
results = cursor.fetchall()
|
| 1271 |
+
|
| 1272 |
+
# Process results efficiently
|
| 1273 |
+
context = self._build_context_from_results(results, session_id, user_id)
|
| 1274 |
+
|
| 1275 |
+
# Update cache
|
| 1276 |
+
self._session_cache[session_id] = {
|
| 1277 |
+
'context': context,
|
| 1278 |
+
'timestamp': time.time()
|
| 1279 |
+
}
|
| 1280 |
+
|
| 1281 |
+
return context
|
| 1282 |
+
|
| 1283 |
+
except Exception as e:
|
| 1284 |
+
logger.error(f"Error in get_or_create_session_context: {e}", exc_info=True)
|
| 1285 |
+
# Return safe fallback
|
| 1286 |
+
return {
|
| 1287 |
+
"session_id": session_id,
|
| 1288 |
+
"user_id": user_id or "Test_Any",
|
| 1289 |
+
"interaction_contexts": [],
|
| 1290 |
+
"session_context": None,
|
| 1291 |
+
"preferences": {},
|
| 1292 |
+
"active_tasks": [],
|
| 1293 |
+
"user_context_loaded": False
|
| 1294 |
+
}
|
| 1295 |
+
finally:
|
| 1296 |
+
if conn:
|
| 1297 |
+
conn.close()
|
| 1298 |
+
|
| 1299 |
+
def _build_context_from_results(self, results: list, session_id: str, user_id: Optional[str]) -> Dict:
|
| 1300 |
+
"""Build context dictionary from batch query results"""
|
| 1301 |
+
context = {
|
| 1302 |
+
"session_id": session_id,
|
| 1303 |
+
"user_id": user_id or "Test_Any",
|
| 1304 |
+
"interaction_contexts": [],
|
| 1305 |
+
"session_context": None,
|
| 1306 |
+
"user_context": "",
|
| 1307 |
+
"preferences": {},
|
| 1308 |
+
"active_tasks": [],
|
| 1309 |
+
"user_context_loaded": False
|
| 1310 |
+
}
|
| 1311 |
+
|
| 1312 |
+
if not results:
|
| 1313 |
+
return context
|
| 1314 |
+
|
| 1315 |
+
# Process first row for session data
|
| 1316 |
+
first_row = results[0]
|
| 1317 |
+
if first_row[0]: # session_data
|
| 1318 |
+
try:
|
| 1319 |
+
session_data = json.loads(first_row[0])
|
| 1320 |
+
context["preferences"] = session_data.get("preferences", {})
|
| 1321 |
+
context["active_tasks"] = session_data.get("active_tasks", [])
|
| 1322 |
+
except:
|
| 1323 |
+
pass
|
| 1324 |
+
|
| 1325 |
+
if first_row[1]: # user_metadata
|
| 1326 |
+
try:
|
| 1327 |
+
user_metadata = json.loads(first_row[1])
|
| 1328 |
+
context["preferences"].update(user_metadata.get("preferences", {}))
|
| 1329 |
+
except:
|
| 1330 |
+
pass
|
| 1331 |
+
|
| 1332 |
+
context["last_activity"] = first_row[2] # last_activity
|
| 1333 |
+
|
| 1334 |
+
if first_row[3]: # user_profile
|
| 1335 |
+
context["user_context"] = first_row[3]
|
| 1336 |
+
context["user_context_loaded"] = True
|
| 1337 |
+
|
| 1338 |
+
# Process interaction contexts
|
| 1339 |
+
seen_interactions = set()
|
| 1340 |
+
for row in results:
|
| 1341 |
+
if row[5]: # context_summary
|
| 1342 |
+
# Deduplicate interactions
|
| 1343 |
+
if row[5] not in seen_interactions:
|
| 1344 |
+
seen_interactions.add(row[5])
|
| 1345 |
+
context["interaction_contexts"].append({
|
| 1346 |
+
"summary": row[5],
|
| 1347 |
+
"timestamp": None # Could extract from row if available
|
| 1348 |
+
})
|
| 1349 |
+
|
| 1350 |
+
return context
|
src/llm_router.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# llm_router.py - FIXED VERSION
|
| 2 |
import logging
|
| 3 |
import asyncio
|
|
|
|
| 4 |
from .models_config import LLM_CONFIG
|
| 5 |
|
| 6 |
logger = logging.getLogger(__name__)
|
|
@@ -256,3 +257,101 @@ class LLMRouter:
|
|
| 256 |
}
|
| 257 |
|
| 258 |
return health_status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# llm_router.py - FIXED VERSION
|
| 2 |
import logging
|
| 3 |
import asyncio
|
| 4 |
+
from typing import Dict
|
| 5 |
from .models_config import LLM_CONFIG
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
|
|
|
| 257 |
}
|
| 258 |
|
| 259 |
return health_status
|
| 260 |
+
|
| 261 |
+
def prepare_context_for_llm(self, raw_context: Dict, max_tokens: int = 4000) -> str:
|
| 262 |
+
"""Smart context windowing for LLM calls"""
|
| 263 |
+
|
| 264 |
+
try:
|
| 265 |
+
from transformers import AutoTokenizer
|
| 266 |
+
|
| 267 |
+
# Initialize tokenizer lazily
|
| 268 |
+
if not hasattr(self, 'tokenizer'):
|
| 269 |
+
try:
|
| 270 |
+
self.tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct")
|
| 271 |
+
except Exception as e:
|
| 272 |
+
logger.warning(f"Could not load tokenizer: {e}, using character count estimation")
|
| 273 |
+
self.tokenizer = None
|
| 274 |
+
except ImportError:
|
| 275 |
+
logger.warning("transformers library not available, using character count estimation")
|
| 276 |
+
self.tokenizer = None
|
| 277 |
+
|
| 278 |
+
# Priority order for context elements
|
| 279 |
+
priority_elements = [
|
| 280 |
+
('current_query', 1.0),
|
| 281 |
+
('recent_interactions', 0.8),
|
| 282 |
+
('user_preferences', 0.6),
|
| 283 |
+
('session_summary', 0.4),
|
| 284 |
+
('historical_context', 0.2)
|
| 285 |
+
]
|
| 286 |
+
|
| 287 |
+
formatted_context = []
|
| 288 |
+
total_tokens = 0
|
| 289 |
+
|
| 290 |
+
for element, priority in priority_elements:
|
| 291 |
+
# Map element names to context keys
|
| 292 |
+
element_key_map = {
|
| 293 |
+
'current_query': raw_context.get('user_input', ''),
|
| 294 |
+
'recent_interactions': raw_context.get('interaction_contexts', []),
|
| 295 |
+
'user_preferences': raw_context.get('preferences', {}),
|
| 296 |
+
'session_summary': raw_context.get('session_context', {}),
|
| 297 |
+
'historical_context': raw_context.get('user_context', '')
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
content = element_key_map.get(element, '')
|
| 301 |
+
|
| 302 |
+
# Convert to string if needed
|
| 303 |
+
if isinstance(content, dict):
|
| 304 |
+
content = str(content)
|
| 305 |
+
elif isinstance(content, list):
|
| 306 |
+
content = "\n".join([str(item) for item in content[:10]]) # Limit to 10 items
|
| 307 |
+
|
| 308 |
+
if not content:
|
| 309 |
+
continue
|
| 310 |
+
|
| 311 |
+
# Estimate tokens
|
| 312 |
+
if self.tokenizer:
|
| 313 |
+
try:
|
| 314 |
+
tokens = len(self.tokenizer.encode(content))
|
| 315 |
+
except:
|
| 316 |
+
# Fallback to character-based estimation (rough: 1 token β 4 chars)
|
| 317 |
+
tokens = len(content) // 4
|
| 318 |
+
else:
|
| 319 |
+
# Character-based estimation (rough: 1 token β 4 chars)
|
| 320 |
+
tokens = len(content) // 4
|
| 321 |
+
|
| 322 |
+
if total_tokens + tokens <= max_tokens:
|
| 323 |
+
formatted_context.append(f"=== {element.upper()} ===\n{content}")
|
| 324 |
+
total_tokens += tokens
|
| 325 |
+
elif priority > 0.5: # Critical elements - truncate if needed
|
| 326 |
+
available = max_tokens - total_tokens
|
| 327 |
+
if available > 100: # Only truncate if we have meaningful space
|
| 328 |
+
truncated = self._truncate_to_tokens(content, available)
|
| 329 |
+
formatted_context.append(f"=== {element.upper()} (TRUNCATED) ===\n{truncated}")
|
| 330 |
+
break
|
| 331 |
+
|
| 332 |
+
return "\n\n".join(formatted_context)
|
| 333 |
+
|
| 334 |
+
def _truncate_to_tokens(self, content: str, max_tokens: int) -> str:
|
| 335 |
+
"""Truncate content to fit within token limit"""
|
| 336 |
+
if not self.tokenizer:
|
| 337 |
+
# Simple character-based truncation
|
| 338 |
+
max_chars = max_tokens * 4
|
| 339 |
+
if len(content) <= max_chars:
|
| 340 |
+
return content
|
| 341 |
+
return content[:max_chars-3] + "..."
|
| 342 |
+
|
| 343 |
+
try:
|
| 344 |
+
# Tokenize and truncate
|
| 345 |
+
tokens = self.tokenizer.encode(content)
|
| 346 |
+
if len(tokens) <= max_tokens:
|
| 347 |
+
return content
|
| 348 |
+
|
| 349 |
+
truncated_tokens = tokens[:max_tokens-3] # Leave room for "..."
|
| 350 |
+
truncated_text = self.tokenizer.decode(truncated_tokens)
|
| 351 |
+
return truncated_text + "..."
|
| 352 |
+
except Exception as e:
|
| 353 |
+
logger.warning(f"Error truncating with tokenizer: {e}, using character truncation")
|
| 354 |
+
max_chars = max_tokens * 4
|
| 355 |
+
if len(content) <= max_chars:
|
| 356 |
+
return content
|
| 357 |
+
return content[:max_chars-3] + "..."
|
src/orchestrator_engine.py
CHANGED
|
@@ -4,6 +4,8 @@ import logging
|
|
| 4 |
import time
|
| 5 |
import asyncio
|
| 6 |
from datetime import datetime
|
|
|
|
|
|
|
| 7 |
import sys
|
| 8 |
import os
|
| 9 |
|
|
@@ -199,39 +201,50 @@ class MVPOrchestrator:
|
|
| 199 |
"reasoning": f"Context analysis shows user is focused on {main_topic} with {interaction_contexts_count} previous interaction contexts and {'existing' if has_user_context else 'new'} user context"
|
| 200 |
}
|
| 201 |
|
| 202 |
-
# Step 3: Intent
|
| 203 |
-
|
| 204 |
-
self
|
| 205 |
-
"step": "intent_recognition",
|
| 206 |
-
"agent": "intent_recognition",
|
| 207 |
-
"status": "executing"
|
| 208 |
-
})
|
| 209 |
-
intent_result = await self.agents['intent_recognition'].execute(
|
| 210 |
-
user_input=user_input,
|
| 211 |
-
context=context
|
| 212 |
-
)
|
| 213 |
-
self.execution_trace[-1].update({
|
| 214 |
-
"status": "completed",
|
| 215 |
-
"result": {"primary_intent": intent_result.get('primary_intent', 'unknown')}
|
| 216 |
-
})
|
| 217 |
-
logger.info(f"Intent detected: {intent_result.get('primary_intent', 'unknown')}")
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
# Add skills reasoning to chain
|
| 237 |
reasoning_chain["chain_of_thought"]["step_2_5"] = {
|
|
@@ -680,6 +693,106 @@ This response has been flagged for potential safety concerns:
|
|
| 680 |
|
| 681 |
return " | ".join(summary_parts) if summary_parts else "No prior context"
|
| 682 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 683 |
def _build_task_prompts(self, user_input: str, context_summary: str, primary_intent: str) -> dict:
|
| 684 |
"""Build task-specific prompts for execution"""
|
| 685 |
|
|
|
|
| 4 |
import time
|
| 5 |
import asyncio
|
| 6 |
from datetime import datetime
|
| 7 |
+
from typing import List, Dict, Optional
|
| 8 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
import sys
|
| 10 |
import os
|
| 11 |
|
|
|
|
| 201 |
"reasoning": f"Context analysis shows user is focused on {main_topic} with {interaction_contexts_count} previous interaction contexts and {'existing' if has_user_context else 'new'} user context"
|
| 202 |
}
|
| 203 |
|
| 204 |
+
# Step 3: Parallel Intent, Skills, and Safety Assessment
|
| 205 |
+
# Check if parallel processing is enabled (can be controlled via config)
|
| 206 |
+
use_parallel = getattr(self, '_parallel_processing_enabled', True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
+
if use_parallel:
|
| 209 |
+
logger.info("Step 3: Processing intent, skills, and safety in parallel...")
|
| 210 |
+
parallel_results = await self.process_request_parallel(session_id, user_input, context)
|
| 211 |
+
intent_result = parallel_results.get('intent', {})
|
| 212 |
+
skills_result = parallel_results.get('skills', {})
|
| 213 |
+
# Safety will be checked later on the response
|
| 214 |
+
else:
|
| 215 |
+
# Sequential processing (fallback)
|
| 216 |
+
logger.info("Step 3: Recognizing intent...")
|
| 217 |
+
self.execution_trace.append({
|
| 218 |
+
"step": "intent_recognition",
|
| 219 |
+
"agent": "intent_recognition",
|
| 220 |
+
"status": "executing"
|
| 221 |
+
})
|
| 222 |
+
intent_result = await self.agents['intent_recognition'].execute(
|
| 223 |
+
user_input=user_input,
|
| 224 |
+
context=context
|
| 225 |
+
)
|
| 226 |
+
self.execution_trace[-1].update({
|
| 227 |
+
"status": "completed",
|
| 228 |
+
"result": {"primary_intent": intent_result.get('primary_intent', 'unknown')}
|
| 229 |
+
})
|
| 230 |
+
logger.info(f"Intent detected: {intent_result.get('primary_intent', 'unknown')}")
|
| 231 |
+
|
| 232 |
+
# Step 3.5: Skills Identification
|
| 233 |
+
logger.info("Step 3.5: Identifying relevant skills...")
|
| 234 |
+
self.execution_trace.append({
|
| 235 |
+
"step": "skills_identification",
|
| 236 |
+
"agent": "skills_identification",
|
| 237 |
+
"status": "executing"
|
| 238 |
+
})
|
| 239 |
+
skills_result = await self.agents['skills_identification'].execute(
|
| 240 |
+
user_input=user_input,
|
| 241 |
+
context=context
|
| 242 |
+
)
|
| 243 |
+
self.execution_trace[-1].update({
|
| 244 |
+
"status": "completed",
|
| 245 |
+
"result": {"skills_count": len(skills_result.get('identified_skills', []))}
|
| 246 |
+
})
|
| 247 |
+
logger.info(f"Skills identified: {len(skills_result.get('identified_skills', []))} skills")
|
| 248 |
|
| 249 |
# Add skills reasoning to chain
|
| 250 |
reasoning_chain["chain_of_thought"]["step_2_5"] = {
|
|
|
|
| 693 |
|
| 694 |
return " | ".join(summary_parts) if summary_parts else "No prior context"
|
| 695 |
|
| 696 |
+
async def process_request_parallel(self, session_id: str, user_input: str, context: Dict) -> Dict:
|
| 697 |
+
"""Process intent, skills, and safety in parallel"""
|
| 698 |
+
|
| 699 |
+
# Run agents in parallel using asyncio.gather
|
| 700 |
+
try:
|
| 701 |
+
intent_task = self.agents['intent_recognition'].execute(
|
| 702 |
+
user_input=user_input,
|
| 703 |
+
context=context
|
| 704 |
+
)
|
| 705 |
+
|
| 706 |
+
skills_task = self.agents['skills_identification'].execute(
|
| 707 |
+
user_input=user_input,
|
| 708 |
+
context=context
|
| 709 |
+
)
|
| 710 |
+
|
| 711 |
+
# Safety check on user input (pre-check)
|
| 712 |
+
safety_task = self.agents['safety_check'].execute(
|
| 713 |
+
response=user_input,
|
| 714 |
+
context=context
|
| 715 |
+
)
|
| 716 |
+
|
| 717 |
+
# Wait for all to complete
|
| 718 |
+
results = await asyncio.gather(
|
| 719 |
+
intent_task,
|
| 720 |
+
skills_task,
|
| 721 |
+
safety_task,
|
| 722 |
+
return_exceptions=True
|
| 723 |
+
)
|
| 724 |
+
|
| 725 |
+
# Handle results
|
| 726 |
+
intent_result = results[0] if not isinstance(results[0], Exception) else {}
|
| 727 |
+
skills_result = results[1] if not isinstance(results[1], Exception) else {}
|
| 728 |
+
safety_result = results[2] if not isinstance(results[2], Exception) else {}
|
| 729 |
+
|
| 730 |
+
# Log any exceptions
|
| 731 |
+
if isinstance(results[0], Exception):
|
| 732 |
+
logger.error(f"Intent recognition error: {results[0]}")
|
| 733 |
+
if isinstance(results[1], Exception):
|
| 734 |
+
logger.error(f"Skills identification error: {results[1]}")
|
| 735 |
+
if isinstance(results[2], Exception):
|
| 736 |
+
logger.error(f"Safety check error: {results[2]}")
|
| 737 |
+
|
| 738 |
+
return {
|
| 739 |
+
'intent': intent_result,
|
| 740 |
+
'skills': skills_result,
|
| 741 |
+
'safety_precheck': safety_result
|
| 742 |
+
}
|
| 743 |
+
|
| 744 |
+
except Exception as e:
|
| 745 |
+
logger.error(f"Error in parallel processing: {e}", exc_info=True)
|
| 746 |
+
# Fallback to sequential processing
|
| 747 |
+
return {
|
| 748 |
+
'intent': await self.agents['intent_recognition'].execute(user_input=user_input, context=context),
|
| 749 |
+
'skills': await self.agents['skills_identification'].execute(user_input=user_input, context=context),
|
| 750 |
+
'safety_precheck': {}
|
| 751 |
+
}
|
| 752 |
+
|
| 753 |
+
def _build_enhanced_context(self, session_id: str, prior_interactions: List[Dict]) -> Dict:
|
| 754 |
+
"""Build enhanced context with memory accumulation"""
|
| 755 |
+
|
| 756 |
+
# Intelligent context summarization
|
| 757 |
+
context = {
|
| 758 |
+
'session_memory': [],
|
| 759 |
+
'user_preferences': {},
|
| 760 |
+
'interaction_patterns': {},
|
| 761 |
+
'skills_used': set()
|
| 762 |
+
}
|
| 763 |
+
|
| 764 |
+
# Process prior interactions with decay
|
| 765 |
+
for idx, interaction in enumerate(prior_interactions):
|
| 766 |
+
weight = 1.0 / (idx + 1) # Recent interactions weighted more
|
| 767 |
+
|
| 768 |
+
# Extract key information
|
| 769 |
+
if 'skills' in interaction:
|
| 770 |
+
for skill in interaction['skills']:
|
| 771 |
+
if isinstance(skill, dict):
|
| 772 |
+
context['skills_used'].add(skill.get('name', skill.get('skill', '')))
|
| 773 |
+
elif isinstance(skill, str):
|
| 774 |
+
context['skills_used'].add(skill)
|
| 775 |
+
|
| 776 |
+
# Accumulate patterns
|
| 777 |
+
if 'intent' in interaction:
|
| 778 |
+
intent = interaction['intent']
|
| 779 |
+
if intent not in context['interaction_patterns']:
|
| 780 |
+
context['interaction_patterns'][intent] = 0
|
| 781 |
+
context['interaction_patterns'][intent] += weight
|
| 782 |
+
|
| 783 |
+
# Build memory summary
|
| 784 |
+
if idx < 3: # Keep last 3 interactions in detail
|
| 785 |
+
context['session_memory'].append({
|
| 786 |
+
'summary': interaction.get('summary', ''),
|
| 787 |
+
'timestamp': interaction.get('timestamp'),
|
| 788 |
+
'relevance': weight
|
| 789 |
+
})
|
| 790 |
+
|
| 791 |
+
# Convert skills_used set to list for JSON serialization
|
| 792 |
+
context['skills_used'] = list(context['skills_used'])
|
| 793 |
+
|
| 794 |
+
return context
|
| 795 |
+
|
| 796 |
def _build_task_prompts(self, user_input: str, context_summary: str, primary_intent: str) -> dict:
|
| 797 |
"""Build task-specific prompts for execution"""
|
| 798 |
|
test_context_flow.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test script for context building across two turns
|
| 3 |
+
"""
|
| 4 |
+
import time
|
| 5 |
+
import json
|
| 6 |
+
import asyncio
|
| 7 |
+
import sys
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Add project paths
|
| 11 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 12 |
+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src'))
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
from src.orchestrator_engine import MVPOrchestrator
|
| 16 |
+
from src.context_manager import EfficientContextManager
|
| 17 |
+
from src.llm_router import LLMRouter
|
| 18 |
+
from src.agents.intent_agent import create_intent_agent
|
| 19 |
+
from src.agents.synthesis_agent import create_synthesis_agent
|
| 20 |
+
from src.agents.safety_agent import create_safety_agent
|
| 21 |
+
from src.agents.skills_identification_agent import create_skills_identification_agent
|
| 22 |
+
except ImportError as e:
|
| 23 |
+
print(f"Error importing modules: {e}")
|
| 24 |
+
print("Please ensure all dependencies are installed and paths are correct.")
|
| 25 |
+
sys.exit(1)
|
| 26 |
+
|
| 27 |
+
def test_two_turn_conversation():
|
| 28 |
+
"""Test context building across two turns"""
|
| 29 |
+
|
| 30 |
+
print("=" * 60)
|
| 31 |
+
print("TWO-TURN CONVERSATION CONTEXT TEST")
|
| 32 |
+
print("=" * 60)
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# Initialize components
|
| 36 |
+
print("\n[1/4] Initializing components...")
|
| 37 |
+
hf_token = os.getenv('HF_TOKEN', '')
|
| 38 |
+
if not hf_token:
|
| 39 |
+
print("WARNING: HF_TOKEN not set. Some tests may fail.")
|
| 40 |
+
|
| 41 |
+
llm_router = LLMRouter(hf_token)
|
| 42 |
+
|
| 43 |
+
agents = {
|
| 44 |
+
'intent_recognition': create_intent_agent(llm_router),
|
| 45 |
+
'response_synthesis': create_synthesis_agent(llm_router),
|
| 46 |
+
'safety_check': create_safety_agent(llm_router),
|
| 47 |
+
'skills_identification': create_skills_identification_agent(llm_router)
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
context_manager = EfficientContextManager(llm_router=llm_router)
|
| 51 |
+
|
| 52 |
+
orchestrator = MVPOrchestrator(llm_router, context_manager, agents)
|
| 53 |
+
|
| 54 |
+
session_id = "test_session_context"
|
| 55 |
+
user_id = "test_user"
|
| 56 |
+
|
| 57 |
+
print(f"β Components initialized")
|
| 58 |
+
print(f" Session ID: {session_id}")
|
| 59 |
+
print(f" User ID: {user_id}")
|
| 60 |
+
|
| 61 |
+
# Turn 1
|
| 62 |
+
print("\n[2/4] TURN 1 - Initial Query")
|
| 63 |
+
print("-" * 60)
|
| 64 |
+
turn1_input = "I have an AI Assistant for database queries"
|
| 65 |
+
print(f"User Input: {turn1_input}")
|
| 66 |
+
|
| 67 |
+
start = time.time()
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
result1 = asyncio.run(orchestrator.process_request(
|
| 71 |
+
session_id=session_id,
|
| 72 |
+
user_input=turn1_input
|
| 73 |
+
))
|
| 74 |
+
turn1_time = time.time() - start
|
| 75 |
+
|
| 76 |
+
response1 = result1.get('response', result1.get('final_response', ''))
|
| 77 |
+
print(f"β Turn 1 completed in {turn1_time:.2f}s")
|
| 78 |
+
print(f" Response length: {len(response1)} characters")
|
| 79 |
+
|
| 80 |
+
# Check context
|
| 81 |
+
context1 = orchestrator.context_manager.get_or_create_session_context(session_id, user_id)
|
| 82 |
+
context1_size = len(str(context1))
|
| 83 |
+
print(f" Context size after Turn 1: {context1_size} characters")
|
| 84 |
+
print(f" Interaction contexts: {len(context1.get('interaction_contexts', []))}")
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"β Turn 1 failed: {e}")
|
| 88 |
+
import traceback
|
| 89 |
+
traceback.print_exc()
|
| 90 |
+
return False
|
| 91 |
+
|
| 92 |
+
# Turn 2
|
| 93 |
+
print("\n[3/4] TURN 2 - Follow-up Query")
|
| 94 |
+
print("-" * 60)
|
| 95 |
+
turn2_input = "Build a 15-day MVP and 2-month execution plan"
|
| 96 |
+
print(f"User Input: {turn2_input}")
|
| 97 |
+
|
| 98 |
+
start = time.time()
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
result2 = asyncio.run(orchestrator.process_request(
|
| 102 |
+
session_id=session_id,
|
| 103 |
+
user_input=turn2_input
|
| 104 |
+
))
|
| 105 |
+
turn2_time = time.time() - start
|
| 106 |
+
|
| 107 |
+
response2 = result2.get('response', result2.get('final_response', ''))
|
| 108 |
+
print(f"β Turn 2 completed in {turn2_time:.2f}s")
|
| 109 |
+
print(f" Response length: {len(response2)} characters")
|
| 110 |
+
|
| 111 |
+
# Check context growth
|
| 112 |
+
context2 = orchestrator.context_manager.get_or_create_session_context(session_id, user_id)
|
| 113 |
+
context2_size = len(str(context2))
|
| 114 |
+
context_growth = context2_size - context1_size
|
| 115 |
+
print(f" Context size after Turn 2: {context2_size} characters")
|
| 116 |
+
print(f" Context growth: {context_growth} characters")
|
| 117 |
+
print(f" Interaction contexts: {len(context2.get('interaction_contexts', []))}")
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"β Turn 2 failed: {e}")
|
| 121 |
+
import traceback
|
| 122 |
+
traceback.print_exc()
|
| 123 |
+
return False
|
| 124 |
+
|
| 125 |
+
# Verify context continuity
|
| 126 |
+
print("\n[4/4] Context Continuity Verification")
|
| 127 |
+
print("-" * 60)
|
| 128 |
+
|
| 129 |
+
# Check that session_id is preserved
|
| 130 |
+
assert session_id in str(context2), "Session ID not found in context"
|
| 131 |
+
print("β Session ID preserved in context")
|
| 132 |
+
|
| 133 |
+
# Check that interaction contexts accumulated
|
| 134 |
+
interaction_count = len(context2.get('interaction_contexts', []))
|
| 135 |
+
assert interaction_count >= 1, f"Expected at least 1 interaction context, got {interaction_count}"
|
| 136 |
+
print(f"β Interaction contexts accumulated: {interaction_count}")
|
| 137 |
+
|
| 138 |
+
# Check context structure
|
| 139 |
+
assert 'session_id' in context2, "Session ID missing from context"
|
| 140 |
+
assert 'user_id' in context2, "User ID missing from context"
|
| 141 |
+
print("β Context structure valid")
|
| 142 |
+
|
| 143 |
+
print("\n" + "=" * 60)
|
| 144 |
+
print("β ALL TESTS PASSED")
|
| 145 |
+
print("=" * 60)
|
| 146 |
+
print(f"\nSummary:")
|
| 147 |
+
print(f" Turn 1 time: {turn1_time:.2f}s")
|
| 148 |
+
print(f" Turn 2 time: {turn2_time:.2f}s")
|
| 149 |
+
print(f" Context growth: {context_growth} characters")
|
| 150 |
+
print(f" Total interactions: {interaction_count}")
|
| 151 |
+
|
| 152 |
+
return True
|
| 153 |
+
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print(f"\nβ TEST FAILED: {e}")
|
| 156 |
+
import traceback
|
| 157 |
+
traceback.print_exc()
|
| 158 |
+
return False
|
| 159 |
+
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
success = test_two_turn_conversation()
|
| 162 |
+
sys.exit(0 if success else 1)
|
| 163 |
+
|