SQL (Structured Query Language) statement generation method based on large-model multi-stage iteration

Through a method based on multi-stage iteration of large models, combined with advanced semantic modeling, intent recognition, calibration correction and deep optimization strategies, the shortcomings in natural language SQL query understanding and database structure understanding in the existing technology are solved, and more efficient and accurate SQL query generation is achieved.

CN120086244AInactive Publication Date: 2025-06-03ZHIJIA ARTIFICIAL INTELLIGENCE TECH (TIANJIN) CO LTD +1

Patent Information

Application Number
CN202510572331.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-06
Publication Date
2025-06-03
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

The prior art has misjudgments when understanding the user's natural language query intention. The generated SQL query statements cannot accurately reflect user needs, and have limited ability to understand the database structure, resulting in query statements being incorrect or inefficient, and lack of an effective user feedback mechanism.

Method used

The SQL statement generation method based on multi-stage iteration of large models is adopted. Through advanced semantic modeling technology, intent recognition model, calibration correction model and deep optimization strategy, combined with adaptive evolutionary process and user preference feedback, the accuracy and efficiency of natural language SQL query generation are significantly improved.

Benefits of technology

It significantly improves the accuracy and efficiency of SQL query generation, ensures that the generated SQL query statements can accurately reflect the user's query intention and achieve optimal performance, thereby improving the overall query efficiency and user experience of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120086244A_ABST
    Figure CN120086244A_ABST
Patent Text Reader

Abstract

The invention provides an SQL (structured query language) statement generation method based on large-model multi-stage iteration, which is used for database structure understanding and natural language SQL query generation, constructing multi-level semantic embedding of a database structure through advanced semantic modeling, and deeply analyzing a natural language query request of a user by utilizing an intention recognition model; a query intention frame is extracted in combination with a pre-trained large model, the query intention frame and database semantics are embedded and fused by adopting a calibration correction model and in combination with a multi-stage feedback mechanism, and a query intention and model parameters are optimized step by step; an optimal practice SQL statement is generated through deep optimization and a retrieval strategy model, model parameters and retrieval strategies are continuously optimized through self-adaptive evolution and user preference feedback, and a knowledge base is updated. According to the method, the problems of much manual intervention, inaccurate intention understanding, insufficient structure understanding and lack of a feedback mechanism are effectively solved, and efficient and accurate SQL query generation is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the fields of database structure understanding and large language model retrieval augmented generation, and particularly to a method and system for generating SQL statements based on multi-stage iteration of large models. Background Art

[0002] With the advent of the big data era, the demand for data management and analysis by enterprises and individuals has been increasing. Especially in fields such as finance, healthcare, and e-commerce, users often need to obtain specific information in the database through natural language queries. However, users generally do not have professional SQL writing skills. Therefore, it is particularly important to develop a technology that can automatically convert natural language queries into SQL queries. This technology needs to be able to accurately understand the user's query intent and efficiently generate SQL query statements that conform to the database structure to meet the user's need to obtain data quickly and accurately.

[0003] Currently, there are various methods attempting to solve the problem of converting natural language to SQL queries. Common methods include rule-based systems, template-based methods, and machine learning-based models. Rule-based systems generate SQL queries through predefined rules and pattern matching, but this method lacks flexibility and is difficult to handle complex query requirements. Template-based methods generate SQL queries through predefined query templates, but the template coverage is limited and cannot handle diverse query scenarios. Machine learning-based models, such as deep learning models, learn the mapping relationship between query intent and SQL queries through a large amount of training data. Although they improve the accuracy and flexibility of queries to a certain extent, there are still deficiencies in dealing with complex queries and database structure understanding.

[0004] When existing methods understand the user's natural language query intent, they often make misjudgments due to semantic ambiguity or polysemy, and the generated SQL query statements cannot accurately reflect the user's actual needs. Moreover, existing natural language to SQL conversion methods have limited ability to understand the database structure, especially in dealing with complex table relationships, field mappings, and index usage, etc., and are prone to generating incorrect or inefficient query statements. Most existing methods lack an effective user feedback mechanism and cannot continuously optimize the model according to the user's actual usage situation, resulting in a gradual decline in the accuracy and robustness of the model over time.

[0005] To solve the above problems, the present invention proposes a method for generating SQL statements based on multi-stage iteration of large models. Through advanced semantic modeling technology, intent recognition models, calibration and correction models, and deep optimization strategies, combined with an adaptive evolution process and user preference feedback, the accuracy and efficiency of natural language SQL query generation are significantly improved. Summary of the Invention

[0006] The embodiments of the present application provide a method and system for generating SQL statements based on multi-stage iteration of large models to solve the problems of excessive manual intervention, inability to ensure the accuracy and efficiency of detection results, inaccurate understanding of query intentions, insufficient understanding of database structures, and lack of an effective feedback mechanism in the prior art.

[0007] In a first aspect, the embodiments of the present application provide a method for generating SQL statements based on multi-stage iteration of large models, including:

[0008] Using advanced semantic modeling technology, deeply analyze and process the table, field, index, and relationship structures of the target database to construct a multi-level semantic embedding of the database structure;

[0009] Apply an intention recognition model to deeply parse the natural language query request of the user, and combine it with a pre-trained large model to extract a query intention framework including query purpose, operation object, and condition restrictions;

