Construction method, generation method and device of Text-to-SQL model, equipment and medium
By augmenting the training data of the Text-to-SQL model, generating an enhanced training data set and building a SQL generation model, the problem of insufficient training data in the existing technology is solved, and efficient Text-to-SQL model construction and SQL are realized. Statement generation.
Patent Information
- Application Number
- CN202411858657.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-17
- Publication Date
- 2025-01-14
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The lack of training data of existing Text-to-SQL models leads to high cost and time for model construction, limiting its application scenarios.
By obtaining the original training data set, data augmentation is performed, and augmented training data sets are generated, including enhanced SQL data pairs and inference data pairs, which are used to train SQL generative models, thereby building a Text-to-SQL model.
It reduces labor costs and time costs, improves the reasoning ability of the Text-to-SQL model and the quality of generating SQL statements.
Smart Images

Figure CN119311721A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of natural language processing, and in particular to a method for constructing, generating, apparatus, device and medium of a Text-to-SQL model. 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 very difficult to directly write SQL statements.
[0003] To this end, some researchers have proposed a technology that converts natural language text into SQL statements (referred to as Text-to-SQL technology here), that is, to generate SQL statements through a pre-trained Text-to-SQL model. However, in actual applications, there is a lack of training data in many application scenarios. Generally, the training data is enhanced manually and then the Text-to-SQL model is trained, but this requires high labor and time costs, which limits the use of the Text-to-SQL model.
[0004] Therefore the prior art still needs to be improved and enhanced. Summary of the invention
[0005] The technical problem to be solved by the present application is to provide a method for constructing, generating, apparatus, device and medium of a Text-to-SQL model in view of the deficiencies in the prior art.
[0006] In order to solve the above technical problems, the first aspect of the present application provides a method for constructing a Text-to-SQL model, wherein the method for constructing a Text-to-SQL model specifically includes: Acquire an original training data set, wherein the original training data set includes an original SQL data pair, and the original SQL data pair includes an original question and an original SQL statement; Performing data enhancement on the original training data set to obtain an enhanced training data set, wherein the enhanced training data set includes original SQL data pairs, enhanced reasoning data pairs, and enhanced SQL data pairs, the enhanced reasoning data pairs include reasoning path descriptions of original questions and original SQL statements, and the enhanced SQL data pairs include enhanced questions and enhanced SQL statements; A preset generation model is trained based on the enhanced training data set to obtain a SQL generation model, and a Text-to-SQL model is determined based on the SQL generation model.
[0007] In the method for constructing the Text-to-SQL model, the step of performing data enhancement on the original training data set to obtain the enhanced training data set specifically includes: Obtaining an original SQL statement of each original SQL data pair in the original training data set, and obtaining a sub-SQL statement set and an SQL clause set according to the original SQL statement; Constructing an enhanced SQL data pair corresponding to the original SQL data pair based on the sub-SQL statement set; An enhanced inference data pair corresponding to the original SQL data pair is constructed based on the sub-SQL statement set and the SQL clause set.
[0008] The method for constructing the Text-to-SQL model, wherein the step of obtaining the original SQL statement of each original SQL data pair in the original training data set and obtaining the sub-SQL statement set and the SQL clause set according to the original SQL statement specifically comprises: Parsing the original SQL statement in each of the original SQL data pairs to obtain a SQL clause set corresponding to the original SQL statement; Merge the SQL clauses in the acquired SQL clause set to obtain a candidate sub-SQL statement set; All executable sub-SQL statements in the candidate sub-SQL statement set are obtained to obtain a sub-SQL statement set.
[0009] The method for constructing the Text-to-SQL model, wherein the step of constructing the enhanced SQL data pair corresponding to the original SQL data pair based on the sub-SQL statement set specifically includes: Selecting a first target sub-SQL statement from the sub-SQL statement set as an enhanced SQL statement, and obtaining an enhanced SQL statement set according to the enhanced SQL statement; Generating a first prompt word for each enhanced SQL statement in the enhanced SQL statement set; The first prompt word is input into a preset large language model, and an enhanced question corresponding to the enhanced SQL statement is output through the preset large language model, wherein the enhanced SQL statement corresponds to the enhanced question one by one.
[0010] The method for constructing the Text-to-SQL model, wherein the step of constructing the enhanced reasoning data pair corresponding to the original SQL data pair based on the sub-SQL statement set and the SQL clause set specifically includes: Selecting a second target sub-SQL statement from the sub-SQL statement set to obtain a second target sub-SQL statement set; Determine a reasoning path of the original SQL statement based on the second target sub-SQL statement set and the SQL clause set, wherein the reasoning path corresponds one-to-one to the original question; Based on the reasoning path and the original question, determining a reasoning path description of the original SQL statement by using a preset large language model; An enhanced reasoning data pair of the original SQL data pair is obtained according to the original question and the reasoning path description.
[0011] The method for constructing the Text-to-SQL model, wherein the step of determining the reasoning path of the original SQL statement based on the second target sub-SQL statement set and the SQL clause set specifically comprises: Acquire a target SQL clause in the SQL clause set that is not included in the second target sub-SQL statement set; According to the first preset condition, gradually adding the acquired target SQL clauses to the second target sub-SQL statement set, wherein only one target SQL clause is added in each step; Based on the first preset condition and the second target sub-SQL statement set, an inference path of the original SQL statement is obtained.
[0012] The method for constructing the Text-to-SQL model, wherein the step of determining the reasoning path description of the original SQL statement by presetting a large language model based on the reasoning path and the original question specifically includes: Constructing a second prompt word based on the reasoning path and the original question; The second prompt word is input into a preset large language model, and the reasoning path description of the original SQL statement is output through the preset large language model.
[0013] The method for constructing the Text-to-SQL model, wherein the step of training the preset generation model based on the enhanced training data set to obtain the SQL generation model specifically includes: Constructing a SQL statement training data set based on the SQL data pairs in the enhanced training data set, wherein the SQL data pairs include original SQL data pairs and enhanced SQL data pairs; Constructing a reasoning path description training data set according to the enhanced reasoning data pairs in the enhanced training data set; The preset generation model is trained based on the SQL statement training data set and the reasoning path description training data set to obtain an SQL generation model.
[0014] The method for constructing the Text-to-SQL model, wherein the step of constructing the SQL statement training data set based on the SQL data in the enhanced training data set specifically includes: For each SQL data pair, construct an SQL prefix for the SQL data pair, and obtain database information corresponding to the question in the SQL data pair; generating a first type of input data based on the SQL prefix, the database information, and a question in the SQL data pair; SQL statement training data is formed based on the first type of input data and the SQL statements in the SQL data pair to obtain a SQL statement training data set.
[0015] The method for constructing the Text-to-SQL model, wherein the step of constructing the reasoning path description training data set based on the enhanced reasoning data in the enhanced training data set specifically includes: For each enhanced reasoning data pair, construct a reasoning prefix for the enhanced reasoning data pair, and obtain database information corresponding to the original question in the enhanced reasoning data pair; A second type of input data is generated based on the reasoning prefix, the database information and the original question, and reasoning path description training data is formed based on the second type of input data and the reasoning path description in the enhanced reasoning data pair to obtain a reasoning path description training data set.
[0016] The method for constructing the Text-to-SQL model, wherein the step of training a preset generation model based on the SQL statement training data set and the reasoning path description training data set to obtain the SQL generation model specifically includes: For each SQL statement training data in the SQL statement training data set, select the reasoning path description training data corresponding to the SQL statement training data in the reasoning path description training data set to obtain an SQL statement training data pair, wherein the SQL statement training data pair includes the SQL statement training data and the reasoning path description training data corresponding to the SQL statement training data; Inputting the SQL statement training data pair into a preset generation model, and determining a generated SQL statement and a generated reasoning path description through the preset generation model; The preset generation model is trained based on the generated SQL statement and the generated reasoning path description to obtain a SQL generation model.
[0017] The method for constructing the Text-to-SQL model, wherein determining the Text-to-SQL model based on the SQL generation model specifically includes: Constructing a first type of input data based on the original question in the original SQL data pair; Inputting the first type of input data into the SQL generation model, and generating a plurality of generated SQL statements through the SQL generation model; Constructing verification input data based on the several generated SQL statements, and training a preset optimization model based on the verification input data to obtain an optimization model; A Text-to-SQL model is determined based on the SQL generation model and the optimization model.
[0018] The method for constructing the Text-to-SQL model, wherein the step of constructing verification input data based on the plurality of generated SQL statements specifically includes: Obtaining a set of SQL clauses of each of a number of generated SQL statements; Execute each of the several generated SQL statements to obtain execution error information; Dividing the SQL clauses in all the acquired SQL clause sets into a first SQL clause set and a second SQL clause set according to a second preset condition; constructing a basic SQL statement based on the first set of SQL clauses, and determining additional database information and approximate value information based on the second set of SQL clauses; The additional database information, execution error information, basic SQL statements, a second set of SQL clauses, approximate value information, and the original question are merged to obtain verification input data.
[0019] A second aspect of the present application provides a method for generating an SQL statement, using a Text-to-SQL model constructed by the method for constructing a Text-to-SQL model as described above, wherein the method for generating an SQL statement specifically comprises: Constructing a first type of input data based on the problem to be processed; The first type of input data is input into a Text-to-SQL model, and a corresponding target SQL statement is generated for the problem to be processed through the Text-to-SQL model.
[0020] The method for generating an SQL statement, wherein the inputting of the first type of input data into a Text-to-SQL model and the generation of a corresponding target SQL statement for the problem to be processed by the Text-to-SQL model specifically comprises: The first type of input data is input into a SQL generation model of a Text-to-SQL model, and a corresponding target SQL statement is generated for the problem to be processed through the SQL generation model.
[0021] The method for generating an SQL statement, wherein the step of inputting the first type of input data into a Text-to-SQL model and generating a corresponding target SQL statement for the problem to be processed through the Text-to-SQL model further comprises: Inputting the first type of input data into an SQL generation model of a Text-to-SQL model, and generating a plurality of target SQL statements for the problem to be processed through the SQL generation model; The target SQL statements are optimized by using the optimization model in the Text-to-SQL model to obtain the target SQL statements corresponding to the problem to be processed.
[0022] The method for generating SQL statements, wherein the step of optimizing the target SQL statements by using the optimization model in the Text-to-SQL model to obtain the target SQL statements corresponding to the problem to be processed specifically includes: Obtain a target SQL clause set and target execution error information of each target SQL statement in a number of target SQL statements; Allocating the SQL clauses in all the acquired target SQL clause sets to the first target SQL clause set and the second target SQL clause set according to the second preset condition; A final SQL statement is constructed for the problem to be processed according to the target execution error information, the first target SQL clause set and the second target SQL clause set.
[0023] The method for generating an SQL statement, wherein the step of constructing a final SQL statement for the problem to be processed according to the target execution error information, the first target SQL clause set and the second target SQL clause set is as follows: When the second target SQL clause set is empty, constructing a final SQL statement for the problem to be processed based on the first target SQL clause set; When the second target SQL clause set is not empty, a final SQL statement is constructed for the problem to be processed through an optimization model in the Text-to-SQL model based on the target execution error information, the first target SQL clause set and the second target SQL clause set.
[0024] The method for generating an SQL statement, wherein the step of constructing a final SQL statement for the problem to be processed based on the target execution error information, the first target SQL clause set and the second target SQL clause set through the optimization model in the Text-to-SQL model specifically includes: constructing a target basic SQL statement based on the first target SQL clause set, and determining target additional database information and target approximate value information based on the second target SQL clause set; Merging target additional database information, target execution error information, target basic SQL statements, a second target SQL clause set, target approximate value information, and pending issues to obtain target verification input data; The target verification input data is input into the optimization model in the Text-to-SQL model, and a final SQL statement is generated for the problem to be processed through the optimization model.
[0025] A third aspect of the present application provides a device for constructing a Text-to-SQL model, wherein the device for constructing a Text-to-SQL model specifically includes: An acquisition module, configured to acquire an original training data set, wherein the original training data set includes an original SQL data pair, and the original SQL data pair includes an original question and an original SQL statement; An enhancement module, configured to perform data enhancement on an original training data set to obtain an enhanced training data set, wherein the enhanced training data set includes an original SQL data pair, an enhanced reasoning data pair, and an enhanced SQL data pair, wherein the enhanced reasoning data pair includes a reasoning path description of an original question and an original SQL statement, and the enhanced SQL data pair includes an enhanced question and an enhanced SQL statement; A training module is used to train a preset generation model based on the enhanced training data set to obtain an SQL generation model, and determine a Text-to-SQL model based on the SQL generation model.
[0026] A fourth aspect of the present application provides a device for generating SQL statements, wherein a Text-to-SQL model constructed by the method for constructing a Text-to-SQL model as described above is used, and the device for generating SQL statements specifically includes: A construction module, used for constructing a first type of input data based on the problem to be processed; A generation module is used to input the first type of input data into a Text-to-SQL model, and generate a corresponding target SQL statement for the problem to be processed through the Text-to-SQL model.
[0027] In a fifth aspect, the present application provides a computer-readable storage medium, which stores one or more programs, and the one or more programs can be executed by one or more processors to implement the steps in the method for constructing the Text-to-SQL model as described above, and / or to implement the steps in the method for generating SQL statements as described above.
[0028] A sixth aspect of the present application provides a terminal device, comprising: a processor and a memory; The memory stores a computer-readable program executable by the processor; When the processor executes the computer-readable program, the processor implements the steps in the method for constructing the Text-to-SQL model as described above, and / or implements the steps in the method for generating the SQL statement as described above.
[0029] Beneficial effects: 1. This application expands the original SQL data in the training data set into enhanced SQL data pairs, and forms enhanced reasoning data pairs based on the reasoning process of the original SQL statements, without the need for manual writing, thus reducing labor costs and time costs.
[0030] 2. This application adds enhanced reasoning data pairs to the training data to narrow the distance between the problem and the SQL statement. This can not only improve the reasoning ability of the Text-to-SQL model, but also improve the quality of the SQL statements generated by the Text-to-SQL model.
[0031] 3. After training the generative model, this application will train the optimization model based on the SQL statements generated by the generative model, and then verify and optimize the SQL statements generated by the generative model through the optimization model, so as to further improve the quality of the SQL statements generated by the Text-to-SQL model. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0033] Figure 1 A flowchart of a method for constructing a Text-to-SQL model provided in an embodiment of the present application.
[0034] Figure 2 A principle flow chart of a specific example of a method for constructing a Text-to-SQL model provided in an embodiment of the present application.
[0035] Figure 3 A diagram showing an example of the inputs and outputs for an optimization model.
[0036] Figure 4A flowchart of a method for generating a SQL statement provided in an embodiment of the present application.
[0037] Figure 5 A functional block diagram of a device for constructing a Text-to-SQL model provided in an embodiment of the present application.
[0038] Figure 6 A functional block diagram of a device for generating SQL statements provided in an embodiment of the present application.
[0039] Figure 7 A functional block diagram of a terminal device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0040] The embodiments of the present application provide a method for constructing a Text-to-SQL model, a method for generating a Text-to-SQL model, an apparatus, a device, and a medium. To make the purpose, technical solution, and effect of the present application clearer and more specific, the present application is further described in detail with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0041] It will be understood by those skilled in the art that, unless expressly stated, the singular forms "one", "said", and "the" used herein may also include plural forms. It should be further understood that the term "comprising" used in the specification of the present application refers to the presence of the features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof. It should be understood that when we refer to an element as being "connected" or "coupled" to another element, it may be directly connected or coupled to the other element, or there may be intermediate elements. In addition, the "connection" or "coupling" used herein may include wireless connection or wireless coupling. The term "and / or" used herein includes all or any unit and all combinations of one or more associated listed items.
[0042] It will be understood by those skilled in the art that, unless otherwise defined, all terms (including technical and scientific terms) used herein have the same meaning as those generally understood by those skilled in the art to which this application belongs. It should also be understood that terms such as those defined in general dictionaries should be understood to have meanings consistent with the meanings in the context of the prior art, and will not be interpreted with idealized or overly formal meanings unless specifically defined as here.
[0043] It should be understood that the sequence numbers and sizes of the steps in this embodiment do not mean the order of execution. The execution order of each process is determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiment of the present application.
[0044] The application content is further explained below through the description of embodiments in conjunction with the accompanying drawings.
[0045] This embodiment provides a method for constructing a Text-to-SQL model. Figure 1 , Figure 2 and Figure 3 As shown, the method includes: S10. Obtain the original training data set.
[0046] Specifically, the original training data set includes several original SQL data pairs, each of which includes an original question and an original SQL statement. The original question and the original SQL statement correspond to each other one by one. Here, the original SQL data pair is recorded as ( , ), Indicates the original problem, Represents the original SQL statement. The original question is a question description in natural language. The language of the original question is not limited. For example, the original question is "What are the URL to the list page on Mubi of the lists with followers between 1-2 and whose last update timestamp was on 2012?". The original SQL statement is the SQL statement corresponding to the original question. Through the original SQL statement, the answer data corresponding to the original question can be queried in the SQL database. For example, the original SQL statement is "SELECT list_url FROM LISTS WHERE list_update_timestamp_utc LIKE '2012%' AND list_followers BETWEEN 1 AND 2 ORDER BY list_update_timestamp_utcDESC LIMIT 1".
[0047] S20. Perform data enhancement on the original training data set to obtain an enhanced training data set.
[0048] Specifically, the enhanced training data set includes original SQL data pairs, enhanced reasoning data pairs and enhanced SQL data pairs, wherein the enhanced reasoning data pairs include the original questions and the reasoning path descriptions of the original SQL statements, and the original questions correspond to the reasoning path descriptions one by one, and the enhanced SQL data pairs include enhanced questions and enhanced SQL statements, and the enhanced questions correspond to the enhanced SQL statements one by one. Here, the enhanced SQL data pairs are represented by ( , ), the enhanced reasoning data pair is represented as ( , ),in, Indicates enhancement problem, Indicates enhanced SQL statements, Indicates the original problem, Represents the reasoning path description.
[0049] Exemplarily, the performing data enhancement on the original training data set to obtain the enhanced training data set specifically includes: S21, obtaining an original SQL statement of each original SQL data pair in the original training data set, and obtaining a sub-SQL statement set and an SQL clause set according to the original SQL statement; S22, constructing an enhanced SQL data pair corresponding to the original SQL data pair based on the sub-SQL statement set; S23. Construct an enhanced reasoning data pair corresponding to the original SQL data pair based on the sub-SQL statement set and the SQL clause set.
[0050] Specifically, in step S21, a sub-SQL statement set (referred to as a subSQL set) includes several sub-SQL statements, each of which is an executable SQL clause, that is, each sub-SQL statement in the subSQL set is an executable statement and is composed of some SQL clauses in the original SQL statement. The SQL clause set includes all sub-SQL clauses in the original SQL statement, wherein the SQL clause is a conditional clause of the SQL statement, for example, a SELEC clause, a FROM clause, and a WHERE clause.
[0051] In one implementation, the obtaining of the original SQL statement of each original SQL data pair in the original training data set and obtaining the sub-SQL statement set and the SQL clause set according to the original SQL statement specifically includes: Parsing the original SQL statement in each of the original SQL data pairs to obtain a SQL clause set corresponding to the original SQL statement; Merge the SQL clauses in the acquired SQL clause set to obtain a candidate sub-SQL statement set; All executable sub-SQL statements in the candidate sub-SQL statement set are obtained to obtain a sub-SQL statement set.
[0052] Specifically, the original SQL statement is parsed to obtain all SQL clauses included in the original SQL statement. For example, an SQL parsing tool such as SQLparser, Apache Calcite open source tool, READER, etc. is used to parse the original SQL statement to obtain all SQL clauses (such as SELEC clause, FROM clause, WHERE clause, etc.) included in the original SQL statement, and then the set consisting of all the parsed SQL clauses is used as the SQL clause set. In addition, after the SQL clause set is obtained, the SQL clauses in the SQL clause set are merged, that is, the SQL clauses are combined to obtain a candidate sub-SQL statement set, wherein the candidate sub-SQL statement set includes all sub-SQL statements that can be combined with the SQL clauses in the SQL clause set.
[0053] After obtaining the candidate sub-SQL statement set, the executability of each candidate sub-SQL statement is determined respectively. If executable, the candidate sub-SQL statement is used as a sub-SQL statement of the original SQL statement; finally, the set consisting of all sub-SQL statements is used as the sub-SQL statement set.
[0054] Of course, in practical applications, other methods can also be used to obtain the sub-SQL statement set of the original SQL statement, for example, first split the original SQL statement into a SQL clause set, then determine the necessary SQL clauses of the executable SQL statement, and form an initial sub-SQL statement based on the necessary SQL clauses, and finally, based on the initial sub-SQL statement, add the other SQL clauses in the SQL clause set except the necessary SQL clauses to the initial sub-SQL statement to form candidate sub-SQL statements, and perform executable verification on the candidate sub-SQL statements to obtain the sub-SQL statement set of the original SQL statement. Alternatively, the candidate sub-SQL statements can be formed by deleting SQL clauses from the original SQL statement.
[0055] For example, assuming that the original SQL statement is "SELECT list_url FROM LISTS WHERE list_update_timestamp_utc LIKE '2012%' AND list_followers BETWEEN 1 AND 2 ORDER BY list_update_timestamp_utc DESC LIMIT 1", the SQL clause set of the original SQL statement may include: SQL clause 1: SELECT list_url belongs to the SLELCT part SQL clause 2: FROM LISTS belongs to the FROM part SQL clause 3: list_update_timestamp_utc LIKE '2012%' belongs to the WHERE part SQL clause 4: list_followers BETWEEN 1 AND 2 belongs to the WHERE part SQL clause 5: ORDER BY list_update_timestamp_utc DESC belongs to the ORDER BY part SQL clause 6: LIMIT 1 belongs to the SELECT part; The sub - SQL set of the original SQL statement can include: 1: SELECT * FROM LISTS; 2: SELECT * FROM LISTS ORDER BY list_update_timestamp_utc DESC; 3: SELECT * FROM LISTS WHERE list_followers BETWEEN 1 AND 2 ORDER BY list_update_timestamp_utc DESC; 4: SELECT * FROM LISTS WHERE list_update_timestamp_utc LIKE '2012%' AND list_followers BETWEEN 1 AND 2 ORDER BY list_update_timestamp_utc DESC; 5: SELECT * FROM LISTS WHERE list_update_timestamp_utc LIKE '2012%' AND list_followers BETWEEN 1 AND 2 ORDER BY list_update_timestamp_utc DESC LIMIT 1; 6: SELECT list_url FROM LISTS WHERE list_update_timestamp_utc LIKE '2012%' AND list_followers BETWEEN 1 AND 2 ORDER BY list_update_timestamp_utc DESC LIMIT 1.
[0056] Further, in step S12, the enhanced SQL data pair is generated based on the original SQL data pair, and the enhanced SQL data pair Aligned with original SQL data Similar to and better than the original SQL data Contains more information about fuzzy conditions and allows the Text-to-SQL model to learn by enhancing SQL data pairs The correspondence between the fuzzy conditions in the query and the SQL clauses in the original SQL statement can improve the ability of the trained Text-to-SQL model to understand the original question.
[0057] Exemplarily, constructing the enhanced SQL data pair corresponding to the original SQL data pair based on the sub-SQL statement set specifically includes: Selecting a first target sub-SQL statement from the sub-SQL statement set as an enhanced SQL statement, and obtaining an enhanced SQL statement set according to the enhanced SQL statement; Generating a first prompt word for each enhanced SQL statement in the enhanced SQL statement set; The first prompt word is input into a preset large language model, and the preset large language model is used to output an enhanced question corresponding to the enhanced SQL statement, wherein the enhanced SQL statement corresponds to the enhanced question one by one.
[0058] Specifically, the first target sub-SQL statement is included in the sub-SQL statement set, and is used as an enhanced SQL statement in the enhanced SQL data pair. The first target sub-SQL statement may be any sub-SQL statement in the sub-SQL statement set, or may be a plurality of sub-SQL statements in the sub-SQL statement set that meet preset requirements, for example, the first target sub-SQL statement includes a first number of SQL clauses, the original SQL statement includes a second number of SQL clauses, the difference between the second number and the first number is less than or equal to a preset number threshold, or the similarity between the first target sub-SQL statement and the original SQL statement meets a preset similarity requirement, etc.
[0059] In an embodiment of the present application, a selection condition is set such that the difference between the second quantity and the first quantity is less than or equal to a preset quantity threshold, and then a sub-SQL statement is selected from the sub-SQL statement set based on the selection condition, and each selected sub-SQL statement is used as a first target sub-SQL statement. That is, the enhanced SQL statement set may include one enhanced SQL statement, or may include multiple enhanced SQL statements. Specifically, when a first target sub-SQL statement is selected from the sub-SQL statement set, the enhanced SQL statement set includes one enhanced SQL statement, and when multiple first target sub-SQL statements are selected from the sub-SQL statement set, the enhanced SQL statement set includes multiple enhanced SQL statements, and when the enhanced SQL statement set includes multiple enhanced SQL statements, an enhanced question is generated for each enhanced SQL statement to form multiple enhanced SQL data pairs.
[0060] The embodiment of the present application can generate an enhanced SQL data pair that is similar to the original SQL data pair by selecting a sub-SQL statement that meets the selection conditions as an enhanced SQL statement, so that the preset generation model can identify the enhanced SQL data pair, thereby improving the preset generation model's ability to understand the enhanced SQL data pair.
[0061] Further, after obtaining the enhanced SQL statement, an enhanced question will be generated for the enhanced SQL statement, wherein the enhanced question can be generated by a preset large language model (such as LLM). That is to say, the enhanced SQL statement can be input into the preset large language model, and the enhanced question can be output through the preset large language model. Based on this, a first prompt word will be generated for the enhanced SQL statement, wherein the first prompt word includes a question generation instruction, several examples of generating questions, and the enhanced SQL statement, the question generation instruction is used to instruct the preset large language model to generate an enhanced question for the enhanced SQL statement, and the several examples of generating questions are used as prior knowledge for the preset large language model to learn, and the enhanced SQL statement is used to inform the preset large language model that the SQL statement for the enhanced question needs to be generated. Finally, after the first prompt word is generated, the first prompt word is input into the preset large language model, and the enhanced question corresponding to the enhanced SQL statement is output through the preset large language model. Wherein, the enhanced SQL statement corresponds to the enhanced question one by one.
[0062] For example: the first prompt word can be: "Question Generation Instructions You are an expert at translating SQL queries into natural language questions. Your task is to generate a clear, concise, detailed question that accurately captures the intent of the SQL query.
[0063] Generate question examples SQL query: SELECT avg(rating score) FROM movie's internal joint rating, movie_id = rated movie ID WHERE movie title is "XXX" Generating questions: What is the average rating for XXX? SQL query: SELECT product name FROM product set, productid = sales.productid WHERE salesperson number is 20 and sales volume is the highest Generating question: What is the name of the product that salesperson number 20 sells the most? SQL query: {original SQL statement} Generate question: {Original question} Now, create a question that is as detailed and concise as the following SQL query, and whose semantics are consistent with the following SQL query. Make sure that the generated question follows the structure of the generated question example, taking into account the differences in the SQL query.
[0064] SQL query: {Enhanced SQL statement} Generate question: {}.
[0065] Of course, in practical applications, other methods may also be used to construct the first prompt word. Usually, the first prompt word includes a question generation instruction and an enhanced SQL statement.
[0066] In the embodiment of the present application, a sub-SQL statement of the original SQL statement is selected as an enhanced SQL statement, and then an enhanced question is generated for the enhanced SQL statement by presetting a large language model (such as LLM) to form a number of enhanced SQL data pairs, and the formed number of enhanced SQL data pairs are added to the original training data set. The enhanced SQL data pairs have certain similarities with the original SQL statements and also have certain differences. This not only increases the amount of data included in the original training data set, but also improves the understanding ability of the preset generation model, and solves the problem that a high-precision Text-to-SQL model cannot be built in a scenario with less training data.
[0067] The following is a specific example to illustrate the difference between the enhanced SQL data pair and the original SQL data pair. As shown in Table 1, the enhanced SQL statement in enhanced SQL data pair 1 lacks the "LIMIT 1" clause compared to the original SQL statement in the original data pair, and the enhanced SQL statement in enhanced SQL data pair 2 lacks the "list_update_timestamp_utc LIKE '2012%'" clause compared to the original SQL statement in the original SQL data pair. The enhanced question generated for this also has conditional differences from the original question.
[0068] Table 1 Original SQL data pairs and enhanced SQL data pairs
[0069] The enhanced reasoning data pair is used to reflect the reasoning information generated by the original SQL statement, which includes the reasoning path description of the original question and the original SQL statement. The reasoning path description is used as the reasoning information generated by the original SQL statement, so that the preset generation model can better understand the original SQL data pair, thereby improving the understanding ability of the preset generation model and assisting the preset generation model to generate high-quality SQL statements. This application adds enhanced reasoning data pairs to the original training data set, and uses the reasoning path description in the enhanced reasoning data pairs as intermediate transmission information, which can not only reduce error conduction but also enable the preset generation model to better understand the problem, thereby improving the reasoning ability of the trained Text-to-SQL model.
[0070] Exemplarily, constructing the enhanced reasoning data pair corresponding to the original SQL data pair based on the sub-SQL statement set and the SQL clause set specifically includes: S121, selecting a second target sub-SQL statement from the sub-SQL statement set to obtain a second target sub-SQL statement set; S122: Determine a reasoning path of the original SQL statement based on the second target sub-SQL statement set and the SQL clause set, wherein the reasoning path corresponds to the original question one by one; S123. Based on the reasoning path and the original question, determine the reasoning path description of the original SQL statement through a preset large language model, and obtain an enhanced reasoning data pair of the original SQL data pair according to the original question and the reasoning path description.
[0071] In step S121, the second target sub-SQL statement is included in the sub-SQL statement set, and the second target sub-SQL statement is the sub-SQL statement containing the least SQL clauses. That is, after the sub-SQL statement set is obtained, the sub-SQL statement containing the least SQL clauses is selected from the sub-SQL statement set, and the selected sub-SQL statement is used as the second target sub-SQL statement. After the second target sub-SQL statement is obtained, the second target sub-SQL statement is added to the preset data set to obtain the second target sub-SQL statement set, wherein when the second target sub-SQL statement is not added, the preset data set is empty.
[0072] In step S122, after acquiring the second target sub-SQL statement, an expansion process from the second target sub-SQL statement to the original SQL statement is determined according to the second target sub-SQL statement and the SQL clause set, and the expansion process is used as the reasoning path of the original SQL statement.
[0073] Exemplarily, determining the reasoning path of the original SQL statement based on the second target sub-SQL statement set and the SQL clause set specifically includes: Acquire a target SQL clause in the SQL clause set that is not included in the second target sub-SQL statement set; According to the first preset condition, gradually adding the acquired target SQL clauses to the second target sub-SQL statement set, wherein only one target SQL clause is added in each step; Based on the first preset condition and the second target sub-SQL statement set, an inference path of the original SQL statement is obtained.
[0074] Specifically, the target SQL clause is a SQL clause included in the SQL clause set but not included in the second target sub-SQL statement, wherein the second target sub-SQL statement is the sub-SQL statement including the fewest SQL clauses, and all the acquired target SQL clauses are gradually added to the second target sub-SQL statement, and the second target sub-SQL statement will be restored to the original SQL statement.
[0075] The reasoning path includes all sub-SQL statements formed in the recovery process of the second target sub-SQL statement-original SQL statement, and each time a target SQL clause is added to form a new sub-SQL statement, the new sub-SQL statement is added to the second target sub-SQL statement set, and then the reasoning path of the original SQL statement is determined based on the second target sub-SQL statement set and the first preset condition, wherein the first preset condition may include the termination condition of the adding process and the adding condition of each adding target SQL clause, the termination condition may be that the second target sub-SQL statement set includes the original SQL statement, and the adding condition may be that each time a target SQL clause that has not been added is added in the order of the position of the target SQL clause in the original SQL statement. Of course, in actual applications, the adding condition may be that each time a target SQL clause that has not been added is added in accordance with the logical relationship between the SQL clauses in the original SQL statement.
[0076] For example: Assume that the sub-SQL statement set and the SQL clause set are the sub-SQL statement set and the SQL clause set in the above example. During the recovery process, one target sub-statement is added each time. The second target sub-SQL statement is: SELECT * FROM LISTS. The reasoning path from the second target sub-SQL statement to the original SQL statement is [1,2,3,4,5,6], where 1 represents the second target sub-SQL statement, 6 represents the original SQL statement, and 2,3,4 and 5 all represent sub-SQL statements in the recovery process from the second target sub-SQL statement to the original SQL statement. Specifically: 2: SELECT * FROM LISTS ORDER BY list_update_timestamp_utc DESC; 3: SELECT * FROM LISTS WHERE list_followers BETWEEN 1 AND 2 ORDER BYlist_update_timestamp_utc DESC; 4: SELECT * FROM LISTS WHERE list_update_timestamp_utc LIKE '2012%'AND list_followers BETWEEN 1 AND 2 ORDER BY list_update_timestamp_utc DESC; 5: SELECT * FROM LISTS WHERE list_update_timestamp_utc LIKE '2012%'AND list_followers BETWEEN 1 AND 2 ORDER BY list_update_timestamp_utc DESCLIMIT 1.
[0077] In step S123, after the reasoning path is obtained, a reasoning path description is generated for the original SQL statement based on the reasoning path by using a preset large language model, and then the reasoning path description and the original question are used as an enhanced reasoning data pair for the original SQL statement. Exemplarily, the reasoning path description of the original SQL statement is determined by using a preset large language model based on the reasoning path and the original question, specifically including: Constructing a second prompt word based on the reasoning path and the original question; The second prompt word is input into a preset large language model, and the reasoning path description of the original SQL statement is output through the preset large language model.
[0078] Specifically, the second prompt word includes an instruction to generate a reasoning path description, an example of generating a reasoning path description, the reasoning path and the original question; the instruction to generate a reasoning path description is used to instruct the preset large language model to generate a reasoning path description for the original SQL statement, the example of generating a reasoning path description is used as prior knowledge for the preset large language model to learn, and the original question and reasoning path are used to inform the preset large language model of the conditional information of the reasoning path description that needs to be generated.
[0079] After the second prompt word is constructed, the second prompt word is input into the preset large language model, and the preset large language model generates a reasoning path description of the original SQL statement according to the second prompt word. For example, the reasoning path description of the original SQL statement can be "Select all lists, then sort by update timestamp in descending order; filterfor lists with followers between 1-2 and an update timestamp in 2012; Keepthe order and limit it to the first item; retrieve the URL for the specifiedlist". In addition, after the reasoning path description of the original SQL statement is generated, the reasoning path description of the original SQL statement and the original question are combined to obtain an enhanced reasoning data pair, wherein an example of the enhanced reasoning data pair can be shown in Table 2.
[0080] Table 2 Examples of enhanced reasoning data pairs
[0081] S30. Training a preset generation model based on the enhanced training data set to obtain an SQL generation model, and determining a Text-to-SQL model based on the SQL generation model.
[0082] Specifically, the preset generation model is pre-set and used as the SQL generation model to be trained. The SQL generation model (referred to as the Generator model) is formed by training the preset generation model. The model structures of the preset generation model and the SQL generation model differ in that the model parameters are different. The preset generation model uses the original network parameters, and the SQL generation model uses the network parameters trained with the enhanced training data set.
[0083] When training the preset generation model, the present application uses enhanced SQL data pairs containing fuzzy conditions and SQL clause information of the problem and information containing SQL reasoning process as training data, and performs multi-task training of SQL statement generation tasks and reasoning path description generation tasks on the preset generation model, which can improve the generation ability and reasoning ability of the trained SQL generation model, and thus enable the SQL generation model to generate high-quality SQL statements.
[0084] Exemplarily, the training of a preset generation model based on the enhanced training data set to obtain a SQL generation model specifically includes: S31, constructing an SQL statement training data set based on the SQL data pairs in the enhanced training data set, wherein the SQL data pairs include original SQL data pairs and enhanced SQL data pairs; S32, constructing a reasoning path description training data set according to the enhanced reasoning data pairs in the enhanced training data set; S33. Training a preset generation model based on the SQL statement training data set and the reasoning path description training data set to obtain an SQL generation model.
[0085] In step S31, the SQL statement training data set is used to perform the SQL statement generation task, which includes each original SQL data pair in the enhanced training data set and the SQL statement training data corresponding to each enhanced SQL data pair, wherein the SQL statement training data can be expressed as ( ,SQL), It represents the first type of input data used to reflect the problem, and SQL represents the SQL statement in the SQL data pair.
[0086] Exemplarily, the constructing the SQL statement training data set based on the SQL data pairs in the enhanced training data set specifically includes: For each SQL data pair, construct an SQL prefix for the SQL data pair, and obtain database information corresponding to the question in the SQL data pair; generating a first type of input data based on the SQL prefix, the database information, and a question in the SQL data pair; SQL statement training data is formed based on the first type of input data and the SQL statements in the SQL data pair to obtain a SQL statement training data set.
[0087] Specifically, the SQL prefix is used to inform the preset generation model of the task type performed by the SQL statement training data, and the database information is used to reflect the database information corresponding to the problem. The problem is the problem included in the SQL data pair. When the SQL data pair is the original SQL data pair, the problem is the original problem. When the SQL data pair is the enhanced SQL data pair, the problem is the enhanced problem, wherein the enhanced SQL data pair is determined based on the original SQL data pair, so that the database information of the enhanced problem in the enhanced SQL data pair is the database information of the original problem in the original SQL data pair corresponding to it. Among them, the SQL prefix can be set to [SQL], so as to inform the preset generation model through [SQL] that the SQL statement needs to be output for the first type of input data. The database information can be determined by obtaining the database table structure information of the database corresponding to the problem using the implementation method of CodeS, which can be expressed as: table {table_name},columns=[{table_name}.{column_name}({column_type}|values:{value1},{value2}),…]…Organization; Among them, table, columns, and values are keywords corresponding to database prompts, and {} corresponds to specific values, such as table_name represents the table name, column_name represents the column name, column_type represents the column type, and value1 and value2 represent the specific values of the corresponding columns in the database.
[0088] It should be noted that since the enhanced SQL data pairs are generated based on the original SQL data pairs, the enhanced questions in the enhanced SQL data pairs and the original questions in the corresponding original SQL data pairs correspond to the same database. Therefore, when obtaining the database information, only the database information corresponding to the original question in each original SQL data pair in the enhanced training data set can be obtained. For each enhanced SQL data pair, the database information corresponding to the original question in the corresponding original SQL data pair is used as the database information of the enhanced SQL data pair.
[0089] Further, after obtaining the SQL prefix and the database information, the SQL prefix, the database information and the question in the SQL data are concatenated to obtain the first type of input data, wherein the first type of input data can be expressed as: , in, Represents the first type of input data, Indicates the SQL prefix, Represents database information. Indicates a problem with SQL data pairing.
[0090] In step S32, the reasoning path description training data set is used to perform the reasoning path description generation task, which includes the reasoning path description training data formed by each enhanced reasoning data pair in the enhanced training data set. The reasoning path description training data can be expressed as ( , reason), It represents the second type of input data used to reflect the problem, and reason represents the description of the reasoning path in the enhanced reasoning data pair.
[0091] Exemplarily, the constructing the reasoning path description training data set based on the enhanced reasoning data in the enhanced training data set specifically includes: For each enhanced reasoning data pair, construct a reasoning prefix for the enhanced reasoning data pair, and obtain database information corresponding to the original question in the enhanced reasoning data pair; A second type of input data is generated based on the reasoning prefix, the database information and the original question, and reasoning path description training data is formed based on the second type of input data and the reasoning path description in the enhanced reasoning data pair to obtain a reasoning path description training data set.
[0092] Specifically, the reasoning prefix is used to inform the preset generated reasoning path to describe the task type of the training data, and the database information is used to reflect the database information corresponding to the original question in the enhanced reasoning data pair, and the original question is the original question included in the enhanced reasoning data pair. Among them, the SQL prefix can be set to , through Tell the preset generation model that the reasoning path description needs to be output for the reasoning path description training data. The database information is the database information of the original problem, which is the same as the database information extracted in the construction process of the above SQL statement training data set.
[0093] Further, after obtaining the reasoning prefix and the database information, the reasoning prefix, the database information and the original question in the enhanced reasoning data pair are concatenated to obtain the second type of input data, wherein the second type of input data can be expressed as: , in, Represents the second type of input data, represents an inference prefix, Represents database information. Represents the original problem in the augmented inference data pair.
[0094] In order to more clearly illustrate the SQL statement training data and the reasoning path description training data, the SQL statement training data and the reasoning path description training data shown in Table 3 are given.
[0095] Table 3 Two types of input examples
[0096] In step S33, after obtaining the SQL statement training data set and the reasoning path description training data set, select a SQL statement training data in the SQL statement training data set, and then select a reasoning path description training data in the reasoning path description training data set, input the selected SQL statement training data and the reasoning path description training data into the preset generation model together, generate SQL statements and generate reasoning path descriptions through the preset generation model output, and then train the preset generation model based on the generated SQL statements and the generated reasoning path descriptions. Based on this, the training of the preset generation model based on the SQL statement training data set and the reasoning path description training data set to obtain the SQL generation model specifically includes: For each SQL statement training data in the SQL statement training data set, select the reasoning path description training data corresponding to the SQL statement training data in the reasoning path description training data set to obtain an SQL statement training data pair, wherein the SQL statement training data pair includes the SQL statement training data and the reasoning path description training data corresponding to the SQL statement training data; Inputting the SQL statement training data pair into a preset generation model, and determining a generated SQL statement and a generated reasoning path description through the preset generation model; The preset generation model is trained based on the generated SQL statement and the generated reasoning path description to obtain a SQL generation model.
[0097] Specifically, the reasoning path description training data corresponding to the SQL statement training data refers to the original SQL data pair corresponding to the enhanced reasoning data pair corresponding to the reasoning path description training data, which is the same as the original SQL data pair corresponding to the SQL data pair corresponding to the SQL statement training data. In other words, the reasoning path description in the reasoning path description training data is the reasoning path description of the original SQL statement corresponding to the SQL statement in the SQL statement training data pair.
[0098] Furthermore, the preset generation model can use a pre-trained large model, and fine-tune the preset generation model through a multi-task fine-tuning framework to obtain a SQL generation model. For example, the preset generation model can be a code LLM model, and the code LLM model is fine-tuned through the decoder framework using the LoRA fine-tuning technology to obtain a SQL generation model (Generator model). For example, the original network parameters of the pre-trained large model are , two additional low-rank parameter matrices are trained when determining the SQL generation model and , where d, r, k represent the corresponding matrix dimensions (usually r<<{d, k}), R represents the real space, and two additional low-rank parameter matrices are trained and When , the initial network parameters are frozen as .
[0099] When additionally training two low-rank parameter matrices, function loss terms can be constructed based on generating SQL statements and generating reasoning path descriptions, respectively, and then a total loss term is constructed based on minimizing the two function loss terms, and the low-rank parameter matrices A and B are trained based on the total loss term, where the total loss term can be expressed as: , in, Represents the conditional probability of the preset generation model generating the next token, Indicates the generation of SQL statements. It represents the description of the generated reasoning path. It is worth noting that the purpose of adding enhanced reasoning data pairs during the training process is to enable the Generator to understand the reasoning process of SQL and assist in generating SQL.
[0100] To this end, the SQL generation model obtained by training the preset generation model can be expressed as: , in, represents the original network parameters, A and B represent two low-rank parameter matrices trained based on the SQL statement training dataset and the inference path description training dataset, respectively.
[0101] The above completes the description of the construction process of the SQL generation model. In the process of constructing the SQL generation model, the SQL statement training data set and the reasoning path description training data set can also be generated in other ways. For example, directly using the SQL prefix and the question in the SQL data pair as the first type of input data, and using the reasoning prefix and the original question as the reasoning input data, etc.
[0102] In the embodiment of the present application, in order to further improve the model performance of the Text-to-SQL model, after the SQL generation model is constructed, an optimization model (denoted as the Corrector model) is also constructed, and the SQL statements generated by the SQL generation model are verified and optimized by the optimization model, that is, the Text-to-SQL model includes the SQL generation model and the optimization model. It should be noted that in actual applications, the SQL generation model can also be directly used as the Text-to-SQL model, that is, the Text-to-SQL model includes the SQL generation model.
[0103] Based on this, determining the Text-to-SQL model based on the SQL generation model specifically includes: B10. constructing first type input data based on the original question in the original SQL data pair; B20, inputting the first type of input data into the SQL generation model, and generating a plurality of SQL statements through the SQL generation model; B30. Constructing verification input data based on the several generated SQL statements, and training a preset optimization model based on the verification input data to obtain an optimization model; B40. Determine a Text-to-SQL model based on the SQL generation model and the optimization model.
[0104] In step B10, the process of constructing the first type of input data is the same as the process of constructing the first type of input data described above, and the details may refer to the above description, which will not be repeated here.
[0105] In step B20, the plurality of generated SQL statements are all generated by the SQL generation model based on the first type of input data, and the generated SQL statements in the plurality of generated SQL statements may include different SQL clauses or the same SQL clauses. That is, the generated SQL statements in the plurality of generated SQL statements may be completely the same or some of the generated SQL statements may be different.
[0106] In step B30, if Figure 3 As shown, the verification input data is the input data of the preset optimization model (such as the code LLM model). The verification SQL statement corresponding to the verification input data is generated by the preset optimization model, and then the optimization model is verified based on the verification SQL statement and the original SQL statement in the original SQL data pair. The LoRA method is used to fine-tune the preset optimization model (such as the LLM model) to obtain the optimization model (referred to as the Corrector model).
[0107] Exemplarily, the constructing verification input data based on the several generated SQL statements specifically includes: Obtaining a set of SQL clauses of each of a number of generated SQL statements; Execute each of the several generated SQL statements to obtain execution error information; Dividing the SQL clauses in all the acquired SQL clause sets into a first SQL clause set and a second SQL clause set according to a second preset condition; constructing a basic SQL statement based on the first set of SQL clauses, and determining additional database information and approximate value information based on the second set of SQL clauses; The additional database information, execution error information, basic SQL statements, a second set of SQL clauses, approximate value information, and the original question are merged to obtain verification input data.
[0108] Specifically, the acquisition process of the SQL clause set of the generated SQL statement is the same as the acquisition process of the SQL clause set of the above-mentioned original SQL statement. For details, please refer to the acquisition process of the SQL clause set of the above-mentioned original SQL statement, which will not be repeated here. The execution error information refers to the error information generated when the generated SQL statement is executed, that is, each generated SQL statement can be executed to determine the execution error information of each generated SQL statement, wherein, when no error information is generated during the execution of the generated SQL statement, the execution error information of the generated SQL statement is empty, and when error information is generated during the execution of the generated SQL statement, the execution error information of the generated SQL statement is the generated error information.
[0109] The second preset condition refers to clause commonality, which can be reflected by the number of subsets of the SQL clause set containing the SQL clause. To this end, the set consisting of SQL clauses common to all SQL clause sets can be used as a high-confidence SQL clause set (i.e., a first SQL clause set), and the set consisting of the remaining clauses can be used as a low-confidence SQL clause set (i.e., a second SQL clause set), wherein the confidence of the first clause set SQL is higher than the confidence of the second SQL clause set. In an embodiment of the present application, a clause commonality threshold (such as 2, etc.) is pre-set, and when the clause commonality of the SQL clause is greater than or equal to the clause commonality threshold, the SQL clause is included in the first SQL clause set, and when the clause commonality of the SQL clause is less than the clause commonality threshold, the SQL clause is included in the second SQL clause set. In addition, it should be noted that the number of SQL statements included in the several generated SQL statements is greater than or equal to the clause commonality threshold. Of course, in practical applications, the first SQL clause set and the second SQL clause set may also be divided in other ways, for example, by interaction with a user, or by the number of occurrences of an SQL clause in all SQL clause sets.
[0110] The basic SQL statement is determined based on the first SQL clause set, for example, the basic SQL statement is composed of all SQL clauses in the first SQL clause set. That is, after obtaining the first SQL clause set, all SQL clauses in the first SQL clause set are combined to form the basic SQL statement. Of course, the basic SQL statement can also be constructed in other ways, for example, according to the combination operation performed by the user on the first SQL clause set.
[0111] The additional database information is used to reflect the database structure information involved in the second SQL clause set, for example, the table name, column name, and numerical value involved in the second SQL clause set, and the approximate value information is used to reflect the approximate value information related to the cell value. Both the additional database information and the approximate value information can be obtained through the string approximate retrieval function provided by tools (such as Elasticsearch).
[0112] After obtaining the additional database information, execution error information, basic SQL statements, second SQL clause set and approximate value information, the additional database information, execution error information, basic SQL statements, second SQL clause set, approximate value information and the original question are fused to obtain verification input data, wherein the fusion can be performed in a splicing manner, and the additional database information, execution error information, second SQL clause set and approximate value information are used as other auxiliary information.
[0113] It should be noted that, in practical applications, other methods may be used to verify input data, for example, not including approximate value information, or not including execution error information, or not including execution error information and approximate value information, and so on.
[0114] Further, after obtaining the verification input data, the optimization model is trained based on the verification input data and the original SQL statement, wherein the loss function used in training the optimization model may be: , in, Represents validation of input data, Represents the original SQL statement corresponding to the original SQL data pair. Represents the conditional probability of the optimization model generating the next token.
[0115] To further illustrate the optimization model, a self-verified input and output example is given as shown in Table 4.
[0116] Table 4. Example of input and output for self-verification
[0117] In Table 4, Table schema and Foreign keys in the verification input data are additional database information; Error execute messages are beam search execution error messages; Beam search other conditions are displays of different clauses; Value match is approximate value information; Basic SQL is a basic SQL statement composed of the same clauses; and question is the original question.
[0118] In summary, this embodiment provides a method for constructing a Text-to-SQL model, and the method specifically includes: obtaining an original training data set, wherein the original training data set includes original SQL data pairs, and the original SQL data pairs include original questions and original SQL statements; performing data enhancement on the original training data set to obtain an enhanced training data set, wherein the enhanced training data set includes original SQL data pairs, enhanced reasoning data pairs, and enhanced SQL data pairs, and the enhanced reasoning data pairs include reasoning path descriptions of original questions and original SQL statements, and the enhanced SQL data pairs include enhanced questions and enhanced SQL statements; based on the enhanced training data set, a preset generation model is trained to obtain an SQL generation model, and a Text-to-SQL model is determined based on the SQL generation model. This application expands the original SQL data in the training data set into enhanced SQL data pairs, and forms enhanced reasoning data pairs based on the reasoning process of the original SQL statements, without the need for manual writing, thereby reducing labor costs and time costs. At the same time, by adding enhanced reasoning data pairs to the training data, the distance between the problem and the SQL statement can be shortened through enhanced reasoning data. This can not only improve the reasoning ability of the Text-to-SQL model, but also improve the quality of the SQL statements generated by the Text-to-SQL model.
[0119] Based on the above-mentioned method for constructing a Text-to-SQL model, an embodiment of the present application provides a method for generating an SQL statement, using the Text-to-SQL model constructed by the method for constructing a Text-to-SQL model as described above, such as Figure 4 As shown, the method for generating the SQL statement specifically includes: M10. Construct the first type of input data based on the problem to be processed; M20. Input the first type of input data into a Text-to-SQL model, and generate a corresponding target SQL statement for the problem to be processed through the Text-to-SQL model.
[0120] In one implementation, inputting the first type of input data into a Text-to-SQL model, and generating a corresponding target SQL statement for the problem to be processed through the Text-to-SQL model specifically includes: The first type of input data is input into a SQL generation model of a Text-to-SQL model, and a corresponding target SQL statement is generated for the problem to be processed through the SQL generation model.
[0121] In one implementation, inputting the first type of input data into a Text-to-SQL model, and generating a corresponding target SQL statement for the problem to be processed through the Text-to-SQL model specifically includes: Inputting the first type of input data into an SQL generation model of a Text-to-SQL model, and generating a plurality of target SQL statements for the problem to be processed through the SQL generation model; The target SQL statements are optimized by using the optimization model in the Text-to-SQL model to obtain the target SQL statements corresponding to the problem to be processed.
[0122] In one implementation, the optimizing the target SQL statements by using the optimization model in the Text-to-SQL model to obtain the target SQL statements corresponding to the problem to be processed specifically includes: Obtain a target SQL clause set and target execution error information of each target SQL statement in a number of target SQL statements; Allocating the SQL clauses in all the acquired target SQL clause sets to the first target SQL clause set and the second target SQL clause set according to the second preset condition; A final SQL statement is constructed for the problem to be processed according to the target execution error information, the first target SQL clause set and the second target SQL clause set.
[0123] In one implementation, the constructing of a final SQL statement for the problem to be processed according to the target execution error information, the first target SQL clause set, and the second target SQL clause set specifically includes: When the second target SQL clause set is empty, that is, the number of set elements of the second target SQL clause set is 0, constructing a final SQL statement for the problem to be processed based on the first target SQL clause set; When the second target SQL clause set is not empty, that is, the number of set elements of the second target SQL clause set is not 0, based on the target execution error information, the first target SQL clause set and the second target SQL clause set, a final SQL statement is constructed for the problem to be processed through the optimization model in the Text-to-SQL model.
[0124] In one implementation, constructing a final SQL statement for the problem to be processed by using an optimization model in the Text-to-SQL model based on the target execution error information, the first target SQL clause set, and the second target SQL clause set specifically includes: constructing a target basic SQL statement based on the first target SQL clause set, and determining target additional database information and target approximate value information based on the second target SQL clause set; Merging target additional database information, target execution error information, target basic SQL statements, a second target SQL clause set, target approximate value information, and pending issues to obtain target verification input data; The target verification input data is input into the optimization model in the Text-to-SQL model, and a final SQL statement is generated for the problem to be processed through the optimization model.
[0125] Based on the above-mentioned method for constructing a Text-to-SQL model, this embodiment provides a device for constructing a Text-to-SQL model, such as Figure 5 As shown, the construction device of the Text-to-SQL model specifically includes: An acquisition module 101 is used to acquire an original training data set, wherein the original training data set includes an original SQL data pair, and the original SQL data pair includes an original question and an original SQL statement; The enhancement module 102 is used to perform data enhancement on the original training data set to obtain an enhanced training data set, wherein the enhanced training data set includes original SQL data pairs, enhanced reasoning data pairs and enhanced SQL data pairs, the enhanced reasoning data pairs include reasoning path descriptions of original questions and original SQL statements, and the enhanced SQL data pairs include enhanced questions and enhanced SQL statements; The training module 103 is used to train a preset generation model based on the enhanced training data set to obtain a SQL generation model, and determine a Text-to-SQL model based on the SQL generation model.
[0126] Based on the above SQL statement generation method, this embodiment provides a SQL statement generation device, using the Text-to-SQL model constructed by the above Text-to-SQL model construction method, such as Figure 6 As shown, the SQL statement generating device specifically includes: A construction module 201 is used to construct a first type of input data based on the problem to be processed; The generating module 202 is used to input the first type of input data into a Text-to-SQL model, and generate a corresponding target SQL statement for the problem to be processed through the Text-to-SQL model.
[0127] Based on the above-mentioned method for constructing a Text-to-SQL model, this embodiment provides a computer-readable storage medium, which stores one or more programs. The one or more programs can be executed by one or more processors to implement the steps in the method for constructing a Text-to-SQL model as described in the above-mentioned embodiment.
[0128] Based on the above-mentioned method for constructing the Text-to-SQL model, the present application also provides a terminal device, such as Figure 7 As shown, it includes at least one processor 20; a display screen 21; and a memory 22, and may also include a communications interface 23 and a bus 24. The processor 20, the display screen 21, the memory 22, and the communications interface 23 may communicate with each other through the bus 24. The display screen 21 is configured to display a preset user guide interface in the initial setting mode. The communications interface 23 may transmit information. The processor 20 may call the logic instructions in the memory 22 to execute the method in the above embodiment.
[0129] In addition, the logic instructions in the memory 22 can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product.
[0130] The memory 22 is a computer-readable storage medium that can be configured to store software programs, computer executable programs, such as program instructions or modules corresponding to the methods in the embodiments of the present disclosure. The processor 20 executes functional applications and data processing by running the software programs, instructions or modules stored in the memory 22, that is, implementing the methods in the above embodiments.
[0131] The memory 22 may include a program storage area and a data storage area, wherein the program storage area may store an operating system and at least one application required for a function; the data storage area may store data created according to the use of the terminal device, etc. In addition, the memory 22 may include a high-speed random access memory and may also include a non-volatile memory. For example, a variety of media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk, may also be a transient storage medium.
[0132] In addition, the specific process of loading and executing the multiple instruction processors in the above-mentioned storage medium and the terminal device has been described in detail in the above-mentioned method, and will not be described one by one here.
[0133] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A method for constructing a Text-to-SQL model, characterized in that: The method for constructing the Text-to-SQL model specifically includes: Acquire an original training data set, wherein the original training data set includes an original SQL data pair, and the original SQL data pair includes an original question and an original SQL statement; Performing data enhancement on the original training data set to obtain an enhanced training data set, wherein the enhanced training data set includes original SQL data pairs, enhanced reasoning data pairs, and enhanced SQL data pairs, the enhanced reasoning data pairs include reasoning path descriptions of original questions and original SQL statements, and the enhanced SQL data pairs include enhanced questions and enhanced SQL statements; A preset generation model is trained based on the enhanced training data set to obtain a SQL generation model, and a Text-to-SQL model is determined based on the SQL generation model.
2. The method for constructing a Text-to-SQL model according to claim 1, characterized in that: The data enhancement of the original training data set to obtain the enhanced training data set specifically includes: Obtaining an original SQL statement of each original SQL data pair in the original training data set, and obtaining a sub-SQL statement set and an SQL clause set according to the original SQL statement; Constructing an enhanced SQL data pair corresponding to the original SQL data pair based on the sub-SQL statement set; An enhanced inference data pair corresponding to the original SQL data pair is constructed based on the sub-SQL statement set and the SQL clause set.
3. The method for constructing a Text-to-SQL model according to claim 2, characterized in that: The obtaining of the original SQL statement of each original SQL data pair in the original training data set and obtaining the sub-SQL statement set and the SQL clause set according to the original SQL statement specifically includes: Parsing the original SQL statement in each of the original SQL data pairs to obtain a SQL clause set corresponding to the original SQL statement; Merge the SQL clauses in the acquired SQL clause set to obtain a candidate sub-SQL statement set; All executable sub-SQL statements in the candidate sub-SQL statement set are obtained to obtain a sub-SQL statement set.
4. The method for constructing a Text-to-SQL model according to claim 2, characterized in that: The step of constructing the enhanced SQL data pair corresponding to the original SQL data pair based on the sub-SQL statement set specifically includes: Selecting a first target sub-SQL statement from the sub-SQL statement set as an enhanced SQL statement, and obtaining an enhanced SQL statement set according to the enhanced SQL statement; Generating a first prompt word for each enhanced SQL statement in the enhanced SQL statement set; The first prompt word is input into a preset large language model, and an enhanced question corresponding to the enhanced SQL statement is output through the preset large language model, wherein the enhanced SQL statement corresponds to the enhanced question one by one.
5. The method for constructing a Text-to-SQL model according to claim 2, characterized in that: The step of constructing the enhanced reasoning data pair corresponding to the original SQL data pair based on the sub-SQL statement set and the SQL clause set specifically includes: Selecting a second target sub-SQL statement from the sub-SQL statement set to obtain a second target sub-SQL statement set; Determine a reasoning path of the original SQL statement based on the second target sub-SQL statement set and the SQL clause set, wherein the reasoning path corresponds one-to-one to the original question; Based on the reasoning path and the original question, determining a reasoning path description of the original SQL statement by using a preset large language model; An enhanced reasoning data pair of the original SQL data pair is obtained according to the original question and the reasoning path description.
6. The method for constructing a Text-to-SQL model according to claim 5, characterized in that: Determining the reasoning path of the original SQL statement based on the second target sub-SQL statement set and the SQL clause set specifically includes: Acquire a target SQL clause in the SQL clause set that is not included in the second target sub-SQL statement set; According to the first preset condition, gradually adding the acquired target SQL clauses to the second target sub-SQL statement set, wherein only one target SQL clause is added in each step; Based on the first preset condition and the second target sub-SQL statement set, an inference path of the original SQL statement is obtained.
7. The method for constructing a Text-to-SQL model according to claim 5, characterized in that: The step of determining the reasoning path description of the original SQL statement based on the reasoning path and the original question by using a preset large language model specifically includes: Constructing a second prompt word based on the reasoning path and the original question; The second prompt word is input into a preset large language model, and the reasoning path description of the original SQL statement is output through the preset large language model.
8. The method for constructing a Text-to-SQL model according to claim 1, characterized in that: The training of the preset generation model based on the enhanced training data set to obtain the SQL generation model specifically includes: Constructing a SQL statement training data set based on the SQL data pairs in the enhanced training data set, wherein the SQL data pairs include original SQL data pairs and enhanced SQL data pairs; Constructing a reasoning path description training data set according to the enhanced reasoning data pairs in the enhanced training data set; The preset generation model is trained based on the SQL statement training data set and the reasoning path description training data set to obtain an SQL generation model.
9. The method for constructing a Text-to-SQL model according to claim 8, characterized in that: The step of constructing a SQL statement training data set based on the SQL data pairs in the enhanced training data set specifically includes: For each SQL data pair, construct an SQL prefix for the SQL data pair, and obtain database information corresponding to the question in the SQL data pair; generating a first type of input data based on the SQL prefix, the database information, and a question in the SQL data pair; SQL statement training data is formed based on the first type of input data and the SQL statements in the SQL data pair to obtain a SQL statement training data set.
10. The method for constructing a Text-to-SQL model according to claim 8, characterized in that: The step of constructing the reasoning path description training data set based on the enhanced reasoning data in the enhanced training data set specifically includes: For each enhanced reasoning data pair, construct a reasoning prefix for the enhanced reasoning data pair, and obtain database information corresponding to the original question in the enhanced reasoning data pair; A second type of input data is generated based on the reasoning prefix, the database information and the original question, and reasoning path description training data is formed based on the second type of input data and the reasoning path description in the enhanced reasoning data pair to obtain a reasoning path description training data set.
11. The method for constructing a Text-to-SQL model according to claim 8, characterized in that: The step of training a preset generation model based on the SQL statement training data set and the reasoning path description training data set to obtain an SQL generation model specifically includes: For each SQL statement training data in the SQL statement training data set, select the reasoning path description training data corresponding to the SQL statement training data in the reasoning path description training data set to obtain an SQL statement training data pair, wherein the SQL statement training data pair includes the SQL statement training data and the reasoning path description training data corresponding to the SQL statement training data; Inputting the SQL statement training data pair into a preset generation model, and determining a generated SQL statement and a generated reasoning path description through the preset generation model; The preset generation model is trained based on the generated SQL statement and the generated reasoning path description to obtain a SQL generation model.
12. The method for constructing a Text-to-SQL model according to any one of claims 1 to 11, characterized in that: Determining the Text-to-SQL model based on the SQL generation model specifically includes: Constructing a first type of input data based on the original question in the original SQL data pair; Inputting the first type of input data into the SQL generation model, and generating a plurality of generated SQL statements through the SQL generation model; Constructing verification input data based on the several generated SQL statements, and training a preset optimization model based on the verification input data to obtain an optimization model; A Text-to-SQL model is determined based on the SQL generation model and the optimization model.
13. The method for constructing a Text-to-SQL model according to claim 12, characterized in that: The constructing verification input data based on the several generated SQL statements specifically includes: Obtaining a set of SQL clauses of each of a number of generated SQL statements; Execute each of the several generated SQL statements to obtain execution error information; Dividing the SQL clauses in all the acquired SQL clause sets into a first SQL clause set and a second SQL clause set according to a second preset condition; constructing a basic SQL statement based on the first set of SQL clauses, and determining additional database information and approximate value information based on the second set of SQL clauses; The additional database information, execution error information, basic SQL statements, a second set of SQL clauses, approximate value information, and the original question are merged to obtain verification input data.
14. A method for generating a SQL statement, characterized in that: Using the Text-to-SQL model constructed by the method for constructing a Text-to-SQL model according to any one of claims 1 to 13, the method for generating SQL statements specifically includes: Constructing a first type of input data based on the problem to be processed; The first type of input data is input into a Text-to-SQL model, and a corresponding target SQL statement is generated for the problem to be processed through the Text-to-SQL model.
15. The method for generating a SQL statement according to claim 14, characterized in that: The step of inputting the first type of input data into a Text-to-SQL model and generating a corresponding target SQL statement for the problem to be processed by using the Text-to-SQL model specifically includes: The first type of input data is input into a SQL generation model of a Text-to-SQL model, and a corresponding target SQL statement is generated for the problem to be processed through the SQL generation model.
16. The method for generating a SQL statement according to claim 14, characterized in that: The step of inputting the first type of input data into a Text-to-SQL model, and generating a corresponding target SQL statement for the problem to be processed through the Text-to-SQL model, further includes: Inputting the first type of input data into an SQL generation model of a Text-to-SQL model, and generating a plurality of target SQL statements for the problem to be processed through the SQL generation model; The target SQL statements are optimized by using the optimization model in the Text-to-SQL model to obtain the target SQL statements corresponding to the problem to be processed.
17. The method for generating a SQL statement according to claim 16, characterized in that: The optimizing the target SQL statements by using the optimization model in the Text-to-SQL model to obtain the target SQL statements corresponding to the problem to be processed specifically includes: Obtain a target SQL clause set and target execution error information of each target SQL statement in a number of target SQL statements; Allocating the SQL clauses in all the acquired target SQL clause sets to the first target SQL clause set and the second target SQL clause set according to the second preset condition; A final SQL statement is constructed for the problem to be processed according to the target execution error information, the first target SQL clause set and the second target SQL clause set.
18. The method for generating a SQL statement according to claim 17, characterized in that: The step of constructing a final SQL statement for the problem to be processed according to the target execution error information, the first target SQL clause set and the second target SQL clause set is as follows: When the second target SQL clause set is empty, constructing a final SQL statement for the problem to be processed based on the first target SQL clause set; When the second target SQL clause set is not empty, a final SQL statement is constructed for the problem to be processed through an optimization model in the Text-to-SQL model based on the target execution error information, the first target SQL clause set and the second target SQL clause set.
19. The method for generating a SQL statement according to claim 18, characterized in that: The step of constructing a final SQL statement for the problem to be processed based on the target execution error information, the first target SQL clause set and the second target SQL clause set by using the optimization model in the Text-to-SQL model specifically includes: constructing a target basic SQL statement based on the first target SQL clause set, and determining target additional database information and target approximate value information based on the second target SQL clause set; Merging target additional database information, target execution error information, target basic SQL statements, a second target SQL clause set, target approximate value information, and pending issues to obtain target verification input data; The target verification input data is input into the optimization model in the Text-to-SQL model, and a final SQL statement is generated for the problem to be processed through the optimization model.
20. A device for constructing a Text-to-SQL model, characterized in that: The construction device of the Text-to-SQL model specifically includes: An acquisition module, configured to acquire an original training data set, wherein the original training data set includes an original SQL data pair, and the original SQL data pair includes an original question and an original SQL statement; An enhancement module, configured to perform data enhancement on an original training data set to obtain an enhanced training data set, wherein the enhanced training data set includes an original SQL data pair, an enhanced reasoning data pair, and an enhanced SQL data pair, wherein the enhanced reasoning data pair includes a reasoning path description of an original question and an original SQL statement, and the enhanced SQL data pair includes an enhanced question and an enhanced SQL statement; A training module is used to train a preset generation model based on the enhanced training data set to obtain an SQL generation model, and determine a Text-to-SQL model based on the SQL generation model.
21. A device for generating SQL statements, characterized in that: Using the Text-to-SQL model constructed by the method for constructing a Text-to-SQL model according to any one of claims 1 to 13, the SQL statement generation device specifically includes: A construction module, used for constructing a first type of input data based on the problem to be processed; A generation module is used to input the first type of input data into a Text-to-SQL model, and generate a corresponding target SQL statement for the problem to be processed through the Text-to-SQL model.
22. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores one or more programs, and the one or more programs can be executed by one or more processors to implement the steps in the method for constructing a Text-to-SQL model as described in any one of claims 1 to 13, and / or to implement the steps in the method for generating an SQL statement as described in any one of claims 14 to 19.
23. A terminal device, characterized in that: include: Processor and memory; The memory stores a computer-readable program executable by the processor; When the processor executes the computer-readable program, it implements the steps in the method for constructing a Text-to-SQL model as described in any one of claims 1 to 13, and / or implements the steps in the method for generating an SQL statement as described in any one of claims 14 to 19.
Citation Information
Patent Citations
Data expansion method based on hierarchical question generation and self-gaming
CN119025540A
Cited By
Structured data processing method and device based on large model and server
CN119807270A
Query optimization method and device used under Text-to-SQL (Structured Query Language) task, storage medium and electronic equipment
CN120336373A
A query optimization method, device, storage medium and electronic device for Text-to-SQL tasks
CN120336373B