Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -123,36 +123,55 @@ def process_nl_query(api_key, natural_query):
|
|
| 123 |
output_text += "### Step 1: Understanding User Intent\n"
|
| 124 |
output_text += f"**User Query:** {natural_query}\n\n"
|
| 125 |
|
| 126 |
-
# Call Groq API for SQL generation
|
| 127 |
response = client.chat.completions.create(
|
| 128 |
-
model="
|
| 129 |
messages=[
|
| 130 |
{
|
| 131 |
"role": "system",
|
| 132 |
-
"content": "You are a SQL expert. Generate structured SQL queries from natural language descriptions with proper syntax validation and metadata.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
},
|
| 134 |
-
{"role": "user", "content": natural_query},
|
| 135 |
],
|
| 136 |
response_format={
|
| 137 |
"type": "json_object"
|
| 138 |
-
}
|
|
|
|
| 139 |
)
|
| 140 |
|
| 141 |
# Parse the response
|
| 142 |
response_content = response.choices[0].message.content
|
| 143 |
sql_data = json.loads(response_content)
|
| 144 |
|
| 145 |
-
# Try to map to our Pydantic model
|
| 146 |
try:
|
| 147 |
sql_query_gen = SQLQueryGeneration(**sql_data)
|
| 148 |
-
except:
|
| 149 |
# If response doesn't match exact schema, create it manually
|
| 150 |
sql_query_gen = SQLQueryGeneration(
|
| 151 |
-
query=sql_data.get('query', ''),
|
| 152 |
query_type=sql_data.get('query_type', 'SELECT'),
|
| 153 |
-
tables_used=sql_data.get('tables_used', []),
|
| 154 |
estimated_complexity=sql_data.get('estimated_complexity', 'medium'),
|
| 155 |
-
execution_notes=sql_data.get('execution_notes', []),
|
| 156 |
validation_status=ValidationStatus(
|
| 157 |
is_valid=sql_data.get('validation_status', {}).get('is_valid', True),
|
| 158 |
syntax_errors=sql_data.get('validation_status', {}).get('syntax_errors', [])
|
|
@@ -187,7 +206,10 @@ def process_nl_query(api_key, natural_query):
|
|
| 187 |
else:
|
| 188 |
output_text += "✅ **Query executed successfully!**\n\n"
|
| 189 |
output_text += "**📈 SQL Execution Result:**\n\n"
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
| 191 |
result_table = result_df
|
| 192 |
|
| 193 |
conn.close()
|
|
|
|
| 123 |
output_text += "### Step 1: Understanding User Intent\n"
|
| 124 |
output_text += f"**User Query:** {natural_query}\n\n"
|
| 125 |
|
| 126 |
+
# Call Groq API for SQL generation - FIXED: Added "JSON" to system message
|
| 127 |
response = client.chat.completions.create(
|
| 128 |
+
model="mixtral-8x7b-32768",
|
| 129 |
messages=[
|
| 130 |
{
|
| 131 |
"role": "system",
|
| 132 |
+
"content": """You are a SQL expert. Generate structured SQL queries from natural language descriptions with proper syntax validation and metadata.
|
| 133 |
+
|
| 134 |
+
Return your response in JSON format with the following structure:
|
| 135 |
+
{
|
| 136 |
+
"query": "SQL query string",
|
| 137 |
+
"query_type": "SELECT/INSERT/UPDATE/DELETE",
|
| 138 |
+
"tables_used": ["table1", "table2"],
|
| 139 |
+
"estimated_complexity": "low/medium/high",
|
| 140 |
+
"execution_notes": ["note1", "note2"],
|
| 141 |
+
"validation_status": {
|
| 142 |
+
"is_valid": true/false,
|
| 143 |
+
"syntax_errors": []
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
Use standard SQL syntax compatible with SQLite. Always use proper JOINs, WHERE clauses, and GROUP BY when needed.""",
|
| 148 |
+
},
|
| 149 |
+
{
|
| 150 |
+
"role": "user",
|
| 151 |
+
"content": f"Convert this natural language query to SQL and return as JSON: {natural_query}"
|
| 152 |
},
|
|
|
|
| 153 |
],
|
| 154 |
response_format={
|
| 155 |
"type": "json_object"
|
| 156 |
+
},
|
| 157 |
+
temperature=0.3
|
| 158 |
)
|
| 159 |
|
| 160 |
# Parse the response
|
| 161 |
response_content = response.choices[0].message.content
|
| 162 |
sql_data = json.loads(response_content)
|
| 163 |
|
| 164 |
+
# Try to map to our Pydantic model with better error handling
|
| 165 |
try:
|
| 166 |
sql_query_gen = SQLQueryGeneration(**sql_data)
|
| 167 |
+
except Exception as e:
|
| 168 |
# If response doesn't match exact schema, create it manually
|
| 169 |
sql_query_gen = SQLQueryGeneration(
|
| 170 |
+
query=sql_data.get('query', sql_data.get('sql_query', '')),
|
| 171 |
query_type=sql_data.get('query_type', 'SELECT'),
|
| 172 |
+
tables_used=sql_data.get('tables_used', sql_data.get('tables', [])),
|
| 173 |
estimated_complexity=sql_data.get('estimated_complexity', 'medium'),
|
| 174 |
+
execution_notes=sql_data.get('execution_notes', sql_data.get('notes', [])),
|
| 175 |
validation_status=ValidationStatus(
|
| 176 |
is_valid=sql_data.get('validation_status', {}).get('is_valid', True),
|
| 177 |
syntax_errors=sql_data.get('validation_status', {}).get('syntax_errors', [])
|
|
|
|
| 206 |
else:
|
| 207 |
output_text += "✅ **Query executed successfully!**\n\n"
|
| 208 |
output_text += "**📈 SQL Execution Result:**\n\n"
|
| 209 |
+
if len(result_df) > 0:
|
| 210 |
+
output_text += result_df.to_markdown(index=False)
|
| 211 |
+
else:
|
| 212 |
+
output_text += "*No results found matching the criteria*"
|
| 213 |
result_table = result_df
|
| 214 |
|
| 215 |
conn.close()
|