Spaces:
Sleeping
Sleeping
Download agent/intent_classifier.py from bagustenan/backend: direct link, hf CLI and curl.
- Browser
- Download file 4.24 kB
-
https://huggingface.co/spaces/bagustenan/backend/resolve/main/agent/intent_classifier.py
- Command line
-
hf download hf://spaces/bagustenan/backend/agent/intent_classifier.py
-
curl -L -o intent_classifier.py https://huggingface.co/spaces/bagustenan/backend/resolve/main/agent/intent_classifier.py
4.24 kB
| """ | |
| 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] | |