Training method and device for converting natural language into structured query language model

By performing synonym replacement and database pattern adjustment on the initial sample dataset, a sample dataset rich in perturbation features is constructed. The initial conversion model is then trained, which solves the problem of insufficient robustness of the natural language to structured query language model and achieves high accuracy and usability in complex scenarios.

CN121880359APending Publication Date: 2026-04-17ZHONGKE ZIDONG TAICHU (BEIJING) TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
ZHONGKE ZIDONG TAICHU (BEIJING) TECH CO LTD
Filing Date
2025-12-26
Publication Date
2026-04-17

AI Technical Summary

Technical Problem

Existing natural language to structured query language models are insufficient in terms of robustness, making it difficult to cope with complex user queries and changes in database schemas, resulting in insufficient accuracy and usability of the generated SQL queries.

Method used

By performing synonym replacement and database pattern adjustment on the initial sample dataset, a sample dataset rich in perturbation features is constructed. The initial conversion model is then trained, breaking the inherent correspondence between natural language queries and database patterns, and forcing the model to learn deep semantic alignment relationships.

Benefits of technology

It significantly improves the robustness and generalization ability of the model in complex scenarios such as synonym rewriting, database schema structure changes or out-of-order processing, ensuring the high accuracy and usability of the generated structured query language.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121880359A_ABST
    Figure CN121880359A_ABST
Patent Text Reader

Abstract

According to the natural language to structured query language model training method and device provided by the invention, the sample data set is obtained by performing synonym replacement on the initial sample data set and / or adjusting the initial database mode, so that targeted adversarial disturbance is introduced to the source of model training, and the training efficiency is improved. And the fixed literal correspondence between the natural language query keyword and the database mode is broken. Training is carried out based on the sample data set rich in disturbance features, so that a natural language to structured query language model is forced to jump out of simple feature matching logic, and a deep semantic alignment relationship between the natural language and a database architecture is learned; therefore, the robustness and generalization ability of the model when facing complex scenes such as synonymous rewriting in user query, structural change or disorder of a database mode and the like are remarkably improved, and high accuracy and usability of the generated structured query language are ensured.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of natural language processing technology, and in particular to a training method and apparatus for a natural language to structured query language model. Background Technology

[0002] Natural Language to Structured Query (NL2SQL) aims to directly translate natural language questions into SQL queries that can be executed in relational databases. This allows users to bypass the tedious process of learning database schemas and writing SQL statements, thereby achieving more efficient data acquisition and analysis. With the growth of data scale and business needs, NL2SQL has become one of the key technologies for data access, intelligent BI, and conversational data analysis.

[0003] Although large-scale pre-trained language models have demonstrated excellent capabilities in many natural language processing tasks, robustness issues are particularly prominent in the practical application of natural language to structured queries, which restricts the transformation from research to commercial application. Summary of the Invention

[0004] This invention provides a training method and apparatus for a natural language to structured query language model, which addresses the weakness of robustness in existing natural language to structured query language models.

[0005] This invention provides a training method for a natural language to structured query language model, comprising: Obtain the initial sample dataset; the initial sample dataset includes an initial sample database pattern and the initial sample query text corresponding to the initial sample database pattern; The initial sample dataset is subjected to synonym replacement, and / or the initial database pattern is adjusted to obtain the sample dataset; Based on the sample dataset, the initial conversion model is trained to obtain a natural language to structured query language model.

[0006] According to a training method for a natural language to structured query language model provided by the present invention, the adjustment of the initial database pattern includes: Obtain the initial database schema; The description order of the initial database schema is changed, and / or, elements in the initial database schema are added or deleted.

[0007] According to a training method for a natural language to structured query language model provided by the present invention, the change in the description order of the initial database schema includes: The order of description of table names in the initial database schema, and / or the order of description of fields in any table, may be changed.

[0008] According to a training method for a natural language to structured query language model provided by the present invention, the step of adding or deleting elements in the initial database schema includes: Perform add / delete operations on the table names in the initial database schema and / or fields in any table.

