A language conversion method and device based on data enhancement and a large language model
By combining data augmentation and large language models, the problems of high threshold for SQL queries and poor model generalization ability are solved, achieving friendly conversion and efficient prediction of natural language to SQL queries.
Patent Information
- Application Number
- CN202410065536.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-17
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2044-01-17
AI Technical Summary
In existing technologies, writing SQL queries has a high barrier to entry, making it unfriendly to non-technical users. Furthermore, the BERT pre-trained language model has a small number of parameters and poor generalization ability, making it difficult to effectively convert natural language into SQL query statements.
By generating more training corpus through data augmentation techniques, breaking down the Text-To-SQL task into multiple steps, training it step by step using a large language model, and combining multiple methods to generate prompts during the prediction phase, the generalization ability and prediction accuracy of the model are improved.
It lowers the barrier to using SQL queries, improves the model's generalization ability and prediction accuracy, and enhances the effect of converting natural language to SQL queries.
Smart Images

Figure CN117931841B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of network security, and in particular to a language conversion method based on data augmentation and a large language model. Background Technology
[0002] Databases are a crucial data storage method, allowing users to extract valuable industry knowledge through database queries. The default query interface provided by databases is SQL, requiring the writing of structured, grammatically correct SQL statements. However, learning and using SQL requires a certain technical skill level, making it less user-friendly for non-technical personnel; furthermore, the underlying logic of SQL statements becomes increasingly complex, increasing the probability of errors when writing them.
[0003] Using natural language for database queries lowers the barrier to entry and improves the user experience. Text-to-SQL is a technology that converts natural language descriptions into SQL query statements. Early methods primarily used rule-based transformation. With the rapid development of Natural Language Processing (NLP), various models and methods have been applied to Text-to-SQL. For example, the patent "Language Conversion Method and System Based on Semantic Parsing and Data Construction" (application number: CN116108175A) uses the BERT pre-trained language model. However, BERT has a small number of parameters and poor generalization ability, belonging to the early product of large language models. Large language models have a large number of parameters and strong predictive power, but they have higher requirements for the quantity, diversity, and quality of data. Summary of the Invention
[0004] This disclosure provides a language transformation method and system based on data augmentation and a large language model. The method utilizes data augmentation techniques to improve the quantity, diversity, and quality of training data. To enhance the model's prediction performance, the Text-to-SQL task is broken down into steps, and a large model is used to solve each step individually, thereby obtaining the SQL query statement corresponding to the natural language.
[0005] The language transformation method based on data augmentation and large language models disclosed herein includes the following steps:
[0006] S1, Data Augmentation: Based on the initially constructed SQL query statement template and corresponding text template, according to the database schema and the semantic representation vector features of the text, more Text-To-SQL training corpus is obtained, and the training corpus is divided into training set and validation set;
[0007] S2. During the training of the Text-To-SQL model, identify the templates with high prediction error rates in the validation set, and generate multiple Text-To-SQL samples based on these templates according to the logic of step S1. Use these samples for further training of the model to obtain the final model.
[0008] S3, in the prediction phase, the prompt input to the large model is obtained through merging, including the following parts: using the semantic representation vector of the text, the first part of the prompt is to find relatively similar columns and their corresponding tables; named entity recognition is performed on the text, and the columns contained in the text and their corresponding tables are used as the second part of the prompt; similarity matching is performed between the text and the text composed of different columns and their enumerated values, and several relatively similar columns are found as the third part of the prompt.
[0009] S4 uses the predicted SQL for execution.
[0010] This disclosure also provides a language transformation system based on data augmentation and a large language model that applies the above methods, mainly including:
[0011] The data enhancement module is used to generate richer data based on existing data and the database schema.
[0012] The Text-To-SQL module is used to complete the training and prediction of the model.
[0013] The SQL execution display module executes the predicted SQL and displays the resulting data type using appropriate tools.
[0014] Compared with the prior art, the beneficial effects of this disclosure are:
[0015] (1) By using carefully designed data augmentation methods, the diversity and quality of the data were improved, laying a solid foundation for the improvement of the model's predictive ability;
[0016] (2) The model was trained by gradually increasing the difficulty of the data and combining it with the new data generated from the template with a high error rate in the previous stage, which ultimately improved the generalization ability of the model.
[0017] (3) In the model prediction stage, combining methods with different granularities effectively improves the recall rate of the prompt containing the correct column. Attached Figure Description
[0018] The above and other objects, features and advantages of this disclosure will become more apparent from the more detailed description of exemplary embodiments of this disclosure taken in conjunction with the accompanying drawings, in which the same reference numerals generally represent the same components.
[0019] Figure 1 This is an overall flowchart of an exemplary embodiment of the present disclosure. Detailed Implementation
[0020] Preferred embodiments of the present disclosure will now be described in more detail with reference to the accompanying drawings. While preferred embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that the present disclosure will be thorough and complete, and will fully convey the scope of the present disclosure to those skilled in the art.
[0021] This disclosure provides a language transformation method based on data augmentation and a large language model. A flowchart of an exemplary implementation is attached. Figure 1 As shown, it mainly includes the following modules and steps:
[0022] (1) Data Augmentation Module
[0023] Create multiple template pairs of SQL query statements and corresponding text, and categorize them into three difficulty levels: low, medium, and high. The criteria for determining the difficulty level are as follows:
[0024] 1. Low: The number of keywords does not exceed i_1 (e.g., 3), the number of fields does not exceed i_2 (e.g., 3), and nested queries are not included.
[0025] 2. In Chinese: the number of keywords does not exceed j_1 (e.g., 6), the number of fields does not exceed j_2 (e.g., 6), and the number of nested queries does not exceed j_3 (e.g., 2).
[0026] 3. High: Other queries.
[0027] The text template is paraphrased (e.g., text replacement, text rewriting) to generate multiple text templates with the same meaning. The table and columns corresponding to the template are randomly selected based on the database schema. If the column involves comparison operations, it is randomly generated (e.g., IP address, port number) or randomly selected (e.g., country name, APT organization) based on the column type, thus obtaining the final SQL statement.
[0028] However, the prompt input to the large model needs to include not only the column names directly involved in the SQL statement, but also other column names. In this embodiment, the text is input into the embedding model to obtain its semantic representation vector, and the cosine similarity is calculated between the text and the semantic representation vectors corresponding to the column descriptions. The columns with the highest similarity ranking (top k%) and their corresponding tables are added as the prompt to be generated. The order of the columns in the tables is randomly shuffled, and the column names are randomly modified based on different naming conventions (e.g., converting from serpentine to camelCase), thus obtaining the final training corpus. The training corpora of low and medium difficulty are then divided into training and validation sets.
[0029] (2) Text-To-SQL module
[0030] It is mainly divided into two stages: training and prediction. Training involves supervised fine-tuning (SFT) on the basis of a large base model, thereby enabling better generalization ability for Text-To-SQL tasks.
[0031] To further improve the model's predictive ability, this embodiment preferably uses a boosting learning method:
[0032] The first epoch trains on low-difficulty Text-To-SQL data. After the first epoch is completed, the low-difficulty data in the validation set is evaluated to obtain the data with prediction errors. These data are then mapped to templates to obtain several templates with the highest prediction error rates. Based on the logic of the previous module, multiple Text-To-SQL samples are generated using these templates.
[0033] In the second epoch, the Text-To-SQL data of varying difficulty and the data generated in the previous step are trained. Similarly, after the training in the second epoch is completed, the data of medium and low difficulty in the validation set are evaluated separately, and multiple Text-To-SQL samples are generated according to the above logic.
[0034] In the third epoch, the model is trained on the more complex Text-To-SQL data and the data generated in the previous step, resulting in the final model.
[0035] In addition to training the large model, it is also necessary to train a model with a smaller number of parameters, whose task is to perform named entity recognition (NER).
[0036] In the prediction phase, this embodiment uses three approaches to merge the predictions for the large model:
[0037] First, the text is input into the embedding model to obtain its semantic representation vector. Then, the cosine similarity of the semantic representation vectors corresponding to the column descriptions is calculated. The columns with the highest similarity ranking in the top k% and their corresponding tables are selected as part of the prompt.
[0038] Named entity recognition is performed on the text using the NER model. If an entity appears in the text, the column containing the same entity and its corresponding table are used as part of the prompt.
[0039] The BM25 algorithm is used to perform similarity matching on the text and the text composed of different columns and their enumerated values, and the columns with the highest similarity ranking are selected as part of the prompt.
[0040] The three prompts are merged and fed into the larger model for prediction.
[0041] (3) SQL execution display module
[0042] The predicted SQL is executed to obtain the corresponding results. When there are many results, they are displayed in a table format. When there are few results, appropriate tools can be used to display them, such as bar charts or column charts, based on the user's intent.
[0043] The above technical solutions are merely exemplary embodiments of the present invention. For those skilled in the art, based on the application methods and principles disclosed in the present invention, it is easy to make various types of improvements or modifications, and not limited to the methods described in the specific embodiments of the present invention. Therefore, the methods described above are merely preferred and not restrictive.
Claims
1. A language transformation method based on data augmentation and a large language model, comprising the following steps: S1, Data Augmentation: Based on the initially constructed SQL query statement template and corresponding text template, according to the database schema and the semantic representation vector features of the text, more Text-To-SQL training corpus is obtained, and the training corpus is divided into training set and validation set; S2. During the training of the Text-To-SQL model, identify the templates with high prediction error rates in the validation set, and generate multiple Text-To-SQL samples based on these templates according to the logic of step S1. Use these samples for further training of the model to obtain the final model. S3, in the prediction phase, obtains the prompt input to the large model through merging, where... It includes the following parts: using the semantic representation vector of the text, find similar columns and their corresponding tables as the first part of the prompt; perform named entity recognition on the text, and use the columns contained in the text and their corresponding tables as the second part of the prompt; perform similarity matching between the text and the text composed of different columns and their enumerated values, and find several similar columns as the third part of the prompt. S4, executes the predicted SQL; Step S1 specifically includes: S11, create multiple template pairs of SQL query statements and corresponding text texts, and divide them into low, medium and high difficulty levels; S12, rewrite the text template with synonyms to generate multiple text templates with the same meaning; according to the database schema, randomly select the table and column corresponding to the template. If the column involves comparison operations, it is randomly generated or randomly selected according to the column type, thus obtaining the final SQL statement. S13. The semantic representation vector of the text is obtained by using the embedding model, and the cosine similarity between the text and the semantic representation vector of the column description is calculated. The columns with the highest similarity ranking in the top k% and their corresponding tables are found. The order of the columns in the tables is randomly shuffled, and the column names are randomly modified based on different naming methods. These columns and their corresponding tables, together with the column names directly involved in the SQL statement, are used to generate the model prompt, thus obtaining the final training corpus. The training corpus with low and medium difficulty is divided into training set and validation set. The learning method employed in step S2 is enhancement learning, specifically including: S21, the first epoch trains on low-difficulty Text-To-SQL data. After the first epoch is completed, the low-difficulty data in the validation set is evaluated to obtain the data with prediction errors. The data is then mapped to templates to obtain several templates with the highest prediction error rates. Based on the logic of step S1, multiple Text-To-SQL samples are generated. S22, in the second epoch, train on the medium difficulty Text-To-SQL and the data generated in step S21. Similarly, after the second epoch is completed, evaluate the medium and low difficulty data in the validation set respectively, and generate multiple Text-To-SQL samples according to the above logic. S23, in the third epoch, train the model on the challenging Text-to-SQL and the data generated in step S22, and obtain the final model.
2. The method according to claim 1, characterized in that, The conditions for determining different levels of difficulty in step S11 include: Low: No more than i_1 keywords, no more than i_2 fields, and no nested queries; In Chinese: the number of keywords does not exceed j_1, the number of fields does not exceed j_2, and the number of nested queries does not exceed j_3; High: Other more complex queries.
3. The method according to claim 2, characterized in that, In step S11: i_1=3, i_2=3, j_1=6, j_2=6, j_3=2.
4. The method according to claim 1, characterized in that, The naming methods mentioned in step S13 include: serpentine naming and camelCase naming.
5. The method according to claim 1, characterized in that, It also includes the step of training a model with a small number of parameters for named entity recognition.
6. The method according to claim 1, characterized in that, In step S3, the method for generating the prompt for the large model in the prediction stage includes: The text is input into the embedding model to obtain its semantic representation vector, and the cosine similarity is calculated with the semantic representation vectors corresponding to the column descriptions. The columns with the highest similarity ranking (k%) and their corresponding tables are selected as the first part of the prompt. The text is identified using a named entity recognition model. If an entity appears in the text, the column containing the same entity and its corresponding table are used as the second part of the prompt. The text is matched with the text composed of different columns and their enumerated values based on similarity. The columns with the highest similarity ranking are selected as the third part, prompt. The three prompts are merged to obtain the prompt used as input to the large model for prediction.
7. A language conversion apparatus based on data augmentation and a large language model, applying the method of any one of claims 1-6, characterized in that, include: The data augmentation module is used to expand the initially constructed SQL query statement template and corresponding text template based on the database schema and the semantic representation vector features of the text to obtain more Text-To-SQL training data, which is then divided into training set and validation set; The Text-To-SQL module is used to train and predict the Text-To-SQL model. During training, templates with high prediction error rates in the validation set are used to generate multiple Text-To-SQL samples using the data augmentation module. These samples are then used to continue training the model to obtain the final model. During the prediction phase, the prompt is obtained by merging the samples to feed into the larger model. The SQL execution module is used to execute the predicted SQL and obtain the corresponding execution results.
8. The language conversion device based on data augmentation and a large language model according to claim 7, characterized in that, The SQL execution module is also used to display the SQL execution results.
Citation Information
Patent Citations
Language transformation method and system based on semantic analysis and data construction
CN116108175A
Method and device for converting natural language into database statement, equipment and medium
CN117370378A
