Natural language to structured query generation method and device, electronic equipment and medium

By performing structured fine-grained decomposition of manually annotated sample data sets and constructing automatic training sets, a structured query language with structured fine-grained decomposition is generated, which solves the problem of complex SQL structural characteristics and fine-grained correspondence not being considered in existing technologies, and achieves higher robustness and interpretability.

CN119848075BActive Publication Date: 2025-10-17TSINGHUA UNIVERSITY
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411600102.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-11
Publication Date
2025-10-17
Estimated Expiration
2044-11-11

AI Technical Summary

Technical Problem

The existing technology does not consider the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language questions, resulting in poor robustness and difficulty in effectively dealing with complex natural language questions.

Method used

By performing structured fine-grained decomposition on the manually annotated sample dataset, constructing an automatic training set and adjusting the target basic model, a structured query language of structured fine-grained decomposition is generated. The decomposition process is divided into multiple sub-steps, each step generates fine-grained query fragments, and finally combines them into the final query.

Benefits of technology

It improves the interpretability and accuracy of the generation process, can better handle complex natural language problems, and reduces the difficulty of queries.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119848075B_ABST
    Figure CN119848075B_ABST
Patent Text Reader

Abstract

The application relates to a natural language to structured query generation method and device, electronic equipment and medium, wherein the method comprises: performing structured fine-grained decomposition on an artificially annotated sample data set to obtain a structured fine-grained decomposition process under each query mode; constructing an automatic training set based on a natural language sample question and the structured fine-grained decomposition process; adjusting a target basic model by using the automatic training set to obtain a corresponding query model, so as to convert a natural language question into a structured query language of structured fine-grained decomposition by using the query model. Thus, the technical problem that, in the related art, the structural characteristics of a complex SQL and the fine-grained correspondence between a SQL segment and a natural language question are not considered, thereby affecting the query result, the robustness is poor, and complex natural language questions cannot be effectively dealt with is solved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of data analysis, and particularly relates to a natural language to structured query generation method and device, electronic equipment and medium. BACKGROUND

[0002] Structured databases are gradually becoming a common carrier for storing structured data. The natural language to structured query generation task (Text-to-SQL) aims to convert natural language questions into machine executable SQL (Structured Query Language). Although skilled professionals can efficiently access these data through structured query language, a natural language interface can greatly reduce the threshold of data access to help more non-technical users access data. Natural language to structured query generation is the basis for data analysis on 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 non-technical business analysts more easily access and analyze this data. In the manufacturing industry, companies often 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 more conveniently analyze this data. Therefore, the natural language to structured query generation task has attracted widespread attention from academia and industry.

[0003] In related technologies, language model-based methods have become the mainstream method for the natural language to structured query generation task. Among them, the fine-tuning-based method improves the performance of generating SQL by fine-tuning an open source small model using natural language question-SQL query pairs.

[0004] However, in related technologies, the fine-tuning-based method still directly generates corresponding SQL according to the natural language question, and cannot effectively deal with complex natural language questions, and the generation process also lacks explainability. Although the natural language question to SQL generation process can be divided into several sub-steps to complete in related technologies, the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language questions are not considered, resulting in suboptimal performance, which needs to be improved. SUMMARY

[0005] The present application provides a natural language to structured query generation method, device, electronic equipment and medium to solve the technical problem that the related art does not consider the structural characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language questions, thereby affecting the query result, having poor robustness, and being difficult to effectively deal with complex natural language questions.

[0006] The first aspect of the present application provides a natural language to structured query generation method, which is applied to a model construction stage, and 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 a natural language sample question and the structured fine-grained decomposition process; adjusting a target base model by using the automatic training set to obtain a corresponding query model, so as to convert a natural language question into a structured fine-grained decomposition structured query language by using the query model.

[0007] Optionally, in one embodiment of the present application, before performing structured fine-grained decomposition on the manually annotated sample data set, the method further includes: determining keywords of the natural language question, wherein the keywords include selected content, conditional content, grouping and sorting content, and table connection; and defining the 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 a structured fine-grained decomposition process under each query mode, the method further includes: obtaining a plurality of query modes based on keywords included in the natural language question; and analyzing the keywords in the manually annotated sample data to determine the corresponding query mode.

[0009] Optionally, in one embodiment of the present application, the method of constructing an automatic training set based on a natural language sample question and the structured fine-grained decomposition process includes: obtaining a to-be-labeled data set in the natural language sample question; extracting keywords from the to-be-labeled data set and determining the corresponding query mode based on the keywords; obtaining corresponding prompts in combination with the query mode, database related information, and the structured fine-grained decomposition process; obtaining a thought chain labeling result based on the prompts, so as to construct the automatic training set by using the thought chain labeling result.

