Generation method and device of language model for automatically generating query statement

By automatically synthesizing data pairs of query statements and natural language problems in the database, expanding the full data set and conducting forward and reverse interaction training, the insufficient accuracy and insufficient training data when converting natural language queries into SQL queries in the existing technology is solved. The problem of this method enables more efficient and accurate database query.

CN119396855APending Publication Date: 2025-02-07BEIJING HONGLIAN 95 INFORMATION IND CO LTD +1
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202411496847.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-24
Publication Date
2025-02-07

AI Technical Summary

Technical Problem

The prior art when converting natural language queries into SQL query statements, especially when handling complex queries and spanning multiple data tables, there is a problem of insufficient accuracy, and due to the lack of sufficient high-quality training data, the generalization of the model The ability to transform needs to be improved.

Method used

By automatically synthesizing the data pairs of query statements and natural language problems based on multiple existing query statements in the database, the entire data set is extended to train the language model. Adopt forward and reverse interaction training and continuous training to improve the language model's ability to generate natural language problems and query statements.

Benefits of technology

Excellent in handling complex cross-table queries, effectively improving the efficiency and accuracy of database queries, solving the problem of insufficient training data, and improving the generalization ability of language models.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119396855A_ABST
    Figure CN119396855A_ABST
Patent Text Reader

Abstract

The invention discloses a method and a device for generating a language model for automatically generating a query statement. The method comprises the steps that corresponding natural language questions are generated at least based on existing query statements in a database, and a seed data pair set is generated; processing the seed data pair set to expand a total data set, and constructing a first training sample, a second training sample, a third training sample and a fourth training sample; constructing an initial language model; training the initial language model by using the first training sample, and optimizing the current language model by using the second training sample based on a direct preference optimization method to generate a forward language model; training the forward language model by using the third training sample, and optimizing the current language model by using the fourth training sample based on a direct preference optimization method to generate a reverse language model; and repeating the step of generating the forward language model and the step of generating the reverse language model until a final language model is obtained.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a method and device for generating a language model for automatically generating query statements. Background Art

[0002] Currently, many companies and organizations are looking for more efficient ways to process and analyze large amounts of data stored in databases. Usually, users enter queries in natural language, such as "Please count the shopping habits of users in each region", "Please count the number of users in each age group", etc. When searching the database, it is necessary to convert the natural language questions of the user's query into the language recognized by the database, such as SQL (Structured Query Language). Although there are some tools that can convert natural language into SQL, there are still deficiencies in processing complex queries, especially when spanning multiple data tables.

[0003] In the prior art, deep learning models are used to convert natural language questions queried by users into SQL query statements. However, due to the lack of sufficient high-quality training data, the existing models may not be able to generate accurate query statements in some cases (e.g., processing complex queries).

[0004] Therefore, a new processing method is needed to overcome the above problems. Summary of the invention

[0005] The present application provides a method and device for generating a language model for automatically generating query statements, in an effort to solve or at least alleviate at least one of the above problems.

[0006] According to one aspect of the present application, a method for generating a language model for automatically generating query statements is provided, comprising: generating corresponding natural language questions respectively based on at least multiple query statements existing in a database, and using the query statements and their corresponding natural language questions as data pairs to generate a seed data pair set, wherein the query statements are based on a structured language, and the seed data pair set includes positive data pairs representing correct queries and negative data pairs representing incorrect queries; processing the seed data pair set to expand a full data set about data pairs, and constructing a first training sample, a second training sample, a third training sample and a fourth training sample based on the full data set, wherein the first training sample includes a natural language question and a query statement indicated by its positive data pair, the second training sample includes a natural language question and a query statement indicated by its positive data pair and a query statement indicated by its negative data pair, the third training sample includes a query statement and a natural language question indicated by its positive data pair, and the fourth training sample includes The method comprises the steps of: generating a query statement and a natural language question indicated by a positive data pair and a natural language question indicated by a negative data pair; constructing an initial language model, wherein the initial language model comprises a pre-training model and a fine-tuning module, and the language model is suitable for generating a corresponding query statement according to an input natural language question; training the initial language model with the first training sample, obtaining a current language model by adjusting parameters of the fine-tuning module, and optimizing the current language model with the second training sample based on a direct preference optimization method to generate a positive language model; training the positive language model with the third training sample, obtaining a current language model by adjusting parameters of the fine-tuning module, and optimizing the current language model with the fourth training sample based on a direct preference optimization method to generate a reverse language model; and repeating the steps of generating a positive language model and generating a reverse language model until a predetermined condition is met, and taking the corresponding positive language model as the final generated language model.

[0007] According to another aspect of the present application, there is provided a device for generating a language model for automatically generating query statements, comprising: a data acquisition module, adapted to generate corresponding natural language questions respectively based on at least a plurality of existing query statements in a database, and to use the query statements and the corresponding natural language questions as data pairs to generate a set of seed data pairs, and also adapted to process the set of seed data pairs to expand a full data set about data pairs, wherein the query statements are based on a structured language, and the set of seed data pairs includes positive data pairs representing correct queries and negative data pairs representing incorrect queries; a training sample generation module, adapted to construct a first training sample, a second training sample, a third training sample and a fourth training sample based on the full data set, wherein the first training sample includes a natural language question and a query statement indicated by its positive data pair, the second training sample includes a natural language question and a query statement indicated by its positive data pair and a query statement indicated by its negative data pair, the third training sample includes a query statement and a natural language question indicated by its positive data pair, and the fourth training sample includes The method comprises a method for generating a query statement and a natural language problem indicated by a positive data pair and a natural language problem indicated by a negative data pair; a model building module, adapted to build an initial language model, wherein the initial language model comprises a pre-training model and a fine-tuning module, and the language model is adapted to generate a corresponding query statement according to an input natural language problem; an interactive training module, adapted to train the initial language model using the first training sample, obtain a current language model by adjusting the parameters of the fine-tuning module, and optimize the current language model using the second training sample based on a direct preference optimization method to generate a positive language model; and adapted to train the positive language model using the third training sample, obtain a current language model by adjusting the parameters of the fine-tuning module, and optimize the current language model using the fourth training sample based on a direct preference optimization method to generate a reverse language model; and repeat the steps of generating a positive language model and generating a reverse language model until a predetermined condition is met, and the corresponding positive language model is used as the final generated language model.

[0008] According to another aspect of the present application, a data query system is provided, which is connected to one or more databases, and the data query system includes: a device for generating a language model for automatically generating query statements as described above, which is suitable for training and generating a language model for automatically generating query statements based on existing query statements in the database; a data conversion module, which is suitable for receiving natural language questions queried by users, and using the language model to process the received natural language questions to generate structured query statements; a data query module, which is suitable for querying in the corresponding database based on the query statement and returning the query results.

[0009] According to another aspect of the present application, a computing device is provided, comprising: one or more processors; a memory; and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs include instructions for executing the method as described above.

[0010] According to another aspect of the present application, a computer-readable storage medium storing one or more programs is provided. The one or more programs include instructions, which, when executed by a computing device, cause the computing device to perform the method described above.

[0011] According to another aspect of the present application, a computer program product is provided, comprising a computer program / instruction, wherein the computer program / instruction implements the above-mentioned method when executed by a processor.