[0010] Through a calibration and correction model, use a multi-stage feedback-guided correction mechanism to combine the initially parsed query intention framework with the semantic embedding of the database structure to correct the query intention. The calibration and correction model returns the error type and modification suggestions, and gradually optimizes the query intention and the parameters of the intention recognition model;

[0011] Using a deep optimization strategy, perform fine-grained correction on the corrected query intention and the parameters of the pre-trained large model, combine them with the semantic embedding of the database structure, and retrieve the best practice strategy generated by the pre-trained large model through a retrieval strategy model to form an optimized SQL query statement;

[0012] Through an adaptive evolution process and human preference feedback on the model output results, use the DPO (Direct Preference Optimization) algorithm to continuously optimize the parameters of the pre-trained large model and the retrieval strategy model, synchronously update the knowledge base of the large model and the retrieval strategy, and obtain an optimized large model for SQL query generation.

[0013] Optionally, the step of using a calibration and correction model, with a multi-stage feedback-guided correction mechanism, to combine the initially parsed query intention framework with the semantic embedding of the database structure to correct the query intention includes:

[0014] Using a graph neural network, transform the initially parsed query intention framework into a graph structure representation, and at the same time transform the database structure semantic embedding model into a graph structure representation;

[0015] Apply a structure similarity algorithm to quantify the similarity between the query intention framework graph and the database structure graph and identify the mismatch points between the two;

[0016] The attention mechanism is adopted to precisely locate the differences between the query intention and the database structure by combining mismatched points, and a specific error type report is generated. The error type report includes that the query object does not exist, the query conditions are unclear, field mapping errors, and semantic understanding deviations.

[0017] The sequence-to-sequence model is used to generate a more accurate description of the query conditions in combination with the error type report.

[0018] Combined with the feedback information provided by the user and the effect evaluation of the correction strategy, the reinforcement learning algorithm is used to dynamically adjust the query intention framework to ensure that the correction direction always moves towards the goal of improving query accuracy.

[0019] After each correction, the ensemble learning method is used to re-evaluate the matching degree between the corrected query intention framework and the database structure. If the matching degree does not meet the expected standard, the above correction process is repeated until the query intention framework completely conforms to the database structure or reaches the preset maximum number of correction rounds.

[0020] Optionally, the combination of the feedback information provided by the user and the effect evaluation of the correction strategy, using the reinforcement learning algorithm to dynamically adjust the query intention framework to ensure that the correction direction always moves towards the goal of improving query accuracy, includes:

[0021] Collect user feedback information, including the satisfaction evaluation of the initially generated SQL query statement, the pointed-out errors and improvement suggestions, and the confirmation or negation of the user for the corrected query results.

[0022] Perform preprocessing quantization of sentiment analysis, keyword extraction, and semantic understanding on the user feedback information, and construct a reward function in combination with the effect evaluation of the correction strategy.

[0023] Use the reinforcement learning algorithm to learn the optimal correction path through trial and error based on the constructed reward function, and dynamically adjust the correction strategy of the query intention framework.

[0024] After each correction operation, generate and execute an SQL query statement according to the new query intention framework, obtain the latest query results, and collect user feedback information again to form a closed-loop feedback mechanism.

[0025] Optionally, the use of a deep optimization strategy to perform fine-grained correction on the corrected query intention and pre-trained large model parameters, combine it with the semantic embedding of the database structure, and retrieve the best practice strategy generated by the pre-trained large model through the retrieval strategy model to form an optimized SQL query statement, includes:

[0026] Using a deep learning model, perform fine-grained analysis and processing on the key elements in the corrected query intention framework, enhance the semantic expressions of key elements such as query purpose, operation object, and conditional restrictions in the query intention framework, and obtain the enhanced key elements of the query intention.

[0027] Using the attention mechanism, perform dynamic weighting processing on the enhanced key elements of the query intention, highlight the important parts, reduce noise interference, improve the accuracy of the query intention, and obtain the optimized key elements of the query intention.

[0028] Using the semantic embedding model of the database structure, perform multi-dimensional feature vector construction processing on the optimized key elements of the query intention, including table structure features, field attribute features, relationship features, etc., to provide rich context information for subsequent optimization, and obtain the multi-dimensional feature vector of the query intention.

[0029] Using the retrieval strategy model, retrieve the historical best practice strategies that best match the multi-dimensional feature vector of the query intention from the pre-trained large model. These strategies include efficient query paths, optimized index usage, reasonable connection methods, etc., and obtain the best practice strategies applicable to the current query intention.

[0030] Using the best practice strategies, perform preliminary SQL query statement generation processing on the query intention, and perform syntax and logical verification and performance optimization processing on the preliminarily generated SQL query statements through the SQL optimizer to ensure the correctness and efficiency of the query statements, and finally generate optimized SQL query statements. Record all data and results during the optimization process for continuous updating of the pre-trained large model and the retrieval strategy model to improve the generalization ability and adaptability of the models.

[0031] Through the above deep optimization strategies, the present invention can correct and optimize the query intention at a fine-grained level, ensure that the generated SQL query statements not only accurately reflect the user's query intention but also achieve the best performance, thereby significantly improving the overall query efficiency and user experience of the system.

[0032] Optionally, using the deep optimization strategy, perform fine-grained correction on the corrected query intention and the pre-trained large model parameters, combine it with the semantic embedding of the database structure, retrieve the best practice strategies generated by the pre-trained large model through the retrieval strategy model, and form optimized SQL query statements, including:

