Structured query language generation method and device, equipment and storage medium

By analyzing the user's natural language to generate structured data, and adjusting the model in combination with the database knowledge graph and policy gradient algorithm, the accuracy and adaptability problems of the large model in complex query scenarios are solved, and efficient and accurate SQL statement generation is achieved.

CN120371854AActive Publication Date: 2025-07-25INSPUR YUNZHOU (SHANDONG) IND INTERNET CO LTD

Patent Information

Application Number
CN202510865651.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-26
Publication Date
2025-07-25
Estimated Expiration
2045-06-26

AI Technical Summary

Technical Problem

Existing large models make mistakes in multi-table connections and miss key logic in complex query scenarios, and cannot effectively track the pre-order query status. The model hallucination problems are prominent, and lack the ability to adapt to the field, making it difficult to meet the high standard needs of enterprise-level data analysis.

Method used

By obtaining the user's natural language, using preset large-scale models to analyze and generate structured data, combining the database knowledge graph to determine the mapping relationship, adjust the model using the strategy gradient algorithm, and generate the target structured query language.

Benefits of technology

Improves the accuracy, stability and domain adaptability of SQL statement generation, and improves the user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120371854A_ABST
    Figure CN120371854A_ABST
Patent Text Reader

Abstract

The invention discloses a structured query language generation method and device, equipment and a storage medium, and relates to the technical field of database query, and the method comprises the following steps: obtaining a natural language input by a target user, and analyzing the natural language by using a preset large model to obtain structured data; determining a corresponding mapping relationship based on the structured data and a preset database knowledge graph, determining an initial structured query language based on a preset large model and the mapping relationship, and performing query by using the initial structured query language to obtain a query result; determining a first reward value based on the structured data, determining a second reward value based on the query result, and adjusting a preset large model by using the first reward value, the second reward value and a strategy gradient algorithm to obtain an adjusted large model; and evaluating the adjusted large model by utilizing a preset verification set, and if the evaluation is passed, generating a target structured query language by utilizing the adjusted large model. Therefore, the SQL generation accuracy can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of database query, and in particular to a structured query language generation method, device, equipment and storage medium. Background Art

[0002] In the era of big data, NL2SQL (Natural Language to Structured Query Language) technology has become a key technology for connecting user natural language and database operations, allowing non-technical users to perform data query and analysis conveniently and efficiently.

[0003] However, the current NL2SQL system based on large models has many technical bottlenecks in practical applications. In complex query scenarios, when dealing with multi-table joins and nested subqueries, the model often encounters problems such as multi-table join errors and missing key logic, which affects the accuracy and execution efficiency of SQL statements; in the face of multiple rounds of queries or context-related queries, the model cannot effectively track the status of previous queries and lacks awareness of dynamically changing database models; the model has a prominent hallucination problem, and will fabricate non-existent fields and table names, resulting in the inability to execute SQL statements; in addition, the data query logic in different fields varies greatly, and the existing large models are difficult to accurately understand the business logic of specific industries, lack domain adaptability, and are difficult to meet the high standards of enterprise-level data analysis.

[0004] Therefore, how to improve the accuracy, stability and domain adaptability of SQL statement generation is a technical problem that needs to be solved urgently. Summary of the invention

[0005] In view of this, the purpose of the present invention is to provide a structured query language generation method, device, equipment and storage medium, which can improve the accuracy, stability and domain adaptability of SQL statement generation. The specific scheme is as follows:

[0006] In a first aspect, the present application provides a structured query language generation method, comprising:

[0007] Obtaining natural language input by the target user based on the query requirements, and parsing the natural language using a preset large model to obtain structured data;

[0008] Determine a mapping relationship between a corresponding user query intention and a database structure based on the structured data and a preset database knowledge graph, determine an initial structured query language based on the preset large model and the mapping relationship, and use the initial structured query language to perform a query to obtain a query result;

[0009] Determine a first reward value based on the structured data, and determine a second reward value based on the query result. Use the first reward value, the second reward value, and the policy gradient algorithm to adjust the preset large model to obtain an adjusted large model;

[0010] Evaluate the adjusted large model using a preset validation set. If the evaluation passes, use the adjusted large model as the target large model to generate a target structured query language using the target large model.

[0011] Optionally, the determining the mapping relationship between the corresponding user query intent and the database structure based on the structured data and the preset database knowledge graph includes:

[0012] Perform synonym transcription on the initial text template and the preset business terms through an open-source large model to generate a target text template and a term dictionary;

[0013] Obtain libraries, tables, and fields related to the database from the target text template to determine corresponding first target entities, and correspond the first target entities with the preset business terms to obtain a corresponding relationship;

[0014] Construct a preset database knowledge graph based on the term dictionary, the first target entity, and the corresponding relationship, and use the structured data and the preset database knowledge graph to determine the mapping relationship between the corresponding user query intent and the database structure.

