Training method of database query model and database query system

By generating and splitting SQL statements, using the language transformation model to generate and filter natural language and SQL clauses, and iteratively update the training set, the existing database query model training methods are solved, and the complex instruction processing capability and accuracy of the model are improved.

CN119848537APending Publication Date: 2025-04-18ANHUI YUNSEN INTERNET OF THINGS TECH
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411774335.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-05
Publication Date
2025-04-18

AI Technical Summary

Technical Problem

The existing database query model training methods are costly and inefficient in training, and have poor understanding and conversion functions for complex instructions and multiple rounds of instructions, resulting in low robustness and accuracy.

Method used

By generating the target SQL statements of the target database, split them into SQL clauses, use the SQL-to-NL and NL-to-SQL models to generate natural language and inverse SQL clauses, calculate the similarity and filter the correct data, iteratively update the model training set, and generate the target training set for training the NL-to-SQL model.

Benefits of technology

It reduces the intensity of manual operation, improves the accuracy of query conversion of complex instructions by the NL-to-SQL model, enhances the model's retrieval accuracy and the ability to translate and convert nested out-of-order natural statements, and significantly reduces the difficulty of ordinary people using the query system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119848537A_ABST
    Figure CN119848537A_ABST
Patent Text Reader

Abstract

The invention relates to a training method of a database query model and a database query system.The training method of the database query model comprises the steps that a plurality of target SQL statements adopted by a target database are generated, and the target SQL statements are split to obtain a plurality of target SQL clauses; the method comprises the following steps: preliminarily generating a query pair through an SQL-to-NL model and an NL-to-SQL model, screening wrong and unmatched data through iteration, leaving correct data and generating a new training set, and training the NL-to-SQL model through the new training set, thereby obtaining the trained NL-to-SQL model. And in the training process, manual screening and input of a large amount of data are not needed, the manual operation intensity is greatly reduced, and the problems that an existing database query model training method is high in cost and low in training efficiency are solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of model training, and in particular to a database query model training method and a database query system. Background Art

[0002] With the rapid development of artificial intelligence and machine learning, researchers have gradually realized the convenience of natural language queries, especially in database access and operation. By converting users' natural language requests into corresponding SQL statements, the database interaction process can be greatly simplified, allowing non-professional users to easily obtain the required information. In this regard, most experiments use open source databases for training, such as MySQL, PostgreSQL, etc. These open source databases not only provide rich features and flexible usage, but also have extensive community support.

[0003] However, the existing database query model training method requires manual screening of a large number of query language pairs as data sets for model training, which consumes a lot of labor costs and is very time-consuming. Therefore, the current database query model training method has the problems of high training cost and low training efficiency.

[0004] In addition, the current database query model has poor understanding and conversion capabilities for complex instructions and multiple rounds of instructions, resulting in poor robustness and low accuracy of the current database query model.

[0005] There is currently no effective solution to the problem of high cost and low training efficiency of current database query model training methods. Summary of the invention

[0006] The present invention provides a database query model training method and a database query system to solve the problems of high cost and low training efficiency of the current database query model training method.

[0007] In a first aspect, the present invention provides a method for training a database query model, comprising:

[0008] Step A: Generate several target SQL statements used by the target database;

[0009] Step B: splitting the target SQL statements into several target SQL clauses;

[0010] Step C: Generate a target natural sentence corresponding to each target SQL clause through a SQL-to-NL model, and generate a reverse SQL clause corresponding to each target natural sentence through an NL-to-SQL model, wherein the SQL-to-NL model and the NL-to-SQL model are pre-trained using open source datasets respectively;

[0011] Step D: Calculate the similarity between the target SQL clauses and the inverse SQL clauses corresponding to each group, filter out the inverse SQL clauses whose similarity is less than the set first threshold, form query pairs with the remaining inverse SQL clauses and their corresponding natural statements, and combine several query pairs to obtain the target data set;

[0012] Step E: Taking the union of the open source dataset and the target dataset to obtain an updated training set, and updating and training the SQL-to-NL and NL-to-SQL models by using the updated training set;

[0013] Iteratively execute step C, step D, and step E, and gradually increase the first threshold in the iterative process and filter out the inferred SQL clauses whose similarity is less than the increased first threshold, until the average similarity between the target SQL clauses and the inferred SQL clauses corresponding to each group is greater than the second threshold;

[0014] Step F: Generate a target training set according to each group of the corresponding reverse SQL clauses and the target natural sentences, wherein the target training set is used to train the NL-to-SQL model.

[0015] In a second aspect, the present invention provides a training system for a database query model, comprising:

[0016] A conversion module is used to generate several target SQL statements used by the target database;

[0017] A splitting module, used for splitting the target SQL statements into a plurality of target SQL clauses;

[0018] A conversion module, used to generate a target natural sentence corresponding to each target SQL clause through a SQL-to-NL model, and to generate a reverse SQL clause corresponding to each target natural sentence through an NL-to-SQL model, wherein the SQL-to-NL model and the NL-to-SQL model are pre-trained using open source datasets respectively;

[0019] A detection module is used to calculate the similarity between each group of corresponding target SQL clauses and inverse SQL clauses, filter out the inverse SQL clauses whose similarity is less than a set first threshold, and obtain the target data set;