[0033] Using a deep learning model, perform fine-grained analysis and processing on the key elements in the corrected query intention framework, enhance the semantic expressions of the query purpose, operation object, and conditional restrictions in the query intention framework, and at the same time combine word embedding technology and sentence encoders to generate high-dimensional semantic vectors, and obtain the enhanced key elements of the query intention.

[0034] Through the adaptive attention mechanism, the attention weights are dynamically adjusted according to the complexity of the query intention, further optimizing the weight distribution of the key elements of the query intention to obtain the optimized key elements of the query intention.

[0035] Using the retrieval strategy model, retrieve the historical best practice strategy that best matches the multi-dimensional feature vector of the query intention from the pre-trained large model;

[0036] Adopt the ranking learning algorithm to rank the retrieval results according to the execution effect of the historical best practice strategy and user feedback, obtain the best practice strategy applicable to the current query intention, and perform preliminary SQL query statement generation processing on the query intention.

[0037] Optionally, the step of adopting the ranking learning algorithm to rank the retrieval results according to the execution effect of the historical best practice strategy and user feedback, obtain the best practice strategy applicable to the current query intention, and perform preliminary SQL query statement generation processing on the query intention includes:

[0038] Adopt dynamic programming and greedy algorithms to optimize the query path, index usage, and connection method to obtain an SQL optimizer;

[0039] Use a sequence-to-sequence model to generate a preliminary SQL query statement, and perform syntax, logic verification, and performance optimization processing on the preliminarily generated SQL query statement through the SQL optimizer to generate an optimized SQL query statement;

[0040] Record the query intention framework, the generated SQL query statement, and the optimization strategy during the optimization process.

[0041] Optionally, through the adaptive evolution process and human preference feedback on the model output results, use the DPO algorithm to continuously optimize the parameters of the pre-trained large model and the retrieval strategy model, synchronously update the knowledge base of the large model and the retrieval strategy, and obtain an optimized SQL query generation large model, specifically including:

[0042] Set the initial values and mutation ranges of the model parameters through the adaptive evolution process, use genetic algorithms and particle swarm optimization methods to perform random mutation and crossover operations on the model parameters, search for the optimal solution, and judge whether the optimization process terminates according to the set convergence conditions;

[0043] Collect the user's satisfaction score, modification opinions, and confirmation information on the SQL query statement of the model output results as user preference feedback.

[0044] Apply the DPO algorithm to process the collected user preference feedback to generate a reward signal, and adjust the model parameters according to the reward signal in each iteration process.

[0045] During the optimization process, record the results of each parameter adjustment and user feedback, continuously enrich the knowledge base of the large model, and update the best practice strategies in the retrieval strategy model.

[0046] In a second aspect, an embodiment of the present application provides an SQL statement generation system based on multi-stage iteration of a large model, including:

[0047] A semantic embedding module, which uses advanced semantic modeling technology to deeply analyze and process the table, field, index, and relationship structures of the target database, and constructs a multi-level semantic embedding of the database structure;

[0048] A query framework module, which applies an intent recognition model to deeply parse the user's natural language query request, and combines it with a pre-trained large model to extract a query intent framework including the query purpose, operation object, and condition restrictions;

[0049] A calibration and correction module, through a calibration and correction model, uses a multi-stage feedback-guided correction mechanism to combine the initially parsed query intent framework with the semantic embedding of the database structure to correct the query intent. The calibration and correction model returns the error type and modification suggestions, and gradually optimizes the query intent and the parameters of the intent recognition model;

[0050] A retrieval optimization module, which uses a deep optimization strategy to perform fine-grained correction on the corrected query intent and the parameters of the pre-trained large model, combines them with the semantic embedding of the database structure, retrieves the best practice strategies generated by the pre-trained large model through a retrieval strategy model, and forms an optimized SQL query statement;

[0051] A reinforcement learning module, through an adaptive evolution process and human preference feedback on the model output results, uses the DPO algorithm to continuously optimize the parameters of the pre-trained large model and the retrieval strategy model, synchronously updates the knowledge base of the large model and the retrieval strategy, and obtains an optimized large model for SQL query generation.

[0052] In a third aspect, an embodiment of the present application provides a computing device, including a processing component and a storage component; the storage component stores one or more computer instructions; the one or more computer instructions are used to be called and executed by the processing component to implement the method described in the first aspect above.

[0053] In a fourth aspect, an embodiment of the present application provides a computer storage medium storing a computer program, and when the computer program is executed by a computer, it implements the method described in the first aspect above.

[0054] In the embodiments of the present invention, through advanced semantic modeling technology, a multi-level semantic embedding of the database structure is constructed. The application intention recognition model deeply analyzes the natural language query request of the user, and combines with the pre-trained large model to extract the query intention framework. Through the calibration and correction model, using the multi-stage feedback mechanism, the initially parsed query intention framework is combined with the semantic embedding of the database structure for correction processing, and the query intention and the parameters of the intention recognition model are gradually optimized. Using the deep optimization strategy combined with the retrieval strategy model to generate the best practice strategy, and form an optimized SQL query statement. Through the adaptive evolution process and user preference feedback, the algorithm is used to continuously optimize the model parameters, update the knowledge base and retrieval strategy of the large model, and finally obtain an optimized SQL query generation large model. The present invention solves the problems of excessive manual intervention, inaccurate query intention understanding, insufficient understanding of the database structure, and lack of effective feedback mechanism in the prior art, and realizes higher query efficiency and accuracy.