[0010] The second aspect of the present application provides a natural language to structured query generation method, which is applied to a model application stage, and includes the following steps: obtaining a user question to be queried; inputting the user question into a pre-constructed query model to convert the user question into a structured fine-grained decomposition structured query language, so as to query a question answer in a database by using the structured query language, wherein the query model is obtained from a base 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 a model construction stage, and includes: a decomposition module configured to perform structured fine-grained decomposition on a manually annotated sample dataset to obtain a structured fine-grained decomposition process under each query mode; a construction module configured to construct an automatic training set based on a natural language sample question and the structured fine-grained decomposition process; and an adjustment module configured to adjust a target base model by using the automatic training set to obtain a corresponding query model, so that the query model is used to convert a natural language question into a structured fine-grained decomposition structured query language.

[0012] Optionally, in an embodiment of the present application, the device further includes a determination module configured to determine keywords of the natural language question, wherein the keywords include selected content, conditional content, grouping and sorting content, and table connection; and a definition module configured to define the structured fine-grained decomposition based on the keywords.

[0013] Optionally, in an embodiment of the present application, the device further includes an acquisition module configured to obtain a plurality of query modes based on keywords included in the natural language question; and an analysis module configured to analyze the keywords in the manually annotated sample data to determine corresponding query modes.

[0014] Optionally, in an embodiment of the present application, the construction module includes: an acquisition unit configured to acquire a to-be-annotated dataset in a natural language sample question; an extraction unit configured to extract keywords from the to-be-annotated dataset and determine corresponding query modes based on the keywords; a prompting unit configured to obtain corresponding prompts in combination with the query modes, database related information, and the structured fine-grained decomposition process; and a construction unit configured to obtain a thought chain annotation result based on the prompts, and construct the automatic training set by 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 a model application stage, and includes: an acquisition module configured to acquire a user question to be queried; and a query module configured to input the user question into a pre-constructed query model, convert the user question into a structured fine-grained decomposition structured query language, and query a question answer in a database by using the structured query language, wherein the query model is obtained from a base model and an automatic training set.

[0016] The fifth aspect of the present application provides an electronic device, which includes 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 natural language to structured query generation method according to the above-described embodiments.

[0017] The sixth aspect of the present application provides a computer readable storage medium storing computer instructions for causing the computer to perform the natural language to structured query generation method according to the above embodiments.

[0018] The seventh aspect of the present application provides a computer program product comprising a computer program, which, when executed, implements the natural language to structured query generation method as above.

[0019] The embodiments of the present application can perform structured fine-grained decomposition on the artificially annotated sample dataset to obtain a structured fine-grained decomposition process under each query mode, construct an automatic training set according to the natural language sample question and the structured fine-grained decomposition process, adjust a target base model to obtain a corresponding query model, and thus realize the process of converting the natural language question into the structured fine-grained decomposition structured query language 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 segment, and finally the final structured query is obtained by combining the segments of each sub-step. The structured fine-grained decomposition reduces the difficulty of complex natural questions, and the generation process has good interpretability. Thus, the technical problem that the related art does not consider the structural characteristics of complex SQL and the fine-grained correspondence between the SQL segment and the natural language question, thereby affecting the query result, having poor robustness, and being difficult to effectively cope with complex natural language questions is solved.

[0020] Additional aspects and advantages of the present application will be in part apparent and in part pointed out hereinafter. BRIEF DESCRIPTION OF DRAWINGS

[0021] The above and / or additional aspects and advantages of the present application will become apparent and be readily appreciated from the following description, taken in conjunction with the accompanying drawings, in which:

[0022] Figure 1 A flowchart of a natural language to structured query generation method according to an embodiment of the present application;

[0023] Figure 2 A flowchart of a natural language to structured query generation method according to an embodiment of the present application;

[0024] Figure 3 An example diagram of structured fine-grained decomposition according to an embodiment of the present application;

[0025] Figure 4 A structural schematic diagram of a natural language to structured query generation apparatus according to an embodiment of the present application;

[0026] Figure 5 A flowchart of another natural language to structured query generation method according to an embodiment of the present application is provided.

[0027] Figure 6 A structural schematic diagram of another natural language to structured query generation apparatus according to an embodiment of the present application is provided.

[0028] Figure 7 A structural schematic diagram of an electronic device according to an embodiment of the present application is provided. DETAILED DESCRIPTION

[0029] Embodiments of the present application are described in detail below with reference to the accompanying drawings, in which the same or similar components have the same or similar designations and functions throughout. The embodiments described below are exemplary and are intended to explain the present application, and are not to be understood as limiting the present application.

