amalsp commited on
Commit
c4b2bae
·
verified ·
1 Parent(s): e5ba825

Add Groq AI integration for real NL2SQL functionality

Browse files

- Integrated Groq AI (Mixtral-8x7b-32768) for SQL generation
- Added generate_sql_with_groq() function
- Added explain_sql_with_groq() for query explanations
- Replaced hardcoded demo responses with real AI-generated SQL
- Ready to process natural language questions dynamically
- Requires GROQ_API_KEY to be set in HF Spaces secrets

Files changed (1) hide show
  1. main.py +126 -15
main.py CHANGED
@@ -1,5 +1,5 @@
1
- """FastAPI Backend for OpenNL2SQL - Simplified for HuggingFace Spaces
2
- Author: Amal SP
3
  Created: December 2025
4
  """
5
 
@@ -9,6 +9,8 @@ from pydantic import BaseModel
9
  from typing import Optional, List, Dict, Any
10
  import os
11
  import logging
 
 
12
 
13
  # Configure logging
14
  logging.basicConfig(
@@ -33,6 +35,15 @@ app.add_middleware(
33
  allow_headers=["*"],
34
  )
35
 
 
 
 
 
 
 
 
 
 
36
  # Request/Response Models
37
  class QueryRequest(BaseModel):
38
  question: str
@@ -47,6 +58,70 @@ class QueryResponse(BaseModel):
47
  error: Optional[str] = None
48
  session_id: str
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  @app.get("/")
51
  async def root():
52
  """Health check endpoint"""
@@ -54,7 +129,8 @@ async def root():
54
  "status": "healthy",
55
  "service": "OpenNL2SQL API",
56
  "version": "1.0.0",
57
- "message": "FastAPI backend is running on Hugging Face Spaces!"
 
58
  }
59
 
60
  @app.get("/health")
@@ -62,25 +138,60 @@ async def health_check():
62
  """Detailed health check"""
63
  return {
64
  "status": "healthy",
65
- "groq_api_configured": bool(os.getenv("GROQ_API_KEY")),
66
  "service": "OpenNL2SQL API"
67
  }
68
 
69
  @app.post("/query", response_model=QueryResponse)
70
  async def process_query(request: QueryRequest):
71
- """Process natural language query"""
72
- # For now, return a placeholder response
73
- # This will be integrated with GROQ API and full backend logic
74
  session_id = request.session_id or "demo-session"
75
 
