Method for converting natural language into structured query language

By using the RAG-Qwen fusion framework, which combines enhanced retrieval generation with intelligent agent collaboration, the problems of long-tail semantic matching and domain migration in the natural language to SQL conversion are solved, enabling efficient and accurate cross-domain complex queries.

CN122045236APending Publication Date: 2026-05-15NANJING UNIV OF POSTS & TELECOMM
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
NANJING UNIV OF POSTS & TELECOMM
Filing Date
2026-02-11
Publication Date
2026-05-15

AI Technical Summary

Technical Problem

Existing technologies suffer from long-tail semantic matching and domain migration issues when converting natural language into SQL queries, making it difficult to achieve efficient and accurate cross-domain complex queries.

Method used

The RAG-Qwen fusion framework is adopted, which combines retrieval enhancement generation and intelligent agent collaboration mechanism. Through semantic parsing, multi-source knowledge base retrieval, adaptive attention mechanism and multi-turn interaction, the conversion of natural language to SQL is realized.

Benefits of technology

It significantly improves the ability to model complex logic, reduces semantic ambiguity, and enhances the accuracy and robustness of cross-domain query generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122045236A_ABST
    Figure CN122045236A_ABST
Patent Text Reader

Abstract

The invention provides a method for converting a natural language into a structured query language. According to the method, efficient and accurate conversion from a natural language (NL) to a structured query language (SQL) is realized through a retrieval enhancement generation and agent collaboration mechanism. An RAG (Retrieval Enhanced Generation) mechanism is innovatively combined with the interactive decision-making ability of a universal agent: firstly, the universal agent actively initiates an intention clarification dialogue based on semantic analysis, and a fuzzy expression in user query is accurately positioned; secondly, the RAG module dynamically retrieves context information from a multi-source knowledge base; finally, knowledge semantics are injected into the SQL generation process through a self-adaptive attention mechanism, and the modeling capacity of the model for complex logic is enhanced. According to the method, the problems of long-tail semantic matching and domain migration are effectively solved in a cross-domain complex query scene.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the interdisciplinary technical fields of retrieval enhancement generation, large language model, and database information retrieval. Specifically, it relates to a natural language to SQL query conversion method based on retrieval enhancement generation and agent collaboration. Background Technology

[0002] With the continuous development of modern computer technology, people began to try using computers to process and understand human language. Early NLP research was mainly based on rules and handcrafted features, but it was limited by challenges in terms of scale, efficiency, and generalization ability. With the introduction of deep learning, especially the proposal of the Transformer model, the field of NLP has achieved a major breakthrough. The Transformer model processes text data through a self-attention mechanism, enabling it to capture long-term dependencies and contextual information in the text, thus achieving excellent performance in a variety of NLP tasks.

[0003] Currently, large language models typically employ the Transformer model architecture, which processes text data through a self-attention mechanism. The Transformer model consists of an encoder and a decoder. The encoder transforms the input sequence into a high-dimensional vector representation, while the decoder generates the output sequence based on these vector representations. This architecture enables the model to capture long-term dependencies and contextual information within the text, thus achieving superior performance in various NLP tasks.

[0004] The applications of Natural Language Processing (NLP) are very broad, including automatic question answering, machine translation, sentiment analysis, and text generation. In automatic question answering systems, NLP models can understand the semantics and intent of questions and extract relevant answers from large amounts of structured and unstructured data. In machine translation, NLP models can learn the semantic and grammatical rules of a language, transforming source language text into target language text. Sentiment analysis understands the author's emotional attitude by analyzing the sentiment tendency and polarity of the text. Text generation involves the automatic generation and creation of natural language text, including text summarization, article generation, dialogue systems, and literary creation. Summary of the Invention

[0005] Technical Problem: To address the aforementioned issues, this invention proposes an NL2SQL conversion method based on retrieval-enhanced generation and a collaborative intelligent agent. A fusion framework of RAG-Qwen (retrieval-enhanced generation and generalized intelligent agent) is designed, achieving efficient and accurate natural language to SQL conversion through a collaborative mechanism between retrieval-enhanced generation and the intelligent agent. It innovatively combines the RAG mechanism with the interactive decision-making capabilities of the generalized intelligent agent: First, the generalized intelligent agent proactively initiates intent clarification dialogue based on semantic parsing, accurately locating ambiguous expressions in the user's query; second, the RAG module dynamically retrieves contextual information from a multi-source knowledge base; finally, an adaptive attention mechanism injects knowledge semantics into the SQL generation process, enhancing the model's ability to model complex logic. This invention demonstrates significant advantages in cross-domain complex query scenarios, and its knowledge enhancement mechanism effectively solves the problems of long-tail semantic matching and domain transfer.

[0006] Technical Solution: To achieve the above objectives, this invention provides a method for converting natural language into structured query language, comprising the following steps:

