NL2SQL method based on LLM model and storage medium
By classifying the predicted SQL statements by difficulty level and calculating the loss, the LLM model is updated to adapt to different business needs, solving the problem of insufficient accuracy in generating SQL statements by large language models and achieving more efficient SQL generation results.
Patent Information
- Application Number
- CN202311788083.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-22
- Publication Date
- 2026-11-27
- Estimated Expiration
- 2043-12-22
AI Technical Summary
Existing technologies have low accuracy rates in generating SQL statements using large language models, and lack evaluation criteria for different levels of difficulty, resulting in insufficient accuracy and applicability of the generated SQL statements.
By obtaining a list of natural language and SQL statements for training, inputting them into the initial LLM model, splitting the predicted SQL statements into key-value pairs, calculating the loss for different difficulty levels, and updating the target LLM model if the loss is less than a threshold, specific difficulty levels can be trained for different business needs.
It improves the accuracy and applicability of the LLM model in generating SQL statements, meeting the SQL generation requirements under different business needs.
Smart Images

Figure CN117609279B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of natural language processing, and in particular to an NL2SQL method and storage medium based on an LLM model. Background Technology
[0002] NL2SQL is one of the core tasks of Natural Language Processing (NLP) technology and a subtask of semantic parsing. Its essence is to transform users' natural language into a semantic representation that computers can understand and execute. For big data companies, business personnel need to write many SQL statements, but the accuracy of direct NL2SQL generators is relatively low. With the development of large language models, they are gradually being used as NL2SQL generators. However, current technology lacks evaluation criteria for the generated SQL statements at different levels of difficulty. Summary of the Invention
[0003] To address the aforementioned technical problems, the present invention adopts the following technical solution: an NL2SQL method based on an LLM model, the method comprising the following steps:
[0004] S1, Obtain the target statement for the first business, wherein the target statement for the first business is a statement in natural language, and the first business is one of a plurality of preset businesses in a preset domain;
[0005] S2, input the target statement of the first business into the target LLM model, and obtain the SQL statement corresponding to the target statement of the first business;
[0006] The target LLM model is obtained through the following steps:
[0007] S100, Obtain the list of natural language statements for training A={A1, A2, ..., A...} i A m The training SQL statement list B = {B1, B2, ..., B} corresponds to A. i B m}, A i B is the i-th training natural language statement used in the first business. i It is A i The training SQL statements for the transformation, where the value of i ranges from 1 to m, and m is the number of training natural statements used in the first business.
[0008] S200, Input the training natural language statement list A into the initial LLM model to obtain the predicted SQL statement list C = {C1, C2, ..., C...} i C m}, C i It is A i The predicted SQL statement output by the initial LLM model after inputting the initial LLM model;
[0009] S300, based on a list of SQL functions, for C i Split it to get C i The corresponding key-value pairs, where the key of each key-value pair is the SQL function and operator, and the value in each key-value pair is the number of parameters and operators corresponding to the SQL function, wherein the SQL function list J = {J1, J2, ..., J...} r , ..., J s}, J r It is the r-th SQL function or operator, where r ranges from 1 to s, and s is the total number of SQL functions and operators;
[0010] S400, obtain the difficulty level list D={D1, D2, ..., D...} j D n}, and based on C i and C i The corresponding key-value pair, get C i The corresponding difficulty level is D. j , where D j It is the j-th difficulty level, where j ranges from 1 to n, and n is the number of difficulty levels;
[0011] S500, acquisition difficulty level is D. j Given a list of predicted SQL statements, and based on the training SQL statement list B corresponding to A, the computation difficulty level is D. j The loss E corresponding to the list of predicted SQL statements j ;
[0012] S600, if for any E j E exists in all of them j ≤F j The initial LLM model is used as the target LLM model, where F j It is D j The corresponding loss threshold.
[0013] A non-transitory computer-readable storage medium storing at least one instruction or at least one program segment, characterized in that the at least one instruction or the at least one program segment is loaded and executed by a processor to implement the NL2SQL method based on the LLM model as described above.
[0014] The present invention has at least the following beneficial effects:
[0015] In summary, the process involves obtaining a list of natural language statements for training and a corresponding list of SQL statements for training. The list of natural language statements is then input into the initial LLM model to obtain a list of predicted SQL statements. Based on the list of SQL functions, the predicted SQL statements are split to obtain the key-value pairs corresponding to each predicted SQL statement. Based on the predicted SQL statements and their corresponding key-value pairs, the difficulty level of each predicted SQL statement is determined. A list of predicted SQL statements at any difficulty level is then obtained, and the loss corresponding to that difficulty level is calculated. If the loss for any difficulty level of the predicted SQL statement list is less than the corresponding loss threshold, the initial LLM model is used as the target LLM model, and the SQL statement corresponding to the target statement is obtained using the target LLM model. This invention achieves training of the LLM model to convert SQL statements to specific difficulty levels by dividing predicted SQL statements into different difficulty levels and using different loss thresholds for different difficulty levels based on business requirements. Attached Figure Description
[0016] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0017] Figure 1 A flowchart of an NL2SQL method based on an LLM model provided in an embodiment of the present invention. Detailed Implementation
[0018] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0019] This invention provides an NL2SQL method based on an LLM model, such as... Figure 1 As shown, the method includes the following steps:
[0020] S1, Obtain the target statement for the first business, wherein the target statement for the first business is a statement in natural language, and the first business is one of a plurality of preset businesses in a preset domain.
[0021] Specifically, the preset services can be front-end data query, data analysis, or services that require model understanding.
[0022] S2, input the target statement of the first business into the target LLM model, and obtain the SQL statement corresponding to the target statement of the first business.
[0023] The target LLM model is obtained through the following steps:
[0024] S100, Obtain the list of natural language statements for training A={A1, A2, ..., A...} i A m The training SQL statement list B = {B1, B2, ..., B} corresponds to A. i B m}, A i B is the i-th training natural language statement used in the first business. i It is A i The training SQL statements for the transformation, where i ranges from 1 to m, and m is the number of training natural statements used in the first business.
[0025] Specifically, the training natural statements are statements in natural language, and the training SQL statements are converted from the training natural statements. In particular, the training natural statements can be manually converted into training SQL statements.
[0026] S200, Input the training natural language statement list A into the initial LLM model to obtain the predicted SQL statement list C = {C1, C2, ..., C...} i C m}, C i It is A i The predicted SQL statement output by the initial LLM model after inputting the initial LLM model.
[0027] S300, based on a list of SQL functions, for C i Split it to get C i The corresponding key-value pairs, where the key of each key-value pair is the SQL function and operator, and the value in each key-value pair is the number of parameters and operators corresponding to the SQL function, wherein the SQL function list J = {J1, J2, ..., J...} r , ..., J s}, J r It is the r-th SQL function or operator, where r ranges from 1 to s, and s is the total number of SQL functions and operators.
[0028] Specifically, the parameters corresponding to the SQL function can be constants, column names, expressions, or other functions.
[0029] Specifically, the list of SQL functions includes at least the following: form function, select function, where function, groupBy function, having function, orderBy function, limit function, and separation function; the operators include at least the following: like, or, and join.
[0030] This can be understood as predicting SQL statement C. i Split according to the key of the key-value pair to obtain C. i A list of key-value pairs.
[0031] In one embodiment of the present invention, the predicted SQL statement is:
[0032] SELECT AVG(s_graduate_year) AS avg_graduate_year
[0033] FROM Student;
[0034] The predicted SQL statement is split into its corresponding key-value pairs: {"from":{"table_units":[["table_unit","_student_"]]","conds":[]},"select":[false,[[5,[0,[0,"_student.s_graduate_year_",false],null]]]]","where":[],"groupBy":[]","orderBy":[]","limit":null","having":[],"operators":{}}.
[0035] S400, obtain the difficulty level list D={D1, D2, ..., D...} j D n}, and based on C i and C i The corresponding key-value pair, get C i The corresponding difficulty level is D. j , where D j It is the j-th difficulty level, where j ranges from 1 to n, and n is the number of difficulty levels.
[0036] Specifically, it can be understood as being based on C i Includes functions and C i The corresponding key-value pairs have the same restrictions and C++ requirements for the specified database functions. i The number of operators corresponding to the corresponding key-value pairs, get C i The corresponding difficulty level.
[0037] Specifically, the difficulty level list D can be determined according to actual needs.
[0038] S500, acquisition difficulty level is D. j Given a list of predicted SQL statements, and based on the training SQL statement list B corresponding to A, the computation difficulty level is D. j The loss E corresponding to the list of predicted SQL statements j .
[0039] Specifically, as those skilled in the art will know, any method for calculating the loss of predicted values in the prior art falls within the protection scope of this invention, and will not be elaborated further here.
[0040] S600, if for any E j E exists in all of them j ≤F j The initial LLM model is used as the target LLM model, where F j It is D j The corresponding loss threshold.
[0041] Specifically, F j The threshold is determined based on business requirements. Specifically, when the business requirement is a front-end data query, such as a simple business command like querying an event that occurred at a specific point in time, the loss threshold is lower for lower difficulty levels and higher for other difficulty levels. When the business requirement is data analysis, such as needing to combine multiple tables to query and extract content corresponding to specific tags, the loss threshold is lower for medium difficulty levels and higher for other difficulty levels. When the business requirement is a business that requires model understanding, such as querying a user's trajectory over the past month or other content that cannot be logically represented, the loss threshold is lower for high difficulty levels and higher for other difficulty levels.
[0042] In summary, the process involves obtaining a list of natural language statements for training and a corresponding list of SQL statements for training. The list of natural language statements is then input into the initial LLM model to obtain a list of predicted SQL statements. Based on the list of SQL functions, the predicted SQL statements are split to obtain the key-value pairs corresponding to each predicted SQL statement. Based on the predicted SQL statements and their corresponding key-value pairs, the difficulty level of each predicted SQL statement is determined. A list of predicted SQL statements at any difficulty level is then obtained, and the loss corresponding to that difficulty level is calculated. If the loss for any difficulty level of the predicted SQL statement list is less than the corresponding loss threshold, the initial LLM model is used as the target LLM model, and the SQL statement corresponding to the target statement is obtained using the target LLM model. This invention achieves training of the LLM model to convert SQL statements to specific difficulty levels by dividing predicted SQL statements into different difficulty levels and using different loss thresholds for different difficulty levels based on business requirements.
[0043] Specifically, S400 includes the following steps:
[0044] S410, Calculate the number of non-empty values in the first key-value pair and denote it as the first difficulty value H. i1 In the first key-value pair, the key must include at least an operator.
[0045] Specifically, the keys of the first key-value pair must include at least the following functions: WHERE, GROUP BY, ORDER BY, LIMIT, LIKE, JOIN, and OR. This can be understood as follows: if the value of the first key-value pair is not empty, it is recorded as 1; the number of non-empty values in the first key-value pair is counted as the first difficulty value.
[0046] S420, obtain the number of select functions as the second difficulty value H. i2 .
[0047] Specifically, a predictive SQL statement may contain multiple clauses, and the number of clauses is determined by the number of select functions used. The more clauses a predictive SQL statement contains, the more complex it is, and the higher its difficulty level.
[0048] S430, obtain the third difficulty value H i3 The third difficulty value is the number of constraints greater than 1 contained in the specified database function corresponding to the key in the key-value pair.
[0049] Specifically, S430 includes the following steps:
[0050] S431, obtain C i The number of aggregate functions in the middle; if the number of aggregate functions is greater than 1, AH i3 =1, otherwise, AHi3 =0.
[0051] S432, Get C i The number of query conditions in the WHERE function, if C i When the number of query conditions in the WHERE function is greater than 1, BH i3 =1, otherwise, BH i3 =0.
[0052] S433, obtain C i The number of constraints in the groupby function, if C i The number of constraints in the groupby function is greater than 1. i3 =1, otherwise, CH i3 =0.
[0053] S434, Get C i The number of select functions in C, if C i The number of select functions in the DH array is greater than 1. i3 =1, otherwise, DH i3 =0.
[0054] S435, Get H i3 =AH i3 +BH i3 +CH i3 +DH i3 .
[0055] Based on S431-S435, the third difficulty value is determined based on the number of aggregate functions, where functions, groupby functions, and select functions, or the number of restrictive conditions contained in the SQL statement. The more restrictive conditions, aggregate functions, and select functions a predictive SQL statement contains, the deeper the predictive SQL statement is, and the higher the third difficulty value is.
[0056] S440, based on H i1 H i2 and H i3 Match it with the difficulty level list D to obtain C. i The corresponding difficulty level.
[0057] Based on S410-S440, the number of non-empty values in the first key-value pair is recorded as the first difficulty value, the number of clauses is recorded as the second difficulty value, and the third difficulty value is recorded as the third difficulty value. The first, second, and third difficulty values are added together to obtain the final difficulty value, which is then matched with the difficulty level list to obtain the corresponding difficulty level. This invention calculates the final difficulty value of the predicted SQL statement from aspects such as the number of specified database functions included, the number of clauses included, and the number of specified database constraints included, and obtains the corresponding difficulty level.
[0058] Furthermore, in S440, when n=4, the difficulty level list D={D1, D2, D3, D4}, where the difficulty level of D1 < the difficulty level of D2 < the difficulty level of D3. This can be understood as D1 being the easy difficulty level, D2 being the medium difficulty level, D3 being the hard difficulty level, and D4 being the extra difficulty level.
[0059] Furthermore, the S440 also includes:
[0060] S401, if H i1 ≤1, H i2 =0, H i3 =0, C i The corresponding difficulty level is D1.
[0061] S402, if H i1 ≤1, H i2 =0, H i3 ≤2, or H i1 ≤2, H i2 =0, H i3 ≤1, C i The corresponding difficulty level is D2.
[0062] S403, if H i1 ≤2, H i2 =0, H i3 >2, or 2<H i1 ≤3, H i2 =0, H i3 ≤2, or H i1 ≤1, H i2 ≤1, H i3 =0, C i The corresponding difficulty level is D3.
[0063] S404, otherwise, C i The corresponding difficulty level is D4.
[0064] Specifically, this invention divides the difficulty into four levels by defining the requirements for the first, second, and third difficulty values under each difficulty level. This can be understood as the number of clauses, i.e., the second difficulty value, also affecting the first difficulty value. Therefore, the difficulty level is divided into three parts and treated differently.
[0065] Furthermore, in another embodiment of the present invention, S440 specifically includes:
[0066] S441, Get G i =w1×H i1 +w2×H i2 +w3×H i3 +w4, where w1 is the first weighting factor, w2 is the second weighting factor, and w3 is the third weighting factor.
[0067] S442, using G i Match with difficulty level list D to obtain C. i The corresponding difficulty level.
[0068] Specifically, w1, w2, and w3 are obtained through the following steps:
[0069] S4411, Retrieve historical SQL statement data list K={K1, K2, ..., K...} g , ..., K z}, the g-th historical SQL statement data K g =(K g1 K g2 K g3 K g0 ), K g1 K is the first difficulty value corresponding to the g-th historical SQL statement. g2 K is the second difficulty value corresponding to the g-th historical SQL statement. g3 K is the third difficulty value corresponding to the g-th historical SQL statement. g0 It is the total difficulty value corresponding to the g-th historical SQL statement.
[0070] S4412, based on the historical SQL statement data list J, use a linear regression function to fit the data and obtain w1, w2, and 3.
[0071] Specifically, S4412 also includes: using the log-likelihood cost function as the loss function and using Adam as the optimizer.
[0072] Specifically, the first, second, third, and fourth weighting factors are randomly initialized. The prediction difficulty value is calculated using a generalized linear regression model. The loss between the prediction difficulty value and the total difficulty value corresponding to the historical SQL statements is calculated. The loss is optimized using an optimizer until a fit is achieved on the historical SQL statement data list, thereby obtaining the first, second, and third weighting factors.
[0073] In summary, by obtaining the first, second, third, and fourth weighting factors, the final difficulty value is obtained and matched with the difficulty level list to obtain the corresponding difficulty level. By using a linear regression function fitting method, the requirement to fix the first, second, and third difficulty values for each difficulty level is no longer necessary, making it more flexible.
[0074] Embodiments of the present invention also provide a non-transitory computer-readable storage medium that can be disposed in an electronic device to store at least one instruction or at least one program related to implementing a method in the method embodiments, wherein the at least one instruction or the at least one program is loaded and executed by the processor to implement the method provided in the above embodiments.
[0075] Embodiments of the present invention also provide an electronic device, including a processor and the aforementioned non-transitory computer-readable storage medium.
[0076] Embodiments of the present invention also provide a computer program product including program code, which, when the program product is run on an electronic device, causes the electronic device to perform the steps of the methods described above in various exemplary embodiments of the present invention.
[0077] While specific embodiments of the invention have been described in detail by way of example, those skilled in the art should understand that the above examples are for illustrative purposes only and are not intended to limit the scope of the invention. Those skilled in the art should also understand that various modifications can be made to the embodiments without departing from the scope and spirit of the invention. The scope of the invention is defined by the appended claims.
Claims
1. An NL2SQL method based on an LLM model, characterized in that, The method includes the following steps: S1, Obtain the target statement for the first business, wherein the target statement for the first business is a statement in natural language, and the first business is one of a plurality of preset businesses in a preset domain; S2, input the target statement of the first business into the target LLM model, and obtain the SQL statement corresponding to the target statement of the first business; The target LLM model is obtained through the following steps: S100, Obtain the list of natural language statements for training A={A1, A2, ..., A...} i A m The training SQL statement list B = {B1, B2, ..., B} corresponds to A. i B m }, A i B is the i-th training natural language statement used in the first business. i It is A i The training SQL statements for the transformation, where the value of i ranges from 1 to m, and m is the number of training natural statements used in the first business. S200, Input the training natural language statement list A into the initial LLM model to obtain the predicted SQL statement list C = {C1, C2, ..., C...} i C m }, C i It is A i The predicted SQL statement output by the initial LLM model after inputting the initial LLM model; S300, based on a list of SQL functions, for C i Split it to get C i The corresponding key-value pairs, where the key of the key-value pair is an SQL function or operator, and the value in the key-value pair is the number of parameters and operators corresponding to the SQL function, wherein the SQL function list J = {J1, J2, ..., J...} r , ..., J s }, J r It is the r-th SQL function or operator, where r ranges from 1 to s, and s is the total number of SQL functions and operators; S400, obtain the difficulty level list D={D1, D2, ..., D...} j D n }, and based on C i and C i The corresponding key-value pair, get C i The corresponding difficulty level is D. j , where D j This is the j-th difficulty level, where j ranges from 1 to n, and n is the number of difficulty levels; in S400, based on C... i and C i The corresponding key-value pair, get C i The corresponding difficulty level is D. j Specifically, the steps include the following: S410, Calculate the number of non-empty values in the first key-value pair and denote it as the first difficulty value H. i1 In the first key-value pair, the key must include at least an operator; S420, obtain the number of select functions as the second difficulty value H. i2 ; S430, obtain the third difficulty value H i3 The third difficulty value is the number of restrictions greater than 1 contained in the specified database function corresponding to the key in the key-value pair; S440, based on H i1 H i2 and H i3 Match it with the difficulty level list D to obtain C. i The corresponding difficulty level; S500, acquisition difficulty level is D. j Given a list of predicted SQL statements, and based on the training SQL statement list B corresponding to A, the computation difficulty level is D. j The loss E corresponding to the list of predicted SQL statements j ; S600, if for any E j E exists in all of them j ≤F j The initial LLM model is used as the target LLM model, where F j It is D j The corresponding loss threshold.
2. The NL2SQL method based on the LLM model according to claim 1, characterized in that, The S430 specifically includes the following steps: S431, obtain C i The number of aggregate functions in the middle; if the number of aggregate functions is greater than 1, AH i3 =1, otherwise, AH i3 =0; S432, Get C i The number of query conditions in the WHERE function, if C i When the number of query conditions in the WHERE function is greater than 1, BH i3 =1, otherwise, BH i3 =0; S433, obtain C i The number of constraints in the groupby function, if C i The number of constraints in the groupby function is greater than 1. i3 =1, otherwise, CH i3 =0; S434, Get C i The number of select functions in C, if C i The number of select functions in the DH array is greater than 1. i3 =1, otherwise, DH i3 =0; S435, Get H i3 =AH i3 +BH i3 +CH i3 +DH i3 .
3. The NL2SQL method based on the LLM model according to claim 1, characterized in that, In S440, when n=4, the difficulty level list D={D1, D2, D3, D4}, where the difficulty level of D1 < the difficulty level of D2 < the difficulty level of D3.
4. The NL2SQL method based on the LLM model according to claim 3, characterized in that, The S440 also includes: S401, if H i1 ≤1, H i2 =0, H i3 =0, C i The corresponding difficulty level is D1; S402, if H i1 ≤1, H i2 =0, H i3 ≤2, or H i1 ≤2, H i2 =0, H i3 ≤1, C i The corresponding difficulty level is D2; S403, if H i1 ≤2, H i2 =0, H i3 >2, or 2<H i1 ≤3, H i2 =0, H i3 ≤2, or H i1 ≤1, H i2 ≤1, H i3 =0, C i The corresponding difficulty level is D3; S404, otherwise, C i The corresponding difficulty level is D4.
5. The NL2SQL method based on the LLM model according to claim 1, characterized in that, The S440 specifically includes: S441, Get G i =w1×H i1 +w2×H i2 +w3×H i3 Where w1 is the first weighting factor, w2 is the second weighting factor, and w3 is the third weighting factor; S442, using G i Match with difficulty level list D to obtain C. i The corresponding difficulty level.
6. The NL2SQL method based on the LLM model according to claim 5, characterized in that, Obtain w1, w2, and w3 using the following steps: S4411, Retrieve historical SQL statement data list K={K1, K2, ..., K...} g , ..., K z }, the g-th historical SQL statement data K g =(K g1 K g2 K g3 K g0 ), K g1 K is the first difficulty value corresponding to the g-th historical SQL statement. g2 K is the second difficulty value corresponding to the g-th historical SQL statement. g3 K is the third difficulty value corresponding to the g-th historical SQL statement. g0 It is the total difficulty value corresponding to the g-th historical SQL statement; S4412, based on the historical SQL statement data list K, use a linear regression function to fit the data and obtain w1, w2 and w3.
7. The NL2SQL method based on the LLM model according to claim 6, characterized in that, S4412 also includes: using the log-likelihood cost function as the loss function.
8. The NL2SQL method based on the LLM model according to claim 6, characterized in that, S4412 also includes: using adam as the optimizer.
9. A non-transitory computer-readable storage medium, wherein the storage medium stores at least one instruction or at least one program segment, characterized in that, The at least one instruction or the at least one program segment is loaded and executed by the processor to implement the NL2SQL method based on the LLM model as described in any one of claims 1-8.
Citation Information
Patent Citations
Improved NL2SQL method based on generative large language model
CN116821168A
Data training method for LLM model and storage medium
CN117131945A