[0012] In summary, according to the solution of this application, based on the existing multiple query statements in the database, data pairs about query statements and natural language questions are automatically synthesized, and the prompt method is used to expand the full data set for training the language model, solving the problem of insufficient training data. At the same time, through forward and reverse interactive training and continuous training, the ability of the language model to generate natural language questions and query statements is improved.

[0013] The solution according to the present application performs well in processing complex cross-table queries, effectively improving the efficiency and accuracy of database queries.

[0014] The above description is only an overview of the technical solution of the present application. In order to more clearly understand the technical means of the present application, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present application more obvious and easy to understand, the specific implementation methods of the present application are listed below. BRIEF DESCRIPTION OF THE DRAWINGS

[0015] In order to achieve the above and related purposes, certain illustrative aspects are described herein in conjunction with the following description and accompanying drawings, which indicate various ways in which the principles applied herein can be practiced, and all aspects and their equivalents are intended to fall within the scope of the claimed subject matter. By reading the following detailed description in conjunction with the accompanying drawings, the above and other purposes, features and advantages of the present application will become more apparent. Throughout the application, the same reference numerals generally refer to the same parts or elements.

[0016] Figure 1 A schematic diagram of a data query system 100 according to some embodiments of the present application is shown;

[0017] Figure 2 A schematic diagram of a computing device 200 according to some embodiments of the present application is shown;

[0018] Figure 3 A flow chart of a method 300 for generating a language model for automatically generating query statements according to some embodiments of the present application is shown. DETAILED DESCRIPTION

[0019] The exemplary embodiments of the present application will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present application are shown in the accompanying drawings, it should be understood that the present application can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present application and to fully convey the scope of the present application to those skilled in the art.

[0020] This application introduces a large language model (LLM), which uses its deep understanding and generation capabilities of natural language to continuously train the language model by automatically synthesizing data sets, so that the language model can generate accurate SQL query statements based on the input natural language questions, thereby solving the problems that the prior art is insufficient in accuracy when processing complex queries and the generalization ability of the model needs to be improved due to the limited training data sets.

[0021] Figure 1 FIG. 1 is a schematic diagram of a data query system 100 according to some embodiments of the present application. Figure 1 On the one hand, the data query system 100 is connected to one or more clients to receive user queries from the clients. User queries can be in the form of text, voice, etc. When the user query is voice, it can be converted into text using automatic speech recognition technology (such as Automatic Speech Recognition, ASR). This application does not limit this. On the other hand, the data query system 100 is connected to one or more databases to search in the database according to the data tables, fields, etc. defined by the query language to obtain query results. And the query results are fed back to the client.

[0022] According to an embodiment of the present application, a data query system 100 includes a coupled generation device for a language model for automatically generating query statements (hereinafter referred to as a generation device) 110, a data conversion module 120 and a data query module 130.

[0023] The generating device 110 trains and generates a language model for automatically generating query statements based on existing query statements in the database.

[0024] The data conversion module 120 receives a natural language question from a user, and processes the received natural language question using a language model to generate a structured query statement (ie, SQL).

[0025] The data query module 130 can realize user interaction, perform a query in the corresponding database based on the query statement, and return the query result.

[0026] According to some embodiments, the generation device 110 can complete the training process of the language model offline, and then deploy the trained language model to the actual application environment, which includes, for example, a data conversion module 120 and a data query module 130. In some embodiments, after training and generating the language model, the model generation device 110 transmits the language model to the data conversion module 120, which completes the conversion of natural language questions and SQL query statements online. At the same time, new query statements and / or natural language questions generated during online use, and user feedback in the actual application environment are collected as new data, so that the generation device 110 can use the newly collected data to regularly train the language model, thereby achieving continuous optimization of the language model.

[0027] Continue as Figure 1 The generating device 110 includes: a data acquisition module 112 , a training sample generating module 114 , a model building module 116 and an interactive training module 118 .

[0028] The data acquisition module 112 generates corresponding natural language questions based on at least a plurality of existing query statements in the database, wherein the query statements are based on structured language (SQL), and the natural language questions can accurately reflect the purpose of the query and are easy to understand.

[0029] Afterwards, the data acquisition module 112 uses the query statement and its corresponding natural language question as a data pair to generate a seed data pair set. The seed data pair set is represented, for example, as: {(query statement 1, natural language question 1), (query statement 2, natural language question 2), ...}. In some embodiments, the seed data pair set includes positive data pairs representing correct queries and negative data pairs representing incorrect queries. A correct query means that the query statement and the natural language question match, and the query statement and the natural language question themselves are correct. Incorrect queries include: grammatical errors and logical errors in the query statement, grammatical errors in the natural language question, mismatch between the natural language question and the query statement, and the like.

[0030] Afterwards, the data acquisition module 112 processes the seed data pair set to expand the full data set about the data pair. This greatly expands the scale and diversity of the data set, which is very helpful for subsequently improving the generalization ability of the model and the ability to handle complex queries.

[0031] For specific details about how the data acquisition module 112 automatically synthesizes a data set using existing query statements, please refer to the detailed description of method 300 below, which will not be expanded here.

[0032] The training sample generation module 114 constructs a first training sample, a second training sample, a third training sample, and a fourth training sample based on the full data set. Specifically, the first training sample includes a natural language question and a query statement indicated by a positive data pair thereof, the second training sample includes a natural language question and a query statement indicated by a positive data pair thereof and a query statement indicated by a negative data pair thereof, the third training sample includes a query statement and a natural language question indicated by a positive data pair thereof, and the fourth training sample includes a query statement and a natural language question indicated by a positive data pair thereof and a natural language question indicated by a negative data pair thereof.

[0033] The model building module 116 builds an initial language model, which includes a pre-trained model and a fine-tuning module. The language model can generate corresponding query statements based on the input natural language questions. In some embodiments, the pre-trained model adopts a large language model architecture based on Transformer, and the fine-tuning module adopts a LoRA module.

[0034] The interactive training module 118 adopts a forward and reverse interactive training method, wherein the forward training refers to inputting a natural language question and outputting a SQL query statement; the reverse training refers to inputting a SQL query statement and outputting a natural language question.

[0035] Specifically, the initial language model is trained using the first training sample, and the current language model is obtained by adjusting the parameters of the fine-tuning module, and the current language model is optimized using the second training sample based on the direct preference optimization method to generate a forward language model. Afterwards, the forward language model is trained using the third training sample, and the current language model is obtained by adjusting the parameters of the fine-tuning module, and the current language model is optimized using the fourth training sample based on the direct preference optimization method to generate a reverse language model. The steps of generating the forward language model and generating the reverse language model are repeated until the predetermined conditions are met, and the corresponding forward language model is used as the final generated language model.

[0036] According to the method of the present application, during the training process, the LoRA module is trained using positive data pairs (i.e., the first training sample and the third training sample), with the goal of minimizing the difference between the generated SQL query statement and the real SQL query statement. The weights in the LoRA module are updated by back propagation, while the weights of the original model remain unchanged. Gradient accumulation technology is used to improve training efficiency, because the task of generating SQL queries may result in higher losses for each batch. Through such a process, the powerful language capabilities of the pre-trained model can be effectively utilized, and its ability to generate SQL query statements can be further improved through the fine-tuning module.