[0020] A merging module, used to take the union of the open source dataset and the target dataset, and update the training SQL-to-NL and NL-to-SQL models;

[0021] A generating module is used to generate a target training set according to each group of the corresponding reverse SQL clauses and the target natural sentences, wherein the target training set is used to train the NL-to-SQL model.

[0022] Compared with the related art, the present invention has the following beneficial effects:

[0023] 1. Initially generate query pairs through SQL-to-NL model and NL-to-SQL model, then iteratively filter out erroneous and mismatched data, leaving correct data to generate new training sets, train NL-to-SQL model with new training sets, and obtain trained NL-to-SQL model. In the training process, there is no need to manually screen and input a large amount of data, which greatly reduces the intensity of manual operation and solves the problem of high cost and low training efficiency of existing database query model training methods.

[0024] 2. The present invention splits a complete single target SQL statement into multiple SQL clauses, and then forms a training set with query pairs obtained by randomly mixing multiple SQL clauses. The NL-to-SQL model is further trained with the combined training set, which can effectively improve the query conversion accuracy of the NL-to-SQL model for complex instructions, thereby improving the retrieval accuracy of the NL-to-SQL model. The training process is completed by machine learning, reducing the intensity of manual labor.

[0025] 3. The present invention uses a nested dialogue structure, takes the newly added SQL query statement and the target natural language corresponding to the newly added reverse SQL clause as a query pair, obtains a connection training set to train the NL-to-SQL model, thereby improving the translation and conversion ability of the NL-to-SQL model for nested disordered natural sentences. In this way, the machine can automatically learn the nested dialogue logic of humans, make the information of human-computer interaction more accurate, and significantly reduce the difficulty for ordinary people to use the query system.

[0026] Details of one or more embodiments of the present application are set forth in the following drawings and description to make other features, objects, and advantages of the present application more readily apparent. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] Figure 1 It is a flow chart of the training method of the database query model of the present invention;

[0028] Figure 2 It is a flow chart for converting SQL statements and natural statements. DETAILED DESCRIPTION

[0029] In order to more clearly understand the purpose, technical solutions and advantages of the present application, the present application is described and illustrated below in conjunction with the accompanying drawings and embodiments.

[0030] Unless otherwise defined, the technical terms or scientific terms involved in this application shall have the general meaning understood by people with ordinary skills in the technical field to which this application belongs. The words "one", "a", "a", "the", "these" and the like in this application do not represent quantitative restrictions, and they can be singular or plural. The terms "include", "comprise", "have" and any variants thereof involved in this application are intended to cover non-exclusive inclusions; for example, a process, method and system, product or device comprising a series of steps or modules (units) is not limited to the listed steps or modules (units), but may include unlisted steps or modules (units), or may include other steps or modules (units) inherent to these processes, methods, products or devices. The words "connect", "connected", "coupled" and the like involved in this application are not limited to physical or mechanical connections, but may include electrical connections, whether directly or indirectly. The "multiple" involved in this application refers to two or more. "And / or" describes the association relationship of associated objects, indicating that there can be three relationships. For example, "A and / or B" can mean: A exists alone, A and B exist at the same time, and B exists alone. Usually, the character " / " indicates that the objects associated with each other are in an "or" relationship. The terms "first", "second", "third", etc. involved in this application are only used to distinguish similar objects and do not represent a specific ordering of the objects.

[0031] The technical background of natural language conversion to SQL (Structured Query Language) statements mainly revolves around the combination of natural language processing (NLP) and database queries. With the rapid development of artificial intelligence and machine learning, researchers have gradually realized the convenience of natural language queries, especially in database access and operation. By converting users' natural language requests into corresponding SQL statements, the database interaction process can be greatly simplified, allowing non-professional users to easily obtain the required information.

[0032] In this regard, existing technologies usually use open source databases for training, such as MySQL, PostgreSQL, etc. These open source databases not only provide rich features and flexible usage methods, but also have extensive community support. However, the trained models are often targeted at specific database architectures and data sets, which makes them unable to be directly ported to other databases for use. The syntax differences, data structures, and types between different databases mean that a model that performs well in a specific environment may experience significant performance degradation or fail to correctly parse queries in other environments. Manually customizing a dataset consisting of query language pairs for training models for a specific database is very expensive and time-consuming.

[0033] In order to solve the above problems, a method for training a database query model is provided in an embodiment of the present invention, including: step A, step B, step C, step D, step E and step F.

[0034] Step A: Generate several target SQL statements used by the target database.

[0035] Specifically, the ASTG method (Alibaba SQL Translation Gateway) is used to convert SQL statements in the existing database into several target SQL statements used by the target database. The target database is a new database that the operator wants to build. Therefore, it is necessary to train a language conversion model corresponding to the target database to convert the natural statements input by the user into SQL statements of the target database. The target SQL statements are the SQL statements used by the target database.

[0036] It should be noted that generating an SQL statement for a database is a relatively mature technology at present, so step A is not specifically introduced in the present invention.

[0037] Step B: Split several target SQL statements into several target SQL clauses.