[0009] According to a training method for a natural language to structured query language model provided by the present invention, the synonym replacement of the initial sample dataset includes: The elements in the initial database schema are replaced with synonyms; the elements include table names and fields in the table. Alternatively, synonym replacement can be performed on the query keywords in the initial sample query text corresponding to the sample database pattern.

[0010] This invention also provides a method for converting natural language to structured queries, including: Retrieve the database schema corresponding to the text to be queried; The text to be queried and the database schema are input into the Natural Language to Structured Query Language Model to obtain the SQL query statement corresponding to the text to be queried. The natural language to structured query language model is obtained based on the training method of the natural language to structured query language model described above.

[0011] The present invention also provides a training device for a natural language to structured query language model, comprising: An initial acquisition unit acquires an initial sample dataset; the initial sample dataset includes an initial sample database pattern and an initial sample query text corresponding to the initial sample database pattern. The construction unit performs synonym replacement on the initial sample dataset and / or adjusts the initial database pattern to obtain the sample dataset; The training unit trains the initial conversion model based on the sample dataset to obtain a natural language to structured query language model.

[0012] The present invention also provides a natural language to structured query device, comprising: Input the acquisition unit to obtain the database schema corresponding to the text to be queried; The conversion unit inputs the text to be queried and the database schema into the natural language to structured query language model to obtain the SQL query statement corresponding to the text to be queried. The natural language to structured query language model is obtained based on the training method of the natural language to structured query language model described in any one of the above-mentioned methods.

[0013] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, it implements a training method for a natural language to structured query language model as described above, or a natural language to structured query method.

[0014] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a training method for a natural language to structured query language model as described above, or a natural language to structured query method.

[0015] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements a training method for a natural language to structured query language model as described above, or a natural language to structured query method.

[0016] The training method and apparatus for a natural language to structured query language model provided by this invention introduces targeted adversarial perturbations at the source of model training by performing synonym replacement on the initial sample dataset and / or adjusting the initial database schema. This breaks the fixed word-for-word correspondence between natural language query keywords and database schemas. By training on this perturbation-rich sample dataset, the natural language to structured query language model is forced to move beyond simple feature matching logic and instead learn the deep semantic alignment relationship between natural language and database architecture. This significantly improves the model's robustness and generalization ability in complex scenarios such as synonym rewriting in user queries, structural changes in database schemas, or disordered queries, ensuring the high accuracy and usability of the generated structured query language. Attached Figure Description

[0017] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.

[0018] Figure 1 This is a flowchart illustrating the training method for the natural language to structured query language model provided by this invention. Figure 2 This is a flowchart illustrating the natural language to structured query method provided by the present invention; Figure 3 This is a schematic diagram of the structure of the training device for the natural language to structured query language model provided by the present invention; Figure 4 This is a schematic diagram of the structure of the natural language to structured query device provided by the present invention; Figure 5 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation

[0019] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.

[0020] It should be noted that early methods relied heavily on rules, templates, or domain-specific parsers, making it difficult to handle open-domain data patterns and complex SQL structures. In recent years, end-to-end neural network models, trained on large amounts of labeled data, have significantly improved the ability to convert natural language to SQL. To enhance cross-domain adaptability, explicit modeling of database schemas has been introduced, such as using tables, columns, and their relationships as input information, or encoding schemas using graph structures, enabling the model to maintain a certain degree of semantic alignment when encountering new schemas. Datasets such as Spider and WikiSQL have become benchmarks for evaluating cross-domain generalization, complex query expressiveness, and execution accuracy, driving research progress in interpretability, robustness, and dialect adaptation.

[0021] While large-scale pre-trained language models demonstrate excellent capabilities on many NLP tasks, robustness is a particularly prominent issue in practical NL2SQL applications. To address this problem, this invention provides a training method for a natural language to structured query language model, enabling the model to withstand interference and thus improving its robustness, allowing it to generate SQL effectively on real-world enterprise data. Figure 1 This is a flowchart illustrating the training method for the natural language to structured query language model provided by this invention, as shown below. Figure 1 As shown, the method includes: Step 110: Obtain the initial sample dataset.

