File size: 10,968 Bytes
9f9844d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10613ff
 
 
 
35b0fd8
6134c15
10613ff
 
9f9844d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557b97f
9f9844d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
332
333
334
335
336
337
import streamlit as st
import os
import pandas as pd
from typing import Literal, TypedDict
from sqlalchemy import create_engine, inspect
import json
from transformers import AutoTokenizer
from utils import pprint
import time
import re

from openai import OpenAI
import anthropic
from clients.openRouter import OpenRouter

# Load environment variables
from dotenv import load_dotenv
load_dotenv()

# Set up page configuration
st.set_page_config(
    page_title="SQL Query Assistant",
    page_icon="💾",
    layout="centered",
    initial_sidebar_state="collapsed"
)

ModelType = Literal["GPT_4o", "GPT_o1", "CLAUDE", "LLAMA", "DEEPSEEK", "DEEPSEEK_R1", "DEEPSEEK_R1_DISTILL"]
ModelConfig = TypedDict("ModelConfig", {
    "client": OpenAI | anthropic.Anthropic,
    "model": str,
    "max_context": int,
    "tokenizer": AutoTokenizer
})

MODEL_CONFIG: dict[ModelType, ModelConfig] = {
    "CLAUDE": {
        "client": anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY")),
        "model": "claude-3-5-haiku-20241022",
        # "model": "claude-3-5-sonnet-20241022",
        # "model": "claude-3-5-sonnet-20240620",
        "max_context": 40000,
        "tokenizer": AutoTokenizer.from_pretrained("Xenova/claude-tokenizer")
    },
    "GPT_4o": {
        "client": OpenAI(api_key=os.environ.get("OPENAI_API_KEY")),
        "model": "gpt-4o",
        "max_context": 15000,
        "tokenizer": AutoTokenizer.from_pretrained("Xenova/gpt-4o")
    },
    # "GPT_o1": {
    #     "client": OpenAI(api_key=os.environ.get("OPENAI_API_KEY")),
    #     "model": "o1-preview",
    #     "max_context": 15000,
    #     "tokenizer": AutoTokenizer.from_pretrained("Xenova/gpt-4o")
    # },
    "DEEPSEEK": {
        "client": OpenRouter(
            api_key=os.environ.get("OPENROUTER_API_KEY"),
        ),
        "model": "deepseek/deepseek-chat",
        "max_context": 30000,
        "tokenizer": AutoTokenizer.from_pretrained("Xenova/gpt-4o")
    },
    "DEEPSEEK_R1": {
        "client": OpenRouter(
            api_key=os.environ.get("OPENROUTER_API_KEY"),
        ),
        "model": "deepseek/deepseek-r1",
        "max_context": 30000,
        "tokenizer": AutoTokenizer.from_pretrained("Xenova/gpt-4o")
    },
}


def get_model_type():
    """
    Get the model type from Streamlit sidebar with model names
    """
    # Get the available model types from the MODEL_CONFIG keys
    available_models = list(MODEL_CONFIG.keys())
    
    # Create a list of display labels with just the model names
    model_display_labels = [
        MODEL_CONFIG[model_type]['model']
        for model_type in available_models
    ]
    
    # Add a sidebar selection for model name
    selected_model_name = st.sidebar.selectbox(
        "Select AI Model", 
        model_display_labels, 
        index=0
    )
    
    # Find the corresponding model type for the selected model name
    selected_model_type = next(
        model_type for model_type in available_models 
        if MODEL_CONFIG[model_type]['model'] == selected_model_name
    )
    
    return selected_model_type


# In the main application flow, replace the previous modelType assignment
modelType = get_model_type()

client = MODEL_CONFIG[modelType]["client"]
MODEL = MODEL_CONFIG[modelType]["model"]
TOOLS_MODEL = MODEL_CONFIG[modelType].get("tools_model") or MODEL
MAX_CONTEXT = MODEL_CONFIG[modelType]["max_context"]
tokenizer = MODEL_CONFIG[modelType]["tokenizer"]

isClaudeModel = modelType == "CLAUDE"
isDeepSeekModel = modelType.startswith("DEEPSEEK")


def __countTokens(text):
    text = str(text)
    tokens = tokenizer.encode(text, add_special_tokens=False)
    return len(tokens)


# Initialize session state variables
if "ipAddress" not in st.session_state:
    st.session_state.ipAddress = st.context.headers.get("x-forwarded-for")
if "connection_string" not in st.session_state:
    st.session_state.connection_string = None
if "selected_table" not in st.session_state:
    st.session_state.selected_table = None
if "table_schema" not in st.session_state:
    st.session_state.table_schema = None
if "sample_data" not in st.session_state:
    st.session_state.sample_data = None
if "engine" not in st.session_state:
    st.session_state.engine = None


def connect_to_db(connection_string):
    try:
        engine = create_engine(connection_string)
        # Test the connection
        with engine.connect():
            pass
        st.session_state.engine = engine
        return True
    except Exception as e:
        st.error(f"Failed to connect to database: {str(e)}")
        return False


def get_table_schema(table_name):
    if not st.session_state.engine:
        return None
    
    inspector = inspect(st.session_state.engine)
    columns = inspector.get_columns(table_name)
    return {col['name']: str(col['type']) for col in columns}


