Skip to main content

Google Colab

Here are the steps:
1

Implement text2SQL

2

Setup dataset and evaluators

3

Run experiments

Implement Text2SQL

We are going to use the NBA dataset that information from 2014 - 2018 about every game played in that span. We will use DuckDB as our database.
Here’s the example of one row of a game in the dataset
{'Unnamed: 0': 1, 'Team': 'ATL', 'Game': 1, 'Date': '10/29/14', 'Home': 'Away', 'Opponent': 'TOR', 'WINorLOSS': 'L', 'TeamPoints': 102, 'OpponentPoints': 109, 'FieldGoals': 40, 'FieldGoalsAttempted': 80, 'FieldGoals.': 0.5, 'X3PointShots': 13, 'X3PointShotsAttempted': 22, 'X3PointShots.': 0.591, 'FreeThrows': 9, 'FreeThrowsAttempted': 17, 'FreeThrows.': 0.529, 'OffRebounds': 10, 'TotalRebounds': 42, 'Assists': 26, 'Steals': 6, 'Blocks': 8, 'Turnovers': 17, 'TotalFouls': 24, 'Opp.FieldGoals': 37, 'Opp.FieldGoalsAttempted': 90, 'Opp.FieldGoals.': 0.411, 'Opp.3PointShots': 8, 'Opp.3PointShotsAttempted': 26, 'Opp.3PointShots.': 0.308, 'Opp.FreeThrows': 27, 'Opp.FreeThrowsAttempted': 33, 'Opp.FreeThrows.': 0.818, 'Opp.OffRebounds': 16, 'Opp.TotalRebounds': 48, 'Opp.Assists': 26, 'Opp.Steals': 13, 'Opp.Blocks': 9, 'Opp.Turnovers': 9, 'Opp.TotalFouls': 22}
Let’s start by implementing a simple text2sql logic.
SELECT Team, COUNT(*) AS Wins FROM nba WHERE WINorLOSS = ‘W’ GROUP BY Team ORDER BY Wins DESC LIMIT 1;
Let’s run the query against our database now!
[{‘Team’: ‘GSW’, ‘Wins’: 265}]

Setup dataset and evaluators

To setup an experiment we need a dataset, task and evaluator. Let’s setup each. Setup dataset
Let’s store the data above as a versioned dataset in Arize AX.
Setup task Next, we’ll define the task. The task is to generate SQL queries from natural language questions.
Setup evaluator Finally, we’ll define the evaluator. We’ll use the following simple scoring functions to see if the generated SQL queries are correct.
Now let’s run the experiment.

Interpreting the results

Now that we ran the initial evaluation, it looks like three of the results are valid, one produces SQL errors, and one has no results. The second query for `Which team won the most games in 2015` looks for Date LIKE '2015%' which is not correct. The fourth query does not have TEAM in the group by clause. Let’s try to improve the prompt with few-shot examples and see if we can get better results.
SELECT Team, COUNT(*) AS Wins FROM nba WHERE WINorLOSS = ‘W’ AND Date LIKE ’%/15’ GROUP BY Team ORDER BY Wins DESC LIMIT 1;
Looking better! Finally, let’s add a scoring function that compares the results, if they exist, with the expected results. And then we can run this as another experiment and compare the results.
You can see that the newer SQL has improved some cases, but there are still some other errors to iron out. As you experiment with different models, prompts, and techniques, you can continuously optimize your applications until they reach the performance thresholds you want.