ZennyKenny commited on
Commit
35cddc5
·
verified ·
1 Parent(s): 042246b

try to fix return

Browse files
Files changed (1) hide show
  1. app.py +16 -10
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
- # Generate SQL from natural language
47
- generated_sql = agent.run(f"Convert this request into SQL: {user_query}")
 
 
 
 
 
 
 
 
 
 
48
 
49
  # Log the generated SQL for debugging
50
  print(f"Generated SQL: {generated_sql}")
51
 
52
- # Ensure we only execute valid SQL queries
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 result for debugging
60
  print(f"SQL Query Result: {result}")
61
 
62
- # If the result is empty, return a friendly message
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(