[0030] The natural language to structured query generation method, apparatus, electronic device and medium of the embodiments of the present application are described below with reference to the accompanying drawings. In view of the technical problems in the related art mentioned above, the structure characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not considered, which affects the query result, has poor robustness, and is difficult to effectively deal with complex natural language problems. The present application provides a natural language to structured query generation method, in which the artificial annotated sample data set is structured and fine-grainedly decomposed to obtain a structured fine-grained decomposition process under each query mode. An automatic training set is constructed according to the natural language sample problem and the structured fine-grained decomposition process to adjust a target base model to obtain a corresponding query model. Thus, the process of converting a natural language problem into a structured fine-grained decomposition structured query language by using the query model is realized. The natural language to structured query generation process is divided into structured sub-steps, each sub-step generates a fine-grained structured query fragment, and finally the final structured query is obtained by combining the fragments of each sub-step. The structured fine-grained decomposition reduces the difficulty of complex natural language problems, and the generation process has good interpretability. Thus, the technical problems in the related art that the structure characteristics of complex SQL and the fine-grained correspondence between SQL fragments and natural language problems are not considered, which affects the query result, has poor robustness, and is difficult to effectively deal with complex natural language problems are solved.

[0031] Specifically, Figure 1 A flowchart of a natural language to structured query generation method according to an embodiment of the present application is provided.

[0032] As Figure 1As shown, the natural language to structured query generation method is applied to a model construction phase, and the method includes the following steps:

[0033] In step S101, the artificially annotated sample data set is subjected to structured fine-grained decomposition to obtain a structured fine-grained decomposition process under each query mode.

[0034] It can be understood that the core idea of structured fine-grained decomposition is to convert the conversion process of natural language problems to structured queries into a structured fine-grained matching problem. First, the structure is used to utilize the relatively structured characteristics of SQL syntax, and the entire decomposition process is structured and decomposed into steps. Secondly, each step further includes fine-grained decomposition steps, and each segment of the step is generated in turn, such as generating three condition segments in the condition content in turn.

[0035] In actual execution, the embodiment of the present application can determine the query mode corresponding to each sample before the artificially annotated sample data set is subjected to structured fine-grained decomposition, thereby obtaining a structured fine-grained decomposition process under each query mode.

[0036] Optionally, in an embodiment of the present application, before the artificially annotated sample data set is subjected to structured fine-grained decomposition, it further includes: determining the keywords of the natural language problem, wherein the keywords include selection content, condition content, grouping and ordering content, and table connection; and defining structured fine-grained decomposition based on the keywords.

[0037] In some embodiments, the embodiment of the present application can determine the keywords of the natural language problem: determine the selection content SELECT, determine the condition content WHERE, determine the grouping GROUP and ordering content ORDER, determine the table connection JOIN, and synthesize the final SQL based on the above keywords.

[0038] In each keyword determination, the embodiment of the present application further includes fine-grained decomposition steps, such as determining the condition content part, which can be divided into determining the related segment in the natural language problem, determining the table, and determining the column and value of the condition. Note that the condition content may include multiple conditions, and even nested query statements, so that each step may further recursively include three sub-steps.

[0039] Optionally, in an embodiment of the present application, before the artificially annotated sample data set is subjected to structured fine-grained decomposition to obtain a structured fine-grained decomposition process under each query mode, it further includes: obtaining multiple query modes based on the keywords included in the natural language problem; and analyzing the keywords in the artificially 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 artificially annotate the structured fine-grained decomposition process corresponding to the problem.

[0041] The embodiment of the application can define the category and number of SQL keywords contained in the SQL query as a query pattern. The structured fine-grained decomposition process of different query patterns is slightly different because of the different keywords contained. The most common query patterns are determined by the keyword patterns of the SQL queries 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 the natural language sample question and the structured fine-grained decomposition process.

[0043] Further, the sample annotated in the manually annotated sample data set can be used as a few-shot example in the prompt of this step. The embodiment of the application can give the natural language question, the database related information, the SQL, the few-shot example, and obtain the structured fine-grained decomposition thought chain data corresponding to the natural language question by prompting the open source LLM or closed source LLM. And the embodiment of the application can use different LLMs to generate thought chains for the same question to enhance the diversity of the thought chain data. The thought chain of the embodiment of the application is completed in the form of code, so the correctness of the thought chain can be automatically verified by executing the code, and incorrect thought chain data can be effectively filtered.

[0044] Optionally, in an embodiment of the application, the automatic training set is constructed based on the natural language sample question and the structured fine-grained decomposition process, comprising: obtaining a to-be-labeled data set in the natural language sample question; extracting keywords from the to-be-labeled data set, and determining the corresponding query pattern based on the keywords; obtaining the corresponding prompt by combining the query pattern, the database related information and the structured fine-grained decomposition process; obtaining the thought chain annotation result based on the prompt, so as to construct the automatic training set by using the thought chain annotation result.

[0045] Specifically, for the to-be-labeled data set and the structured fine-grained decomposition examples of the m common patterns that have been labeled:

[0046]

