Natural language-to-structured query generation method and device, electronic equipment and medium
By performing structured fine-grained decomposition and automatic training set construction on natural language problems, the problem of complex SQL structural characteristics and SQL fragment correspondence in the existing technology is solved, and an efficient and interpretable natural language to structured query generation process is realized.
Patent Information
- Application Number
- CN202411600102.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-11
- Publication Date
- 2025-04-18
- Estimated Expiration
- 2044-11-11
AI Technical Summary
The structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not considered in the prior art, resulting in poor query results, poor robustness, and it is difficult to deal with complex natural language problems.
By performing structured fine-grained decomposition of manually labeled sample data sets, building automatic training sets, adjusting the basic model to obtain the query model, and realizing the transformation of natural language problems into a structured query language with structured fine-grained decomposition.
It reduces the difficulty of complex natural language problems, improves the accuracy and robustness of query results, and has good interpretability in the generation process.
Smart Images

Figure CN119848075A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of data analysis, and in particular to a method, device, electronic device and medium for generating structured queries from natural language. Background Art
[0002] Structured databases have gradually become a common carrier for storing structured data. The task of natural language to structured query generation (Text-to-SQL) aims to convert natural language questions into machine-executable SQL (Structured Query Language). Although skilled professionals can efficiently access this data through structured query language, natural language interfaces can help more non-technical users access data, greatly reducing the threshold for data access. Natural language to structured query generation is the basis for data analysis of structured databases and is widely used in many data analysis scenarios. In the retail industry, companies usually have a large amount of sales, inventory, and customer data stored in structured databases. Text-to-SQL systems can help business analysts with non-technical backgrounds access and analyze this data more easily. In the manufacturing industry, companies usually collect a large amount of production line data, including equipment status, product quality test results, etc. Text-to-SQL systems can help quality control personnel analyze this data more conveniently. Therefore, the task of natural language to structured query generation has received widespread attention from academia and industry.
[0003] In the related art, the language model-based method has become the mainstream method for natural language to structured query generation tasks. Among them, the fine-tuning method fine-tunes the open source small model by using natural language question-SQL query pairs to improve its performance in generating SQL.
[0004] However, in the related art, the fine-tuning-based method still directly generates the corresponding SQL based on the natural language question, which cannot effectively deal with complex natural language questions, and the generation process lacks explainability. Although the related art can divide the generation process from natural language questions to SQL into several sub-steps, it does not consider the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language questions, resulting in suboptimal performance, which needs to be improved. Summary of the invention
[0005] The present application provides a method, device, electronic device and medium for generating structured queries from natural language to solve the technical problems in the related art that the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not taken into account, thereby affecting the query results, having poor robustness, and being difficult to effectively deal with complex natural language problems.
[0006] The first aspect of the present application provides a method for generating structured queries from natural language, which is applied to the model building stage, wherein the method includes the following steps: performing structured fine-grained decomposition on a manually annotated sample data set to obtain a structured fine-grained decomposition process under each query mode; constructing an automatic training set based on natural language sample questions and the structured fine-grained decomposition process; using the automatic training set to adjust the target basic model to obtain a corresponding query model, so as to use the query model to convert the natural language question into a structured query language with structured fine-grained decomposition.
[0007] Optionally, in one embodiment of the present application, before performing structured fine-grained decomposition of a manually annotated sample data set, it also includes: determining keywords of natural language questions, wherein the keywords include selection content, conditional content, grouping and sorting content, and table connections; and defining structured fine-grained decomposition based on the keywords.
[0008] Optionally, in one embodiment of the present application, before performing structured fine-grained decomposition on the manually annotated sample data set to obtain the structured fine-grained decomposition process under each query mode, it also includes: obtaining multiple query modes based on keywords included in the natural language question; analyzing the keywords in the manually annotated sample data set to determine the corresponding query mode.
[0009] Optionally, in one embodiment of the present application, the construction of an automatic training set based on a natural language sample question and the structured fine-grained decomposition process includes: obtaining a data set to be labeled in the natural language sample question; extracting keywords from the data set to be labeled, and determining a corresponding query pattern based on the keywords; obtaining corresponding prompts based on the query pattern, database-related information and the structured fine-grained decomposition process; obtaining a thought chain annotation result based on the prompt, and using the thought chain annotation result to construct the automatic training set.
[0010] The second aspect of the present application provides a method for generating structured queries from natural language, which is applied to the model application stage, wherein the method includes the following steps: obtaining a user question to be queried; inputting the user question into a pre-built query model, and converting the user question into a structured query language with structured fine-grained decomposition, so as to use the structured query language to query the answer to the question in a database, wherein the query model is obtained by a basic model and an automatic training set.
[0011] The third aspect of the present application provides a natural language to structured query generation device, which is applied to the model building stage, wherein the device includes: a decomposition module, which is used to perform structured fine-grained decomposition on a manually annotated sample data set to obtain a structured fine-grained decomposition process under each query mode; a construction module, which is used to construct an automatic training set based on natural language sample questions and the structured fine-grained decomposition process; an adjustment module, which is used to use the automatic training set to adjust the target basic model to obtain a corresponding query model, so as to use the query model to convert the natural language question into a structured query language with structured fine-grained decomposition.
[0012] Optionally, in one embodiment of the present application, it also includes: a determination module for determining keywords of natural language questions, wherein the keywords include selection content, conditional content, grouping and sorting content, and table connections; a definition module for defining structured fine-grained decomposition based on the keywords.
[0013] Optionally, in one embodiment of the present application, it also includes: an acquisition module for obtaining multiple query patterns based on keywords included in the natural language question; and an analysis module for analyzing keywords in the manually annotated sample data to determine corresponding query patterns.
[0014] Optionally, in one embodiment of the present application, the construction module includes: an acquisition unit, used to acquire a data set to be annotated in a natural language sample question; an advance unit, used to extract keywords from the data set to be annotated, and determine a corresponding query pattern based on the keywords; a prompt unit, used to obtain corresponding prompts in combination with the query pattern, database-related information and the structured fine-grained decomposition process; and a construction unit, used to obtain a thought chain annotation result based on the prompt, so as to construct the automatic training set using the thought chain annotation result.
[0015] The fourth aspect of the present application provides a natural language to structured query generation device, which is applied to the model application stage, wherein the device includes: an acquisition module, used to obtain user questions to be queried; a query module, used to input the user questions into a pre-built query model, and convert the user questions into a structured query language with structured fine-grained decomposition, so as to use the structured query language to query the answers to the questions in the database, wherein the query model is obtained by a basic model and an automatic training set.
[0016] The fifth aspect of the present application provides an electronic device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the method for generating a structured query from a natural language as described in the above embodiment.
[0017] A sixth aspect of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable the computer to execute the natural language to structured query generation method as described in the above embodiment.
[0018] The seventh aspect of the present application provides a computer program product, including a computer program, which, when executed, is used to implement the above natural language to structured query generation method.
[0019] The embodiment of the present application can perform structured fine-grained decomposition on the manually annotated sample data set to obtain the structured fine-grained decomposition process under each query mode, construct an automatic training set according to the natural language sample problem and the structured fine-grained decomposition process, adjust the target basic model, and obtain the corresponding query model, thereby realizing the process of converting the natural language problem into a structured query language with structured fine-grained decomposition by using the query model, so as to divide the generation process from natural language to structured query into structured sub-steps, each sub-step generates a fine-grained structured query fragment, and finally combine the fragments of each sub-step to obtain the final structured query, and reduce the difficulty of complex natural problems through structured fine-grained decomposition, while the generation process has good interpretability. Thus, the technical problem that the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not taken into account in the related technology, thereby affecting the query results, having poor robustness, and being difficult to effectively deal with complex natural language problems is solved.
[0020] Additional aspects and advantages of the present application will be given in part in the description below, and in part will become apparent from the description below, or will be learned through the practice of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] The above and / or additional aspects and advantages of the present application will become apparent and easily understood from the following description of the embodiments in conjunction with the accompanying drawings, in which:
[0022] Figure 1 A flowchart of a method for generating a structured query from a natural language according to an embodiment of the present application;
[0023] Figure 2 A flowchart of a method for generating a structured query from a natural language according to an embodiment of the present application;
[0024] Figure 3 This is an example diagram of structured fine-grained decomposition according to an embodiment of the present application;
[0025] Figure 4 A schematic diagram of the structure of a natural language to structured query generation device provided according to an embodiment of the present application;
[0026] Figure 5 A flowchart of another method for generating a structured query from a natural language according to an embodiment of the present application;
[0027] Figure 6 A schematic diagram of the structure of another natural language to structured query generation device provided according to an embodiment of the present application;
[0028] Figure 7 It is a schematic diagram of the structure of an electronic device provided according to an embodiment of the present application. DETAILED DESCRIPTION
[0029] Embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present application, and should not be construed as limiting the present application.
[0030] The following describes the natural language to structured query generation method, device, electronic device and medium of the embodiment of the present application with reference to the accompanying drawings. In view of the related art mentioned in the above background technology, the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not taken into account, thereby affecting the query results, the robustness is poor, and it is difficult to effectively deal with the technical problems of complex natural language problems. The present application provides a natural language to structured query generation method, in which a manually annotated sample data set can be structured fine-grained decomposition to obtain a structured fine-grained decomposition process under each query mode, and an automatic training set is constructed according to the natural language sample problem and the structured fine-grained decomposition process to adjust the target basic model and obtain a corresponding query model, thereby realizing the process of converting the natural language problem into a structured fine-grained decomposed structured query language using the query model, so as to divide the natural language to structured query generation process into structured sub-steps, each sub-step generates a fine-grained structured query fragment, and finally combines the fragments of each sub-step to obtain the final structured query, and reduces the difficulty of complex natural problems through structured fine-grained decomposition, and the generation process has good interpretability. As a result, the technical problem in the related technology that the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not taken into account, which affects the query results, has poor robustness, and is difficult to effectively deal with complex natural language problems is solved.
[0031] Specifically, Figure 1 A flowchart of a method for generating a structured query from a natural language is provided in an embodiment of the present application.
[0032] like Figure 1As shown, the method for generating structured queries from natural language is applied to the model building stage, wherein the method comprises the following steps:
[0033] In step S101, a structured fine-grained decomposition is performed on the manually annotated sample data set to obtain a structured fine-grained decomposition process under each query mode.
[0034] It is understandable that the core idea of structured fine-grained decomposition is to transform the conversion process from natural language questions to structured queries into structured fine-grained matching problems. First, structuring means using the relatively structured characteristics of SQL syntax to structure the entire decomposition process into steps. Second, each step contains fine-grained decomposition steps, and each fragment of the step is generated in sequence, such as the three conditional fragments in the conditional content are generated in sequence.
[0035] In the actual implementation process, the embodiment of the present application can determine the query mode corresponding to each sample before performing structured fine-grained decomposition on the manually annotated sample data set, thereby obtaining a structured fine-grained decomposition process under each query mode.
[0036] Optionally, in one embodiment of the present application, before performing structured fine-grained decomposition of a manually annotated sample data set, it also includes: determining keywords of natural language questions, wherein the keywords include selection content, conditional content, grouping and sorting content, and table connections; and defining structured fine-grained decomposition based on the keywords.
[0037] In some embodiments, the embodiments of the present application can determine the keywords of natural language questions: determine the selection content SELECT, determine the condition content WHERE, determine the grouping GROUP and sorting content ORDER, determine the table connection JOIN, and synthesize the final SQL based on the above keywords.
[0038] When each keyword is determined, the embodiment of the present application also includes fine-grained decomposition steps, such as determining the condition content part, which can be divided into determining the relevant fragments in the natural language question, determining the table, and determining the columns and values of the condition. Note that the condition content may contain multiple conditions, and there may even be nested query statements, so each step may recursively contain three sub-steps.
[0039] Optionally, in one embodiment of the present application, before performing structured fine-grained decomposition on the manually annotated sample data set to obtain the structured fine-grained decomposition process under each query mode, it also includes: obtaining multiple query modes based on keywords included in the natural language question; analyzing the keywords in the manually annotated sample data to determine the corresponding query mode.
[0040] According to the idea of structured fine-grained decomposition, the embodiment of the present application can manually annotate to obtain the structured fine-grained decomposition process corresponding to the problem.
[0041] In the embodiment of the present application, the categories and quantities of SQL keywords contained in the SQL query can be defined as query patterns. The structured fine-grained decomposition process of different query patterns is slightly different because the keywords contained are different. The most common query pattern is determined by the keyword pattern of the SQL query in the statistical data, and the structured fine-grained decomposition process is manually annotated for each common pattern according to the above definition.
[0042] In step S102, an automatic training set is constructed based on natural language sample questions and a structured fine-grained decomposition process.
[0043] Furthermore, in an embodiment of the present application, the samples annotated in the manually annotated sample data set can be used as the few sample examples in the prompt of this step. In an embodiment of the present application, a natural language question, database-related information, SQL, and a few sample examples can be given, and the structured fine-grained decomposed thinking chain data corresponding to the natural language question can be obtained by prompting an open source LLM or a closed source LLM. And the embodiment of the present application can use different LLMs to generate thinking chains for the same problem to enhance the diversity of thinking chain data. The thinking chain of the embodiment of the present application is completed in code form, so the correctness of the thinking chain can be automatically verified by executing the code, effectively filtering incorrect thinking chain data.
[0044] Optionally, in one embodiment of the present application, an automatic training set is constructed based on a natural language sample question and a structured fine-grained decomposition process, including: obtaining a data set to be labeled in the natural language sample question; extracting keywords from the data set to be labeled, and determining a corresponding query pattern based on the keywords; obtaining corresponding prompts based on the query pattern, database-related information and the structured fine-grained decomposition process; obtaining a thought chain annotation result based on the prompt, and using the thought chain annotation result to construct an automatic training set.
[0045] Specifically, for the unlabeled dataset and the structured fine-grained decomposition example of the m common patterns that have been labeled:
[0046]
[0047] Among them, q i For user questions, a i For SQL query, DB i Database related information, f j Mode M j The corresponding manually annotated structured fine-grained decomposition process. First, according to a i The SQL keywords in the question determine the mode corresponding to the question, and then get prompts based on the predefined connection templates:
[0048] M j =mode(ai )
[0049] p i = template(q i ,DB i ,f j )
[0050] Among them, p i The prompt contains natural language questions, database related information, and examples. The Mode function determines a i The Template function fills the three pieces of information into the string template to get the final prompt p i , a simple string template is as follows:
[0051] Given a natural language question {q i}, related database information {DB i}, structural fine-grained decomposition example {f j}, please refer to the example, which gives the structured fine-grained decomposition process of natural language questions to obtain the final SQL.
[0052] Finally, according to the prompt, LLM is requested to obtain the thinking chain annotation result c i :
[0053] c i =LLM(p i )
[0054] All questions are annotated to obtain an enhanced dataset with fine-grained decomposition of thought chains LLM using GPT-3.5 can already better annotate code thinking chains, and other LLMs such as GPT-4o-mini can also be used.
[0055] The thought chain designed in the embodiment of the present application is completed in code form, so the correctness of the thought chain can be automatically verified. Specifically, the generated thought chain is an executable Python code. By executing this code, an SQL statement can be generated, and then the query result can be obtained by executing the SQL statement. By comparing the query result with the standard result, the correctness of the code thought chain can be automatically determined. Therefore, the code thought chain format of the embodiment of the present application can effectively filter incorrect code thought chains.
[0056] In step S103, the target basic model is adjusted using the automatic training set to obtain a corresponding query model, so as to use the query model to convert the natural language question into a structured query language with structured fine-grained decomposition.
[0057] As a possible implementation method, the embodiment of the present application uses a data fine-tuning model with a fine-grained thought chain decomposition process to enable the model to have the ability to generate a structured fine-grained decomposition process. The embodiment of the present application can use cross entropy as the loss function to train the model on the constructed training set. The goal of the embodiment of the present application is to build an open source Text-to-SQL system, so an open source model is used as the basic model. Since the code model has more advantages in data query tasks, the embodiment of the present application can use the DeepSeek-Coder series model with strong open source code capabilities as the basic model. These basic models all use a structure similar to the Transformer decoder, mainly including multiple stacked self-attention layers, each self-attention layer mainly including a multi-head attention layer and a feedforward layer. Given a prompt input p containing natural language questions, database-related information, and examples. i And the corresponding thinking chain output c i , the model uses the following cross entropy as the loss function:
[0058]
[0059] Combination Figure 2 and Figure 3 As shown, the working principle of the natural language to structured query generation method of the embodiment of the present application is described in detail with an embodiment.
[0060] In the actual implementation process, the Text-to-SQL task can be formally defined as: input a natural language question q and database-related information DB, and the model M needs to output the structured fine-grained decomposition steps and the final SQL corresponding to the question q, that is, c = M (q, DB). The model M and the application process of the model M can be obtained as follows: Figure 2 As shown, the embodiment of the present application may include the following steps:
[0061] Step S201: structured fine-grained decomposition definition.
[0062] The core idea of structured fine-grained decomposition is to transform the conversion process from natural language questions to structured queries into structured fine-grained matching problems. First, structuring means using the relatively structured characteristics of SQL syntax to structure the entire decomposition process into five steps: determining the selection content SELECT, determining the condition content WHERE, determining the grouping GROUP and sorting content ORDER, determining the table connection JOIN, and finally combining the contents of these steps to synthesize the final SQL, such as Figure 3 As shown, it can be seen that the final SQL query is obtained by combining the variables in the first four parts according to the syntax.
[0063] Secondly, each step contains fine-grained decomposition steps. For example, the determination of the condition content can be divided into determining the relevant fragments in the natural language question, determining the table, and determining the columns and values of the condition. Note that the condition content may contain multiple conditions, and even nested query statements, so each step may recursively contain three sub-steps. Figure 3 An example of fine-grained decomposition is given, such as determining the condition content in the second step, and determining two WHERE conditions in turn.
[0064] Step S202: structured fine-grained decomposition example annotation.
[0065] In the embodiment of the present application, the categories and quantities of SQL keywords contained in SQL queries are defined as query patterns. The structured fine-grained decomposition process of different query patterns is slightly different because of the different keywords contained. A query containing a SELECT, WHERE, and JOIN is the simplest query pattern, and a complex query may contain multiple SELECT or WHERE keywords. The most common query patterns are determined by the keyword patterns of SQL queries in the statistical data, and structured fine-grained decomposition examples are manually annotated for each common pattern. These examples will serve as a few sample examples to prompt LLM to automatically construct a training set. The embodiment of the present application identifies 6 common query patterns and annotates an example for each pattern. The specific 6 patterns are as follows:
[0066] 1. Include the GROUP and HAVING keywords;
[0067] 2. Contains the GROUP keyword;
[0068] 3. Contains the WHERE keyword, and the WHERE condition contains a clause;
[0069] 4. Contains the WHERE keyword, multiple WHERE conditions, and the JOIN keyword;
[0070] 5. Contains the WHERE keyword, multiple WHERE conditions, and the ORDER keyword;
[0071] 6. Contains the SELECT keyword.
[0072] Step S203: Automatic training set construction based on few-sample prompts.
[0073] Given a natural language question, database-related information, and SQL, we can obtain structured fine-grained decomposed thought chain data corresponding to the user's question by prompting open source LLM or closed source LLM. And we can use different LLMs to generate thought chains for the same question to enhance the diversity of thought chain data.
[0074] Specifically, for the unlabeled dataset and the structured fine-grained decomposition example of the m common patterns that have been labeled:
[0075]
[0076] Among them, q i For user questions, a i For SQL query, DB i Database related information, f j Mode M j The corresponding manually annotated structured fine-grained decomposition process. First, according to a i The SQL keywords in the question determine the mode corresponding to the question, and then get prompts based on the predefined connection templates:
[0077] M j =mode(a i )
[0078] p i = template(q i ,DB i ,f j )
[0079] Among them, p i The prompt contains natural language questions, database related information, and examples. The Mode function determines a i The Template function fills the three pieces of information into the string template to get the final prompt p i , a simple string template is as follows:
[0080] Given a natural language question {q i}, related database information {BD i}, structural fine-grained decomposition example {f j}, please refer to the example, which gives the structured fine-grained decomposition process of natural language questions to obtain the final SQL.
[0081] Finally, according to the prompt, LLM is requested to obtain the thinking chain annotation result c i :
[0082] c i =LLM(p i )
[0083] All questions are annotated to obtain an enhanced dataset with fine-grained decomposition of thought chains LLM using GPT-3.5 can already better annotate code thinking chains, and other LLMs such as GPT-4o-mini can also be used.
[0084] The thought chain designed in the embodiment of the present application is completed in code form, so the correctness of the thought chain can be automatically verified. Specifically, the generated thought chain is an executable Python code. By executing this code, an SQL statement can be generated, and then the query result can be obtained by executing the SQL statement. By comparing the query result with the standard result, the correctness of the code thought chain can be automatically determined. Therefore, the code thought chain format of the embodiment of the present application can effectively filter incorrect code thought chains.
[0085] Step S204: model training.
[0086] The embodiment of the present application uses a data fine-tuning model with a fine-grained thought chain decomposition process to enable the model to have the ability to generate a structured fine-grained decomposition process. The embodiment of the present application can use cross entropy as the loss function to train the model on the constructed training set. The goal of the embodiment of the present application is to build an open source Text-to-SQL system, so an open source model is used as the basic model. Since the code model has more advantages in data query tasks, the embodiment of the present application can use the DeepSeek-Coder series model with strong open source code capabilities as the basic model. These basic models all use a structure similar to the Transformer decoder, mainly including multiple stacked self-attention layers, each self-attention layer mainly including a multi-head attention layer and a feedforward layer. Given a prompt input p containing natural language questions, database-related information, and examples i And the corresponding thinking chain output c i , the model uses the following cross entropy as the loss function:
[0087]
[0088] Step S205: Model application.
[0089] After the model training is completed, it can be applied to the processing of new problems. Given a new natural language problem, the model can automatically generate structured fine-grained decomposed SQL fragments, and finally combine the fragments to generate the final SQL. This generates SQL with higher accuracy and stronger interpretability of the generation process.
[0090] Furthermore, the embodiments of the present application can use BIRD to conduct experiments to verify the beneficial effects.
[0091] Among them, BIRD is a commonly used Text-to-SQL benchmark dataset, which is divided into training set, development set and test set. Each sample contains natural language questions, database-related information, database, and SQL. The embodiment of the present application can obtain a model that can generate a structured fine-grained decomposition process according to steps S201, S202, and S203, and evaluate the model on the development set.
[0092] Experimental results show that the method described in the embodiment of the present application exceeds the method of directly generating structured queries based on natural language questions, and the improvement on complex natural language questions is more significant. At the same time, the embodiment of the present application can generate a decomposition process, so it is more interpretable.
[0093] In summary, the natural language to structured query generation method of the embodiment of the present application can be implemented based on structured fine-grained decomposition, and by defining the structured fine-grained decomposition process, the structured fine-grained decomposition data construction with few sample prompts can be achieved.
[0094] According to the method for generating natural language to structured query proposed in the embodiment of the present application, the manually annotated sample data set can be structured and fine-grained to obtain the structured and fine-grained decomposition process under each query mode, and an automatic training set can be constructed according to the natural language sample problem and the structured and fine-grained decomposition process to adjust the target basic model and obtain the corresponding query model, thereby realizing the process of converting the natural language problem into a structured and fine-grained decomposed structured query language using the query model, so as to divide the generation process of natural language to structured query into structured sub-steps, each sub-step generates a fine-grained structured query fragment, and finally combines the fragments of each sub-step to obtain the final structured query, and reduces the difficulty of complex natural problems through structured and fine-grained decomposition, while the generation process has good interpretability. Thus, the technical problem that the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not taken into account in the related technology, thereby affecting the query results, having poor robustness, and being difficult to effectively deal with complex natural language problems is solved.
[0095] Next, a natural language to structured query generation device proposed in an embodiment of the present application is described with reference to the accompanying drawings.
[0096] Figure 4 It is a block diagram of a natural language to structured query generation device according to an embodiment of the present application.
[0097] like Figure 4 As shown, the natural language to structured query generation device 10 is applied to the model building stage, wherein the device 10 includes: a decomposition module 101, a construction module 102 and an adjustment module 103.
[0098] Specifically, the decomposition module 101 is used to perform structured fine-grained decomposition on the manually labeled sample data set to obtain a structured fine-grained decomposition process under each query mode.
[0099] The construction module 102 is used to construct an automatic training set based on natural language sample questions and a structured fine-grained decomposition process.
[0100] The adjustment module 103 is used to adjust the target basic model using the automatic training set to obtain a corresponding query model, so as to use the query model to convert the natural language question into a structured query language with structured fine-grained decomposition.
[0101] Optionally, in one embodiment of the present application, the natural language to structured query generating device 10 further includes: a determination module and a definition module.
[0102] Among them, the determination module is used to determine the keywords of the natural language question, wherein the keywords include selection content, conditional content, grouping and sorting content and table connection.
[0103] Definition module, used to define structured fine-grained decomposition based on keywords.
[0104] Optionally, in one embodiment of the present application, the natural language to structured query generating device 10 further includes: an acquisition module and an analysis module.
[0105] The acquisition module is used to obtain multiple query modes based on keywords included in the natural language question.
[0106] The analysis module is used to analyze the keywords in the manually labeled sample data to determine the corresponding query patterns.
[0107] Optionally, in one embodiment of the present application, the construction module 102 includes: an acquisition unit, an advance unit, a prompt unit and a construction unit.
[0108] The acquisition unit is used to acquire the dataset to be labeled in the natural language sample problem.
[0109] The advance unit is used to extract keywords from the dataset to be labeled and determine the corresponding query mode based on the keywords.
[0110] The prompt unit is used to obtain corresponding prompts by combining the query pattern, database related information and the structured fine-grained decomposition process.
[0111] The construction unit is used to obtain the thought chain annotation result based on the prompt, so as to construct an automatic training set using the thought chain annotation result.
[0112] It should be noted that the above explanation of the embodiment of the method for generating structured queries from natural language is also applicable to the device for generating structured queries from natural language in this embodiment, and will not be repeated here.
[0113] According to the natural language to structured query generation device proposed in the embodiment of the present application, the manually annotated sample data set can be structured and fine-grained to obtain the structured fine-grained decomposition process under each query mode, and an automatic training set is constructed according to the natural language sample problem and the structured fine-grained decomposition process to adjust the target basic model and obtain the corresponding query model, thereby realizing the process of converting the natural language problem into a structured query language with structured fine-grained decomposition by using the query model, so as to divide the natural language to structured query generation process into structured sub-steps, each sub-step generates a fine-grained structured query fragment, and finally combines the fragments of each sub-step to obtain the final structured query, and reduces the difficulty of complex natural problems through structured fine-grained decomposition, and the generation process has good interpretability. Therefore, the technical problem that the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not taken into account in the related technology, thereby affecting the query results, having poor robustness, and being difficult to effectively deal with complex natural language problems is solved.
[0114] The above is an explanation of the embodiment of the present application at the model building stage. The following is an explanation of the embodiment of the present application at the model application stage.
[0115] Specifically, Figure 5 A flowchart of a method for generating a structured query from a natural language is provided in an embodiment of the present application.
[0116] like Figure 5 As shown, the method for generating structured queries from natural language is applied in the model application stage, wherein the method comprises the following steps:
[0117] In step S501, the user question to be queried is obtained.
[0118] In step S502, the user question is input into a pre-built query model, and the user question is converted into a structured query language with structured fine-grained decomposition, so as to use the structured query language to query the answer to the question in the database, wherein the query model is obtained from the basic model and the automatic training set.
[0119] During the actual implementation process, the embodiments of the present application may use a pre-built and trained query model to process new questions.
[0120] Users can give new natural language questions, and the query model can automatically generate structured and fine-grained decomposed SQL fragments, and finally combine the fragments to generate the final SQL. This will generate SQL with higher accuracy and a stronger interpretability of the generation process.
[0121] According to the method for generating natural language to structured query proposed in the embodiment of the present application, the manually annotated sample data set can be structured and fine-grained to obtain the structured and fine-grained decomposition process under each query mode, and an automatic training set can be constructed according to the natural language sample problem and the structured and fine-grained decomposition process to adjust the target basic model and obtain the corresponding query model, thereby realizing the process of converting the natural language problem into a structured and fine-grained decomposed structured query language using the query model, so as to divide the generation process of natural language to structured query into structured sub-steps, each sub-step generates a fine-grained structured query fragment, and finally combines the fragments of each sub-step to obtain the final structured query, and reduces the difficulty of complex natural problems through structured and fine-grained decomposition, while the generation process has good interpretability. Thus, the technical problem that the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not taken into account in the related technology, thereby affecting the query results, having poor robustness, and being difficult to effectively deal with complex natural language problems is solved.
[0122] Next, a natural language to structured query generation device proposed in an embodiment of the present application is described with reference to the accompanying drawings.
[0123] Figure 6 It is a block diagram of a natural language to structured query generation device according to an embodiment of the present application.
[0124] like Figure 6 As shown, the natural language to structured query generating device 20 is applied in the model application stage, wherein the device 20 includes: an acquisition module 201 and a query module 202.
[0125] Specifically, the acquisition module 201 is used to acquire the user question to be queried.
[0126] The query module 202 is used to input the user's question into a pre-built query model, convert the user's question into a structured query language with structured fine-grained decomposition, and use the structured query language to query the answer to the question in the database, wherein the query model is obtained from the basic model and the automatic training set.
[0127] It should be noted that the above explanation of the embodiment of the method for generating structured queries from natural language is also applicable to the device for generating structured queries from natural language in this embodiment, and will not be repeated here.
[0128] According to the natural language to structured query generation device proposed in the embodiment of the present application, the manually annotated sample data set can be structured and fine-grained to obtain the structured fine-grained decomposition process under each query mode, and an automatic training set is constructed according to the natural language sample problem and the structured fine-grained decomposition process to adjust the target basic model and obtain the corresponding query model, thereby realizing the process of converting the natural language problem into a structured query language with structured fine-grained decomposition by using the query model, so as to divide the natural language to structured query generation process into structured sub-steps, each sub-step generates a fine-grained structured query fragment, and finally combines the fragments of each sub-step to obtain the final structured query, and reduces the difficulty of complex natural problems through structured fine-grained decomposition, and the generation process has good interpretability. Therefore, the technical problem that the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not taken into account in the related technology, thereby affecting the query results, having poor robustness, and being difficult to effectively deal with complex natural language problems is solved.
[0129] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. The electronic device may include:
[0130] A memory 701 , a processor 702 , and a computer program stored in the memory 701 and executable on the processor 702 .
[0131] When the processor 702 executes the program, the method for generating a structured query from a natural language provided in the above embodiment is implemented.
[0132] Furthermore, the electronic device further comprises:
[0133] The communication interface 703 is used for communication between the memory 701 and the processor 702 .
[0134] The memory 701 is used to store computer programs that can be executed on the processor 702 .
[0135] The memory 701 may include a high-speed RAM memory, and may also include a non-volatile memory (non-volatile memory), such as at least one disk memory.
[0136] If the memory 701, the processor 702 and the communication interface 703 are implemented independently, the communication interface 703, the memory 701 and the processor 702 can be connected to each other through a bus and communicate with each other. The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 7 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0137] Optionally, in a specific implementation, if the memory 701, the processor 702 and the communication interface 703 are integrated on a chip, the memory 701, the processor 702 and the communication interface 703 can communicate with each other through an internal interface.
[0138] The processor 702 may be a central processing unit (CPU), or an application specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of the present application.
[0139] This embodiment also provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the above-mentioned natural language to structured query generation method is implemented.
[0140] The embodiment of the present application also provides a computer program product, including a computer program, which, when executed by a processor, implements the natural language to structured query generation method provided in the embodiment of the present application.
[0141] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present application. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or N embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine the different embodiments or examples described in this specification and the features of the different embodiments or examples, without contradiction.
[0142] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include at least one of the features. In the description of this application, "N" means at least two, such as two, three, etc., unless otherwise clearly and specifically defined.
[0143] Any process or method description in a flowchart or otherwise described herein may be understood to represent a module, fragment or portion of code comprising one or N executable instructions for implementing the steps of a custom logical function or process, and the scope of the preferred embodiments of the present application includes alternative implementations in which functions may not be performed in the order shown or discussed, including performing functions in a substantially simultaneous manner or in reverse order depending on the functions involved, which should be understood by technicians in the technical field to which the embodiments of the present application belong.
[0144] The logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as an ordered list of executable instructions for implementing logical functions, and can be embodied in any computer-readable medium for use by an instruction execution system, device or apparatus (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or apparatus and execute instructions), or in combination with these instruction execution systems, devices or apparatuses. For the purpose of this specification, "computer-readable medium" can be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, device or apparatus, or in combination with these instruction execution systems, devices or apparatuses. More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection with one or N wirings (electronic devices), a portable computer disk box (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and programmable read-only memory (EPROM or flash memory), a fiber optic device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be paper or other suitable medium on which the program is printed, since the program may be obtained electronically by optically scanning the paper or other medium and then editing, interpreting or processing in other suitable ways as necessary and then storing it in a computer memory.
[0145] It should be understood that the various parts of the present application can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiment, the N steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.
[0146] A person skilled in the art may understand that all or part of the steps in the method for implementing the above-mentioned embodiment may be completed by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, which, when executed, includes one or a combination of the steps of the method embodiment.
[0147] In addition, each functional unit in each embodiment of the present application may be integrated into a processing module, or each unit may exist physically separately, or two or more units may be integrated into one module. The above-mentioned integrated module may be implemented in the form of hardware or in the form of a software functional module. If the integrated module is implemented in the form of a software functional module and sold or used as an independent product, it may also be stored in a computer-readable storage medium.
[0148] The storage medium mentioned above may be a read-only memory, a magnetic disk or an optical disk, etc. Although the embodiments of the present application have been shown and described above, it can be understood that the above embodiments are exemplary and cannot be understood as limiting the present application. A person of ordinary skill in the art may change, modify, replace and modify the above embodiments within the scope of the present application.
Claims
1. A method for generating a structured query from a natural language, characterized in that: Applied to the model building stage, wherein the method comprises the following steps: Perform structured fine-grained decomposition on the manually annotated sample dataset to obtain the structured fine-grained decomposition process under each query mode; constructing an automatic training set based on natural language sample questions and the structured fine-grained decomposition process; The target basic model is adjusted by using the automatic training set to obtain a corresponding query model, so as to use the query model to convert natural language questions into a structured query language with structured fine-grained decomposition.
2. The method according to claim 1, characterized in that Before the structured fine-grained decomposition of the manually annotated sample dataset, it also includes: Determine keywords of the natural language question, wherein the keywords include selection content, condition content, grouping and sorting content, and table connection; A structured fine-grained decomposition is defined based on the keywords.
3. The method according to claim 2, characterized in that Before performing structured fine-grained decomposition on the manually annotated sample data set to obtain the structured fine-grained decomposition process under each query mode, the following steps are also included: Obtaining multiple query patterns based on keywords included in natural language questions; The keywords in the manually annotated sample data are analyzed to determine corresponding query patterns.
4. The method according to claim 1, characterized in that: The automatic training set is constructed based on the natural language sample question and the structured fine-grained decomposition process, including: Obtain the dataset to be labeled in the natural language sample problem; Extracting keywords from the to-be-annotated data set, and determining corresponding query patterns based on the keywords; Combining the query pattern, database related information and the structured fine-grained decomposition process to obtain corresponding prompts; A thought chain annotation result is obtained based on the prompt, so as to construct the automatic training set using the thought chain annotation result.
5. A method for generating a structured query from a natural language, characterized in that: Applied to the model application stage, wherein the method comprises the following steps: Get the user questions to be queried; The user question is input into a pre-built query model, and the user question is converted into a structured query language with structured fine-grained decomposition, so as to use the structured query language to query the answer to the question in the database, wherein the query model is obtained from a basic model and an automatic training set.
6. A natural language to structured query generation device, characterized in that: Applied to the model building stage, wherein the device comprises: A decomposition module is used to perform structured fine-grained decomposition on the manually annotated sample data set to obtain a structured fine-grained decomposition process under each query mode; A construction module, used for constructing an automatic training set based on natural language sample questions and the structured fine-grained decomposition process; The adjustment module is used to adjust the target basic model using the automatic training set to obtain a corresponding query model, so as to use the query model to convert natural language questions into structured query language with structured fine-grained decomposition.
7. A natural language to structured query generation device, characterized in that: Applied to the model application stage, wherein the device comprises: The acquisition module is used to obtain the user questions to be queried; A query module is used to input the user question into a pre-built query model, convert the user question into a structured query language with structured fine-grained decomposition, so as to use the structured query language to query the answer to the question in the database, wherein the query model is obtained from a basic model and an automatic training set.
8. An electronic device, characterized in that: include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the method for generating structured queries from natural language as claimed in any one of claims 1 to 4 or 5.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: The program is executed by a processor to implement the method for generating a structured query from a natural language as claimed in any one of claims 1 to 4 or 5.
10. A computer program product, characterized in that It comprises a computer program, which, when executed, is used to implement the method for generating a structured query from a natural language as claimed in any one of claims 1 to 4 or 5.
Citation Information
Patent Citations
Chinese natural language interrogative sentence semantization knowledge base automatic question-answering method
CN105701253A
Method and system for converting natural language into structured query language
CN111651474A
Method and device for converting natural language into database statement, equipment and medium
CN117370378A
Structured query language query execution using natural language and related techniques
US11954102B1
System and Method for Universal Translating From Natural Language Questions to Structured Queries
US20130226846A1