[0022] The initial sample dataset includes an initial sample database pattern and an initial sample query text corresponding to the initial sample database pattern.

[0023] Here, the initial sample dataset refers to the raw training data without any perturbation, including the initial database schema and the corresponding initial sample query text. The initial database schema refers to the original, unadjusted database architecture description in the initial sample dataset. Specifically, existing datasets can be used as initial sample datasets, such as the Spider or WikiSQL datasets.

[0024] Step 120: Perform synonym replacement on the initial sample dataset, and / or adjust the initial database pattern to obtain the sample dataset.

[0025] Here, the sample dataset refers to the data set used to train the model to improve its robustness. In practical scenarios, it can be understood as training corpora containing adversarial features, either manually constructed or generated by algorithms. The sample dataset includes sample database schemas and corresponding sample query texts. The sample database schema refers to the structural description information of the database contained in the sample dataset, typically covering metadata such as table names, column names, relationships between tables, and field types, which helps the model understand the data organization. The sample query text refers to the user's query requirements described in natural language, which, combined with the database schema, is intended to be converted into executable SQL statements.

[0026] It should be noted that the purpose of constructing the schema and natural language query synonym perturbations is to improve the model's robustness when the relationships between entities are unclear. The purpose of constructing the schema structure order perturbations is to reduce the model's dependence on the inherent order of the schema. The purpose of constructing the schema structure addition and deletion perturbations is to improve the model's adaptability to changes in the data table business structure in real-world scenarios.

[0027] Understandably, the process of acquiring a sample dataset is essentially a process of constructing adversarial examples to enhance the model's generalization ability. Specifically, firstly, this can be achieved by acquiring an initial sample dataset and extracting the initial database patterns and corresponding initial sample query text.

[0028] Then, based on synonym replacement of the initial sample dataset and / or adjustments to the initial database schema, a sample dataset is obtained. Synonym replacement of the initial sample dataset can be performed on entity words or attribute words in the natural language query, or on table names or column names in the initial database schema, thereby creating semantically equivalent but differently expressed input samples.

[0029] Adjusting the initial database schema can involve rearranging the order of tables or fields, or simulating real database maintenance by adding redundant fields or deleting some fields. Finally, the data combination containing the sample database schema and sample query text, obtained after processing using one or more of these strategies, is defined as the sample dataset.

[0030] It should be noted that obtaining the sample dataset through synonym replacement and / or database pattern adjustment effectively introduces diverse noise and perturbation data during the training phase. This disrupts the fixed word-to-word correspondence between natural language queries and database patterns, simulating the diversity of user expressions and dynamic changes in database structures in real-world scenarios, such as field additions, deletions, and order changes. The resulting sample dataset provides rich adversarial examples for subsequent model training, forcing the model to learn deep semantic alignment relationships rather than simply relying on pattern matching. This lays a data foundation for improving the model's robustness in unknown or complex scenarios.

[0031] Step 130: Based on the sample dataset, train the initial conversion model to obtain a natural language to structured query language model; Here, the initial conversion model refers to the basic neural network model to be trained or fine-tuned. It can be a pre-trained large language model or other end-to-end sequence generation model with basic language understanding and generation capabilities. The Natural Language to Structured Query Language (NL2SQL) model, after optimization with specific training data, is the final model that can accurately output executable structured query language statements in the target database based on the input natural language question and database schema. This model possesses robustness against interference.

[0032] Specifically, the sample dataset constructed in step 110 can be used as training corpus input into the initial transformation model. During training, the initial transformation model receives sample query text and sample database patterns as input and calculates the loss between the predicted SQL statement output by the model and the standard SQL statement. Since the sample dataset contains synonym variants, out-of-order pattern structures, or added / deleted field structures, the parameters of the initial transformation model will be optimized to adapt to these changes during iterative training. For example, the model will learn that even if column names are replaced with synonyms or the order of tables is changed, it still needs to focus on core semantic matching rather than positional dependencies.