[0055] These aspects or other aspects of the present application will be more clearly understood in the following description of the embodiments. Brief Description of the Drawings

[0056] In order to more clearly illustrate the technical solutions in the embodiments of the present application or in the prior art, the following will briefly introduce the drawings required for the description of the embodiments or the prior art. Obviously, the drawings in the following description are some embodiments of the present application. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0057] Figure 1 It is a flowchart of a method for generating SQL statements based on multi-stage iteration of a large model provided by an embodiment of the present application;

[0058] Figure 2 It is a schematic diagram of a system for generating SQL statements based on multi-stage iteration of a large model provided by an embodiment of the present application;

[0059] Figure 3 It is a schematic diagram of a computing device provided by an embodiment of the present application. Detailed Embodiments

[0060] The present invention proposes a method for optimizing natural language SQL query generation, including the following five core steps, and realizes efficient natural language query transformation and execution through the cooperation between different modules:

[0061] Step 101: Use advanced semantic modeling techniques to deeply analyze and process the table, field, index, and relationship structures of the target database, and construct a multi-level semantic embedding of the database structure. This module aims to build a multi-level semantic embedding representation for the target database to comprehensively characterize the semantic structure of the database and its internal correlations. By deeply analyzing the structure and semantic information of the database and combining the support of an external knowledge graph, the accuracy and richness of the embedding representation are improved. The specific implementation steps are as follows:

[0062] First, perform a comprehensive semantic parsing of the target database to extract its structure and content information:

[0063] Table structure analysis: Traverse all tables in the database to obtain metadata information such as table names, primary keys, foreign keys, and indexes. Field information extraction: Conduct a detailed analysis of the fields in each table to obtain information such as field names, data types, default values, and constraint conditions. Relationship parsing: Identify the relationships between tables, such as one-to-one, one-to-many, and many-to-many relationships, as well as the correlations between fields. By parsing the table structure, field information, and index relationships of the target database, a multi-level embedding representation is generated. Let the table embedding of the target database be , the field embedding be , and the relationship embedding be . The semantic similarity between each embedding is calculated through the following formula:

[0064]

[0065] where and represent two embedding vectors respectively, represents the inner product of vectors, represents the two-norm of the vector. This formula can effectively evaluate the similarity degree of embedding vectors in the high-dimensional semantic space, ensuring the accuracy and consistency of the multi-level embedding representation.

[0066] Then, use knowledge fusion to align the relevant entities and relationships in the external knowledge graph with the database elements and establish mapping relationships. Embedding optimization: By minimizing the following objective function, the effective fusion of external knowledge and database embedding is achieved: Combining the external knowledge graph and its embedding representation , the database embedding is enhanced and optimized. The effective fusion of knowledge is achieved through the following optimization formula:

[0067]

[0068] where and is a hyperparameter used to adjust the influence weight of the external knowledge graph on the database embedding. The optimization process aims to minimize the difference between the database embedding and the weighted combination of external knowledge so as to integrate rich external semantic information into the database embedding representation.

[0069] By experimentally adjusting the hyperparameter and , the weights between the database's own information and external knowledge are balanced, improving the quality of the embedding representation. This process effectively supplements the context information of the database embedding, enhancing the richness and robustness of the embedding representation, and enabling it to more comprehensively reflect the semantic features of the database.

[0070] To further improve the representation ability of the embedding vector, the gradient descent method is used to optimize the embedding so that the generated embedding better conforms to the structural characteristics of the database. The optimization objectives are as follows:

[0071]

[0072] Among them, is the learning rate, is the loss function, is the gradient of the loss function with respect to the embedding . It is used to measure the optimization effect of the embedding. Through iterative optimization, through the above iterative optimization, the embedding representation is gradually updated, developing in the direction of reducing the loss function, and finally obtaining a more accurate and stable database semantic embedding representation.

[0073] Step 102: Apply the intent recognition model to deeply analyze the user's natural language query request, and combine with the pre-trained large model to extract the query intent framework including the query purpose, operation object, and condition restrictions. This module is used to convert the user's input natural language query into a quantified query intent vector to ensure that the user's needs can be accurately mapped to the database query. The specific implementation includes the following.

[0074] Use the pre-trained language model to encode the user's input query text and the relevant constraint conditions to generate independent semantic vector representations:

[0075]

[0076] This process can capture the semantic features of natural language and provide a basis for subsequent intent recognition and fusion.

[0077] Combine the query text, context information, and constraint conditions to generate a global query intent vector Its calculation formula is as follows:

[0078]

[0079] Among them, , , are the weight parameters corresponding to the input, and the ability to depict the user's query intention is enhanced through weighted fusion.

[0080] Combined with the user's historical query records , the current intention vector is optimized to better fit the user's habits and preferences:

[0081]

[0082] Among them, is the historical optimization weight, represents the vector similarity function. This optimization step can use historical data to improve the accuracy and consistency of intention recognition.

[0083] Step 103: Calibrate and correct the model. Use a multi-stage feedback-guided correction mechanism to combine the query intention framework obtained by preliminary parsing with the semantic embedding of the database structure to correct the query intention. The calibration and correction model returns the error type and modification suggestions, and gradually optimizes the query intention and the parameters of the intention recognition model. This module optimizes the query intention through a feedback mechanism to improve its matching degree with the database semantic structure and ensure that the generated query can be executed efficiently.

