File size: 2,694 Bytes
6086e71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Conversational Fast-Path β€” Server-side detection for chat queries.

Handles greetings, thanks, identity questions, etc. without invoking the
full LLM pipeline. Provides instant (<1ms) responses for conversational queries.
"""

import re

# ── Pattern Registry ─────────────────────────────────────
_CHAT_PATTERNS = [
    re.compile(r'^(hi|hello|hey|howdy|sup|yo)\b[\s!.?]*$', re.I),
    re.compile(r'^how are you\??[\s!.]*$', re.I),
    re.compile(r'^thank(s| you)[\s!.,]*$', re.I),
    re.compile(r'^(bye|goodbye|see you|cya|later)[\s!.]*$', re.I),
    re.compile(r'^good (morning|afternoon|evening|night)[\s!.]*$', re.I),
    re.compile(r'^(ok|okay|alright|great|perfect|cool|nice|awesome)[\s!.]*$', re.I),
    re.compile(r'^(who are you|what are you|what is plainsql)\??$', re.I),
    re.compile(r'^(what can you do|help me|help)\??[\s!.]*$', re.I),
]

_CHAT_RESPONSES = {
    'greeting': "Hello! I'm PlainSQL β€” your AI-powered data assistant. Ask me anything about your database in plain English and I'll handle the SQL.",
    'thanks': "You're welcome! Let me know if you'd like to explore more insights from your data.",
    'farewell': "Goodbye! Your data will be here when you need it.",
    'identity': "I'm PlainSQL β€” an enterprise AI platform that converts natural language into SQL using a hybrid RAG + LLM pipeline.",
    'capabilities': "I can help you query your database in plain English, generate safe SQL, visualize results with charts, and provide AI insights. Try: 'Show me top 5 employees by salary'",
    'default': "I'm PlainSQL, your AI data assistant. Ask me questions about your database!",
}


def detect_conversational(query: str):
    """
    Returns a fast-path response string if the query is conversational, else None.

    This avoids a full LLM round-trip for simple greetings/thanks/identity questions.
    Average latency: <0.1ms.
    """
    q = query.strip()
    if re.match(r'^(hi|hello|hey|howdy|sup|yo)\b', q, re.I):
        return _CHAT_RESPONSES['greeting']
    if re.match(r'^how are you', q, re.I):
        return _CHAT_RESPONSES['greeting']
    if re.match(r'^thank(s| you)', q, re.I):
        return _CHAT_RESPONSES['thanks']
    if re.match(r'^(bye|goodbye|see you)', q, re.I):
        return _CHAT_RESPONSES['farewell']
    if re.match(r'^(who are you|what are you|what is plainsql)', q, re.I):
        return _CHAT_RESPONSES['identity']
    if re.match(r'^(what can you do|help)', q, re.I):
        return _CHAT_RESPONSES['capabilities']
    for pat in _CHAT_PATTERNS:
        if pat.match(q):
            return _CHAT_RESPONSES['default']
    return None