[0038] The target SQL statement is a whole, complete sentence, and the target SQL clause is the smallest component unit of the target SQL statement. Multiple target SQL clauses are spliced ​​together in a specific order to form a target SQL statement. Conversely, splitting a target SQL statement can get multiple target SQL clauses. It should be noted that different target SQL statements may be composed of different numbers of target SQL clauses, so the length of the target SQL statement structure may be inconsistent.

[0039] It should be noted that the main reason for executing this step is that there may be relatively complex SQL statements in the target SQL statement, such as nested structures, etc. These SQL statements need to be split.

[0040] Therefore, in some embodiments, step B specifically includes step B1 and step B2.

[0041] Step B1, determining a target SQL statement having a nested structure.

[0042] Step B2: split the target SQL statement with the nested structure.

[0043] First, for nested SQL statements, a top-down decomposition method is required. In the nested structure, the query clause at the bottom level is generated first and treated as an independent block. The SQL fragments generated at the bottom level are used as building blocks for the upper-level SQL, and are gradually combined upward until a complete query sentence is generated. For example, if the innermost query is a SELECT statement and the outer level has a WHERE filter condition, then the inner SELECT is processed first during generation, and then its result is combined with the outer WHERE as a whole clause.

[0044] The clauses of the above nested structure are split again according to keywords, including SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING and JOIN. When a keyword is encountered, the SQL statement is split and these clauses are sorted according to the database query method to ensure the semantic consistency in the subsequent training of NL2SQL. The sorting method is as follows:

[0045] WHERE->GROUP BY->HAVING->SELECT->ORDER BY->LIMIT.

[0046] Finally, the disassembled target SQL statement and the undisassembled simple target SQL statement are used as target SQL clauses.

[0047] Step C: Generate target natural sentences corresponding to each target SQL clause through the SQL-to-NL model, and generate reverse SQL clauses corresponding to each target natural sentence through the NL-to-SQL model. The SQL-to-NL model and the NL-to-SQL model are pre-trained using open source datasets respectively.

[0048] Specifically, the SQL-to-NL model and the NL-to-SQL model are pre-trained using open source datasets. The language conversion model trained using the open source dataset has certain language conversion capabilities. Since the open source dataset is not a model training set for the target database, the trained language conversion model has large errors when converting the target SQL statement and the natural statement.

[0049] The SQL-to-NL model is a model that converts SQL language into natural language, and the NL-to-SQL model is a model that converts natural language into SQL language. Exemplarily, the SQL-to-NL model can be a Seq2Seq or Transformer model trained based on Spider or WikiSQL database.

[0050] During training, the SQL-to-NL model first converts the target SQL clause into a target natural sentence, and then the NL-to-SQL model converts the target natural sentence into a reverse SQL clause. In this process, since the SQL-to-NL model and the NL-to-SQL model have not been trained for the target database or have been trained sufficiently, they will inevitably produce some erroneous data when performing language conversion, that is, they fail to perform correct language conversion. At this time, the erroneous data can be filtered out by comparing the corresponding target SQL clauses and reverse SQL clauses. For example, the SQL-to-NL model converts the target SQL clause A into natural language B, and the NL-to-SQL model converts natural language B into reverse SQL clause C. If the target SQL clause A and the reverse SQL clause C are exactly the same or have a high degree of similarity, it can be considered that the language conversion process of the two models is relatively accurate, and the above statement pair can be retained. If the target SQL clause A and the reverse SQL clause C are very different, it means that the language conversion process of at least one of the models is wrong and will result in a large error.

[0051] Step D: Calculate the similarity between each group of corresponding target SQL clauses and inverse SQL clauses, filter out the inverse SQL clauses whose similarity is less than the set first threshold, form query pairs with the remaining inverse SQL clauses and their corresponding natural statements, and combine several query pairs to obtain the target data set.

[0052] Specifically, after the NL-to-SQL model converts the target natural statement into a reverse SQL statement, the similarity between the target SQL clause and the reverse SQL clause is obtained by comparing the structures between the target SQL clause and the reverse SQL clause, and the reverse SQL clauses whose similarity is less than a set first threshold are removed. The remaining reverse SQL clauses and their corresponding natural statements are combined into query pairs, and several query pairs are combined to obtain a target data set. The target data set here is used for the next step of SQL-to-NL and NL-to-SQL model training, and the target data set is equivalent to the purified data set.

[0053] Since there are different substructures in SQL statements, each reverse SQL clause belongs to at least one of the structures. Therefore, when comparing the target SQL clause and the reverse SQL clause, the similarity can be calculated in different ways according to the clause structure. Among them, the similarity is structural similarity, which is divided into SELECT substructure similarity, WHERE substructure similarity, GROUP BY substructure similarity, HAVING substructure similarity, ORDER BY substructure similarity and LIMIT substructure similarity.

[0054] Specifically, for the SELECT substructure, if the generated reverse SQL clause completely contains all the columns of the target SQL clause, the score is full marks. If there are redundant or missing columns, the score is adjusted according to the matching ratio. The similarity calculation of the WHERE substructure allows slight differences in the condition values, and the score is adjusted according to the matching ratio. The GROUP BY and HAVING substructures are scored based on the coverage of the fields and conditions. ORDER BY considers the matching degree of the sorting fields and directions to give a score. In the LIMIT substructure, the limit value of the generated SQL is compared with the target and adjusted according to the degree of difference. Finally, the system calculates the overall similarity based on the similarity scores of each substructure.