[0084] Calculate the matching error between the query intention vector and the database semantic embedding . The formula is as follows:

[0085]

[0086] Error calculation can quantify the gap between the query intention and the database semantics, providing a basis for subsequent optimization.

[0087] Use the gradient descent method to optimize the query intention to reduce the error value:

[0088]

[0089] Among them, is the learning rate, is the error gradient. Through multiple iterations of optimization, the error value is gradually reduced.

[0090] For the error optimization process, adopt a multi-stage feedback mechanism until the error converges:

[0091]

[0092] Among them, is the convergence threshold. This mechanism can ensure that the finally generated query intent has a high semantic matching degree.

[0093] Step 104: Using the deep optimization strategy, perform fine-grained correction on the corrected query intent and the pre-trained large model parameters, combine them with the semantic embedding of the database structure, and retrieve the best practice strategies generated by the pre-trained large model through the retrieval strategy model to form an optimized SQL query statement. This module is responsible for generating an efficient SQL query statement according to the optimized query intent and optimizing the generation result.

[0094] Using the corrected query intent Q′, fine-tune the pre-trained large model. Let the model parameters be , and the goal of fine-tuning is to minimize the loss function , where measures the difference between the model generation result and the expected output.

[0095]

[0096] According to the optimized query intent Q′, initially generate an SQL query statement S to ensure the accuracy and executability of semantic mapping. Among them, is the true label, is the predicted probability of the model.

[0097] Collect the structure information of the database, including table names, field names, data types, etc., to form a metadata set D = {d 1 , d 2 ,..., d m}.

[0098] Embed the metadata set, using a pre-trained word vector model (Word2Vec), to convert the metadata into a vector representation:

[0099]

[0100] Convert the query intent into a vector representation , using the same embedding method:

[0101]

[0102] Calculate the correlation between the query intent and the database semantics to generate a fusion representation:

[0103]

[0104] Select the strategy with the highest similarity as the best practice strategy :

[0105]

[0106] Using the fine-tuned large model, generate SQL query statements according to the adjusted input. The generation process can be regarded as a mapping from the fused representation to the SQL statement:

[0107]

[0108] Optimize the initially generated query statement S through the optimization objective function. The objective function is as follows:

[0109]

[0110] Among them, , , , are the weights of different evaluation metrics, which are used to balance accuracy, precision, recall, and consistency respectively.

[0111] Then we conduct quality assessment, aiming to comprehensively and multi-dimensionally evaluate the result R returned by the query to ensure that the final result can meet the user's needs to the greatest extent and optimize the user experience. By setting various evaluation metrics, the module can analyze and improve the system performance from multiple perspectives. The following is a detailed description of each evaluation metric and its extended content:

[0112] The core objectives of quality assessment are: ensuring that the query results can accurately reflect the user's intention. Maximizing the relevance, comprehensiveness, and consistency of the query. Providing clear quantitative metrics to provide feedback for optimizing the model and algorithm.

[0113] The main evaluation metrics include: Accuracy: Measuring the correctness of the query results to ensure that the results obtained by the user's query conform to the expected semantics and facts. Calculated by the ratio of the number of correctly predicted results to the total number of results. The calculation method is: . Precision: Measuring the proportion of relevant results in the query results. Reducing the interference of irrelevant content and improving the focus of the results. The calculation method is: . Recall: Measuring the proportion of relevant results retrieved by the system among all relevant results. Ensuring that all information that the user cares about can be covered. The calculation method is: .

[0114] Step 105: Through the adaptive evolution process and human preference feedback on the model output results, use the DPO (Direct Preference Optimization) algorithm to continuously optimize the parameters of the pre-trained large model and the retrieval strategy model, synchronously update the large knowledge base and the retrieval strategy, and obtain the optimized large model for SQL query generation. The adaptive optimization module aims to dynamically adjust the model parameters and strategy configurations of the system based on user feedback to continuously improve the model performance and user satisfaction. By introducing an adaptive mechanism, the system can continuously learn and adapt to changes in user needs in practical applications, maintaining high efficiency and accuracy.

[0115] To achieve precise adaptation to user preferences, this module adopts the method of Direct Preference Optimization (DPO) for the model parameters and the retrieval strategy , and conducts dynamic optimization. The optimization objective function is defined as follows:

[0116]

[0117] where, is the user preference weight, is the loss function, which is used to measure the gap between the user's expectation and the actual result. By minimizing the weighted loss function, this optimization objective ensures that the prediction results of the model can conform to the user's preferences and expectations as much as possible. The introduction of the weight enables the model to focus on the samples that the user deems more important, thereby improving the performance of key tasks.

[0118] To achieve the above optimization objective, this module uses the gradient descent algorithm or its improved versions (such as stochastic gradient descent, Adam optimizer, etc.) for the model parameters Φ and the retrieval strategy P, continuously improving the system performance and user satisfaction.

[0119]

[0120]

[0121] where: and are the model parameters and the retrieval strategy parameters at the -th iteration. is the corresponding learning rate, which controls the step size of parameter update. and represent the gradients of the loss function with respect to the model parameters and the retrieval strategy parameters, and are calculated as:

[0122]

[0123]

