File size: 11,162 Bytes
09801ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""
Intelligent Query Router
=========================

Detects user intent from query and routes to appropriate:
- Response type: text, chart, table, graph visualization
- Mode: RAG, GraphRAG, Hybrid, Vision, Prediction
- Model: DeepSeek, Claude, GPT-4V based on complexity

No hardcoding - all pattern-based detection.
"""

import re
from typing import Dict, Any, List, Tuple, Optional
from dataclasses import dataclass
from enum import Enum


class ResponseType(Enum):
    TEXT = "text"
    CHART = "chart"
    TABLE = "table"
    GRAPH = "graph"  # Knowledge graph visualization
    MIXED = "mixed"  # Text + visuals


class QueryIntent(Enum):
    LOOKUP = "lookup"           # Simple fact retrieval
    ANALYSIS = "analysis"       # Deeper analysis
    COMPARISON = "comparison"   # Compare entities
    TREND = "trend"             # Time-based trends
    BREAKDOWN = "breakdown"     # Category breakdown
    PREDICTION = "prediction"   # Future forecasting
    VISUALIZATION = "visualization"  # Explicit chart request
    RELATIONSHIP = "relationship"    # Entity relationships
    SUMMARY = "summary"         # Overview/summary


@dataclass
class QueryAnalysis:
    """Result of query analysis"""
    intent: QueryIntent
    response_type: ResponseType
    suggested_mode: str
    suggested_model: str
    chart_type: Optional[str]
    entities: List[str]
    metrics: List[str]
    time_reference: Optional[str]
    confidence: float


# Intent detection patterns (no hardcoding - pattern-based)
INTENT_PATTERNS = {
    QueryIntent.COMPARISON: [
        r'\bcompare\b', r'\bvs\b', r'\bversus\b', r'\bdifference\b',
        r'\bhigher\b.*\bthan\b', r'\blower\b.*\bthan\b', r'\bbetter\b.*\bthan\b',
        r'\bwhich\b.*\b(more|less|higher|lower|better)\b'
    ],
    QueryIntent.TREND: [
        r'\btrend\b', r'\bover time\b', r'\bhistory\b', r'\bgrowth\b',
        r'\bchange\b.*\b(over|since|from)\b', r'\bmonthly\b', r'\byearly\b',
        r'\bquarterly\b', r'\blast\b.*\b(months?|years?|weeks?)\b'
    ],
    QueryIntent.BREAKDOWN: [
        r'\bby\b.*\b(department|category|type|region|product)\b',
        r'\bbreakdown\b', r'\bdistribution\b', r'\bsplit\b',
        r'\bper\b', r'\beach\b', r'\bgroup\b.*\bby\b'
    ],
    QueryIntent.PREDICTION: [
        r'\bpredict\b', r'\bforecast\b', r'\bestimate\b', r'\bproject\b',
        r'\bwill\b.*\bbe\b', r'\bnext\b.*\b(month|year|quarter)\b',
        r'\bfuture\b', r'\bexpect\b'
    ],
    QueryIntent.VISUALIZATION: [
        r'\bchart\b', r'\bgraph\b', r'\bvisualize\b', r'\bplot\b',
        r'\bshow\b.*\b(me|chart|graph)\b', r'\bdisplay\b.*\b(chart|graph)\b',
        r'\bbar\b.*\bchart\b', r'\bpie\b.*\bchart\b', r'\bline\b.*\bchart\b'
    ],
    QueryIntent.RELATIONSHIP: [
        r'\brelate\b', r'\bconnect\b', r'\blink\b', r'\bdepend\b',
        r'\brelationship\b', r'\bnetwork\b', r'\bwho\b.*\b(works|reports|manages)\b'
    ],
    QueryIntent.SUMMARY: [
        r'\bsummary\b', r'\boverview\b', r'\bsummarize\b', r'\bhighlight\b',
        r'\bkey\b.*\b(points|metrics|insights)\b', r'\btop\b.*\b\d+\b'
    ],
    QueryIntent.ANALYSIS: [
        r'\banalyze\b', r'\banalysis\b', r'\bexplain\b', r'\bwhy\b',
        r'\bunderstand\b', r'\binsight\b', r'\bpattern\b'
    ],
}

# Chart type detection patterns
CHART_PATTERNS = {
    'bar': [r'\bbar\b', r'\bcompar', r'\brank', r'\btop\b', r'\bby\b.*\b(department|category)\b'],
    'line': [r'\bline\b', r'\btrend\b', r'\bover time\b', r'\bgrowth\b', r'\bmonthly\b', r'\byearly\b'],
    'pie': [r'\bpie\b', r'\bdistribution\b', r'\bbreakdown\b', r'\bshare\b', r'\bproportion\b'],
    'scatter': [r'\bscatter\b', r'\bcorrelation\b', r'\brelation\b.*\bbetween\b'],
    'heatmap': [r'\bheatmap\b', r'\bmatrix\b', r'\bdensity\b'],
}


def detect_intent(query: str) -> QueryIntent:
    """Detect the primary intent of a query"""
    query_lower = query.lower()
    
    # Check each intent pattern
    for intent, patterns in INTENT_PATTERNS.items():
        for pattern in patterns:
            if re.search(pattern, query_lower):
                return intent
    
    # Default to lookup for simple queries
    if len(query.split()) <= 5:
        return QueryIntent.LOOKUP
    
    return QueryIntent.ANALYSIS


def detect_response_type(query: str, intent: QueryIntent) -> ResponseType:
    """Determine what type of response the user wants"""
    query_lower = query.lower()
    
    # Explicit visualization request
    if intent == QueryIntent.VISUALIZATION:
        return ResponseType.CHART
    
    # Relationship queries β†’ graph
    if intent == QueryIntent.RELATIONSHIP:
        return ResponseType.GRAPH
    
    # Trend/comparison/breakdown β†’ chart
    if intent in [QueryIntent.TREND, QueryIntent.COMPARISON, QueryIntent.BREAKDOWN]:
        return ResponseType.CHART
    
    # Check for explicit table request
    if re.search(r'\btable\b|\blist\b.*\ball\b', query_lower):
        return ResponseType.TABLE
    
    # Check for "show me" pattern
    if re.search(r'\bshow\b.*\b(chart|graph|visual)\b', query_lower):
        return ResponseType.CHART
    
    # Summary/analysis with data β†’ mixed
    if intent in [QueryIntent.SUMMARY, QueryIntent.ANALYSIS]:
        return ResponseType.MIXED
    
    return ResponseType.TEXT


def detect_chart_type(query: str, intent: QueryIntent) -> Optional[str]:
    """Detect which chart type is most appropriate"""
    query_lower = query.lower()
    
    for chart_type, patterns in CHART_PATTERNS.items():
        for pattern in patterns:
            if re.search(pattern, query_lower):
                return chart_type
    
    # Default chart types based on intent
    intent_chart_map = {
        QueryIntent.TREND: 'line',
        QueryIntent.COMPARISON: 'bar',
        QueryIntent.BREAKDOWN: 'pie',
        QueryIntent.PREDICTION: 'line',
    }
    
    return intent_chart_map.get(intent)


def extract_entities(query: str) -> List[str]:
    """Extract entity names from query (departments, products, etc.)"""
    entities = []
    
    # Look for quoted terms
    quoted = re.findall(r'"([^"]+)"', query)
    entities.extend(quoted)
    
    # Look for capitalized words (likely proper nouns)
    words = query.split()
    for i, word in enumerate(words):
        # Skip first word and common words
        if i > 0 and word[0].isupper() and word.lower() not in ['the', 'a', 'an', 'and', 'or']:
            entities.append(word)
    
    return list(set(entities))


def extract_metrics(query: str) -> List[str]:
    """Extract metric/measure names from query"""
    metrics = []
    
    metric_patterns = [
        r'\b(salary|revenue|sales|cost|profit|expense|budget)\b',
        r'\b(count|total|average|sum|max|min|mean)\b',
        r'\b(performance|rating|score|percentage|rate)\b',
    ]
    
    query_lower = query.lower()
    for pattern in metric_patterns:
        matches = re.findall(pattern, query_lower)
        metrics.extend(matches)
    
    return list(set(metrics))


def extract_time_reference(query: str) -> Optional[str]:
    """Extract time reference from query"""
    query_lower = query.lower()
    
    time_patterns = [
        (r'last\s+(\d+)\s+(day|week|month|year)s?', 'relative'),
        (r'this\s+(week|month|quarter|year)', 'current'),
        (r'(january|february|march|april|may|june|july|august|september|october|november|december)', 'month'),
        (r'(\d{4})', 'year'),
        (r'(q[1-4]|quarter\s*\d)', 'quarter'),
    ]
    
    for pattern, time_type in time_patterns:
        match = re.search(pattern, query_lower)
        if match:
            return f"{time_type}:{match.group(0)}"
    
    return None


def suggest_mode(intent: QueryIntent, response_type: ResponseType, has_image: bool = False) -> str:
    """Suggest the best mode for this query"""
    
    # Image attached β†’ Vision mode
    if has_image:
        return "vision"
    
    # Prediction intent β†’ Prediction mode
    if intent == QueryIntent.PREDICTION:
        return "prediction"
    
    # Relationship/network β†’ GraphRAG
    if intent == QueryIntent.RELATIONSHIP or response_type == ResponseType.GRAPH:
        return "graphrag"
    
    # Complex analysis β†’ Hybrid
    if intent in [QueryIntent.ANALYSIS, QueryIntent.COMPARISON] and response_type == ResponseType.MIXED:
        return "hybrid"
    
    # Default to RAG
    return "rag"


def suggest_model(intent: QueryIntent, query: str, has_image: bool = False) -> str:
    """Suggest the best model for this query"""
    
    # Vision required
    if has_image:
        return "gpt4v"
    
    # Complex reasoning β†’ Claude
    if intent in [QueryIntent.ANALYSIS, QueryIntent.PREDICTION]:
        if len(query.split()) > 15:  # Long, complex query
            return "claude-3.5"
    
    # Default to DeepSeek (fast, accurate)
    return "deepseek"


def analyze_query(query: str, has_image: bool = False) -> QueryAnalysis:
    """
    Full query analysis - determines intent, response type, mode, and model.
    This is the main entry point.
    """
    intent = detect_intent(query)
    response_type = detect_response_type(query, intent)
    chart_type = detect_chart_type(query, intent) if response_type == ResponseType.CHART else None
    
    return QueryAnalysis(
        intent=intent,
        response_type=response_type,
        suggested_mode=suggest_mode(intent, response_type, has_image),
        suggested_model=suggest_model(intent, query, has_image),
        chart_type=chart_type,
        entities=extract_entities(query),
        metrics=extract_metrics(query),
        time_reference=extract_time_reference(query),
        confidence=0.8 if intent != QueryIntent.LOOKUP else 0.9
    )


def get_routing_decision(query: str, has_image: bool = False) -> Dict[str, Any]:
    """
    Get routing decision for the query.
    Returns dict with mode, model, response_type for use by send_message.
    """
    analysis = analyze_query(query, has_image)
    
    return {
        "mode": analysis.suggested_mode,
        "model": analysis.suggested_model,
        "response_type": analysis.response_type.value,
        "intent": analysis.intent.value,
        "chart_type": analysis.chart_type,
        "entities": analysis.entities,
        "metrics": analysis.metrics,
        "generate_chart": analysis.response_type in [ResponseType.CHART, ResponseType.MIXED],
    }


# Test
if __name__ == "__main__":
    test_queries = [
        "What is total salary?",
        "Compare Engineering and Sales salary",
        "Show me revenue trend over the last 6 months",
        "Create a pie chart of department distribution",
        "Who reports to the Engineering manager?",
        "Predict next quarter's revenue",
        "Give me a summary of key metrics",
    ]
    
    print("Query Intent Detection Test")
    print("=" * 60)
    
    for q in test_queries:
        result = get_routing_decision(q)
        print(f"\nQuery: {q}")
        print(f"  Intent: {result['intent']}")
        print(f"  Response: {result['response_type']}")
        print(f"  Mode: {result['mode']}")
        print(f"  Model: {result['model']}")
        print(f"  Chart: {result['chart_type']}")