[0033] Understandably, this adversarial training method updates the parameters of the initial conversion model until the model converges or reaches the preset number of training rounds, ultimately resulting in a highly robust natural language to structured query language model.

[0034] It should be noted that adversarial training of the initial conversion model using a sample dataset containing perturbation features enables the generated natural language to structured query language model to effectively overcome overfitting to specific lexical expressions or specific database structures. Specifically, the model improves its tolerance to semantic shifts by learning to process data after synonym substitution; it reduces its dependence on the inherent order of the schema by learning pattern-adjusted data and enhances its adaptability to cross-domain new table structures or structural changes. Ultimately, this significantly improves the accuracy, stability, and interpretability of the NL2SQL task in real-world enterprise applications, effectively addressing the poor robustness of traditional models when facing truly complex data.

[0035] The method provided in this invention obtains a sample dataset by performing synonym replacement on the initial sample dataset and / or adjusting the initial database schema. This introduces targeted adversarial perturbations at the source of model training, breaking the fixed word-for-word correspondence between natural language query keywords and database schemas. By training on this perturbation-rich sample dataset, the natural language to structured query language model is forced to move beyond simple feature matching logic and instead learn the deep semantic alignment between natural language and database architecture. This significantly improves the model's robustness and generalization ability in complex scenarios such as synonym rewriting in user queries, structural changes in database schemas, or disordered queries, ensuring the high accuracy and usability of the generated structured query language.

[0036] Based on any of the above embodiments, step 120, adjusting the initial database schema, includes: Obtain the initial database schema; The description order of the initial database schema is changed, and / or, elements in the initial database schema are added or deleted.

[0037] Here, an element refers to the basic unit that constitutes a database schema, mainly including table names and fields.

[0038] Specifically, firstly, the raw database schema information can be read from existing Text-to-SQL datasets, such as Spider, WikiSQL, or from the enterprise's internal business database logs.

[0039] Then, to construct adversarial examples that can improve the model's adaptability, two main structural perturbation strategies can be adopted. The first is sequence perturbation, which randomly shuffles or rearranges the order of the components in the initial database schema to generate a new schema description that is logically equivalent but has a completely different text sequence. The second is addition / deletion perturbation, which randomly removes some of the original components from the initial database schema or randomly inserts some redundant elements that are irrelevant to the current query.

[0040] It should be noted that the order change can be performed alone, the add / delete operation can be performed alone, or both operations can be performed simultaneously, thus obtaining sample database patterns of different forms.

[0041] Understandably, after obtaining the adjusted sample database pattern, it is paired with the corresponding sample query text. This sample query text may be the initial sample query text or it may have been replaced with synonyms. These paired data are then combined and incorporated into the training corpus to form the final sample dataset used for model training.

[0042] The method provided in this invention breaks the overfitting of the model to the positional information of the input sequence by changing the description order of the database schema, forcing the model to focus on the name and semantics of the elements themselves rather than their position in the input. Furthermore, by adding and deleting elements, it simulates field changes caused by database version iterations in real-world scenarios, as well as redundant interference information that may exist in the data, thereby training the model to maintain stable inference capabilities even when facing non-standard or noisy schemas.

[0043] It should be noted that current natural language to structured query tasks are prone to structural errors in complex nesting, aggregation, and multi-table join scenarios, such as incorrect join conditions, incorrect grouping fields, or misaligned subqueries. To address this issue, based on any of the above embodiments, the description order of the initial database schema is changed, including: The order of description of table names in the initial database schema, and / or the order of description of fields in any table, may be changed.

[0044] Here, the order of database table names refers to the order in which each table appears when the entire database schema is serialized into text input. Additionally, the order of field descriptions refers to the order in which the columns and attributes of a specific table appear when describing that table.