[0055] It should be noted that the above is an exemplary description of how to calculate the similarity between the target SQL clause and the reverse SQL clause in the present invention, and those skilled in the art may also use other methods to compare the similarity between the target SQL clause and the reverse SQL clause.

[0056] Step E: The open source dataset and the target dataset are combined to obtain an updated training set, and the SQL-to-NL and NL-to-SQL models are updated and trained by the updated training set.

[0057] The open source dataset is an existing general dataset. By taking the union of the open source dataset and the target dataset obtained above, a new dataset, namely the updated training set, can be obtained. Using the updated training set to train the SQL-to-NL and NL-to-SQL models can further improve the language conversion capabilities of the two models for the target database, because the updated training set contains query statement pairs for the target database. The two models after update training can improve the conversion accuracy between the target SQL clause and the natural statement.

[0058] In order to continuously improve the language conversion capability of the SQL-to-NL and NL-to-SQL models for the target database, iteratively execute steps C, D, and E, and gradually increase the first threshold during the iteration process and filter out the inferred SQL clauses whose similarity is less than the increased first threshold, until the average similarity between the corresponding target SQL clauses and the inferred SQL clauses in each group is greater than the second threshold.

[0059] Specifically, in the first round of screening, a lower threshold can be used. At this time, the first threshold is e, based on the amount of data generated for the query data pairs. Each subsequent execution of this step can gradually increase the first threshold (for example, in the kth iteration, the first threshold used is e+k*h%, where h% is the increase in each threshold) to gradually retain query data pairs with higher accuracy for the target database. At the same time, a termination condition is set. When the average similarity threshold of all clauses reaches the second threshold, it means that the above two models already have good language conversion capabilities for the target database.

[0060] Through the above iterative process, the two models can have better language conversion capabilities for the target database, and obtain query data pairs with higher accuracy, where each pair of query data includes the SQL statement of the target database and the corresponding natural statement.

[0061] Step F: Generate a target training set based on the groups of corresponding inverse SQL clauses and target natural sentences. The target training set is used to train the NL-to-SQL model.

[0062] In order to improve the translation capability of the NL-to-SQL model in various scenarios, different training sets can be generated based on groups of corresponding inverse SQL clauses and target natural sentences to train the NL-to-SQL model.

[0063] In some embodiments, the target training set includes a first target training subset. The first target training subset is for training of some simple natural sentences.

[0064] Generating a target training set based on each group of corresponding reverse SQL clauses and target natural sentences includes:

[0065] The arrangement of the corresponding reverse SQL clauses in step F is determined according to the arrangement of each target SQL clause in the target SQL statement to form a sequence SQL clause. The sequence SQL clause is the whole sentence obtained by arranging the reverse SQL clauses according to the arrangement order of the target SQL clauses in the target SQL statement.

[0066] If the reverse SQL clause corresponding to the target SQL clause is missing in the sequence SQL clause, the sequence SQL clause is separated into several defective sequence SQL clauses at the missing part, and the several defective sequence SQL clauses are combined in order to obtain a simple sorting clause, and the first target training subset is generated according to each group of corresponding simple sorting clauses and the target natural sentence.

[0067] Original sentence and split clauses:

[0068] 1.SELECT customer_id,SUM(order_amount)AS total_amount FROM orders.

[0069] Query the total order amount for each customer.

[0070] 2.WHERE order_date>'2023-01-01'.

[0071] The order date is after January 1, 2023.

[0072] 3. GROUP BY customer_id.

[0073] Group by customer ID.

[0074] 4.HAVING SUM(order_amount)>500.

[0075] Keep customers whose total order amount exceeds 500.

[0076] 5.ORDER BY total_amount DESC.

[0077] Sort by total amount from highest to lowest.

[0078] If clause 2 fails the similarity check with a threshold of e+k*h%, the above SQL statement can be decomposed into two complete sentences:

[0079] Sentence 1:

[0080] SELECT customer_id,SUM(order_amount)AS total_amount FROM orders.

[0081] Query the total order amount for each customer.

[0082] Sentence 2:

[0083] GROUP BY customer_id.

[0084] Group by customer ID.

[0085] HAVING SUM(order_amount)>500.

[0086] Keep customers whose total order amount exceeds 500.

[0087] ORDER BY total_amount DESC.

[0088] Sort by total amount from highest to lowest.

[0089] In a further embodiment, the target training set further includes a second target training subset. The second target training subset is obtained by arbitrarily arranging the reverse SQL clauses, can imitate the natural language of humans in real situations, and has real practicality.

[0090] Generating a target training set based on the corresponding reverse SQL clauses and target natural sentences also includes:

[0091] The inverse SQL clauses in the sequence SQL clauses are randomly arranged and combined to obtain complex sorting clauses, and a second target training subset is generated according to each group of corresponding complex sorting clauses and target natural sentences.

[0092] Taking the whole sentence 2 as an example, by splitting and combining it again, we can get the following data query pair.

[0093] Sentence 1:

[0094] GROUP BY customer_id HAVING SUM(order_amount)>500.

[0095] Group by customer ID and only keep customers whose total order amount exceeds 500.

[0096] Sentence 2:

[0097] HAVING SUM(order_amount)>500ORDER BY total_amount DESC.

[0098] Filter out customers whose total order amount exceeds 500, and sort them by total amount from high to low.

[0099] Sentence 3:

[0100] GROUP BY customer_id HAVING SUM(order_amount)>500ORDER BY total_amount DESC.

[0101] In a further embodiment, the target training set also includes a connection training set. The query pairs in the connection training set are nested data obtained by connecting the previous and next SQL statements. This enables the machine to automatically connect the first natural statement with the second natural statement when recognizing the second natural statement, and obtain a comprehensive SQL statement output. Through this nested statement training, the machine can understand the multi-level dialogue of humans and respond correctly, which greatly improves the intelligence of the machine. It reduces the difficulty of human-computer interaction and allows non-professionals to use the query system well.

[0102] Generating a target training set based on the corresponding reverse SQL clauses and target natural sentences also includes:

[0103] Several SQL query statement groups are generated according to the reverse SQL clauses. In each group of SQL query statement groups, a newly added SQL query statement is obtained by concatenating the previous SQL query statement and the newly added reverse SQL clause. The newly added SQL query statement and the target natural language corresponding to the newly added reverse SQL clause are used as query pairs to obtain a connection training set.

[0104] User: I want to see the total amount of each customer's orders.

[0105] SQL statement conversion system: SELECT customer_id, SUM(order_amount) AS total_amount FROM orders.

[0106] User: I only need order data after January 1, 2023.

[0107] SQL statement conversion system: SELECT customer_id, SUM(order_amount) AS total_amount FROM orders WHERE order_date>'2023-01-01'.

[0108] User: Please group the above data by customer ID.

[0109] SQL statement conversion system: SELECT customer_id, SUM(order_amount) AS total_amount FROM orders WHERE order_date>'2023-01-01' GROUP BY customer_id.

[0110] User: Only display customer records whose total order amount is greater than 500.

[0111] SQL statement conversion system: SELECT customer_id,SUM(order_amount)AS total_amountFROM orders WHERE order_date>'2023-01-01'GROUP BY customer_id HAVING SUM(order_amount)>500.

[0112] Users: Sort by total amount from high to low.

[0113] SQL statement conversion system: SELECT customer_id,SUM(order_amount)AS total_amountFROM orders WHERE order_date>'2023-01-01'GROUP BY customer_id HAVING SUM(order_amount)>500ORDER BY total_amount DESC.

[0114] During training, you can follow the mature training program for multi-round dialogues to train the NL-to-SQL model, which can effectively improve its ability to connect the context of multi-round dialogues, so as to achieve the disassembly of complex instructions and make it easier to use.

[0115] It can be seen from the above description that the target training set may include multiple parts, which should at least include the first target training subset. On this basis, the target training set may also include the second target training subset and the connection training set. Different implementation methods may be used for different training needs. For example, in some embodiments, the target training set only includes the first target training subset. In other embodiments, the target training set includes both the first target training subset and the second target training subset, or the target training set includes all of the above training sets.

[0116] In order to increase the sample size of model training, in some embodiments, the target training set and the open source data set are combined to form a final training set, and the final training set is used to train the NL-to-SQL model. The final training set is a collection of all correct data. By training the NL-to-SQL model with the first target training subset, the second target training subset, the connection training set and the open source data, a NL-to-SQL model that can effectively translate human natural language and allow the machine to accurately recognize it can be finally obtained. The NL-to-SQL model has the ability to translate and convert nested disordered natural sentences, so that the machine can automatically learn the nested dialogue logic of humans, making the information of human-computer interaction more accurate, and significantly reducing the difficulty of ordinary people using the query system.

[0117] Correspondingly, in some other embodiments, the target training set may be used alone to train the NL-to-SQL model.

[0118] As described above, at least one embodiment of each part of the training method for a database query model provided by the present invention has been described separately. It should be noted that each part of the training method for a database query model can be combined using different embodiments to form different embodiments of the training method for a database query model.

[0119] For example, in a specific overall embodiment, the training method of the database query model includes:

[0120] A. Generate SQL statements on a given database. Specifically, you can use the ASTG method or replace the SQL statements in the open source database with the SQL statements of the given database according to certain rules.

[0121] B. Secondly, after the SQL statement generation is completed, since there may be more complex SQL statements, such as nested SQL statements, etc., in the early stage, the SQL statements are split and the SQL-TEXT statements trained by the open source dataset model are used to generate the natural language corresponding to the split SQL. The splitting rules are as follows:

[0122] SQL statement decomposition steps

[0123] First, for nested SQL statements, a top-down decomposition method is required. In the nested structure, the query clause at the bottom level is generated first, and it is treated as an independent block. The SQL fragments generated at the bottom level are used as building blocks for the upper-level SQL, and are gradually combined upward until a complete query is generated. For example, if the innermost query is a SELECT statement, and the outer level has a WHERE filter condition, then the inner SELECT is processed first during generation, and then its result is combined with the outer WHERE as a whole clause.

[0124] The clauses of the above nested structure are split again according to keywords, including SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING and JOIN. When a keyword is encountered, the SQL statement is split and these clauses are sorted according to the database query method to facilitate the subsequent training of NL2SQL to ensure its semantic coherence. The following is the arrangement order of the SQL clauses in the whole sentence:

