Commit
·
ae20ff2
1
Parent(s):
7c65f3f
workflow errors debugging V3
Browse files- LOGGING_GUIDE.md +234 -0
- app.py +160 -19
- context_manager.py +9 -1
- llm_router.py +22 -4
- orchestrator_engine.py +59 -31
LOGGING_GUIDE.md
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 📋 Logging Guide
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
|
| 5 |
+
The application now includes comprehensive logging throughout all components for debugging and monitoring in container environments.
|
| 6 |
+
|
| 7 |
+
## Log Levels
|
| 8 |
+
|
| 9 |
+
- **INFO**: General application flow and important events
|
| 10 |
+
- **WARNING**: Non-critical issues (e.g., missing tokens, fallback modes)
|
| 11 |
+
- **ERROR**: Errors that are caught and handled
|
| 12 |
+
- **DEBUG**: Detailed debugging information
|
| 13 |
+
|
| 14 |
+
## Log Output Locations
|
| 15 |
+
|
| 16 |
+
1. **Console/Stdout** - Real-time logs visible in container output
|
| 17 |
+
2. **File** - `app.log` in the working directory
|
| 18 |
+
|
| 19 |
+
## What Gets Logged
|
| 20 |
+
|
| 21 |
+
### App Initialization (`app.py`)
|
| 22 |
+
|
| 23 |
+
```python
|
| 24 |
+
# Component import attempts
|
| 25 |
+
logger.info("Attempting to import orchestration components...")
|
| 26 |
+
logger.info("✓ Successfully imported orchestration components")
|
| 27 |
+
|
| 28 |
+
# System initialization
|
| 29 |
+
logger.info("INITIALIZING ORCHESTRATION SYSTEM")
|
| 30 |
+
logger.info("Step 1/6: Initializing LLM Router...")
|
| 31 |
+
logger.info("✓ LLM Router initialized")
|
| 32 |
+
logger.info("Step 2/6: Initializing Agents...")
|
| 33 |
+
logger.info("Step 3/6: Initializing Context Manager...")
|
| 34 |
+
logger.info("Step 4/6: Initializing Orchestrator...")
|
| 35 |
+
logger.info("ORCHESTRATION SYSTEM READY")
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
### Request Processing (`app.py`)
|
| 39 |
+
|
| 40 |
+
```python
|
| 41 |
+
logger.info(f"Processing message: {message[:100]}")
|
| 42 |
+
logger.info(f"Session ID: {session_id}")
|
| 43 |
+
logger.info("Attempting full orchestration...")
|
| 44 |
+
logger.info("Orchestrator returned response: {response[:100]}")
|
| 45 |
+
logger.info("Message processing complete")
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
### Orchestrator (`orchestrator_engine.py`)
|
| 49 |
+
|
| 50 |
+
```python
|
| 51 |
+
logger.info(f"Processing request for session {session_id}")
|
| 52 |
+
logger.info(f"User input: {user_input[:100]}")
|
| 53 |
+
logger.info(f"Generated interaction ID: {interaction_id}")
|
| 54 |
+
logger.info("Step 2: Managing context...")
|
| 55 |
+
logger.info(f"Context retrieved: {len(context.get('interactions', []))} interactions")
|
| 56 |
+
logger.info("Step 3: Recognizing intent...")
|
| 57 |
+
logger.info(f"Intent detected: {intent_result.get('primary_intent', 'unknown')}")
|
| 58 |
+
logger.info("Step 4: Creating execution plan...")
|
| 59 |
+
logger.info("Step 5: Executing agents...")
|
| 60 |
+
logger.info(f"Agent execution complete: {len(agent_results)} results")
|
| 61 |
+
logger.info("Step 6: Synthesizing response...")
|
| 62 |
+
logger.info("Step 7: Safety check...")
|
| 63 |
+
logger.info(f"Request processing complete. Response length: ...")
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
### Context Manager (`context_manager.py`)
|
| 67 |
+
|
| 68 |
+
```python
|
| 69 |
+
logger.info(f"Initializing ContextManager with DB path: {db_path}")
|
| 70 |
+
logger.info("Initializing database...")
|
| 71 |
+
logger.info("✓ Sessions table ready")
|
| 72 |
+
logger.info("✓ Interactions table ready")
|
| 73 |
+
logger.info("Database initialization complete")
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
### LLM Router (`llm_router.py`)
|
| 77 |
+
|
| 78 |
+
```python
|
| 79 |
+
logger.info("LLMRouter initialized")
|
| 80 |
+
logger.info("HF token available")
|
| 81 |
+
logger.info(f"Routing inference for task: {task_type}")
|
| 82 |
+
logger.info(f"Selected model: {model_config['model_id']}")
|
| 83 |
+
logger.info(f"Calling HF API for model: {model_id}")
|
| 84 |
+
logger.info(f"HF API returned response (length: ...)")
|
| 85 |
+
logger.info(f"Inference complete for {task_type}")
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
## Viewing Logs
|
| 89 |
+
|
| 90 |
+
### In Development
|
| 91 |
+
|
| 92 |
+
```bash
|
| 93 |
+
# View real-time logs
|
| 94 |
+
python app.py
|
| 95 |
+
|
| 96 |
+
# View log file
|
| 97 |
+
tail -f app.log
|
| 98 |
+
|
| 99 |
+
# Search logs
|
| 100 |
+
grep "ERROR" app.log
|
| 101 |
+
grep "Processing request" app.log
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
### In Container/Docker
|
| 105 |
+
|
| 106 |
+
```bash
|
| 107 |
+
# View container logs
|
| 108 |
+
docker logs <container_id>
|
| 109 |
+
|
| 110 |
+
# Follow logs in real-time
|
| 111 |
+
docker logs -f <container_id>
|
| 112 |
+
|
| 113 |
+
# View recent logs
|
| 114 |
+
docker logs --tail 100 <container_id>
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
### In Kubernetes
|
| 118 |
+
|
| 119 |
+
```bash
|
| 120 |
+
# View pod logs
|
| 121 |
+
kubectl logs <pod-name>
|
| 122 |
+
|
| 123 |
+
# Follow logs
|
| 124 |
+
kubectl logs -f <pod-name>
|
| 125 |
+
|
| 126 |
+
# View logs from specific container
|
| 127 |
+
kubectl logs <pod-name> -c <container-name>
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
## Debugging with Logs
|
| 131 |
+
|
| 132 |
+
### Example Log Flow for a Request
|
| 133 |
+
|
| 134 |
+
```
|
| 135 |
+
2024-01-15 10:30:00 - app - INFO - Processing message: Hello, I need help with...
|
| 136 |
+
2024-01-15 10:30:00 - app - INFO - Session ID: abc12345
|
| 137 |
+
2024-01-15 10:30:00 - app - INFO - Attempting full orchestration...
|
| 138 |
+
2024-01-15 10:30:00 - orchestrator_engine - INFO - Processing request for session abc12345
|
| 139 |
+
2024-01-15 10:30:00 - orchestrator_engine - INFO - User input: Hello, I need help with...
|
| 140 |
+
2024-01-15 10:30:00 - orchestrator_engine - INFO - Generated interaction ID: ...
|
| 141 |
+
2024-01-15 10:30:01 - orchestrator_engine - INFO - Step 2: Managing context...
|
| 142 |
+
2024-01-15 10:30:01 - context_manager - INFO - Retrieving context for session abc12345
|
| 143 |
+
2024-01-15 10:30:01 - orchestrator_engine - INFO - Context retrieved: 5 interactions
|
| 144 |
+
2024-01-15 10:30:01 - orchestrator_engine - INFO - Step 3: Recognizing intent...
|
| 145 |
+
2024-01-15 10:30:02 - intent_agent - INFO - INTENT_REC_001 processing user input: Hello...
|
| 146 |
+
2024-01-15 10:30:02 - orchestrator_engine - INFO - Intent detected: information_request
|
| 147 |
+
...
|
| 148 |
+
```
|
| 149 |
+
|
| 150 |
+
## Common Error Patterns
|
| 151 |
+
|
| 152 |
+
### Missing Dependencies
|
| 153 |
+
```
|
| 154 |
+
ERROR - Could not import orchestration components: No module named 'src.agents'
|
| 155 |
+
```
|
| 156 |
+
**Solution**: Check imports and PYTHONPATH
|
| 157 |
+
|
| 158 |
+
### Database Issues
|
| 159 |
+
```
|
| 160 |
+
ERROR - Database initialization error: unable to open database file
|
| 161 |
+
```
|
| 162 |
+
**Solution**: Check file permissions and disk space
|
| 163 |
+
|
| 164 |
+
### API Errors
|
| 165 |
+
```
|
| 166 |
+
ERROR - HF API error: 429 - Rate limit exceeded
|
| 167 |
+
```
|
| 168 |
+
**Solution**: Check rate limits and API token
|
| 169 |
+
|
| 170 |
+
### Orchestrator Errors
|
| 171 |
+
```
|
| 172 |
+
ERROR - Orchestrator error: 'NoneType' object has no attribute 'execute'
|
| 173 |
+
```
|
| 174 |
+
**Solution**: Check agent initialization
|
| 175 |
+
|
| 176 |
+
## Log Management
|
| 177 |
+
|
| 178 |
+
### Log Rotation
|
| 179 |
+
|
| 180 |
+
For production, consider adding log rotation:
|
| 181 |
+
|
| 182 |
+
```python
|
| 183 |
+
from logging.handlers import RotatingFileHandler
|
| 184 |
+
|
| 185 |
+
handler = RotatingFileHandler(
|
| 186 |
+
'app.log',
|
| 187 |
+
maxBytes=10*1024*1024, # 10MB
|
| 188 |
+
backupCount=5
|
| 189 |
+
)
|
| 190 |
+
```
|
| 191 |
+
|
| 192 |
+
### Structured Logging
|
| 193 |
+
|
| 194 |
+
For JSON logging (useful in containers):
|
| 195 |
+
|
| 196 |
+
```python
|
| 197 |
+
import structlog
|
| 198 |
+
|
| 199 |
+
logger = structlog.get_logger()
|
| 200 |
+
logger.info("event", session_id=session_id, status="success")
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
## Performance Considerations
|
| 204 |
+
|
| 205 |
+
- Logging to file has minimal overhead
|
| 206 |
+
- Console logging is fast in containers
|
| 207 |
+
- DEBUG level can be verbose - use only when needed
|
| 208 |
+
- Consider async logging for high-throughput scenarios
|
| 209 |
+
|
| 210 |
+
## Best Practices
|
| 211 |
+
|
| 212 |
+
1. ✅ Log entry and exit of critical functions
|
| 213 |
+
2. ✅ Include context (session_id, request_id, etc.)
|
| 214 |
+
3. ✅ Log errors with full stack traces (`exc_info=True`)
|
| 215 |
+
4. ✅ Use appropriate log levels
|
| 216 |
+
5. ❌ Don't log sensitive data (tokens, passwords)
|
| 217 |
+
6. ❌ Don't over-log in hot paths
|
| 218 |
+
|
| 219 |
+
## Quick Commands
|
| 220 |
+
|
| 221 |
+
```bash
|
| 222 |
+
# Find all errors
|
| 223 |
+
grep "ERROR" app.log
|
| 224 |
+
|
| 225 |
+
# Count messages processed
|
| 226 |
+
grep "Message processing complete" app.log | wc -l
|
| 227 |
+
|
| 228 |
+
# Find slow requests
|
| 229 |
+
grep -E "processing|complete" app.log | grep -E "[0-9]{4,}"
|
| 230 |
+
|
| 231 |
+
# Watch real-time
|
| 232 |
+
tail -f app.log | grep -E "INFO|ERROR|WARNING"
|
| 233 |
+
```
|
| 234 |
+
|
app.py
CHANGED
|
@@ -1,14 +1,55 @@
|
|
| 1 |
# app.py - Mobile-First Implementation
|
| 2 |
import gradio as gr
|
| 3 |
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
try:
|
| 6 |
from spaces import GPU
|
| 7 |
SPACES_GPU_AVAILABLE = True
|
|
|
|
| 8 |
except ImportError:
|
| 9 |
# Not running on HF Spaces or spaces module not available
|
| 10 |
SPACES_GPU_AVAILABLE = False
|
| 11 |
GPU = None
|
|
|
|
| 12 |
|
| 13 |
def create_mobile_optimized_interface():
|
| 14 |
"""Create the mobile-optimized Gradio interface and return demo with components"""
|
|
@@ -264,50 +305,86 @@ def setup_event_handlers(demo, event_handlers):
|
|
| 264 |
|
| 265 |
return demo
|
| 266 |
|
| 267 |
-
def
|
| 268 |
"""
|
| 269 |
-
Process message with
|
| 270 |
-
|
| 271 |
-
NOTE: This is currently using a placeholder. For full orchestration:
|
| 272 |
-
- Make this function async if you need to call async orchestrator methods
|
| 273 |
-
- Or use threading/background tasks for long-running operations
|
| 274 |
-
- Or use Gradio's streaming capabilities for progressive responses
|
| 275 |
"""
|
|
|
|
|
|
|
| 276 |
try:
|
| 277 |
-
|
|
|
|
|
|
|
| 278 |
if not message or not message.strip():
|
|
|
|
| 279 |
return history if history else [], ""
|
| 280 |
|
| 281 |
-
# Initialize history if None
|
| 282 |
if history is None:
|
| 283 |
history = []
|
| 284 |
|
| 285 |
-
# Create a copy to avoid mutating the input
|
| 286 |
new_history = list(history) if isinstance(history, list) else []
|
| 287 |
|
| 288 |
# Add user message
|
| 289 |
new_history.append({"role": "user", "content": message.strip()})
|
| 290 |
|
| 291 |
-
#
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
-
# Add assistant response
|
| 295 |
new_history.append({"role": "assistant", "content": response})
|
|
|
|
| 296 |
|
| 297 |
-
# Return updated history and clear input
|
| 298 |
return new_history, ""
|
| 299 |
|
| 300 |
except Exception as e:
|
| 301 |
-
|
| 302 |
-
import traceback
|
| 303 |
-
print(f"ERROR in process_message: {e}")
|
| 304 |
-
traceback.print_exc()
|
| 305 |
-
|
| 306 |
error_history = list(history) if history else []
|
| 307 |
error_history.append({"role": "user", "content": message})
|
| 308 |
error_history.append({"role": "assistant", "content": f"I encountered an error: {str(e)}"})
|
| 309 |
return error_history, ""
|
| 310 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
# Decorate the chat handler with GPU if available
|
| 312 |
if SPACES_GPU_AVAILABLE and GPU is not None:
|
| 313 |
@GPU # This decorator is detected by HF Spaces for ZeroGPU allocation
|
|
@@ -318,10 +395,74 @@ if SPACES_GPU_AVAILABLE and GPU is not None:
|
|
| 318 |
else:
|
| 319 |
chat_handler_fn = process_message
|
| 320 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
demo, components = create_mobile_optimized_interface()
|
| 323 |
|
|
|
|
|
|
|
|
|
|
| 324 |
# Launch the app
|
|
|
|
|
|
|
|
|
|
| 325 |
demo.launch(
|
| 326 |
server_name="0.0.0.0",
|
| 327 |
server_port=7860,
|
|
|
|
| 1 |
# app.py - Mobile-First Implementation
|
| 2 |
import gradio as gr
|
| 3 |
import uuid
|
| 4 |
+
import logging
|
| 5 |
+
import traceback
|
| 6 |
+
from typing import Optional, Tuple, List, Dict, Any
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Configure comprehensive logging
|
| 10 |
+
logging.basicConfig(
|
| 11 |
+
level=logging.INFO,
|
| 12 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 13 |
+
handlers=[
|
| 14 |
+
logging.StreamHandler(),
|
| 15 |
+
logging.FileHandler('app.log')
|
| 16 |
+
]
|
| 17 |
+
)
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
|
| 20 |
+
# Try to import orchestration components
|
| 21 |
+
orchestrator = None
|
| 22 |
+
orchestrator_available = False
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
logger.info("Attempting to import orchestration components...")
|
| 26 |
+
import sys
|
| 27 |
+
sys.path.insert(0, '.')
|
| 28 |
+
sys.path.insert(0, 'src')
|
| 29 |
+
|
| 30 |
+
from src.agents.intent_agent import create_intent_agent
|
| 31 |
+
from src.agents.synthesis_agent import create_synthesis_agent
|
| 32 |
+
from src.agents.safety_agent import create_safety_agent
|
| 33 |
+
from llm_router import LLMRouter
|
| 34 |
+
from orchestrator_engine import MVPOrchestrator
|
| 35 |
+
from context_manager import EfficientContextManager
|
| 36 |
+
from config import settings
|
| 37 |
+
|
| 38 |
+
logger.info("✓ Successfully imported orchestration components")
|
| 39 |
+
orchestrator_available = True
|
| 40 |
+
except ImportError as e:
|
| 41 |
+
logger.warning(f"Could not import orchestration components: {e}")
|
| 42 |
+
logger.info("Will use placeholder mode")
|
| 43 |
|
| 44 |
try:
|
| 45 |
from spaces import GPU
|
| 46 |
SPACES_GPU_AVAILABLE = True
|
| 47 |
+
logger.info("HF Spaces GPU available")
|
| 48 |
except ImportError:
|
| 49 |
# Not running on HF Spaces or spaces module not available
|
| 50 |
SPACES_GPU_AVAILABLE = False
|
| 51 |
GPU = None
|
| 52 |
+
logger.info("Running without HF Spaces GPU")
|
| 53 |
|
| 54 |
def create_mobile_optimized_interface():
|
| 55 |
"""Create the mobile-optimized Gradio interface and return demo with components"""
|
|
|
|
| 305 |
|
| 306 |
return demo
|
| 307 |
|
| 308 |
+
async def process_message_async(message: str, history: Optional[List], session_id: str) -> Tuple[List, str]:
|
| 309 |
"""
|
| 310 |
+
Process message with full orchestration system
|
| 311 |
+
Returns (updated_history, empty_string)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
"""
|
| 313 |
+
global orchestrator
|
| 314 |
+
|
| 315 |
try:
|
| 316 |
+
logger.info(f"Processing message: {message[:100]}")
|
| 317 |
+
logger.info(f"Session ID: {session_id}")
|
| 318 |
+
|
| 319 |
if not message or not message.strip():
|
| 320 |
+
logger.debug("Empty message received")
|
| 321 |
return history if history else [], ""
|
| 322 |
|
|
|
|
| 323 |
if history is None:
|
| 324 |
history = []
|
| 325 |
|
|
|
|
| 326 |
new_history = list(history) if isinstance(history, list) else []
|
| 327 |
|
| 328 |
# Add user message
|
| 329 |
new_history.append({"role": "user", "content": message.strip()})
|
| 330 |
|
| 331 |
+
# Try to use orchestrator if available
|
| 332 |
+
if orchestrator is not None:
|
| 333 |
+
try:
|
| 334 |
+
logger.info("Attempting full orchestration...")
|
| 335 |
+
# Use orchestrator to process
|
| 336 |
+
result = await orchestrator.process_request(
|
| 337 |
+
session_id=session_id,
|
| 338 |
+
user_input=message.strip()
|
| 339 |
+
)
|
| 340 |
+
|
| 341 |
+
# Extract response from result
|
| 342 |
+
response = result.get('response', result.get('final_response', str(result)))
|
| 343 |
+
logger.info(f"Orchestrator returned response: {response[:100]}")
|
| 344 |
+
|
| 345 |
+
except Exception as orch_error:
|
| 346 |
+
logger.error(f"Orchestrator error: {orch_error}", exc_info=True)
|
| 347 |
+
response = f"[Orchestrator Error] {str(orch_error)}"
|
| 348 |
+
else:
|
| 349 |
+
# Fallback placeholder
|
| 350 |
+
logger.info("Using placeholder response")
|
| 351 |
+
response = f"I received your message: {message}\n\nThis is a placeholder response. The orchestrator system is {'' if orchestrator_available else 'not'} available."
|
| 352 |
|
| 353 |
+
# Add assistant response
|
| 354 |
new_history.append({"role": "assistant", "content": response})
|
| 355 |
+
logger.info("Message processing complete")
|
| 356 |
|
|
|
|
| 357 |
return new_history, ""
|
| 358 |
|
| 359 |
except Exception as e:
|
| 360 |
+
logger.error(f"Error in process_message_async: {e}", exc_info=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
error_history = list(history) if history else []
|
| 362 |
error_history.append({"role": "user", "content": message})
|
| 363 |
error_history.append({"role": "assistant", "content": f"I encountered an error: {str(e)}"})
|
| 364 |
return error_history, ""
|
| 365 |
|
| 366 |
+
def process_message(message: str, history: Optional[List]) -> Tuple[List, str]:
|
| 367 |
+
"""
|
| 368 |
+
Synchronous wrapper for async processing
|
| 369 |
+
"""
|
| 370 |
+
import asyncio
|
| 371 |
+
|
| 372 |
+
# Generate a session ID
|
| 373 |
+
session_id = str(uuid.uuid4())[:8]
|
| 374 |
+
|
| 375 |
+
try:
|
| 376 |
+
# Run async processing
|
| 377 |
+
loop = asyncio.new_event_loop()
|
| 378 |
+
asyncio.set_event_loop(loop)
|
| 379 |
+
result = loop.run_until_complete(process_message_async(message, history, session_id))
|
| 380 |
+
return result
|
| 381 |
+
except Exception as e:
|
| 382 |
+
logger.error(f"Error in process_message: {e}", exc_info=True)
|
| 383 |
+
error_history = list(history) if history else []
|
| 384 |
+
error_history.append({"role": "user", "content": message})
|
| 385 |
+
error_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
|
| 386 |
+
return error_history, ""
|
| 387 |
+
|
| 388 |
# Decorate the chat handler with GPU if available
|
| 389 |
if SPACES_GPU_AVAILABLE and GPU is not None:
|
| 390 |
@GPU # This decorator is detected by HF Spaces for ZeroGPU allocation
|
|
|
|
| 395 |
else:
|
| 396 |
chat_handler_fn = process_message
|
| 397 |
|
| 398 |
+
# Initialize orchestrator on module load
|
| 399 |
+
def initialize_orchestrator():
|
| 400 |
+
"""Initialize the orchestration system with logging"""
|
| 401 |
+
global orchestrator
|
| 402 |
+
|
| 403 |
+
if not orchestrator_available:
|
| 404 |
+
logger.info("Orchestrator components not available, skipping initialization")
|
| 405 |
+
return
|
| 406 |
+
|
| 407 |
+
try:
|
| 408 |
+
logger.info("=" * 60)
|
| 409 |
+
logger.info("INITIALIZING ORCHESTRATION SYSTEM")
|
| 410 |
+
logger.info("=" * 60)
|
| 411 |
+
|
| 412 |
+
# Get HF token
|
| 413 |
+
hf_token = os.getenv('HF_TOKEN', '')
|
| 414 |
+
if not hf_token:
|
| 415 |
+
logger.warning("HF_TOKEN not found in environment")
|
| 416 |
+
|
| 417 |
+
# Initialize LLM Router
|
| 418 |
+
logger.info("Step 1/6: Initializing LLM Router...")
|
| 419 |
+
llm_router = LLMRouter(hf_token)
|
| 420 |
+
logger.info("✓ LLM Router initialized")
|
| 421 |
+
|
| 422 |
+
# Initialize Agents
|
| 423 |
+
logger.info("Step 2/6: Initializing Agents...")
|
| 424 |
+
agents = {
|
| 425 |
+
'intent_recognition': create_intent_agent(llm_router),
|
| 426 |
+
'response_synthesis': create_synthesis_agent(llm_router),
|
| 427 |
+
'safety_check': create_safety_agent(llm_router)
|
| 428 |
+
}
|
| 429 |
+
logger.info(f"✓ Initialized {len(agents)} agents")
|
| 430 |
+
|
| 431 |
+
# Initialize Context Manager
|
| 432 |
+
logger.info("Step 3/6: Initializing Context Manager...")
|
| 433 |
+
context_manager = EfficientContextManager()
|
| 434 |
+
logger.info("✓ Context Manager initialized")
|
| 435 |
+
|
| 436 |
+
# Initialize Orchestrator
|
| 437 |
+
logger.info("Step 4/6: Initializing Orchestrator...")
|
| 438 |
+
orchestrator = MVPOrchestrator(llm_router, context_manager, agents)
|
| 439 |
+
logger.info("✓ Orchestrator initialized")
|
| 440 |
+
|
| 441 |
+
logger.info("=" * 60)
|
| 442 |
+
logger.info("ORCHESTRATION SYSTEM READY")
|
| 443 |
+
logger.info("=" * 60)
|
| 444 |
+
|
| 445 |
+
except Exception as e:
|
| 446 |
+
logger.error(f"Failed to initialize orchestrator: {e}", exc_info=True)
|
| 447 |
+
orchestrator = None
|
| 448 |
+
|
| 449 |
+
# Try to initialize orchestrator
|
| 450 |
+
initialize_orchestrator()
|
| 451 |
+
|
| 452 |
if __name__ == "__main__":
|
| 453 |
+
logger.info("=" * 60)
|
| 454 |
+
logger.info("STARTING APP")
|
| 455 |
+
logger.info("=" * 60)
|
| 456 |
+
|
| 457 |
demo, components = create_mobile_optimized_interface()
|
| 458 |
|
| 459 |
+
logger.info("✓ Interface created")
|
| 460 |
+
logger.info(f"Orchestrator available: {orchestrator is not None}")
|
| 461 |
+
|
| 462 |
# Launch the app
|
| 463 |
+
logger.info("=" * 60)
|
| 464 |
+
logger.info("LAUNCHING GRADIO APP")
|
| 465 |
+
logger.info("=" * 60)
|
| 466 |
demo.launch(
|
| 467 |
server_name="0.0.0.0",
|
| 468 |
server_port=7860,
|
context_manager.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
# context_manager.py
|
| 2 |
import sqlite3
|
| 3 |
import json
|
|
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
|
|
|
|
|
|
|
| 6 |
class EfficientContextManager:
|
| 7 |
def __init__(self):
|
| 8 |
self.session_cache = {} # In-memory for active sessions
|
|
@@ -13,11 +16,13 @@ class EfficientContextManager:
|
|
| 13 |
"eviction_policy": "LRU"
|
| 14 |
}
|
| 15 |
self.db_path = "sessions.db"
|
|
|
|
| 16 |
self._init_database()
|
| 17 |
|
| 18 |
def _init_database(self):
|
| 19 |
"""Initialize database and create tables"""
|
| 20 |
try:
|
|
|
|
| 21 |
conn = sqlite3.connect(self.db_path)
|
| 22 |
cursor = conn.cursor()
|
| 23 |
|
|
@@ -31,6 +36,7 @@ class EfficientContextManager:
|
|
| 31 |
user_metadata TEXT
|
| 32 |
)
|
| 33 |
""")
|
|
|
|
| 34 |
|
| 35 |
# Create interactions table
|
| 36 |
cursor.execute("""
|
|
@@ -43,12 +49,14 @@ class EfficientContextManager:
|
|
| 43 |
FOREIGN KEY(session_id) REFERENCES sessions(session_id)
|
| 44 |
)
|
| 45 |
""")
|
|
|
|
| 46 |
|
| 47 |
conn.commit()
|
| 48 |
conn.close()
|
|
|
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
|
| 52 |
|
| 53 |
async def manage_context(self, session_id: str, user_input: str) -> dict:
|
| 54 |
"""
|
|
|
|
| 1 |
# context_manager.py
|
| 2 |
import sqlite3
|
| 3 |
import json
|
| 4 |
+
import logging
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
class EfficientContextManager:
|
| 10 |
def __init__(self):
|
| 11 |
self.session_cache = {} # In-memory for active sessions
|
|
|
|
| 16 |
"eviction_policy": "LRU"
|
| 17 |
}
|
| 18 |
self.db_path = "sessions.db"
|
| 19 |
+
logger.info(f"Initializing ContextManager with DB path: {self.db_path}")
|
| 20 |
self._init_database()
|
| 21 |
|
| 22 |
def _init_database(self):
|
| 23 |
"""Initialize database and create tables"""
|
| 24 |
try:
|
| 25 |
+
logger.info("Initializing database...")
|
| 26 |
conn = sqlite3.connect(self.db_path)
|
| 27 |
cursor = conn.cursor()
|
| 28 |
|
|
|
|
| 36 |
user_metadata TEXT
|
| 37 |
)
|
| 38 |
""")
|
| 39 |
+
logger.info("✓ Sessions table ready")
|
| 40 |
|
| 41 |
# Create interactions table
|
| 42 |
cursor.execute("""
|
|
|
|
| 49 |
FOREIGN KEY(session_id) REFERENCES sessions(session_id)
|
| 50 |
)
|
| 51 |
""")
|
| 52 |
+
logger.info("✓ Interactions table ready")
|
| 53 |
|
| 54 |
conn.commit()
|
| 55 |
conn.close()
|
| 56 |
+
logger.info("Database initialization complete")
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
+
logger.error(f"Database initialization error: {e}", exc_info=True)
|
| 60 |
|
| 61 |
async def manage_context(self, session_id: str, user_input: str) -> dict:
|
| 62 |
"""
|
llm_router.py
CHANGED
|
@@ -1,22 +1,36 @@
|
|
| 1 |
# llm_router.py
|
|
|
|
| 2 |
from models_config import LLM_CONFIG
|
| 3 |
|
|
|
|
|
|
|
| 4 |
class LLMRouter:
|
| 5 |
def __init__(self, hf_token):
|
| 6 |
self.hf_token = hf_token
|
| 7 |
self.health_status = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
async def route_inference(self, task_type: str, prompt: str, **kwargs):
|
| 10 |
"""
|
| 11 |
Smart routing based on task specialization
|
| 12 |
"""
|
|
|
|
| 13 |
model_config = self._select_model(task_type)
|
|
|
|
| 14 |
|
| 15 |
# Health check and fallback logic
|
| 16 |
if not await self._is_model_healthy(model_config["model_id"]):
|
|
|
|
| 17 |
model_config = self._get_fallback_model(task_type)
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def _select_model(self, task_type: str) -> dict:
|
| 22 |
model_map = {
|
|
@@ -64,6 +78,9 @@ class LLMRouter:
|
|
| 64 |
model_id = model_config["model_id"]
|
| 65 |
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
headers = {
|
| 68 |
"Authorization": f"Bearer {self.hf_token}",
|
| 69 |
"Content-Type": "application/json"
|
|
@@ -90,15 +107,16 @@ class LLMRouter:
|
|
| 90 |
generated_text = result[0].get("generated_text", "")
|
| 91 |
else:
|
| 92 |
generated_text = str(result)
|
|
|
|
| 93 |
return generated_text
|
| 94 |
else:
|
| 95 |
-
|
| 96 |
return None
|
| 97 |
|
| 98 |
except ImportError:
|
| 99 |
-
|
| 100 |
return f"[Mock] Response to: {prompt[:100]}..."
|
| 101 |
except Exception as e:
|
| 102 |
-
|
| 103 |
return None
|
| 104 |
|
|
|
|
| 1 |
# llm_router.py
|
| 2 |
+
import logging
|
| 3 |
from models_config import LLM_CONFIG
|
| 4 |
|
| 5 |
+
logger = logging.getLogger(__name__)
|
| 6 |
+
|
| 7 |
class LLMRouter:
|
| 8 |
def __init__(self, hf_token):
|
| 9 |
self.hf_token = hf_token
|
| 10 |
self.health_status = {}
|
| 11 |
+
logger.info("LLMRouter initialized")
|
| 12 |
+
if hf_token:
|
| 13 |
+
logger.info("HF token available")
|
| 14 |
+
else:
|
| 15 |
+
logger.warning("No HF token provided")
|
| 16 |
|
| 17 |
async def route_inference(self, task_type: str, prompt: str, **kwargs):
|
| 18 |
"""
|
| 19 |
Smart routing based on task specialization
|
| 20 |
"""
|
| 21 |
+
logger.info(f"Routing inference for task: {task_type}")
|
| 22 |
model_config = self._select_model(task_type)
|
| 23 |
+
logger.info(f"Selected model: {model_config['model_id']}")
|
| 24 |
|
| 25 |
# Health check and fallback logic
|
| 26 |
if not await self._is_model_healthy(model_config["model_id"]):
|
| 27 |
+
logger.warning(f"Model unhealthy, using fallback")
|
| 28 |
model_config = self._get_fallback_model(task_type)
|
| 29 |
+
logger.info(f"Fallback model: {model_config['model_id']}")
|
| 30 |
|
| 31 |
+
result = await self._call_hf_endpoint(model_config, prompt, **kwargs)
|
| 32 |
+
logger.info(f"Inference complete for {task_type}")
|
| 33 |
+
return result
|
| 34 |
|
| 35 |
def _select_model(self, task_type: str) -> dict:
|
| 36 |
model_map = {
|
|
|
|
| 78 |
model_id = model_config["model_id"]
|
| 79 |
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
|
| 80 |
|
| 81 |
+
logger.info(f"Calling HF API for model: {model_id}")
|
| 82 |
+
logger.debug(f"Prompt length: {len(prompt)}")
|
| 83 |
+
|
| 84 |
headers = {
|
| 85 |
"Authorization": f"Bearer {self.hf_token}",
|
| 86 |
"Content-Type": "application/json"
|
|
|
|
| 107 |
generated_text = result[0].get("generated_text", "")
|
| 108 |
else:
|
| 109 |
generated_text = str(result)
|
| 110 |
+
logger.info(f"HF API returned response (length: {len(generated_text)})")
|
| 111 |
return generated_text
|
| 112 |
else:
|
| 113 |
+
logger.error(f"HF API error: {response.status_code} - {response.text}")
|
| 114 |
return None
|
| 115 |
|
| 116 |
except ImportError:
|
| 117 |
+
logger.warning("requests library not available, using mock response")
|
| 118 |
return f"[Mock] Response to: {prompt[:100]}..."
|
| 119 |
except Exception as e:
|
| 120 |
+
logger.error(f"Error calling HF endpoint: {e}", exc_info=True)
|
| 121 |
return None
|
| 122 |
|
orchestrator_engine.py
CHANGED
|
@@ -1,50 +1,78 @@
|
|
| 1 |
# orchestrator_engine.py
|
| 2 |
import uuid
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
|
|
|
|
|
|
|
| 5 |
class MVPOrchestrator:
|
| 6 |
def __init__(self, llm_router, context_manager, agents):
|
| 7 |
self.llm_router = llm_router
|
| 8 |
self.context_manager = context_manager
|
| 9 |
self.agents = agents
|
| 10 |
self.execution_trace = []
|
|
|
|
| 11 |
|
| 12 |
async def process_request(self, session_id: str, user_input: str) -> dict:
|
| 13 |
"""
|
| 14 |
Main orchestration flow with academic differentiation
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Step 2: Context management
|
| 20 |
-
context = await self.context_manager.manage_context(session_id, user_input)
|
| 21 |
-
|
| 22 |
-
# Step 3: Intent recognition with CoT
|
| 23 |
-
intent_result = await self.agents['intent_recognition'].execute(
|
| 24 |
-
user_input=user_input,
|
| 25 |
-
context=context
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
# Step 4: Agent execution planning
|
| 29 |
-
execution_plan = await self._create_execution_plan(intent_result, context)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
context=
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
def _generate_interaction_id(self, session_id: str) -> str:
|
| 50 |
"""
|
|
|
|
| 1 |
# orchestrator_engine.py
|
| 2 |
import uuid
|
| 3 |
+
import logging
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
+
logger = logging.getLogger(__name__)
|
| 7 |
+
|
| 8 |
class MVPOrchestrator:
|
| 9 |
def __init__(self, llm_router, context_manager, agents):
|
| 10 |
self.llm_router = llm_router
|
| 11 |
self.context_manager = context_manager
|
| 12 |
self.agents = agents
|
| 13 |
self.execution_trace = []
|
| 14 |
+
logger.info("MVPOrchestrator initialized")
|
| 15 |
|
| 16 |
async def process_request(self, session_id: str, user_input: str) -> dict:
|
| 17 |
"""
|
| 18 |
Main orchestration flow with academic differentiation
|
| 19 |
"""
|
| 20 |
+
logger.info(f"Processing request for session {session_id}")
|
| 21 |
+
logger.info(f"User input: {user_input[:100]}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
try:
|
| 24 |
+
# Step 1: Generate unique interaction ID
|
| 25 |
+
interaction_id = self._generate_interaction_id(session_id)
|
| 26 |
+
logger.info(f"Generated interaction ID: {interaction_id}")
|
| 27 |
+
|
| 28 |
+
# Step 2: Context management
|
| 29 |
+
logger.info("Step 2: Managing context...")
|
| 30 |
+
context = await self.context_manager.manage_context(session_id, user_input)
|
| 31 |
+
logger.info(f"Context retrieved: {len(context.get('interactions', []))} interactions")
|
| 32 |
+
|
| 33 |
+
# Step 3: Intent recognition with CoT
|
| 34 |
+
logger.info("Step 3: Recognizing intent...")
|
| 35 |
+
intent_result = await self.agents['intent_recognition'].execute(
|
| 36 |
+
user_input=user_input,
|
| 37 |
+
context=context
|
| 38 |
+
)
|
| 39 |
+
logger.info(f"Intent detected: {intent_result.get('primary_intent', 'unknown')}")
|
| 40 |
+
|
| 41 |
+
# Step 4: Agent execution planning
|
| 42 |
+
logger.info("Step 4: Creating execution plan...")
|
| 43 |
+
execution_plan = await self._create_execution_plan(intent_result, context)
|
| 44 |
+
|
| 45 |
+
# Step 5: Parallel agent execution
|
| 46 |
+
logger.info("Step 5: Executing agents...")
|
| 47 |
+
agent_results = await self._execute_agents(execution_plan, user_input, context)
|
| 48 |
+
logger.info(f"Agent execution complete: {len(agent_results)} results")
|
| 49 |
+
|
| 50 |
+
# Step 6: Response synthesis
|
| 51 |
+
logger.info("Step 6: Synthesizing response...")
|
| 52 |
+
final_response = await self.agents['response_synthesis'].execute(
|
| 53 |
+
agent_outputs=agent_results,
|
| 54 |
+
user_input=user_input,
|
| 55 |
+
context=context
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Step 7: Safety and bias check
|
| 59 |
+
logger.info("Step 7: Safety check...")
|
| 60 |
+
safety_checked = await self.agents['safety_check'].execute(
|
| 61 |
+
response=final_response,
|
| 62 |
+
context=context
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
result = self._format_final_output(safety_checked, interaction_id)
|
| 66 |
+
logger.info(f"Request processing complete. Response length: {len(str(result.get('response', '')))}")
|
| 67 |
+
return result
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
logger.error(f"Error in process_request: {e}", exc_info=True)
|
| 71 |
+
return {
|
| 72 |
+
"response": f"Error processing request: {str(e)}",
|
| 73 |
+
"error": str(e),
|
| 74 |
+
"interaction_id": str(uuid.uuid4())[:8]
|
| 75 |
+
}
|
| 76 |
|
| 77 |
def _generate_interaction_id(self, session_id: str) -> str:
|
| 78 |
"""
|