[0007] Step 1: Input a piece of natural language text;

[0008] Step 2: Remove special characters and redundant spaces from the natural language text obtained in Step 1, and standardize the English to lowercase letters and the Chinese to simplified Chinese to obtain a preprocessed text sequence. Through text cleaning and standardization, noise in the input is eliminated to ensure uniform text format, laying the foundation for subsequent parsing.

[0009] Step 3: Perform word segmentation and entity recognition on the preprocessed text sequence obtained in Step 2, extract key intents, operation types and constraints, and output a structured semantic representation; use natural language processing technology to parse the semantic elements of the query from the preprocessed text, including intents, operation types and constraints, to form a structured semantic representation.

[0010] Step 4: Based on the structured semantic representation obtained in Step 3, relevant information is dynamically retrieved from multi-source knowledge bases, including database architecture metadata, domain terminology database, and historical query logs. A hybrid strategy is used for retrieval to obtain retrieval results. According to the intent and constraints in the semantic representation, relevant knowledge and metadata are retrieved to enhance contextual understanding. The retrieval process dynamically adjusts the fusion weights to adapt to the query complexity.

[0011] Step 5: Using a generalized intelligent agent, interactive intent clarification is performed based on the structured semantic representation from Step 3 and the retrieval results from Step 4. Ambiguous entities and missing constraints are clarified through multi-turn dialogue, and user feedback is recorded to obtain the clarified intent. Interaction logs between the agent and the user are recorded to resolve semantic ambiguity or missing information issues, and the intent representation is optimized based on the feedback.

[0012] Step 6: Perform context fusion and semantic enhancement on the search results from Step 4 and the interaction logs from Step 5. Construct an enhanced contextual representation through an adaptive attention mechanism; fuse search knowledge and interaction history to generate rich contextual information and provide semantic support for the generation of Structured Query Language (SQL).

[0013] Step 7: Based on the enhanced context representation in Step 6, construct the SQL syntax tree and decode constraints. Use the Abstract Syntax Tree (AST) template and the PICARD constraint decoding mechanism (text-to-SQL) to generate SQL statements that conform to the database syntax and semantics. Map the semantic intent to the SQL structure to ensure that the generated SQL statements are syntactically and semantically correct.

[0014] Step 8: Execute the SQL statement generated in Step 7 on the target database, obtain the query results, and compare and verify them with the expected results; execute the SQL and check the correctness of the results to identify potential errors or deviations.

[0015] Step 9: If there are errors in the execution results of Step 8 or the user provides feedback for correction, perform error analysis and feedback learning, use the error information as reinforcement learning signals, update the agent decision model and retrieval weights to achieve closed-loop optimization; continuously improve model performance through the feedback mechanism to enhance the accuracy of subsequent queries.

[0016] Step 10: Output the SQL statement generated in Step 7 that conforms to the database syntax and semantics, and the query results from Step 8, completing the end-to-end conversion from natural language to SQL; return the executable SQL and query results.

[0017] in:

[0018] Step 3 includes the following steps:

[0019] Step 3-1: A natural language problem is obtained from Step 2. and a series of words or tags With database schema Concatenate them into a sequence; where Represented as a set of columns, Represented as a set of tables;

[0020] Step 3-2: Represent the sequence obtained in Step 3-1 as follows:

[0021]

[0022] in For database name, For the columns of each table, This indicates a special column marker in the database;

[0023] Step 3-3: Use a bidirectional encoder to learn the input. Hidden state The specific formula is as follows:

[0024]

[0025] Steps 3-4: The semantic representation of the hidden state is encoded using a Transformer block, which contains a multi-head self-attention network. And the fully connected feedforward network FFN, in the... The hidden state of a layer is represented as

[0026] ,in It is the maximum length of the input;

[0027] Steps 3-5 and 3-4 The first part will query the matrix Key matrix Value matrix This is mapped to an attention vector using a self-attention mechanism; the formula is as follows:

[0028]

[0029] in, It is the number of query vectors. It is the number of key or value vectors; Self-attention is performed on each attention head, where T represents the linear transformation matrix for each attention head; T denotes the transpose. Representing the query matrix AND key matrix Multiply by the transpose of the matrix; This represents the dimension of a large language model. express Mapped to the target dimension of the query vector. The dimension of the value vector in each attention head has been removed here;

[0030] Steps 3-6: In the encoder module of the large language model, After calculating the attention output of each head, they are connected together to obtain the result of the multi-head self-attention network;

[0031] Steps 3-7: Semantic hidden states also need to be handled by another component. The formula is as follows:

[0032]

[0033] The linear weight matrix is ​​shown as follows: , , This represents the hidden layer dimension of the feedforward neural network, used to expand the feature space; Indicates the first Layer input, , This represents the bias vector, used to provide an offset for the calculation of each layer;

[0034] Steps 3-8: After step 3-7, normalization and residual connections are performed to obtain the semantic hidden state; the formula is as follows:

[0035]

[0036] This represents the result after normalization.

[0037] Step 4, which involves dynamically retrieving relevant information from a multi-source knowledge base, including database schema metadata, domain terminology database, and historical query logs, includes the following steps:

[0038] Step 4-1: Multi-source knowledge retrieval. Based on the structured semantic representation obtained in Step 3, parallel retrieval is performed from multi-source knowledge bases to obtain fragmented knowledge related to the current query.

[0039] Step 4-2: Dynamic weight fusion. A hybrid strategy is used to fuse the fragmented search results mentioned above. The fusion weights of each source are dynamically adjusted according to the complexity of the current query to generate the final enhanced context.

[0040] Step 5 clarifies ambiguous entities and missing constraints through multiple rounds of dialogue, records user feedback, and obtains the clarified intent. This involves the following steps:

[0041] Step 5-1: Regarding context management, Qwen (Alibaba Tongyi Qianwen Big Language Model Series) employs a dynamic file ID referencing mechanism and segmented encoding technology to handle long dialogue scenarios. Each uploaded file is assigned a unique identifier, and cross-round data association is achieved through a formulaic memory index. The storage efficiency of the file service system is optimized using the information entropy formula:

[0042]

[0043] in It is a random variable. For random variables The (i)th possible value, For random events The probability of i, entropy This indicates the average amount of information required to determine the outcome of an event.

[0044] Step 5-2: Enhance the context The generalized intelligent agent first performs fuzziness detection; then defines a fuzziness scoring function. To identify semantic units that need clarification; ;

[0045] in:

[0046] u represents a semantic unit to be evaluated. It is a lexical fuzziness index It represents the confidence level of the named entity recognition model in recognizing this entity.

[0047] This indicates that the unit is related to the retrieved knowledge. Is there a conflict? , , Here are the weighting coefficients, where Indicators of fuzziness Weighting in the total ambiguity score The number representing the confidence level of named entity recognition weights, Indicators of conflict The weights;

[0048] when Exceeding the threshold At that time, the unit is marked as needing clarification; subsequently, the agent determines the state based on the current state. and input Through the policy function Generate clarification action:

[0049]

[0050] Step 5-3: Based on the user's clarification feedback f, the Agent updates the structured semantic representation S and records this interaction to optimize future strategies;

[0051] Intent update: Use an update function U to correct the semantic representation; for example, for an ambiguous entity. Based on feedback f, update it to a definite entity. :

[0052]

[0053] This process will correct the corresponding fields in S and may improve the confidence level of the relevant entities.

[0054] Step 6 integrates retrieval knowledge and interaction history to generate rich contextual information, providing semantic support for SQL generation.

[0055] Step 6-1: Compare the search results from Step 4 with the interaction logs from Step 5. If a conflict is found, the information confirmed by the user in Step 5 shall be given the highest priority, and the search results shall be overwritten or annotated to ensure the consistency of the information source.

[0056] Step 6-2: Organize the aligned multi-source information in a structured manner according to a fixed template; typically, it follows the logical order of "architectural information -> domain knowledge -> clarified intent -> interaction history" to form a long text prompt with rich content and clear logic, which serves as background knowledge for SQL generation;

[0057] Step 6-3: Employ an adaptive attention mechanism to automatically learn and assign different attention weights to different types of information when processing the concatenated long text.

[0058] Step 6-4: The weighted and enhanced semantic information is then integrated with the initial user query to form a complete and enhanced contextual representation. This representation not only includes the original question but also incorporates the external knowledge and interaction results necessary to solve the question, providing comprehensive and accurate input for the next step of SQL generation.

[0059] Step 7 involves SQL syntax tree construction and constraint decoding.

[0060] Step 7-1: Based on the enhanced context generated in Step 6, the model first identifies and determines the macro-skeleton of the SQL query; this includes determining the query type, identifying which core clauses need to be included, and initially determining the arrangement and dependencies of these clauses.

[0061] Step 7-2: Map the semantic intent to specific nodes in the Abstract Syntax Tree (AST). Based on the context, the model fills the identified entities, operations, and constraints into the corresponding positions in the AST.

[0062] Step 7-3: During the process of generating SQL tokens word by word in the model, constraint decoding algorithms such as PICARD are integrated; this algorithm monitors the generated strings in real time and corrects the model's output.

[0063] Step 7-4: Perform final formatting on the SQL string generated by constraint decoding; finally, output a syntactically correct, semantically complete, and target database-compliant executable SQL statement and pass it to the execution engine.

[0064] Step 8 executes the SQL statement generated in step 7 on the target database, obtains the query results, and compares and verifies them with the expected results. By executing the SQL and checking the correctness of the results, potential errors or deviations are identified.