[0045] Specifically, when processing the initial database schema, table-level shuffling can be performed, that is, randomly shuffling the definition order of different data tables. For example, the original order is "Table A, Table B, Table C", but after the change, it may become "Table C, Table A, Table B".

[0046] Alternatively, field-level reordering can be performed, which means randomly shuffling the order of column names within each data table. For example, for the "Employees" table, the original field order is "ID, Name, Salary", but after the change, it may become "Salary, ID, Name".

[0047] It should be noted that table-level shuffling, field-level shuffling, or both can be implemented to generate a sample database pattern for training.

[0048] The method provided in this invention completely eliminates the possibility of the model using positional bias for prediction by finely shuffling the order of table names and field names. It can prevent the model from incorrectly learning false relevances such as the first field usually being the primary key or usually querying table B after table A, thereby significantly improving the model's generalization ability and robustness when facing database patterns with arbitrary permutations.

[0049] It should be noted that current natural language to structured query tasks have limited generalization ability to unseen schemas, and are prone to generating invalid or incomplete SQL when faced with new tables, fields, and relationships. To address this issue, based on any of the above embodiments, adding or deleting elements in the initial database schema includes: Perform add / delete operations on the table names in the initial database schema and / or fields in any table.

[0050] Specifically, to simulate the dynamic evolution of database structures in real-world business scenarios, some table names or field names can be randomly deleted from the initial database schema. For example, the "department_id" field can be deleted to train the model to still perform inference based on the remaining information even when some non-core information is missing. Alternatively, noise can be injected into the initial database schema by adding redundant tables or fields that are completely irrelevant to the current natural language query. For example, in an employee information query task, an additional "hobbies" field or a "cafeteria menu" table could be added. This method yields a sample database schema containing distractors.

[0051] It should be noted that adding and deleting table names and fields greatly enhances the model's robustness against interference. Specifically, deletion operations force the model to adapt to incomplete schema contexts, preventing program crashes or illusions caused by missing fields; while addition operations effectively train the model's attention mechanism, enabling it to accurately filter out the truly needed tables and fields from a large amount of irrelevant and redundant information, thereby avoiding structural errors and improving the model's practicality in complex and redundant enterprise-level database environments.

[0052] Based on any of the above embodiments, step 120, performing synonym replacement on the initial sample dataset, includes: The elements in the initial database schema are replaced with synonyms; the elements include table names and fields in the table. Alternatively, synonym replacement can be performed on the query keywords in the initial sample query text corresponding to the sample database pattern.

[0053] Specifically, when choosing to perform synonym replacement on the initial sample dataset, one of the following two methods can be selected to construct the sample dataset. It should be noted that, to maximize the effect of adversarial training, the method provided in this embodiment will not simultaneously perform synonym replacement on the initial sample query text and the initial database schema. For example, when performing synonym replacement on the schema, synonym replacement will not be performed on the natural language query. This is to ensure that there is no direct correspondence between the natural language query and the column names and table names in the database schema, thereby enhancing the model's robustness to the diversity and expression variations of natural language, avoiding semantic deviation caused by superficial differences in vocabulary, and generating semantically consistent SQL queries.

[0054] The first method involves replacing elements in the initial database schema with synonyms. Specifically, to simulate the difference between database naming conventions in real-world enterprise data and everyday user language, table or column names in the initial database schema can be replaced. For example, using a large language model or a pre-defined thesaurus, key entity words in the initial database schema can be identified. For instance, a table named "Employee" can be replaced with "Staff" or "Personnel"; or a field named "Salary" can be replaced with "Wages," "Salary," or "Compensation."

[0055] Understandably, by replacing synonyms, sample database patterns with changes in naming are generated, while the corresponding SQL logical structure remains unchanged. This requires the model to not rely solely on the word "salary" to match the "salary" column during training, but to understand the semantic relationship between "salary" and "nature," that is, to understand the semantic relationship between synonyms.

