Download src/nodes/classifier.py from mrmayankmathurorg/Customer-EMAIL-Agent: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/mrmayankmathurorg/Customer-EMAIL-Agent/resolve/main/src/nodes/classifier.py
- Command line
-
hf download hf://spaces/mrmayankmathurorg/Customer-EMAIL-Agent/src/nodes/classifier.py
-
curl -L -o classifier.py https://huggingface.co/spaces/mrmayankmathurorg/Customer-EMAIL-Agent/resolve/main/src/nodes/classifier.py
1.63 kB
| """ | |
| Classifier node — categorises the incoming email using the LLM. | |
| """ | |
| from loguru import logger | |
| from src.prompts.templates import CLASSIFIER_PROMPT | |
| from src.schemas.state import EmailCategory, SupportState | |
| from src.services.llm_service import invoke_llm_json | |
| from src.utils.security import sanitize_input | |
| # Mapping of raw category strings to the EmailCategory enum | |
| _CATEGORY_MAP = { | |
| "billing": EmailCategory.BILLING, | |
| "technical": EmailCategory.TECHNICAL, | |
| "account": EmailCategory.ACCOUNT, | |
| "general": EmailCategory.GENERAL, | |
| } | |
| async def classify_email(state: SupportState) -> dict: | |
| """ | |
| Classify the incoming email into a support category. | |
| Uses the LLM to determine the intent of the customer email, | |
| returning the category, confidence score, and reasoning. | |
| """ | |
| logger.info("Classifying email: {}", state.email.subject) | |
| prompt = CLASSIFIER_PROMPT.format( | |
| subject=sanitize_input(state.email.subject), | |
| body=sanitize_input(state.email.body), | |
| ) | |
| result = await invoke_llm_json(prompt) | |
| # Extract and map category | |
| raw_category = result.get("category", "general").lower().strip() | |
| category = _CATEGORY_MAP.get(raw_category, EmailCategory.UNKNOWN) | |
| confidence = float(result.get("confidence", 0.5)) | |
| reasoning = result.get("reasoning", "") | |
| logger.info( | |
| "Classified as '{}' (confidence={:.2f}): {}", | |
| category.value, | |
| confidence, | |
| reasoning, | |
| ) | |
| return { | |
| "category": category, | |
| "confidence": confidence, | |
| "metadata": {**state.metadata, "classification_reasoning": reasoning}, | |
| } | |