File size: 1,630 Bytes
6793beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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},
    }