[0056] The second approach involves replacing query keywords in the initial sample query text corresponding to the sample database pattern with synonyms. Specifically, to improve the model's ability to understand diverse user expressions, the initial sample query text can be processed. First, a large language model or other natural language processing tools can be used to segment and extract keywords from the query text, identifying nouns in the sentences that refer to database tables or columns. Then, synonyms or near-synonyms of these keywords are generated for replacement. For example, replacing "salary" with "wage" in the query text "What is the average salary..." yields the new sample query text "What is the average wage...".

[0057] The method provided in this invention constructs adversarial samples with semantically equivalent but different expressions through the two synonym replacement strategies described above. This data feature of semantically equivalent but differing expressions can effectively train the model to overcome surface-level differences in vocabulary and establish a deep semantic mapping relationship between natural language and database schema. Especially in real-world applications, the natural language vocabulary used by users often differs from the professional field names defined by database administrators. For example, a user might say "money," while the database stores "amount." The method provided in this invention can significantly enhance the model's robustness in the face of such synonym rewriting, reduce semantic deviations caused by keyword mismatches, and thus generate semantically consistent and correct SQL queries.

[0058] Based on any of the above embodiments Figure 2 This is a flowchart illustrating the natural language to structured query method provided by the present invention, as shown below. Figure 2 As shown, the method includes: Step 210: Obtain the database schema corresponding to the text to be queried.

[0059] Here, the query text refers to the natural language question entered by the user in a real-world application scenario, expressing the expectation of obtaining data results. For example, a user might ask, "What employees earn more than 5000?" Here, "database schema" specifically refers to the architectural description information of the target database, that is, the architectural description information of the actual database from which the user hopes to retrieve data, including but not limited to the list of table names, the list of field names for each table, primary and foreign key relationships, and the data types of the fields.

[0060] Specifically, when a user enters text to query via a terminal device or application programming interface (API), the underlying data source for the query must first be determined. This can be done through system preset configuration or user selection, locking onto the target database. Subsequently, the database schema for the current query is obtained by reading the target database's metadata or a pre-stored schema definition file. It's worth noting that in a real-world enterprise environment, this database schema may dynamically change as business evolves, or its naming conventions may differ from the user's colloquial expressions.

[0061] Step 220: Input the text to be queried and the database schema into the Natural Language to Structured Query Language Model to obtain the SQL query statement corresponding to the text to be queried.

[0062] The Natural Language to Structured Query Language Model is obtained based on the training method of the Natural Language to Structured Query Language Model in any of the above embodiments.

[0063] Specifically, the obtained query text and database schema are preprocessed, such as concatenating and encoding, and then fed as input data into the aforementioned natural language to structured query language model. Since this model is trained based on the methods described in the above embodiments, it has already learned a large number of adversarial examples involving synonym substitution, schema disorder, and redundant or missing fields during the training phase. Therefore, even when the input query text contains synonyms that do not perfectly match the database schema—for example, when the user inputs "employee" but the database field is "staff"—or when the input database schema contains a large number of redundant fields or the field order differs from the common order, the model can still accurately identify the correspondence between the user's query intent and the core database entities based on its trained robustness features. Finally, the model outputs the predicted SQL query statement.

[0064] The method provided in this invention utilizes an adversarially trained and enhanced model for reasoning, significantly reducing the SQL generation error rate in practical applications. Even when user queries are varied, database structures are complex, or contain interfering information, the generated SQL statements can still be guaranteed to be logically accurate and syntactically valid, thereby achieving efficient and reliable conversion from natural language to structured queries.

[0065] Based on any of the above embodiments Figure 3 This is a schematic diagram of the structure of the training device for the natural language to structured query language model provided by the present invention, as shown below. Figure 3 As shown, the device includes: The initial acquisition unit 310 acquires an initial sample dataset; the initial sample dataset includes an initial sample database pattern and an initial sample query text corresponding to the initial sample database pattern. Construction unit 320 performs synonym replacement on the initial sample dataset and / or adjusts the initial database pattern to obtain the sample dataset; Training unit 330 trains the initial conversion model based on the sample dataset to obtain a natural language to structured query language model.

