-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
304 lines (252 loc) · 9.83 KB
/
Copy pathapi.py
File metadata and controls
304 lines (252 loc) · 9.83 KB
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
"""
FastAPI Endpoint for CloudCost Intelligence Text2SQL Engine
RESTful API for natural language querying of cloud cost data
Enhanced with dynamic clarification, SQL validation, and semantic metadata
"""
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Optional, Any, List, Dict
import uvicorn
from text2sql_engine import Text2SQLEngine
app = FastAPI(
title="CloudCost Intelligence API",
description="Natural language to SQL API for cloud cost analytics with intelligent clarification",
version="2.0.0"
)
# Enable CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Initialize engine
engine = None
class QueryRequest(BaseModel):
"""Natural language query request"""
question: str
explain: Optional[bool] = True
provider_context: Optional[str] = None # 'aws', 'azure', or 'both'
class ClarificationOption(BaseModel):
"""Clarification option"""
value: str
label: str
class ClarificationResponse(BaseModel):
"""Response when clarification is needed"""
needs_clarification: bool
question: Optional[str] = None
options: List[ClarificationOption] = []
missing_context: List[str] = []
original_query: str
class QueryResponse(BaseModel):
"""Query response with SQL and results"""
natural_query: str
sql_query: str
method: str
row_count: int
results: Optional[Any] = None
explanation: Optional[str] = None
warning: Optional[str] = None
used_tables: List[str] = []
query_metadata: Optional[Dict] = None
class ClarificationRequest(BaseModel):
"""Request to apply clarification context"""
original_query: str
context_key: str
context_value: str
@app.on_event("startup")
async def startup_event():
"""Initialize Text2SQL engine on startup"""
global engine
engine = Text2SQLEngine()
print("✅ Text2SQL Engine initialized")
@app.on_event("shutdown")
async def shutdown_event():
"""Close database connection on shutdown"""
global engine
if engine:
engine.close()
print("👋 Text2SQL Engine closed")
@app.get("/")
async def root():
"""API health check"""
return {
"message": "CloudCost Intelligence API",
"status": "operational",
"version": "1.0.0",
"endpoints": {
"POST /query": "Execute natural language query",
"GET /stats": "Get database statistics",
"GET /examples": "Get example queries"
}
}
@app.post("/query", response_model=QueryResponse)
async def execute_query(request: QueryRequest):
"""
Execute a natural language query against cloud cost data
Example:
```
POST /query
{
"question": "What is the total AWS cost?",
"explain": true
}
```
"""
try:
if not request.question or not request.question.strip():
raise HTTPException(status_code=400, detail="Question cannot be empty")
# Execute query
result = engine.execute_natural_query(request.question)
# Generate natural language explanation if requested
explanation = None
if request.explain and result['results'] is not None:
explanation = generate_explanation(
request.question,
result['sql_query'],
result['results'],
result['row_count']
)
# Convert DataFrame to dict for JSON serialization
results_data = None
if result['results'] is not None:
results_data = result['results'].to_dict('records')
return QueryResponse(
natural_query=result['natural_query'],
sql_query=result['sql_query'],
method=result['method'],
row_count=result['row_count'],
results=results_data,
explanation=explanation
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/stats")
async def get_database_stats():
"""Get database statistics for AWS and Azure costs"""
try:
# AWS stats
aws_result = engine.execute_natural_query("SELECT COUNT(*) as count, SUM(billedcost) as total FROM aws_cost")
aws_stats = {
"records": int(aws_result['results'].iloc[0]['count']) if aws_result['results'] is not None else 0,
"total_cost": float(aws_result['results'].iloc[0]['total']) if aws_result['results'] is not None else 0.0
}
# Azure stats
azure_result = engine.execute_natural_query("SELECT COUNT(*) as count, SUM(billedcost) as total FROM azure_cost")
azure_stats = {
"records": int(azure_result['results'].iloc[0]['count']) if azure_result['results'] is not None else 0,
"total_cost": float(azure_result['results'].iloc[0]['total']) if azure_result['results'] is not None else 0.0
}
return {
"aws": aws_stats,
"azure": azure_stats,
"combined": {
"records": aws_stats["records"] + azure_stats["records"],
"total_cost": aws_stats["total_cost"] + azure_stats["total_cost"]
}
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/examples")
async def get_example_queries():
"""Get example queries to try"""
return {
"examples": [
{
"category": "Basic Aggregation",
"queries": [
"What is the total AWS cost?",
"What is the total Azure cost?",
"What is the average cost per service?"
]
},
{
"category": "Grouping & Analysis",
"queries": [
"Show me costs by service",
"What are the costs by region?",
"Show me Azure costs by service category"
]
},
{
"category": "Top N Queries",
"queries": [
"Show me top 5 services by cost",
"What are the top 10 most expensive regions?",
"Show me the top 3 accounts by spend"
]
},
{
"category": "Comparative Analysis",
"queries": [
"Compare AWS and Azure costs",
"Which provider has higher compute costs?",
"Show cost breakdown by provider"
]
}
]
}
def generate_explanation(question: str, sql: str, results, row_count: int) -> str:
"""
Generate a natural language explanation of query results
This function creates a conversational response explaining what the data shows.
In a production system, this could use an LLM to generate more sophisticated explanations.
"""
# Simple rule-based explanation generation
explanation_parts = []
# Start with acknowledgment
explanation_parts.append(f"Based on your question '{question}',")
# Analyze the query type
sql_lower = sql.lower()
if "count(*)" in sql_lower or "count(distinct" in sql_lower:
# Count query
if row_count > 0:
count_value = results.iloc[0].iloc[0]
explanation_parts.append(f"I found {count_value:,} matching records.")
else:
explanation_parts.append("I found no matching records.")
elif "sum(" in sql_lower:
# Aggregation query
if row_count > 0:
if "group by" in sql_lower:
explanation_parts.append(f"I found {row_count} groups with the following breakdown:")
else:
total_cost = results.iloc[0].iloc[0]
explanation_parts.append(f"the total comes to ${total_cost:,.2f}.")
else:
explanation_parts.append("no data was found matching your criteria.")
elif "avg(" in sql_lower:
# Average query
if row_count > 0:
avg_value = results.iloc[0].iloc[0]
explanation_parts.append(f"the average is ${avg_value:,.2f}.")
else:
explanation_parts.append("no data was found to calculate an average.")
elif "top" in question.lower() or "limit" in sql_lower:
# Top N query
if row_count > 0:
explanation_parts.append(f"here are the top {row_count} results sorted by cost.")
else:
explanation_parts.append("no results were found.")
else:
# General query
if row_count > 0:
explanation_parts.append(f"I found {row_count} matching records.")
else:
explanation_parts.append("no records matched your query.")
# Add data quality note if relevant
if row_count > 100:
explanation_parts.append(f"Note: Only showing first 100 rows out of {row_count} total.")
return " ".join(explanation_parts)
if __name__ == "__main__":
print("🚀 Starting CloudCost Intelligence API...")
print("📝 API Documentation: http://localhost:8000/docs")
print("🔍 Interactive API: http://localhost:8000/redoc")
print("\nExample usage:")
print(" curl -X POST http://localhost:8000/query \\")
print(' -H "Content-Type: application/json" \\')
print(' -d \'{"question": "What is the total AWS cost?"}\'')
print()
uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info")