ZennyKenny commited on
Commit
10e2935
·
verified ·
1 Parent(s): fd2e4bf

handle numerical output

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -25,13 +25,16 @@ def sql_engine(query: str) -> str:
25
  if not rows:
26
  return "No results found."
27
 
 
 
 
 
28
  # Convert query results into a clean, readable format
29
  return "\n".join([", ".join(map(str, row)) for row in rows])
30
 
31
  except Exception as e:
32
  return f"Error: {str(e)}"
33
 
34
- @tool
35
  def query_sql(user_query: str) -> str:
36
  """
37
  Converts natural language input to an SQL query using CodeAgent
@@ -61,8 +64,8 @@ def query_sql(user_query: str) -> str:
61
  print(f"Generated SQL: {generated_sql}")
62
 
63
  # Ensure we only execute valid SELECT queries
64
- # if not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
65
- # return "Error: Only SELECT queries are allowed."
66
 
67
  # Execute the SQL query and return the result
68
  result = sql_engine(generated_sql)
@@ -70,7 +73,11 @@ def query_sql(user_query: str) -> str:
70
  # Log the SQL query result
71
  print(f"SQL Query Result: {result}")
72
 
73
- return result # Return only the final query result, NOT the generated SQL
 
 
 
 
74
 
75
  def handle_query(user_input: str) -> str:
76
  """
 
25
  if not rows:
26
  return "No results found."
27
 
28
+ # If query returns a single value (e.g., AVG, SUM, COUNT), return as a string
29
+ if len(rows) == 1 and len(rows[0]) == 1:
30
+ return str(rows[0][0]) # Convert numerical result to string
31
+
32
  # Convert query results into a clean, readable format
33
  return "\n".join([", ".join(map(str, row)) for row in rows])
34
 
35
  except Exception as e:
36
  return f"Error: {str(e)}"
37
 
 
38
  def query_sql(user_query: str) -> str:
39
  """
40
  Converts natural language input to an SQL query using CodeAgent
 
64
  print(f"Generated SQL: {generated_sql}")
65
 
66
  # Ensure we only execute valid SELECT queries
67
+ if not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
68
+ return "Error: Only SELECT queries are allowed."
69
 
70
  # Execute the SQL query and return the result
71
  result = sql_engine(generated_sql)
 
73
  # Log the SQL query result
74
  print(f"SQL Query Result: {result}")
75
 
76
+ try:
77
+ float_result = float(result)
78
+ return f"{float_result:.2f}"
79
+ except ValueError:
80
+ return result
81
 
82
  def handle_query(user_input: str) -> str:
83
  """