File size: 4,241 Bytes
3851c17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Intent Classifier for Dynamic Tool Loading.

Classifies user query into an intent category, then returns only the
tools relevant for that intent. This keeps the model context lean by
injecting 2–3 tools per request instead of the full tool list.

Intent categories
-----------------
- STATISTICAL_QA  β†’ questions about BPS data, statistics, population, etc.
- EXPORT          β†’ user explicitly wants to download / export to Excel
- WEB_SEARCH      β†’ general info about BPS office, news, contact, etc.
- GREETING        β†’ greetings / small-talk (no tool needed)
- UNKNOWN         β†’ fallback β€” load all tools
"""

from __future__ import annotations

import re
from typing import Literal

# ---------------------------------------------------------------------------
# Type alias
# ---------------------------------------------------------------------------
IntentCategory = Literal[
    "STATISTICAL_QA",
    "EXPORT",
    "WEB_SEARCH",
    "GREETING",
    "UNKNOWN",
]

# ---------------------------------------------------------------------------
# Keyword banks (Indonesian + common abbreviations)
# ---------------------------------------------------------------------------
_EXPORT_KEYWORDS = [
    "download", "unduh", "export", "ekspor",
    "excel", "xlsx", "tabel excel", "simpan", "buatkan file",
    "cetak", "unduhkan",
]

_WEB_SEARCH_KEYWORDS = [
    "siapa kepala", "alamat", "kontak", "telepon", "email",
    "struktur organisasi", "berita", "pengumuman", "jadwal",
    "jam buka", "jam operasional", "website", "web bps",
    "sejarah bps", "profil bps", "visi misi",
]

_STATISTICAL_KEYWORDS = [
    "berapa", "jumlah", "angka", "data", "statistik",
    "penduduk", "populasi", "inflasi", "pdrb", "pertumbuhan",
    "kemiskinan", "pengangguran", "ipg", "indeks", "survei",
    "sensus", "proyeksi", "rata-rata", "median", "persentase",
    "kabupaten", "flores timur", "kecamatan", "kelurahan",
    "tahun", "2020", "2021", "2022", "2023", "2024", "2025",
]

_GREETING_KEYWORDS = [
    "halo", "hai", "hello", "hi", "selamat pagi", "selamat siang",
    "selamat sore", "selamat malam", "apa kabar", "terima kasih",
    "makasih", "thanks", "assalamualaikum", "salam",
]


# ---------------------------------------------------------------------------
# Classifier
# ---------------------------------------------------------------------------
def classify_intent(query: str) -> IntentCategory:
    """
    Lightweight keyword-based intent classifier.

    Returns one of: STATISTICAL_QA, EXPORT, WEB_SEARCH, GREETING, UNKNOWN.
    Priority order: EXPORT > WEB_SEARCH > STATISTICAL_QA > GREETING > UNKNOWN.
    """
    q = query.lower().strip()
    # Collapse multiple spaces
    q = re.sub(r"\s+", " ", q)

    def _hits(keywords: list[str]) -> int:
        return sum(1 for kw in keywords if kw in q)

    # Hard-priority: export is always unambiguous
    if _hits(_EXPORT_KEYWORDS) >= 1:
        return "EXPORT"

    web_score = _hits(_WEB_SEARCH_KEYWORDS)
    stat_score = _hits(_STATISTICAL_KEYWORDS)
    greet_score = _hits(_GREETING_KEYWORDS)

    if greet_score >= 1 and stat_score == 0 and web_score == 0:
        return "GREETING"

    if stat_score > 0 and stat_score >= web_score:
        return "STATISTICAL_QA"

    if web_score > 0:
        return "WEB_SEARCH"

    # Fallback
    return "UNKNOWN"


# ---------------------------------------------------------------------------
# Tool selector β€” returns the minimal tool list for the given intent
# ---------------------------------------------------------------------------
def get_tools_for_intent(intent: IntentCategory):
    """
    Return a list of LangChain tool objects relevant to the detected intent.

    Importing here (lazy) to avoid circular import at module load time.
    """
    from .tools import rag_tool, rag_excel_tool, search_tool  # noqa: PLC0415

    _INTENT_TOOL_MAP: dict[IntentCategory, list] = {
        "STATISTICAL_QA": [rag_tool],
        "EXPORT":         [rag_excel_tool],
        "WEB_SEARCH":     [search_tool],
        "GREETING":       [],                              # no tool needed
        "UNKNOWN":        [rag_tool, rag_excel_tool, search_tool],  # full set
    }

    return _INTENT_TOOL_MAP[intent]