[0124] Wherein: is the loss function, which measures the difference between the model prediction result and the actual target. is the prediction function determined by the parametric model and the retrieval strategy with the input being the sample . is the sample 's true label. is the partial derivative of the loss function with respect to the model parameter , which describes the sensitivity of the loss to each parameter.

[0125] In this embodiment, the tables, fields, indexes and their relationships in the target database are first subjected to multi-level semantic parsing to generate a semantic embedding representation of the database structure. During the parsing process, the knowledge graph and external knowledge base are combined to enhance the semantic expression of the database embedding. Through semantic similarity calculation, it is ensured that the generated embedding can comprehensively capture the relevance between elements in the database. At the same time, the multi-stage retrieval ability of the pre-trained large model is used to extract associated information from historical query records and the knowledge base, further optimizing the integrity and consistency of the embedding representation, providing basic support for subsequent query intention recognition and SQL generation.

[0126] For the natural language query input by the user, this embodiment uses a pre-trained large model to perform intention parsing on it, generating a query intention framework including query targets, operation scopes and conditional restrictions. Combining historical query data, the parsing accuracy is improved by retrieving similar intention frameworks. The initially generated intention framework will be matched with the database semantic embedding through a multi-stage feedback mechanism, and a calibration and correction model is used to adjust potential semantic deviations, finally obtaining an optimized intention framework. The calibration and correction process in this step also utilizes common error cases in the database query pattern to further ensure the consistency between the generated intention and the database structure.

[0127] When generating the SQL query, this embodiment generates a preliminary SQL query statement based on the calibrated query intention framework, combined with the database structure semantic information. Subsequently, parameter optimization is performed through best practice cases in a large corpus, including adjusting the query tree structure, reducing redundant conditions and optimizing query indexes. In the final stage of query generation, the generated SQL query is subjected to multiple rounds of execution tests, and the quality of the execution result is analyzed by evaluating the model to ensure the accuracy and efficiency of the query. The evaluation result will be used as feedback data for further optimizing the query generation strategy to achieve closed-loop optimization.

[0128] The above embodiments aim to highlight the key applications of this method in database semantic parsing, natural language intent recognition, and SQL query generation. Meanwhile, the precision and efficiency of query generation are significantly improved through the retrieval enhancement and feedback mechanism.

[0129] Figure 2 The described SQL statement generation method based on multi-stage iteration of large models can be executed Figure 1 For the SQL statement generation method based on multi-stage iteration of large models in the above embodiments, its implementation principle and technical effects will not be elaborated further. For the SQL statement generation method based on multi-stage iteration of large models in the above embodiments, the specific ways in which each module and unit perform operations have been described in detail in the embodiments related to this method, and will not be elaborated here.

[0130] Figure 2 The SQL statement generation method based on multi-stage iteration of large models in the shown embodiments can be implemented as a computing device, such as Figure 3 As shown, the computing device may include a storage component 31 and a processing component 32;

[0131] The storage component 31 stores one or more computer instructions, wherein the one or more computer instructions are called and executed by the processing component 32.

[0132] The processing component 32 is used to: construct a multi-level semantic embedding of the database structure through advanced semantic modeling techniques. Apply an intent recognition model to deeply parse the user's natural language query request, and extract a query intent framework in combination with a pre-trained large model. Through a calibration and correction model, use a multi-stage feedback mechanism to combine the preliminarily parsed query intent framework with the semantic embedding of the database structure for correction processing, and gradually optimize the query intent and the parameters of the intent recognition model. Use a deep optimization strategy combined with a retrieval strategy model to generate a best practice strategy, and form an optimized SQL query statement. Through an adaptive evolution process and user preference feedback, use an algorithm to continuously optimize the model parameters, update the knowledge base and retrieval strategy of the large model, and finally obtain an optimized SQL query generation large model.

[0133] Among them, the processing component 32 may include one or more processors to execute computer instructions to complete all or part of the steps in the above method. Of course, the processing component may also be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components for executing the above method.

[0134] The storage component 31 is configured to store various types of data to support the operation of the terminal. The storage component can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk, or optical disk.

[0135] Of course, the computing device may also necessarily include other components, such as input / output interfaces, display components, communication components, and the like.

[0136] The input / output interface provides an interface between the processing component and the peripheral interface module, and the above-mentioned peripheral interface module can be an output device, an input device, etc.

[0137] The communication component is configured to facilitate communication between the computing device and other devices in a wired or wireless manner, etc.

[0138] Among them, the computing device can be a physical device or an elastic computing host provided by a cloud computing platform, etc. At this time, the computing device can refer to a cloud server, and the above-mentioned processing component, storage component, etc. can be basic server resources leased or purchased from a cloud computing platform.

[0139] The embodiment of the present invention also provides a computer storage medium storing a computer program, and when the computer program is executed by a computer, it can implement the above-mentioned Figure 1 SQL statement generation method based on large model multi-stage iteration shown in the embodiment.

[0140] Those skilled in the art can clearly understand that for the convenience and brevity of description, the specific working processes of the above-described systems, devices, and units can refer to the corresponding processes in the foregoing method embodiments, and will not be described herein again.

[0141] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place, or may be distributed to multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the solution of this embodiment. Those of ordinary skill in the art can understand and implement it without creative labor.