76
- return QueryResponse(
77
- success=True,
78
- sql="SELECT * FROM demo_table LIMIT 10;",
79
- results=[{"status": "Backend is running! Full implementation coming soon."}],
80
- sql_explanation="This is a demo response. Full NL2SQL functionality will be integrated.",
81
- results_explanation="The backend is successfully deployed on Hugging Face Spaces.",
82
- session_id=session_id
83
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  if __name__ == "__main__":
86
  import uvicorn
 
1
+ """FastAPI Backend for OpenNL2SQL with Groq AI Integration
2
+ Author: Amal SP
3
  Created: December 2025
4
  """
5
 
 
9
  from typing import Optional, List, Dict, Any
10
  import os
11
  import logging
12
+ from groq import Groq
13
+ import json
14
 
15
  # Configure logging
16
  logging.basicConfig(
 
35
  allow_headers=["*"],
36
  )
37
 
38
+ # Initialize Groq client
39
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
40
+ if GROQ_API_KEY:
41
+ groq_client = Groq(api_key=GROQ_API_KEY)
42
+ logger.info("Groq client initialized successfully")
43
+ else:
44
+ groq_client = None
45
+ logger.warning("GROQ_API_KEY not found - running in demo mode")
46
+
47
  # Request/Response Models
48
  class QueryRequest(BaseModel):
49
  question: str
 
58
  error: Optional[str] = None
59
  session_id: str
60
 
61
+ def generate_sql_with_groq(question: str) -> tuple:
62
+ """Generate SQL using Groq AI"""
63
+ try:
64
+ # Sample database schema
65
+ schema = """
66
+ Database Schema:
67
+ - customers (id, name, email, created_at)
68
+ - orders (id, customer_id, total, status, created_at)
69
+ - products (id, name, price, category)
70
+ - order_items (id, order_id, product_id, quantity, price)
71
+ """
72
+
73
+ prompt = f"""{schema}
74
+
75
+ Convert this natural language question to a SQL query:
76
+ Question: {question}
77
+
78
+ Generate ONLY a valid SELECT SQL query. No explanations.
79
+ SQL Query:"""
80
+
81
+ response = groq_client.chat.completions.create(
82
+ model="mixtral-8x7b-32768",
83
+ messages=[
84
+ {"role": "system", "content": "You are a SQL expert. Generate only valid SQL SELECT queries without any explanations or markdown formatting."},
85
+ {"role": "user", "content": prompt}
86
+ ],
87
+ temperature=0.2,
88
+ max_tokens=500
89
+ )
90
+
91
+ sql = response.choices[0].message.content.strip()
92
+ # Clean up the SQL
93
+ sql = sql.replace("```sql", "").replace("```", "").strip()
94
+
95
+ return sql, None
96
+ except Exception as e:
97
+ logger.error(f"Error generating SQL: {str(e)}")
98
+ return None, str(e)
99
+
100
+ def explain_sql_with_groq(sql: str, question: str) -> str:
101
+ """Generate explanation for SQL query"""
102
+ try:
103
+ prompt = f"""Explain this SQL query in simple terms:
104
+
105
+ Original Question: {question}
106
+ SQL Query: {sql}
107
+
108
+ Provide a brief, clear explanation:"""
109
+
110
+ response = groq_client.chat.completions.create(
111
+ model="mixtral-8x7b-32768",
112
+ messages=[
113
+ {"role": "system", "content": "You are a helpful assistant that explains SQL queries in simple terms."},
114
+ {"role": "user", "content": prompt}
115
+ ],
116
+ temperature=0.3,
117
+ max_tokens=300
118
+ )
119
+
120
+ return response.choices[0].message.content.strip()
121
+ except Exception as e:
122
+ logger.error(f"Error explaining SQL: {str(e)}")
123
+ return "SQL query generated successfully."
124
+
125
  @app.get("/")
126
  async def root():
127
  """Health check endpoint"""
 
129
  "status": "healthy",
130
  "service": "OpenNL2SQL API",
131
  "version": "1.0.0",
132
+ "message": "FastAPI backend with Groq AI integration running on Hugging Face Spaces!",
133
+ "groq_enabled": groq_client is not None
134
  }
135
 
136
  @app.get("/health")
 
138
  """Detailed health check"""
139
  return {
140
  "status": "healthy",
141
+ "groq_api_configured": groq_client is not None,
142
  "service": "OpenNL2SQL API"
143
  }
144
 
145
  @app.post("/query", response_model=QueryResponse)
146
  async def process_query(request: QueryRequest):
147
+ """Process natural language query with Groq AI"""
 
 
148
  session_id = request.session_id or "demo-session"
149
 
150
+ # Check if Groq is available
151
+ if not groq_client:
152
+ return QueryResponse(
153
+ success=False,
154
+ error="GROQ_API_KEY not configured. Please add it in HF Spaces Settings > Variables.",
155
+ session_id=session_id
156
+ )
157
+
158
+ try:
159
+ # Generate SQL using Groq
160
+ sql, error = generate_sql_with_groq(request.question)
161
+
162
+ if error:
163
+ return QueryResponse(
164
+ success=False,
165
+ error=f"Failed to generate SQL: {error}",
166
+ session_id=session_id
167
+ )
168
+
169
+ # Generate explanation
170
+ explanation = explain_sql_with_groq(sql, request.question)
171
+
172
+ # For demo: return mock results
173
+ # In production, you'd execute the SQL against a real database
174
+ results = [
175
+ {"info": "SQL generated successfully! In production, this would execute against your database."},
176
+ {"note": "Connect your database to see real query results."}
177
+ ]
178
+
179
+ return QueryResponse(
180
+ success=True,
181
+ sql=sql,
182
+ results=results,
183
+ sql_explanation=explanation,
184
+ results_explanation=f"Generated SQL query for: '{request.question}'. Ready to execute against your database.",
185
+ session_id=session_id
186
+ )
187
+
188
+ except Exception as e:
189
+ logger.error(f"Error processing query: {str(e)}")
190
+ return QueryResponse(
191
+ success=False,
192
+ error=f"Error: {str(e)}",
193
+ session_id=session_id
194
+ )
195
 
196
  if __name__ == "__main__":
197
  import uvicorn