[0125] WHERE->GROUP BY->HAVING->SELECT->ORDER BY->LIMIT.

[0126] C. Use a pre-trained SQL-to-NL (SQL to natural language) model (such as a Seq2Seq or Transformer model trained based on a Spider or WikiSQL database) to generate the natural language corresponding to the above-split SQL query statements on a given database to form a query pair for the given database. This step is already relatively mature.

[0127] D. On a given database that has not been trained, the natural language generated by the model usually shows certain errors. In this case, the pre-trained NL-to-SQL (natural language to SQL) model is used to generate SQL statements for the natural language. By comparing the generated SQL statements with the execution results of the split target SQL, it is possible to confirm whether the intermediate natural statements are normal and filter out errors. The filtering method is as follows:

[0128] Each substructure compares the matching degree between the generated SQL and the target SQL item by item according to the key elements, and quantifies the matching effect through similarity scoring. For example, for the SELECT substructure, if the generated SQL completely contains all the columns of the target SQL, the score is 100%. If there are extra or missing columns, the score is adjusted according to the matching ratio. The similarity calculation of the WHERE substructure allows slight differences in the condition values. A higher score (such as 75%) can be obtained when the fields are consistent but the values ​​are different. GROUP BY is similar to HAVING, and is also scored based on the coverage of fields and conditions. ORDER BY considers the matching degree of the sorting fields and directions, and gives slightly lower scores for different directions. In the LIMIT substructure, if the limit value of the generated SQL matches the target, the score is full, otherwise it is adjusted according to the degree of difference. Finally, the system calculates the overall similarity based on the similarity scores of each substructure, and retains the SQL conversation data with high similarity for further optimization of the model.

[0129] In the first round of screening, a lower threshold e can be used to generate the amount of query data pairs. When this step is executed for the kth time, the screening threshold can be gradually increased to e+k*h%. h% is the increase in each threshold. At the same time, the termination condition is set. When the average similarity threshold of all clauses is > p1% and the success rate of clause generation is > p2%, step E is skipped and F is executed, which means that the above two models can achieve good results on the given data set.

[0130] E. Take the union of the open source datasets used to train the SQL-to-NL and NL-to-SQL models in steps C and D and the above-screened datasets, retrain the above two models, and use these two models to execute step D again.

[0131] F. When the average clause similarity threshold is greater than p1%, the selected query data pairs are manually checked for clauses to filter out erroneous content. Thus, query pairs with split clauses for a given data set can be obtained.

[0132] G. Arrange the clause sequence obtained in F according to the sentence structure (i.e., execution order) in step A. If there are missing clauses in the original sentence, split the sentence into independent sentences. The purpose of splitting is to combine them into more query data pairs in the next step. For example:

[0133] Original sentence and split clauses:

[0134] 1.SELECT customer_id,SUM(order_amount)AS total_amount FROM orders.

[0135] Query the total order amount for each customer.

[0136] 2.WHERE order_date>'2023-01-01'.

[0137] The order date is after January 1, 2023.

[0138] 3. GROUP BY customer_id.

[0139] Group by customer ID.

[0140] 4.HAVING SUM(order_amount)>500.

[0141] Keep customers whose total order amount exceeds 500.

[0142] 5.ORDER BY total_amount DESC.

[0143] Sort by total amount from highest to lowest.

[0144] If clause 2 fails the similarity check with a threshold of e+k*h%, the above SQL statement can be decomposed into two complete sentences:

[0145] Sentence 1:

[0146] SELECT customer_id,SUM(order_amount)AS total_amount FROM orders.

[0147] Query the total order amount for each customer.

[0148] Sentence 2:

[0149] GROUP BY customer_id.

[0150] Group by customer ID.

[0151] HAVING SUM(order_amount)>500.

[0152] Keep customers whose total order amount exceeds 500.

[0153] ORDER BY total_amount DESC.

[0154] Sort by total amount from highest to lowest.

[0155] H. In the above process, the model only trains its ability to convert the split simple SQL statements into natural language for a given database. In the actual process, we hope that the model can also make correct responses to complex instructions. Therefore, taking the whole sentence 2 in step G as an example, it is split and combined again to obtain the following data query pair.

[0156] Sentence 1:

[0157] GROUP BY customer_id HAVING SUM(order_amount)>500.

[0158] Group by customer ID and only keep customers whose total order amount exceeds 500.

[0159] Sentence 2:

[0160] HAVING SUM(order_amount)>500ORDER BY total_amount DESC.

[0161] Filter out customers whose total order amount exceeds 500, and sort them by total amount from high to low.

[0162] Sentence 3:

[0163] GROUP BY customer_id HAVING SUM(order_amount)>500ORDER BY total_amount DESC.

[0164] Group customer IDs, filter out customers whose total order amount is greater than 500, and sort the results in descending order by total amount.

[0165] Therefore, if a whole sentence in step G contains N clauses, N(N+1) / 2 combined whole sentences can be constructed. Expand the data set's ability to handle complex natural language instructions. When merging the natural language of the clauses, you can also use the commercial large model LLM to merge them and make it output multiple expressions to generate richer data pairs. Taking the original clause combination in G as an example, you can use the commercial large model to output multiple expressions, such as:

[0166] Query the total order amount of each customer, filter by order date after January 1, 2023, and group by customer ID. Only customers with a total order amount of more than 500 are retained, and the final results are sorted from high to low by total amount.

[0167] Extract the total order amount of customers after January 1, 2023 from the order data, group them by customer ID, retain customer records with a total amount greater than 500, and sort the results from high to low by total amount.

[0168] Filter the total order amount of each customer after January 1, 2023 from the order records, group them by customer ID, retain customers with a total amount exceeding 500, and sort the results in descending order by amount.

[0169] I. In actual dialogue systems, users may expect to clarify nested rules or filter rules through multiple rounds of dialogue. The dialogue content has a strong order dependency. In this part, the clauses in the whole sentence can also be used to construct multiple rounds of dialogue to train the model. Taking the original clause in G as an example, the following multiple rounds of dialogue content can be generated:

[0170] User: I want to see the total amount of each customer's orders.

[0171] SQL statement conversion system: SELECT customer_id, SUM(order_amount) AS total_amount FROM orders.

[0172] User: I only need order data after January 1, 2023.

[0173] SQL statement conversion system: SELECT customer_id, SUM(order_amount) AS total_amount FROM orders WHERE order_date>'2023-01-01'.

[0174] User: Please group the above data by customer ID.

[0175] SQL statement conversion system: SELECT customer_id, SUM(order_amount) AS total_amount FROM orders WHERE order_date>'2023-01-01' GROUP BY customer_id.

[0176] User: Only display customer records whose total order amount is greater than 500.

[0177] SQL statement conversion system: SELECT customer_id,SUM(order_amount)AS total_amountFROM orders WHERE order_date>'2023-01-01'GROUP BY customer_id HAVING SUM(order_amount)>500.

[0178] Users: Sort by total amount from high to low.

[0179] SQL statement conversion system: SELECT customer_id,SUM(order_amount)AS total_amountFROM orders WHERE order_date>'2023-01-01'GROUP BY customer_id HAVING SUM(order_amount)>500ORDER BY total_amount DESC.

[0180] During training, you can follow the mature training program for multi-round dialogues to train the NL-to-SQL model, which can effectively improve its ability to connect the context of multi-round dialogues, so as to achieve the disassembly of complex instructions and make it easier to use.

[0181] J. Use the complex instructions and multi-round dialogue instruction datasets generated in steps H and I together with the open source dataset to train the NL-to-SQL model, and finally obtain an easy-to-use natural language to database conversion model.

[0182] The following are the beneficial effects of this complete embodiment:

[0183] On the one hand, it can automatically generate natural language query pairs for specific databases, significantly shortening the system construction time. On the other hand, through a variety of SQL splitting and combination methods, as well as simulation training of complex instructions and multiple rounds of instructions, the system's ability to handle complex query requirements is effectively improved. This design not only improves the quality of generated data, but also enhances the model's understanding of complex semantics and conversation context, making it more suitable for complex practical application scenarios.

[0184] In the embodiments of the present invention, a database query system is also provided, which is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions that have been made will not be repeated. The terms "module", "unit", "subunit" and the like used below can implement a combination of software and / or hardware of a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware is also possible and conceivable.

[0185] The present invention provides a database query system, which converts natural sentences input by users into corresponding SQL sentences through an NL-to-SQL model.

[0186] The NL-to-SQL model is obtained by the training method of the database query model according to any one of claims 1 to 6.

[0187] The present invention also provides a database query model training system, comprising:

[0188] The conversion module is used to generate several target SQL statements used by the target database.

[0189] The splitting module is used to split a plurality of target SQL statements into a plurality of target SQL clauses.

[0190] The conversion module is used to generate the target natural sentences corresponding to each target SQL clause through the SQL-to-NL model, and to generate the reverse SQL clauses corresponding to each target natural sentence through the NL-to-SQL model. The SQL-to-NL model and the NL-to-SQL model are pre-trained using open source datasets respectively.

[0191] The detection module is used to calculate the similarity between each group of corresponding target SQL clauses and inverse SQL clauses, filter out the inverse SQL clauses with similarity less than a set first threshold, and obtain the target data set.

[0192] The merging module is used to merge the open source dataset with the target dataset and update the training SQL-to-NL and NL-to-SQL models.

[0193] The generation module is used to generate a target training set according to each group of corresponding reverse SQL clauses and target natural sentences, and the target training set is used to train the NL-to-SQL model.

[0194] The present invention also provides an electronic device, comprising a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to execute the database query model training method of any one of claims 1 to 6.

[0195] The present invention also provides a computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the steps of the method for training a database query model according to any one of claims 1 to 6 are implemented.

[0196] It should be noted that the above modules can be functional modules or program modules, and can be implemented by software or hardware. For modules implemented by hardware, the above modules can be located in the same processor; or the above modules can be located in different processors in any combination.

[0197] It should be understood that the specific embodiments described herein are only used to explain the application, rather than to limit it. Based on the embodiments provided in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the protection scope of this application.