[0142] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on such an understanding, the essence of the above technical solution, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0143] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements for some of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A SQL statement generation method based on a large model and multi-stage iteration, characterized in that: include: Using advanced semantic modeling technology, we conduct in-depth analysis and processing of the target database's tables, fields, indexes, and relational structures, and build a multi-level database structure semantic embedding; The application intent recognition model deeply analyzes the user's natural language query request and extracts the query intent framework including the query purpose, operation object and condition restrictions by combining with the pre-trained large model; Through the calibration and correction model, the correction mechanism is guided by multi-stage feedback, and the query intent framework obtained by the preliminary analysis is combined with the semantic embedding of the database structure to correct the query intent. The calibration and correction model returns the error type and modification suggestions, and gradually optimizes the query intent and intent recognition model parameters. Using deep optimization strategies, we make fine-grained corrections to the corrected query intent and pre-trained large model parameters, combine them with the semantic embedding of the database structure, and use the retrieval strategy model to retrieve the best practice strategies generated by the pre-trained large model to form optimized SQL query statements. Through the adaptive evolution process and human preference feedback on the model output results, the DPO (direct preference optimization) algorithm is used to continuously optimize the parameters of the pre-trained large model and the retrieval strategy model, and the knowledge base and retrieval strategy of the large model are updated synchronously to obtain the optimized SQL query to generate the large model.

2. The method according to claim 1, characterized in that The calibration correction model is used to guide the correction mechanism with multi-stage feedback, and the query intent framework obtained by preliminary analysis is combined with the semantic embedding of the database structure to correct the query intent, including: Using graph neural networks, the query intent framework obtained through preliminary analysis is converted into a graph structure representation, and the database structure semantic embedding model is also converted into a graph structure representation; Apply structural similarity algorithm to quantify the similarity between query intent framework graph and database structure graph and identify mismatch points between the two. The attention mechanism is used in combination with mismatch points to accurately locate the difference between the query intent and the database structure, and a specific error type report is generated. The error type report includes the query object does not exist, the query condition is unclear, the field mapping error and the semantic understanding deviation; Use sequence-to-sequence models combined with error type reporting to generate more precise descriptions of query conditions; Combined with the feedback information provided by users and the evaluation of the effectiveness of the correction strategy, the reinforcement learning algorithm is used to dynamically adjust the query intent framework to ensure that the correction direction is always moving towards the goal of improving query accuracy; After each correction, the ensemble learning method is used to re-evaluate the matching degree between the corrected query intent framework and the database structure. If the matching degree does not meet the expected standard, the above correction process is repeated until the query intent framework fully conforms to the database structure or the preset maximum number of correction rounds is reached.

3. The method according to claim 2, characterized in that The query intent framework is dynamically adjusted using a reinforcement learning algorithm in combination with the feedback information provided by the user and the effect evaluation of the correction strategy to ensure that the correction direction is always moving towards the goal of improving the query accuracy, including: Collect user feedback information on satisfaction evaluation of the initially generated SQL query statements, errors pointed out and suggestions for improvement, and confirmation or denial of the revised query results; Perform sentiment analysis, keyword extraction, and semantic understanding preprocessing and quantification on user feedback information, and build a reward function based on the effect evaluation of the correction strategy; Use reinforcement learning algorithms to learn the optimal correction path through trial and error based on the constructed reward function, and dynamically adjust the correction strategy of the query intent framework; After each correction operation, an SQL query statement is generated and executed according to the new query intent framework to obtain the latest query results, and user feedback information is collected again to form a closed-loop feedback mechanism.

4. The method according to claim 3, characterized in that The deep optimization strategy is used to make fine-grained corrections to the corrected query intent and pre-trained large model parameters, and is combined with the semantic embedding of the database structure. The best practice strategy generated by the pre-trained large model is retrieved through the retrieval strategy model to form an optimized SQL query statement, including: Using the deep learning model, the key elements in the revised query intent framework are analyzed and processed in a fine-grained manner to enhance the semantic expression of key elements such as query purpose, operation object, and conditional restrictions in the query intent framework, thereby obtaining enhanced key elements of the query intent. Using the attention mechanism, the enhanced key elements of the query intent are dynamically weighted to highlight the important parts, reduce noise interference, improve the accuracy of the query intent, and obtain the optimized key elements of the query intent; Using the semantic embedding model of the database structure, a multi-dimensional feature vector is constructed for the optimized key elements of the query intent, including table structure features, field attribute features, relationship features, etc., to provide rich contextual information for subsequent optimization and obtain a multi-dimensional feature vector of the query intent. Using the retrieval strategy model, we retrieve the best historical best practice strategies that best match the multi-dimensional feature vector of the query intent from the pre-trained large model. These strategies include efficient query paths, optimized index usage, and reasonable connection methods, and obtain the best practice strategies that are suitable for the current query intent. Use best practice strategies to generate preliminary SQL query statements for query intent, and use SQL optimizers to perform syntax and logic verification and performance optimization on the preliminary generated SQL query statements to ensure the correctness and efficiency of the query statements. Finally, generate optimized SQL query statements and record all data and results during the optimization process to continuously update the pre-trained large model and retrieval strategy model to improve the generalization ability and adaptability of the model. Through the above-mentioned deep optimization strategy, the present invention can correct and optimize the query intent at a fine-grained level, ensuring that the generated SQL query statement not only accurately reflects the user's query intent, but also achieves optimal performance, thereby significantly improving the overall query efficiency of the system and user experience.

