File size: 1,642 Bytes
2e9afea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"