[0065] Step 8-1: Before executing the SQL, first obtain the query execution plan of the statement using the database's EXPLAIN command or similar function. By analyzing the key indicators in the execution plan, the query performance can be estimated.

[0066] Step 8-2: To ensure system stability, the generated SQL statements are executed in a database sandbox or read-only copy. This is to prevent problematic SQL statements from polluting production data or causing performance bottlenecks.

[0067] Step 8-3: After obtaining the execution results, perform automated verification from the dimensions of structure, scale, and content;

[0068] Step 8-4: Combine the performance analysis results with the multidimensional verification results to score and label the overall quality of this SQL generation and execution. This result will be used as a key feedback signal and input into the feedback learning mechanism in step 9 for continuous system optimization.

[0069] Step 9 provides feedback on any errors found in the execution results of step 8 or on corrections requested by the user, and includes the following steps:

[0070] Step 9-1: The system collects execution errors, performance alerts, and user correction feedback from Step 8, and uses a rule engine and classification model to attribute the errors to the following dimensions:

[0071] SQL syntax errors: such as incorrect keywords or mismatched parentheses;

[0072] Semantic logic errors: such as mismatched field types or missing table join conditions;

[0073] Performance issues: such as not using indexes, or performing a full table scan on a large data table;

[0074] Intent misunderstanding: For example, a user query for "recent data" is incorrectly interpreted as the past month;

[0075] Step 9-2: Transform the verified user correction feedback into new training samples, store them in the experience replay pool, update the domain terminology library, supplement new business terms and their SQL mapping relationships, establish a knowledge base version management mechanism to support gray-scale updates and rollbacks of model components, record error patterns in the case library, and build an index system of "common problems-solutions". Finally, the system achieves closed-loop learning through these four steps, and each error handling is transformed into a continuous improvement of system capabilities, forming an ever-evolving intelligent agent ecosystem.

[0076] Beneficial effects: This invention innovatively combines the retrieval enhancement generation mechanism with the interactive decision-making ability of intelligent agents. Through dynamic knowledge retrieval, multi-round intent clarification and constraint decoding generation, it achieves high-precision parsing and robust generation of complex cross-domain queries.

[0077] Specifically:

[0078] (1) This invention uses the RAG module to dynamically retrieve context information from a multi-source knowledge base, which effectively solves the semantic understanding bias problem caused by the lack of external knowledge in the traditional NL2SQL model and significantly improves the model's adaptability to domain terms and complex logic.

[0079] (2) This invention introduces a generalized intelligent agent as the core of interactive decision-making, and realizes multi-round intent clarification and query completion based on a finite state machine. It can actively guide users to eliminate ambiguous expressions, significantly reduce the semantic ambiguity rate, and improve the generation accuracy of complex queries.

[0080] (3) The present invention designs a context fusion strategy based on an adaptive attention mechanism, which dynamically injects the retrieval results and interaction logs into the SQL generation process, thereby enhancing the model’s ability to model complex structures such as multi-table joins and nested aggregations. Attached Figure Description

[0081] Figure 1 This is a flowchart of the method for converting natural language into structured query language according to the present invention. Detailed Implementation

[0082] The embodiments of the present invention will be disclosed below with reference to the drawings. For clarity, many practical details will be described in the following description. However, it should be understood that these practical details are not intended to limit the invention. That is, in some embodiments of the invention, these practical details are not essential.

[0083] Considering the limited capacity of the model's knowledge base, an efficient and accurate natural language to SQL conversion is achieved through a retrieval-enhanced generation (RAG) and agent collaboration mechanism. The model innovatively combines the RAG mechanism with the interactive decision-making capabilities of a generalized agent. This model achieves performance breakthroughs through a three-stage closed-loop optimization: First, the generalized agent proactively initiates intent clarification dialogue based on semantic parsing, accurately locating ambiguous expressions in the user's query; second, the RAG module dynamically retrieves contextual information from a multi-source knowledge base; finally, an adaptive attention mechanism injects semantic knowledge into the SQL generation process, strengthening the model's ability to model complex logic.

[0084] like Figure 1 As shown, this invention is a method for converting natural language into structured query language (SQL) based on retrieval enhancement generation and agent collaboration, comprising the following steps:

[0085] Step 1: Input a piece of natural language text.

[0086] Step 2: Remove special characters and redundant spaces from the natural language text obtained in Step 1, and standardize the English text to lowercase and the Chinese text to simplified Chinese, resulting in a preprocessed text sequence. Text cleaning and standardization eliminate noise in the input, ensuring a consistent text format and laying the foundation for subsequent parsing.

[0087] Step 3-1: A natural language problem is obtained from Step 2. and a series of words or tags With database schema Concatenate them into a sequence; where Represented as a set of columns, Represented as a set of tables;

[0088] Step 3-2: Represent the sequence obtained in Step 3-1 as follows:

[0089]

[0090] in For database name, For the columns of each table, This indicates a special column marker in the database;