[0198] Obviously, the drawings are only some examples or embodiments of the present application. For ordinary technicians in the field, the present application can also be applied to other similar situations based on these drawings without creative work. In addition, it is understandable that although the work done in this development process may be complicated and lengthy, for ordinary technicians in the field, certain changes in design, manufacturing or production based on the technical content disclosed in this application are only conventional technical means and should not be regarded as insufficient content disclosed in this application.

Claims

1. A method for training a database query model, characterized in that: include: Step A: Generate several target SQL statements used by the target database; Step B: splitting the target SQL statements into several target SQL clauses; Step C: Generate a target natural sentence corresponding to each target SQL clause through a SQL-to-NL model, and generate a reverse SQL clause corresponding to each target natural sentence through an NL-to-SQL model, wherein the SQL-to-NL model and the NL-to-SQL model are pre-trained using open source datasets respectively; Step D: Calculate the similarity between the target SQL clauses and the inverse SQL clauses corresponding to each group, filter out the inverse SQL clauses whose similarity is less than the set first threshold, form query pairs with the remaining inverse SQL clauses and their corresponding natural statements, and combine several query pairs to obtain the target data set; Step E: Taking the union of the open source dataset and the target dataset to obtain an updated training set, and updating and training the SQL-to-NL and NL-to-SQL models by using the updated training set; Iteratively execute step C, step D, and step E, and gradually increase the first threshold in the iterative process and filter out the inferred SQL clauses whose similarity is less than the increased first threshold, until the average similarity between the target SQL clauses and the inferred SQL clauses corresponding to each group is greater than the second threshold; Step F: Generate a target training set according to each group of the corresponding reverse SQL clauses and the target natural sentences, and train the NL-to-SQL model based on the target training set.

2. The method for training a database query model according to claim 1, characterized in that: The target training set includes a first target training subset; Generating a target training set according to each group of the inverse SQL clauses and the target natural sentences corresponding to each other includes: Determine the arrangement of the corresponding reverse SQL clauses in step F according to the arrangement of each target SQL clause in the target SQL statement so as to form a sequence SQL clause; If the reverse SQL clause corresponding to the target SQL clause is missing in the sequence SQL clause, the sequence SQL clause is separated into several defective sequence SQL clauses at the missing part, and the several defective sequence SQL clauses are combined in order to obtain a simple sorting clause, and the first target training subset is generated according to the groups of corresponding simple sorting clauses and the target natural sentence.

3. The method for training a database query model according to claim 2, characterized in that: The target training set also includes a second target training subset; Generating a target training set according to each group of the inverse SQL clauses and the target natural sentences corresponding to each other also includes: The inverse SQL clauses in the sequence SQL clauses are randomly arranged and combined to obtain complex sorting clauses, and a second target training subset is generated according to the groups of mutually corresponding complex sorting clauses and the target natural sentences.

4. The method for training a database query model according to claim 3, characterized in that: The target training set also includes a connection training set; Generating a target training set according to each group of the inverse SQL clauses and the target natural sentences corresponding to each other also includes: Several SQL query statement groups are generated according to the reverse SQL clause. In each group of the SQL query statement groups, a newly added SQL query statement is obtained by concatenating the previous SQL query statement and the newly added reverse SQL clause. The newly added SQL query statement and the target natural language corresponding to the newly added reverse SQL clause are used as query pairs to obtain a connection training set.

5. The method for training a database query model according to claim 4, characterized in that: Training the NL-to-SQL model based on the target training set includes: The target training set and the open source data set are combined to form a final training set, and the NL-to-SQL model is trained by the final training set.

6. The method for training a database query model according to claim 1, characterized in that: The similarity is structural similarity, and the structural similarity is divided into SELECT substructure similarity, WHERE substructure similarity, GROUP BY substructure similarity, HAVING substructure similarity, ORDER BY substructure similarity and LIMIT substructure similarity.

7. A database query system, characterized in that: The database query system converts the natural sentence input by the user into the corresponding SQL sentence through the NL-to-SQL model; The NL-to-SQL model is obtained by the training method of the database query model described in any one of claims 1-6.

8. A database query model training system, characterized in that: include: A conversion module is used to generate several target SQL statements used by the target database; A splitting module, used for splitting the target SQL statements into a plurality of target SQL clauses; A conversion module, used to generate a target natural sentence corresponding to each target SQL clause through a SQL-to-NL model, and to generate a reverse SQL clause corresponding to each target natural sentence through an NL-to-SQL model, wherein the SQL-to-NL model and the NL-to-SQL model are pre-trained using open source datasets respectively; A detection module is used to calculate the similarity between each group of corresponding target SQL clauses and inverse SQL clauses, filter out the inverse SQL clauses whose similarity is less than a set first threshold, and obtain the target data set; A merging module, used to take the union of the open source dataset and the target dataset, and update the training SQL-to-NL and NL-to-SQL models; A generating module is used to generate a target training set according to each group of the mutually corresponding reverse SQL clauses and the target natural sentences, wherein the target training set is used to train the NL-to-SQL model.

9. An electronic device, comprising a memory and a processor, characterized in that: A computer program is stored in the memory, and the processor is configured to run the computer program to execute the database query model training method according to any one of claims 1 to 6.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method for training a database query model according to any one of claims 1 to 6 are implemented.

Citation Information

Cited By

  • Text conversion query statement generation method and system, medium and equipment

    CN120371853A