[0066] The apparatus provided in this invention obtains a sample dataset by performing synonym replacement on the initial sample dataset and / or adjusting the initial database schema. This introduces targeted adversarial perturbations at the source of model training, breaking the fixed word-for-word correspondence between natural language query keywords and database schemas. By training on this perturbation-rich sample dataset, the natural language to structured query language model is forced to move beyond simple feature matching logic and instead learn the deep semantic alignment between natural language and database architecture. This significantly improves the model's robustness and generalization ability in complex scenarios such as synonym rewriting in user queries, structural changes in database schemas, or disordered queries, ensuring the high accuracy and usability of the generated structured query language.

[0067] Based on any of the above embodiments, the construction unit is specifically used for: Obtain the initial database schema; The description order of the initial database schema is changed, and / or, elements in the initial database schema are added or deleted.

[0068] Based on any of the above embodiments, the building unit is further specifically used for: The order of description of table names in the initial database schema, and / or the order of description of fields in any table, may be changed.

[0069] Based on any of the above embodiments, the building unit is further specifically used for: Perform add / delete operations on the table names in the initial database schema and / or fields in any table.

[0070] Based on any of the above embodiments, the building unit is further specifically used for: The elements in the initial database schema are replaced with synonyms; the elements include table names and fields in the table. Alternatively, synonym replacement can be performed on the query keywords in the initial sample query text corresponding to the sample database pattern.

[0071] Based on any of the above embodiments Figure 4 This is a schematic diagram of the natural language to structured query device provided by the present invention, as shown below. Figure 4 As shown, the device includes: Input acquisition unit 410 to obtain the database pattern corresponding to the text to be queried; The conversion unit 420 inputs the text to be queried and the database pattern into the natural language to structured query language model to obtain the SQL query statement corresponding to the text to be queried. The natural language to structured query language model is obtained based on the training method of the natural language to structured query language model in any of the above embodiments.

[0072] The apparatus provided in this invention utilizes an adversarially trained and enhanced model for reasoning, significantly reducing the SQL generation error rate in practical applications. Even when user queries are varied, database structures are complex, or contain interfering information, it can ensure the logical accuracy and syntactic validity of the generated SQL statements, thereby achieving efficient and reliable conversion from natural language to structured queries.

[0073] Figure 5 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 5 As shown, the electronic device may include a processor 510, a communication interface 520, a memory 530, and a communication bus 540, wherein the processor 510, the communication interface 520, and the memory 530 communicate with each other via the communication bus 540. The processor 510 can call logical instructions in the memory 530 to execute a training method for a natural language to structured query language model. This method includes: acquiring an initial sample dataset; the initial sample dataset includes an initial sample database pattern and initial sample query text corresponding to the initial sample database pattern; performing synonym replacement on the initial sample dataset, and / or adjusting the initial database pattern to obtain a sample dataset; and training an initial conversion model based on the sample dataset to obtain a natural language to structured query language model.

[0074] Alternatively, a natural language to structured query method can be executed, the method comprising: obtaining the database schema corresponding to the text to be queried; inputting the text to be queried and the database schema into a natural language to structured query language model to obtain an SQL query statement corresponding to the text to be queried; the natural language to structured query language model is obtained based on the training method of the natural language to structured query language model described above.

[0075] Furthermore, the logical instructions in the aforementioned memory 530 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0076] On the other hand, the present invention also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the training method for the natural language to structured query language model provided by the above methods. The method includes: obtaining an initial sample dataset; the initial sample dataset includes an initial sample database pattern and initial sample query text corresponding to the initial sample database pattern; performing synonym replacement on the initial sample dataset, and / or adjusting the initial database pattern to obtain a sample dataset; and training an initial conversion model based on the sample dataset to obtain a natural language to structured query language model.