[0015] Optionally, the determining the first reward value based on the structured data includes:

[0016] Obtain libraries, tables, and fields related to the database from the structured data to determine corresponding second target entities;

[0017] Determine the precision rate and recall rate based on the second target entity, and use the precision rate and recall rate to determine the F1 score;

[0018] Determine the first reward value using a first preset weight coefficient, the F1 score, the precision rate, and the recall rate.

[0019] Optionally, the determining the second reward value based on the query result includes:

[0020] Determine the query success rate based on the query result, and determine the query time from the initiation of the query request to the acquisition of the query result;

[0021] Determine the second reward value using a second preset weight coefficient, the query success rate, and the query time.

[0022] Optionally, adjusting the preset large model by using the first reward value, the second reward value, and a policy gradient algorithm to obtain an adjusted large model includes:

[0023] Determining a target reward value based on the first reward value and the second reward value;

[0024] Determining the large model parameters used in generating the initial structured query language as the current policy. Under the current policy, determining the probability value of taking a target action when the state is fixed through the policy gradient algorithm, and using the backpropagation algorithm, the target reward value, the probability value, and a preset expected operation to determine a target gradient;

[0025] Wherein, the state is the environmental state at a target time step under the current policy; the target action is the action generated by the preset large model at the target time step;

[0026] Updating the model parameters of the preset large model by using the target gradient to obtain an adjusted large model.

[0027] Optionally, in the process of determining the target gradient by using the backpropagation algorithm, the target reward value, the probability value, and a preset expected operation, it further includes:

[0028] Iteratively determining a current gradient by using the backpropagation algorithm, the target reward value, the probability value, and a preset expected operation, and determining a maximum number of iterations based on the result of each iteration;

[0029] Updating the current gradient based on the backpropagation algorithm, the target reward value, the probability value, the preset expected operation, an early stopping mechanism, and the maximum number of iterations to obtain a target gradient.

[0030] Optionally, generating a target structured query language by using the target large model includes:

[0031] Determining single-table query, conditional filtering, and field aggregation as the first query requirements, and determining multi-table join, nested subquery, and dynamic schema change as the second query requirements;

[0032] When the query requirement of the target user is the first query requirement, generating a target structured query language based on a preset semantic matching rule and a template in a preset structured query language template library;

[0033] When the query requirement is the second query requirement, generating a target structured query language based on the target large model, the preset database knowledge graph, and prompt engineering.

[0034] In a second aspect, the present application provides a structured query language generation device, including:

[0035] A language parsing module, configured to obtain natural language input by a target user based on a query requirement, and parse the natural language by using a preset large model to obtain structured data;

[0036] A data query module, configured to determine a mapping relationship between a corresponding user query intention and a database structure based on the structured data and a preset database knowledge graph, determine an initial structured query language based on the preset large model and the mapping relationship, and perform a query by using the initial structured query language to obtain a query result;

[0037] A model adjustment module, configured to determine a first reward value based on the structured data, determine a second reward value based on the query result, and adjust the preset large model by using the first reward value, the second reward value, and a policy gradient algorithm to obtain an adjusted large model;

[0038] A language generation module, configured to evaluate the adjusted large model by using a preset validation set. If the evaluation passes, the adjusted large model is used as a target large model to generate a target structured query language by using the target large model.

[0039] In a third aspect, the present application provides an electronic device, including:

[0040] A memory, configured to store a computer program;

[0041] A processor, configured to execute the computer program to implement the foregoing structured query language generation method.

[0042] In a fourth aspect, the present application provides a computer-readable storage medium, configured to store a computer program; wherein, when the computer program is executed by a processor, the foregoing structured query language generation method is implemented.

[0043] In this application, natural language input by a target user based on a query requirement is obtained, and a preset large model is used to parse the natural language to obtain structured data; based on the structured data and a preset database knowledge graph, the mapping relationship between the corresponding user query intention and the database structure is determined, and an initial structured query language is determined based on the preset large model and the mapping relationship, and the initial structured query language is used for querying to obtain a query result; a first reward value is determined based on the structured data, and a second reward value is determined based on the query result, and the preset large model is adjusted using the first reward value, the second reward value, and a policy gradient algorithm to obtain an adjusted large model; the adjusted large model is evaluated using a preset validation set, and if the evaluation passes, the adjusted large model is used as the target large model to generate a target structured query language using the target large model. As can be seen from the above, when the target user inputs natural language based on a query requirement, a preset large model is called to parse it to generate structured data. Then, based on the structured data and the preset database knowledge graph, the mapping relationship between the user query intention and the database structure is determined, and an initial structured query language is generated in combination with the preset large model and this mapping relationship, and the query is executed to obtain a result. Subsequently, a first reward value and a second reward value are calculated based on the structured data and the query result respectively, and the parameters of the preset large model are adjusted through a policy gradient algorithm to obtain an adjusted large model. Finally, the adjusted large model is evaluated using a preset validation set, and if the evaluation meets the standard, it is determined as the target large model for subsequent generation of the target structured query language. In this way, this application can improve the accuracy, stability, and domain adaptability of SQL statement generation, thereby improving the user experience. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are only the embodiments of the present invention, and for those of ordinary skill in the art, other drawings can be obtained according to the provided drawings without creative efforts.

