Method for converting natural language text into structured query language
By employing a thought chain reasoning and debate mechanism within a dual-model collaborative framework, the low accuracy and high resource consumption of the zero-sample Text2SQL method in complex queries are addressed, achieving efficient and stable SQL generation.
Patent Information
- Application Number
- CN202511548598.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-28
- Publication Date
- 2026-01-23
AI Technical Summary
Existing zero-sample Text2SQL methods suffer from low accuracy, lack of effective error correction mechanisms, high resource consumption, and difficulty in effectively correcting inference errors when handling complex queries.
A dual-model collaborative framework is adopted, which uses the reasoning mechanism of thought chain and the debate mechanism of large language model to cross-critique and iteratively optimize two Transformer models with different architectures or parameter scales to generate and correct SQL query statements.
It significantly improves the accuracy and robustness of complex query generation, reduces resource consumption, enhances the system's fault tolerance and stability, and has good versatility and scalability.
Smart Images

Figure CN121387945A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of artificial intelligence and natural language processing technology, specifically a method for converting natural language text into structured query language. Background Technology
[0002] Translating users' natural language questions directly into executable SQL statements in a database (Text2SQL) is a key technology connecting non-technical users with massive amounts of data. Currently, zero-shot Text2SQL methods based on large language models are gaining attention due to their flexibility in not requiring additional training for specific databases. However, existing zero-shot Text2SQL techniques face the following challenges when handling complex queries:
[0003] 1. Insufficient complex reasoning ability: For queries that require multi-step reasoning, join multiple data tables, or contain complex conditions, the accuracy of SQL generated directly by the large language model drops significantly.
[0004] 2. Vulnerability of the reasoning process: Although Chain-of-Thought (CoT) technology improves performance to some extent by guiding the model to generate intermediate reasoning steps, if an error occurs at any point in the reasoning chain, the error will propagate to the final SQL statement, causing the entire query to fail. This "one wrong step leads to another" problem is difficult to avoid.
[0005] 3. Limitations of self-correction: Allowing a single large language model to "self-reflect" or "self-correct" its own erroneous reasoning is often ineffective. The model tends to stick to its initial judgment, making it difficult to break free from inherent erroneous logic, leading to failed correction.
[0006] 4. Existing debate mechanisms have high resource consumption: Although introducing multi-model debate is an effective way to solve the cognitive limitations of a single model, traditional multi-model debate frameworks usually include two "debaters" and one "judge", which requires the simultaneous use of three large language models, resulting in huge consumption of computing resources and high deployment costs.
[0007] Therefore, there is an urgent need for a new method to convert zero-sample natural language text into the structured query language Text2SQL, which can utilize deep reasoning through thought chains, effectively correct reasoning errors, and take into account resource efficiency. Summary of the Invention
[0008] The purpose of this invention is to provide a method for converting natural language text into structured query language, especially a method that improves the accuracy and robustness of generating complex database query statements in zero-shot scenarios by integrating chain-of-thought reasoning and large language model (LLM) debate mechanisms, in order to overcome the problems of low accuracy and lack of effective error correction mechanisms in existing zero-shot Text2SQL methods when dealing with complex problems.
[0009] The technical solution adopted by this invention to achieve the above objectives is: a method for converting natural language text into structured query language, comprising the following steps:
[0010] Step S1: Initialize Model A and Model B; Distribute the received user request containing natural language query and target database table structure information to Model A and Model B simultaneously; Model A and Model B independently use the CoT (Cooperative Reasoning) method to perform reasoning, and generate the initial reasoning process and initial SQL query statement respectively.
[0011] Step S2: Model A and Model B exchange their generated reasoning processes and SQL statements; Model A and Model B act as critics, reviewing each other's reasoning processes and SQL statements, and generating structured critical comments which are then sent to the other party.
[0012] Step S3: Model A and Model B receive the corresponding criticisms, and revise their reasoning process and SQL statements based on their initial reasoning to generate an optimized new version;
[0013] Step S4: After the cross-critique in step S2 and the iterative optimization in step S3, Model A and Model B output their final versions of SQL query statements, denoted as SQL-A and SQL-B, respectively.
[0014] Step S5: The system attempts to execute SQL-A and SQL-B in the target database; if the execution is successful, it returns a successful query result to the user.
[0015] Step S6: If both SQL-A and SQL-B fail to execute, the final error correction step is triggered and executed.
[0016] Both Model A and Model B are two large language models with the same functionality but different model architectures or parameter scales using the Transformer architecture. This is to introduce differentiated reasoning perspectives during cross-critique and iterative optimization, thereby enhancing the diversity of error identification.
[0017] In step S1, Model A and Model B use the CoT (Cooperation of Reasoning) approach for reasoning, and their generation process is defined by the following joint probability formula:
[0018] ;
[0019] in, This represents the input information, including natural language queries and database table structure information. This represents the intermediate inference process of model generation. This represents the final generated SQL query statement;
[0020] This formula indicates that the model is first based on the input. Generate inference chain Based on and Together, we generate the final answer. .
[0021] The initialization in step S1 specifically includes:
[0022] Model A is Based on input Generate the initial inference chain and initial SQL statement , denoted as: ;
[0023] Model B is Based on input Generate the initial inference chain and initial SQL statement , denoted as: .
[0024] Step S2 is achieved in the following manner:
[0025] In the In rounds of iteration, the model For the model The output is critiqued to generate critical opinions: ;
[0026] In the In rounds of iteration, the model For the model The output is critiqued to generate critical opinions: ;
[0027] in, and Representing the model respectively and Critical operators.
[0028] In step S2, the structured critiques are generated through a prompting process, and their review dimensions include: the correctness of the data table selection, the rationality of field associations, the appropriateness of the use of aggregate functions, and the completeness and logical correctness of the WHERE clause.
[0029] Step S3 is achieved in the following manner:
[0030] Model Revise based on previous output and received criticism: ;
[0031] Model Revise based on previous output and received criticism: ;
[0032] in, and Representing the model respectively and model The self-correcting operator.
[0033] In step S4, the final SQL statement is output, which is achieved in the following way:
[0034] After N rounds of iteration, the output model is... The final chain of reasoning and SQL statements Output Model The final chain of reasoning and SQL statements The output sequence is as follows: .
[0035] Step S5: If both SQL-A and SQL-B execute successfully, randomly select one of the query results and return it to the user; if only one of SQL-A or SQL-B executes successfully, return the successful query result to the user.
[0036] Step S6 specifically includes:
[0037] The final correction steps based on execution errors are as follows: When both SQL-A and SQL-B fail to execute, the execution error information of SQL-A is fed back to Model A and it is instructed to make corrections. The execution error information of SQL-B is fed back to Model B and it is instructed to make corrections. Model A and Model B generate corrected SQL statements based on the error information and try to execute them again until an executable SQL statement is generated or the maximum number of attempts is reached.
[0038] The present invention has the following beneficial effects and advantages:
[0039] 1. This invention significantly improves the accuracy and robustness of complex SQL statement generation and reduces the resource cost of system implementation through iterative cross-critique and self-optimization.
[0040] 2. This invention, through the combination of thought chain and multi-round debate, can deeply analyze complex query logic, effectively identify and correct errors that a single model is prone to make in the reasoning process, thereby significantly improving the accuracy of the final generated SQL.
[0041] 3. This invention designs a two-layer error correction mechanism: "cross-criteria" and "execution rollback". Cross-criteria detects and corrects logical errors before SQL is generated, while execution rollback uses real database feedback to fix errors after SQL execution, greatly enhancing the system's fault tolerance and stability.
[0042] 4. Compared with the traditional three-model architecture of "two debaters + one judge", this invention only uses two models and completes the debate by role switching, which reduces resource consumption by about one-third and is easier to deploy and apply in practice.
[0043] 5. The entire process of this invention requires no fine-tuning of the model, relying entirely on the general reasoning capabilities of the large model, enabling it to easily adapt to any new database and business scenario, and possessing excellent versatility and scalability. Attached Figure Description
[0044] Figure 1 Overall system architecture diagram of the present invention;
[0045] Figure 2 The overall flowchart of the Text2SQL method of this invention. Detailed Implementation
[0046] The present invention will now be described in further detail with reference to the accompanying drawings and embodiments.
[0047] like Figure 1 The diagram shown illustrates the overall system architecture of this invention. This invention proposes an innovative dual-model collaborative framework, which achieves the conversion from natural language to structured query language through the following steps: [Details of the steps are omitted as they are not provided in the original text.] Figure 2 As shown, it includes:
[0048] (a) Initialize parallel inference:
[0049] like Figure 1As shown, the system initializes two large language models with the same functionality (hereinafter referred to as "Model A" and "Model B"). Both Model A and Model B are two large language models with the same functionality but different model architectures or different parameter scales, which introduce differentiated reasoning perspectives during cross-critique and iterative optimization to enhance the diversity of error recognition.
[0050] The initialization process specifically involves:
[0051] Model A is Based on input Generate the initial inference chain and initial SQL statement , denoted as: ;
[0052] Model B is Based on input Generate the initial inference chain and initial SQL statement , denoted as: .
[0053] Upon receiving a user's natural language query request and the target database's table structure information (Schema), the system simultaneously distributes the request to both Model A and Model B.
[0054] Model A and Model B each independently use the Chain of Reasoning (CoT) approach to reason, analyze user intent, understand table structure, plan query steps, and generate a complete reasoning process and corresponding initial SQL query statements.
[0055] Among them, the thought chain suggestion is an advanced suggestion strategy. For complex query problems in databases, thought chain suggestions can further integrate intermediate reasoning steps to guide the reasoning process from input to output. The generation process of large-scale model thought chain reasoning can be represented by the following formula:
[0056] ;
[0057] in, This represents the input information, including natural language queries and database table structure information. This represents the intermediate inference process of model generation. This represents the final generated SQL query statement;
[0058] This formula indicates that the model is first based on the input. Generate inference chain Based on and Together, we generate the final answer. .
[0059] (II) Cross-critique and Iterative Optimization This stage is the core of this invention. This invention uses sophisticated prompt engineering to guide the large language model to act as both a "reasoner" and a "criterionist." Debate-based optimization is achieved by iteratively executing the following steps:
[0060] Step 1) Exchange and critique:
[0061] Model A submits its generated complete thought chain reasoning process and SQL statement to Model B. Simultaneously, Model B also submits its generated content to Model A.
[0062] Step 2) Generate critical comments:
[0063] During the critique phase, structured critiques are generated through prompting engineering, providing explicit instructions to the model. The review dimensions include requiring it to be checked from multiple dimensions such as "the table selection is correct", "the field association is reasonable", "the aggregate function is used appropriately", and "the WHERE condition is missing".
[0064] Each model shifts roles, becoming the "critic" of the other. Model A is responsible for reviewing Model B's reasoning process, identifying potential logical flaws, biases in understanding the business, errors in table or field selection, SQL syntax issues, etc., and generating structured critiques or suggestions for improvement. Model B, in turn, critiques Model A's solution.
[0065] Critical opinions are generated in the following ways:
[0066] In the In rounds of iteration, the model For the model The output is critiqued to generate critical opinions: ;
[0067] In the In rounds of iteration, the model For the model The output is critiqued to generate critical opinions: ;
[0068] in, and Representing the model respectively and Critical operators.
[0069] Step 3) Self-correction:
[0070] Model A receives critical feedback from Model B and, combined with its initial reasoning, reflects on and corrects its thought process and SQL statements, generating an optimized new version. Model B performs the same operation simultaneously.
[0071] The self-correction method is achieved in the following ways:
[0072] Model Revise based on previous output and received criticism: ;
[0073] Model Revise based on previous output and received criticism: ;
[0074] in, and Representing the model respectively and model The self-correcting operator.
[0075] Step 4) Iterate the above "exchange criticism-self-correction" process repeatedly for a predetermined number of N times (e.g., 2-3 times), or until the reasoning processes and SQL statements of the two models become stable or consistent. Through multiple rounds of debate, the reasoning paths of both sides are continuously improved, and potential errors are gradually eliminated.
[0076] The number of iterations N can be dynamically set according to the task complexity and response time requirements.
[0077] (III) Final Decision-Making and Implementation Backtracking Mechanism:
[0078] After multiple rounds of debate and optimization, the system enters the decision-making and execution phase:
[0079] Model A and Model B output their final versions of the SQL query statements (SQL-A and SQL-B), respectively.
[0080] The final SQL statement output is achieved in the following way:
[0081] After N rounds of iteration, the output model is... The final chain of reasoning and SQL statements Output Model The final chain of reasoning and SQL statements The output sequence is as follows: .
[0082] The system attempts to execute SQL-A and SQL-B simultaneously and records the execution results: success or failure.
[0083] Select the return result based on the execution result:
[0084] If both SQL-A and SQL-B execute successfully, the system randomly selects one of the query results and returns it to the user, thus ending the process.
[0085] If only one SQL query executes successfully, the system returns the result of that successful query to the user, and the process ends.
[0086] If both SQL-A and SQL-B fail to execute, the system captures the error information for both SQL statements and proceeds to the final correction phase based on the execution errors.
[0087] (iv) Final correction based on execution errors:
[0088] If both SQL-A and SQL-B fail to execute, the system will not terminate directly.
[0089] The system sends the execution error message of SQL-A to model A and instructs it to correct the error accordingly. Simultaneously, it sends the error message of SQL-B to model B for correction.
[0090] The model makes targeted adjustments based on specific execution errors (such as "column not found" or "syntax error"), generates a final corrected version of the SQL, and attempts to execute it again until an executable SQL is generated or the maximum number of attempts is reached.
[0091] In summary, this invention constructs an efficient, robust, and easily deployable Text2SQL system by introducing a dual-model collaborative reasoning and cross-critique mechanism, combined with a thought chain and execution feedback backoff strategy. This method not only significantly improves the quality of SQL generation in complex query scenarios but also effectively solves the problems of reasoning fragility, self-correction limitations, and high resource consumption inherent in traditional methods. This invention is entirely based on zero-shot learning, requiring no model fine-tuning for specific databases, and possesses good versatility and practicality. It provides a novel and reliable solution for natural language and database interaction, with broad application prospects and promotional value.
[0092] Those skilled in the art will understand that the above description is merely a preferred embodiment of the present invention, and the features described in the various embodiments and / or claims of this disclosure can be combined or combined in various ways, even if such combinations or combinations are not explicitly described in this disclosure. This is not intended to limit the present invention. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art can still modify the technical solutions described in the foregoing embodiments or make equivalent substitutions for some of the technical features. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
[0093] Although preferred embodiments of the invention have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including both the preferred embodiments and all changes and modifications falling within the scope of the invention. Clearly, those skilled in the art can make various alterations and modifications to the invention without departing from its spirit and scope. Thus, if these modifications and modifications of the invention fall within the scope of the claims and their equivalents, the invention is also intended to include these modifications and modifications.
Claims
1. A method of converting natural language text into structured query language, characterized by, Comprise the following steps: Step S1: initialize model A and model B; the received user request containing natural language query and target database table structure information is distributed to model A and model B at the same time; model A and model B each independently adopts the thought chain CoT mode for reasoning, and generates the initial reasoning process and initial SQL query statement respectively; Step S2: model A and model B exchange the reasoning process and SQL statement generated by them; Model A and model B respectively as critic, review the reasoning process and SQL statement of the other party, and generate structured criticism opinion and send to the other party; Step S3: model A and model B receive the corresponding criticism opinion, modify the reasoning process and SQL statement of itself combined with the initial reasoning of itself, and generate the new version of optimization; Step S4: after the cross criticism of step S2 and the iterative optimization of step S3, model A and model B output the final version of SQL query statement respectively, recorded as SQL-A and SQL-B; Step S5: the system tries to execute SQL-A and SQL-B in the target database; if the execution is successful, the successful query result is returned to the user; Step S6: if SQL-A and SQL-B are both executed unsuccessfully, the final error correction step is triggered and executed.
2. The method of claim 1, wherein, Said model A and model B are both: two Transformer architecture large language models with the same function and different model architectures or different parameter scales, in order to introduce differentiated reasoning perspective in the process of cross criticism and iterative optimization, and enhance the diversity of error identification.
3. The method of claim 1, wherein, In step S1, the model A and model B adopt the thought chain CoT mode for reasoning, and its generation process is defined by the following joint probability formula: ; wherein, represents input information, including natural language queries and database table structure information, represents the intermediate reasoning process generated by the model, represents the final generated SQL query statement; The formulae represent that the model first generates an inference chain based on the input and then generates a final answer based on the inference chain together. 4. The method of claim 1, wherein, The initialization in step S1 is specifically: Model A, i.e. Based on the input Generate an initial inference chain And an initial SQL statement Noted as: ; Model B, i.e. Based on the input Generate initial inference chain And initial SQL statement , denoted as: 。 5. The method of claim 1, wherein, The step S2 is realized by the following way: In the first iteration, the model critiques the output of the model and generates critique opinions: ; In the In rounds of iteration, the model For the model The output is critiqued to generate critical opinions: ; wherein, and denote the critic operators of the models and respectively.
6. The method of claim 1, wherein, In step S2, the structured criticism opinion is generated by prompt engineering guidance, and the review dimensions include: the correctness of data table selection, the rationality of field association, the appropriateness of aggregation function use, the completeness and logical correctness of WHERE condition clause.
7. The method of claim 1, wherein, The step S3 is realized by the following way: Model Correct based on previous output and received criticism: ; Model Correcting based on its own previous round output and received criticism: ; wherein, and denote the self-correcting operator of the model and the model respectively.
8. The method of claim 1, wherein, In step S4, the final SQL statement is output by the following way: After N iterations, output model Final inference chain and SQL statement ; output model Final inference chain and SQL statement ; The output result constitutes a sequence of: .
9. The method of claim 1, wherein, Step S5: if SQL-A and SQL-B are both executed successfully, randomly select one of the query results to return to the user; if only one of SQL-A or SQL-B is executed successfully, return the successful query result to the user.
10. The method of claim 1, wherein, The step S6 is specifically: Based on the final error correction step of execution: when SQL-A and SQL-B are both executed unsuccessfully, the execution error information of SQL-A is fed back to model A and instructed to modify, and the execution error information of SQL-B is fed back to model B and instructed to modify; model A and model B generate the modified version of SQL statement according to the error information and try to execute again, until the executable SQL statement is generated or the maximum number of attempts is reached.