A SQL statement generation method and related device
By using a combination of large language model and supervised fine-tuning in Text-to-SQL technology, the Text-to-SQL target sample is determined and mixed training is carried out, and the problem of poor accuracy in SQL statement generation in the existing technology is solved, and the implementation of the problem of "unsatisfactory" is achieved. More efficient SQL statement generation.
Patent Information
- Application Number
- CN202411006589.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-25
- Publication Date
- 2024-12-20
- Estimated Expiration
- 2044-07-25
AI Technical Summary
The existing Text-to-SQL technology generates SQL statements with poor accuracy and is difficult to meet the writing needs of non-professional users.
By obtaining the pending question and database table structure information, using the Text-to-SQL dataset to train the open source large language model, determine the Text-to-SQL target sample similar to the question, and input it into the model Generate SQL statements, combined with mixed training strategies with supervised fine-tuning and direct preference optimization, improve generation accuracy.
It significantly improves the accuracy of SQL statement generation, takes advantage of the powerful capabilities of large language models and provides more reference examples, reducing generation costs.
Smart Images

Figure CN118535591B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of natural language processing technology, and in particular to a SQL statement generation method and related devices. Background Art
[0002] In modern information systems, databases are important tools for storing and managing data. SQL (Structured Query Language) is a standard language for managing databases and is widely used for database creation, query, update, and deletion operations. There are many types of SQL statements, and the writing of SQL statements depends on a deep understanding of the database structure and SQL. For non-professional users, it is difficult to directly write SQL statements. Therefore, it is particularly necessary to provide a technology that converts natural language text into SQL statements (referred to as Text-to-SQL technology in this article).
[0003] However, the accuracy of SQL statements generated by current Text-to-SQL technologies is relatively poor.
[0004] Therefore, how to provide a Text-to-SQL technology to improve the accuracy of SQL statement generation has become a technical problem that needs to be solved urgently by those skilled in the art. Summary of the invention
[0005] In view of the above problems, the present application provides a method and related device for generating SQL statements to improve the accuracy of generating SQL statements. The specific solution is as follows:
[0006] The first aspect of the present application provides a method for generating an SQL statement, comprising:
[0007] Get the questions to be processed and the database table structure information;
[0008] Determine a Text-to-SQL target sample; the Text-to-SQL target sample is Text-to-SQL data similar to the question in the Text-to-SQL data set;
[0009] The question, the database table structure information and the Text-to-SQL target sample are input into a Text-to-SQL model to obtain an SQL statement generated by the Text-to-SQL model as an SQL statement corresponding to the question; the Text-to-SQL model is obtained by training an open source large language model using the Text-to-SQL dataset.
[0010] In a possible implementation, determining the Text-to-SQL target sample includes:
[0011] Determine a first Text-to-SQL example by using the sentence similarity between the sentence and each sentence in the Text-to-SQL dataset;
[0012] The first Text-to-SQL sample is determined as the Text-to-SQL target sample.
[0013] In a possible implementation, determining the Text-to-SQL target sample includes:
[0014] Determine a first Text-to-SQL example by using the sentence similarity between the sentence and each sentence in the Text-to-SQL dataset;
[0015] Inputting the question, the database table structure information, and the first Text-to-SQL example into the Text-to-SQL model to obtain a first SQL statement generated by the Text-to-SQL model;
[0016] Determine a second Text-to-SQL example by using the question sentence similarity between the question sentence and each question sentence in the Text-to-SQL dataset, and the SQL statement similarity between the first SQL statement and each SQL statement in the Text-to-SQL dataset;
[0017] The second Text-to-SQL sample is determined as the Text-to-SQL target sample.
[0018] In a possible implementation, the training method of the Text-to-SQL model includes:
[0019] Get a pre-trained large language model;
[0020] Determine the Text-to-SQL supervised fine-tuning dataset;
[0021] Using the Text-to-SQL supervised fine-tuning dataset, supervised fine-tuning the pre-trained large language model is performed to obtain a fine-tuned large language model;
[0022] Determine the Text-to-SQL preference optimization data set;
[0023] Based on the Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset, a direct preference optimization and supervised fine-tuning hybrid training strategy is adopted to perform direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model to obtain the Text-to-SQL model.
[0024] In a possible implementation, determining a Text-to-SQL supervised fine-tuning dataset includes:
[0025] For each piece of original training data in the Text-to-SQL data set, determining a sample of the original training data;
[0026] The Text-to-SQL supervised fine-tuning dataset is constructed by using each piece of original training data and a sample of each piece of original training data in the Text-to-SQL dataset.
[0027] In a possible implementation, determining the sample of the original training data includes:
[0028] The remaining original training data in the Text-to-SQL dataset except the original training data are sorted according to the question similarity, and candidate samples are selected according to the beta distribution;
[0029] The candidate samples are sorted according to the similarity of the SQL statements, and the samples of the original training data are selected according to the beta distribution.
[0030] In a possible implementation, determining the Text-to-SQL preference optimization data set includes:
[0031] Get the original Text-to-SQL training data to be optimized;
[0032] For each original SQL statement in the Text-to-SQL original training data to be optimized, generate an equivalent SQL statement corresponding to the original SQL statement;
[0033] Checking each of the equivalent SQL statements to obtain an equivalent SQL statement that passes the check;
[0034] Using a preset SQL statement optimization category, a target equivalent SQL statement is screened from the checked equivalent SQL statements to be used as an optimized SQL statement corresponding to the original SQL statement;
[0035] The Text-to-SQL preference optimization data set is generated by using the optimized SQL statements corresponding to the original SQL statements.
[0036] In a possible implementation, based on the Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset, a direct preference optimization and supervised fine-tuning hybrid training strategy (DPO-SFT) is adopted to perform direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model, including:
[0037] Modifying the format of the Text-to-SQL supervised fine-tuning dataset to make it consistent with the format of the Text-to-SQL preference optimization dataset, and obtaining a modified Text-to-SQL supervised fine-tuning dataset;
[0038] Using the modified Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset as training data, performing direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model based on a target loss function;
[0039] The target loss function is jointly determined based on the supervised fine-tuning loss function and the direct preference optimization loss function.
[0040] A second aspect of the present application provides a SQL statement generating device, comprising:
[0041] An acquisition unit, used to acquire the question sentence to be processed and the database table structure information;
[0042] A Text-to-SQL target sample determining unit, used to determine a Text-to-SQL target sample; the Text-to-SQL target sample is Text-to-SQL data similar to the question sentence in the Text-to-SQL data set;
[0043] The SQL statement generation unit is used to input the question, the database table structure information and the Text-to-SQL target sample into a Text-to-SQL model to obtain the SQL statement generated by the Text-to-SQL model as the SQL statement corresponding to the question; the Text-to-SQL model is obtained by training an open source large language model using the Text-to-SQL dataset.
[0044] In a possible implementation, the Text-to-SQL target sample determination unit is specifically used to:
[0045] Determine a first Text-to-SQL example by using the sentence similarity between the sentence and each sentence in the Text-to-SQL dataset;
[0046] The first Text-to-SQL sample is determined as the Text-to-SQL target sample.
[0047] In a possible implementation, the Text-to-SQL target sample determination unit is specifically used to:
[0048] Determine a first Text-to-SQL example by using the sentence similarity between the sentence and each sentence in the Text-to-SQL dataset;
[0049] Inputting the question, the database table structure information, and the first Text-to-SQL example into the Text-to-SQL model to obtain a first SQL statement generated by the Text-to-SQL model;
[0050] Determine a second Text-to-SQL example by using the question sentence similarity between the question sentence and each question sentence in the Text-to-SQL dataset, and the SQL statement similarity between the first SQL statement and each SQL statement in the Text-to-SQL dataset;
[0051] The second Text-to-SQL sample is determined as the Text-to-SQL target sample.
[0052] In a possible implementation, the apparatus further includes a Text-to-SQL model training unit, and the Text-to-SQL model training unit includes:
[0053] A pre-trained large language model acquisition unit, used to acquire a pre-trained large language model;
[0054] A Text-to-SQL supervised fine-tuning data set determination unit, used to determine a Text-to-SQL supervised fine-tuning data set;
[0055] A supervised fine-tuning unit, used to use the Text-to-SQL supervised fine-tuning dataset to perform supervised fine-tuning on the pre-trained large language model to obtain a fine-tuned large language model;
[0056] A Text-to-SQL preference optimization data set determination unit, used to determine a Text-to-SQL preference optimization data set;
[0057] A hybrid training unit is used to perform direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model based on the Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset, adopting a direct preference optimization and supervised fine-tuning hybrid training strategy to obtain the Text-to-SQL model.
[0058] In a possible implementation, the Text-to-SQL supervised fine-tuning dataset determination unit includes:
[0059] An original training data sample determination unit, configured to determine a sample of the original training data for each piece of original training data in the Text-to-SQL data set;
[0060] The Text-to-SQL supervised fine-tuning dataset construction unit is used to construct the Text-to-SQL supervised fine-tuning dataset by using each original training data and the sample of each original training data in the Text-to-SQL dataset.
[0061] In a possible implementation, the sample determination unit of the original training data is specifically used to:
[0062] The remaining original training data in the Text-to-SQL dataset except the original training data are sorted according to the question similarity, and candidate samples are selected according to the beta distribution;
[0063] The candidate samples are sorted according to the similarity of the SQL statements, and the samples of the original training data are selected according to the beta distribution.
[0064] In a possible implementation, the Text-to-SQL preference optimization data set determination unit is specifically used to:
[0065] Get the original Text-to-SQL training data to be optimized;
[0066] For each original SQL statement in the Text-to-SQL original training data to be optimized, generate an equivalent SQL statement corresponding to the original SQL statement;
[0067] Checking each of the equivalent SQL statements to obtain an equivalent SQL statement that passes the check;
[0068] Using a preset SQL statement optimization category, a target equivalent SQL statement is screened from the checked equivalent SQL statements to be used as an optimized SQL statement corresponding to the original SQL statement;
[0069] The Text-to-SQL preference optimization data set is generated by using the optimized SQL statements corresponding to the original SQL statements.
[0070] In a possible implementation, the hybrid training unit is specifically used to:
[0071] Modifying the format of the Text-to-SQL supervised fine-tuning dataset to make it consistent with the format of the Text-to-SQL preference optimization dataset, and obtaining a modified Text-to-SQL supervised fine-tuning dataset;
[0072] Using the modified Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset as training data, performing direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model based on a target loss function;
[0073] The target loss function is jointly determined based on the supervised fine-tuning loss function and the direct preference optimization loss function.
[0074] A third aspect of the present application provides a computer program product, including computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements the SQL statement generation method of the first aspect or any implementation of the first aspect.
[0075] A fourth aspect of the present application provides an electronic device, comprising at least one processor and a memory connected to the processor, wherein:
[0076] The memory is used to store computer programs;
[0077] The processor is used to execute the computer program so that the electronic device can implement the SQL statement generation method of the above-mentioned first aspect or any implementation manner of the first aspect.
[0078] In a fifth aspect, the present application provides a computer storage medium, which carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can generate a SQL statement according to the first aspect or any implementation of the first aspect.
[0079] By means of the above technical scheme, the present application provides a method and related device for generating SQL statements. The Text-to-SQL model is obtained by pre-training an open source large language model with a Text-to-SQL data set. After obtaining the question to be processed and the database table structure information, Text-to-SQL data similar to the question is first determined from the Text-to-SQL data set as a Text-to-SQL target sample. The question, the database table structure information and the Text-to-SQL target sample are then input into the Text-to-SQL model to obtain a SQL statement generated by the Text-to-SQL model as the SQL statement corresponding to the question. On the one hand, this scheme utilizes the super power of the large language model, and on the other hand, provides the large language model with a more reference Text-to-SQL target sample, thereby improving the accuracy of SQL statement generation. BRIEF DESCRIPTION OF THE DRAWINGS
[0080] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.
[0081] Figure 1 A flowchart of a method for generating a SQL statement provided in an embodiment of the present application;
[0082] Figure 2 An example schematic diagram of generating SQL statements using a Text-to-SQL model provided in an embodiment of the present application;
[0083] Figure 3 A flowchart of a training method of a Text-to-SQL model disclosed in an embodiment of the present application;
[0084] Figure 4 A schematic diagram of a method for determining a Text-to-SQL preference optimization data set provided in an embodiment of the present application;
[0085] Figure 5 A schematic diagram of the structure of a SQL statement generation device provided in an embodiment of the present application;
[0086] Figure 6 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0087] Before introducing this application scheme, the terms involved in this article are first explained:
[0088] prompt: Instruction. When communicating with AI (such as a large language model), you need to send instructions to the AI. It can be a text description, such as "Please recommend me a pop song" when you are talking to the AI, or it can be a parameter description in a certain format. For example, if you want the AI to draw a picture in a certain format, you need to describe the relevant drawing parameters.
[0089] Large language model: (LLM) refers to a generative deep neural network model based on the Transformer structure, which has super strong language comprehension, generation, logical reasoning and multi-round dialogue capabilities. Common large language models include ChatGPT model, PaLM (Pathways Language Model) model, Pangu large model, Wenxin Yiyan large model, Xinghuo cognitive large model, etc.
[0090] The following describes the embodiments of the present application in conjunction with the drawings in the embodiments of the present application. The terms used in the implementation method section of the present application are only used to explain the specific embodiments of the present application, and are not intended to limit the present application.
[0091] The embodiments of the present application are described below in conjunction with the accompanying drawings. Those skilled in the art will appreciate that, with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0092] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and need not be used to describe a specific order or sequential order. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances, which is only to describe the distinction mode adopted by the objects of the same attributes when describing in the embodiments of the present application. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, so that the process, method, system, product or equipment comprising a series of units need not be limited to those units, but may include other units that are not clearly listed or inherent to these processes, methods, products or equipment.
[0093] At present, Text-to-SQL technology usually uses deep learning algorithms to fine-tune Seq2Seq (sequence to sequence) pre-trained models (such as BERT, T5, etc.) so that the pre-trained models learn how to convert user questions into SQL statements. This method requires a large amount of training data, including database table structure information, user questions, and corresponding SQL statements. Since the training data does not contain samples, after the pre-trained model is fine-tuned for a specific task, the model overfits to the fine-tuning data format and usually loses the ability to learn context. Therefore, sample prompts cannot be used to improve the performance of the fine-tuned pre-trained model on Text-to-SQL tasks.
[0094] Recently, with the rise of large language models (LLM), its powerful capabilities can handle almost all NLP (Natural Language Processing) problems. Compared with previous small models, it is more generalizable and has certain learning and thinking capabilities. Therefore, large language models can also be used to implement Text-to-SQL technology. This method mainly calls the Text-to-SQL API (Application Programming Interface) provided by the closed-source large language model, inputs user questions and database table structure information, and the large language model generates the corresponding SQL statement. However, this method is heavily dependent on the closed-source large language model, and the call of the closed-source large language model API will incur additional overhead, resulting in a high cost for converting natural language text into SQL statements.
[0095] In order to solve the above problems, the embodiment of the present application provides a method for generating SQL statements. The following is a detailed introduction to the method for generating SQL statements in the embodiment of the present application in conjunction with the accompanying drawings.
[0096] Reference Figure 1 , Figure 1 A flowchart of a method for generating a SQL statement provided in an embodiment of the present application is shown in FIG. Figure 1 As shown, an embodiment of the present application provides a method for generating a SQL statement, which may include the following steps:
[0097] S101: Obtaining the question sentence to be processed and database table structure information;
[0098] In this application, the question is input by the user in natural language, regardless of the language, such as "What are flight numbers of flights departing from City'Aberdeen'?". The database table structure information can also be input by the user, such as uploaded by the user through an electronic device. In this application, the database table structure information is used to represent the structure of the database table, and can specifically be a database table structure text sequence, such as: airlines: uid, airline, abbreviation, county; airports: city, airportcode, airportname, county, countyyabbrev; flights: airline, flightno, sourceairport, destairport.
[0099] S102: Determine a Text-to-SQL target sample; the Text-to-SQL target sample is Text-to-SQL data similar to the question in the Text-to-SQL data set;
[0100] In this application, the Text-to-SQL dataset includes multiple original training data, each of which does not contain samples but only contains a question and a SQL statement corresponding to the question. In this application, the Text-to-SQL target sample is the Text-to-SQL data in the Text-to-SQL dataset that is at least similar to the question.
[0101] As an implementable embodiment, the determining of the Text-to-SQL target sample includes: determining a first Text-to-SQL sample by using the sentence similarity between the question and each sentence in the Text-to-SQL data set; and determining the first Text-to-SQL sample as the Text-to-SQL target sample.
[0102] Specifically, question similarity can be used to find k original training data similar to the question from the Text-to-SQL data set as the first Text-to-SQL sample; in this application, when calculating question similarity, the part of the question involving the database (such as table name, column name, Value, etc.) can be masked first to obtain the question skeleton, and then the similarity between the two question skeletons is calculated as the question similarity. The value of k can be set based on the scenario requirements, and this application does not impose any restrictions on this.
[0103] Examples of question sentences and question skeletons are as follows:
[0104] Question: Show the name of singers whose birth year is either 1948 or 1949 ?
[0105] Question skeleton: Show the <mask>< / mask> of <mask>< / mask> whose <mask> <mask>< / mask> < / mask> is either <unk > or <unk>< / unk> ?
[0106] In order to further improve the reference value of the examples, as another feasible implementation method, the determining of the Text-to-SQL target example includes: determining a first Text-to-SQL example by using the sentence similarity between the question and each sentence in the Text-to-SQL data set; inputting the question, the database table structure information, and the first Text-to-SQL example into the Text-to-SQL model to obtain a first SQL statement generated by the Text-to-SQL model; determining a second Text-to-SQL example by using the sentence similarity between the question and each sentence in the Text-to-SQL data set, and the SQL statement similarity between the first SQL statement and each SQL statement in the Text-to-SQL data set; and determining the second Text-to-SQL example as the Text-to-SQL target example.
[0107] It should be noted that the method for calculating question similarity can refer to the aforementioned related description, which will not be repeated here. The original training data in the Text-to-SQL dataset can be sorted according to the question similarity between the question and each question in the Text-to-SQL dataset, and the SQL statement similarity between the first SQL statement and each SQL statement in the Text-to-SQL dataset, and the k original training data with the top comprehensive ranking are selected as the second Text-to-SQL samples. Among them, the method for calculating question similarity can refer to the aforementioned related description, which will not be repeated here. The value of k can be set based on the scenario requirements, and this application does not impose any restrictions on this.
[0108] When calculating the similarity of SQL statements, you can first extract the SQL statement skeleton (such as removing the table name, column name, Value, etc. involved in the SQL statement), and then calculate the similarity between the two SQL statement skeletons as the SQL statement similarity.
[0109] It should be noted that in the present application, any similarity calculation algorithm can be used to calculate the similarity between two question skeletons and the similarity between two SQL statement skeletons, such as Euclidean distance, jaccard similarity, etc., and the present application does not impose any limitation on this.
[0110] Examples of SQL statements and SQL statement skeletons are as follows:
[0111] SQL statement: select name from singer where birth_year =1948 or birth_year= 1949
[0112] SQL statement skeleton: select_from_where_
[0113] The “_” part in the SQL statement skeleton can be filled with specific information to obtain a complete SQL statement.
[0114] S103: Input the question, the database table structure information and the Text-to-SQL target sample into a Text-to-SQL model to obtain an SQL statement generated by the Text-to-SQL model as an SQL statement corresponding to the question; the Text-to-SQL model is obtained by training an open source large language model using the Text-to-SQL dataset.
[0115] In this application, the Text-to-SQL model is trained on the basis of an open source large language model. The open source large language model can be any open source large language model, and this application does not impose any limitation on this. The specific implementation method of using the Text-to-SQL dataset to train the open source large language model to obtain the Text-to-SQL model will be described in detail through the following embodiments, and will not be described in detail here.
[0116] For ease of understanding, refer to Figure 2 , Figure 2 An example schematic diagram of a Text-to-SQL model generating SQL statements provided in an embodiment of the present application is as follows: Figure 2 As shown, the input includes a question, the database table structure information and the Text-to-SQL target sample, and the output is a SQL statement corresponding to the question.
[0117] The present embodiment provides a method for generating SQL statements. The Text-to-SQL data set is used to train an open source large language model in advance to obtain a Text-to-SQL model. After obtaining a question to be processed and database table structure information, Text-to-SQL data similar to the question is first determined from the Text-to-SQL data set as a Text-to-SQL target sample. The question, database table structure information and Text-to-SQL target sample are then input into the Text-to-SQL model to obtain an SQL statement generated by the Text-to-SQL model as the SQL statement corresponding to the question. This solution not only utilizes the super capability of the large language model, but also provides a more reference Text-to-SQL target sample for the large language model. Therefore, the accuracy of SQL statement generation can be improved.
[0118] In another embodiment of the present application, the training method of the Text-to-SQL model is described in detail. Figure 3 , Figure 3 A flow chart of a training method of a Text-to-SQL model disclosed in an embodiment of the present application is provided. The training method may include the following steps:
[0119] S201: Obtain a pre-trained large language model;
[0120] In the present application, the pre-trained large language model can be obtained by pre-training any open source large language model, and the present application does not impose any limitation on this.
[0121] S202: Determine a Text-to-SQL supervised fine-tuning dataset;
[0122] In the present application, a Text-to-SQL supervised fine-tuning dataset includes multiple Text-to-SQL supervised fine-tuning data, each Text-to-SQL supervised fine-tuning data includes a sample, a question, and a SQL statement part, wherein the SQL statement part includes an SQL statement, which is a SQL statement corresponding to the question.
[0123] As an implementable embodiment, determining the Text-to-SQL supervised fine-tuning dataset includes: determining a sample of the original training data for each original training data in the Text-to-SQL dataset; and constructing the Text-to-SQL supervised fine-tuning dataset using each original training data and the sample of each original training data in the Text-to-SQL dataset.
[0124] In the present application, k pieces of original training data similar to the original training data can be searched from the Text-to-SQL data set as k samples of the original training data; wherein, whether the two pieces of original training data are similar can be determined based on at least one of question similarity and SQL statement similarity.
[0125] As an implementable method, for each original training data in the Text-to-SQL dataset, the remaining original training data in the Text-to-SQL dataset except the original training data is first sorted according to question similarity, and candidate samples are selected according to β distribution. Then, the candidate samples are sorted according to SQL statement similarity, and samples of the original training data are selected according to β distribution.
[0126] Specifically, for each original training data in the Text-to-SQL data set, the remaining original training data in the Text-to-SQL data set except the original training data are first sorted according to the question similarity, and j original training data are selected as candidate samples according to the β distribution. Then, the candidate samples are sorted according to the SQL statement similarity, and k original training data are selected as samples of the original training data according to the β distribution, where j>k, and j and k are integers greater than 0.
[0127] The purpose of using the beta distribution to select samples is: if only the most similar samples are provided during training, the trained model will be heavily dependent on the samples provided by the context. When the quality of the matched samples is poor, the model's Text-to-SQL accuracy will drop significantly, and when multiple samples are provided, the model is accustomed to learning from the most similar samples and ignoring other samples. However, when using the beta distribution to select samples, the dependence of the trained model on the most similar samples is reduced due to the introduction of small-probability non-related samples, and the model will pay attention to samples in different positions at the same time when generating results to generate the final SQL statement. There are two parameters α and β in the beta distribution. The specific values of the two parameters can be set based on the scenario requirements. This application does not impose any restrictions on this. In one feasible method, the value of α can be set to 1.5 and the value of β can be set to 0.5.
[0128] In the present application, when calculating the question similarity of two original training data, the part of the question involving the internal database (such as table name, column name, Value, etc.) can be masked first to obtain the question skeleton, and then the similarity between the two question skeletons is calculated as the question similarity; when calculating the SQL statement similarity of two original training data, the SQL statement skeleton can be extracted first (such as removing the table name, column name, Value, etc. involved in the SQL statement), and then the similarity between the two SQL statement skeletons is calculated as the SQL statement similarity.
[0129] In this application, the Text-to-SQL dataset is the Text-to-SQL 0-shot dataset, and the Text-to-SQL supervised fine-tuning dataset is the union of the Text-to-SQL 0-shot dataset, the Text-to-SQL 1-shot dataset, ..., and the Text-to-SQL k-shot dataset, wherein the number of samples of each training data in the Text-to-SQL 0-shot dataset is 0, the number of samples of each training data in the Text-to-SQL 1-shot dataset is 1, the number of samples of each training data in the Text-to-SQL 2-shot dataset is 2, ..., and the number of samples of each training data in the Text-to-SQL k-shot dataset is k, that is, the size of the Text-to-SQL supervised fine-tuning dataset is k+1 times the size of the Text-to-SQL dataset.
[0130] S203: Using the Text-to-SQL supervised fine-tuning dataset, perform supervised fine-tuning on the pre-trained large language model to obtain a fine-tuned large language model;
[0131] Contextual learning ability is an emergent ability of large language models. That is, when a pre-trained large language model is migrated to a new task, it does not need to be retrained. Instead, it only needs to provide a task description and a few examples, and the model can generate the correct output for the new input.
[0132] In this application, a Text-to-SQL supervised fine-tuning dataset is used to perform supervised fine-tuning (SFT) on the pre-trained large language model, so that the fine-tuned large language model has the ability to learn the Text-to-SQL domain context.
[0133] S204: Determine a Text-to-SQL preference optimization data set;
[0134] In the present application, a Text-to-SQL preference optimization data set includes multiple Text-to-SQL preference optimization data, each Text-to-SQL preference optimization data includes a question and an SQL statement part, wherein the SQL statement part includes two SQL statements, one SQL statement is the original SQL statement corresponding to the question, and the other SQL statement is the optimized SQL statement corresponding to the original SQL statement.
[0135] As an implementable method, determining the Text-to-SQL preference optimization data set includes:
[0136] S2041: Obtaining original Text-to-SQL training data to be optimized;
[0137] In the present application, the Text-to-SQL original training data to be optimized is part or all of the original training data in the Text-to-SQL data set.
[0138] S2042: for each original SQL statement in the Text-to-SQL original training data to be optimized, generate an equivalent SQL statement corresponding to the original SQL statement;
[0139] In this application, the original SQL statements and related table structures in the original Text-to-SQL training data to be optimized can be input into a large model (such as GPT-4, Qwen, etc.), and the large model can be guided to generate equivalent SQL statements corresponding to the original SQL statements by means of prompts. It should be noted that the purpose of providing the relevant table structure to the large model by means of prompts is to deepen the large model's understanding of the table fields involved in the SQL statement.
[0140] S2043: Check each of the equivalent SQL statements to obtain an equivalent SQL statement that passes the check;
[0141] In this application, since the large model may generate an equivalent SQL statement in the process of generating an equivalent SQL statement, there may be hallucinations or SQL statements that do not conform to a specific specification (such as MySQL, Oracle, etc.), therefore, each of the equivalent SQL statements needs to be checked in this application. The checking method can be to execute the original SQL statement and each of the equivalent SQL statements. If the execution result of an equivalent SQL statement is consistent with the execution result of the original SQL statement, the equivalent SQL statement passes the check.
[0142] S2044: using a preset SQL statement optimization category, screening out a target equivalent SQL statement from the checked equivalent SQL statements as an optimized SQL statement corresponding to the original SQL statement;
[0143] In the present application, for each verified equivalent SQL statement, it can be evaluated whether the optimization method belongs to the preset SQL statement optimization category compared with the original SQL statement. If so, the verified equivalent SQL statement is determined to be the target equivalent SQL statement.
[0144] In this application, the preset SQL statement optimization categories may include the following:
[0145]
[0146] For ease of understanding, this application provides examples corresponding to the above-mentioned SQL statement optimization categories, as follows:
[0147] 1. Optimization of non-essential components
[0148] Before optimization: select distinct _ from _ | select distinct catalog_entry_namefrom catalog_contents
[0149] After optimization: select distinct (_) from _ | select distinct (catalog_entry_name) from catalog_contents
[0150] 2. Introduce new keywords
[0151] Before optimization: select _ from _ using ( _ ) join _ where _ group by _ | selectdepartment.creation from department join management using ( department_id )join head on management.head_id = head.head_id where head.born_state = 'Alabama' group by department.creation
[0152] After optimization: select distinct _ from _ where _ | select distinctdepartment.creation from department join management on department.department_id = management.department_id join head on management.head_id = head.head_idwhere head.born_state = 'Alabama'
[0153] 3. Table connection optimization
[0154] Before optimization: select distinct _ from _ where _ in ( select _ from _ where _) | select distinct candidate_id from candidates where candidate_id in (select person_id from people where email_address ='stanley.monahan@example.org' )
[0155] After optimization: select _ from _ where _ | select candidates.candidate_id from people join candidates on people.person_id = candidates.candidate_id where people.email_address ='stanley.monahan@example.org'
[0156] 4. Optimization of set operations
[0157] Before optimization: select _ from _ where _ group by _ having avg ( _ ) > _ | select station.name from station join status on station.id = status.station_id where station.city != 'San Jose' group by station.id having avg (status.bikes_available ) > 10
[0158] After optimization: select _ from _ group by _ having avg ( _ ) > _ except select_ from _ where _ | select station.name from station join status on station.id= status.station_id group by status.station_id having avg ( bikes_available )> 10 except select name from station where city = 'San Jose'
[0159] 5. Sorting operation optimization
[0160] Before optimization: select max ( _ ) from _ | select max ( basketball_match.acc_percent ) from basketball_match
[0161] After optimization: select _ from _ order by _ desc limit _ | select acc_percentfrom basketball_match order by acc_percent desc limit 1
[0162] 6. Other optimizations involving SQL framework changes
[0163] Before optimization: select _ from _ where _ not in ( select _ from _ ) | select customer_name from customers where customer_id not in ( select customer_id from first_notification_of_loss )
[0164] After optimization: select _ from _ except select _ from _ | select customer_namefrom customers except select customers.customer_name from customers joinfirst_notification_of_loss on customers.customer_id = first_notification_of_loss.customer_id
[0165] S2045: Generate the Text-to-SQL preference optimization data set by using the optimized SQL statements corresponding to the original SQL statements.
[0166] In the present application, for each Text-to-SQL original training data to be optimized, an optimized SQL statement corresponding to the original SQL statement in the Text-to-SQL original training data to be optimized can be added to obtain Text-to-SQL preference optimization training data, and each Text-to-SQL preference optimization training data is combined to obtain the Text-to-SQL preference optimization data set.
[0167] For ease of understanding, refer to Figure 4 , Figure 4 A schematic diagram of a method for determining a Text-to-SQL preference optimization data set provided in an embodiment of the present application.
[0168] S205: Based on the Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset, a direct preference optimization and supervised fine-tuning hybrid training strategy is adopted to perform direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model to obtain the Text-to-SQL model.
[0169] The Direct Preference Optimization (DPO) method is generally used in text generation tasks, aiming to enable the model to generate richer answers. However, using the Text-to-SQL preference optimization dataset alone to perform direct preference optimization on the fine-tuned large language model will cause the SQL statement logic generated by the trained large language model to be confusing. This is because in the Text-to-SQL task, the output of the model must strictly follow the SQL statement format, and the Direct Preference Optimization (DPO) method lacks supervised fine-tuning loss (SFT Loss), which causes the model generation results to diverge.
[0170] Therefore, in this application, based on the Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset, a direct preference optimization and supervised fine-tuning hybrid training (DPO-SFT) strategy is adopted to perform direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model.
[0171] As an implementable method, based on the Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset, a direct preference optimization and supervised fine-tuning hybrid training strategy (DPO-SFT) is adopted to perform direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model, including:
[0172] The format of the Text-to-SQL supervised fine-tuning dataset is modified to be consistent with the format of the Text-to-SQL preference optimization dataset, so as to obtain a modified Text-to-SQL supervised fine-tuning dataset; the modified Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset are used as training data, and direct preference optimization and supervised fine-tuning hybrid training is performed on the fine-tuned large language model based on a target loss function, where the target loss function is jointly determined based on a supervised fine-tuning loss function and a direct preference optimization loss function.
[0173] In the present application, modifying the format of the Text-to-SQL supervised fine-tuning dataset to make it consistent with the format of the Text-to-SQL preference optimization dataset means modifying the SQL statement part of each Text-to-SQL supervised fine-tuning data in the Text-to-SQL supervised fine-tuning dataset to include two SQL statements, both of which are original SQL statements in the Text-to-SQL supervised fine-tuning data.
[0174] As an implementable embodiment, the target loss function includes a first loss function and a second loss function, the first loss function includes a supervised fine-tuning loss function and a direct preference optimization loss function; the second loss function only includes a supervised fine-tuning loss function, when the training data is the Text-to-SQL preference optimization data set, the first loss function is used, when the training data is the modified Text-to-SQL supervised fine-tuning data set, the second loss function is used.
[0175] Next, the loss function of the direct preference optimization and supervised fine-tuning hybrid training (DPO-SFT) strategy is explained.
[0176] The loss function of the direct preference optimization and supervised fine-tuning hybrid training (DPO-SFT) strategy is determined jointly based on the direct preference optimization loss function (DPO Loss) and the supervised fine-tuning loss function (SFT Loss), where the direct preference optimization loss function (DPO Loss) is expressed as follows:
[0177]
[0178] (1)
[0179] Among them, the optimization target model and reference models From supervised training model (that is, using the Text-to-SQL supervised fine-tuning dataset, the pre-trained large language model is fine-tuned to obtain the fine-tuned large language model) is initialized. For an input data , , Represents the labeled satisfactory and unsatisfactory output data, respectively, in the Text-to-SQL supervised fine-tuning dataset = ,Text-to-SQL prefers to optimize data sets, ≠ .
[0180] The supervised fine-tuning loss function (SFT Loss) is expressed as follows:
[0181] (2)
[0182] According to the above formulas (1) and (2), the loss function (DPO-SFT loss) of the direct preference optimization and supervised fine-tuning hybrid training (DPO-SFT) strategy can be expressed as follows:
[0183] when ≠ , indicating that the input data is a Text-to-SQL preference optimization data set. Down:
[0184]
[0185] (3)
[0186] in, It is a hyperparameter that controls the degree to which the model learns to optimize the data format and is set to 1 in the experiment.
[0187] when = , indicating that the input data Text-to-SQL supervised fine-tuning dataset is now in data distribution Next, order =0,
[0188]
[0189] (4)
[0190] It can be seen that, using the modified Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset as training data, in the process of performing direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model based on the target loss function, the fine-tuned large language model will learn from two data distributions (Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset), so that the fine-tuned large language model can maintain the SQL statement format while performing preference data learning.
[0191] The above describes a method for generating a SQL statement provided in an embodiment of the present application. The following describes a device for executing the above method for generating a SQL statement.
[0192] See also Figure 5 , Figure 5 A schematic diagram of the structure of a SQL statement generation device provided in an embodiment of the present application. Figure 5 As shown, the SQL statement generating device comprises:
[0193] An acquisition unit 11 is used to acquire the question sentence to be processed and the database table structure information;
[0194] A Text-to-SQL target sample determining unit 12 is used to determine a Text-to-SQL target sample; the Text-to-SQL target sample is Text-to-SQL data similar to the question sentence in the Text-to-SQL data set;
[0195] The SQL statement generating unit 13 is used to input the question, the database table structure information and the Text-to-SQL target sample into a Text-to-SQL model, and obtain the SQL statement generated by the Text-to-SQL model as the SQL statement corresponding to the question; the Text-to-SQL model is obtained by training an open source large language model using the Text-to-SQL dataset.
[0196] In a possible implementation, the Text-to-SQL target sample determination unit is specifically used to:
[0197] Determine a first Text-to-SQL example by using the sentence similarity between the sentence and each sentence in the Text-to-SQL dataset;
[0198] The first Text-to-SQL sample is determined as the Text-to-SQL target sample.
[0199] In a possible implementation, the Text-to-SQL target sample determination unit is specifically used to:
[0200] Determine a first Text-to-SQL example by using the sentence similarity between the sentence and each sentence in the Text-to-SQL dataset;
[0201] Inputting the question, the database table structure information, and the first Text-to-SQL example into the Text-to-SQL model to obtain a first SQL statement generated by the Text-to-SQL model;
[0202] Determine a second Text-to-SQL example by using the question sentence similarity between the question sentence and each question sentence in the Text-to-SQL dataset, and the SQL statement similarity between the first SQL statement and each SQL statement in the Text-to-SQL dataset;
[0203] The second Text-to-SQL sample is determined as the Text-to-SQL target sample.
[0204] In a possible implementation, the apparatus further includes a Text-to-SQL model training unit, and the Text-to-SQL model training unit includes:
[0205] A pre-trained large language model acquisition unit, used to acquire a pre-trained large language model;
[0206] A Text-to-SQL supervised fine-tuning data set determination unit, used to determine a Text-to-SQL supervised fine-tuning data set;
[0207] A supervised fine-tuning unit, used to use the Text-to-SQL supervised fine-tuning dataset to perform supervised fine-tuning on the pre-trained large language model to obtain a fine-tuned large language model;
[0208] A Text-to-SQL preference optimization data set determination unit, used to determine a Text-to-SQL preference optimization data set;
[0209] A hybrid training unit is used to perform direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model based on the Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset, adopting a direct preference optimization and supervised fine-tuning hybrid training strategy to obtain the Text-to-SQL model.
[0210] In a possible implementation, the Text-to-SQL supervised fine-tuning dataset determination unit includes:
[0211] An original training data sample determination unit, configured to determine a sample of the original training data for each piece of original training data in the Text-to-SQL data set;
[0212] The Text-to-SQL supervised fine-tuning dataset construction unit is used to construct the Text-to-SQL supervised fine-tuning dataset by using each original training data and the sample of each original training data in the Text-to-SQL dataset.
[0213] In a possible implementation, the sample determination unit of the original training data is specifically used to:
[0214] The remaining original training data in the Text-to-SQL dataset except the original training data are sorted according to the question similarity, and candidate samples are selected according to the beta distribution;
[0215] The candidate samples are sorted according to the similarity of the SQL statements, and the samples of the original training data are selected according to the beta distribution.
[0216] In a possible implementation, the Text-to-SQL preference optimization data set determination unit is specifically used to:
[0217] Get the original Text-to-SQL training data to be optimized;
[0218] For each original SQL statement in the Text-to-SQL original training data to be optimized, generate an equivalent SQL statement corresponding to the original SQL statement;
[0219] Checking each of the equivalent SQL statements to obtain an equivalent SQL statement that passes the check;
[0220] Using a preset SQL statement optimization category, a target equivalent SQL statement is screened from the checked equivalent SQL statements to be used as an optimized SQL statement corresponding to the original SQL statement;
[0221] The Text-to-SQL preference optimization data set is generated by using the optimized SQL statements corresponding to the original SQL statements.
[0222] In a possible implementation, the hybrid training unit is specifically used to:
[0223] Modifying the format of the Text-to-SQL supervised fine-tuning dataset to make it consistent with the format of the Text-to-SQL preference optimization dataset, and obtaining a modified Text-to-SQL supervised fine-tuning dataset;
[0224] Using the modified Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset as training data, performing direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model based on a target loss function;
[0225] The target loss function is jointly determined based on the supervised fine-tuning loss function and the direct preference optimization loss function.
[0226] The present application also provides an electronic device in an embodiment. Figure 6 As shown, it shows a schematic diagram of the structure of an electronic device suitable for implementing the embodiment of the present application. The electronic device in the embodiment of the present application may include but is not limited to fixed terminals such as mobile phones, laptops, PDAs (personal digital assistants), PADs (tablet computers), desktop computers, etc. Figure 6 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0227] like Figure 6 As shown, the electronic device may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 to a random access memory (RAM) 603. When the electronic device is powered on, various programs and data required for the operation of the electronic device are also stored in the RAM 603. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0228] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a memory card, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device to communicate with other devices wirelessly or by wire to exchange data. Although Figure 6 An electronic device having various devices is shown, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0229] An embodiment of the present application also provides a computer program product including computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements any one of the SQL statement generation methods provided in the embodiments of the present application.
[0230] A computer-readable storage medium is also provided in an embodiment of the present application. The computer storage medium carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement any SQL statement generation method provided in the embodiment of the present application.
[0231] It should also be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed over multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the drawings of the device embodiments provided by the present application, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines.
[0232] Through the description of the above implementation mode, the technicians in the field can clearly understand that the present application can be implemented by means of software plus necessary general hardware, and of course, it can also be implemented by special hardware including special integrated circuits, special CPUs, special memories, special components, etc. In general, all functions completed by computer programs can be easily implemented by corresponding hardware, and the specific hardware structure used to implement the same function can also be various, such as analog circuits, digital circuits or special circuits. However, for the present application, software program implementation is a better implementation mode in more cases. Based on such an understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a readable storage medium, such as a computer floppy disk, a U disk, a mobile hard disk, a ROM, a RAM, a disk or an optical disk, etc., including a number of instructions to enable a computer device (which can be a personal computer, a training device, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0233] In the above embodiments, all or part of the embodiments may be implemented by software, hardware, firmware or any combination thereof. When implemented by software, all or part of the embodiments may be implemented in the form of a computer program product.
[0234] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from a website site, a computer, a training device, or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, training device, or data center. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a training device, a data center, etc. that includes one or more available media integrations. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)), etc.
Claims
1. A method for generating a SQL statement, characterized in that: include: Obtaining a question sentence to be processed and database table structure information; wherein the database table structure information is used to represent the structure of the database table; Determine a Text-to-SQL target sample; the Text-to-SQL target sample is Text-to-SQL data similar to the question in the Text-to-SQL data set; The question, the database table structure information and the Text-to-SQL target sample are input into a Text-to-SQL model to obtain an SQL statement generated by the Text-to-SQL model as an SQL statement corresponding to the question; the Text-to-SQL model is obtained by training an open source large language model using the Text-to-SQL dataset.
2. The method according to claim 1, characterized in that The example of determining a Text-to-SQL target includes: Determine a first Text-to-SQL example by using the sentence similarity between the sentence and each sentence in the Text-to-SQL dataset; The first Text-to-SQL sample is determined as the Text-to-SQL target sample.
3. The method according to claim 1, characterized in that The example of determining a Text-to-SQL target includes: Determine a first Text-to-SQL example by using the sentence similarity between the sentence and each sentence in the Text-to-SQL dataset; Inputting the question, the database table structure information, and the first Text-to-SQL example into the Text-to-SQL model to obtain a first SQL statement generated by the Text-to-SQL model; Determine a second Text-to-SQL example by using the question sentence similarity between the question sentence and each question sentence in the Text-to-SQL dataset, and the SQL statement similarity between the first SQL statement and each SQL statement in the Text-to-SQL dataset; The second Text-to-SQL sample is determined as the Text-to-SQL target sample.
4. The method according to any one of claims 1 to 3, characterized in that The training method of the Text-to-SQL model includes: Get a pre-trained large language model; Determine the Text-to-SQL supervised fine-tuning dataset; Using the Text-to-SQL supervised fine-tuning dataset, supervised fine-tuning the pre-trained large language model is performed to obtain a fine-tuned large language model; Determine the Text-to-SQL preference optimization data set; Based on the Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset, a direct preference optimization and supervised fine-tuning hybrid training strategy is adopted to perform direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model to obtain the Text-to-SQL model.
5. The method according to claim 4, characterized in that The determination of the Text-to-SQL supervised fine-tuning dataset includes: For each piece of original training data in the Text-to-SQL data set, determining a sample of the original training data; The Text-to-SQL supervised fine-tuning dataset is constructed by using each piece of original training data and a sample of each piece of original training data in the Text-to-SQL dataset.
6. The method according to claim 5, characterized in that The determining of the sample of the original training data comprises: The remaining original training data in the Text-to-SQL dataset except the original training data are sorted according to the question similarity, and candidate samples are selected according to the beta distribution; The candidate samples are sorted according to the similarity of the SQL statements, and the samples of the original training data are selected according to the beta distribution.
7. The method according to claim 4, characterized in that The determining of the Text-to-SQL preference optimization data set includes: Get the original Text-to-SQL training data to be optimized; For each original SQL statement in the Text-to-SQL original training data to be optimized, generate an equivalent SQL statement corresponding to the original SQL statement; Checking each of the equivalent SQL statements to obtain an equivalent SQL statement that passes the check; Using a preset SQL statement optimization category, a target equivalent SQL statement is screened from the checked equivalent SQL statements to be used as an optimized SQL statement corresponding to the original SQL statement; The Text-to-SQL preference optimization data set is generated by using the optimized SQL statements corresponding to the original SQL statements.
8. The method according to claim 4, characterized in that The method of performing direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model by adopting a direct preference optimization and supervised fine-tuning hybrid training strategy based on the Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset includes: Modifying the format of the Text-to-SQL supervised fine-tuning dataset to make it consistent with the format of the Text-to-SQL preference optimization dataset, and obtaining a modified Text-to-SQL supervised fine-tuning dataset; Using the modified Text-to-SQL supervised fine-tuning dataset and the Text-to-SQL preference optimization dataset as training data, performing direct preference optimization and supervised fine-tuning hybrid training on the fine-tuned large language model based on a target loss function; The target loss function is jointly determined based on the supervised fine-tuning loss function and the direct preference optimization loss function.
9. A SQL statement generating device, characterized in that: include: An acquisition unit, used to acquire the question sentence to be processed and database table structure information; wherein the database table structure information is used to represent the structure of the database table; A Text-to-SQL target sample determining unit, used to determine a Text-to-SQL target sample; the Text-to-SQL target sample is Text-to-SQL data similar to the question sentence in the Text-to-SQL data set; The SQL statement generation unit is used to input the question, the database table structure information and the Text-to-SQL target sample into a Text-to-SQL model to obtain the SQL statement generated by the Text-to-SQL model as the SQL statement corresponding to the question; the Text-to-SQL model is obtained by training an open source large language model using the Text-to-SQL dataset.
10. A computer program product, characterized in that The method comprises computer-readable instructions, and when the computer-readable instructions are executed on an electronic device, the electronic device implements the SQL statement generating method as claimed in any one of claims 1 to 8.
11. An electronic device, characterized in that: The method comprises at least one processor and a memory connected to the processor, wherein: The memory is used to store computer programs; The processor is used to execute the computer program so that the electronic device can implement the SQL statement generation method as described in any one of claims 1 to 8.
12. A computer storage medium, characterized in that: The computer storage medium carries one or more computer programs, and when the one or more computer programs are executed by an electronic device, the electronic device can implement the SQL statement generation method as described in any one of claims 1 to 8.
Citation Information
Patent Citations
Method, system and device for converting natural language query into SQL and storage medium
CN114547072A
Improved NL2SQL method based on generative large language model
CN116821168A
Method and device for converting natural language into database statement, equipment and medium
CN117370378A
SQL (Structured Query Language) statement processing model construction and SQL statement generation method and device
CN118051594A
Method, device and equipment for converting natural language into SQL (Structured Query Language) and storage medium
CN118093646A