[0045] Figure 1 It is a flowchart of a method for generating a structured query language disclosed in this application;

[0046] Figure 2 It is a flowchart of a specific method for generating a structured query language disclosed in this application;

[0047] Figure 3 It is a schematic structural diagram of a device for generating a structured query language disclosed in this application;

[0048] Figure 4A structural diagram of an electronic device disclosed in this application. Detailed implementation manners

[0049] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.

[0050] Currently, there are many technical bottlenecks in the practical application of the NL2SQL system based on large models. In complex query scenarios, when dealing with multi-table joins and nested subqueries, the model often has problems such as multi-table join errors and omission of key logic, which affect the accuracy and execution efficiency of SQL statements; in the face of multi-round queries or context-related queries, the model cannot effectively track the previous query state and has insufficient perception of the dynamically changing database schema; the model hallucination problem is prominent, and it will fabricate non-existent fields, table names, etc., resulting in the inability to execute SQL statements; in addition, the data query logics in different fields vary greatly, and existing large models are difficult to accurately understand the business logics of specific industries and lack domain adaptability, making it difficult to meet the high standards of enterprise-level data analysis. Therefore, this application provides a method, device, equipment, and storage medium for generating structured query languages, which can improve the accuracy, stability, and domain adaptability of SQL statement generation.

[0051] See Figure 1 and Figure 2 As shown in

[0052] Step S11: Obtain the natural language input by the target user based on the query requirement, and parse the natural language by using a preset large model to obtain structured data.

[0053] In this embodiment, first, the natural language query text input by the user is captured in real time through an interaction interface, and this interface supports multi-modal input methods, including but not limited to keyboard input, speech-to-text input, etc., to adapt to the operation habits of different users. The captured original text needs to go through a preprocessing process, including removing redundant spaces, standardizing punctuation marks, converting simplified and traditional Chinese characters, etc., to ensure the format consistency of the input text.

[0054] The preset large model can use a pre-trained named entity recognition component to parse natural language, so as to extract database-related entities from natural language texts, including database names, table names, field names, numeric parameters, etc. For example, for the query "Count the names of customers whose order volume in each region exceeded 500 in 2023", the preset large model can identify "order volume" and "customer name" as field entities, "2023" and "500" as conditional parameters, and "each region" as the grouping basis. In the semantic parsing stage, the preset large model can generate a dependency syntax tree of the query text through syntactic analysis to clarify the grammatical relationships between sentence components.

[0055] To handle context dependencies in multi-turn conversations, the preset large model can maintain a dialogue state tracking module. Based on a recurrent neural network, this module structurally stores the query intent, recognized entities, and unresolved information gaps in historical conversations. When the user enters a new query, the model combines the current text with the historical dialogue state for cross-turn semantic disambiguation and entity reference resolution.

[0056] Finally, the parsed structured data, that is, the data covering key database elements such as databases, tables, and fields, retains both the semantic information of natural language and the logical structure for database operations, providing a certain information basis for generating accurate SQL statements.

[0057] Step S12: Determine the mapping relationship between the corresponding user query intent and the database structure based on the structured data and the preset database knowledge graph, and determine an initial structured query language based on the preset large model and the mapping relationship, and use the initial structured query language for querying to obtain a query result.

[0058] In this embodiment, the open-source large model is used to perform synonym rewriting on the initial text template and the preset business terms to generate a target text template and a term dictionary covering different expression dimensions. This process utilizes the semantic understanding ability of the large model to generate a corresponding set of synonyms for the input set of business terms, constructs a synonym mapping function to expand the semantic expression space. Then, the first target entities related to the database such as databases, tables, and fields are automatically extracted from the target text template, and the accuracy of entity extraction is improved through a domain-specific rule engine. The extracted first target entities are aligned with the business terms preset in the semantic layer to form the corresponding relationship between the first target entities and the business terms.

[0059] Next, based on the term dictionary, the first target entity, and the corresponding relationships, a preset database knowledge graph containing libraries, tables, fields, business terms, and their relationships is constructed. The preset database knowledge graph uses entity modeling to take tables, fields, foreign key relationships, and business terms in the database schema as nodes, and through relationship modeling, it establishes a many-to-many mapping between business terms and database fields and tables, supplements synonymous term relationships to enhance semantic associations, and also supports a knowledge update mechanism that periodically optimizes the graph structure from actual query data. Using the entities, conditions, and logical relationships in the structured data, combined with the entity associations and semantic rules stored in the knowledge graph, it parses the entity references, logical operations, and constraint conditions in the user's query intent, and establishes a mapping relationship between the query intent and the database table structure, field attributes, and association relationships.

