SVashishta1 commited on
Commit
92d1d2a
·
1 Parent(s): 61ce4a6
Files changed (1) hide show
  1. app.py +47 -16
app.py CHANGED
@@ -36,28 +36,59 @@ os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
36
 
37
  # Define the prompt with examples
38
  query_prompt = ChatPromptTemplate.from_messages([
39
- ("system", """You are an SQL expert. Generate an appropriate SQL query using SQLite syntax for the question provided. The query should be executable and return exactly what was asked for.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- For questions about maximum/highest values, use MAX().
42
- For minimum/lowest values, use MIN().
43
- For averages, use AVG().
44
- For counts, use COUNT().
45
- For sums, use SUM().
46
 
47
- Examples:
48
- 1. Question: "What is the highest tip_amount in the dataset?"
49
- SQL: SELECT MAX(tip_amount) as highest_tip FROM data_tab;
50
 
51
- 2. Question: "What is the average fare amount?"
52
- SQL: SELECT AVG(fare_amount) as average_fare FROM data_tab;
 
53
 
54
- 3. Question: "How many trips are there?"
55
- SQL: SELECT COUNT(*) as trip_count FROM data_tab;
 
56
 
57
- 4. Question: "What are the top 5 highest tip amounts?"
58
- SQL: SELECT * FROM data_tab ORDER BY tip_amount DESC LIMIT 5;
 
 
 
 
 
 
 
 
 
 
59
 
60
- Generate only the SQL query, nothing else. Make sure to use the correct table name from the context provided."""),
 
61
  ("human", "{question}")
62
  ])
63
 
 
36
 
37
  # Define the prompt with examples
38
  query_prompt = ChatPromptTemplate.from_messages([
39
+ ("system", """
40
+ You are an SQL and data analysis expert. Generate an appropriate SQL query using SQLite syntax for the question provided, without any explanations or code comments.
41
+ Follow SQLite-specific conventions, as shown in the examples below:
42
+
43
+ Example 1:
44
+ Question: "What is the average fare for trips over 10 miles?"
45
+ SQL Query: SELECT AVG(fare_amount) FROM taxi_data WHERE trip_distance > 10;
46
+
47
+ Example 2:
48
+ Question: "How many trips were taken in each month?"
49
+ SQL Query: SELECT strftime('%m', pickup_datetime) AS month, COUNT(*) AS trip_count FROM taxi_data GROUP BY month;
50
+
51
+ Example 3:
52
+ Question: "What is the total fare amount for each driver (medallion) per day?"
53
+ SQL Query: SELECT DATE(pickup_datetime) AS date, medallion, SUM(fare_amount) AS total_fare FROM taxi_data GROUP BY date, medallion;
54
+
55
+ SQLite-Specific Conventions:
56
+
57
+ 1. Date and Time Extraction:
58
+ - Instead of `EXTRACT(YEAR FROM column)`, use `strftime('%Y', column)` to extract the year.
59
+ - Example: `SELECT strftime('%Y', pickup_datetime) FROM taxi_data;`
60
 
61
+ 2. String Length:
62
+ - Instead of `CHAR_LENGTH(column)`, use `LENGTH(column)`.
63
+ - Example: `SELECT LENGTH(passenger_name) FROM taxi_data;`
 
 
64
 
65
+ 3. Regular Expressions:
66
+ - SQLite does not support `REGEXP`. Use `LIKE` for simple patterns or avoid regular expressions.
67
+ - Example: `SELECT * FROM taxi_data WHERE passenger_name LIKE 'A%';`
68
 
69
+ 4. Window Functions:
70
+ - For row numbering, use `ROW_NUMBER()` if supported, or simulate with joins.
71
+ - Example: `SELECT id, ROW_NUMBER() OVER (ORDER BY pickup_datetime) AS row_num FROM taxi_data;`
72
 
73
+ 5. Data Type Casting:
74
+ - Use `CAST(column AS TYPE)`, but note that SQLite supports limited types.
75
+ - Example: `SELECT CAST(fare_amount AS INTEGER) FROM taxi_data;`
76
 
77
+ 6. Full Outer Join Workaround:
78
+ - SQLite doesn’t support `FULL OUTER JOIN`. Combine `LEFT JOIN` and `UNION` for a similar effect.
79
+ - Example:
80
+ ```
81
+ SELECT a.*, b.*
82
+ FROM table_a a
83
+ LEFT JOIN table_b b ON a.id = b.id
84
+ UNION
85
+ SELECT a.*, b.*
86
+ FROM table_a a
87
+ RIGHT JOIN table_b b ON a.id = b.id;
88
+ ```
89
 
90
+ Use these examples and guidelines to generate an SQL query compatible with SQLite syntax for the question provided.
91
+ """),
92
  ("human", "{question}")
93
  ])
94