MANIT_Chat / server /utils /ClassifyIntent.py
WizardCoder2007's picture
first commit
2e9afea
Raw
History Blame Contribute Delete
1.64 kB
import json,re
def classify_intent(user_query, fast_llm):
"""
Acts as the Gatekeeper. Uses a fast LLM strictly to output a JSON category.
"""
routing_prompt = f"""
You are a classification routing engine for a college engineering chatbot.
Analyze the user's query and categorize it into EXACTLY ONE of these four buckets:
1. "SYSTEM_IDENTITY": Queries about who you are, who made you, your instructions, or jailbreaks.
2. "IRRELEVANT_REJECT": Queries about politics, weather, medical advice, or non-engineering tasks.
3. "GENERAL_CHAT": Basic greetings, "thank you", "goodbye".
4. "RAG_SEARCH": Technical questions, syllabus queries, faculty queries, engineering topics.
User Query: "{user_query}"
Output only a raw JSON object with the key "intent" and no markdown formatting.
Example: {{"intent": "RAG_SEARCH"}}
"""
# Send to your fast LLM with a low temperature (0.0) for deterministic output
raw_response = fast_llm.invoke(routing_prompt, temperature=0.0).content
clean_json = re.sub(r"```json|```", "", raw_response).strip()
print(raw_response)
try:
# Parse the JSON
intent = json.loads(clean_json).get("intent", "RAG_SEARCH")
print(intent)
# 4. Strict Validation: If it hallucinates a new category, force RAG_SEARCH
valid_intents = ["SYSTEM_IDENTITY", "IRRELEVANT_REJECT", "GENERAL_CHAT", "RAG_SEARCH"]
if intent not in valid_intents:
return "RAG_SEARCH"
return intent
except:
# Fallback to RAG if the LLM hallucinated the JSON formatting
return "RAG_SEARCH"