File size: 2,980 Bytes
905e3b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Intent classification node for DocIntel."""
from app.agents.state import AgentState
from app.config import settings
import json

try:
    from groq import Groq
    client = Groq(api_key=settings.GROQ_API_KEY) if settings.GROQ_API_KEY else None
except ImportError:
    client = None

def classify_intent(state: AgentState) -> AgentState:
    """Classify user intent"""
    print(f"πŸ“‹ Classifying intent for: {state['query']}")
    
    # Simple keyword-based classification first (faster, no API call)
    query = state["query"].lower()
    
    # Check for summary intent
    summary_keywords = ["summarize", "summary", "brief", "overview", "summarise"]
    if any(word in query for word in summary_keywords):
        state["intent"] = "summary"
        print(f"βœ… Intent: summary (keyword match)")
        return state
    
    # Check for email intent
    email_keywords = ["email", "draft", "send", "compose", "write an email"]
    if any(word in query for word in email_keywords):
        state["intent"] = "email"
        print(f"βœ… Intent: email (keyword match)")
        return state
    
    # Check for extraction intent
    extract_keywords = [
        "extract", "list", "find", "show me", 
        "what are", "dates", "amounts", "numbers",
        "entities", "names", "people", "organizations"
    ]
    if any(word in query for word in extract_keywords):
        state["intent"] = "extract"
        print(f"βœ… Intent: extract (keyword match)")
        return state
    
    # Use LLM fallback for better classification
    if client is not None:
        try:
            prompt = f"""
            Analyze this user query and classify it into one of these intents:
            
            1. "qa" - General question about document content
            2. "summary" - Request for document summary
            3. "email" - Need to draft an email based on documents
            4. "extract" - Need specific data extraction (dates, amounts, names, etc.)
            
            Query: {state["query"]}
            
            Return ONLY a JSON object with:
            {{
                "intent": "category",
                "reasoning": "brief explanation"
            }}
            """
            
            response = client.chat.completions.create(
                model=settings.GROQ_MODEL,
                messages=[{"role": "user", "content": prompt}],
                temperature=0.1,
                max_tokens=200
            )
            
            result = json.loads(response.choices[0].message.content)
            state["intent"] = result.get("intent", "qa")
            print(f"βœ… Intent: {state['intent']} (LLM)")
            
        except Exception as e:
            print(f"⚠️ Classification error: {e}")
            state["intent"] = "qa"
            state["error"] = str(e)
    else:
        # Default to Q&A if no client
        state["intent"] = "qa"
        print(f"βœ… Intent: qa (no LLM client)")
    
    return state