Download src/engine/router.py from YGCXLab/ux-research-agent-slackbot: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/spaces/YGCXLab/ux-research-agent-slackbot/resolve/main/src/engine/router.py
- Command line
-
hf download hf://spaces/YGCXLab/ux-research-agent-slackbot/src/engine/router.py
-
curl -L -o router.py https://huggingface.co/spaces/YGCXLab/ux-research-agent-slackbot/resolve/main/src/engine/router.py
5.01 kB
| 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 = "" | |
| 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 | |
| def validate_secondary(cls, v: Optional[str]) -> Optional[str]: | |
| if v and v not in BLENDABLE_INTENTS: | |
| return None | |
| return v | |
| 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" | |
| } | |
| 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() | |