[0037] During the Direct Preference Optimization (DPO) training phase, the model is helped to learn the preference criteria that should be followed when generating SQL query statements, thereby improving the quality of generated query statements and making them more in line with human expectations.

[0038] LoRA fine-tuning and DPO training are performed alternately, and DPO training is focused on after LoRA fine-tuning reaches a certain level. When performing iterative training, in addition to forward training, reverse training is also required. The purpose of this forward and reverse interactive training is to enable the model to better understand the bidirectional mapping relationship between natural language questions and SQL query statements, thereby improving its accuracy in generating query statements and the rationality of natural language question generation.

[0039] The data query system 100 according to the present application can be deployed in any query scenario. For example, an e-commerce platform can analyze the shopping habits of users in different regions according to the data query system 100 of the present application to optimize inventory management and marketing strategies. Specifically, a one-year sales record data set is automatically synthesized, including a sales table (sales records), a customers table (customer information), and a products table (product information). The system will automatically generate a series of natural language questions and their corresponding SQL query statements to assist in data analysis. The following is a specific business scenario example.

[0040] Assume that the e-commerce platform operation department wants to obtain shopping behavior data about users in Beijing in the fourth quarter of 2023 (October to December) to optimize inventory management. By deploying the system 100 of the present application, incremental data sets can be automatically synthesized, including relevant natural language questions and SQL query statements, and a language model can be generated to achieve data conversion, thereby obtaining query results.

[0041] In some embodiments, the database includes a sales record data table, a customer information data table, and a product information data table, and the structure of each data table is designed as follows.

[0042] Sales table (sales record data table)

[0043] sales_id (sales ID)

[0044] product_id (product ID)

[0045] customer_id

[0046] sale_date (sale date)

[0047] quantity (sales quantity)

[0048] price

[0049] Customers table (customer information data table)

[0050] customer_id

[0051] name

[0052] address

[0053] region(region)

[0054] email

[0055] phone_number

[0056] Products table (product information data table)

[0057] product_id (product ID)

[0058] product_name (product name)

[0059] category

[0060] description

[0061] unit_price

[0062] Automatically generated natural language questions and their corresponding SQL queries:

[0063] Assume that an e-commerce operator needs to find out the ten most popular products in Beijing in the fourth quarter of 2023. System 100 generates the following natural language questions based on business requirements:

[0064] “Find the ten most popular commodities in Beijing in the fourth quarter of 2023.”

[0065] The data conversion module 120 uses the language model to convert it into a corresponding SQL query statement:

[0066] SELECT p.product_name,SUM(s.quantity)AS total_quantity FROM sales sJOIN customers c ON s.customer_id=c.customer_id JOIN products p ONs.product_id=p.product_id WHERE s.sale_date BETWEEN'2023-10-01'AND'2023-12-31'AND c.region='Beijing'GROUP BY p.product_name ORDER BY total_quantity DESCLIMIT 10;

[0067] Afterwards, the data query module 130 selects the sales records of customers in Beijing within the specified time period (October 1 to December 31, 2023) from the sales table according to the query statement, and then obtains the corresponding product name and total sales quantity by associating the customers table with the products table. Finally, the total sales quantity is sorted in descending order, and the results are limited to display only the first ten records.

[0068] In some other embodiments, the model generation device 100 also performs model optimization regularly. Specifically, over time, the e-commerce platform will continue to accumulate new sales data. These new data can be used to continuously optimize model performance. For example, when new data indicates that user preferences have changed, the system 100 can automatically generate new natural language questions and SQL query statements to reflect the latest business needs. In addition, by continuously training the model, it can be ensured that the generated query statements are more accurately in line with the actual business logic.

[0069] In this way, e-commerce platforms can automatically synthesize data sets and generate natural language questions and SQL queries that meet business needs, thereby improving the efficiency and accuracy of data analysis. This not only helps optimize inventory management, but also helps e-commerce platforms better understand user behavior and develop more effective marketing strategies.

[0070] The system 100 of the present application can be implemented by one or more computing devices. The computing device can be any device with storage and computing capabilities, such as a server, a workstation, etc., or a personal computer such as a desktop computer or a notebook computer, or a terminal device such as a mobile phone, a tablet computer, a smart wearable device, an Internet of Things device, etc., but is not limited thereto.

[0071] Figure 2FIG. 2 shows a schematic diagram of a computing device 200 according to some embodiments of the present application. It should be noted that: Figure 2 The computing device 200 shown is only an example. In practice, the computing device used to implement the method of the present application can be any type of device, and its hardware configuration can be different from the above. Figure 2 The computing device 200 shown is the same as Figure 2 The computing device 200 shown is different. In practice, the computing device used to implement the embodiment of the present application can be Figure 2 The hardware components of the computing device 200 shown are increased or deleted, and the present application does not limit the specific hardware configuration of the computing device.

[0072] like Figure 2 In a basic configuration, computing device 200 includes at least one processing unit 202 and system memory 204. According to one aspect, depending on the configuration and type of computing device, processing unit 202 can be implemented as a processor. System memory 204 includes, but is not limited to, volatile storage (e.g., random access memory), non-volatile storage (e.g., read-only memory), flash memory, or any combination of such memories. According to one aspect, system memory 204 includes operating system 205 and program modules 206.

[0073] According to one aspect, operating system 205 is suitable for controlling the operation of computing device 200, for example. Furthermore, examples are practiced in conjunction with graphics libraries, other operating systems, or any other application programs, and are not limited to any particular application or system. Figure 2 This basic configuration is illustrated in FIG. 2 by those components within dashed line 208. According to one aspect, computing device 200 has additional features or functionality. For example, according to one aspect, computing device 200 includes additional data storage devices (removable and / or non-removable), such as magnetic disks, optical disks, or tapes. Such additional storage Figure 2 2 is illustrated by removable storage 209 and non-removable storage 210.

[0074] As stated above, according to one aspect, program modules are stored in the system memory 204. According to one aspect, the program modules may include one or more application programs, and the present application does not limit the types of application programs, for example, the application programs may include: email and contact applications, word processing applications, spreadsheet applications, database applications, slide show applications, drawing or computer-aided applications, web browser applications, etc.

[0075] According to one aspect, the examples may be practiced on a circuit comprising discrete electronic components, a packaged or integrated electronic chip containing logic gates, a circuit utilizing a microprocessor, or a single chip containing electronic components or a microprocessor. Figure 2 Each or many components shown in can be integrated in a system on chip (SOC) on a single integrated circuit to practice examples. According to one aspect, such a SOC device may include one or more processing units, a graphics unit, a communication unit, a system virtualization unit, and various application functions, all of which are integrated (or "burned") into a chip substrate as a single integrated circuit. When operated via SOC, the functions described in the present application can be operated via a dedicated logic integrated with other components of a computing device 200 on a single integrated circuit (chip). Other technologies that can perform logical operations (such as AND, OR, and NOT) can also be used to practice embodiments of the present application, including but not limited to mechanical, optical, fluid, and quantum technologies. In addition, embodiments of the present application can be practiced in a general-purpose computer or in any other circuit or system.