[0060] Furthermore, an initial structured query language is generated through the preset large model and the obtained mapping relationship. When generating the initial structured query language, prompt engineering techniques can be used to guide the preset large model to output SQL statements that conform to the database syntax. The prompt template can include database schema information, mapping relationship descriptions, and query intent descriptions. By adjusting the structure and content of the prompt words, the generation direction of the large model can be controlled. To improve the generation quality, a few-shot learning strategy is introduced, and the large model is fine-tuned using a small number of labeled samples in the domain to enhance its understanding ability for domain-specific queries. Then, the initial SQL statement is executed through the database connection pool to obtain the query result. Performance metrics such as execution time and affected row count can be recorded in the query result to provide a basis for subsequent optimization.

[0061] Step S13: Determine the first reward value based on the structured data, and determine the second reward value based on the query result. Use the first reward value, the second reward value, and the policy gradient algorithm to adjust the preset large model to obtain an adjusted large model.

[0062] In this embodiment, first, second target entities are extracted from the structured data. The second target entities are the libraries, tables, and fields in the structured data that are related to the database. When determining the precision and recall rate, an entity matching algorithm is used to compare the identified second target entities with the database schema. The precision is calculated as the ratio of the number of correctly identified entities to the total number of identified entities, and the recall rate is calculated as the ratio of the number of correctly identified entities to the actual number of relevant entities in the database. To handle fuzzy matching scenarios, a semantic similarity threshold can be introduced. When the semantic similarity of the entity names exceeds the semantic similarity threshold, it is considered a successful match.

[0063] Then, the F1 score calculated using the precision and recall rate is used as an intermediate evaluation metric. Through the first preset weight coefficient for the F1 score , precision and recall rate Perform weighted combination to determine the first reward value to balance the importance of different evaluation dimensions. Among them, the calculation formula of the first reward value is . In the formula, , and are the weight coefficients for controlling the F1 score, recall rate, and precision rate respectively.

[0064] Meanwhile, determine the query success rate based on the query results, that is, the ratio of the number of successfully executed queries to the total number of queries, and record the query time from the initiation of the query request to the acquisition of the query results. Perform weighted combination on the query success rate and query time through the second preset weight coefficient to determine the second reward value. Among them, the calculation formula of the second reward value is . In the formula, and are the weight coefficients for controlling the query success rate and query time respectively.

[0065] Furthermore, fuse the first reward value and the second reward value to determine the final target reward value to comprehensively reflect the performance of the model in terms of both structured data understanding and query execution effect. Determine the large model parameters used in generating the initial structured query language as the current policy. Under this policy, calculate the probability value of taking the target action when the state is fixed through the policy gradient algorithm. Here, the state is the environmental state at the target time step under the current policy, including query intention representation, database schema information, etc.; the target action is the action generated by the preset large model at the target time step, that is, the specific SQL query statement. Use the backpropagation algorithm, target reward value, probability value, and preset expected operation to iteratively calculate the current gradient and determine the maximum number of iterations, and dynamically adjust the learning rate during the iteration to balance the training efficiency and stability. Among them, the gradient calculation formula is . In the formula, is the current policy, is the target reward value, is the expected operation under the current policy , is the probability value of taking action when the given state is under the current policy, is the gradient of the logarithm of the probability value with respect to the policy parameters, is the target gradient.

[0066] In the process of determining the target gradient, the backpropagation algorithm is used to gradually calculate and update the gradients of the model parameters in combination with the target reward value, action probability value, and preset expected operations. After each parameter update, the performance of the current model is evaluated, and the magnitude and trend of performance improvement are recorded. By analyzing the results of multiple rounds of iteration, the maximum number of iterations is dynamically determined to ensure that the model is trained sufficiently but not excessively.

[0067] In addition, an early stopping mechanism is introduced as an additional convergence control measure. After each iteration, the performance difference between the current model and the historical best model can be compared. If the performance improvement is lower than the preset threshold for multiple consecutive cycles, or if there is a trend of performance degradation, the early stopping mechanism is triggered to terminate the iteration process and restore to the historical best parameter state. At the same time, in combination with the preset upper limit of the maximum number of iterations, infinite training caused by data noise or model oscillation is prevented.

[0068] By integrating the backpropagation algorithm, target reward value, action probability distribution, preset optimization objective, early stopping strategy, and maximum number of iterations, etc., the gradient is dynamically adjusted and updated. In each iteration, according to the current reward feedback and model performance, the gradient direction and step size are adaptively adjusted to ensure that the model is optimized in the direction of improving the query generation quality. Finally, after multiple rounds of iteration and gradient update, a stable and excellent-performing target gradient is obtained for precise adjustment of the large model parameters. Finally, the model parameters of the preset large model are updated using the target gradient to obtain an adjusted large model, enabling it to more accurately understand user intentions and generate efficient SQL statements.