[0047] Wherein, q i is the user question, a i is the SQL query, DB i is the database related information, f j is the manually annotated structured fine-grained decomposition process corresponding to the mode M j . First, the mode corresponding to the question is determined according to the SQL keyword in a i , and then the prompt is obtained according to the predefined connection template:

[0048] M j = mode(ai )

[0049] p i = template(q i , DB i , f j )

[0050] where p i is the prompt containing the natural language question, database related information, and examples. The Mode function determines the specific mode by judging the keywords in a i . The Template function fills the three pieces of information into a string template to obtain 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}, and structured fine-grained decomposition examples {f j}, please refer to the examples to give the structured fine-grained decomposition process of the natural language question to obtain the final SQL.

[0052] Finally, the prompt is used to request the LLM to obtain the thought chain annotation result c i :

[0053] c i = LLM(p i )

[0054] An enhanced data set with fine-grained decomposition thought chains is obtained by annotating all questions. The LLM adopts GPT-3.5 and can also adopt other LLMs such as GPT-4o-mini.

[0055] The thought chain designed in the embodiments of the present application is completed in the form of code, so the correctness of the thought chain can be automatically verified. Specifically, the generated thought chain is executable Python code, and the SQL statement can be generated by executing the code. The query result can be obtained by executing the SQL statement, and the correctness of the code thought chain can be automatically determined by comparing the query result with the standard result. Therefore, the code thought chain format adopted in the embodiments of the present application can effectively filter incorrect code thought chains.

[0056] In step S103, the target base model is adjusted by using the automatic training set to obtain a corresponding query model, so as to convert the natural language question into a structured fine-grained decomposition structured query language by using the query model.

[0057] As a possible implementation manner, the data fine-tuning model with a fine-grained thought chain decomposition process can make the model have the ability to generate a structured fine-grained decomposition process. The cross-entropy can be used as a loss function to train the model on the constructed training set. The purpose 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 a base model. Since the code model has more advantages in the data query task, the DeepSeek-Coder series model with strong code capability can be used as a base model. These base 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 i and the corresponding thought chain output c i , the model uses the following cross-entropy as the loss function:

[0058]

[0059] In combination with Figure 2 and Figure 3 , the working principle of the natural language to structured query generation method of the embodiment of the present application is described in detail.

[0060] In actual execution process, the Text-to-SQL task can be defined as: input a natural language question q and database related information DB, the model M needs to output the structured fine-grained decomposition steps corresponding to the question q and the final SQL, that is, c = M(q, DB). The model M and the application process of the model M can be as shown in Figure 2 The embodiment of the present application can include the following steps:

[0061] Step S201: structured fine-grained decomposition definition.

[0062] The core idea of structured fine-grained decomposition is to convert the conversion process from natural language question to structured query into a structured fine-grained matching problem. First, the structure is used to structure the entire decomposition process into five steps: determine the SELECT content, determine the WHERE content, determine the GROUP and ORDER content, determine the JOIN content, and finally combine the contents of the above steps to synthesize the final SQL, as shown in Figure 3 It can be seen that the final SQL query is obtained by combining the variables of the first four parts according to the syntax.

[0063] Secondly, each step contains a fine-grained decomposition step, such as determining the conditional content part, which can be divided into determining the relevant fragment in the natural language question, determining the table, determining the column and value of the condition. Note that the conditional content may contain multiple conditions, and even there are nested query statements, so each step may recursively contain three sub-steps. Figure 3 An example of fine-grained decomposition is given, such as the second step of determining the conditional content, which determines two WHERE conditions in turn.

[0064] Step S202: Structured fine-grained decomposition example annotation.

[0065] The number of categories containing SQL keywords in the SQL query is defined as the query pattern in the embodiment of the application. The structured fine-grained decomposition process of different query patterns is slightly different because of the different keywords contained. The query containing a SELECT, WHERE, JOIN is the simplest query pattern, and the complex query may contain multiple SELECT or WHERE keywords. By counting the keyword patterns of SQL queries in the data, the most common query patterns are determined, and a structured fine-grained decomposition example is manually annotated for each common pattern. These examples will serve as a few-shot example for automatic training set construction of LLM. The embodiment of the application determines 6 common query patterns, and annotates an example for each pattern. The specific 6 patterns are as follows:

[0066] 1. Contains GROUP and HAVING keywords;

[0067] 2. Contains GROUP keyword;

[0068] 3. Contains WHERE keyword, WHERE condition contains sub-clause;

[0069] 4. Contains WHERE keyword, multiple WHERE conditions, JOIN keyword;

[0070] 5. Contains WHERE keyword, multiple WHERE conditions, ORDER keyword;

[0071] 6. Contains SELECT keyword.

[0072] Step S203: Automatic training set construction based on few-shot prompting.