[0091] Step 3-3: Use a bidirectional encoder to learn the input. Hidden state The specific formula is as follows:

[0092]

[0093] Steps 3-4: The semantic representation of the hidden state is encoded using a Transformer block, which contains a multi-head self-attention network. And the fully connected feedforward network FFN, in the... The hidden state of a layer is represented as

[0094] ,in It is the maximum length of the input;

[0095] Steps 3-5 and 3-4 The first part will query the matrix Key matrix , This is mapped to an attention vector using a self-attention mechanism; the formula is as follows:

[0096]

[0097] in, It is the number of query vectors. It represents the number of key or value vectors; self-attention is performed at each attention head, and T represents the linear transformation matrix for each attention head; T denotes transpose. Representing the query matrix AND key matrix Multiply by the transpose of the matrix; This represents the dimension of a large language model. express Mapped to the target dimension of the query vector. This represents the dimension of the value vector in each attention head;

[0098] Steps 3-6: In the encoder module of the large language model, After calculating the attention output of each head, they are connected together to obtain the result of the multi-head self-attention network;

[0099] Steps 3-7: Semantic hidden states also need to be handled by another component. The formula is as follows:

[0100]

[0101] The linear weight matrix is ​​shown as follows: , , This represents the hidden layer dimension of the feedforward neural network, used to expand the feature space; Indicates the first Layer input, , This represents the bias vector, used to provide an offset for the calculation of each layer;

[0102] Steps 3-8: After step 3-7, normalization and residual connections are performed to obtain the semantic hidden state; the formula is as follows:

[0103]

[0104] This represents the result after normalization.

[0105] Step 4-1: Multi-source knowledge retrieval. Based on the structured semantic representation obtained in Step 3, parallel retrieval is performed from multi-source knowledge bases to obtain fragmented knowledge related to the current query.

[0106] Step 4-2: Dynamic weight fusion. A hybrid strategy is used to fuse the fragmented search results. The fusion weights of each source are dynamically adjusted according to the complexity of the current query to generate the final enhanced top and bottom results.

[0107] Step 5-1: Regarding context management, Qwen employs a dynamic file ID referencing mechanism and segmented encoding technology to handle long-dialogue scenarios. Each uploaded file is assigned a unique identifier, and cross-round data association is achieved through a formulaic memory index. The storage efficiency of the file service system can be optimized using the information entropy formula:

[0108]

[0109] in It is a random variable. For random variables The (i)th possible value, For random events probability, entropy This indicates the average amount of information required to determine the outcome of an event.

[0110] Step 5-2: Enhance the context The generalized intelligent agent first performs fuzziness detection. A fuzziness scoring function is defined. To identify semantic units that need clarification (such as entity e, constraints) ):

[0111]

[0112] in:

[0113] u represents a semantic unit to be evaluated. It is an indicator of lexical fuzziness (such as the degree of fuzziness of words like "nearby" and "nearest").

[0114] It represents the confidence level of the named entity recognition model in recognizing this entity.

[0115] This indicates that the unit is related to the retrieved knowledge. Is there a conflict? , , Here are the weighting coefficients, where Indicators of fuzziness Weighting in the total ambiguity score The number representing the confidence level of named entity recognition weights, Indicators of conflict The weight.

[0116] when Exceeding the threshold At that time, the unit is marked as needing clarification. Subsequently, the agent determines the state based on the current state. and input Through the policy function Generate clarification action a:

[0117]

[0118] Step 5-3: Based on the user's clarification feedback f, the Agent updates the structured semantic representation S and records this interaction to optimize future strategies.

[0119] Intent Update: Use an update function U to correct the semantic representation. Update it to a definite entity based on feedback f. :

[0120]

[0121] This process will correct the corresponding fields in S and may improve the confidence level of the relevant entities.

[0122] Step 6-1: Compare the search results from Step 4 with the interaction logs from Step 5. When a conflict is found, the information confirmed by the user in Step 5 is given the highest priority, and the search results are overwritten or annotated to ensure the consistency of the information source.

[0123] Step 6-2: Organize the aligned multi-source information into a structured format according to a fixed template. This typically follows the logical order of "architectural information -> domain knowledge -> clarified intent -> interaction history," forming a rich and logically clear long text prompt that serves as background knowledge for SQL generation.

[0124] Step 6-3: Adopt an adaptive attention mechanism to allow the model to automatically learn and assign different attention weights to different types of information when processing the spliced ​​long text.

[0125] Step 6-4: The weighted and enhanced semantic information is then integrated with the initial user query to form a complete and enhanced contextual representation. This representation not only includes the original question but also incorporates the external knowledge and interaction results necessary to solve the question, providing comprehensive and accurate input for the next step of SQL generation.

[0126] Step 7-1: Based on the enhanced context generated in Step 6, the model first identifies and determines the macro-skeleton of the SQL query. This includes determining the query type, identifying which core clauses need to be included, and initially determining the arrangement and dependencies of these clauses.

