Method and device for enhancing natural language to SQL (Structured Query Language) performance based on dynamic planner
By introducing a dynamic planner into the large language model technology, the semantics and database fields of user queries are associated, and the problem of low SQL generation accuracy is solved, achieving higher SQL generation accuracy and widespread use.
Patent Information
- Application Number
- CN202510063050.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-15
- Publication Date
- 2025-05-09
AI Technical Summary
The prior art is difficult to effectively correlate the semantics of user queries and the correct fields of the database, resulting in low SQL generation accuracy, especially the problem of inability to parse when user queries are complex.
Using a dynamic planner-based method, user queries are converted into syntax structures, and associated information is found in the pre-configured database to form a binary structure, and the accuracy of SQL generation is improved through semantic completeness detection and associated information construction.
It significantly improves the accuracy of SQL generation, especially reduces field matching errors in SQL, and improves the ability to use in real and wide scenarios.
Smart Images

Figure CN119961284A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of large language models, and in particular to a method and device for enhancing the performance of natural language to SQL conversion based on a dynamic planner. Background Art
[0002] At present, for expert systems such as knowledge bases, the mapping and matching is mainly based on keyword rules. Although this method has high accuracy, it has poor generalization. It can only use specific questioning methods and can only replace specified words, otherwise it will lead to failure to match successfully. Generalization is the main problem of this method, which makes it difficult to use in real wide scenarios and can only be used in very specific scenarios.
[0003] For deep learning methods represented by RNN and BERT, while the generalization is improved, the accuracy and controllability are still very poor. They have used query and SQL to construct training samples, and some simple query SQL can be generated. However, if the user query is slightly complex, it will also face the problem of being unable to parse.
[0004] For LLM (Large Language Model) methods, although LLM's ability to understand queries has been greatly improved, the accuracy of SQL generated by directly sending queries and database information to LLM is still very low (can be directly executed or logically correct). The main problem is that generating SQL is a very difficult task, which requires not only a very deep understanding of the user's query, but also a considerable understanding of the business (database schema), and an understanding of SQL syntax knowledge. We found through experiments that this is a difficult task, and the error rate of LLM is very high. Therefore, even though the accuracy of generating SQL based on LLM has increased, there are still major problems, and it is still far from practical.
[0005] In order to solve the problem of LLM generating SQL, the study found that a significant factor in LLM generating SQL errors is that LLM cannot associate the query semantics with the correct fields of the database. Summary of the invention
[0006] The technical problem to be solved by the present invention is how to associate query semantics with the correct fields of a database; in view of this, the present invention provides a method and device for enhancing the performance of natural language to SQL conversion based on a dynamic planner.
[0007] The technical solution adopted by the present invention is a method for enhancing the performance of natural language to SQL conversion based on a dynamic planner, comprising:
[0008] Step S1, in response to a user query, converting the query question into a corresponding grammatical structure;
[0009] Step S2, for each segment of the grammatical structure, find corresponding associated information in a pre-configured database to form a tuple structure;
[0010] Step S3, determining whether the two-tuple structure is semantically complete, if yes, executing step S4, otherwise repeating step S2;
[0011] Step S4, the user query, the grammatical structure, the associated information, and the tuple structure are summarized and encapsulated for further generating an SQL statement.
[0012] In one embodiment, the step of finding corresponding associated information in a preconfigured database for each segment of the grammatical structure to form a tuple structure further includes:
[0013] When the corresponding association information cannot be found in the pre-configured database, matching association information is constructed for the current structure fragment.
[0014] In one embodiment, in response to a user query, converting the query question into a corresponding grammatical structure includes:
[0015] Using a word segmenter to segment the user query to generate multiple segments;
[0016] Use LLM to determine the part of speech in each segment;
[0017] LLM is further used to determine the modification relationship between the segments according to the segments and the corresponding parts of speech, and to construct a grammatical structure.
[0018] In one embodiment, the step of finding corresponding associated information in a preconfigured database for each segment of the grammatical structure to form a tuple structure includes:
[0019] Construct corpus database:
[0020] Determine the data table of the segment matching from the corpus database:
[0021] Determine the matching association information in the data table:
[0022] The parsed fragments and associated information are sorted and merged into tuples.
[0023] In one embodiment, determining whether the two-tuple structure is semantically complete includes:
[0024] Extracting the segment corresponding to the main part of speech specified in the user query;
[0025] Determine whether the association information mapping of the fragment exists, and if not, construct matching association information for the current structure fragment;
[0026] It is determined whether the number of association information mappings of the fragment is less than a preset number, and if not, matching association information is constructed for the current structure fragment.
[0027] In one embodiment, the user query, the grammatical structure, the associated information, and the tuple structure are aggregated and encapsulated to further generate an SQL statement, including:
[0028] Integrate the original user query, the corresponding data table, the grammatical structure, and the associated information mapping;
[0029] Merge the information in the corpus database with the integrated information;
[0030] Send the merged data to LLM to generate output SQL.
[0031] Another aspect of the present invention further provides a device for enhancing the performance of natural language to SQL conversion based on a dynamic planner, comprising:
[0032] A conversion unit, configured to convert the query question into a corresponding grammatical structure in response to a user query;
[0033] an associating unit configured to find corresponding associating information in a pre-configured database for each segment of the grammatical structure to form a tuple structure;
[0034] a detection unit configured to determine whether the two-tuple structure is semantically complete, and if so, perform a generation process, otherwise repeat an association process;
[0035] The generating unit is configured to summarize and encapsulate the user query, the grammatical structure, the associated information, and the tuple structure for further generating an SQL statement.
[0036] In one embodiment, the device further comprises:
[0037] The construction unit is configured to construct matching association information for the current structure fragment when the corresponding association information cannot be found in the pre-configured database.
[0038] Another aspect of the present invention provides an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the steps of the method for enhancing the performance of natural language to SQL conversion based on a dynamic planner as described in any one of the above items.
[0039] Another aspect of the present invention further provides a computer storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the steps of the method for enhancing the performance of natural language to SQL conversion based on a dynamic planner as described in any one of the above items are implemented.
[0040] Compared with the prior art, the present invention has at least the following advantages:
[0041] The present invention significantly improves the SQL generation accuracy, especially reduces field matching errors in SQL, by obtaining the mapping relationship between user queries and data tables and the semantic expression relationship of user queries in advance. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 A schematic diagram of a method flow chart for enhancing the performance of natural language to SQL conversion based on a dynamic planner according to an embodiment of the present invention;
[0043] Figure 2 A schematic diagram of an implementation architecture of a method for enhancing natural language to SQL performance based on a dynamic planner according to an embodiment of the present invention;
[0044] Figure 3 A schematic diagram of a grammatical structure (Boolean expression) according to an embodiment of the present invention;
[0045] Figure 4 A schematic diagram of semantic distribution according to an embodiment of the present invention;
[0046] Figure 5 A schematic diagram of the composition of a device for enhancing the performance of natural language to SQL conversion based on a dynamic planner according to an embodiment of the present invention;
[0047] Figure 6 FIG. 1 is a schematic diagram of an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0048] In order to further explain the technical means and effects adopted by the present invention to achieve the predetermined purpose, the present invention is described in detail below in conjunction with the accompanying drawings and preferred embodiments.
[0049] Unless otherwise defined, all terms (including technical terms and scientific terms) used in this article have the same meaning as commonly understood by ordinary technicians in the field to which this application belongs. It should also be understood that terms (such as terms defined in commonly used dictionaries) should be interpreted as having the same meaning as their meaning in the context of the relevant technology, and will not be interpreted in an idealized or overly formal sense unless explicitly defined in this article.
[0050] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.
[0051] In an embodiment of the present invention, a method for improving the performance of natural language to SQL conversion based on a dynamic planner is provided. Figure 1 As shown, including:
[0052] Step S1, in response to a user query, converting the query question into a corresponding grammatical structure;
[0053] Step S2, for each segment of the grammatical structure, find corresponding associated information in a pre-configured database to form a tuple structure;
[0054] Step S3, determining whether the two-tuple structure is semantically complete, if yes, executing step S4, otherwise repeating step S2;
[0055] Step S4, the user query, the grammatical structure, the associated information, and the tuple structure are summarized and encapsulated for further generating an SQL statement.
[0056] In this embodiment, the step of finding corresponding associated information in a pre-configured database for each segment of the grammatical structure to form a tuple structure further includes:
[0057] When the corresponding association information cannot be found in the pre-configured database, matching association information is constructed for the current structure fragment.
[0058] In this embodiment, in response to a user query, converting the query question into a corresponding grammatical structure includes:
[0059] Using a word segmenter to segment the user query to generate multiple segments;
[0060] Use LLM to determine the part of speech in each segment;
[0061] LLM is further used to determine the modification relationship between the segments according to the segments and the corresponding parts of speech, and to construct a grammatical structure.
[0062] In this embodiment, for each segment of the grammatical structure, corresponding associated information is found in a pre-configured database to form a tuple structure, including:
[0063] Constructing corpus database:
[0064] Determine the data table of the segment matching from the corpus database:
[0065] Determine the matching association information in the data table:
[0066] The parsed fragments and associated information are sorted and merged into tuples.
[0067] In this embodiment, determining whether the binary structure is semantically complete includes:
[0068] Extracting the segment corresponding to the main part of speech specified in the user query;
[0069] Determine whether the association information mapping of the fragment exists, and if not, construct matching association information for the current structure fragment;
[0070] It is determined whether the number of association information mappings of the fragment is less than a preset number, and if not, matching association information is constructed for the current structure fragment.
[0071] In this embodiment, the user query, the grammatical structure, the associated information, and the tuple structure are summarized and encapsulated to further generate an SQL statement, including:
[0072] Integrate the original user query, the corresponding data table, the grammatical structure, and the associated information mapping;
[0073] Merge the information in the corpus database with the integrated information;
[0074] Send the merged data to LLM to generate output SQL.
[0075] The following will be combined Figure 2 The method provided in this embodiment is described in detail.
[0076] Step S1, in response to a user query, convert the query question into a corresponding grammatical structure.
[0077] In this embodiment, the processing body of this step can be abstractly summarized as a "semantic parsing layer", and its steps include:
[0078] For example, if the user's query is "What was the best-selling product in Beijing last year?", the standard algorithm should be:
[0079] ■First, segment the query into words, and turn it into "what was the best product in Beijing's sales volume last year";
[0080] ■ Mark the subject, predicate, object, attributive, adverbial, complement, "last year [time attribute] / Beijing [place attribute] / sales volume [modifying attribute] / the best [strength attribute] / product [subject] / what is [auxiliary word]";
[0081] ■ Arrange the relationship between various language components to form a grammatical structure tree;
[0082] ■Form the Boolean expression product[modifying attribute = "sales volume"]&&sales volume[time attribute = "last year"]&&sales volume[location attribute = "Beijing"]&&sales volume[strength attribute = "best"].
[0083] Among them, the word segmentation can be performed with the help of a word segmenter, and each query fragment after word segmentation is called a token;
[0084] Specifically, LLM can be used to perform lexical analysis to determine the subject, predicate, object, attributive, adverbial, and complement status of each token;
[0085] Furthermore, LLM is used to further assist in determining the types of attributes: time attributes, place attributes, modifying attributes and other attribute modification relationships, and construct a grammatical structure tree.
[0086] refer to Figure 3 , construct the two connected nodes in the graph into a ternary expression and combine them into a Boolean expression.
[0087] Step S2: For each segment of the grammatical structure, find the corresponding associated information in a pre-configured database to form a tuple structure.
[0088] In this embodiment, the processing body of this step can be abstractly summarized as a "token_match_field module", and its steps include:
[0089] The main function of the token_match_field module is to try to associate the nodes of the syntax tree with the fields of the database schema [schema is the metadata information of the database, including the name of the table, field name, field type, and field description], determine the field mapping information for subsequent SQL generation, and help LLM reduce the difficulty of generating SQL.
[0090] Specifically, this module uses LLM as a semantic analysis tool, constructs a suitable prompt template, embeds tokens and schemas, and finally determines the mapping relationship between tokens and fields. The main methods are as follows:
[0091] ■Constructing a few shot corpus:
[0092] ■ Collect 100 common business queries and corresponding SQL;
[0093] ■ Parse and restore the required schema from the corresponding SQL;
[0094] ■Construct a number of bigram corpora as the data of few shots;
[0095] ■Determine the appropriate schema tabel:
[0096] ■ Combine table name, table description, field information, field type, field description and other information into database compression information;
[0097] ■ Take some examples from the few shot corpus above, merge the schema compression information above, and then merge the semantic parsing information to construct a prompt;
[0098] ■Parse the required table from LLM;
[0099] ■Determine the appropriate field in the table:
[0100] ■ Combine table name, table description, field information, field type, field description and other information into table compression information;
[0101] ■ Take some examples from the few shot corpus above, merge the table compression information above, and then merge the semantic parsing information to construct a prompt;
[0102] ■ Parse the required filed from LLM;
[0103] ■Organize the parsed token and filed information and merge them into two tuples.
[0104] There are some situations in the above processing, for example, a token and filed cannot be mapped, or a token and multiple fileds form a mapping, and this binary relationship is many-to-many.
[0105] In some possible implementations, it is necessary to reconstruct the two-tuples that lack accurate mapping relationships. In this embodiment, the processing subject of this step can be abstractly summarized as a "sub-query constructor".
[0106] This module is mainly used to supplement and improve the mapping relationship between token and field. Because of the matching process between token and original schemafiled, some tokens may not be mapped to any field. In this case, this module is required to create a virtual field for this token. This sub-query constructor is prepared for creating a virtual field.
[0107] ■Construct a few shot sub-query corpus;
[0108] ■Manually collect 100 query templates and mark them to remove the subject to form a replacement symbol;
[0109] ■ Paraphrase the query for tokens that do not match filed, select a suitable query template from the above sub-query corpus, use the semantic structure tree information to paraphrase the query, and expand the unmarked token. This is equivalent to paraphrasing the token into a more detailed query after paraphrase expansion;
[0110] ■Use the above-mentioned "semantic analysis" unit and the "token match field" unit to reprocess the new query and finally obtain the mapping relationship between the new token and field;
[0111] ■These new mapping relationships are finally used as the mapping description of the token;
[0112] ■This module may not be able to match a suitable filed description for a token, or it may match multiple suitable filed descriptions for a token. This is a many-to-many relationship.
[0113] Step S3, determining whether the bigram structure is semantically complete.
[0114] In this embodiment, the processing subject of this step can be abstractly summarized as a "semantic determiner".
[0115] The semantic determiner determines the token-filed mapping relationship found to see whether it is semantically complete. If it is semantically complete, it will generate the subsequent SQL. If it is not semantically complete, it will continue to execute the "sub-query construction" unit. The specific execution steps include:
[0116] ■ Extract the token components of the main parts of speech from the query analysis results of the "semantic analysis" unit;
[0117] ■ Determine whether the filed mapping of the main part of speech exists. If not, continue with the "subquery construction" unit. A token can be constructed as a subquery up to 3 times;
[0118] ■ Determine whether the number of filed mappings of the main part of speech is less than or equal to 3. If not, proceed to the "subquery construction" unit;
[0119] In general, the role of this module is to determine whether the fild mapping of a token is complete and relatively accurate, and to recalculate tokens that do not meet the conditions.
[0120] Step S4, summarizing and encapsulating the user query, grammatical structure, associated information, and bigram structure for further generating SQL statements.
[0121] In this embodiment, the processing subject of this step can be abstractly summarized as a "prompt construction module".
[0122] refer to Figure 4 This part specifically aggregates and encapsulates various information such as the original query, the original database schema, the query parsing syntax tree information, the token field mapping, etc. into a suitable prompt, and generates SQL with the help of LLM, including:
[0123] ■Constructing a few shot corpus:
[0124] ■ Collect 100 common business queries and their corresponding SQL statements, and parse and restore the required schema from the corresponding SQL statements;
[0125] ■Construct some bigram corpora (query-scheme is used as the question, and sql is used as the answer to form a question-answer pair) as the data for few shots.
[0126] ■SQL generation based on LLM:
[0127] ■ Organize the original query, original database schema, query parsing syntax tree information, token field mapping and other information together;
[0128] ■ Remove some examples from the few shot corpus above, and then merge them with the information in the previous step into a prompt; (i.e., from the question-answer pairs above, extract 5 question-answer pairs as examples, and splice them into the prompt to form a large prompt)
[0129] ■Send the prompt to LLM and get the output sql.
[0130] Compared with the prior art, the advantages of the embodiments of the present invention are:
[0131] This embodiment significantly improves the SQL generation accuracy, especially reduces field matching errors in SQL, by obtaining the mapping relationship between user queries and data tables and the semantic expression relationship of user queries in advance.
[0132] The second embodiment of the present invention corresponds to the first embodiment. Figure 5 As shown, this embodiment introduces a device for enhancing the performance of natural language to SQL conversion based on a dynamic planner, including the following components:
[0133] A conversion unit, configured to convert the query question into a corresponding grammatical structure in response to a user query;
[0134] an associating unit configured to find corresponding associating information in a pre-configured database for each segment of the grammatical structure to form a tuple structure;
[0135] a detection unit configured to determine whether the two-tuple structure is semantically complete, and if so, perform a generation process, otherwise repeat an association process;
[0136] The generating unit is configured to summarize and encapsulate the user query, the grammatical structure, the associated information, and the tuple structure for further generating an SQL statement.
[0137] In this embodiment, the device further includes:
[0138] The construction unit is configured to construct matching association information for the current structure fragment when the corresponding association information cannot be found in the pre-configured database.
[0139] A third embodiment of the present invention is an electronic device, such as Figure 6 As shown, it can be understood as a physical device, including a processor and a memory storing instructions executable by the processor. When the instructions are executed by the processor, the following operations are performed:
[0140] Step S1, in response to a user query, converting the query question into a corresponding grammatical structure;
[0141] Step S2, for each segment of the grammatical structure, find corresponding associated information in a pre-configured database to form a tuple structure;
[0142] Step S3, determining whether the two-tuple structure is semantically complete, if yes, executing step S4, otherwise repeating step S2;
[0143] Step S4, the user query, the grammatical structure, the associated information, and the tuple structure are summarized and encapsulated for further generating an SQL statement.
[0144] The fourth embodiment of the present invention, the process of the method for enhancing the performance of natural language to SQL conversion based on a dynamic planner in this embodiment is the same as that of the first, second or third embodiment, except that, in terms of engineering implementation, this embodiment can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the method of the present invention can be embodied in the form of a computer software product, which is stored in a storage medium (such as ROM / RAM, a disk, or an optical disk), and includes a number of instructions for enabling a device to execute the method described in the embodiment of the present invention.
[0145] Through the description of the specific implementation methods, a deeper and more specific understanding of the technical means and effects adopted by the present invention to achieve the predetermined purpose should be obtained. However, the accompanying drawings are only for reference and illustration purposes and are not intended to limit the present invention.
Claims
1. A method for enhancing the performance of natural language to SQL conversion based on a dynamic planner, characterized in that: include: Step S1, in response to a user query, converting the query question into a corresponding grammatical structure; Step S2, for each segment of the grammatical structure, find corresponding associated information in a pre-configured database to form a tuple structure; Step S3, determining whether the two-tuple structure is semantically complete, if yes, executing step S4, otherwise repeating step S2; Step S4, the user query, the grammatical structure, the associated information, and the tuple structure are summarized and encapsulated for further generating an SQL statement.
2. The method for enhancing the performance of natural language to SQL conversion based on a dynamic planner according to claim 1, characterized in that: The step of finding corresponding associated information in a pre-configured database for each segment of the grammatical structure to form a tuple structure further includes: When the corresponding association information cannot be found in the pre-configured database, matching association information is constructed for the current structure fragment.
3. The method for enhancing the performance of natural language to SQL conversion based on a dynamic planner according to claim 2, characterized in that: The step of converting the query question into a corresponding grammatical structure in response to the user query includes: Using a word segmenter to segment the user query to generate multiple segments; Use LLM to determine the part of speech in each segment; LLM is further used to determine the modification relationship between the segments according to the segments and the corresponding parts of speech, and to construct a grammatical structure.
4. The method for enhancing the performance of natural language to SQL conversion based on a dynamic planner according to claim 3, characterized in that: The step of finding corresponding associated information in a pre-configured database for each segment of the grammatical structure to form a tuple structure includes: Constructing corpus database: Determine the data table of the segment matching from the corpus database: Determine the matching association information in the data table: The parsed fragments and associated information are sorted and merged into tuples.
5. The method for enhancing the performance of natural language to SQL conversion based on a dynamic planner according to claim 4, characterized in that: The determining whether the binary structure has semantic completeness includes: Extracting the segment corresponding to the main part of speech specified in the user query; Determine whether the association information mapping of the fragment exists, and if not, construct matching association information for the current structure fragment; It is determined whether the number of association information mappings of the fragment is less than a preset number, and if not, matching association information is constructed for the current structure fragment.
6. The method for enhancing the performance of natural language to SQL conversion based on a dynamic planner according to claim 5, characterized in that: The user query, the grammatical structure, the associated information, and the tuple structure are summarized and encapsulated to further generate an SQL statement, including: Integrate the original user query, the corresponding data table, the grammatical structure, and the associated information mapping; Merge the information in the corpus database with the integrated information; Send the merged data to LLM to generate output SQL.
7. A device for enhancing the performance of natural language to SQL conversion based on a dynamic planner, characterized in that: include: A conversion unit, configured to convert the query question into a corresponding grammatical structure in response to a user query; an associating unit configured to find corresponding associating information in a pre-configured database for each segment of the grammatical structure to form a tuple structure; a detection unit configured to determine whether the two-tuple structure is semantically complete, and if so, perform a generation process, otherwise repeat an association process; The generating unit is configured to summarize and encapsulate the user query, the grammatical structure, the associated information, and the tuple structure for further generating an SQL statement.
8. The device for enhancing the performance of natural language to SQL conversion based on a dynamic planner according to claim 7, characterized in that: The device also includes: The construction unit is configured to construct matching association information for the current structure fragment when the corresponding association information cannot be found in the pre-configured database.
9. An electronic device, characterized in that: The electronic device comprises: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the steps of the method for enhancing the performance of natural language to SQL conversion based on a dynamic planner as claimed in any one of claims 1 to 6.
10. A computer storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the steps of the method for enhancing the performance of natural language to SQL conversion based on a dynamic planner as claimed in any one of claims 1 to 6 are implemented.