Methods to fine tune simple query language generation from natural language input
A real-time system for generating Structured Query Language (SQL) queries from natural language input includes a server in communication with a large language model (LLM). The server is configured to: receive a natural language input from a user; generate a prompt that includes a terse table name or column name from the database, and instructions for generating an SQL query for the database, based on the natural language input. The processor is further configured to: replace the terse table name or column name with a verbose table name or column name; send the prompt to the LLM; receive an SQL query from the LLM, including the at least one verbose table name or column name; replace the at least one verbose table name or column name with the at least one terse table name or column name; execute the SQL query on the database; and receive an SQL query result.
1 . A real-time system for generating Structured Query Language (SQL) queries from natural language input, the system comprising:
a language server having at least one processor and a non-transitory computer readable medium operably coupled thereto, the server being in electronic communication with a large language model (LLM), the server being configured to receive a natural language input from a user, send a prompt to the LLM, and receive an SQL query from the LLM, the processor comprising a graphical user interface (GUI), an SQL interpreter, a prompt generation service, and a prompt refinement service, the server being in electronic communication with a database, the computer readable medium comprising a plurality of instructions stored in association therewith that are accessible to, and executable by, the processor, to perform operations which comprise:
with the GUI, receiving the natural language input from the user;
from the database, receiving a database schema comprising at least one terse table name or column name;
with the prompt refinement service, within the database schema, replacing the at least one terse table name or column name with at least one verbose table name or column name;
with the prompt generation service, the natural language input, and the database, generating a prompt, wherein the prompt comprises:
the at least one verbose table name or column name from the database schema; and
instructions for generating an SQL query for the database, based on the natural language input;
sending the prompt to the LLM;
receiving the SQL query from the LLM, wherein the SQL query includes the at least one verbose table name or column name;
within the SQL query, replacing the at least one verbose table name or column name with the at least one terse table name or column name;
with the SQL interpreter, executing the SQL query on the database; and
from the SQL interpreter, receiving an SQL query result.
2 . The system of claim 1 , wherein the operations further comprise:
adding, to the prompt, an instruction or SQL example comprising a time limit placeholder or row limit placeholder; and
replacing the time limit placeholder with a time limit or the row limit placeholder with a row limit.
3 . The system of claim 1 , wherein the operations further comprise:
determining whether the SQL query includes a condition on a timestamp column; and
if the SQL query does not include the condition on the timestamp column:
revising the prompt to instruct the LLM to include the condition on the timestamp column; and
sending the revised prompt to the LLM.
4 . The system of claim 1 , wherein the operations further comprise:
if the SQL query result includes an exception or error message:
revising the prompt to include the exception or error message;
sending the revised prompt to the LLM;
receiving the revised SQL query from the LLM;
with the SQL interpreter, executing the revised SQL query on the database; and
from the SQL interpreter, receiving a revised SQL query result.
5 . The system of claim 1 , wherein the operations further comprise:
based on the SQL query result, generating a graph or table; and
with the GUI, displaying the graph or table to the user.
6 . The system of claim 1 , wherein the operations further comprise:
based on the SQL query result, generating a second prompt instructing the LLM to explain the SQL query result;
sending the second prompt to the LLM;
receiving an LLM response; and
with the GUI, displaying the LLM response.
7 . The system of claim 1 , wherein the at least one terse table name or column name from the database is identified via a schema of the database, and
wherein the schema of the database is included in the prompt.
8 . The system of claim 1 , wherein replacing the at least one terse table name or column name with the at least one verbose table name or column name results in higher accuracy for the LLM in generating an SQL query with valid syntax.
9 . The system of claim 1 , wherein replacing the at least one terse table name or column name with the at least one verbose table name or column name results in faster execution time for the SQL query.
10 . The system of claim 1 , wherein replacing the at least one terse table name or column name with the at least one verbose table name or column name results in an improvement, within the SQL query generated by the LLM, in the relevance of selected table and column names.
11 . A computer-implemented method for generating Structured Query Language (SQL) queries from natural language input, which method comprises:
with a language server having at least one processor and a non-transitory computer readable medium operably coupled thereto, the server being in electronic communication with a large language model (LLM), the server being configured to receive a natural language input from a user, send a prompt to the LLM, and receive an SQL query from the LLM, the processor comprising a graphical user interface (GUI), an SQL interpreter, a prompt generation service, and a prompt refinement service, the server being in electronic communication with a database, performing operations which comprise:
with the GUI, receiving the natural language input from the user;
from the database, receiving a database schema comprising at least one terse table name or column name;
with the prompt refinement service, within the database schema, replacing the at least one terse table name or column name with at least one verbose table name or column name;
with the prompt generation service, the natural language input, and the database, generating a prompt, wherein the prompt comprises:
the at least one verbose table name or column name from the database schema; and
instructions for generating an SQL query for the database, based on the natural language input;
sending the prompt to the LLM;
receiving the SQL query from the LLM, wherein the SQL query includes the at least one verbose table name or column name;
within the SQL query, replacing the at least one verbose table name or column name with the at least one terse table name or column name;
with the SQL interpreter, executing the SQL query on the database; and
from the SQL interpreter, receiving an SQL query result.
12 . The method of claim 11 , wherein the operations further comprise:
adding, to the prompt, an instruction or SQL example comprising a time limit placeholder or row limit placeholder; and
replacing the time limit placeholder with a time limit or the row limit placeholder with a row limit.
13 . The method of claim 11 , wherein the operations further comprise:
determining whether the SQL query includes a condition on a timestamp column; and
if the SQL query does not include the condition on the timestamp column:
revising the prompt to instruct the LLM to include the condition on the timestamp column; and
sending the revised prompt to the LLM.
14 . The method of claim 11 , wherein the operations further comprise:
if the SQL query result includes an exception or error message:
revising the prompt to include the exception or error message;
sending the revised prompt to the LLM;
receiving the revised SQL query from the LLM;
with the SQL interpreter, executing the revised SQL query on the database; and
from the SQL interpreter, receiving a revised SQL query result.
15 . The method of claim 11 , wherein the operations further comprise:
based on the SQL query result, generating a graph or table; and
with the GUI, displaying the graph or table to the user.
16 . The method of claim 11 , wherein the operations further comprise:
based on the SQL query result, generating a second prompt instructing the LLM to explain the SQL query result;
sending the second prompt to the LLM;
receiving an LLM response; and
with the GUI, displaying the LLM response.
17 . The method of claim 11 , wherein the at least one terse table name or column name from the database is identified via a schema of the database, and
wherein the schema of the database is included in the prompt.
18 . The method of claim 11 , wherein replacing the at least one terse table name or column name with the at least one verbose table name or column name results in higher accuracy for the LLM in generating an SQL query with valid syntax.
19 . The method of claim 11 , wherein replacing the at least one terse table name or column name with the at least one verbose table name or column name results in faster execution time for the SQL query.
20 . The method of claim 11 , wherein replacing the at least one terse table name or column name with the at least one verbose table name or column name results in an improvement, within the SQL query generated by the LLM, in the relevance of selected table and column names.