[0127] Step 7-2: Map the semantic intent to specific nodes in the Abstract Syntax Tree (AST). The model fills the identified entities, operations, and constraints into the corresponding positions in the AST based on the context.

[0128] Step 7-3: During the process of generating SQL tokens word by word in the model, constraint decoding algorithms such as PICARD are integrated. This algorithm monitors the generated strings in real time and corrects the model's output.

[0129] Step 7-4: Perform final formatting on the SQL string generated by constraint decoding. Finally, output a syntactically correct, semantically complete, and target database-compliant executable SQL statement, which is then passed to the execution engine.

[0130] Step 8-1: Before executing the SQL statement, first obtain the query execution plan using the database's EXPLAIN command or similar function. Analyze the key metrics in the execution plan to estimate query performance.

[0131] Step 8-2: To ensure system stability, execute the generated SQL statements in a database sandbox or read-only copy. This is to prevent problematic SQL statements from polluting production data or causing performance bottlenecks.

[0132] Step 8-3: After obtaining the execution results, perform automated verification from the dimensions of structure, scale, and content.

[0133] Step 8-4: Combine the performance analysis results with the multidimensional verification results to score and label the overall quality of this SQL generation and execution. For example, label it as "successful and efficient," "successful but needs optimization," or "failure." This result will serve as a key feedback signal, inputting into the feedback learning mechanism in step 9 for continuous system optimization.

[0134] Step 9-1: The system collects execution errors, performance alerts, and user feedback from Step 8. Using a rule engine and classification model, errors are attributed to the following dimensions:

[0135] SQL syntax errors: such as incorrect keywords or mismatched parentheses.

[0136] Semantic logic errors: such as mismatched field types or missing table join conditions.

[0137] Performance issues: such as not using indexes, full table scans of large data tables.

[0138] Intent misunderstanding: For example, a user query for "recent data" is incorrectly interpreted as the data for the past month.

[0139] Step 9-2: Transform the verified user correction feedback into new training samples, store them in the experience replay pool, update the domain terminology library, supplement new business terms and their SQL mapping relationships, establish a knowledge base version management mechanism to support gray-scale updates and rollbacks of model components, record error patterns in the case library, and build an index system of "common problems-solutions". Finally, the system achieves closed-loop learning through these four steps, and each error handling is transformed into a continuous improvement of system capabilities, forming an ever-evolving intelligent agent ecosystem.

[0140] Step 10 returns the executable SQL and query results to meet the user's needs.

[0141] This invention provides a comprehensive solution for Text-to-SQL tasks, combining innovative methods of semantic understanding, dynamic retrieval, database structure understanding, information fusion, and error handling, enabling it to efficiently, accurately, and reliably transform natural language problems into SQL queries.

[0142] It is understood that the present invention has been described through some embodiments, and those skilled in the art will recognize that various changes or equivalent substitutions can be made to these features and embodiments without departing from the spirit and scope of the invention. Furthermore, under the teachings of the present invention, these features and embodiments can be modified to adapt to specific situations and materials without departing from the spirit and scope of the invention. Therefore, the present invention is not limited to the specific embodiments disclosed herein, and all embodiments falling within the scope of the claims of this application are within the protection scope of the present invention.

Claims

1. A method for converting natural language into structured query language, characterized in that, Includes the following steps: Step 1: Input a piece of natural language text; Step 2: Remove special characters and redundant spaces from the natural language text obtained in Step 1, and standardize the English to lowercase letters and the Chinese to simplified Chinese to obtain a preprocessed text sequence. Through text cleaning and standardization, noise in the input is eliminated to ensure uniform text format, laying the foundation for subsequent parsing. Step 3: Perform word segmentation and entity recognition on the preprocessed text sequence obtained in Step 2, extract key intents, operation types and constraints, and output structured semantic representation; Using natural language processing technology, the semantic elements of the query are parsed from the preprocessed text, including intent, operation type and constraints, to form a structured semantic representation; Step 4: Based on the structured semantic representation obtained in Step 3, relevant information is dynamically retrieved from multi-source knowledge bases, including database architecture metadata, domain terminology database, and historical query logs. A hybrid strategy is used for retrieval to obtain retrieval results. According to the intent and constraints in the semantic representation, relevant knowledge and metadata are retrieved to enhance contextual understanding. The retrieval process dynamically adjusts the fusion weights to adapt to the query complexity. Step 5: Using a generalized intelligent agent, interactive intent clarification is performed based on the structured semantic representation from Step 3 and the retrieval results from Step 4. Ambiguous entities and missing constraints are clarified through multiple rounds of dialogue, and user feedback is recorded to obtain the clarified intent. Record interaction logs between the intelligent agent and the user to resolve semantic ambiguity or missing information, and optimize intent representation based on feedback; Step 6: Perform context fusion and semantic enhancement on the search results from Step 4 and the interaction logs from Step 5. Construct an enhanced contextual representation through an adaptive attention mechanism; fuse search knowledge and interaction history to generate rich contextual information and provide semantic support for the generation of Structured Query Language (SQL). Step 7: Based on the enhanced context representation in Step 6, construct the SQL syntax tree and decode constraints. Use the Abstract Syntax Tree (AST) template and the PICARD constraint decoding mechanism (text-to-SQL) to generate SQL statements that conform to the database syntax and semantics. Map the semantic intent to the SQL structure to ensure that the generated SQL statements are syntactically and semantically correct. Step 8: Execute the SQL statement generated in Step 7 on the target database, obtain the query results, and compare and verify them with the expected results; execute the SQL and check the correctness of the results to identify potential errors or deviations. Step 9: If there are errors in the execution results of Step 8 or the user provides feedback for correction, perform error analysis and feedback learning, use the error information as reinforcement learning signals, update the agent decision model and retrieval weights to achieve closed-loop optimization; continuously improve model performance through the feedback mechanism to enhance the accuracy of subsequent queries. Step 10: Output the SQL statement generated in Step 7 that conforms to the database syntax and semantics, and the query results from Step 8, completing the end-to-end conversion from natural language to SQL; return the executable SQL and query results.