[0076] According to one aspect, the computing device 200 may also have one or more input devices 212, such as a keyboard, a mouse, a pen, a voice input device, a touch input device, a VR motion capture input device, etc. Output devices 214 may also be included, such as a display, a speaker, a printer, etc. The aforementioned devices are examples and other devices may also be used. The computing device 200 may include one or more communication connections 216 that allow communication with other computing devices 218. Examples of suitable communication connections 216 include, but are not limited to: RF transmitters, receivers and / or transceiver circuits; Universal Serial Bus (USB), parallel and / or serial ports.

[0077] The term computer-readable media as used in this application includes computer storage media. Computer storage media may include volatile and non-volatile, removable and non-removable media implemented in any method or technology for storing information (e.g., computer-readable instructions, data structures, or program modules). System memory 204, removable storage 209, and non-removable storage 210 are all examples of computer storage media (i.e., memory storage). Computer storage media may include random access memory (RAM), read-only memory (ROM), electrically erasable read-only memory (EEPROM), flash memory or other memory technology, CD-ROM, digital versatile disk (DVD) or other optical storage, cassettes, magnetic tapes, magnetic disk storage or other magnetic storage devices, or any other products that can be used to store information and can be accessed by computing device 200. According to one aspect, any such computer storage media may be part of computing device 200. Computer storage media do not include carrier waves or other propagated data signals.

[0078] According to one aspect, communication media is implemented by computer readable instructions, data structures, program modules, or other data in a modulated data signal (e.g., a carrier wave or other transport mechanism), and includes any information delivery media. According to one aspect, the term "modulated data signal" describes a signal that has one or more characteristics set or changed in a manner that encodes information in the signal. By way of example and not limitation, communication media includes wired media such as a wired network or direct-wired connection, and wireless media such as acoustic, radio frequency (RF), infrared, and other wireless media.

[0079] In an embodiment according to the present application, the computing device 200 is configured to execute the method 300 for generating a language model for automatically generating a query statement according to the present application. The program module 206 includes a plurality of program instructions for executing the method 300, and these program instructions can instruct the processing unit 202 to execute the method 300 of the present application.

[0080] Figure 3 A flow chart of a method 300 for generating a language model for automatically generating a query statement according to some embodiments of the present application is shown. The method 300 is complementary to the description of the aforementioned system 100, and the relevant parts are not repeated here.

[0081] The method 300 starts at step S310. In step S310, corresponding natural language questions are generated respectively based on at least a plurality of existing query statements in the database, and the query statements and the corresponding natural language questions are used as data pairs to generate a seed data pair set.

[0082] The query statement is based on a structured language, and the seed data pair set includes positive data pairs representing correct queries and negative data pairs representing incorrect queries.

[0083] First, based on at least a plurality of existing query statements in the database, corresponding natural language questions are generated respectively, including the following four steps.

[0084] The first step is to generate a query statement set based on multiple existing query statements in the database. Specifically, first, a query template is generated based on multiple existing query statements in the database. For example, existing SQL query statements are collected as templates. These templates have both SQL query records from actual business operations and sample queries designed according to business needs. These query templates should cover various types from simple to complex, including but not limited to operations such as SELECT, INSERT, UPDATE, and DELETE. In some embodiments, the above process can be automated by programming. Write a script or program to dynamically generate the required sample query statements according to predefined rules. These rules include how to construct a WHERE clause, JOIN operations, and other SQL syntax elements. Afterwards, modify the query template according to the business logic to generate a new query statement. For example, on an e-commerce platform, the query template can be adjusted to match the data analysis needs based on the needs of querying sales within a specific time period or user purchasing behavior in a certain region to obtain a series of new query statements. Afterwards, the data distribution of the template is statistically analyzed to generate common query statements. For example, considering the statistical characteristics of the data, such as data distribution (mean, median, standard deviation, etc.) and fields or conditions with high common query frequencies, representative query statements are generated as common query statements to ensure that common business scenarios are covered and actual query requirements are simulated. After that, complex query statements are generated. Complex queries are suitable for processing at least the following operations: table relationships, sorting, filtering, and complex queries, for example, involving multiple table joins, subqueries, etc. After that, an initial query statement set is generated using new query statements, common query statements, and complex query statements. After that, natural language processing is used to process each query statement in the initial query statement set to obtain a query statement set. For example, in order to enhance the diversity of generated SQL query statements, existing natural language processing technologies are used, such as synonym replacement, sentence structure changes, etc., to change the expression form of SQL query statements, but still retain their core logic and functions.

[0085] The second step is to parse each query statement separately to extract key information, where the key information includes the data table pointed to by the query statement (i.e., clarify the specific data table name targeted by the query) and field (i.e., determine which specific field contents need to be presented in the query results), and query conditions. The query conditions include connection conditions (including the association method and basis between different data tables), filtering conditions (such as the WHERE clause, used to filter out data records that meet specific conditions), and sorting conditions (such as the ORDER BY clause, used to determine the order in which the query results are arranged).

[0086] The third step is to generate the initial natural language questions corresponding to each query statement based on the key information. Specifically, first define the template of the natural language question. Based on the extracted rich information, define one or more groups of description templates for natural language questions. These templates can be simpler string replacement templates that generate descriptions by directly replacing specific keywords, or complex structured templates that can fully consider the query requirements in different scenarios and generate description content with different styles and characteristics. After that, use the key information accurately extracted in the second step to fill in the placeholders in the template. For example, if the SQL query statement is to select all records with order dates in 2021 from the "orders" table, then the corresponding natural language question description template may be "Get ** orders". By filling the extracted table name "orders" and the specific date condition "2021" into the template, a clear and accurate natural language question is obtained: "Get all orders in 2021".

[0087] The fourth step is to optimize each initial natural language question to generate a corresponding natural language question. In some embodiments, the optimization process includes: natural language processing enhancement, semantic consistency check, readability optimization, etc., as follows.

[0088] (1) Natural language processing enhancement: In order to further increase the variability and richness of natural language descriptions, the descriptions are further processed with the help of existing natural language processing technologies. For example, synonym replacement technology is used to replace some common words with words with similar meanings but different expressions to make the descriptions more diverse; or sentence structure change technology is used to adjust the word order and sentence structure of the sentence so that the generated descriptions can cover more real scenarios.

[0089] (2) Semantic consistency check: Strictly ensure that the generated natural language question is completely consistent with the original SQL query statement to avoid any ambiguity or misunderstanding. By comparing the logical relationship between the natural language question and the SQL query statement, any inconsistencies that may exist are corrected in a timely manner to ensure the accuracy and reliability of the natural language question description.

[0090] (3) Readability optimization: The expression of descriptions is comprehensively optimized to make them easier to understand. For example, active voice is used instead of passive voice to make sentences more concise and clear. At the same time, overly long sentences are reasonably simplified, redundant modifiers are removed, and key information is highlighted, so that users can more easily understand the content described in natural language questions.

[0091] Through the above steps, natural language questions corresponding to SQL query statements can be generated. These natural language questions should accurately convey the purpose of the query and be easy for users to understand. This natural language question generation technology is crucial for developing user-friendly natural language interfaces, which allow users to express their query needs in everyday language without having to understand the specific syntax of SQL.

