Two-Stage Text2SQL Model, Method, and System Based on Large Language Models
Through the two-stage Text2SQL model, it is divided into table item filtering and SQL generation stages, and the problem of generating complex SQL statements in the existing technology is solved, high-quality and complex SQL statements are generated, and the scalability and accuracy of the model are improved.
Patent Information
- Application Number
- CN202311273899.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-28
- Publication Date
- 2025-07-22
- Estimated Expiration
- 2043-09-28
AI Technical Summary
The existing Text2SQL technology consumes complex manpower and cannot effectively process complex SQL statements, especially multi-table queries and nested queries, and cannot fully utilize the prior knowledge of large language models, resulting in poor accuracy in generating SQL.
A two-stage Text2SQL model based on a large language model is adopted, which is divided into two stages: table item filtering and SQL generation. Small models and pre-trained large language models are trained respectively. The learning data set of the specified content database is used for model training, and combined with the SQL scoring system and the labeling system to ensure the diversity and quality of the data set.
It improves the scalability of the model and the adaptability of professional fields, can effectively generate high-quality and complex SQL statements, simplifies the difficulty of Text2SQL problems, and improves the generalization ability of the model.
Smart Images

Figure CN117290376B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of natural language processing and the generation of structured query statements, and in particular to a two-stage Text2SQL model, method and system based on a large language model. Background Art
[0002] Currently, in the information society, the information in various databases is vast. When information needs to be queried, the conventional method is to convert the description into an SQL query statement and then hand it over to the computer for execution, so that the database can be conveniently queried, greatly improving the efficiency of life and work.
[0003] Text2SQL is a currently commonly used technology for converting natural language descriptions into SQL query statements. However, the existing technical solutions mainly fall into three directions:
[0004] 1. SQL generation based on SQL grammar parsing and rule parsing
[0005] 2. End-to-end SQL generation based on traditional small models such as bert
[0006] 3. Based on large language models (such as ChatGPT, GPT-4), without supervised fine-tuning, directly using prompts for querying and answering
[0007] These methods have the following disadvantages:
[0008] 1. It is necessary to construct templates based on existing rules, consuming manpower, and the overall process and decomposed subtasks are relatively complex
[0009] 2. The ability to construct information for complex SQL statements such as nested queries and multi-table queries is insufficient
[0010] 3. The prior knowledge of the trained model cannot be utilized, and the transferability and scalability are poor
[0011] 4. Directly using prompts cannot utilize the expert knowledge in the field, resulting in poor accuracy of the answered content; and the quality of the output SQL is limited by the capabilities of the large language model itself and cannot be optimized.
[0012] It can be seen that there is still much room for improvement in the current Text2SQL technology. Summary of the Invention
[0013] In order to overcome the defects of the Text2SQL technology in the above-mentioned prior art, the present invention proposes a two-stage Text2SQL model based on a large language model, which simplifies the difficulty of the Text2SQL problem and also enhances the ability of the model to generate high-quality and complex SQL statements.
[0014] Refer toFigure 1 , a training method for a two-stage Text2SQL model based on a large language model proposed by the present invention includes the following steps:
[0015] S1. Obtain a learning data set based on a specified content database, and the labeled samples in the learning data set are natural language statements labeled with SQL;
[0016] S2. Construct a first learning sample and a second learning sample based on the learning data set; the first learning sample is a natural language statement labeled with the target table column items corresponding to the specified content database; the second learning sample includes a natural language statement, its corresponding target table column items, the summary value of the content stored in the target table column items, and the labeled SQL; the table column items include the table name and the column name of any column in the table;
[0017] Set a first basic model and a second basic model; the input of the first basic model is a natural language statement and all the table column items in the specified content database, and the output of the first basic model is the distribution probability of the table column items; the input of the second basic model includes a natural language statement, its target table column items, and the summary value of the content stored in the target table column items, and the output of the second basic model is the SQL of the natural language statement;
[0018] Let the set first basic model learn from the first learning sample to obtain the converged first basic model as the table column item screening model; let the set second basic model learn from the second learning sample to obtain the converged second basic model as the SQL generation model;
[0019] S3. Combine the table column item screening model and the SQL generation model to form a two-stage SQL model; where the input of the table column item screening model is used as the input of the two-stage SQL model, the output of the table column item screening model is connected to the input of the SQL generation model, and the output of the SQL generation model is used as the output of the two-stage SQL model.
[0020] Preferably, in S2, the way to obtain the summary value of the content stored in the target table column items is:
[0021] If the content stored in the target table column items is a discrete value, the summary value is the result after removing duplicates of the stored content;
[0022] If the content stored in the target table column items is a continuous value, the summary value is the interval value where the stored content is located.
[0023] Preferably, S2 specifically includes the following sub-steps:
[0024] S21. Construct a first learning sample based on the learning data set, and let the first basic model perform machine learning on the first learning sample to obtain the table column item screening model;
[0025] S22. Select some labeled samples as alternative second learning samples, input the natural language sentences in each alternative second learning sample into the table item screening model, and obtain the table items corresponding to the N largest probability values in the table item distribution probability output by the table item screening model as the target table items;
[0026] S23. Obtain the summary value of the stored content of the target table items of the natural language sentences in the alternative second learning samples, and combine the natural language sentences, target table items, summary value of the stored content of the target table items, and SQL of the alternative second learning samples to form the second learning samples;
[0027] S24. Let the second basic model perform machine learning on the second learning samples to obtain the SQL generation model.
[0028] Preferably, the acquisition of the learning data set in S1 includes the following sub-steps:
[0029] S11. Obtain samples from the specified content database to form a data set. The samples include natural language sentences and SQL. Score the SQL of each natural language sentence in the data set, and divide the natural language sentences into multiple difficulty levels according to the scores;
[0030] S12. Obtain the quantity distribution of natural language sentences at each difficulty level in the data set, and determine whether the distribution trend is the set distribution trend; if so, use this data set as the learning data set; if not, execute step S13;
[0031] S13. Adjust the number of natural language sentences in the data set up or down, and then return to step S11.
[0032] Preferably, the method of scoring the SQL in S11 is: count the total number of keywords and built-in functions included in the SQL as the score. The higher the score, the greater the difficulty of the natural language sentence.
[0033] Preferably, S1 also includes standardizing the SQL in the labeled samples. The standardization process includes: all uppercase or lowercase, character constants uniformly use single quotes or double quotes, all column names are uniformly supplemented with table names, and characters are uniformly spaced by one space.
[0034] Preferably, the first basic model uses a natural language model. During the machine learning process of the first basic model, calculate the mean square error loss according to the probability values of the target table items of each first learning sample in the table item distribution probability output by the table item screening model, and update the table item screening model in reverse according to the mean square error loss; during the training process of the SQL generation model, use the PEFT technology to fine-tune the pre-trained second basic model in combination with the cross-entropy loss function.
[0035] A SQL query method proposed by the present invention is characterized by comprising the following steps:
[0036] St1. Obtain a two-stage SQL model corresponding to a specified content database by using the training method of the two-stage Text2SQL model based on a large language model;
[0037] St2. Input the natural language statement to be queried and all table column items in the target content database into the two-stage SQL model, and the two-stage SQL model outputs the SQL of the natural language statement to be queried as an alternative SQL;
[0038] St3. Standardize the alternative SQL, and then convert the alternative SQL into corresponding target SQL according to a set of multiple conversion rules;
[0039] St4. Sequentially execute each target SQL in any type of target content database until the target SQL is successfully executed on the target content database; the database type refers to the content storage form of the database.
[0040] A SQL query system proposed by the present invention includes a memory and a processor. The memory stores a computer program, and the processor is connected to the memory. The processor is configured to execute the computer program to implement the SQL query method.
[0041] A SQL query system proposed by the present invention includes: a two-stage SQL model, a conversion module, a rule library, and an execution module;
[0042] The two-stage SQL model is configured to output the SQL of the obtained natural language statement as an alternative SQL;
[0043] One or more conversion rules are stored in the rule library, and the conversion rules are used to convert SQL into SQL applicable to its corresponding type of database;
[0044] The execution module is externally connected to the database and is configured to load the target SQL for SQL query in the database;
[0045] The conversion module is respectively connected to the two-stage SQL model, the rule library, and the execution module;
[0046] The conversion module obtains the alternative SQL output by the two-stage SQL model, loads the conversion rules in the rule library, and converts the alternative SQL according to the loaded conversion rules to obtain the target SQL corresponding to each conversion rule; the execution module obtains each target SQL; when querying a specified type of database, the execution module sequentially executes each target SQL in the specified type of database until the execution is successful.
[0047] The advantages of the present invention are:
[0048] (1) The overall solution of the present invention is divided into two stages. The first stage is to determine the required tables and columns in the specified content database for the input natural language; the second stage is to use a large model to perform the Seq2Seq task based on the tables and columns selected in the first stage to complete the generation of SQL. The entire Text2SQL process is split into two stages: selecting tables and columns and generating SQL, and small models and pre-trained large language models are trained respectively to complete the tasks of these two stages. Therefore, the data construction and use of the model have strong scalability, can well support multi-table queries and the generation of complex statements, and have strong adaptability to professional fields.
[0049] (2) Sufficient amounts of high-quality data sets are required for the training of both stages. The present invention designs a set of SQL difficulty scoring systems and tagging systems, which can be used to evaluate whether the training set has a moderate difficulty distribution and sufficient diversity. In this way, the present invention enhances the data set by combining SQL scoring, ensuring the sample diversity of the data set in the model training stage, which is conducive to improving the model accuracy.
[0050] (3) The present invention adopts a two-stage training method, which simplifies the difficulty of the Text2SQL problem while enhancing the model's ability to generate high-quality and complex SQL statements. Based on existing open-source large models, it fully utilizes their prior language knowledge, enabling the present invention to have good generalization ability. Description of the Drawings
[0051] Figure 1 It is a flowchart of the training method for a two-stage Text2SQL model based on a large language model;
[0052] Figure 2 It is a distribution diagram of keywords and built-in functions in the open-source Text2SQL data set CSpider;
[0053] Figure 3 It is a flowchart of the SQL query method. Detailed Embodiments
[0054] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0055] The table column item screening model proposed in this embodiment adopts a natural language model, which extracts table column items for summarizing natural language statements based on the table column information of the specified content database.
[0056] The input of the table column screening model is a natural language statement and all table column items in the specified content database. The table column items include the table name and the column name of any column in the table. It can be seen that the number of table column items in the content database is the sum of the number of columns in all tables in the content database. The content database is used to distinguish the stored content of the database, and the specified content database is the database that stores the specified content. The type of the subsequent database refers to the storage form of the content in the database, that is, different types of databases can store the same content.
[0057] The output of the table column screening model is the distribution probability of table column items. The table column items corresponding to the largest N probability values are obtained as the predicted values of the target table column items, and the target table column items are the table column items used to summarize the input natural language statement.
[0058] The table column screening model is obtained by performing machine learning on the first learning samples, and the first learning samples are natural language statements labeled with target table column items. During the learning process of the table column screening model, the mean squared error loss is calculated based on the probability values of the target table column items of each first learning sample in the distribution probability of the table column items output by the table column screening model, so as to update the table column screening model backward according to the mean squared error loss.
[0059] The input of the SQL generation model proposed in this embodiment includes: a natural language statement, the target table column items of the natural language statement screened by the table column screening model on the specified content database, and the summary value of the stored content of the target table column items; the output of the SQL generation model is the SQL of the natural language statement.
[0060] The way to obtain the summary value of the stored content of the table column items is as follows:
[0061] If the stored content of the target table column item is a discrete value, the summary value is the result after removing duplicates of the stored content; for example, the data stored in the gender column is processed into a value set ("male", "female");
[0062] If the stored content of the target table column item is a continuous value, the summary value is the interval value where the stored content is located; for example, for the data stored in the blood pressure value, the maximum value is 200 and the minimum value is 120, then it is processed into a value range (120, 200).
[0063] The SQL generation model is obtained by using the second learning samples. The second learning samples include a natural language statement, the target table column items of the natural language statement screened by the table column screening model on the specified content database, and the summary value of the stored content of the target table column items. The labeled label in the second learning samples is the SQL corresponding to the natural language statement. The cross-entropy loss function is used during the training process of the SQL generation model. Specifically, during the training process of the SQL generation model, the pre-trained model is fine-tuned by using the PEFT technique in combination with the cross-entropy loss function.
[0064] For the convenience of model training, during the training process, the output of the SQL generation model is defined as SQL in the embedded template paradigm. That is, during the training process of the SQL generation model, the annotation label of the second learning sample can be set as SQL in the embedded template paradigm.
[0065] For example, if the natural language statement is "Query the number of female patients in each department ward area", combined with the template paradigm "select _, count(_) as patientCount from _ where _ group by _|", its annotation label, that is, the SQL in the embedded template paradigm, is as follows:
[0066] select _, count(_) as patientCount from _ where _ group by _|
[0067] SELECT Code,admitDepartmentAreaName,originalGender,COUNT(*) AS patientCount FROM hospitalRecord WHERE originalGender = ‘female’ GROUP BY Code,admitDepartmentAreaName
[0068] It should be noted that the annotation template paradigm "select _, count(_) as patientCount from _ where _ group by _|" during the training process is for the convenience of model training and to improve the model's understanding ability of labels. During the actual application process, only the SQL needs to be extracted from the output of the SQL model for application. For example, for the natural language statement "Query the number of female patients in each department ward area", the SQL is as follows: SELECT Code,admitDepartmentAreaName,originalGender,COUNT(*) AS patientCount FROM hospitalRecord WHERE originalGender = ‘female’ GROUP BY Code,admitDepartmentAreaName
[0069] A training method for a two-stage Text2SQL model based on a large language model proposed in this embodiment includes the following steps S1 - S3:
[0070] S1. Obtain a learning dataset based on a specified content database. The labeled samples in the learning dataset are natural language statements labeled with SQL. In this embodiment, for the convenience of subsequent processing, the SQL in the labeled samples is standardized. The standardization process includes: all in uppercase or lowercase, character constants uniformly using single quotes or double quotes, all column names uniformly supplemented with table names, and characters uniformly leaving a space. The standardization process can avoid interference caused by irrelevant factors that do not affect the accuracy of SQL.
[0071] The learning dataset in this embodiment is a dataset enhanced by evaluation. The evaluation enhancement of the dataset includes the following steps S11 - S13;
[0072] S11. Randomly select data from the specified content database to construct a dataset, score the SQL of each natural language statement in the dataset, and divide the natural language and statements into multiple difficulty levels according to the scores. Specifically, in this step, the score of SQL is the total number of keywords and built-in functions included in the SQL.
[0073] For example, a certain SQL "select avg(patientInfo.patientAge), medicalInfo.medicalTime from patientInfo join medicalInfo where patientInfo.patientId = medicalInfo.patientId" contains 5 keywords select, from, join, avg, and where, so the score of this SQL is 5 points. Obviously, the more keywords and built-in functions are included, the higher the score, indicating that this SQL statement is more complex.
[0074] In specific implementation, for the convenience of statistics, each keyword and built-in function can be set to correspond to a label, so as to score according to the label, which is convenient for classification statistics and data enhancement. For example, in the above embodiment, the SQL can be set to have 5 labels, namely select, from, join, avg, and where. And the statistics at the label level for the dataset can represent the richness of the dataset.
[0075] In specific implementation, the scoring at the sentence level can represent the difficulty grading of the dataset. The difficulty grading is represented by Level 0, Level 1,..., Level n, and the specific number of levels is determined according to the application scenario. Since a practical SQL must contain the two keywords "select" and "from", therefore, Level 0 corresponds to 2 points, and the scores of other levels can increase sequentially, or different mapping relationships can be adjusted according to the application scenario.
[0076] S12. Obtain the distribution of the number of natural language sentences at each difficulty level in the dataset, and determine whether the distribution trend is the set distribution trend. Specifically, the set distribution trend can be set as a normal distribution. If so, use this dataset as the learning dataset. Otherwise, execute step S13.
[0077] S13. Adjust the number of natural language sentences in the dataset (increase or decrease), and then return to step S11.
[0078] By scoring the sentences, the overall quality and difficulty level of the dataset can be obtained. Users can also customize the difficulty ratio, that is, the set distribution trend, according to different application scenarios. For example, Level 0 (2 points): Level 1 (3 points): Level 2 (4 points): Level 3 (5 points and above) = 3:3:2:2. Thus, users can enhance the dataset in a planned manner, making the data distribution in the dataset tend to the set distribution trend, such as a normal distribution. Since the label is not bound to the difficulty, after meeting the sentence difficulty ratio, it can be enriched and expanded according to the label to maintain data balance.
[0079] S2. Combine the following steps S21 - S24 to obtain an SQL generation model. The input of the SQL generation model is a natural language sentence, and the output is SQL.
[0080] S21. Construct a first learning sample based on the learning dataset, and let the first basic model perform machine learning on the first learning sample to obtain a table column item screening model.
[0081] S22. Select some labeled samples as alternative second learning samples, input the natural language sentences in each alternative second learning sample into the table column item screening model, and obtain the table column items corresponding to the N largest probability values in the table column item distribution probability output by the table column item screening model as the target table column items.
[0082] S23. Obtain the summary value of the stored content of the target table column items of the natural language sentences in the alternative second learning samples, and combine the natural language sentences, target table column items, summary value of the stored content of the target table column items, and SQL in the alternative second learning samples to form a second learning sample.
[0083] S24. Let the second basic model perform machine learning on the second learning sample to obtain an SQL generation model.
[0084] S3. Combine the table column item screening model and the SQL generation model to form a two - stage SQL model. Among them, the input of the table column item screening model is used as the input of the two - stage SQL model, the output of the table column item screening model is connected to the input of the SQL generation model, and the output of the SQL generation model is used as the output of the two - stage SQL model.
[0085] Thus, by simply inputting a natural language statement into the two-stage SQL model, the SQL corresponding to the natural language statement can be obtained.
[0086] It should be noted that during the training process of the two-stage SQL model, multiple specified content databases can be set. During the training process, the input of the first basic model is a natural language statement, the target table columns on the corresponding specified content database, and all the table columns of the corresponding specified content database. The training objective is to make the two-stage SQL model output the target table columns of the natural language statement on the corresponding specified content database. The two-stage SQL model obtained in this way can be suitable for databases with different stored contents. By simply inputting the natural language statement and all the table columns of the corresponding target database into the two-stage SQL model, the two-stage SQL model can output the SQL corresponding to the target database.
[0087] The following combines specific embodiments to verify the above two-stage SQL model.
[0088] In this embodiment, the open-source Text2SQL dataset CSpider is taken as an example. The dataset CSpider has a total of 8,656 data items. The distribution of keywords and built-in functions in the dataset is as Figure 2 shown, and the scoring results of the data are shown in Table 1. It can be seen from Table 1 that the SQL difficulty distribution of CSpider is reasonable, close to the actual usage scenario. The number of the most difficult and the simplest is small, and the SQL with medium difficulty is evenly distributed. From Figure 2 it can be seen that the SQL of CSpider covers the common situations of Sqlite syntax. Therefore, considering the two indicators of comprehensiveness and richness, CSpider is a dataset with good quality.
[0089] Table 1: Data structure of the open-source Text2SQL dataset CSpider
[0090]
[0091] In this embodiment, data is selected from the dataset CSpider and the medical dataset to form a training set, and the data scores in the training set are normally distributed. The ratio of the data of the dataset CSpider to the medical data in the training set is 8:1.
[0092] Considering that the purpose of the model is to learn the mapping from natural language expressions to SQL, in order to improve the model's attention to the details of SQL syntax and reduce the interference of irrelevant factors such as case-sensitive table aliases, the number of spaces between characters, and the omission of table names in SQL on the model, in this embodiment, the SQL in the training set is standardized to eliminate all differences between SQL statements that have nothing to do with the accuracy rate, such as format differences and other irrelevant factors.
[0093] For example, after standardization, SQL "SELECT distinct(BillingCountry)FROM INVOICE" is converted to "select distinct(invoice.billingcountry)frominvoice";
[0094] After standardization, "SELECT FirstName,LastName FROM EMPLOYEE WHERE City=\"Qingdao\"" is converted to "select employee.firstname,employee.lastname from employeewhere employee.city='Qingdao'".
[0095] Refer to Figure 3 In this embodiment, the training set is used as the learning data set, and the above-mentioned training method of the two-stage Text2SQL model based on the large language model is executed to obtain the table column item screening model and the two-stage SQL model.
[0096] In this embodiment, the performance of the two-stage SQL model is first verified with the validation set of the open-source data set CSpider.
[0097] In this embodiment, the natural language statements in the validation set and all the table column items of the specified content database in the open-source data set CSpider are input into the table column item screening model, and the table column item screening model outputs the target table column items of the natural language statements to be queried.
[0098] In this embodiment, the target table column items of each validation sample in the validation set are compared with the manually labeled table column items of each validation sample in the validation set, and the AUC index of the table column item screening model is calculated to be above 0.99. In this embodiment, the two-stage SQL model is further used to obtain the alternative SQLs of each natural language statement in the validation set, and then the alternative SQLs are standardized, and then the alternative SQLs are converted into the corresponding target SQLs according to a set of conversion rules. In this embodiment, the target SQLs are sequentially executed on the specified content databases of types such as sqlite and mysql for each natural language statement in the validation set, and the execution results are used as the judgment criteria, and finally an accuracy of 0.8195 is achieved.
[0099] In this embodiment, the accuracy of the table column item screening model and the two-stage SQL model obtained in this embodiment is further verified by combining with the medical data set.
[0100] In this embodiment, natural language statements in the medical field are collected to construct a test set. The natural language statements in the test set and all the tabular items in the medical data set are input into the tabular item screening model. The target tabular items output by the tabular item screening model are compared with the manually annotated tabular items in the test set, and the AUC index of the tabular item screening model reaches above 0.99.
[0101] In this embodiment, a two-stage SQL model is further used to obtain alternative SQLs for each natural language statement in the test set, and then the alternative SQLs are standardized. Then, the alternative SQLs are converted into corresponding target SQLs according to a set of multiple conversion rules. In this embodiment, the target SQLs are sequentially executed for each natural language statement in the test set on medical data sets of types such as sqlite and mysql, and the execution results are used as the judgment criteria, and finally an accuracy of 0.93 is achieved.
[0102] Certainly, for those skilled in the art, the present invention is not limited to the details of the above exemplary embodiments, but also includes the same or similar structures that can be implemented in other specific forms without departing from the spirit or basic characteristics of the present invention. Therefore, from any point of view, the embodiments should be regarded as exemplary and non-limiting. The scope of the present invention is defined by the appended claims rather than the above description. Therefore, all changes falling within the meaning and scope of the equivalent elements of the claims are intended to be embraced within the present invention. Any reference signs in the claims should not be construed as limiting the claimed invention.
[0103] In addition, it should be understood that although this specification is described according to embodiments, not every embodiment only contains an independent technical solution. This narrative way of the specification is only for clarity. Those skilled in the art should regard the specification as a whole, and the technical solutions in each embodiment can also be appropriately combined to form other embodiments that can be understood by those skilled in the art.
[0104] The technologies, shapes, and structures not detailedly described in the present invention are all well-known technologies.
Claims
1. A training method for a two-stage Text2SQL model based on a large language model, characterized in that, It includes the following steps: S1. Obtain a learning dataset based on a specified content database, where the labeled samples in the learning dataset are natural language statements labeled with SQL; S2. Construct a first learning sample and a second learning sample based on the learning dataset; The first learning sample is a natural language statement labeled with the target table column items corresponding to the specified content database; the second learning sample includes a natural language statement, its corresponding target table column items, the summary value of the content stored in the target table column items, and the labeled SQL; the table column items include the table name and the column name of any column in the table; Set a first basic model and a second basic model; the input of the first basic model is a natural language statement and all table column items in the specified content database, and the output of the first basic model is the distribution probability of the table column items; The input of the second basic model includes a natural language statement, its target table column items, and the summary value of the content stored in the target table column items, and the output of the second basic model is the SQL of the natural language statement; Let the set first basic model learn from the first learning sample to obtain the converged first basic model as the table column item screening model; Let the set second basic model learn from the second learning sample to obtain the converged second basic model as the SQL generation model; S3. Combine the table column item screening model and the SQL generation model to form a two-stage SQL model; where the input of the table column item screening model is used as the input of the two-stage SQL model, the output of the table column item screening model is connected to the input of the SQL generation model, and the output of the SQL generation model is used as the output of the two-stage SQL model.
2. The training method of the two-stage Text2SQL model based on the large language model according to claim 1, wherein, In S2, the way to obtain the summary value of the content stored in the target table column items is as follows: If the content stored in the target table column items is discrete, the summary value is the result after removing duplicates of the stored content; If the content stored in the target table column items is continuous, the summary value is the interval value where the content is located.
3. The training method of the two-stage Text2SQL model based on the large language model according to claim 2, wherein, S2 specifically includes the following sub-steps: S21. Construct a first learning sample based on the learning dataset, and let the first basic model perform machine learning on the first learning sample to obtain the table column item screening model; S22. Select some labeled samples as alternative second learning samples, input the natural language statements in each alternative second learning sample into the table column item screening model, and obtain the table column items corresponding to the N largest probability values in the distribution probability of the table column items output by the table column item screening model as the target table column items; S23. Obtain the summary value of the content stored in the target table column items of the natural language statements in the alternative second learning samples, and combine the natural language statements, target table column items, summary value of the content stored in the target table column items, and SQL of the alternative second learning samples to form the second learning sample; S24. Let the second basic model perform machine learning on the second learning sample to obtain the SQL generation model.
4. The training method of the two-stage Text2SQL model based on the large language model according to claim 1, wherein The acquisition of the learning dataset in S1 includes the following sub-steps: S11. Obtain samples from the specified content database to form a dataset. The samples contain natural language statements and SQL. Score the SQL of each natural language statement in the dataset, and divide the natural language statements into multiple difficulty levels according to the scores; S12. Obtain the distribution of the number of natural language sentences at each difficulty level in the dataset, and determine whether the distribution trend is the set distribution trend; If yes, use this dataset as the learning dataset; If no, execute step S13; S13. Adjust the number of natural language sentences in the dataset up or down, and then return to step S11.
5. The training method of the two-stage Text2SQL model based on the large language model according to claim 4, characterized in that, The method of scoring the SQL in S11 is: count the total number of keywords and built-in functions included in the SQL as the score. The higher the score, the greater the difficulty of the natural language sentence.
6. The training method of the two-stage Text2SQL model based on the large language model according to claim 1, characterized in that, S1 also includes standardizing the SQL in the labeled samples. The standardization process includes: all uppercase or lowercase, using single quotes or double quotes uniformly for character constants, uniformly supplementing the table name for all column names, and leaving a space for each character.
7. The training method of the two-stage Text2SQL model based on the large language model according to claim 1, characterized in that, The first basic model uses a natural language model. During the machine learning process of the first basic model, the mean squared error loss is calculated based on the probability values of the target table column items of each first learning sample in the column item distribution probability output by the column screening model, and the column screening model is updated backward according to the mean squared error loss; during the training process of the SQL generation model, the pre-trained second basic model is fine-tuned using the PEFT technique in combination with the cross-entropy loss function.
8. An SQL query method using the training method of the two-stage Text2SQL model based on the large language model according to any one of claims 1-7, characterized in that, It includes the following steps: St1. Use the training method of the two-stage Text2SQL model based on the large language model described in any one of claims 1-7 to obtain the two-stage SQL model corresponding to the specified content database; St2. Input the natural language sentence to be queried and all the column items in the target content database into the two-stage SQL model, and the two-stage SQL model outputs the SQL of the natural language sentence to be queried as the alternative SQL; St3. Standardize the alternative SQL, and then convert the alternative SQL into the corresponding target SQL according to the set multiple conversion rules; St4. Sequentially execute each target SQL in any type of target content database until the target SQL is successfully executed on the target content database; the database type refers to the content storage form of the database.
9. A SQL query system, characterized in that, It includes a memory and a processor. The memory stores a computer program, and the processor is connected to the memory. The processor is used to execute the computer program to implement the SQL query method described in claim 8.
10. An SQL query system adopting the training method of the two-stage Text2SQL model based on a large language model according to any one of claims 1-7, characterized in that, It includes: A two-stage SQL model, a conversion module, a rule library, and an execution module; The two-stage SQL model is used to output the SQL of the obtained natural language sentence as the alternative SQL; The rule library stores one or more conversion rules, and the conversion rules are used to convert the SQL into the SQL applicable to its corresponding type of database; The execution module is externally connected to the database and is used to load the target SQL to perform SQL queries in the database; The conversion module is respectively connected to the two-stage SQL model, the rule library, and the execution module; The conversion module obtains the alternative SQLs output by the two-stage SQL model. The conversion module loads the conversion rules in the rule library. The conversion module converts the alternative SQLs according to the loaded conversion rules to obtain the target SQLs corresponding one by one to the conversion rules. The execution module obtains each target SQL. When performing a query on a specified type of database, the execution module sequentially executes each target SQL in the specified type of database until the execution is successful.
Citation Information
Patent Citations
Method, system and device for converting natural language query into SQL and storage medium
CN114547072A
Model training method and device, natural language processing method and device, equipment and storage medium
CN115905282A