def get_sample_data(table_name):
    if not st.session_state.engine:
        return None
    
    query = f"SELECT * FROM {table_name} ORDER BY 1 DESC LIMIT 3"
    try:
        with st.session_state.engine.connect() as conn:
            df = pd.read_sql(query, conn)
            return df
    except Exception as e:
        st.error(f"Error fetching sample data: {str(e)}")
        return None


def clean_sql_response(response: str) -> str:
    """Extract clean SQL query from a potentially formatted response."""
    # If response contains SQL code block, extract it
    sql_block_match = re.search(r'```sql\n(.*?)\n```', response, re.DOTALL)
    if sql_block_match:
        return sql_block_match.group(1).strip()
    return response.strip()


def execute_query(query):
    if not st.session_state.engine:
        return None
    
    try:
        start_time = time.time()
        with st.spinner("Executing SQL query..."):
            with st.session_state.engine.connect() as conn:
                df = pd.read_sql(query, conn)
            execution_time = time.time() - start_time
            pprint(f"[Query Execution] Latency: {execution_time:.2f}s")
        return df
    except Exception as e:
        st.error(f"Error executing query: {str(e)}")
        return None


def generate_sql_query(user_query):
    prompt = f"""You are a SQL expert. Generate a valid PostgreSQL query based on the following context and user query.

Table Name: {st.session_state.selected_table}

Table Schema:
{json.dumps(st.session_state.table_schema, indent=2)}

Sample Data:
{st.session_state.sample_data.to_markdown(index=False)}

Important:
1. Only return the SQL query, nothing else
2. The query should be valid PostgreSQL syntax
3. Do not include any explanations or comments
4. Make sure to handle NULL values appropriately
5. Use the table name '{st.session_state.selected_table}' in your query

User Query: {user_query}
"""
    
    prompt_tokens = __countTokens(prompt)
    pprint(f"\n[{MODEL}] Prompt tokens for SQL generation: {prompt_tokens}")

    # Debug prompt in a Streamlit expander for better organization
    with st.expander("Debug: Prompt Generation"):
        st.write(f"\nUser Query: {user_query}")
        st.write("\nFull Prompt:")
        st.code(prompt, language="text")

    start_time = time.time()
    with st.spinner(f"Generating SQL query using {MODEL}..."):
        if isClaudeModel:
            response = client.messages.create(
                model=MODEL,
                max_tokens=1000,
                messages=[
                    {"role": "user", "content": prompt},
                ]
            )
            raw_response = response.content[0].text
        else:
            response = client.chat.completions.create(
                model=MODEL,
                messages=[
                    {"role": "user", "content": prompt},
                ]
            )
            raw_response = response.choices[0].message.content
        
        generation_time = time.time() - start_time
        pprint(f"[{MODEL}] Query Generation Latency: {generation_time:.2f}s")

    return clean_sql_response(raw_response)


# UI Components
st.title("SQL Query Assistant")

# Database Connection Section
st.header("1. Database Connection")
connection_string = st.text_input(
    "Enter PostgreSQL Connection String",
    value=st.session_state.connection_string if st.session_state.connection_string else "",
    type="password"
)

if connection_string and connection_string != st.session_state.connection_string:
    if connect_to_db(connection_string):
        st.session_state.connection_string = connection_string
        st.success("Successfully connected to database!")

# Table Selection Section
if st.session_state.connection_string:
    st.header("2. Table Selection")
    inspector = inspect(st.session_state.engine)
    tables = inspector.get_table_names()
    
    # Set default index to 'lsq_leads' if present, otherwise 0
    default_index = tables.index('lsq_leads') if 'lsq_leads' in tables else 0
    selected_table = st.selectbox("Select a table", tables, index=default_index)
    
    # Create containers for schema and data
    schema_container = st.container()
    data_container = st.container()
    
    # Always load table data if we have a selected table
    if selected_table:
        # Update session state
        if selected_table != st.session_state.selected_table:
            st.session_state.selected_table = selected_table
        
        # Always fetch schema and sample data
        st.session_state.table_schema = get_table_schema(selected_table)
        st.session_state.sample_data = get_sample_data(selected_table)
        
        # Always display schema and sample data if available
        with schema_container:
            if st.session_state.table_schema:
                st.subheader("Table Schema")
                # Force immediate rendering with an empty element
                st.empty()
                st.json(st.session_state.table_schema)
        
        with data_container:
            if st.session_state.sample_data is not None:
                st.subheader("Sample Data (Last 3 rows)")
                # Force immediate rendering with an empty element
                st.empty()
                st.dataframe(
                    st.session_state.sample_data,
                    use_container_width=True,
                    hide_index=True
                )

# Query Input Section
if st.session_state.selected_table:
    st.header("3. Query Input")
    user_query = st.text_area("Enter your query in plain English")
    
    if st.button("Generate and Execute Query"):
        if user_query:
            # Generate SQL query
            sql_query = generate_sql_query(user_query)
            
            # Display the generated query
            st.subheader("Generated SQL Query")
            st.code(sql_query, language="sql")
            
            # Execute the query
            results = execute_query(sql_query)
            if results is not None:
                st.subheader("Query Results")
                st.dataframe(results)