[0092] After generating the natural language questions corresponding to each query statement, the query statement and the corresponding natural language question are used as data pairs to generate a seed data pair set, which specifically includes the following three steps.

[0093] The first step is to treat each query statement and its corresponding natural language question as a positive data pair representing a correct query. In other words, a query statement and its corresponding natural language question are treated as a positive data pair, recorded as (query statement 1, natural language question 1). All positive data pairs can constitute a set of positive data pairs, recorded as A = {(query statement 1, natural language question 1), (query statement 2, natural language question 2), ...}.

[0094] The second step is to generate multiple negative data pairs representing incorrect queries based at least on the query statement set and / or natural language question, where the negative data pairs include at least one of the following errors: a grammatical error in the query statement, a logical error in the query statement, and a mismatch between the query statement and the natural language question. "Incorrect query" can refer to a grammatical error in the SQL statement itself, or it can refer to a situation where the SQL statement is grammatically correct but does not match the intent of the corresponding natural language question. This means that even if the query statement has no grammatical errors, it will be considered an incorrect query if it does not correctly reflect the requirements of the natural language question. The set of generated negative data pairs is denoted as B = {(query statement m, natural language question m), (query statement m+1, natural language question m+1), ...}.

[0095] The third step is to label each positive data pair generated by the first step and each negative data pair generated by the second step to generate a seed data pair set. Continuing with the above example, the seed data pair set C is expressed as: C = A∪B. In addition, each data pair in the set is labeled to indicate whether it belongs to a positive data pair or a negative data pair.

[0096] In some embodiments, in addition to the positive data pairs and negative data pairs generated by the first step and the second step, additional multiple data pairs are added so that the data pairs contained in the seed data pair set reach a fixed number (the fixed number can be set based on experience, for example, 1000, but not limited to this). It is used to provide a sufficient number of data pair samples to help the language model learn and enhance the diversity and complexity of the data set. The supplemented data pairs can be manually written data pairs based on a database, for example, first determine a natural language question and then convert it into a query statement as a data pair. The manually written data pairs can capture edge cases or uncommon query patterns that may be missed in the above synthesis process, thereby making the language model more robust when dealing with various situations.

[0097] According to the implementation manner of the present application, negative data pairs are used to train the language model to identify and correct erroneous SQL queries, so the following three points need to be paid special attention to when generating negative data pairs.

[0098] The first point is SQL syntax errors. SQL syntax errors are deliberately created, such as missing brackets, keyword spelling errors, etc. These errors are easy to identify and are mainly used to test whether the language model can correctly identify basic SQL syntax problems.

[0099] The second point is intent mismatch. Even if the SQL statement is syntactically correct, if it does not accurately express the intent described by the natural language question, such a query statement is considered incorrect. For example, the natural language question may request "all orders in 2021", but the query statement selects "all orders in 2020", which is a mismatch in intent.

[0100] The third point is logical errors. The SQL query may be syntactically correct, but there are logical flaws, such as using incorrect join conditions and filter conditions, which results in the query results not meeting expectations.

[0101] In some embodiments, negative data pairs are generated in the following ways.

[0102] The first one is manual creation: experts write incorrect SQL query statements based on natural language questions.

[0103] The second type is random mutation: based on the correct query statement, an incorrect version is generated by randomly changing part of it (such as conditions, field names, etc.).

[0104] The third type is rule-guided: formulate a set of rules and generate incorrect SQL query statements based on these rules, such as changing the conditions in the WHERE clause, changing the JOIN conditions, etc.

[0105] The fourth type is language model generation: During the iterative training of the language model, some incorrect queries will be generated, including incorrect query statements and incorrect natural language questions. These incorrect queries are used to generate negative data pairs.

[0106] In short, the ultimate goal is to ensure that the generated negative examples can cover a variety of possible error types, so that the model can learn how to identify and correct these errors during training, thereby improving its ability to generate correct SQL queries.

[0107] In S320, the seed data pair set is processed to expand a full data set about the data pairs, and based on the full data set, a first training sample, a second training sample, a third training sample and a fourth training sample are constructed.

[0108] Among them, the first training sample includes natural language questions and query statements indicated by their positive data pairs, the second training sample includes natural language questions and query statements indicated by their positive data pairs and query statements indicated by their negative data pairs, the third training sample includes query statements and natural language questions indicated by their positive data pairs, and the fourth training sample includes query statements and natural language questions indicated by their positive data pairs and natural language questions indicated by their negative data pairs.

[0109] Specifically, the full dataset of data pairs is expanded in the following way: each data pair in the seed data pair is used as a small sample, and the full dataset is expanded through the prompt-based data augmentation model (PromDA). In other words, the full dataset contains a larger number of data pairs. In low-resource natural language understanding tasks, prompt-based data augmentation models (or data augmentation models) are often used for data augmentation, which will not be described in detail here.

[0110] The benefit of this method is that by using a small number of carefully selected or compiled seed data pairs as samples, the language model can be effectively guided to generate more data pairs that meet the requirements, thereby greatly expanding the scale and diversity of the training data set. This is very helpful for improving the generalization ability of the language model and its ability to handle complex queries.

[0111] In summary, the full data set contains multiple data pairs, and the data pairs contained can be divided into positive data pairs and negative data pairs. Afterwards, the full data set is used to generate training samples. According to an embodiment of the present application, the training samples include positive training samples and reverse training samples, and both the positive training samples and the reverse training samples include positive samples and negative samples.

[0112] Specifically, all positive data pairs are selected from the full data set, and the first training samples are generated using these positive data pairs. Specifically, the natural language questions in the positive data pairs are used as the first training data, and the corresponding query statements (i.e., the query statements in the positive data pairs to which they belong) are used as the corresponding first labeled data to form the first training samples. All natural language questions are selected from the full data set as the second training data, and the corresponding query statements (i.e., the query statements in the positive data pairs or negative data pairs to which they belong) are used as the corresponding second labeled data to form the second training samples. All positive data pairs are selected from the full data set, and the third training samples are generated using these positive data pairs. Specifically, the query statements in the positive data pairs are used as the third training data, and the corresponding natural language questions (i.e., the natural language questions in the positive data pairs to which they belong) are used as the corresponding third labeled data to form the third training samples. All query statements are selected from the full data set as the fourth training data, and the corresponding natural language questions (i.e., the natural language questions in the positive data pairs or negative data pairs to which they belong) are used as the corresponding fourth labeled data to form the fourth training samples.

[0113] In short, the first training sample and the second training sample are positive training samples: converted from natural language questions to query statements; the third training sample and the fourth training sample are reverse training samples: converted from query statements to natural language questions. Among them, the first training sample and the third training sample are both positive samples, indicating correct queries; the second training sample and the fourth training sample contain both positive samples indicating correct queries and negative samples indicating incorrect queries.

[0114] In S330, an initial language model is constructed, wherein the initial language model includes a pre-training model and a fine-tuning module, and the language model is suitable for generating corresponding query statements according to the input natural language question.

[0115] Building the initial language model includes: initializing the pre-trained model; adding a fine-tuning module (LoRA module) to each layer of the pre-trained model, the fine-tuning module contains a trainable weight matrix, and is initially close to zero.

