Spaces:
Paused
Paused
File size: 28,267 Bytes
22eeb7e | 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 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 | """
π¦ PENNY Request Router - Enhanced for Azure ML Production
Routes incoming requests to appropriate agents and tools based on intent classification.
Integrates with enhanced logging, location detection, and intent classification.
Mission: Ensure every resident request reaches the right civic service with proper tracking.
"""
import logging
import time
import asyncio
import os
from typing import Dict, Any, Optional, List
from pathlib import Path
from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse
from app.model_loader import ModelLoader
from app.tool_agent import handle_tool_request
from app.weather_agent import (
get_weather_for_location,
weather_to_event_recommendations,
recommend_outfit
)
from app.intents import classify_intent_detailed, IntentType
from app.event_weather import get_event_recommendations_with_weather
from app.location_utils import (
detect_location_from_text,
get_city_info,
validate_coordinates
)
from app.logging_utils import log_interaction, sanitize_for_logging
logger = logging.getLogger(__name__)
# Initialize FastAPI router
router = APIRouter(prefix="/api", tags=["Penny API"])
# Initialize model loader
models = ModelLoader()
# Supported languages for translation routing
SUPPORTED_LANGUAGES = [
"arabic", "french", "german", "hindi", "mandarin",
"portuguese", "russian", "spanish", "swahili",
"tagalog", "urdu", "vietnamese", "translate", "translation"
]
def validate_request_payload(payload: dict) -> tuple[bool, Optional[str]]:
"""
Validate incoming request payload for required fields and data types.
Args:
payload: Request payload dictionary
Returns:
Tuple of (is_valid, error_message)
"""
if not isinstance(payload, dict):
return False, "Payload must be a dictionary"
# Check for required input field
if "input" not in payload:
return False, "Missing required field: 'input'"
user_input = payload.get("input")
if not isinstance(user_input, str):
return False, "Field 'input' must be a string"
if not user_input.strip():
return False, "Input cannot be empty"
# Validate coordinates if provided
lat = payload.get("lat")
lon = payload.get("lon")
if lat is not None or lon is not None:
if lat is None or lon is None:
return False, "Both 'lat' and 'lon' must be provided together"
try:
lat = float(lat)
lon = float(lon)
is_valid, error = validate_coordinates(lat, lon)
if not is_valid:
return False, f"Invalid coordinates: {error}"
except (ValueError, TypeError):
return False, "Coordinates must be numeric values"
# Validate tenant_id if provided
tenant_id = payload.get("tenant_id")
if tenant_id is not None:
if not isinstance(tenant_id, str):
return False, "Field 'tenant_id' must be a string"
if not tenant_id.strip():
return False, "Field 'tenant_id' cannot be empty"
return True, None
def extract_location_info(payload: dict, user_input: str) -> Dict[str, Any]:
"""
Extract and validate location information from payload or user input.
Args:
payload: Request payload
user_input: User's input text
Returns:
Dictionary with location info: {lat, lon, tenant_id, city_info, location_source}
"""
location_info = {
"lat": payload.get("lat"),
"lon": payload.get("lon"),
"tenant_id": payload.get("tenant_id", "default"),
"city_info": None,
"location_source": "none"
}
try:
# Try to get location from coordinates
if location_info["lat"] is not None and location_info["lon"] is not None:
location_info["location_source"] = "coordinates"
# Try to map coordinates to a tenant city
if location_info["tenant_id"] == "default":
city_info = get_city_info(location_info["tenant_id"])
if city_info:
location_info["city_info"] = city_info
# Try to detect location from text if not provided
elif "near me" in user_input.lower() or any(
keyword in user_input.lower()
for keyword in ["in", "at", "near", "around"]
):
detected = detect_location_from_text(user_input)
if detected.get("found"):
location_info["tenant_id"] = detected.get("tenant_id", "default")
location_info["city_info"] = detected.get("city_info")
location_info["location_source"] = "text_detection"
logger.info(
f"Detected location from text: {location_info['tenant_id']}"
)
# Get city info for tenant_id if we have it
if not location_info["city_info"] and location_info["tenant_id"] != "default":
location_info["city_info"] = get_city_info(location_info["tenant_id"])
except Exception as e:
logger.warning(f"Error extracting location info: {e}")
return location_info
def route_request(payload: dict) -> dict:
"""
Main routing function for PENNY requests.
Routes requests to appropriate agents based on intent classification.
Args:
payload: Request payload with user input and metadata
Returns:
Response dictionary with agent output and metadata
"""
start_time = time.time()
try:
# Validate request payload
is_valid, error_msg = validate_request_payload(payload)
if not is_valid:
logger.warning(f"Invalid request payload: {error_msg}")
return {
"error": "Oops! I couldn't understand that request. " + error_msg,
"status": "validation_error",
"response_time_ms": round((time.time() - start_time) * 1000)
}
# Extract basic request info
user_input = payload.get("input", "").strip()
role = payload.get("role", "unknown")
# Sanitize input for logging (remove PII)
sanitized_input = sanitize_for_logging(user_input)
# Extract location information
location_info = extract_location_info(payload, user_input)
tenant_id = location_info["tenant_id"]
lat = location_info["lat"]
lon = location_info["lon"]
logger.info(
f"Routing request from tenant '{tenant_id}', role '{role}', "
f"location_source: {location_info['location_source']}"
)
# Classify intent using enhanced intent classifier
try:
intent_result = classify_intent_detailed(user_input)
intent = intent_result["intent"]
confidence = intent_result["confidence"]
is_compound = intent_result["is_compound"]
logger.info(
f"Intent classified: {intent} (confidence: {confidence:.2f}, "
f"compound: {is_compound})"
)
except Exception as e:
logger.error(f"Intent classification failed: {e}")
intent = IntentType.GENERAL
confidence = 0.0
is_compound = False
# EMERGENCY ROUTING - Highest priority
if intent == IntentType.EMERGENCY:
logger.critical(
f"EMERGENCY intent detected from tenant '{tenant_id}'. "
f"Routing to safety protocols."
)
# Log emergency interaction for compliance
log_interaction(
tenant_id=tenant_id,
interaction_type="emergency",
intent="emergency",
response_time_ms=round((time.time() - start_time) * 1000),
success=True,
metadata={
"sanitized_input": sanitized_input,
"requires_followup": True,
"escalation_level": "critical"
}
)
return {
"response": (
"I can see you might need urgent help. Please contact:\n\n"
"π¨ **Emergency Services**: 911\n"
"π **National Crisis Hotline**: 988\n"
"π¬ **Crisis Text Line**: Text HOME to 741741\n\n"
"You're not alone, and help is available 24/7."
),
"intent": "emergency",
"model_id": "safety-agent",
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"escalation_required": True
}
# WEATHER ROUTING
if intent == IntentType.WEATHER:
return handle_weather_request(
user_input, lat, lon, tenant_id, role, start_time
)
# WEATHER + EVENTS ROUTING (compound intent)
if intent == IntentType.WEATHER_EVENTS or (
is_compound and "weather" in intent_result.get("components", [])
):
return handle_weather_events_request(
user_input, lat, lon, tenant_id, role, start_time
)
# EVENTS ROUTING
if intent == IntentType.EVENTS:
return handle_events_request(
user_input, tenant_id, role, start_time
)
# TOOL-BASED ROUTING (transit, alerts, resources, etc.)
if intent in [
IntentType.TRANSIT, IntentType.ALERTS, IntentType.RESOURCES,
IntentType.PUBLIC_WORKS
]:
return handle_tool_based_request(
user_input, intent, tenant_id, role, start_time
)
# TRANSLATION ROUTING
if intent == IntentType.TRANSLATION or any(
lang in user_input.lower() for lang in SUPPORTED_LANGUAGES
):
return handle_translation_request(
user_input, tenant_id, role, start_time
)
# DOCUMENT/PDF ROUTING
if any(term in user_input.lower() for term in ["form", "upload", "document", "pdf"]):
return handle_document_request(
user_input, tenant_id, role, start_time
)
# SENTIMENT ANALYSIS ROUTING
if any(term in user_input.lower() for term in ["angry", "sentiment", "how do i feel"]):
return handle_sentiment_request(
user_input, tenant_id, role, start_time
)
# BIAS DETECTION ROUTING
if any(term in user_input.lower() for term in ["bias", "is this fair", "offensive"]):
return handle_bias_request(
user_input, tenant_id, role, start_time
)
# GENERAL/FALLBACK ROUTING
return handle_general_request(
user_input, tenant_id, role, start_time
)
except Exception as e:
logger.error(f"Unexpected error in route_request: {e}", exc_info=True)
return {
"error": (
"I'm having trouble processing that right now. "
"Could you try rephrasing your question? π"
),
"status": "server_error",
"response_time_ms": round((time.time() - start_time) * 1000)
}
def handle_weather_request(
user_input: str, lat: Optional[float], lon: Optional[float],
tenant_id: str, role: str, start_time: float
) -> dict:
"""Handle weather-specific requests."""
try:
if lat is None or lon is None:
return {
"response": (
"I'd love to help with the weather! To give you accurate info, "
"I need your location. Can you share your coordinates or tell me "
"what city you're in? π€οΈ"
),
"intent": "weather",
"model_id": "weather-agent",
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"location_required": True
}
# Get weather data
weather = asyncio.run(get_weather_for_location(lat, lon))
# Generate recommendations
recs = weather_to_event_recommendations(weather)
outfit = recommend_outfit(
weather.get("temperature", {}).get("value"),
weather.get("phrase", "")
)
end_time = time.time()
response_time = round((end_time - start_time) * 1000)
# Log successful interaction
log_interaction(
tenant_id=tenant_id,
interaction_type="weather",
intent="weather",
response_time_ms=response_time,
success=True
)
return {
"response": {
"weather": weather,
"recommendations": recs,
"outfit": outfit
},
"intent": "weather",
"model_id": "weather-agent",
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": response_time
}
except Exception as e:
logger.error(f"Error handling weather request: {e}")
return {
"response": (
"I'm having trouble getting the weather right now. "
"The weather service might be down. Want to try again in a moment? π¦οΈ"
),
"intent": "weather",
"model_id": "weather-agent",
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"error": "weather_service_unavailable"
}
def handle_weather_events_request(
user_input: str, lat: Optional[float], lon: Optional[float],
tenant_id: str, role: str, start_time: float
) -> dict:
"""Handle combined weather and events requests."""
try:
if lat is None or lon is None:
return {
"response": (
"I can suggest events based on the weather! "
"To do that, I need your location. Can you share your coordinates "
"or tell me what city you're in? πβοΈ"
),
"intent": "weather_events",
"model_id": "event-weather-agent",
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"location_required": True
}
# Get combined weather and event recommendations
combined = asyncio.run(
get_event_recommendations_with_weather(tenant_id, lat, lon)
)
end_time = time.time()
response_time = round((end_time - start_time) * 1000)
# Log successful interaction
log_interaction(
tenant_id=tenant_id,
interaction_type="weather_events",
intent="weather_events",
response_time_ms=response_time,
success=True
)
return {
"response": combined,
"intent": "weather_events",
"model_id": "event-weather-agent",
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": response_time
}
except Exception as e:
logger.error(f"Error handling weather_events request: {e}")
return {
"response": (
"I'm having trouble combining weather and events right now. "
"Let me try to help you with just one or the other! π€"
),
"intent": "weather_events",
"model_id": "event-weather-agent",
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"error": "combined_service_unavailable"
}
def handle_events_request(
user_input: str, tenant_id: str, role: str, start_time: float
) -> dict:
"""Handle events-only requests."""
try:
tool_response = handle_tool_request(user_input, role, tenant_id, "events")
end_time = time.time()
return {
"response": tool_response.get("response"),
"intent": "events",
"model_id": "event-agent",
"tenant_id": tool_response.get("city", tenant_id),
"user_role": role,
"response_time_ms": round((end_time - start_time) * 1000)
}
except Exception as e:
logger.error(f"Error handling events request: {e}")
return {
"response": (
"I'm having trouble finding events right now. "
"Let me know what you're interested in and I'll do my best! π"
),
"intent": "events",
"model_id": "event-agent",
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"error": "events_service_unavailable"
}
def handle_tool_based_request(
user_input: str, intent: str, tenant_id: str, role: str, start_time: float
) -> dict:
"""Handle tool-based requests (transit, alerts, resources, etc.)."""
try:
tool_response = handle_tool_request(user_input, role, tenant_id, intent)
end_time = time.time()
return {
"response": tool_response.get("response"),
"intent": str(intent),
"model_id": tool_response.get("tool", "tool-agent"),
"tenant_id": tool_response.get("city", tenant_id),
"user_role": role,
"response_time_ms": round((end_time - start_time) * 1000)
}
except Exception as e:
logger.error(f"Error handling tool request for {intent}: {e}")
return {
"response": (
f"I'm having trouble with that {intent} request right now. "
"Could you try again or ask me something else? π"
),
"intent": str(intent),
"model_id": "tool-agent",
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"error": f"{intent}_service_unavailable"
}
def handle_translation_request(
user_input: str, tenant_id: str, role: str, start_time: float
) -> dict:
"""Handle translation requests."""
model_id = "penny-translate-agent"
try:
model = models.get(model_id)
if not model:
raise ValueError(f"Translation model not found: {model_id}")
result = model.predict(user_input)
end_time = time.time()
return {
"response": result,
"intent": "translation",
"model_id": model_id,
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((end_time - start_time) * 1000)
}
except Exception as e:
logger.error(f"Error handling translation request: {e}")
return {
"response": (
"I'm having trouble with translation right now. "
"Which language would you like help with? π"
),
"intent": "translation",
"model_id": model_id,
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"error": "translation_service_unavailable"
}
def handle_document_request(
user_input: str, tenant_id: str, role: str, start_time: float
) -> dict:
"""Handle document/PDF processing requests."""
model_id = "penny-doc-agent"
try:
model = models.get(model_id)
if not model:
raise ValueError(f"Document model not found: {model_id}")
result = model.predict(user_input)
end_time = time.time()
return {
"response": result,
"intent": "document",
"model_id": model_id,
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((end_time - start_time) * 1000)
}
except Exception as e:
logger.error(f"Error handling document request: {e}")
return {
"response": (
"I'm having trouble processing documents right now. "
"What kind of form or document do you need help with? π"
),
"intent": "document",
"model_id": model_id,
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"error": "document_service_unavailable"
}
def handle_sentiment_request(
user_input: str, tenant_id: str, role: str, start_time: float
) -> dict:
"""Handle sentiment analysis requests."""
model_id = "penny-sentiment-agent"
try:
model = models.get(model_id)
if not model:
raise ValueError(f"Sentiment model not found: {model_id}")
result = model.predict(user_input)
end_time = time.time()
return {
"response": result,
"intent": "sentiment",
"model_id": model_id,
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((end_time - start_time) * 1000)
}
except Exception as e:
logger.error(f"Error handling sentiment request: {e}")
return {
"response": (
"I'm having trouble analyzing sentiment right now. "
"How are you feeling about things? π"
),
"intent": "sentiment",
"model_id": model_id,
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"error": "sentiment_service_unavailable"
}
def handle_bias_request(
user_input: str, tenant_id: str, role: str, start_time: float
) -> dict:
"""Handle bias detection requests."""
model_id = "penny-bias-checker"
try:
model = models.get(model_id)
if not model:
raise ValueError(f"Bias model not found: {model_id}")
result = model.predict(user_input)
end_time = time.time()
return {
"response": result,
"intent": "bias_check",
"model_id": model_id,
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((end_time - start_time) * 1000)
}
except Exception as e:
logger.error(f"Error handling bias request: {e}")
return {
"response": (
"I'm having trouble checking for bias right now. "
"What content would you like me to review? βοΈ"
),
"intent": "bias_check",
"model_id": model_id,
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"error": "bias_service_unavailable"
}
def handle_general_request(
user_input: str, tenant_id: str, role: str, start_time: float
) -> dict:
"""Handle general/fallback requests."""
model_id = "penny-core-agent"
try:
model = models.get(model_id)
if not model:
raise ValueError(f"Core model not found: {model_id}")
result = model.predict(user_input)
end_time = time.time()
return {
"response": result,
"intent": "general",
"model_id": model_id,
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((end_time - start_time) * 1000)
}
except Exception as e:
logger.error(f"Error handling general request: {e}")
return {
"response": (
"I'm having some technical difficulties right now. "
"Can you try asking your question in a different way? "
"Or let me know if you need help with weather, events, or services! π"
),
"intent": "general",
"model_id": model_id,
"tenant_id": tenant_id,
"user_role": role,
"response_time_ms": round((time.time() - start_time) * 1000),
"error": "general_service_unavailable"
}
@router.post("/chat", response_model=Dict[str, Any])
async def chat_endpoint(payload: Dict[str, Any]) -> JSONResponse:
"""
π¬ Main chat endpoint for Penny.
Processes user requests and routes them to appropriate handlers.
Args:
payload: Request payload with 'input', 'tenant_id', 'lat', 'lon', etc.
Returns:
JSONResponse with Penny's response
"""
try:
result = route_request(payload)
return JSONResponse(status_code=200, content=result)
except Exception as e:
logger.error(f"Error in chat endpoint: {e}", exc_info=True)
return JSONResponse(
status_code=500,
content={
"error": "I'm having trouble processing that right now. Please try again! π",
"detail": str(e) if os.getenv("DEBUG_MODE", "false").lower() == "true" else None
}
)
@router.get("/health/router", response_model=Dict[str, Any])
async def router_health_endpoint() -> JSONResponse:
"""
π Router health check endpoint.
Returns:
Health status of the router component
"""
try:
health = get_router_health()
return JSONResponse(status_code=200, content=health)
except Exception as e:
logger.error(f"Router health check failed: {e}")
return JSONResponse(
status_code=500,
content={
"status": "degraded",
"error": str(e)
}
)
def get_router_health() -> dict:
"""
Check router health status.
Returns:
Health status dictionary
"""
try:
return {
"status": "operational",
"model_loader": "initialized" if models else "not_initialized",
"supported_languages": len(SUPPORTED_LANGUAGES),
"routing_capabilities": [
"weather", "events", "weather_events", "translation",
"documents", "sentiment", "bias_detection", "general"
]
}
except Exception as e:
logger.error(f"Router health check failed: {e}")
return {
"status": "degraded",
"error": str(e)
} |