[0073] Given the natural language question, database related information, SQL, the structured fine-grained decomposition thought chain data corresponding to the user question is obtained by prompting the open source LLM or closed source LLM. And use different LLM to generate thought chain for the same question to enhance the diversity of thought chain data.

[0074] Specifically, for the to-be-annotated data set and the annotated m common pattern structured fine-grained decomposition examples:

[0075]

[0076] where q i is the user question, a i is the SQL query, DB i is the database related information, and f j is the schema M j The corresponding human-annotated structured fine-grained decomposition process. First, determine the schema corresponding to the question according to the SQL keywords in a i , and then get the prompt according to the predefined connection template:

[0077] M j = mode(a i )

[0078] p i = template(q i , DB i , f j )

[0079] where p i is the prompt containing the natural language question, database related information, and examples. The mode function determines the specific schema by judging the keywords in a i . The template function fills in 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}, and structured fine-grained decomposition examples {f j}, please refer to the examples to give the structured fine-grained decomposition process of the natural language question to get the final SQL.

[0081] Finally, according to the prompt, request LLM to get the thought chain annotation result c i :

[0082] c i = LLM(p i )

[0083] Annotate all questions to get an enhanced dataset with fine-grained decomposition thought chains LLM uses GPT-3.5 to better annotate code thought chains, and other LLMs such as GPT-4o-mini can also be used.

[0084] The thought chain designed in the embodiment of the application is completed in the form of code, and therefore the correctness of the thought chain can be automatically verified. Specifically, the generated thought chain is executable Python code, and the SQL statement can be generated by executing the code, and the query result can be obtained by executing the SQL statement, and the correctness of the code thought chain can be automatically determined by comparing the query result with the standard result. Therefore, the code thought chain format adopted in the embodiment of the application can effectively filter incorrect code thought chains.

[0085] Step S204: model training.

[0086] The data fine-tuning model with the fine-grained thought chain decomposition process used in the embodiment of the application can enable the model to generate a structured fine-grained decomposition process. The embodiment of the application can use cross-entropy as a loss function to train the model on the constructed training set. The goal of the embodiment of the application is to build an open-source Text-to-SQL system, and therefore an open-source model is used as a base model. Since the code model has more advantages in the data query task, the embodiment of the application can use the open-source DeepSeek-Coder series model with strong code capability as a base model. These base models all use a structure similar to the Transformer decoder, mainly including multiple stacked self-attention layers, each of which mainly includes a multi-head attention layer and a feedforward layer. Given a prompt input p i and the corresponding thought chain output c i , the model uses the following cross-entropy as a 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 question, the model can automatically generate a structured fine-grained decomposition SQL segment, and finally combine each segment to generate the final SQL. In this way, the generated SQL has higher accuracy and the generation process has stronger explainability.

[0090] Further, the embodiment of the application can use BIRD to verify the beneficial effects.

[0091] BIRD is a commonly used benchmark data set for Text-to-SQL, and the data set is divided into a training set, a development set and a test set. Each sample contains a natural language question, database related information, a database and SQL. The embodiment of the application can obtain a model capable of generating a structured fine-grained decomposition process according to steps S201, S202 and S203, and evaluate and test the model on the development set.

[0092] The experimental results show that the method described in the embodiments of the application exceeds the method of directly generating a structured query according to a natural language question, and the improvement on a complex natural language question is more significant. Meanwhile, the embodiments of the application can generate a decomposition process, and thus have better interpretability.

[0093] To sum up, the natural language to structured query generation method in the embodiments of the application can be implemented based on structured fine-grained decomposition, and the structured fine-grained decomposition data construction of the few-shot prompting is implemented by defining the structured fine-grained decomposition process.

[0094] According to the natural language to structured query generation method proposed in the embodiments of the application, the artificial annotated sample dataset can be subjected to structured fine-grained decomposition to obtain the structured fine-grained decomposition process under each query mode, the automatic training set is constructed according to the natural language sample question and the structured fine-grained decomposition process, the target base model is adjusted to obtain the corresponding query model, and thus the process of converting the natural language question into the structured fine-grained decomposition structured query language by using the query model is implemented, the natural language to structured query generation process is divided into structured sub-steps, each sub-step generates a fine-grained structured query segment, and finally the final structured query is obtained by combining the segments of each sub-step. The difficulty of a complex natural question is reduced by the structured fine-grained decomposition, and the generation process has good interpretability. Thus, the technical problem that the structure characteristics of a complex SQL and the fine-grained correspondence between the SQL segment and the natural language question are not considered in the related art, thereby affecting the query result, the robustness is poor, and the complex natural language question cannot be effectively dealt with is solved.

[0095] Next, the natural language to structured query generation device proposed in the embodiments of the application is described with reference to the accompanying drawings.

[0096] Figure 4 is a block schematic diagram of the natural language to structured query generation device in the embodiments of the application.

