SVashishta1 commited on
Commit
0588d91
·
1 Parent(s): e3d98a2
Files changed (1) hide show
  1. app.py +46 -5
app.py CHANGED
@@ -65,10 +65,9 @@ Important guidelines:
65
  4. For percentiles, use window functions or approximate methods
66
  5. Keep queries efficient and focused on answering the specific question
67
  6. Always use 'data_tab' as the table name
 
68
 
69
  Question: {question}
70
-
71
- SQL Query:
72
  """)
73
 
74
  # Define the prompt for interpreting the SQL query result
@@ -104,12 +103,51 @@ Important guidelines for SQLite syntax:
104
 
105
  5. Always use 'data_tab' as the table name
106
 
 
 
107
  Question: {question}
108
  Visualization type: {viz_type}
109
-
110
- SQL Query:
111
  """)
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  def process_text_query(query, history):
114
  """Process a text query and update chat history"""
115
  if not query:
@@ -137,7 +175,10 @@ def process_text_query(query, history):
137
 
138
  # Generate SQL query using LLM
139
  ai_msg = query_prompt | llm
140
- sql_query = ai_msg.invoke({"question": question_with_context}).content.strip()
 
 
 
141
 
142
  print(f"Generated SQL Query: {sql_query}")
143
 
 
65
  4. For percentiles, use window functions or approximate methods
66
  5. Keep queries efficient and focused on answering the specific question
67
  6. Always use 'data_tab' as the table name
68
+ 7. IMPORTANT: Return ONLY the SQL query without any markdown formatting, explanations, or code blocks
69
 
70
  Question: {question}
 
 
71
  """)
72
 
73
  # Define the prompt for interpreting the SQL query result
 
103
 
104
  5. Always use 'data_tab' as the table name
105
 
106
+ 6. IMPORTANT: Return ONLY the SQL query without any markdown formatting, explanations, or code blocks
107
+
108
  Question: {question}
109
  Visualization type: {viz_type}
 
 
110
  """)
111
 
112
+ # Add this helper function to clean SQL queries
113
+ def clean_sql_query(query_text):
114
+ """Clean SQL query text by removing markdown formatting and comments"""
115
+ # Remove markdown code blocks
116
+ if "```sql" in query_text.lower():
117
+ # Extract content between code blocks
118
+ pattern = r"```(?:sql)?(.*?)```"
119
+ matches = re.findall(pattern, query_text, re.DOTALL)
120
+ if matches:
121
+ query_text = matches[0].strip()
122
+
123
+ # Remove any "Here is the SQL query" text that might precede the query
124
+ prefixes = [
125
+ "here is the sql query",
126
+ "here is the sqlite query",
127
+ "here is a query",
128
+ "here's the sql query",
129
+ "the sql query is"
130
+ ]
131
+
132
+ for prefix in prefixes:
133
+ if query_text.lower().startswith(prefix):
134
+ # Find the first occurrence of "SELECT", "WITH", etc.
135
+ sql_keywords = ["select", "with", "create", "insert", "update", "delete"]
136
+ positions = [query_text.lower().find(keyword) for keyword in sql_keywords]
137
+ positions = [pos for pos in positions if pos != -1]
138
+
139
+ if positions:
140
+ start_pos = min(positions)
141
+ query_text = query_text[start_pos:]
142
+
143
+ # Remove SQL comments
144
+ query_text = re.sub(r'--.*?(\n|$)', ' ', query_text)
145
+
146
+ # Remove trailing semicolon if present (optional)
147
+ query_text = query_text.strip().rstrip(';')
148
+
149
+ return query_text
150
+
151
  def process_text_query(query, history):
152
  """Process a text query and update chat history"""
153
  if not query:
 
175
 
176
  # Generate SQL query using LLM
177
  ai_msg = query_prompt | llm
178
+ raw_sql_query = ai_msg.invoke({"question": question_with_context}).content.strip()
179
+
180
+ # Clean the SQL query
181
+ sql_query = clean_sql_query(raw_sql_query)
182
 
183
  print(f"Generated SQL Query: {sql_query}")
184