2. The method for converting natural language to structured query language according to claim 1, characterized in that... Step 3 includes the following steps: Step 3-1: A natural language problem is obtained from Step 2. and a series of words or tags With database schema Concatenate them into a sequence; where Represented as a set of columns, Represented as a set of tables; Step 3-2: Represent the sequence obtained in Step 3-1 as follows: ; in For database name, For the columns of each table, This indicates a special column marker in the database; Step 3-3: Use a bidirectional encoder to learn the input. Hidden state The specific formula is as follows: ; Steps 3-4: The semantic representation of the hidden state is encoded using a Transformer block, which contains a multi-head self-attention network. And the fully connected feedforward network FFN, in the... The hidden state of a layer is represented as ,in It is the maximum length of the input; Steps 3-5 and 3-4 The first part will query the matrix Key matrix , This is mapped to an attention vector using a self-attention mechanism; the formula is as follows: ; in, It is the number of query vectors. It is the number of key or value vectors; Self-attention is performed on each attention head, where T represents the linear transformation matrix for each attention head; T denotes the transpose. Representing the query matrix AND key matrix Multiply by the transpose of the matrix; This represents the dimension of a large language model. express Mapped to the target dimension of the query vector. The dimension of the value vector in each attention head has been removed here; Steps 3-6: In the encoder module of the large language model, After calculating the attention output of each head, they are connected together to obtain the result of the multi-head self-attention network; Steps 3-7: Semantic hidden states also need to be handled by another component. The formula is as follows: ; The linear weight matrix is ​​shown as follows: , , This represents the hidden layer dimension of the feedforward neural network, used to expand the feature space; Indicates the first Layer input, , This represents the bias vector, used to provide an offset for the calculation of each layer; Steps 3-8: After step 3-7, normalization and residual connections are performed to obtain the semantic hidden state; the formula is as follows: ; This represents the result after normalization.

3. The method for converting natural language to structured query language according to claim 1, characterized in that... Step 4, which involves dynamically retrieving relevant information from a multi-source knowledge base, including database schema metadata, domain terminology database, and historical query logs, includes the following steps: Step 4-1: Multi-source knowledge retrieval. Based on the structured semantic representation obtained in Step 3, parallel retrieval is performed from multi-source knowledge bases to obtain fragmented knowledge related to the current query. Step 4-2: Dynamic weight fusion. A hybrid strategy is used to fuse the fragmented search results mentioned above. The fusion weights of each source are dynamically adjusted according to the complexity of the current query to generate the final enhanced context.

4. The method for converting natural language to structured query language according to claim 1, characterized in that... Step 5 clarifies ambiguous entities and missing constraints through multiple rounds of dialogue, records user feedback, and obtains the clarified intent. This involves the following steps: Step 5-1: Regarding context management, Qwen (Alibaba Tongyi Qianwen Big Language Model Series) employs a dynamic file ID referencing mechanism and segmented encoding technology to handle long dialogue scenarios. Each uploaded file is assigned a unique identifier, and cross-round data association is achieved through a formulaic memory index. The storage efficiency of the file service system is optimized using the information entropy formula: ; in It is a random variable. For random variables The (i)th possible value, For random events The probability of i, entropy This indicates the average amount of information required to determine the outcome of an event. Step 5-2: Enhance the context The generalized intelligent agent first performs fuzziness detection; then defines a fuzziness scoring function. To identify semantic units that need clarification; ; in: u represents a semantic unit to be evaluated. It is a lexical fuzziness index It represents the confidence level of the named entity recognition model in recognizing this entity. This indicates that the unit is related to the retrieved knowledge. Is there a conflict? , , Here are the weighting coefficients, where Indicators of fuzziness Weighting in the total ambiguity score The number representing the confidence level of named entity recognition weights, Indicators of conflict The weights; when Exceeding the threshold At that time, the unit is marked as needing clarification; subsequently, the agent determines the state based on the current state. and input Through the policy function Generate clarification action: ; Step 5-3: Based on the user's clarification feedback f, the Agent updates the structured semantic representation S and records this interaction to optimize future strategies; Intent update: Use an update function U to correct the semantic representation; for example, for an ambiguous entity. Based on feedback f, update it to a definite entity. : ; This process will correct the corresponding fields in S and may improve the confidence level of the relevant entities.

