IntegraChat / backend /api /services /intent_classifier.py
nothingworry's picture
working the rag and web server
2f235a0
raw
history blame
1.27 kB
from dataclasses import dataclass, field
from typing import Dict, List
@dataclass
class IntentClassifier:
intent_keywords: Dict[str, List[str]] = field(default_factory=lambda:{
"rag":["document","policy","manual","procedure","hr"],
"web":["latest","today","news","current","price","stock"],
"admin":["delete","remove","export","salary","confidential"],
"general":["explain","summary","help"]
})
llm_client: any = None
async def classify(self, text: str) -> str:
t = text.lower()
scores={k:0 for k in self.intent_keywords}
for k, words in self.intent_keywords.items():
for w in words:
if w in t: scores[k]+=1
best = max(scores, key=scores.get)
if scores[best] > 0: return best
# LLM fallback with error handling
if self.llm_client:
try:
prompt=f"Classify into rag/web/admin/general. User: '{text}'"
out = (await self.llm_client.simple_call(prompt)).strip().lower()
return out if out in scores else "general"
except Exception:
# LLM failed (not configured or unavailable), default to general
return "general"
return "general"