[0116] In some embodiments, the pre-trained model uses a large language model based on the Transformer architecture (such as the qwen2-7B model). Since the pre-trained model is pre-trained on a large amount of text data, the model has powerful language understanding and generation capabilities. It has the following characteristics.

[0117] Multi-layer encoder / decoder structure: The model is usually composed of multiple layers of encoders and / or decoders, each of which contains two sub-layers: the self-attention mechanism and the feed-forward neural network. The self-attention mechanism enables the model to pay attention to different positions of the input when processing sequence data, while the feed-forward neural network is used to perform nonlinear transformations on the input.

[0118] Among them, the self-attention mechanism allows the model to focus on different positions of the input when processing sequence data, thereby capturing long-range dependencies in the input sequence. For example, the self-attention mechanism is used to process the input token sequence at each layer.

[0119] At the same time, in each layer, the input will first pass through a residual connection and then a normalization layer, which helps alleviate the gradient disappearance / explosion problem and speed up training.

[0120] Parallel processing within the layer: When processing sequence data, the traditional RNN model needs to process the input tokens one by one in order, while this model can process the entire input sequence in parallel, greatly speeding up the training speed.

[0121] According to an embodiment of the present application, LoRA (Low-Rank Adaptation) modules are added to the pre-trained model, which contain trainable weight matrices that are initially close to zero. The LoRA module is designed to minimize the impact on the original model while allowing the model to quickly adapt to specific tasks, such as generating SQL query statements from natural language questions.

[0122] After the initial language model and the above-mentioned training samples are constructed, training can begin. This application uses forward and reverse interactive training to enable the language model to have a deeper understanding of the bidirectional mapping relationship between natural language questions and query statements. In the subsequent step S340, forward training is performed (i.e., natural language questions are input and query statements are output), and in step S350, reverse training is performed (i.e., query statements are input and natural language questions are output).

[0123] According to some other embodiments of the present application, before training, it also includes: inputting the natural language question in the first training sample into the Retrieval Augmented Generation (RAG) model, so as to obtain knowledge related to the input natural language question through retrieval, to build a knowledge base about this query, so that the language model can refer to the knowledge base when generating query statements. RAG is currently a common application solution for large language models. It retrieves relevant information through its own vertical domain database, and then merges it into a prompt template to generate beautiful answers for the large model. I will not go into details here.

[0124] In S340, the initial language model is trained using the first training sample, and the current language model is obtained by adjusting the parameters of the fine-tuning module. Based on the direct preference optimization method, the current language model is optimized using the second training sample to generate a positive language model.

[0125] The forward training process consists of two steps: the first step is to train the fine-tuning module using the first training sample (i.e., positive sample); the second step is to further optimize the model based on the fine-tuning by using the second training sample (including both positive and negative samples) using the DPO (Direct Preference Optimization) method, so that the query statements it generates are more in line with human preference standards. The first and second steps are performed alternately and iteratively, and after the LoRA fine-tuning (i.e., the first step) reaches a certain level, focus on DPO training (i.e., the second step).

[0126] Specifically, in the first step, the natural language questions in the first training sample are input into the initial language model, and the difference between the query statement output by the model and the corresponding query statement in the first training sample is minimized. The parameters of the fine-tuning module are updated through back propagation (the weights of the pre-trained model remain unchanged), and the iteration is repeated until the current language model is obtained. Through the first step of training, the powerful language ability of the pre-trained model can be effectively utilized, and the LoRA module is fine-tuned using positive samples to ensure that the model can generate basically correct SQL query statements.

[0127] In the second step, the natural language question in the second training sample is input into the current language model, and the current model parameters are adjusted by calculating the contrast loss between the output query statement and the two query statements corresponding to the input natural language question in the second training sample, and the iteration is repeated until a positive language model is obtained.

[0128] Specifically, the objective function calculation method of DPO training is usually based on the concept of preference pairs, and the model parameters are optimized by comparing the difference between the query statements generated by the model and the preferred query statements (positive samples). The objective function calculation method of DPO training is as follows.

[0129] First, preference pair construction: a pair of SQL query statements (one from the positive sample and one from the negative sample) is randomly selected from the training dataset. That is, for the same natural language question, one query statement is from the positive data pair and the other is from the negative data pair, and the positive sample from the positive data pair is marked as better.

[0130] After that, the objective function is constructed: In each training step, the model generates a SQL query based on the same natural language question and compares it with the query in the preference pair. The contrast loss function is used to measure the difference between the query generated by the model and the query in the preference pair, and the model parameters are updated accordingly.

[0131] Specifically, the contrast loss is calculated as follows:

[0132]

[0133] Among them, L represents the contrast loss, Indicates the query statement for the positive data pair. Represents the query statement in the negative data pair, x i represents the input natural language question, that is, the i-th natural language question in the second training sample, and They respectively represent the query question corresponding to the i-th natural language question in the positive data pair and the query question corresponding to the negative data pair. It means that given an input x i In the case of Compare Better probability.

[0134] The purpose of the contrastive loss function is to maximize the probability of positive samples relative to negative samples. That is, we hope that the query statements generated by the model will have a higher score than the positive samples, and a lower score than the negative samples. By calculating the similarity between the generated query statements and the query statements in the preference pair (for example, through cosine similarity, cross entropy loss, etc.), we can measure the quality of the query statements generated by the model, and adjust the model parameters accordingly to make the generated query statements more inclined to the positive samples, that is, the query statements in the positive data pair.

[0135] During DPO training, after each query statement is generated, the model uses a contrast loss function to measure the difference and adjusts the model parameters through back propagation so that the model tends to generate better query statements. This process is repeated until the query statements generated by the model are closer and closer to the positive samples. In this way, DPO training can help the model learn the preference criteria that should be followed when generating query statements, thereby improving the quality of generated query statements and making them more in line with human expectations.

[0136] In S350, the forward language model is trained using the third training sample, and the current language model is obtained by adjusting the parameters of the fine-tuning module. Based on the direct preference optimization method, the current language model is optimized using the fourth training sample to generate a reverse language model.

[0137] The reverse training is based on the result of the forward training and is performed on the forward language model. The specific process includes two steps: the first step is to train the fine-tuning module using the third training sample (i.e., positive sample); the second step is to further optimize the model using the DPO method based on the fine-tuning, using the fourth training sample (including both positive and negative samples), so that the natural language questions it generates are more in line with human preference standards. The first and second steps are performed alternately, iterated repeatedly, and after the LoRA fine-tuning (i.e., the first step) reaches a certain level, focus on DPO training (i.e., the second step).

[0138] Specifically, in the first step, the query statement in the third training sample is input into the forward language model, and the parameters of the fine-tuning module in the current forward language model are updated through back propagation with the goal of minimizing the difference between the output natural language question and the natural language question in the third training sample, and the iteration is repeated until the current language model is obtained.

[0139] In the second step, the query statement in the fourth training sample is input into the current language model, and the current model parameters are adjusted by calculating the contrast loss between the output natural language question and the two natural language questions corresponding to the input query statement in the fourth training sample, and the iteration is repeated until the reverse language model is obtained.