[0097] As shown in Figure 4 , the natural language to structured query generation device 10 is applied to a model construction 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 configured to perform structured fine-grained decomposition on an artificial annotated sample dataset to obtain a structured fine-grained decomposition process under each query mode.

[0099] The construction module 102 is configured to construct an automatic training set based on a natural language sample question and the structured fine-grained decomposition process.

[0100] The adjusting module 103 is configured to adjust the target base model by using the automatic training set to obtain a corresponding query model, so as to convert the natural language question into a structured query language with structured fine-grained decomposition by using the query model.

[0101] Optionally, in an embodiment of the present application, the natural language to structured query generation apparatus 10 further comprises a determining module and a defining module.

[0102] The determining module is configured to determine the keywords of the natural language question, wherein the keywords include selected content, conditional content, grouping and sorting content, and table connection.

[0103] The defining module is configured to define the structured fine-grained decomposition based on the keywords.

[0104] Optionally, in an embodiment of the present application, the natural language to structured query generation apparatus 10 further comprises an obtaining module and an analyzing module.

[0105] The obtaining module is configured to obtain multiple query modes based on the keywords included in the natural language question.

[0106] The analyzing module is configured to analyze the keywords in the artificially labeled sample data to determine the corresponding query mode.

[0107] Optionally, in an embodiment of the present application, the constructing module 102 comprises an obtaining unit, an extracting unit, a prompting unit, and a constructing unit.

[0108] The obtaining unit is configured to obtain the to-be-labeled data set in the natural language sample question.

[0109] The extracting unit is configured to extract the keywords from the to-be-labeled data set and determine the corresponding query mode based on the keywords.

[0110] The prompting unit is configured to obtain the corresponding prompt in combination with the query mode, the database related information, and the structured fine-grained decomposition process.

[0111] The constructing unit is configured to obtain the thought chain labeling result based on the prompt, and to construct the automatic training set by using the thought chain labeling result.

[0112] It should be noted that the foregoing explanation and description of the embodiment of the natural language to structured query generation method also apply to the natural language to structured query generation apparatus of this embodiment, which will not be described here again.

[0113] According to the natural language to structured query generation device provided in the embodiment of the present application, the artificial annotation sample data set can be subjected to structured fine-grained decomposition to obtain a structured fine-grained decomposition process under each query mode, an automatic training set is constructed according to the natural language sample question and the structured fine-grained decomposition process, the target base model is adjusted to obtain a corresponding query model, so that the process of converting the natural language question into the structured fine-grained decomposition structured query language by using the query model is realized, and the natural language to structured query generation process is divided into structured sub-steps, each sub-step generates a fine-grained structured query segment, and finally the final structured query is obtained by combining the segments of each sub-step. The structured fine-grained decomposition reduces the difficulty of complex natural questions, and the generation process has good interpretability. Thus, the technical problem that the structure characteristics of complex SQL and the fine-grained correspondence between the SQL segment and the natural language question are not considered in the related art, thereby affecting the query result, the robustness is poor, and the complex natural language question cannot be effectively dealt with is solved.

[0114] The above is an explanation and description of the model construction stage of the embodiment of the present application. The model application stage of the embodiment of the present application is described below.

[0115] Specifically, Figure 5 A flowchart of a natural language to structured query generation method provided by the embodiment of the present application is shown.

[0116] As Figure 5 shown, the natural language to structured query generation method is applied to the model application stage, and the method includes the following steps:

[0117] In step S501, a user question to be queried is obtained.

[0118] In step S502, the user question is input into a pre-constructed query model, and the user question is converted into a structured fine-grained decomposition structured query language, so as to query the question answer in the database by using the structured query language, wherein the query model is obtained from a base model and an automatic training set.

[0119] In the actual execution process, the query model model pre-constructed and trained can be used to process new questions.

[0120] The user can give a new natural language question, and the query model can automatically generate a structured fine-grained decomposition SQL segment, and finally combine the segments to generate a final SQL. In this way, the accuracy of generating SQL is higher, and the interpretability of the generation process is stronger.

[0121] According to the natural language to structured query generation method provided in the embodiments of the present application, the artificial annotation sample dataset can be subjected to structured fine-grained decomposition to obtain a structured fine-grained decomposition process under each query mode, an automatic training set is constructed according to the natural language sample question and the structured fine-grained decomposition process to adjust a target base model to obtain a corresponding query model, so that the process of converting the natural language question into the structured fine-grained decomposition structured query language by using the query model is realized, and the natural language to structured query generation process is divided into structured sub-steps, each sub-step generates a fine-grained structured query segment, and finally the final structured query is obtained by combining the segments of each sub-step. The structured fine-grained decomposition reduces the difficulty of complex natural questions, and the generation process has good interpretability. Thus, the technical problem that the structure characteristics of complex SQL and the fine-grained correspondence between the SQL segment and the natural language question are not considered in the related art, thereby affecting the query result, the robustness is poor, and the complex natural language question cannot be effectively dealt with is solved.

