Spaces:
Running
Running
try to fix return
Browse files
app.py
CHANGED
@@ -43,27 +43,33 @@ def query_sql(user_query: str) -> str:
|
|
43 |
Returns:
|
44 |
The query result from the database as a formatted string.
|
45 |
"""
|
46 |
-
#
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
# Log the generated SQL for debugging
|
50 |
print(f"Generated SQL: {generated_sql}")
|
51 |
|
52 |
-
# Ensure we only execute valid
|
53 |
-
if not generated_sql.lower().startswith(("select", "show", "pragma")):
|
54 |
return "Error: Only SELECT queries are allowed."
|
55 |
|
56 |
# Execute the SQL query and return the result
|
57 |
result = sql_engine(generated_sql)
|
58 |
|
59 |
-
# Log the
|
60 |
print(f"SQL Query Result: {result}")
|
61 |
|
62 |
-
|
63 |
-
if not result.strip():
|
64 |
-
return "No results found."
|
65 |
-
|
66 |
-
return result # Return clean result, avoiding passing raw SQL back
|
67 |
|
68 |
# Initialize CodeAgent to generate SQL queries from natural language
|
69 |
agent = CodeAgent(
|
|
|
43 |
Returns:
|
44 |
The query result from the database as a formatted string.
|
45 |
"""
|
46 |
+
# Provide the AI with the correct schema
|
47 |
+
schema_info = (
|
48 |
+
"The database has a table named 'receipts' with the following schema:\n"
|
49 |
+
"- receipt_id (INTEGER, primary key)\n"
|
50 |
+
"- customer_name (VARCHAR(16))\n"
|
51 |
+
"- price (FLOAT)\n"
|
52 |
+
"- tip (FLOAT)\n"
|
53 |
+
"Generate an SQL query using ONLY these column names.\n"
|
54 |
+
)
|
55 |
+
|
56 |
+
# Generate SQL query using the provided schema
|
57 |
+
generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
|
58 |
|
59 |
# Log the generated SQL for debugging
|
60 |
print(f"Generated SQL: {generated_sql}")
|
61 |
|
62 |
+
# Ensure we only execute valid SELECT queries
|
63 |
+
if not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
|
64 |
return "Error: Only SELECT queries are allowed."
|
65 |
|
66 |
# Execute the SQL query and return the result
|
67 |
result = sql_engine(generated_sql)
|
68 |
|
69 |
+
# Log the SQL query result
|
70 |
print(f"SQL Query Result: {result}")
|
71 |
|
72 |
+
return result
|
|
|
|
|
|
|
|
|
73 |
|
74 |
# Initialize CodeAgent to generate SQL queries from natural language
|
75 |
agent = CodeAgent(
|