SQL (Structured Query Language) generation method and system based on large language model, terminal and medium
Through the SQL generation method based on the large language model, natural language query is converted into SQL statements, solving the problem of writing SQL by non-professional personnel and improving the ease of use and accuracy of database queries.
Patent Information
- Application Number
- CN202411941389.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-26
- Publication Date
- 2025-05-02
AI Technical Summary
It is difficult for non-professional personnel to write SQL statements quickly and accurately, resulting in low accuracy of database query results, and traditional GUI tools are complex in operation and inefficient in efficiency.
Using the SQL generation method based on the large language model, by receiving natural language query statements, using the large language model to select candidate tables and data columns according to the prompt words, and searching example Q&A from the vector database, finally generating and filtering candidate SQL statements, and selecting the final SQL statement.
It improves the efficiency and accuracy of SQL statement generation, reduces users' requirements for SQL knowledge, improves the ease of use, accuracy and efficiency of database queries, and is suitable for databases in different fields.
Smart Images

Figure CN119917526A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of SQL generation, and in particular relates to a SQL generation method, system, terminal and medium based on a large language model. Background Art
[0002] In the modern information society, databases are widely used in all walks of life as the core technology for information storage and management. Database query language (SQL) is the main means of interacting with databases. However, non-professionals usually lack SQL knowledge and cannot quickly and accurately write SQL statements, thus failing to effectively obtain information from databases.
[0003] Traditional solutions mainly rely on graphical user interfaces (GUIs) to simplify the database query process. These GUI tools lower the threshold for SQL learning by providing functions such as drag-and-drop query builders and visual report generation. However, these tools still have many shortcomings in practical applications. On the one hand, due to the limited intuitiveness of GUI tools, users often find it difficult to accurately express complex query requirements, resulting in low accuracy of query results. On the other hand, the operation process of GUI tools is relatively cumbersome, and its efficiency is low for frequent queries or large-scale data processing tasks. Summary of the invention
[0004] To solve the above problems, the present invention provides a SQL generation method, system, terminal and medium based on a large language model, which converts natural language query statements into SQL statements through a large language model, improves the efficiency and accuracy of SQL statement generation, and further improves the usability and accuracy of database queries.
[0005] In a first aspect, the technical solution of the present invention provides a SQL generation method based on a large language model, comprising the following steps: Receive a natural language query statement input by a user; Write a first prompt word, and pass the natural language query statement and the target database model into the large language model, and the large language model selects and feeds back N candidate tables that are most relevant to the user query based on the first prompt word; Write a second prompt word, and pass N candidate tables and natural language query statements into the large language model. The large language model selects and feeds back several candidate data columns that are most relevant to the user query from each candidate table based on the second prompt word. Retrieve example question-answer pairs similar to the user query from the vector database, where the example question-answer pairs include example questions and example SQL statements; Write the third prompt word, pass N candidate tables, candidate data columns of each candidate table, natural language query statements, and sample question-answer pairs into the large language model, and the large language model generates several candidate SQL statements according to the third prompt word; Perform confidence filtering on several generated candidate SQL statements and select the final SQL statement.
[0006] In an optional implementation, a first prompt word is written, and a natural language query statement and a target database model are passed into a large language model, and the large language model selects and feeds back N candidate tables most relevant to the user query according to the first prompt word, specifically including: Step 1, write the first prompt word, the first prompt word is "please provide the candidate table in the database that is most relevant to the user's query based on the target database model and 'natural language query statement'"; Step 2, using a random function to randomly change the order of tables in each target database schema; Step 3: The transformed target database schema and natural language query statement are passed to the large language model, and the large language model selects and feeds back several candidate tables most relevant to the user query according to the first prompt word; Step 4, putting the multiple candidate tables fed back into the first list, and discarding the candidate tables in the multiple candidate tables fed back that are duplicated with the candidate tables in the current first list; Step 5, repeat the above steps 1 to 4 several times until the number of candidate tables in the first list reaches a preset number, and finally obtain N candidate tables in the first list.
[0007] In an optional implementation, a second prompt word is written, and N candidate tables and natural language query statements are passed into a large language model. The large language model selects and feeds back several candidate data columns that are most relevant to the user query from each candidate table according to the second prompt word, specifically including: Step 1, write a second prompt word, the second prompt word is "Please extract the candidate data column most relevant to the user query in each candidate table based on N candidate tables and 'natural language query statement'"; Step 2: Use a random function to randomly change the order of candidate tables and the order of data columns in each candidate table; Step 3: The transformed N candidate tables and the natural language query sentence are passed to the large language model, and the large language model selects several candidate data columns that are most relevant to the user query from each candidate table according to the second prompt word; Step 4, putting the fed-back candidate data columns into the second list, and discarding the data columns among the fed-back candidate data columns that are duplicated with the data columns currently in the second list; Step 5, repeating the above steps 1 to 4 several times until the number of candidate data columns in the second list reaches a preset number, thereby obtaining several candidate data columns of each candidate table in the final second list.
[0008] In an optional implementation, retrieving example SQL statements similar to the user query from the vector database specifically includes: Embed the natural language query statement and convert it into a vector representation; A vector database is retrieved according to the vector representation to screen out several groups of example questions and example SQL statements similar to the user's query.
[0009] In an optional implementation, a third prompt word is written, N candidate tables, candidate data columns of each candidate table, natural language query statements, and example question-answer pairs are passed into a large language model, and the large language model generates several candidate SQL statements according to the third prompt word, specifically including: Write the third prompt word, which is "Please generate P candidate SQL statements based on the target database mode and 'natural language query statement'. You can refer to the following examples, which include sample questions and sample SQL statements"; N candidate tables, candidate data columns of each candidate table, natural language query statements, and example question-answer pairs are passed into the large language model, and the large language model generates P candidate SQL statements according to the third prompt word.
[0010] In an optional implementation, confidence filtering is performed on several generated candidate SQL statements to select a final SQL statement, specifically including: Execute P candidate SQL statements in the database, remove candidate SQL statements with errors and / or query timeouts, and retain K candidate SQL statements; Divide the K candidate SQL statements with the same query results into one group, and divide them into Q groups of candidate SQL statements in total; The confidence of each group is calculated based on the number of candidate SQL statements in each group. The more candidate SQL statements there are, the higher the confidence. For each group, keep a candidate SQL statement with the shortest query time; According to the confidence threshold, retain M groups of candidate SQL statements whose confidence is higher than the confidence threshold; The final SQL statement is selected from M groups of candidate SQL statements using a large language model.
[0011] In an optional implementation, the final SQL statement is selected from the M groups of candidate SQL statements using a large language model, specifically including: Write a fourth prompt word, which is "for the template database mode and 'natural language query statement', select the most accurate SQL statement from the following candidate SQL statements"; N candidate tables, candidate data columns of each candidate table, natural language query statements, and M candidate SQL statements are passed into the large language model, and the large language model selects the final SQL statement from the M candidate SQL statements according to the third prompt word.
[0012] In a second aspect, the technical solution of the present invention provides a SQL generation system based on a large language model, comprising: A query statement receiving module, used to receive a natural language query statement input by a user; A candidate table acquisition module is used to write a first prompt word, and pass the natural language query statement and the target database model into the large language model, and the large language model selects and feeds back N candidate tables most relevant to the user query according to the first prompt word; A candidate data column acquisition module is used to compile a second prompt word and pass N candidate tables and natural language query statements into the large language model, and the large language model selects and feeds back a number of candidate data columns that are most relevant to the user query from each candidate table based on the second prompt word; An example question-answer pair acquisition module is used to retrieve example question-answer pairs similar to the user query from the vector database, where the example question-answer pairs include example questions and example SQL statements; A candidate SQL statement acquisition module is used to write a third prompt word, pass N candidate tables, candidate data columns of each candidate table, natural language query statements, and sample question-answer pairs into the large language model, and the large language model generates a number of candidate SQL statements according to the third prompt word; The SQL statement screening module is used to perform confidence filtering on several generated candidate SQL statements and select the final SQL statement.
[0013] In a third aspect, the technical solution of the present invention provides a terminal, including: A memory, used for storing a SQL generation program based on a large language model; A processor is used to implement the steps of any of the above-mentioned SQL generation methods based on a large language model when executing the SQL generation program based on a large language model.
[0014] In a fourth aspect, the technical solution of the present invention provides a computer-readable storage medium, on which is stored a SQL generation program based on a large language model. When the SQL generation program based on a large language model is executed by a processor, the steps of the SQL generation method based on a large language model as described in any one of the above items are implemented.
[0015] The SQL generation method, system, terminal and medium based on a large language model provided by the present invention have the following beneficial effects compared with the prior art: the large language model converts natural language query statements into SQL statements based on prompt words and input context, and combines the pattern rules and query technology of the database to achieve efficient conversion from natural language to SQL query. The user does not need to have SQL knowledge, but only needs to input natural language queries to obtain corresponding database information. The parsing ability of the large language model is used to improve the accuracy of converting natural language queries into SQL statements, solves the problems of low accuracy, complex operation and poor versatility in the prior art, improves the ease of use, accuracy and efficiency of database queries, and is suitable for databases in different fields, with good versatility and extensibility. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the technical solution of the present invention, the accompanying drawings required for use in the description will be briefly introduced below. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.
[0017] Figure 1 A flowchart of a SQL generation method based on a large language model is provided in an embodiment of the present invention.
[0018] Figure 2 A schematic block diagram of the structure of a SQL generation system based on a large language model provided in an embodiment of the present invention.
[0019] Figure 3 A schematic diagram of the structure of a terminal provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0020] In order to make the purpose, features and advantages of the present invention more obvious and easy to understand, the technical scheme of the present invention will be clearly and completely described below in conjunction with the drawings in this specific embodiment. Obviously, the embodiments described below are only part of the embodiments of the present invention, not all of them. Based on the embodiments in this patent, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this patent.
[0021] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art of the present invention. The terms used in the specification of the present invention herein are only for the purpose of describing specific embodiments and are not intended to limit the present invention.
[0022] The key terms appearing in the present invention are explained below.
[0023] Large Model Technology refers to deep learning models trained based on massive amounts of data, which usually have billions or even hundreds of billions of parameters. Such models can perform well on multiple tasks, such as image recognition and natural language processing. Large models can capture and understand deep patterns in data through complex neural network structures, providing highly accurate prediction and generation capabilities.
[0024] Natural Language Processing (NLP) is a branch of artificial intelligence that is dedicated to enabling the interaction between computers and human natural languages. NLP technologies include but are not limited to language understanding, language generation, machine translation, sentiment analysis, etc.
[0025] Prompt Engineering is a key technology in big model technology, which is mainly used to optimize the performance of big models in natural language processing tasks. Prompts are text fragments input to big models. By designing and adjusting prompts, big models can be guided to generate specific outputs. For example, in dialogue generation tasks, prompts can be used to set the context and tone of the dialogue, thereby obtaining more accurate and natural answers.
[0026] Vector Database Technology is a database technology specifically designed for storing, querying, and managing high-dimensional vector data. With the development of machine learning and artificial intelligence, especially the widespread application of large models and embedded representations, vector databases have performed well in processing and retrieving similar data. Vector databases can quickly find similar items from a large amount of vector data through efficient similarity search algorithms, such as Approximate Nearest Neighbor (ANN) technology.
[0027] The database schema refers to the structure and organization of the database, including the definition of tables, fields, types, relationships, and constraints. The database schema defines the logical structure and storage method of data in the database and is the basis of database design.
[0028] Database query technology refers to the technology and methods for retrieving information from a database. Common database query languages include SQL (Structured Query Language) and NoSQL (Non-Structured Query Language). The key to database query technology is how to efficiently and accurately extract the required information from a large amount of data.
[0029] SQL (Structured Query Language) is a standard language for managing and operating relational databases. SQL technology is widely used in data definition, data query, data manipulation and data control.
[0030] Figure 1 1 is a flow chart of a SQL generation method based on a large language model provided by an embodiment of the present invention. Figure 1 The execution subject may be a SQL generation system based on a large language model. The SQL generation method based on a large language model provided in the embodiment of the present invention is executed by a computer device, and accordingly, the SQL generation system based on a large language model runs in the computer device. According to different requirements, the order of the steps in the flowchart can be changed, and some can be omitted.
[0031] like Figure 1 As shown, the method includes the following steps.
[0032] S1, receiving a natural language query statement input by a user.
[0033] The user enters a query statement in natural language, such as: Please count the sales of the last two years by product category. The natural language query statement entered by the user is subsequently received and saved, and subsequent steps are executed.
[0034] S2, write a first prompt word, and pass the natural language query statement and the target database model into the large language model, and the large language model selects and feeds back N candidate tables most relevant to the user query based on the first prompt word.
[0035] This step passes the natural language query statement and the target database model into the large language model, and the large language model selects N candidate tables that are most relevant to the user query, which specifically includes the following steps.
[0036] S2.1, write a first prompt word, the first prompt word is "please provide the candidate table in the database that is most relevant to the user's query based on the target database model and 'natural language query statement'".
[0037] The user writes the above prompt words. It should be noted that the 'natural language query sentence' in the above prompt words is an actual input sentence, that is, "please calculate the sales volume in the last two years by product category."
[0038] S2.2, using a random function to randomly change the order of tables in each target database schema.
[0039] The tables in the database schema are generally arranged in alphabetical order of the table names. To avoid the large language model defaulting to the front table as the most relevant table, the order of the tables in the database schema is disrupted to improve the accuracy of screening candidate tables.
[0040] S2.3, the transformed target database schema and natural language query statement are passed into the large language model, and the large language model selects and feeds back several candidate tables most relevant to the user query according to the first prompt word.
[0041] The transformed target database schema and natural language query statement are context information. The large language model selects several candidate tables most relevant to the user query based on this context information and the first prompt word written, and feeds them back to the front end.
[0042] S2.4, putting the fed-back candidate tables into the first list, and discarding the candidate tables in the fed-back candidate tables that are duplicated with the candidate tables in the current first list.
[0043] Check whether the candidate list of the current feedback exists in the first list; if so, discard the candidate list of the current feedback; if not, put the candidate list of the current feedback into the first list.
[0044] S2.5, repeat the above S2.1-S2.4 several times until the number of candidate tables in the first list reaches a preset number, and finally obtain N candidate tables in the first list.
[0045] The above four steps are repeatedly performed multiple times to obtain a preset number of candidate lists, thereby improving the accuracy of candidate list screening, and finally obtaining N candidate lists in the first list.
[0046] S3, write a second prompt word, and pass N candidate tables and natural language query statements into the large language model. The large language model selects and feeds back several candidate data columns that are most relevant to the user query from each candidate table based on the second prompt word.
[0047] This step is based on N candidate tables, and the large language model selects the candidate data column in each candidate table that is most relevant to the user query, which specifically includes the following steps.
[0048] S3.1, write a second prompt word, the second prompt word is "Please extract the candidate data column that is most relevant to the user query in each candidate table based on N candidate tables and 'natural language query statement'."
[0049] S3.2, using a random function to randomly change the order of candidate tables and the order of data columns in each candidate table.
[0050] To improve screening accuracy, the order of candidate tables and the order of data columns in each candidate table are randomly changed.
[0051] S3.3, the transformed N candidate tables and the natural language query statement are passed into the large language model, and the large language model selects several candidate data columns that are most relevant to the user query from each candidate table according to the second prompt word.
[0052] The transformed N candidate tables and natural language query statements are passed as context information to the large language model, and the large language model selects several candidate data columns most relevant to the user query for each candidate table based on the context information according to the second prompt word.
[0053] S3.4, putting the fed-back candidate data columns into the second list, and discarding the data columns among the fed-back candidate data columns that are duplicated with the data columns currently in the second list.
[0054] The candidate data columns are put into the second list in the form of "table name.data column name". Check whether the candidate data column currently fed back exists in the second list. If so, discard the candidate data column currently fed back. If not, put the candidate data column currently fed back into the second list.
[0055] S3.5, repeat the above S3.1-S3.4 several times until the number of candidate data columns in the second list reaches a preset number, and finally obtain several candidate data columns of each candidate table in the second list.
[0056] To improve the screening accuracy, the above four steps are repeated multiple times until a preset number of candidate data columns are obtained, and finally the candidate data columns selected from each candidate table are obtained in the second list.
[0057] S4, retrieve example question-answer pairs similar to the user query from the vector database, where the example question-answer pairs include example questions and example SQL statements.
[0058] Retrieve sample questions and sample SQL statements similar to the user's query from the vector database, where multiple sets of sample questions and sample SQL statements are stored in the form of vectors (Embedding). First, embed the natural language query statement and convert it into a vector representation. Then, search the vector database based on the vector representation to filter out several sets of sample questions and sample SQL statements similar to the user's query.
[0059] S5, write a third prompt word, pass N candidate tables, candidate data columns of each candidate table, natural language query statements, and example question-answer pairs into the large language model, and the large language model generates several candidate SQL statements according to the third prompt word.
[0060] S5.1, write a third prompt word, the third prompt word is "Please generate P candidate SQL statements based on the target database model and 'natural language query statement'. You can refer to the following examples, which include sample questions and sample SQL statements."
[0061] S5.2, N candidate tables, candidate data columns of each candidate table, natural language query statements, and example question-answer pairs are passed into the large language model, and the large language model generates P candidate SQL statements according to the third prompt word.
[0062] In this embodiment, a large language model generates several candidate SQL statements based on the selected candidate table, candidate data columns, and retrieved example SQL statements. The number of generated candidate SQL statements can be set as needed.
[0063] S6, confidence filtering is performed on the generated candidate SQL statements to select the final SQL statement.
[0064] This step performs confidence filtering on the P candidate SQL statements to obtain the most matching M candidate SQL statements, and then uses the large language model to filter out the final SQL statement from the M candidate SQL statements, which specifically includes the following steps.
[0065] S6.1, execute P candidate SQL statements in the database, remove candidate SQL statements with errors and / or query timeouts, and retain K candidate SQL statements.
[0066] S6.2, divide the candidate SQL statements with the same query results among the K candidate SQL statements into one group, and divide them into Q groups of candidate SQL statements in total.
[0067] First, the query results are grouped together, and candidate SQL statements with the same query results are grouped together.
[0068] S6.3, calculating the confidence of each group according to the number of candidate SQL statements in each group, the more the number of candidate SQL statements, the higher the confidence.
[0069] The confidence calculation formula is as follows:
[0070] In the formula, query (q i ) is the query result of the i-th group.
[0071] For example, there are K=10 candidate SQLs, divided into Q=3 groups, named Q1, Q2, Q3. There are 5 candidate SQLs in Q1, 3 candidate SQLs in Q2, and 2 candidate SQLs in Q3. Then, the confidence of Q1 is 5 / 10=0.5, the confidence of Q2 is 3 / 10=0.3, and the confidence of Q3 is 0.2.
[0072] S6.4, for each group, retain a candidate SQL statement with the shortest query time.
[0073] For each group, only the candidate SQL statement with the shortest query time is retained. At this time, there is only one candidate SQL statement in each group.
[0074] S6.5, according to the confidence threshold, retain M groups of candidate SQL statements whose confidence is higher than the confidence threshold.
[0075] Set the confidence threshold T, for example, to 0.65, and remove groups with confidence levels lower than the threshold from candidate SQL statements.
[0076] S6.6, using the large language model to select the final SQL statement from the M groups of candidate SQL statements.
[0077] S6.6.1, write a fourth prompt word, the fourth prompt word is "for the template database mode and 'natural language query statement', select the most accurate SQL statement from the following candidate SQL statements."
[0078] S6.6.2, N candidate tables, candidate data columns of each candidate table, natural language query statements, and M candidate SQL statements are passed to the large language model, and the large language model selects the final SQL statement from the M candidate SQL statements based on the third prompt word.
[0079] An embodiment of a SQL generation method based on a large language model is described in detail above. Based on the SQL generation method based on a large language model described in the above embodiment, an embodiment of the present invention also provides a SQL generation system based on a large language model corresponding to the method.
[0080] Figure 2 It is a schematic block diagram of the structure of a SQL generation system based on a large language model provided by an embodiment of the present invention. In this embodiment, the SQL generation system 200 based on a large language model can be divided into multiple functional modules according to the functions performed by them. The module referred to in the present invention refers to a series of computer program segments that can be executed by at least one processor and can complete fixed functions, which are stored in a memory.
[0081] The query statement receiving module 210 is used to receive a natural language query statement input by a user.
[0082] The candidate table acquisition module 220 is used to write a first prompt word and pass the natural language query statement and the target database model to the large language model, and the large language model selects and feeds back N candidate tables most relevant to the user query according to the first prompt word.
[0083] The candidate data column acquisition module 230 is used to compile the second prompt word and pass N candidate tables and natural language query statements to the large language model, which selects and feeds back several candidate data columns most relevant to the user query from each candidate table based on the second prompt word.
[0084] The example question-answer pair acquisition module 240 is used to retrieve example question-answer pairs similar to the user query from the vector database, where the example question-answer pairs include example questions and example SQL statements.
[0085] The candidate SQL statement acquisition module 250 is used to write a third prompt word, pass N candidate tables, candidate data columns of each candidate table, natural language query statements, and example question-answer pairs into the large language model, and the large language model generates a number of candidate SQL statements based on the third prompt word.
[0086] The SQL statement screening module 260 is used to perform confidence filtering on the generated candidate SQL statements and select the final SQL statement.
[0087] The SQL generation system based on a large language model of this embodiment is used to implement the aforementioned SQL generation method based on a large language model. Therefore, the specific implementation method of the system can be seen in the embodiment part of the SQL generation method based on a large language model in the previous text. Therefore, its specific implementation method can refer to the description of the corresponding embodiments of each part, which will not be introduced in detail here.
[0088] In addition, since the SQL generation system based on a large language model in this embodiment is used to implement the aforementioned SQL generation method based on a large language model, its function corresponds to that of the aforementioned method and will not be repeated here.
[0089] Figure 3 A schematic diagram of the structure of a terminal 300 provided in an embodiment of the present invention includes: a processor 310, a memory 320 and a communication unit 330. The processor 310 is used to implement the following steps when implementing the SQL generation program based on the large language model stored in the memory 320: Receive a natural language query statement input by a user; Write a first prompt word, and pass the natural language query statement and the target database model into the large language model, and the large language model selects and feeds back N candidate tables that are most relevant to the user query based on the first prompt word; Write a second prompt word, and pass N candidate tables and natural language query statements into the large language model. The large language model selects and feeds back several candidate data columns that are most relevant to the user query from each candidate table based on the second prompt word. Retrieve example question-answer pairs similar to the user query from the vector database, where the example question-answer pairs include example questions and example SQL statements; Write the third prompt word, pass N candidate tables, candidate data columns of each candidate table, natural language query statements, and sample question-answer pairs into the large language model, and the large language model generates several candidate SQL statements according to the third prompt word; Perform confidence filtering on several generated candidate SQL statements and select the final SQL statement.
[0090] The present invention also provides a computer storage medium, wherein the storage medium may be a magnetic disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM).
[0091] The computer storage medium stores a SQL generation program based on a large language model, and when the SQL generation program based on the large language model is executed by a processor, the following steps are implemented: Receive a natural language query statement input by a user; Write a first prompt word, and pass the natural language query statement and the target database model into the large language model, and the large language model selects and feeds back N candidate tables that are most relevant to the user query based on the first prompt word; Write a second prompt word, and pass N candidate tables and natural language query statements into the large language model. The large language model selects and feeds back several candidate data columns that are most relevant to the user query from each candidate table based on the second prompt word. Retrieve example question-answer pairs similar to the user query from the vector database, where the example question-answer pairs include example questions and example SQL statements; Write the third prompt word, pass N candidate tables, candidate data columns of each candidate table, natural language query statements, and sample question-answer pairs into the large language model, and the large language model generates several candidate SQL statements according to the third prompt word; Perform confidence filtering on several generated candidate SQL statements and select the final SQL statement.
[0092] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A SQL generation method based on a large language model, characterized in that: The following steps are involved: Receive a natural language query statement input by a user; Write a first prompt word, and pass the natural language query statement and the target database model into the large language model, and the large language model selects and feeds back N candidate tables that are most relevant to the user query based on the first prompt word; Write a second prompt word, and pass N candidate tables and natural language query statements into the large language model. The large language model selects and feeds back several candidate data columns that are most relevant to the user query from each candidate table based on the second prompt word. Retrieve example question-answer pairs similar to the user query from the vector database, where the example question-answer pairs include example questions and example SQL statements; Write the third prompt word, pass N candidate tables, candidate data columns of each candidate table, natural language query statements, and sample question-answer pairs into the large language model, and the large language model generates several candidate SQL statements according to the third prompt word; Perform confidence filtering on several generated candidate SQL statements and select the final SQL statement.
2. The SQL generation method based on a large language model according to claim 1, characterized in that: Write the first prompt word, and pass the natural language query statement and the target database model into the large language model. The large language model selects and feeds back the N candidate tables most relevant to the user query based on the first prompt word, specifically including: Step 1, write the first prompt word, the first prompt word is "please provide the candidate table in the database that is most relevant to the user's query based on the target database model and 'natural language query statement'"; Step 2, using a random function to randomly change the order of tables in each target database schema; Step 3: The transformed target database schema and natural language query statement are passed to the large language model, and the large language model selects and feeds back several candidate tables most relevant to the user query according to the first prompt word; Step 4, putting the multiple candidate tables fed back into the first list, and discarding the candidate tables in the multiple candidate tables fed back that are duplicated with the candidate tables in the current first list; Step 5, repeat the above steps 1 to 4 several times until the number of candidate tables in the first list reaches a preset number, and finally obtain N candidate tables in the first list.
3. The SQL generation method based on a large language model according to claim 1, characterized in that: Write the second prompt word, and pass N candidate tables and natural language query statements into the large language model. The large language model selects and feeds back several candidate data columns that are most relevant to the user query from each candidate table based on the second prompt word, including: Step 1, write a second prompt word, the second prompt word is "Please extract the candidate data column most relevant to the user query in each candidate table based on N candidate tables and 'natural language query statement'"; Step 2: Use a random function to randomly change the order of candidate tables and the order of data columns in each candidate table; Step 3: The transformed N candidate tables and the natural language query sentence are passed to the large language model, and the large language model selects several candidate data columns that are most relevant to the user query from each candidate table according to the second prompt word; Step 4, putting the fed-back candidate data columns into the second list, and discarding the data columns among the fed-back candidate data columns that are duplicated with the data columns currently in the second list; Step 5, repeating the above steps 1 to 4 several times until the number of candidate data columns in the second list reaches a preset number, thereby obtaining several candidate data columns of each candidate table in the final second list.
4. The SQL generation method based on a large language model according to claim 1, characterized in that: Retrieve sample SQL statements similar to the user's query from the vector database, including: Embed the natural language query statement and convert it into a vector representation; A vector database is retrieved according to the vector representation to screen out several groups of example questions and example SQL statements similar to the user's query.
5. The SQL generation method based on a large language model according to claim 1, characterized in that: Write the third prompt word, pass N candidate tables, candidate data columns of each candidate table, natural language query statements, and sample question-answer pairs into the large language model, and the large language model generates several candidate SQL statements based on the third prompt word, including: Write the third prompt word, which is "Please generate P candidate SQL statements based on the target database mode and 'natural language query statement'. You can refer to the following example, which includes a sample question and a sample SQL statement"; N candidate tables, candidate data columns of each candidate table, natural language query statements, and example question-answer pairs are passed into the large language model, and the large language model generates P candidate SQL statements according to the third prompt word.
6. The SQL generation method based on a large language model according to claim 5, characterized in that: Confidence filtering is performed on several generated candidate SQL statements to select the final SQL statement, including: Execute P candidate SQL statements in the database, remove candidate SQL statements with errors and / or query timeouts, and retain K candidate SQL statements; Divide the K candidate SQL statements with the same query results into one group, and divide them into Q groups of candidate SQL statements in total; The confidence of each group is calculated based on the number of candidate SQL statements in each group. The more candidate SQL statements there are, the higher the confidence. For each group, keep a candidate SQL statement with the shortest query time; According to the confidence threshold, retain M groups of candidate SQL statements whose confidence is higher than the confidence threshold; The final SQL statement is selected from M groups of candidate SQL statements using a large language model.
7. The SQL generation method based on a large language model according to claim 6, characterized in that: The final SQL statement is selected from M groups of candidate SQL statements using a large language model, including: Write a fourth prompt word, which is "for the template database mode and 'natural language query statement', select the most accurate SQL statement from the following candidate SQL statements"; N candidate tables, candidate data columns of each candidate table, natural language query statements, and M candidate SQL statements are passed into the large language model, and the large language model selects the final SQL statement from the M candidate SQL statements according to the third prompt word.
8. A SQL generation system based on a large language model, characterized in that: include: A query statement receiving module, used to receive a natural language query statement input by a user; A candidate table acquisition module is used to write a first prompt word, and pass the natural language query statement and the target database model into the large language model, and the large language model selects and feeds back N candidate tables most relevant to the user query according to the first prompt word; A candidate data column acquisition module is used to compile a second prompt word and pass N candidate tables and natural language query statements into the large language model, and the large language model selects and feeds back a number of candidate data columns that are most relevant to the user query from each candidate table based on the second prompt word; An example question-answer pair acquisition module is used to retrieve example question-answer pairs similar to the user query from the vector database, where the example question-answer pairs include example questions and example SQL statements; A candidate SQL statement acquisition module is used to write a third prompt word, pass N candidate tables, candidate data columns of each candidate table, natural language query statements, and sample question-answer pairs into the large language model, and the large language model generates a number of candidate SQL statements according to the third prompt word; The SQL statement screening module is used to perform confidence filtering on several generated candidate SQL statements and select the final SQL statement.
9. A terminal, characterized in that: include: A memory, used for storing a SQL generation program based on a large language model; A processor, configured to implement the steps of the SQL generation method based on a large language model as described in any one of claims 1 to 7 when executing the SQL generation program based on a large language model.
10. A computer-readable storage medium, characterized in that: The readable storage medium stores a SQL generation program based on a large language model, and when the SQL generation program based on a large language model is executed by a processor, the steps of the SQL generation method based on a large language model as described in any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Database query method, device and equipment based on large model retrieval enhancement generation
CN120104762A
SQL data set generation method and device
CN120910080A
A method and apparatus for generating SQL datasets
CN120910080B
Natural language query method and system for rail transit field
CN120994693A
A natural language query method and system for rail transit field
CN120994693B