[0077] Alternatively, a natural language to structured query method can be executed, the method comprising: obtaining the database schema corresponding to the text to be queried; inputting the text to be queried and the database schema into a natural language to structured query language model to obtain an SQL query statement corresponding to the text to be queried; the natural language to structured query language model is obtained based on the training method of the natural language to structured query language model described above.

[0078] In another aspect, the present invention also provides a non-transitory computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements a training method for a natural language to structured query language model provided by the methods described above. The method includes: obtaining an initial sample dataset; the initial sample dataset including an initial sample database pattern and initial sample query text corresponding to the initial sample database pattern; performing synonym replacement on the initial sample dataset, and / or adjusting the initial database pattern to obtain a sample dataset; and training an initial conversion model based on the sample dataset to obtain a natural language to structured query language model.

[0079] Alternatively, a natural language to structured query method can be executed, the method comprising: obtaining the database schema corresponding to the text to be queried; inputting the text to be queried and the database schema into a natural language to structured query language model to obtain an SQL query statement corresponding to the text to be queried; the natural language to structured query language model is obtained based on the training method of the natural language to structured query language model described above.

[0080] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.

[0081] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.

[0082] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for training a natural language to structured query language model, characterized in that, include: Obtain the initial sample dataset; the initial sample dataset includes an initial sample database pattern and the initial sample query text corresponding to the initial sample database pattern; The initial sample dataset is subjected to synonym replacement, and / or the initial database pattern is adjusted to obtain the sample dataset; Based on the sample dataset, the initial conversion model is trained to obtain a natural language to structured query language model.

2. The training method for the natural language to structured query language model according to claim 1, characterized in that, The adjustment of the initial database schema includes: Obtain the initial database schema; The description order of the initial database schema is changed, and / or, elements in the initial database schema are added or deleted.

3. The training method for the natural language to structured query language model according to claim 2, characterized in that, The change in the order of description of the initial database schema includes: The order of description of table names in the initial database schema, and / or the order of description of fields in any table, may be changed.

4. The training method for the natural language to structured query language model according to claim 2, characterized in that, The operations of adding or deleting elements in the initial database schema include: Perform add / delete operations on the table names in the initial database schema and / or fields in any table.

5. The training method for a natural language to structured query language model according to any one of claims 1 to 4, characterized in that, The synonym replacement of the initial sample dataset includes: The elements in the initial database schema are replaced with synonyms; the elements include table names and fields in the table. Alternatively, synonym replacement can be performed on the query keywords in the initial sample query text corresponding to the sample database pattern.

6. A method for converting natural language to structured queries, characterized in that, include: Retrieve the database schema corresponding to the text to be queried; The text to be queried and the database schema are input into the Natural Language to Structured Query Language Model to obtain the SQL query statement corresponding to the text to be queried. The natural language to structured query language model is obtained based on the training method of the natural language to structured query language model according to any one of claims 1 to 5.

7. A training device for a natural language to structured query language model, characterized in that, include: An initial acquisition unit acquires an initial sample dataset; the initial sample dataset includes an initial sample database pattern and an initial sample query text corresponding to the initial sample database pattern. The construction unit performs synonym replacement on the initial sample dataset and / or adjusts the initial database pattern to obtain the sample dataset; The training unit trains the initial conversion model based on the sample dataset to obtain a natural language to structured query language model.

8. A natural language to structured query device, characterized in that, include: Input the acquisition unit to obtain the database schema corresponding to the text to be queried; The conversion unit inputs the text to be queried and the database schema into the natural language to structured query language model to obtain the SQL query statement corresponding to the text to be queried. The natural language to structured query language model is obtained based on the training method of the natural language to structured query language model according to any one of claims 1 to 5.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the training method for the natural language to structured query language model as described in any one of claims 1 to 5, or the natural language to structured query method as described in claim 6.

10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the training method for the natural language to structured query language model as described in any one of claims 1 to 5, or the natural language to structured query method as described in claim 6.