File size: 5,011 Bytes
501bc8c | 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 | import json
import logging
import re
from typing import List, Dict, Any, Optional, Literal
from pydantic import BaseModel, Field, field_validator
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from .prompt_manager import PromptManager
from .config import SCORING
logger = logging.getLogger("IntentRouter")
VALID_INTENTS = {"SIMPLE_SEARCH", "DEEP_RESEARCH", "SPECIFIC_ITEM_ANALYSIS", "MULTIMODAL_ANALYSIS", "CHAT", "ADVISORY"}
BLENDABLE_INTENTS = {"SIMPLE_SEARCH", "DEEP_RESEARCH", "ADVISORY"}
class RoutingResult(BaseModel):
"""Validated routing output from the intent classification LLM."""
intent: str = "SIMPLE_SEARCH"
secondary_intent: Optional[str] = None
confidence: float = Field(default=0.8, ge=0.0, le=1.0)
keywords: List[str] = Field(default_factory=list)
reason: str = ""
@field_validator("intent")
@classmethod
def validate_intent(cls, v: str) -> str:
if v not in VALID_INTENTS:
logger.warning(f"Unknown intent '{v}', falling back to SIMPLE_SEARCH")
return "SIMPLE_SEARCH"
return v
@field_validator("secondary_intent")
@classmethod
def validate_secondary(cls, v: Optional[str]) -> Optional[str]:
if v and v not in BLENDABLE_INTENTS:
return None
return v
@field_validator("confidence", mode="before")
@classmethod
def coerce_confidence(cls, v: Any) -> float:
try:
return max(0.0, min(1.0, float(v)))
except (TypeError, ValueError):
return 0.8
class IntentRouter:
def __init__(self, llm: ChatGoogleGenerativeAI, prompt_manager: PromptManager, run_config: dict = None):
self.llm = llm
self.prompt_manager = prompt_manager
self._run_config = run_config or {}
self._init_chain()
def _init_chain(self):
prompt_str = self.prompt_manager.get_prompt("intent_router")
self.prompt = ChatPromptTemplate.from_template(prompt_str)
self.chain = self.prompt | self.llm | StrOutputParser()
def route(self, query: str, chat_history: List[Dict], has_image: bool = False) -> Dict[str, Any]:
"""
Analyzes the query and returns a validated dictionary:
{
"intent": "SIMPLE_SEARCH" | "DEEP_RESEARCH" | "SPECIFIC_ITEM_ANALYSIS" | "MULTIMODAL_ANALYSIS" | "CHAT" | "ADVISORY",
"secondary_intent": "SIMPLE_SEARCH" | "DEEP_RESEARCH" | null,
"confidence": 0.0 ~ 1.0,
"keywords": [...],
"reason": "..."
}
"""
history_str = "\n".join([f"{msg['role']}: {msg['content']}" for msg in chat_history[-SCORING.max_history_messages:]])
try:
result_str = self.chain.invoke({
"query": query,
"history_str": history_str,
"has_image": str(has_image)
}, config=self._run_config)
# Parse JSON from LLM output
parsed_result = self._parse_llm_json(result_str)
# Validate via Pydantic model
validated = RoutingResult(**parsed_result)
# Post-processing: nullify secondary if same as primary
if validated.secondary_intent == validated.intent:
validated.secondary_intent = None
result_dict = validated.model_dump()
logger.info(f"Intent Routing Result: {result_dict}")
return result_dict
except Exception as e:
logger.error(f"Error during intent routing: {e}")
return {
"intent": "MULTIMODAL_ANALYSIS" if has_image else "SIMPLE_SEARCH",
"secondary_intent": None,
"confidence": 0.3,
"keywords": [query],
"reason": "Error_Fallback"
}
@staticmethod
def _parse_llm_json(text: str) -> Dict:
"""Parse JSON from LLM output with multi-tier fallback."""
try:
return json.loads(text)
except (json.JSONDecodeError, TypeError):
pass
# Markdown fence
match = re.search(r'```json\s*(.*?)\s*```', text, re.DOTALL)
if match:
try:
return json.loads(match.group(1))
except json.JSONDecodeError:
pass
# Brace extraction
start = text.find('{')
end = text.rfind('}') + 1
if start != -1 and end > start:
try:
return json.loads(text[start:end])
except json.JSONDecodeError:
pass
return {}
def _validate_result(self, result: Dict, has_image: bool) -> Dict:
"""Legacy validation — kept for backward compatibility with tests."""
validated = RoutingResult(**result)
if validated.secondary_intent == validated.intent:
validated.secondary_intent = None
return validated.model_dump()
|