[0140] Similarly, the contrastive loss function is used to measure the difference between the natural language questions generated by the model and the natural language questions in the positive data pairs, and the model parameters are updated accordingly. The contrastive loss is calculated as follows:

[0141]

[0142] Among them, L represents the contrast loss, represents the natural language question in the positive data pair, represents the natural language question in the negative data pair, Xi represents the input query statement, i.e., the i-th query statement in the fourth training sample, and They respectively represent the natural language question corresponding to the i-th query statement in the positive data pair and the natural language question corresponding to the negative data pair.

[0143] It should be noted that the process of reverse training is generally the same as that of forward training. LoRA is fine-tuned first, and then DPO is used for optimization. The difference is that the input and output of the two trainings are different. In summary, during forward training, natural language questions are input and query statements are output. Use correct query statements to train the model; use incorrect query statements to optimize the model so that it tends to generate correct query statements. During reverse training, query statements are input and natural language questions are output. Use correct natural language questions to train the model, and use incorrect natural language questions to optimize the model so that it tends to generate correct natural language questions. Therefore, for relevant matters, please refer to the description in step S340, which will not be repeated here.

[0144] The steps of generating a forward language model (i.e., S340) and generating a reverse language model (i.e., S350) are repeated until a predetermined condition is met, and S360 is executed to end the training, and the corresponding forward language model is used as the final generated language model. The predetermined condition is, for example, that the performance of the language model meets a predetermined standard, but is not limited thereto.

[0145] According to the implementation mode of the present application, the purpose of this forward and reverse interactive training is to enable the language model to better understand the bidirectional mapping relationship between natural language questions and query statements, thereby improving its accuracy in generating query statements and the rationality in generating natural language questions.

[0146] In other embodiments, after the language model is generated, the method further includes: obtaining new query statements and natural language questions to update the full data set; and using the updated full data set to regularly train the language model. Specifically, the new query statements and natural language questions can be generated by the system 100 in an actual application environment.

[0147] In summary, based on the method 300 of the present application, based on the existing multiple query statements in the database, data pairs about query statements and natural language questions are automatically synthesized, and the prompt method is used to expand the full data set for training the language model, solving the problem of insufficient training data. At the same time, through forward and reverse interactive training and continuous training, the ability of the language model to generate natural language questions and query statements is improved.

[0148] The solution according to the present application performs well in processing complex cross-table queries, effectively improving the efficiency and accuracy of database queries.

[0149] Optionally, in the method of the present application, after the language model is generated, it also includes: obtaining new query statements and natural language questions to update the full data set; and regularly training the language model using the updated full data set.

[0150] Optionally, the present application also includes a data query system connected to one or more databases, the data query system including: a device for generating a language model for automatically generating query statements as described above, suitable for training and generating a language model for automatically generating query statements based on existing query statements in the database; a data conversion module, suitable for receiving natural language questions queried by users, and using the language model to process the received natural language questions to generate structured query statements; a data query module, suitable for querying the corresponding database based on the query statement and returning the query results.

[0151] Optionally, the present application also includes a computing device, comprising: one or more processors; a memory; one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs include instructions for executing the method as described above.

[0152] Optionally, the present application also includes a computer-readable storage medium storing one or more programs, wherein the one or more programs include instructions, and when the instructions are executed by a computing device, the computing device performs the method as described above.

[0153] Optionally, the present application also includes a computer program product, including a computer program / instruction, wherein the method is implemented when the computer program / instruction is executed by a processor.

[0154] The various techniques described herein may be implemented in combination with hardware or software, or a combination thereof. Thus, the method and apparatus of the present application, or certain aspects or portions of the method and apparatus of the present application may be in the form of program codes (i.e., instructions) embedded in a tangible medium, such as a removable hard disk, a USB flash drive, a floppy disk, a CD-ROM, or any other machine-readable storage medium, wherein when the program is loaded into a machine such as a computer and executed by the machine, the machine becomes a device for practicing the present application.

[0155] In the case where the program code is executed on a programmable computer, the computing device generally includes a processor, a storage medium readable by the processor (including volatile and non-volatile memory and / or storage elements), at least one input device, and at least one output device. The memory is configured to store the program code; the processor is configured to execute the method of the present application according to the instructions in the program code stored in the memory.

[0156] By way of example and not limitation, readable media include readable storage media and communication media. Readable storage media stores information such as computer readable instructions, data structures, program modules or other data. Communication media generally embody computer readable instructions, data structures, program modules or other data in a modulated data signal such as a carrier wave or other transmission mechanism, and include any information transfer medium. Any combination of the above is also included within the scope of readable media.

[0157] In the description provided herein, algorithms and displays are not inherently related to any particular computer, virtual system or other device. Various general purpose systems can also be used together with the examples of the present application. According to the above description, it is obvious to construct the structure required for such systems. In addition, the present application is not directed to any specific programming language either. It should be understood that the content of the present application described herein can be realized by various programming languages, and the description of the specific language above is to disclose the preferred implementation of the present application.

[0158] In the description provided herein, a large number of specific details are described. However, it is understood that the embodiments of the present application can be practiced without these specific details. In some instances, well-known methods, structures and techniques are not shown in detail so as not to obscure the understanding of this description.

[0159] Those skilled in the art will appreciate that the modules or units or components of the devices in the examples disclosed herein may be arranged in the devices described in the embodiment, or alternatively may be located in one or more devices different from the devices in the examples. The modules in the foregoing examples may be combined into one module or may be divided into multiple submodules.

[0160] Those skilled in the art will appreciate that the modules in the devices in the embodiments can be adaptively changed and arranged in one or more devices different from the embodiments. The modules or units or components in the embodiments can be combined into one module or unit or component, and in addition they can be divided into multiple submodules or subunits or subassemblies. Except that at least some of such features and / or processes or units are mutually exclusive, all features disclosed in this specification and all processes or units of any method or device disclosed in this manner can be combined in any combination. Unless otherwise clearly stated, each feature disclosed in this specification can be replaced by an alternative feature providing the same, equivalent or similar purpose.

[0161] Furthermore, those skilled in the art will appreciate that although some embodiments described herein include certain features included in other embodiments but not other features, the combination of features from different embodiments is meant to be within the scope of the present application and to form different embodiments.

[0162] In addition, some of the embodiments are described herein as methods or combinations of method elements that can be implemented by a processor of a computer system or by other devices that perform the functions. Therefore, a processor with necessary instructions for implementing the method or method elements forms a device for implementing the method or method elements. In addition, the elements described herein of the device embodiments are examples of devices that are used to implement the functions performed by the elements for the purpose of implementing the disclosure.

[0163] As used herein, unless otherwise specified, the use of ordinal numbers "first," "second," "third," etc. to describe common objects merely refers to different instances of similar objects and is not intended to imply that the objects so described must have a given order in time, space, order, or in any other manner. In addition, the quantifier "plurality" means "two" and / or "more than two."

[0164] Although the present application is described according to a limited number of embodiments, it will be appreciated by those skilled in the art, with the benefit of the above description, that other embodiments may be envisioned within the scope of the present application thus described. In addition, it should be noted that the language used in this specification is selected primarily for the purpose of readability and teaching, rather than for the purpose of explaining or limiting the subject matter of the present application. Therefore, many modifications and changes will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the appended claims. For the scope of the present application, the disclosure made to the present application is illustrative, not restrictive, and the scope of the present application is defined by the appended claims.