[0069] Step S14: Evaluate the adjusted large model using a preset validation set. If the evaluation passes, use the adjusted large model as the target large model to generate a target structured query language using the target large model.

[0070] In this embodiment, first, a multi-dimensional evaluation index is used to comprehensively verify the adjusted large model. In terms of query generation accuracy, by comparing the generated SQL with the manually annotated standard answers, the syntax error rate, semantic matching degree, and execution success rate are calculated; in terms of query efficiency, the average response time and resource consumption indicators are recorded; in terms of generalization ability, the model's ability to handle unseen business scenarios and complex queries is tested. The validation set covers various typical business queries, including single-table conditional queries, multi-table association analysis, aggregation statistics, etc., to ensure that the evaluation results are representative. If the evaluation results show that the evaluation passes, the adjusted large model is used as the target large model and the target large model is put into use.

[0071] It should be noted that after the target large model is put into use, the generation strategy can be dynamically selected according to the query complexity. For the first query requirements such as single-table query, conditional filtering, and field aggregation, the preset semantic matching rules and SQL template library are preferentially called. Through these templates, efficient SQL can be quickly generated. That is, the corresponding SQL statements can be instantiated directly from the template library through keyword matching and entity mapping.

[0072] For the second query requirements such as multi-table join, nested subquery, and dynamic schema change, an enhanced generation process is started. First, the query intention is parsed based on the preset database knowledge graph to construct a context representation including table relationships, field constraints, and business rules. Then, guiding words are designed through prompt engineering techniques to convert the query intention, database schema information, and semantic mapping rules into an input format understandable by the large model. Finally, the target large model combines these structured prompts to generate SQL queries that conform to complex business logics.

[0073] As can be seen from the above, when the target user inputs natural language based on the query requirements, the preset large model is called to parse it and generate structured data. Then, based on the structured data and the preset database knowledge graph, the mapping relationship between the user's query intention and the database structure is determined. Combining the preset large model and this mapping relationship, an initial structured query language is generated and the query is executed to obtain the result. Subsequently, the first reward value and the second reward value are calculated respectively based on the structured data and the query result, and the parameters of the preset large model are adjusted through the policy gradient algorithm to obtain the adjusted large model. Finally, the adjusted large model is evaluated using the preset validation set. If the evaluation meets the standard, it is determined as the target large model for subsequent generation of the target structured query language. In this way, this application can improve the accuracy, stability, and domain adaptability of SQL statement generation, thereby enhancing the user experience.

[0074] Next, the technical solutions of the embodiments of this application will be specifically described in combination with application scenarios.

[0075] Specifically, in the data query scenario of a large e-commerce platform, the operation staff needs to regularly analyze user behavior data to optimize the marketing strategy.

[0076] When the platform operator puts forward a natural language query requirement of "querying VIP users whose purchase amount exceeds 5,000 yuan within the past month and their repurchase times", the preset large model parses this natural language and identifies key entities and constraint conditions such as "within the past month", "purchase amount greater than 5,000 yuan", "VIP users", and "repurchase times". Through the entity recognition module, "within the past month" is converted into a specific time range, "VIP users" are mapped to the membership level field in the user table, and "repurchase times" are mapped to the aggregated statistics of the order table. Finally, structured data containing user ID, order time, amount threshold, membership level, etc. is generated, providing a clear data structure for subsequent processing.

[0077] Then, based on the generated structured data and the preset database knowledge graph, the mapping relationship between the user's query intention and the database structure is constructed. The table structures and association relationships such as the user table, order table, and product table are pre-stored in the knowledge graph. "VIP users" correspond to the user_level field in the user table, and "purchase amount" corresponds to the order_amount field in the order table. Analyze the query intention to determine that VIP user information needs to be obtained from the user table, transaction records need to be obtained from the order table and the repurchase times need to be calculated, and a mapping relationship between the two tables associated through user_id is established.

[0078] Furthermore, based on this mapping relationship, the preset large model generates an initial SQL query, which is sent to the database for execution, and returns the VIP users who meet the conditions and their repurchase times, providing preliminary analysis data for the operator.

[0079] Next, calculate the first reward value based on the structured data: evaluate the extraction accuracy of entities such as "VIP users" and "within the past month" through the entity recognition accuracy evaluation model, calculate the F1 score in combination with the recall rate, and then comprehensively obtain the first reward value through the preset weight coefficient, reflecting the accuracy of the model's understanding of the query requirement. At the same time, calculate the second reward value based on the query result. Specifically, the query success rate measures whether the generated SQL can be correctly executed, and the query time evaluates the execution efficiency. The two are combined through the preset weight combination to obtain the second reward value. Combining these two reward values, use the policy gradient algorithm to adjust the parameters of the preset large model. In multiple iterations, the model continuously learns how to better understand natural language queries and generate efficient SQL, and finally obtains the adjusted large model.