[0122] Secondly, the natural language to structured query generation device provided in the embodiments of the present application is described with reference to the accompanying drawings.

[0123] Figure 6 FIG. 1 is a block schematic diagram of the natural language to structured query generation device in the embodiments of the present application.

[0124] As shown in FIG. 2, the natural language to structured query generation device 20 is applied to a model application stage, wherein the device 20 comprises an acquisition module 201 and a query module 202. Figure 6

[0125] Specifically, the acquisition module 201 is configured to acquire a user question to be queried.

[0126] The query module 202 is configured to input the user question into a pre-constructed query model to convert the user question into a structured fine-grained decomposition structured query language, so as to query the question answer in the database by using the structured query language, wherein the query model is obtained by a base model and an automatic training set.

[0127] It should be noted that the foregoing explanation and description of the natural language to structured query generation method embodiments are also applicable to the natural language to structured query generation device of the embodiments, which will not be described herein again.

[0128] ​According to the natural language to structured query generation device provided in the embodiments of the present application, the artificial annotation sample data set can be structured and finely granularly decomposed to obtain a structured fine-granular decomposition process under each query mode, an automatic training set is constructed according to the natural language sample question and the structured fine-granular decomposition process, the target base model is adjusted to obtain a corresponding query model, and thus the process of converting the natural language question into the structured fine-granular decomposition structured query language by using the query model is realized, the natural language to structured query generation process is divided into structured sub-steps, each sub-step generates a fine-granular structured query segment, and finally the final structured query is obtained by combining the segments of each sub-step. The structured fine-granular decomposition reduces the difficulty of the complex natural question, and the generation process has good interpretability. Thus, the technical problem that the related art does not consider the structural characteristics of the complex SQL and the fine-granular correspondence between the SQL segment and the natural language question, thereby affecting the query result, having poor robustness, and being difficult to effectively cope with the complex natural language question is solved.

[0129] Figure 7 The electronic device provided in the embodiments of the present application is shown in a structural schematic diagram. The electronic device can include:

[0130] The memory 701, the processor 702, and the computer program stored in the memory 701 and executable on the processor 702.

[0131] The processor 702 implements the natural language to structured query generation method provided in the above embodiments when executing the program.

[0132] Further, the electronic device further includes:

[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 the computer program executable on the processor 702.

[0135] The memory 701 can include a high-speed RAM memory, and can also include a 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 with each other through a bus and complete communication between 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, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For convenience of representation, Figure 7 Only one thick line is used to represent the bus in the figure, but it 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 complete communication between each other through an internal interface.

[0138] The processor 702 can be a Central Processing Unit (CPU), or an Application Specific Integrated Circuit (ASIC), or one or more integrated circuits configured to implement one or more embodiments of the present application.

[0139] The embodiment further provides a computer readable storage medium, having stored thereon a computer program, which, when executed by a processor, implements the natural language to structured query generation method as above.

[0140] The embodiment of the present application further provides a computer program product, comprising a computer program, which, when executed by a processor, implements the natural language to structured query generation method provided by the embodiment of the present application.

[0141] In the description of the application, reference to "one embodiment", "some embodiments", "an example", "a specific example", or "some examples" means that a particular feature, structure, material, or characteristic being described is included in at least one embodiment or example of the application. The appearances of the phrase in various places in the specification are not necessarily all referring to the same embodiment or example. Furthermore, the described specific features, structures, materials, or characteristics can be combined in any suitable manner in one or more embodiments or examples. In addition, the usage of "N" means at least two, for example, two, three or the like, unless explicitly stated otherwise.

[0142] Furthermore, the terms "first", "second", or the like, are used merely as a designation of certain elements or features of the application, and do not imply or connote relative importance or a specific order of precedence. Thus, features defined with "first", "second", etc. can include at least one of the features, either explicitly or implicitly.

[0143] Any process or method descriptions or blocks in flow charts or otherwise described herein represent embodiments of modules, segments, or portions of code which include one or more executable instructions for implementing specific logic functions or steps, and alternate implementations are possible. In some embodiments, the processes or methods described in flow charts or otherwise described herein are not necessarily performed in the order shown or discussed, including, for example, performing or depending from other operations or stages, in parallel, in reverse order, or in a variety of orders.

[0144] The logic and / or steps represented in the flowcharts or otherwise described herein, for example, can be considered as a sequenced list of executable instructions for implementing the logical functions, and can be embodied in any computer-readable medium for use by, or in conjunction with, an instruction execution system, apparatus, or device (e.g., a computer-based system, a system including a processor, or other system that can fetch and execute instructions from an instruction execution system, apparatus, or device). For purposes of this specification, a "computer-readable medium" can be any device that can contain, store, communicate, propagate, or transport a program for use by, or in conjunction with, an instruction execution system, apparatus, or device. More specific examples (a non-exhaustive list) of computer-readable media include the following: an electrical connection with one or N wires (electronic devices), a portable computer disk cartridge (magnetic device), random access memory (RAM), read-only memory (ROM), erasable and programmable read-only memory (EPROM or flash memory), fiber optic devices, and a portable compact disc 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 can be obtained electronically by optically scanning the paper or other medium and then editing, interpreting or processing it in other suitable ways as necessary, and then storing it in a computer memory.

[0145] It should be understood that various parts of the present application can be implemented using hardware, software, firmware, or a combination thereof. In the above embodiment, the N steps or methods can be implemented using software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented using hardware, as in another embodiment, any one of the following technologies known in the art or a combination thereof can be used to implement: a discrete logic circuit having a logic gate circuit for implementing a logic function on a data signal, an application-specific integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.

[0146] Those skilled in the art will understand that all or part of the steps in the method of the above embodiment can be completed by instructing related hardware through a program, and the program can be stored in a computer-readable storage medium. When the program is executed, it includes one or a combination of the steps of the method embodiment.

[0147] In addition, each of the functional units in the various embodiments of the present application can be integrated in one processing module, or each of the units can be physically present separately, or two or more units can be integrated in one module. The integrated module can be realized in the form of hardware or in the form of a software functional module. When the integrated module is realized in the form of a software functional module and sold or used as an independent product, it can also be stored in a computer readable storage medium.

[0148] The storage medium mentioned above can 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 should be understood that the above embodiments are exemplary and should not be construed as limiting the present application, and those skilled in the art can make changes, modifications, replacements and variations to the above embodiments within the scope of the present application.

Claims

1. A method for generating structured queries from 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; Using the automatic training set to adjust the target basic model, a corresponding query model is obtained, and the query model is used to convert the natural language question into a structured query language with structured fine-grained decomposition; The structured fine-grained decomposition includes: determining the selection content SELECT, determining the condition content WHERE, determining the grouping GROUP and sorting content ORDER, and determining the table connection JOIN; Before performing structured fine-grained decomposition on the manually annotated sample data set, the method further includes: determining keywords of natural language questions, wherein the keywords include selection content, condition content, grouping and sorting content, and table connection; defining structured fine-grained decomposition based on the keywords; Before performing structured fine-grained decomposition on the manually annotated sample data set to obtain a structured fine-grained decomposition process under each query mode, the method further 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; Among them, the construction of an automatic training set based on the 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 obtained by user input, 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.

2. A method for generating structured queries from natural language, characterized in that: Applied to the model application stage as claimed in claim 1, 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 the basic model and the automatic training set.

3. A natural language to structured query generation device, characterized in that: Applied to the model building stage, wherein the device includes: The decomposition module is used to perform structured fine-grained decomposition on the manually annotated sample dataset to obtain the structured fine-grained decomposition process under each query mode; A construction module, configured to construct an automatic training set based on natural language sample questions and the structured fine-grained decomposition process; An adjustment module, configured to adjust a target basic model using the automatic training set to obtain a corresponding query model, so as to convert a natural language question into a structured query language with structured fine-grained decomposition using the query model; The structured fine-grained decomposition includes: determining the selection content SELECT, determining the condition content WHERE, determining the grouping GROUP and sorting content ORDER, and determining the table connection JOIN; A determination module is used to determine keywords of natural language questions, wherein the keywords include selection content, condition content, grouping and sorting content, and table connection; a definition module is used to define structured fine-grained decomposition based on the keywords; An acquisition module is used to obtain multiple query patterns based on the keywords included in the natural language question; an analysis module is used to analyze the keywords in the manually annotated sample data to determine the corresponding query pattern; The construction module includes: an acquisition unit for acquiring a dataset to be annotated in a natural language sample question; an advance unit for extracting keywords from the dataset to be annotated and determining a corresponding query pattern based on the keywords; a prompt unit for obtaining corresponding prompts based on the query pattern, database-related information obtained by user input, and the structured fine-grained decomposition process; and a construction unit for obtaining a thought chain annotation result based on the prompt, so as to construct the automatic training set using the thought chain annotation result. The database related information is obtained through input.

4. A natural language to structured query generation device, characterized in that: The natural language to structured query generation device according to claim 3 is used in 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, 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.

5. 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 or 2.

6. 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 or 2.

7. A computer program product, characterized in that The invention comprises a computer program, which is used to implement the method for generating a structured query from a natural language when the computer program is executed as claimed in any one of claims 1 or 2.

Citation Information

Patent Citations

  • 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