Claims

1. A method for generating a language model for automatically generating a query statement, comprising: At least based on multiple existing query statements in the database, corresponding natural language questions are generated respectively, and the query statements and the corresponding natural language questions are used as data pairs to generate a seed data pair set, wherein the query statements are based on structured language, and the seed data pair set includes positive data pairs representing correct queries and negative data pairs representing incorrect queries; Processing the seed data pair set to expand a full data set about the data pairs, and constructing a first training sample, a second training sample, a third training sample, and a fourth training sample based on the full data set, wherein the first training sample includes a natural language question and a query statement indicated by a positive data pair, the second training sample includes a natural language question and a query statement indicated by a positive data pair and a query statement indicated by a negative data pair, the third training sample includes a query statement and a natural language question indicated by a positive data pair, and the fourth training sample includes a query statement and a natural language question indicated by a positive data pair and a natural language question indicated by a negative data pair; Constructing an initial language model, wherein the initial language model includes a pre-training model and a fine-tuning module, and the language model is suitable for generating corresponding query statements according to an input natural language question; The initial language model is trained using the first training sample, a current language model is obtained by adjusting the parameters of the fine-tuning module, and the current language model is optimized using the second training sample based on a direct preference optimization method to generate a positive language model; The forward language model is trained using the third training sample, and the current language model is obtained by adjusting the parameters of the fine-tuning module, and the current language model is optimized using the fourth training sample based on the direct preference optimization method to generate a reverse language model; The steps of generating a forward language model and generating a reverse language model are repeated until a predetermined condition is met, and the corresponding forward language model is used as the finally generated language model.

2. The method of claim 1, wherein: The generating corresponding natural language questions respectively based on at least a plurality of existing query statements in the database includes: Generate a query statement set based on multiple existing query statements in the database; Parsing each of the query statements to extract key information, wherein the key information includes the data table and field pointed to by the query statement, and the query condition; Based on the key information, generating an initial natural language question corresponding to each query statement; Each of the initial natural language questions is optimized respectively to generate the corresponding natural language question.

3. The method of claim 2, wherein: Generating a query statement set includes: Generate query templates based on multiple existing query statements in the database; Modify the query template according to the business logic to generate a new query statement; Performing statistics on the data distribution of the template and generating commonly used query statements; Generate complex query statements, where the complex query is suitable for processing at least the following operations: table relationships, sorting, and filtering; Generate an initial query statement set using the new query statement, the common query statement and the complex query statement; The query statements in the initial query statement set are processed by natural language processing to obtain a query statement set.

4. The method according to any one of claims 1 to 3, wherein: The generating of the seed data pair set comprises: Taking each query statement and its corresponding natural language question as the positive data pair; Based at least on the query statement set and / or the natural language question, a plurality of negative data pairs are generated, wherein the negative data pairs include at least one of the following errors: a syntax error of the query statement, a logic error of the query statement, and a mismatch between the query statement and the natural language question; The positive data pairs and the negative data pairs are labeled, and a seed data pair set is generated.

5. The method according to any one of claims 1 to 4, wherein: The processing of the seed data pair set to expand a full data set about the data pair includes: Each data pair in the seed data pair is used as a small sample, and the full data set is expanded and generated through a prompt-based data expansion model.

6. The method according to any one of claims 1 to 5, wherein: The constructing of the initial language model comprises: Initialize the pre-trained model; A fine-tuning module is added to each layer of the pre-trained model, wherein the fine-tuning module includes a trainable weight matrix.

7. The method according to any one of claims 1 to 6, wherein: The method of training the initial language model using the first training sample, obtaining the current language model by adjusting the parameters of the fine-tuning module, and optimizing the current language model using the second training sample based on the direct preference optimization method to generate a positive language model includes: Inputting the natural language question in the first training sample into the initial language model, and taking the difference between the output query sentence and the query sentence in the first training sample as the minimum, updating the parameters of the fine-tuning module through back propagation, and repeating the iteration until the current language model is obtained; The natural language question in the second training sample is input into the current language model, and the current model parameters are adjusted by calculating the contrast loss between the output query statement and the two query statements corresponding to the input natural language question in the second training sample, and the iteration is repeated until a positive language model is obtained.

8. The method according to any one of claims 1 to 7, wherein: The method of training the forward language model by using the third training sample, obtaining the current language model by adjusting the parameters of the fine-tuning module, and optimizing the current language model by using the fourth training sample based on the direct preference optimization method to generate a reverse language model includes: Inputting the query sentence in the third training sample into the forward language model, and taking the difference between the output natural language question and the natural language question in the third training sample as the minimum, updating the parameters of the fine-tuning module through back propagation, and repeating the iteration until the current language model is obtained; Input the query statement in the fourth training sample into the current language model, and adjust the current model parameters by calculating the contrast loss between the output natural language question and the two natural language questions corresponding to the input query statement in the fourth training sample, and repeat the iteration until the reverse language model is obtained.

9. The method according to claim 7 or 8, wherein the contrast loss is calculated by: in, L represents the contrast loss, Represents the query statement or natural language question in the positive data pair, represents the query statement or natural language question in the negative data pair, x i Represents the input natural language question or query.

10. A device for generating a language model for automatically generating a query statement, comprising: A data acquisition module, adapted to generate corresponding natural language questions respectively based on at least a plurality of existing query statements in a database, and to use the query statements and the corresponding natural language questions as data pairs to generate a set of seed data pairs, and further adapted to process the set of seed data pairs to expand a full data set about the data pairs, wherein the query statements are based on a structured language, and the set of seed data pairs includes positive data pairs representing correct queries and negative data pairs representing incorrect queries; A training sample generation module, adapted to construct a first training sample, a second training sample, a third training sample and a fourth training sample based on the full data set, wherein the first training sample includes a natural language question and a query statement indicated by a positive data pair thereof, the second training sample includes a natural language question and a query statement indicated by a positive data pair thereof and a query statement indicated by a negative data pair thereof, the third training sample includes a query statement and a natural language question indicated by a positive data pair thereof, and the fourth training sample includes a query statement and a natural language question indicated by a positive data pair thereof and a natural language question indicated by a negative data pair thereof; A model building module, adapted to build an initial language model, the initial language model comprising a pre-training model and a fine-tuning module, the language model being adapted to generate a corresponding query statement according to an input natural language question; The interactive training module is suitable for training the initial language model with the first training sample, obtaining the current language model by adjusting the parameters of the fine-tuning module, and optimizing the current language model with the second training sample based on the direct preference optimization method to generate a forward language model; it is also suitable for training the forward language model with the third training sample, obtaining the current language model by adjusting the parameters of the fine-tuning module, and optimizing the current language model with the fourth training sample based on the direct preference optimization method to generate a reverse language model; repeating the steps of generating the forward language model and generating the reverse language model until a predetermined condition is met, and taking the corresponding forward language model as the final generated language model.

Citation Information

Cited By

  • Training corpus generation method and device based on large language model, medium and equipment

    CN120509494A

  • Big language model-based training corpus generation method and device, medium and equipment

    CN120509494B