5. The method according to claim 1, characterized in that Using deep optimization strategies, we make fine-grained corrections to the corrected query intent and pre-trained large model parameters, combine them with the semantic embedding of the database structure, and use the retrieval strategy model to retrieve the best practice strategy generated by the pre-trained large model to form an optimized SQL query statement, including: Using deep learning models, we conduct fine-grained analysis and processing on the key elements in the revised query intent framework, enhance the semantic expression of the query purpose, operation object, and conditional restrictions in the query intent framework, and combine word embedding technology and sentence encoders to generate high-dimensional semantic vectors to obtain enhanced key elements of query intent. Through the adaptive attention mechanism, the attention weight is dynamically adjusted according to the complexity of the query intent, and the weight distribution of the key elements of the query intent is further optimized to obtain the optimized key elements of the query intent; Using the retrieval strategy model, retrieve the historical best practice strategy that best matches the multi-dimensional feature vector of the query intent from the pre-trained large model; A ranking learning algorithm is used to sort the retrieval results according to the execution effects of historical best practice strategies and user feedback, obtain the best practice strategy suitable for the current query intent, and perform preliminary SQL query statement generation processing for the query intent.

6. The method according to claim 5, characterized in that Using the ranking learning algorithm, the search results are sorted according to the execution effect of the historical best practice strategy and user feedback, the best practice strategy applicable to the current query intent is obtained, and preliminary SQL query statement generation and processing is performed on the query intent, including: Dynamic programming and greedy algorithms are used to optimize query paths, index usage, and connection methods to obtain a SQL optimizer; A sequence-to-sequence model is used to generate preliminary SQL query statements. The SQL optimizer performs syntax and logic verification and performance optimization on the preliminary generated SQL query statements to generate optimized SQL query statements. Record the query intent framework, generated SQL query statements, and optimization strategies during the optimization process.

7. The method according to claim 1, characterized in that The adaptive evolution process and human preference feedback on the model output results are used to continuously optimize the parameters of the pre-trained large model and the retrieval strategy model using the DPO algorithm, and the knowledge base and retrieval strategy of the large model are updated synchronously to obtain the optimized SQL query to generate the large model, which specifically includes: The initial values ​​and variation ranges of the model parameters are set through the adaptive evolution process. Genetic algorithms and particle swarm optimization methods are used to perform random mutation and crossover operations on the model parameters to find the optimal solution. Based on the set convergence conditions, it is determined whether the optimization process is terminated. Collect users' satisfaction ratings, modification opinions, and confirmation information on the SQL query statements of the model output results as user preference feedback; The DPO algorithm is used to process the collected user preference feedback to generate a reward signal. In each iteration, the model parameters are adjusted according to the reward signal. During the optimization process, the results of each parameter adjustment and user feedback are recorded to continuously enrich the knowledge base of the large model and update the best practice strategies in the retrieval strategy model.

8. A SQL statement generation method based on a large model and multi-stage iteration, characterized in that: include: The semantic embedding module uses advanced semantic modeling technology to conduct in-depth analysis and processing of the target database's tables, fields, indexes, and relational structures, and build a multi-level database structure semantic embedding; The query framework module uses the intent recognition model to deeply analyze the user's natural language query request, and extracts the query intent framework including the query purpose, operation object and condition restrictions in combination with the pre-trained large model; The calibration and correction module uses a calibration and correction model to guide the correction mechanism with multi-stage feedback, combines the query intent framework obtained from the preliminary analysis with the semantic embedding of the database structure to correct the query intent. The calibration and correction model returns the error type and modification suggestions, and gradually optimizes the query intent and intent recognition model parameters; The retrieval optimization module uses deep optimization strategies to make fine-grained corrections to the corrected query intent and pre-trained large model parameters, combines them with the semantic embedding of the database structure, and retrieves the best practice strategies generated by the pre-trained large model through the retrieval strategy model to form optimized SQL query statements; The reinforcement learning module uses the DPO algorithm to continuously optimize the parameters of the pre-trained large model and retrieval strategy model through an adaptive evolution process and human preference feedback on the model output results, and simultaneously updates the knowledge base and retrieval strategy of the large model to obtain the optimized SQL query to generate the large model.

9. A computing device, characterized in that It comprises a processing component and a storage component; the storage component stores one or more computer instructions; the one or more computer instructions are used to be called and executed by the processing component to implement the method according to any one of claims 1 to 7.

10. A computer storage medium, characterized in that: A computer program is stored, and when the computer program is executed by a computer, the method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Underground coal mine early warning data query method and system based on language large model

    CN118733608A

  • Vertical type government affair large model service method and system based on interactive learning

    CN118820448A

  • Data analysis method and system and storage medium

    CN119557424A

  • Enterprise knowledge base query method based on large language model

    CN119719345A

  • NL2SQL automatic prompting method and system based on retail commodity database

    CN119848076A

Cited By

  • Multi-source heterogeneous data intelligent matching system and method applied to department and wound supply chain online platform

    CN120597889A

  • Multi-modal grain data fusion and decision-making method based on artificial intelligence

    CN120743941A

  • Intelligent SQL (Structured Query Language) generation system based on multistage intention recognition and generation method thereof

    CN120910240A

  • Database query statement generation method based on retrieval enhancement and execution feedback

    CN121117021A

  • Cross-language retrieval method

    CN121117156A