[0080] Use the preset validation set for comprehensive testing. The validation set includes various typical queries, such as single-table conditional queries and multi-table association analysis. For complex queries such as "querying VIP users whose purchase amount exceeds 5,000 yuan within the past month and their repurchase times", the evaluation indicators include query generation accuracy, execution success rate, response time, etc. If the adjusted large model meets or exceeds the preset threshold in all indicators, it is confirmed as the target large model.

[0081] In practical applications, the target large model adopts different generation strategies according to the query complexity. For simple single-table query requirements, such as "query active users in the Shanghai area", the preset semantic matching rules and SQL template library are directly called to quickly generate efficient SQL. For complex multi-table association requirements, such as "analyze the sales conversion rate of each category of products in different promotional activities", an enhanced generation process is started. First, the query intent is parsed based on the knowledge graph to clarify the need to associate the product table, promotional activity table, and order table; then, guiding words containing table structures, field relationships, and business rules are designed through prompt engineering; finally, the target large model generates a complex SQL query containing multi-table joins, aggregate functions, and conditional filtering.

[0082] Correspondingly, as shown in Figure 3 the embodiments of the present application provide a structured query language generation device, including:

[0083] A language parsing module 11, configured to obtain the natural language input by the target user based on the query requirement, and parse the natural language using a preset large model to obtain structured data;

[0084] A data query module 12, configured to determine the mapping relationship between the corresponding user query intent and the database structure based on the structured data and the preset database knowledge graph, and determine an initial structured query language based on the preset large model and the mapping relationship, and perform a query using the initial structured query language to obtain a query result;

[0085] A model adjustment module 13, configured to determine a first reward value based on the structured data, and determine a second reward value based on the query result, and adjust the preset large model using the first reward value, the second reward value, and the policy gradient algorithm to obtain an adjusted large model;

[0086] A language generation module 14, configured to evaluate the adjusted large model using a preset validation set, and if the evaluation passes, use the adjusted large model as the target large model to generate a target structured query language using the target large model.

[0087] As can be seen from the above, when the target user inputs natural language based on the query requirement, a preset large model is called to parse it and generate structured data. Then, based on the structured data and the preset database knowledge graph, the mapping relationship between the user's query intention and the database structure is determined. Next, an initial structured query language is generated by combining the preset large model and this mapping relationship, and the query is executed to obtain the result. Subsequently, the first reward value and the second reward value are calculated based on the structured data and the query result respectively, and the parameters of the preset large model are adjusted through the policy gradient algorithm to obtain the adjusted large model. Finally, the adjusted large model is evaluated using the preset validation set. If the evaluation meets the standard, it is determined as the target large model for generating the subsequent target structured query language. In this way, the present application can improve the accuracy, stability, and domain adaptability of SQL statement generation, thereby enhancing the user experience.

[0088] In some specific embodiments, the data query module 12 specifically includes:

[0089] An information generation unit for performing synonym transcription on the initial text template and the preset business terms through an open-source large model to generate a target text template and a term dictionary;

[0090] A correspondence determination unit for obtaining the libraries, tables, and fields related to the database from the target text template to determine the corresponding first target entities, and corresponding the first target entities and the preset business terms to obtain a correspondence;

[0091] A mapping relationship determination unit for constructing a preset database knowledge graph based on the term dictionary, the first target entities, and the correspondence, and using the structured data and the preset database knowledge graph to determine the mapping relationship between the corresponding user query intention and the database structure.

[0092] In some specific embodiments, the model adjustment module 13 specifically includes:

[0093] An entity determination unit for obtaining the libraries, tables, and fields related to the database from the structured data to determine the corresponding second target entities;

[0094] A score determination unit for determining the precision rate and the recall rate based on the second target entities, and using the precision rate and the recall rate to determine the F1 score;

[0095] A first reward value determination unit for determining the first reward value using the first preset weight coefficient, the F1 score, the precision rate, and the recall rate.

[0096] In some specific embodiments, the model adjustment module 13 specifically includes:

[0097] A time determination unit for determining a query success rate based on the query result and determining a query time from initiating a query request to obtaining the query result;

[0098] A second reward value determination unit for determining a second reward value by using a second preset weight coefficient, the query success rate, and the query time.

[0099] In some specific embodiments, the model adjustment module 13 specifically includes:

[0100] A target reward value determination unit for determining a target reward value based on the first reward value and the second reward value;

[0101] A gradient determination unit for determining the large model parameters used in generating the initial structured query language as the current policy. Under the current policy, determining the probability value of taking a target action when the state is fixed through a policy gradient algorithm, and using a backpropagation algorithm, the target reward value, the probability value, and a preset expected operation to determine a target gradient;

