File size: 14,891 Bytes
92289e1 1e23bc6 f1c8a54 cd844b4 f1c8a54 3f570f2 1e23bc6 36054cd 1e23bc6 cd844b4 1e23bc6 f1c8a54 1e23bc6 cd844b4 1e23bc6 cd844b4 1e23bc6 cd844b4 1e23bc6 8d874f9 1e23bc6 cd844b4 1e23bc6 cd844b4 3f570f2 1e23bc6 d969889 36054cd 77c483a 1e23bc6 36054cd 22c44e6 36054cd 22c44e6 36054cd 22c44e6 1e23bc6 d969889 |
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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
import os
import sqlite3
import random
import json
import openai
from datetime import timedelta
from faker import Faker
from langgraph.graph import StateGraph, START
from typing import TypedDict, Optional
import gradio as gr
# Set your OpenAI API key from environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")
# ------------------ Initialize SQLite Database with Faker Data ------------------
def init_db():
fake = Faker()
conn = sqlite3.connect("complex_test_db.sqlite", timeout=20)
cursor = conn.cursor()
# Drop existing tables if they exist (for a clean setup)
cursor.executescript("""
DROP TABLE IF EXISTS order_items;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS payments;
DROP TABLE IF EXISTS shipment;
""")
# Create Customers Table
cursor.execute("""
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
phone TEXT NOT NULL,
address TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
""")
# Create Products Table
cursor.execute("""
CREATE TABLE products (
product_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
category TEXT NOT NULL,
price DECIMAL(10,2) NOT NULL,
stock_quantity INTEGER NOT NULL
);
""")
# Create Orders Table
cursor.execute("""
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_id INTEGER NOT NULL,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
total_amount DECIMAL(10,2) NOT NULL,
status TEXT CHECK(status IN ('Pending', 'Shipped', 'Delivered', 'Cancelled')) NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
""")
# Create Order Items Table (Many-to-Many relationship between Orders and Products)
cursor.execute("""
CREATE TABLE order_items (
order_item_id INTEGER PRIMARY KEY AUTOINCREMENT,
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL,
subtotal DECIMAL(10,2) NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
""")
# Create Payments Table (One-to-One relationship with Orders)
cursor.execute("""
CREATE TABLE payments (
payment_id INTEGER PRIMARY KEY AUTOINCREMENT,
order_id INTEGER UNIQUE NOT NULL,
payment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
amount DECIMAL(10,2) NOT NULL,
payment_method TEXT CHECK(payment_method IN ('Credit Card', 'Debit Card', 'PayPal', 'Bank Transfer')) NOT NULL,
status TEXT CHECK(status IN ('Success', 'Failed', 'Pending')) NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
""")
# Create Shipment Table (One-to-One relationship with Orders)
cursor.execute("""
CREATE TABLE shipment (
shipment_id INTEGER PRIMARY KEY AUTOINCREMENT,
order_id INTEGER UNIQUE NOT NULL,
shipment_date TIMESTAMP,
delivery_date TIMESTAMP,
carrier TEXT NOT NULL,
tracking_number TEXT UNIQUE,
status TEXT CHECK(status IN ('Processing', 'Shipped', 'Delivered')) NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
""")
conn.commit()
# Insert substantial data
NUM_CUSTOMERS = 1000
NUM_PRODUCTS = 500
NUM_ORDERS = 2000
NUM_ORDER_ITEMS = 5000
NUM_PAYMENTS = NUM_ORDERS
NUM_SHIPMENTS = int(NUM_ORDERS * 0.8) # 80% of orders are shipped
# Insert Customers (Ensure unique emails)
customers = []
unique_emails = set()
while len(customers) < NUM_CUSTOMERS:
name = fake.name()
email = fake.email()
phone = fake.phone_number()
address = fake.address().replace("\n", ", ")
if email not in unique_emails:
customers.append((name, email, phone, address))
unique_emails.add(email)
cursor.executemany("""
INSERT INTO customers (name, email, phone, address)
VALUES (?, ?, ?, ?);
""", customers)
# Insert Products
products = []
categories = ["Electronics", "Clothing", "Books", "Home Appliances", "Toys"]
for _ in range(NUM_PRODUCTS):
products.append((fake.word().capitalize(), random.choice(categories), round(random.uniform(5, 500), 2), random.randint(10, 500)))
cursor.executemany("""
INSERT INTO products (name, category, price, stock_quantity)
VALUES (?, ?, ?, ?);
""", products)
# Fetch inserted customer and product IDs
cursor.execute("SELECT customer_id FROM customers;")
customer_ids = [row[0] for row in cursor.fetchall()]
cursor.execute("SELECT product_id FROM products;")
product_ids = [row[0] for row in cursor.fetchall()]
# Insert Orders
orders = []
statuses = ["Pending", "Shipped", "Delivered", "Cancelled"]
for _ in range(NUM_ORDERS):
customer_id = random.choice(customer_ids)
total_amount = round(random.uniform(20, 2000), 2)
status = random.choice(statuses)
order_date = fake.date_time_between(start_date="-1y", end_date="now")
orders.append((customer_id, order_date, total_amount, status))
cursor.executemany("""
INSERT INTO orders (customer_id, order_date, total_amount, status)
VALUES (?, ?, ?, ?);
""", orders)
# Fetch inserted order IDs
cursor.execute("SELECT order_id FROM orders;")
order_ids = [row[0] for row in cursor.fetchall()]
# Insert Order Items
order_items = []
for _ in range(NUM_ORDER_ITEMS):
order_id = random.choice(order_ids)
product_id = random.choice(product_ids)
quantity = random.randint(1, 5)
subtotal = round(quantity * random.uniform(5, 500), 2)
order_items.append((order_id, product_id, quantity, subtotal))
cursor.executemany("""
INSERT INTO order_items (order_id, product_id, quantity, subtotal)
VALUES (?, ?, ?, ?);
""", order_items)
# Insert Payments
payment_methods = ["Credit Card", "Debit Card", "PayPal", "Bank Transfer"]
payment_statuses = ["Success", "Failed", "Pending"]
payments = []
for order_id in order_ids[:NUM_PAYMENTS]:
amount = round(random.uniform(20, 2000), 2)
payment_method = random.choice(payment_methods)
status = random.choice(payment_statuses)
payments.append((order_id, fake.date_time_between(start_date="-1y", end_date="now"), amount, payment_method, status))
cursor.executemany("""
INSERT INTO payments (order_id, payment_date, amount, payment_method, status)
VALUES (?, ?, ?, ?, ?);
""", payments)
# Insert Shipments (for 80% of orders)
carriers = ["FedEx", "UPS", "DHL", "USPS"]
shipments = []
for order_id in order_ids[:NUM_SHIPMENTS]:
shipment_date = fake.date_time_between(start_date="-1y", end_date="now")
delivery_date = shipment_date + timedelta(days=random.randint(1, 10))
tracking_number = fake.uuid4()
carrier = random.choice(carriers)
status = random.choice(["Processing", "Shipped", "Delivered"])
shipments.append((order_id, shipment_date, delivery_date, carrier, tracking_number, status))
cursor.executemany("""
INSERT INTO shipment (order_id, shipment_date, delivery_date, carrier, tracking_number, status)
VALUES (?, ?, ?, ?, ?, ?);
""", shipments)
conn.commit()
cursor.close()
conn.close()
print("✅ Database setup complete with complex relationships and substantial data!")
# Initialize the database on app startup
init_db()
# ------------------ Define State for the Workflow ------------------
class SQLExecutionState(TypedDict):
sql_query: str
structured_metadata: Optional[dict]
validation_result: Optional[dict]
optimized_sql: Optional[str]
execution_result: Optional[dict]
# Initialize the LangGraph Workflow
graph = StateGraph(state_schema=SQLExecutionState)
# ------------------ 1. Query Understanding Agent ------------------
def query_understanding_agent(state: SQLExecutionState) -> SQLExecutionState:
natural_language_query = state["sql_query"]
prompt = f"""
Convert the following natural language query into **structured SQL metadata** based on the database schema.
If you cannot generate a query that adheres strictly to the schema, return:
{{ "error": "Invalid query: Tables or columns do not match schema" }}
**Query:** "{natural_language_query}"
**Database Schema:**
- **orders** (order_id, customer_id, order_date, total_amount, status)
- **order_items** (order_item_id, order_id, product_id, quantity, subtotal)
- **products** (product_id, name, category, price, stock_quantity)
- **customers** (customer_id, name, email, phone, address, created_at)
- **payments** (payment_id, order_id, payment_date, amount, payment_method, status)
**Rules:**
- Use only the provided tables.
- Ensure correct column names.
- Return output strictly in JSON format.
- Group by relevant fields when necessary.
**Example Output Format:**
{json.dumps({
"operation": "SELECT",
"columns": ["customer_id", "SUM(total_amount) AS total_spent"],
"table": "orders",
"conditions": ["order_date BETWEEN '2024-01-01' AND '2024-12-31'"],
"group_by": ["customer_id"],
"order_by": ["total_spent DESC"],
"limit": 5
}, indent=4)}
**DO NOT return explanations. Only return valid JSON.**
"""
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
try:
metadata = json.loads(response["choices"][0]["message"]["content"])
return {"structured_metadata": metadata}
except json.JSONDecodeError:
return {"structured_metadata": {"error": "Invalid JSON response from OpenAI"}}
graph.add_node("Query Understanding", query_understanding_agent)
# ------------------ 2. Query Validation Agent ------------------
def query_validation_agent(state: SQLExecutionState) -> SQLExecutionState:
sql_metadata = state.get("structured_metadata", {})
if "error" in sql_metadata:
return {"validation_result": {"error": sql_metadata["error"]}}
query = sql_metadata.get("operation", "")
restricted_keywords = ["DROP", "DELETE", "TRUNCATE", "ALTER"]
if any(keyword in query.upper() for keyword in restricted_keywords):
return {"validation_result": {"error": "Potentially harmful SQL operation detected!"}}
return {"validation_result": {"valid": True}}
graph.add_node("Query Validation", query_validation_agent)
# ------------------ 3. Query Optimization Agent ------------------
def query_optimization_agent(state: SQLExecutionState) -> SQLExecutionState:
sql_metadata = state.get("structured_metadata", {})
prompt = f"""
Optimize the following SQL query for performance while ensuring that the output includes only the required columns and necessary joins.
Do not include any extra columns, unnecessary joins, or records that are not required to answer the query.
Here is the original SQL metadata:
{json.dumps(sql_metadata, indent=4)}
Output only the final optimized SQL query in plain text without any markdown formatting or explanations.
"""
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
optimized_query = response["choices"][0]["message"]["content"].strip()
if optimized_query.startswith("```sql"):
optimized_query = optimized_query.replace("```sql", "").replace("```", "").strip()
return {"optimized_sql": optimized_query}
graph.add_node("Query Optimization", query_optimization_agent)
# ------------------ 4. SQL Execution Agent ------------------
def execution_agent(state: SQLExecutionState) -> SQLExecutionState:
query = state.get("optimized_sql", "").strip()
if not query:
return {"execution_result": {"error": "No SQL query to execute."}}
try:
conn = sqlite3.connect("complex_test_db.sqlite", timeout=20)
cursor = conn.cursor()
cursor.execute(query)
result = cursor.fetchall()
cursor.close()
conn.close()
if not result:
return {"execution_result": {"error": "Query executed successfully but returned no results."}}
return {"execution_result": result}
except sqlite3.Error as e:
return {"execution_result": {"error": str(e)}}
graph.add_node("SQL Execution", execution_agent)
# ------------------ Define Execution Flow ------------------
graph.add_edge(START, "Query Understanding")
graph.add_edge("Query Understanding", "Query Validation")
graph.add_edge("Query Validation", "Query Optimization")
graph.add_edge("Query Optimization", "SQL Execution")
compiled_pipeline = graph.compile()
# ------------------ Function to Run the Multi-Agent Query ------------------
def run_multi_agent_query(natural_language_query):
result = compiled_pipeline.invoke({"sql_query": natural_language_query})
return json.dumps(result.get("execution_result", {}), indent=2)
# ------------------ Gradio Interface ------------------
schema_description = """
**Database Schema:**
- **customers**: customer_id, name, email, phone, address, created_at
- **products**: product_id, name, category, price, stock_quantity
- **orders**: order_id, customer_id, order_date, total_amount, status
- **order_items**: order_item_id, order_id, product_id, quantity, subtotal
- **payments**: payment_id, order_id, payment_date, amount, payment_method, status
- **shipment**: shipment_id, order_id, shipment_date, delivery_date, carrier, tracking_number, status
"""
iface = gr.Interface(
fn=run_multi_agent_query,
inputs=gr.Textbox(lines=2, placeholder="Enter your natural language query here.(Which products sold the most in 2024))."),
outputs="text",
title="Multi-Agent SQL Generator",
description=("Enter a natural language query to generate and execute a SQL query. E.g. "
"Find the email_id of the top 5 customers who spent the most in 2024.\n\n"
+ schema_description)
)
if __name__ == "__main__":
iface.launch() |