import os from typing import Optional import pandas as pd from .rag_service import get_rag_service from insurance.coverage_explainer import CoverageExplainer USE_LLM = os.getenv("USE_LLM", "true").lower() == "true" def parse_intent_simple(user_input: str, services_data: pd.DataFrame) -> Optional[str]: """Simple rule-based intent parsing with coverage question support.""" user_input = user_input.lower() # Check coverage questions first (not really used now, but harmless) if CoverageExplainer.identify_coverage_question(user_input): return "coverage_explanation" # Check for help/list requests if any(word in user_input for word in ["help", "support", "services", "available"]): return "list_services" # Service keyword matching - match service descriptions mentioned in the user input res = services_data[ services_data["description"].apply( lambda desc: desc.lower() in user_input ) ] if not res.empty: return res.iloc[0]["intent"] return None def parse_intent_with_llm( user_input: str, services_data: pd.DataFrame, hospital_name: str = "Unknown Hospital", ) -> Optional[str]: """LLM-powered intent parsing with coverage question detection.""" # Coverage questions bypass RAG (though app.py already handles these first) if CoverageExplainer.identify_coverage_question(user_input): return "coverage_explanation" try: rag_service = get_rag_service() rag_service.initialize_vector_store( services_data, hospital_name, force_reload=False, ) intent = rag_service.parse_intent_with_llm(user_input, services_data) return intent except Exception as e: print(f"Error in LLM parsing: {e}. Falling back to simple parsing.") return parse_intent_simple(user_input, services_data) def parse_intent( user_input: str, services_data: pd.DataFrame, hospital_name: str = "Unknown Hospital", use_llm: Optional[bool] = None, ) -> Optional[str]: """ Main intent parser - detects service requests or coverage questions. Returns: service intent, "list_services", "coverage_explanation", or None. Note: coverage questions are already handled in app.py before this is called. """ should_use_llm = use_llm if use_llm is not None else USE_LLM if should_use_llm: return parse_intent_with_llm(user_input, services_data, hospital_name) else: print("Using simple parsing.") return parse_intent_simple(user_input, services_data)