[0102] Wherein, the state is the environmental state at the target time step under the current policy; the target action is the action generated by the preset large model at the target time step;

[0103] A model adjustment unit for updating the model parameters of the preset large model by using the target gradient to obtain an adjusted large model.

[0104] In some specific embodiments, the gradient determination unit specifically further includes:

[0105] An iteration number determination sub-unit for iteratively determining the current gradient by using a backpropagation algorithm, the target reward value, the probability value, and a preset expected operation, and determining the maximum iteration number based on the results of each iteration;

[0106] A gradient update sub-unit for updating the current gradient based on the backpropagation algorithm, the target reward value, the probability value, the preset expected operation, an early stopping mechanism, and the maximum iteration number to obtain a target gradient.

[0107] In some specific embodiments, the language generation module 14 specifically includes:

[0108] A requirement determination unit for determining single-table query, conditional filtering, and field aggregation as the first query requirement, and determining multi-table join, nested subquery, and dynamic mode change as the second query requirement;

[0109] A first language generation unit, configured to, when the query requirement of the target user is the first query requirement, generate a target structured query language based on a preset semantic matching rule and a template in a preset structured query language template library;

[0110] A second language generation unit, configured to, when the query requirement is the second query requirement, generate a target structured query language based on the target large model, the preset database knowledge graph, and prompt engineering.

[0111] Furthermore, an embodiment of the present application also discloses an electronic device. Figure 4 It is a structural diagram of an electronic device 20 shown according to an exemplary embodiment. The content in the figure cannot be considered as any limitation to the scope of use of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. Among them, the memory 22 is used to store a computer program, and the computer program is loaded and executed by the processor 21 to implement the relevant steps in the structured query language generation method disclosed in any of the foregoing embodiments. In addition, the electronic device 20 in this embodiment may specifically be an electronic computer.

[0112] In this embodiment, the power supply 23 is used to provide operating voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows is any communication protocol applicable to the technical solution of the present application, and specific limitations are not imposed here; the input / output interface 25 is used to obtain external input data or output data to the outside, and its specific interface type can be selected according to specific application needs, and specific limitations are not imposed here.

[0113] In addition, as a carrier for resource storage, the memory 22 may be a read-only memory, a random access memory, a magnetic disk, or an optical disc, etc. The resources stored thereon may include an operating system 221, a computer program 222, etc., and the storage method may be short-term storage or permanent storage.

[0114] Among them, the operating system 221 is used to manage and control each hardware device on the electronic device 20 and the computer program 222, and it may be Windows Server, Netware, Unix, Linux, etc. The computer program 222 may further include a computer program capable of completing other specific tasks in addition to the computer program capable of implementing the structured query language generation method executed by the electronic device 20 disclosed in any of the foregoing embodiments.

[0115] Further, the present application also discloses a computer-readable storage medium for storing a computer program. When the computer program is executed by a processor, it implements the structured query language generation method disclosed above. For the specific steps of this method, reference can be made to the corresponding content disclosed in the foregoing embodiments, and details will not be repeated herein.

[0116] The embodiments in this specification are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. For the same or similar parts among the embodiments, reference can be made to each other. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple. For related parts, reference can be made to the description in the method part.

[0117] Those skilled in the art can further realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed in this article can be implemented by electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of each example have been generally described according to their functions in the above description. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present application.

[0118] The steps of the methods or algorithms described in combination with the embodiments disclosed in this article can be directly implemented by hardware, software modules executed by a processor, or a combination of both. The software modules can be placed in a random access memory (RAM), internal memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium well-known in the technical field.

[0119] Finally, it should also be noted that in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements not only includes those elements, but also includes other elements not expressly listed, or elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "comprising a..." does not exclude the existence of additional identical elements in the process, method, article or device comprising the element.

[0120] The above has introduced the technical solution provided by this application in detail. Specific examples are used in this article to elaborate on the principle and implementation manner of this application. The description of the above embodiments is only used to help understand the method and its core idea of this application; at the same time, for those of ordinary skill in the art, according to the idea of this application, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to this application.

Claims

1. A method for generating a Structured Query Language, characterized in that, Including: Obtain the natural language input by the target user based on the query requirement, and use a preset large model to parse the natural language to obtain structured data; Based on the structured data and the preset database knowledge graph, determine the mapping relationship between the corresponding user query intention and the database structure, and based on the preset large model and the mapping relationship, determine the initial structured query language, and use the initial structured query language to query to obtain a query result; Determine a first reward value based on the structured data, and determine a second reward value based on the query result. Use the first reward value, the second reward value, and the policy gradient algorithm to adjust the preset large model to obtain an adjusted large model; Use a preset validation set to evaluate the adjusted large model. If the evaluation passes, use the adjusted large model as the target large model to generate the target structured query language.