5. The method for converting natural language to structured query language according to claim 1, characterized in that... Step 6 integrates retrieval knowledge and interaction history to generate rich contextual information, providing semantic support for SQL generation. Step 6-1: Compare the search results from Step 4 with the interaction logs from Step 5. If a conflict is found, the information confirmed by the user in Step 5 shall be given the highest priority, and the search results shall be overwritten or annotated to ensure the consistency of the information source. Step 6-2: Organize the aligned multi-source information in a structured manner according to a fixed template; typically, it follows the logical order of "architectural information -> domain knowledge -> clarified intent -> interaction history" to form a long text prompt with rich content and clear logic, which serves as background knowledge for SQL generation; Step 6-3: Employ an adaptive attention mechanism to automatically learn and assign different attention weights to different types of information when processing the concatenated long text. Step 6-4: The weighted and enhanced semantic information is then integrated with the initial user query to form a complete and enhanced contextual representation. This representation not only includes the original question but also incorporates the external knowledge and interaction results necessary to solve the question, providing comprehensive and accurate input for the next step of SQL generation.

6. The method for converting natural language to structured query language according to claim 1, characterized in that... Step 7 involves SQL syntax tree construction and constraint decoding. Step 7-1: Based on the enhanced context generated in Step 6, the model first identifies and determines the macro-skeleton of the SQL query; this includes determining the query type, identifying which core clauses need to be included, and initially determining the arrangement and dependencies of these clauses. Step 7-2: Map the semantic intent to specific nodes in the Abstract Syntax Tree (AST). Based on the context, the model fills the identified entities, operations, and constraints into the corresponding positions in the AST. Step 7-3: During the process of generating SQL tokens word by word in the model, constraint decoding algorithms such as PICARD are integrated; this algorithm monitors the generated string in real time and corrects the model's output. Step 7-4: Perform final formatting on the SQL string generated by constraint decoding; finally, output a syntactically correct, semantically complete, and target database-compliant executable SQL statement and pass it to the execution engine.

7. The method for converting natural language to structured query language according to claim 1, characterized in that... Step 8 executes the SQL statement generated in step 7 on the target database, obtains the query results, and compares and verifies them with the expected results. By executing the SQL and checking the correctness of the results, potential errors or deviations are identified. Step 8-1: Before executing the SQL, first obtain the query execution plan of the statement using the database's EXPLAIN command or similar function. By analyzing the key indicators in the execution plan, the query performance can be estimated. Step 8-2: To ensure system stability, the generated SQL statements are executed in a database sandbox or read-only copy. This is to prevent problematic SQL statements from polluting production data or causing performance bottlenecks. Step 8-3: After obtaining the execution results, perform automated verification from the dimensions of structure, scale, and content; Step 8-4: Combine the performance analysis results with the multidimensional verification results to score and label the overall quality of this SQL generation and execution. This result will be used as a key feedback signal and input into the feedback learning mechanism in step 9 for continuous system optimization.

8. The method for converting natural language to structured query language according to claim 1, characterized in that... Step 9 provides feedback on any errors found in the execution results of step 8 or on corrections requested by the user, and includes the following steps: Step 9-1: The system collects execution errors, performance alerts, and user correction feedback from Step 8, and uses a rule engine and classification model to attribute the errors to the following dimensions: SQL syntax errors: such as incorrect keywords or mismatched parentheses; Semantic logic errors: such as mismatched field types or missing table join conditions; Performance issues: such as not using indexes, or performing a full table scan on a large data table; Intent misunderstanding: For example, a user query for "recent data" is incorrectly interpreted as the past month; Step 9-2: Transform the verified user correction feedback into new training samples, store them in the experience replay pool, update the domain terminology library, supplement new business terms and their SQL mapping relationships, establish a knowledge base version management mechanism to support gray-scale updates and rollbacks of model components, record error patterns in the case library, and build an index system of "common problems-solutions". Finally, the system achieves closed-loop learning through these four steps, and each error handling is transformed into a continuous improvement of system capabilities, forming an ever-evolving intelligent agent ecosystem.