Providing additional context for converting natural language queries to SQL
To generate more accurate and context-aware SQL queries, you can provide additional context to the Text-to-SQL service in the form of SQL samples.
SQL samples can help improve the accuracy of the SQL queries that are generated from your plain text input because they can be used as a reference when the input query is ingested (few-shot prompting). You can add SQL samples to any project that is enabled for the Text-to-SQL service.
You create SQL samples as CSV files that must conform to these formatting rules:
-
The CSV file must not have a header row.
-
The CSV file consists of one or more rows in this format:
natural_language_query,evidence,sql- natural_language_query
- The request in plain English.
- This field is mandatory.
- evidence
- Additional information such as a formula to calculate a score or listing synonym terms. If the phrase contains a comma (
,) or a semicolon (;), include the entire phrase in double quotation marks ("). - This field is optional.
- sql
- The expected SQL query for the question. If the SQL statement contains a comma (
,) or a semicolon (;), include the entire statement in double quotation marks ("). - This field is mandatory.
You must maintain the order of the fields as shown.
The file names must follow this convention:
text_to_sql_fewshots_<n>.csv
n is a consecutive number. The first sample file that you upload must be named text_to_sql_fewshots_0.csv. You can add as many files as needed depending on your use cases.
All samples that you upload are searched for every Text-to-SQL conversion. The samples that are relevant to the specific request are retrieved based on the input question and are provided to the LLM as few-shot examples.
Sample CSV file content:
Who are the top 5 players who perform better in crossing actions?,perform better in crossing actions refers to MAX(crossing),SELECT id FROM Player_Attributes ORDER BY crossing DESC LIMIT 5
Give the name of the league had the most goals in the 2016 season?,"""league that had the most goals refers to MAX(SUM(home_team_goal,away_team_goal)); 2016 season refers to season = '2015/2016'""",SELECT t2.name FROM Match AS t1 INNER JOIN League AS t2 ON t1.league_id = t2.id WHERE t1.season = '2015/2016' GROUP BY t2.name ORDER BY SUM(t1.home_team_goal + t1.away_team_goal) DESC LIMIT 1
Indicate the full names of the top 10 players with the highest number of penalties.,,SELECT t2.player_name FROM Player_Attributes AS t1 INNER JOIN Player AS t2 ON t1.id = t2.id ORDER BY t1.penalties DESC LIMIT 10
What is the short name of the football team Queens Park Rangers?,,SELECT team_short_name FROM Team WHERE team_long_name = 'Queens Park Rangers'
To upload SQL sample files:
- In the project, click Import assets.
- Select Local file and click Data asset.
- Browse your local file system for the CSV files to upload.