2. The method for generating a structured query language according to claim 1, wherein The determining the mapping relationship between the corresponding user query intention and the database structure based on the structured data and the preset database knowledge graph includes: Perform synonym transcription on the initial text template and preset business terms through an open-source large model to generate a target text template and a term dictionary; Obtain the libraries, tables, and fields related to the database from the target text template to determine the corresponding first target entities, and correspond the first target entities with the preset business terms to obtain a corresponding relationship; Construct a preset database knowledge graph based on the term dictionary, the first target entity, and the corresponding relationship, and use the structured data and the preset database knowledge graph to determine the mapping relationship between the corresponding user query intention and the database structure.

3. The method for generating a structured query language according to claim 1, wherein The determining the first reward value based on the structured data includes: Obtain the libraries, tables, and fields related to the database from the structured data to determine the corresponding second target entities; Determine the precision rate and recall rate based on the second target entity, and use the precision rate and recall rate to determine the F1 score; Use the first preset weight coefficient, the F1 score, the precision rate, and the recall rate to determine the first reward value.

4. The method for generating a structured query language according to claim 1, wherein, The determining the second reward value based on the query result includes: Determine the query success rate based on the query result, and determine the query time from the initiation of the query request to the obtaining of the query result; Use the second preset weight coefficient, the query success rate, and the query time to determine the second reward value.

5. The method for generating a structured query language according to claim 1, wherein The using the first reward value, the second reward value, and the policy gradient algorithm to adjust the preset large model to obtain an adjusted large model includes: Determine the target reward value based on the first reward value and the second reward value; Determine the large model parameters used in generating the initial structured query language as the current policy. Under the current policy, determine the probability value of taking the target action when the state is fixed through the policy gradient algorithm, and use the backpropagation algorithm, the target reward value, the probability value, and the preset expected operation to determine the target gradient; Wherein, the state is the environmental state at the target time step under the current policy; the target action is the action generated by the preset large model at the target time step; Update the model parameters of the preset large model using the target gradient to obtain an adjusted large model.

6. The method for generating a structured query language according to claim 5, characterized in that, In the process of determining the target gradient using the backpropagation algorithm, the target reward value, the probability value, and the preset expectation operation, it further includes: Iteratively determine the current gradient using the backpropagation algorithm, the target reward value, the probability value, and the preset expectation operation, and determine the maximum number of iterations based on the results of each iteration; Update the current gradient based on the backpropagation algorithm, the target reward value, the probability value, the preset expectation operation, the early stopping mechanism, and the maximum number of iterations to obtain the target gradient.

7. The method for generating a structured query language according to any one of claims 1 to 6, characterized in that The generating of the target structured query language using the target large model includes: Determine the single-table query, conditional filtering, and field aggregation as the first query requirements, and determine the multi-table join, nested subquery, and dynamic schema change as the second query requirements; When the query requirement of the target user is the first query requirement, generate the target structured query language based on the preset semantic matching rules and the templates in the preset structured query language template library; When the query requirement is the second query requirement, generate the target structured query language based on the target large model, the preset database knowledge graph, and prompt engineering.

8. A structured query language generation device, characterized in that, It includes: A language parsing module, configured to obtain the natural language input by the target user based on the query requirement, and parse the natural language using the preset large model to obtain structured data; A data query module, configured to determine the mapping relationship between the corresponding user query intent and the database structure based on the structured data and the preset database knowledge graph, determine the initial structured query language based on the preset large model and the mapping relationship, and perform a query using the initial structured query language to obtain a query result; A model adjustment module, configured to determine a first reward value based on the structured data, determine a second reward value based on the query result, and adjust the preset large model using the first reward value, the second reward value, and the policy gradient algorithm to obtain an adjusted large model; A language generation module, configured to evaluate the adjusted large model using a preset validation set. If the evaluation passes, use the adjusted large model as the target large model to generate the target structured query language using the target large model.

9. An electronic device, characterized in that, It includes: A memory, configured to store a computer program; A processor, configured to execute the computer program to implement the structured query language generation method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, For storing a computer program; wherein, when the computer program is executed by the processor, it implements the structured query language generation method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Neural network based translation of natural language queries to database queries

    CN110945495A

  • Data query method, server, node and storage medium

    CN114840574A

  • Method for converting full-link natural language into structured query language

    CN118227653A

  • Digital factory data query method, device, equipment, medium and product

    CN119025618A

  • Query method and system for generating SQL statement based on natural language

    CN119377241A

Cited By

  • Large language model optimization method and optimization device

    CN120633740A

  • Session data loading method and device based on structured storage, equipment and medium

    CN120763228A

  • Data query method and device based on large language model, program product and medium

    CN120804298A

  • User problem processing method and device based on serial and parallel decoding, equipment and medium

    CN121210626A

  • Method and device for realizing automatic construction of reward model through computing power by intelligent computing cloud platform

    CN121388002A