A structured query language generation method, device, equipment and storage medium

By parsing users' natural language to generate structured data, and combining database knowledge graphs and policy gradient algorithms to adjust the large model, the accuracy and domain adaptability issues of the NL2SQL system in complex query scenarios are solved, achieving efficient and stable SQL statement generation.

CN120371854BActive Publication Date: 2025-10-21INSPUR YUNZHOU (SHANDONG) IND INTERNET CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

Existing NL2SQL systems based on large models suffer from multi-table join errors, omission of key logic, model illusion problems, and lack of domain adaptability in complex query scenarios. This results in insufficient accuracy and execution efficiency of SQL statements, making it difficult to meet enterprise-level data analysis needs.

Method used

By acquiring users' natural language, using a pre-set large model to parse and generate structured data, combining database knowledge graphs to determine mapping relationships, using a policy gradient algorithm to adjust the large model, generating the target structured query language, and introducing a validation set to evaluate the model's accuracy and stability.

Benefits of technology

It improves the accuracy, stability, and domain adaptability of generated SQL statements, enhances the user experience, and can handle complex and multi-round query scenarios, adapting to specific industry business logic.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120371854B_ABST
    Figure CN120371854B_ABST
Patent Text Reader

Abstract

The application discloses a structured query language generation method and device, equipment and a storage medium, and relates to the technical field of database query. The method comprises the following steps: obtaining 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, and determining an initial structured query language based on the preset large model and the mapping relationship, and querying by using the initial structured query language to obtain a query result; determining a first reward value based on the structured data, and determining a second reward value based on the query result, and 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; and evaluating the adjusted large model by using a preset verification set, and if the evaluation is passed, generating a target structured query language by using the adjusted large model. Therefore, the application can improve the accuracy of SQL generation.
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, apparatus, device 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 faces numerous technical bottlenecks in practical applications. In complex query scenarios, when processing multi-table joins and nested subqueries, the model often encounters problems such as multi-table join errors and omissions of key logic, affecting the accuracy and execution efficiency of SQL statements. Faced with multiple rounds of queries or context-dependent queries, the model cannot effectively track the status of previous queries and lacks awareness of dynamically changing database schemas. Model hallucinations are a prominent issue, with the model inventing non-existent fields and table names, resulting in SQL statement execution failures. Furthermore, data query logic varies significantly across different domains, making it difficult for existing large models to accurately understand the business logic of specific industries. They lack domain adaptability and struggle to meet the high standards required for 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, apparatus, device, and storage medium that can improve the accuracy, stability, and domain adaptability of SQL statement generation. The specific solution is as follows:

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

[0007] Obtain the natural language input by the target user based on the query requirements, and use the preset large model to parse the natural language to obtain structured data;

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

[0009] Determining a first reward value based on the structured data, and determining a second reward value based on the query result, and adjusting the preset large model using the first reward value, the second reward value, and a policy gradient algorithm to obtain an adjusted large model;

[0010] The adjusted large model is evaluated 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 using the target large model.

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

[0012] Synonymically transcribe the initial text template and preset business terms through an open source big model to generate the target text template and term dictionary;

[0013] Obtaining a database-related library, table, and field from the target text template to determine a corresponding first target entity, and matching the first target entity with the preset business term to obtain a corresponding relationship;

[0014] A preset database knowledge graph is constructed based on the term dictionary, the first target entity and the corresponding relationship, and the mapping relationship between the corresponding user query intention and the database structure is determined using the structured data and the preset database knowledge graph.

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

[0016] Acquire database-related libraries, tables, and fields from the structured data to determine a corresponding second target entity;

[0017] Determining precision and recall based on the second target entity, and determining an F1 score using the precision and recall;

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

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

[0020] 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;

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

[0022] Optionally, the adjusting the preset large model 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, determining the probability value of taking the target action when the state is fixed using a policy gradient algorithm under the current policy, and determining the target gradient using a backpropagation algorithm, the target reward value, the probability value, and a preset expected operation;

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

[0026] The target gradient is used to update the model parameters of the preset large model to obtain an adjusted large model.

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

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

[0029] The current gradient is updated based on the back propagation algorithm, the target reward value, the probability value, the preset expected operation, the early stopping mechanism, and the maximum number of iterations to obtain a target gradient.

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

[0031] Single-table query, conditional filtering, and field aggregation are identified as the first query requirement, and multi-table join, nested subquery, and dynamic schema change are identified as the second query requirement;

[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, a target structured query language is generated based on the target big model, the preset database knowledge graph and the prompt project.

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

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

[0036] A data query module is used to determine the mapping relationship between the corresponding user query intention and the database structure based on the structured data and the preset database knowledge graph, and to determine an initial structured query language based on the preset large model and the mapping relationship, and to perform a query 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 using the first reward value, the second reward value, and a policy gradient algorithm to obtain an adjusted large model;

[0038] The language generation module is used to evaluate the adjusted large model using a preset validation set. 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.

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

[0040] Memory, used to store computer programs;

[0041] The processor is configured to execute the computer program to implement the aforementioned structured query language generation method.

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

[0043] In this application, the natural language input by the target user based on the query requirements is obtained, and the natural language is parsed using a preset large model to obtain structured data; the mapping relationship between the corresponding user query intention and the database structure is determined based on the structured data and the preset database knowledge graph, and the initial structured query language is determined based on the preset large model and the mapping relationship, and the query is performed using the initial structured query language to obtain the query result; the first reward value is determined based on the structured data, and the 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 the policy gradient algorithm to obtain the 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 the 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 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, and then the initial structured query language is generated in combination with 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 the subsequent generation of the 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. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are merely embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.

[0045] Figure 1 A flow chart of a structured query language generation method disclosed in this application;

[0046] Figure 2 A flow chart of a specific structured query language generation method disclosed in this application;

[0047] Figure 3 This is a schematic diagram of the structure of a structured query language generation device disclosed in this application;

[0048] Figure 4This is a structural diagram of an electronic device disclosed in this application. DETAILED DESCRIPTION

[0049] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.

[0050] The current NL2SQL system based on a large model 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 omissions of key logic, which affect 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 schemas; the model hallucination problem is prominent, and it will fabricate non-existent fields and table names, etc., resulting in the inability to execute SQL statements; in addition, the data query logic in different fields varies greatly, and 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. To this end, the present application provides a structured query language generation method, device, equipment, and storage medium that can improve the accuracy, stability, and domain adaptability of SQL statement generation.

[0051] See also Figure 1 and Figure 2 As shown, an embodiment of the present invention discloses a method for generating a structured query language, comprising:

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

[0053] In this embodiment, natural language query text entered by the user is first captured in real time through an interactive interface. This interface supports multimodal input methods, including but not limited to keyboard input and voice-to-text input, to accommodate different user habits. The captured raw text undergoes a preprocessing process, including removing redundant spaces, standardizing punctuation, and converting between simplified and traditional Chinese characters, to ensure consistent formatting.

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

[0055] To handle contextual dependencies across multiple conversations, the pre-built large model maintains a conversation state tracking module. This module, based on a recurrent neural network, structures and stores query intent, identified entities, and unresolved information gaps from past conversations. When a user enters a new query, the model combines the current text with the past conversation state to perform semantic disambiguation and entity reference resolution across turns.

[0056] Ultimately, the structured data obtained through parsing, that is, data covering key database elements such as libraries, tables, and fields, retains the semantic information of natural language and has the logical structure of database operations, providing a certain information basis for generating accurate SQL statements.

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

[0058] In this embodiment, an open-source big model is used to perform synonymous transcription on the initial text template and preset business terms to generate a target text template and term dictionary covering different expression dimensions. This process utilizes the semantic understanding capabilities of the big model to generate a corresponding synonym set for the input business term set and construct a synonym mapping function to expand the semantic expression space. Then, the first target entity, such as the database-related library, table, and field, is automatically extracted from the target text template. The accuracy of entity extraction is improved through a domain-specific rule engine. The extracted first target entity is aligned with the business term preset in the semantic layer to form a corresponding relationship between the first target entity and the business term.

[0059] Next, based on the term dictionary, the first target entity and the corresponding relationship, a preset database knowledge graph is constructed, which includes libraries, tables, fields, business terms and their relationships. 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 establishes a many-to-many mapping between business terms and database fields and tables through relationship modeling. It also supplements synonymous term relationships to enhance semantic associations, and supports a knowledge update mechanism that regularly optimizes the graph structure from actual query data. By utilizing the entities, conditions and logical relationships in structured data, combined with the entity associations and semantic rules stored in the knowledge graph, the entity references, logical operations and constraints in the user's query intent are parsed, and a mapping relationship between the query intent and the database table structure, field attributes and association relationships is established.

[0060] Furthermore, an initial structured query language is generated by presetting the large model and the mapping relationship obtained above. When generating the initial structured query language, prompt engineering technology can be used to guide the preset large model to output SQL statements that conform to the database syntax. The prompt template can contain database schema information, mapping relationship description and query intent description, and the generation direction of the large model can be controlled by adjusting the structure and content of the prompt words. In order to improve the generation quality, a few-sample learning strategy is introduced to fine-tune the large model using a small number of labeled samples in the field to enhance its understanding of queries in specific fields. Then, the initial SQL statement is executed through the database connection pool to obtain the query results. Performance indicators such as execution time and number of affected rows can be recorded in the query results to provide a basis for subsequent optimization.

[0061] Step S13: determining a first reward value based on the structured data, and determining a second reward value based on the query result; and adjusting the preset large model using the first reward value, the second reward value, and a policy gradient algorithm to obtain an adjusted large model.

[0062] In this embodiment, first, a second target entity is extracted from the structured data. The second target entity is a library, table, or field in the structured data that is related to the database. When determining the precision and recall, an entity matching algorithm is used to compare the identified second target entity with the database pattern. The precision is calculated as the ratio of the number of correctly identified entities to the total number of identified entities, and the recall is calculated as the ratio of the number of correctly identified entities to the number of related entities actually existing in the database. In order to handle fuzzy matching scenarios, a semantic similarity threshold can be introduced. When the semantic similarity of the entity name exceeds the semantic similarity threshold, the match is considered successful.

[0063] Then, the F1 score is calculated using the precision and recall rate as an intermediate evaluation indicator. The F1 score is weighted by the first preset weight coefficient. , precision and recall rate Perform weighted combination to determine the first reward value to balance the importance of different evaluation dimensions. The calculation formula for the first reward value is: Where, 、 as well as They are the weight coefficients for controlling the F1 score, recall rate, and precision rate respectively.

[0064] At the same time, the query success rate is determined based on the query results , which is the ratio of the number of successfully executed queries to the total number of queries, and records the query time from initiating the query request to obtaining the query result The query success rate and query time are weighted and combined by the second preset weight coefficient to determine the second reward value. The calculation formula of the second reward value is: Where, and are the weight coefficients for controlling the query success rate and query time respectively.

[0065] Furthermore, the first reward value and the second reward value are fused to determine the final target reward value to comprehensively reflect the performance of the model in terms of structured data understanding and query execution effect. The large model parameters used in generating the initial structured query language are determined as the current policy. Under this policy, the probability value of taking the target action when the state is fixed is calculated by the policy gradient algorithm. The state here is the environmental state of the target time step under the current policy, including query intent representation, database schema information, etc.; the target action is the action generated by the preset large model at the target time step, that is, a specific SQL query statement. Using the backpropagation algorithm, target reward value, probability value and preset expected operation, the current gradient is iteratively calculated and the maximum number of iterations is determined. The learning rate is dynamically adjusted during the iteration process to balance training efficiency and stability. Among them, the gradient calculation formula is Where, For the current strategy, is the target reward value, For the current strategy The expected operation under Given a state under the current strategy Take action The probability value of To take the logarithm of the probability value and then find the gradient with respect to the policy parameters, is the target gradient.

[0066] To determine the target gradient, we use a backpropagation algorithm, combining the target reward, action probability, and pre-set expected action, to gradually calculate and update the gradient of the model parameters. After each parameter update, we evaluate the current model's performance, recording the magnitude and trend of performance improvement. By analyzing the results of multiple iterations, we dynamically determine the maximum number of iterations to ensure that the model is fully trained without overtraining.

[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 falls below a preset threshold or a downward trend is observed over multiple consecutive cycles, the early stopping mechanism is triggered, terminating the iteration process and restoring the parameters to the historical best state. Furthermore, a preset maximum number of iterations is incorporated to prevent infinite training caused by data noise or model oscillation.

[0068] By integrating the backpropagation algorithm, target reward value, action probability distribution, preset optimization target, early stopping strategy, and maximum number of iterations, the gradient is dynamically adjusted and updated. In each iteration, the gradient direction and step size are adaptively adjusted based on the current reward feedback and model performance, ensuring that the model is optimized towards improving query generation quality. Ultimately, after multiple rounds of iterations and gradient updates, a stable and high-performance target gradient is obtained, which is used to accurately adjust the parameters of the large model. Finally, the target gradient is used to update the model parameters of the preset large model, resulting in an adjusted large model that can more accurately understand user intent 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, a multi-dimensional evaluation index is first used to comprehensively verify the adjusted large model. In terms of query generation accuracy, the syntax error rate, semantic matching degree and execution success rate are calculated by comparing the generated SQL with the manually annotated standard answer; 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, aggregate statistics, etc., to ensure that the evaluation results are representative. If the evaluation results indicate that the evaluation is passed, the adjusted large model will be used as the target large model, and the target large model will be put into use.

[0071] It's important to note that once the target large model is operational, generation strategies can be dynamically selected based on query complexity. For primary query requirements like single-table queries, conditional filtering, and field aggregation, pre-set semantic matching rules and SQL template libraries are prioritized. These templates enable rapid generation of efficient SQL statements. Specifically, corresponding SQL statements can be instantiated directly from the template library through keyword matching and entity mapping.

[0072] For secondary query requirements such as multi-table joins, nested subqueries, and dynamic schema changes, an enhanced generation process is initiated. First, the query intent is parsed based on the pre-set database knowledge graph, constructing a contextual representation that includes table relationships, field constraints, and business rules. Then, by prompting engineering design guidance words, the query intent, database schema information, and semantic mapping rules are converted 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 logic.

[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, and then the initial structured query language is generated in combination with 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 standards, it is determined as the target large model for the subsequent generation of the 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.

[0074] The technical solutions of the embodiments of the present application are described in detail below in conjunction with application scenarios.

[0075] Specifically, in a data query scenario on a large e-commerce platform, operators need to regularly analyze user behavior data to optimize marketing strategies.

[0076] When a platform operator raises a natural language query request such as "Query VIP users whose purchases exceed 5,000 yuan in the past month and their number of repeat purchases," a preset large model parses the natural language and identifies key entities and constraints such as "past month," "purchase amount greater than 5,000 yuan," "VIP users," and "number of repeat purchases." The entity recognition module converts "past month" into a specific time range, maps "VIP users" to the membership level field in the user table, and maps "number of repeat purchases" to the aggregate statistics of the order table. This ultimately generates structured data containing information such as user ID, order time, amount threshold, and membership level, providing a clear data structure for subsequent processing.

[0077] Then, based on the generated structured data and the pre-set database knowledge graph, a mapping relationship between user query intent and database structure is constructed. The knowledge graph already contains the table structures and relationships for user, order, and product tables. "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. After analyzing the query intent, it is determined that VIP user information needs to be retrieved from the user table, and transaction records and repeat purchase counts need to be obtained from the order table. A mapping relationship is then established, linking the two tables using the user_id field.

[0078] Furthermore, based on this mapping relationship, the preset large model generates an initial SQL query, which is sent to the database for execution, returning qualified VIP users and their number of repurchases, providing preliminary analysis data for operations personnel.

[0079] Next, the first reward value is calculated based on the structured data: the entity recognition accuracy evaluation model is used to evaluate the extraction accuracy of entities such as "VIP users" and "last month", and the F1 score is calculated in combination with the recall rate. The first reward value is then synthesized using the preset weight coefficients to reflect the accuracy of the model's understanding of the query requirements. At the same time, the second reward value is calculated based on the query results. Specifically, the query success rate measures whether the generated SQL can be executed correctly, and the query time evaluates the execution efficiency. The two are combined using preset weights to obtain the second reward value. Combining these two reward values, the policy gradient algorithm is used to adjust the parameters of the preset large model. Over multiple iterations, the model continuously learns how to better understand natural language queries and generate efficient SQL, ultimately resulting in an adjusted large model.

[0080] Comprehensive testing was conducted using a pre-set validation set, which included various typical queries, such as single-table conditional queries and multi-table association analysis. For complex queries like "Query VIP users with purchases exceeding 5,000 yuan in the past month and their number of repeat purchases," evaluation metrics included query generation accuracy, execution success rate, and response time. If the adjusted large model met or exceeded the pre-set thresholds for all metrics, it was confirmed as the target large model.

[0081] In actual applications, the target large model adopts different generation strategies based on query complexity. For simple single-table query requirements, such as "querying 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 "analyzing the sales conversion rate of each category of goods in different promotional activities," an enhanced generation process is initiated. First, the query intent is analyzed based on the knowledge graph to clarify the need to associate the product table, promotion activity table, and order table; then, through prompt engineering, the guide words containing table structure, field relationships, and business rules are designed; finally, the target large model generates a complex SQL query involving multi-table joins, aggregate functions, and conditional filtering.

[0082] Accordingly, see Figure 3 As shown, the embodiment of the present application provides a structured query language generation device, including:

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

[0084] A data query module 12 is configured to determine a mapping relationship between a corresponding user query intent 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 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, 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 a policy gradient algorithm to obtain an adjusted large model;

[0086] The language generation module 14 is used to evaluate the adjusted large model 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 using the target large model.

[0087] 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, and then the initial structured query language is generated in combination with 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 standards, it is determined as the target large model for the subsequent generation of the 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 implementations, the data query module 12 specifically includes:

[0089] An information generation unit, which is used to perform synonymous 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;

[0090] a correspondence determination unit, configured to obtain, from the target text template, libraries, tables, and fields related to the database to determine a corresponding first target entity, and to correspond the first target entity to the preset business term to obtain a correspondence;

[0091] A mapping relationship determination unit is used to 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.

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

[0093] An entity determination unit, configured to obtain database-related libraries, tables, and fields from the structured data to determine a corresponding second target entity;

[0094] a score determining unit, configured to determine a precision and a recall based on the second target entity, and determine an F1 score using the precision and the recall;

[0095] The first reward value determining unit is configured to determine a first reward value by using a first preset weight coefficient, the F1 score, the precision rate, and the recall rate.

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

[0097] a time determination unit, configured to determine a query success rate based on the query result, and determine a query time from initiating a query request to obtaining the query result;

[0098] The second reward value determining unit is configured to determine a second reward value by using a second preset weight coefficient, the query success rate, and the query time.

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

[0100] a target reward value determining unit, configured to determine a target reward value based on the first reward value and the second reward value;

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

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

[0103] The model adjustment unit is used to update the model parameters of the preset large model using the target gradient to obtain an adjusted large model.

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

[0105] a number determination subunit, configured to iteratively determine a current gradient using a backpropagation algorithm, the target reward value, the probability value, and a preset expected operation, and determine a maximum number of iterations based on a result of each iteration;

[0106] The gradient update subunit is used to update the current gradient based on the back propagation algorithm, the target reward value, the probability value, the preset expected operation, the early stopping mechanism and the maximum number of iterations to obtain a target gradient.

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

[0108] A requirement determination unit, configured to determine single-table query, condition filtering, and field aggregation as first query requirements, and to determine multi-table join, nested subquery, and dynamic schema change as second query requirements;

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

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

[0111] Furthermore, the embodiment of the present application also discloses an electronic device, Figure 4 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content in the diagram should not be considered as any limitation on the scope of use of this 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. The memory 22 is used to store a computer program, which is loaded and executed by the processor 21 to implement the relevant steps of the structured query language generation method disclosed in any of the aforementioned 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 the external device. The communication protocol it follows is any communication protocol that can be applied to the technical solution of this application and is not specifically limited here; the input and output interface 25 is used to obtain external input data or output data to the outside world. Its specific interface type can be selected according to specific application needs and is not specifically limited here.

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

[0114] The operating system 221 is used to manage and control the hardware devices on the electronic device 20 and the computer program 222, and can be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program capable of implementing the structured query language generation method performed by the electronic device 20 disclosed in any of the aforementioned embodiments, the computer program 222 can further include a computer program capable of implementing other specific tasks.

[0115] Furthermore, this application discloses a computer-readable storage medium for storing a computer program; wherein, when executed by a processor, the computer program implements the aforementioned structured query language generation method. The specific steps of this method can be referred to the corresponding contents disclosed in the aforementioned embodiments and will not be repeated here.

[0116] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from the other embodiments. Reference can be made to the descriptions of the identical or similar parts between the various embodiments. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple, and the relevant parts can be referred to the descriptions of the methods.

[0117] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0118] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in random access memory (RAM), internal memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.

[0119] Finally, it should be noted that, in this document, relational terms such as first and second, etc., are used only 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 terms "comprises," "comprising," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of additional identical elements in the process, method, article, or device comprising the element.

[0120] The above is a detailed introduction to the technical solution provided by the present application. Specific examples are used herein to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea. At the same time, for those skilled in the art, according to the ideas of the present application, there may be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.

Claims

1. A structured query language generation method, characterized in that: include: Obtain the natural language input by the target user based on the query requirements, and use the preset large model to parse the natural language to obtain structured data; Determining a mapping relationship between a corresponding user query intention and a database structure based on the structured data and a preset database knowledge graph, determining an initial structured query language based on the preset large model and the mapping relationship, and performing a query using the initial structured query language to obtain a query result; Determining a first reward value based on the structured data, and determining a second reward value based on the query result, and adjusting the preset large model 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. If the evaluation passes, the adjusted large model is used as a target large model, so as to generate a target structured query language using the target large model. The determining of the first reward value based on the structured data includes: Acquire database-related libraries, tables, and fields from the structured data to determine a corresponding second target entity; Determining precision and recall based on the second target entity, and determining an F1 score using the precision and recall; Determining a first reward value using a first preset weight coefficient, the F1 score, the precision rate, and the recall rate; Wherein, determining the second reward value based on the query result includes: 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; Determining a second reward value using a second preset weight coefficient, the query success rate, and the query time; The step of adjusting the preset large model using the first reward value, the second reward value, and the policy gradient algorithm to obtain the adjusted large model includes: determining a target reward value based on the first reward value and the second reward value; Determining the large model parameters used in generating the initial structured query language as the current policy, determining the probability value of taking the target action when the state is fixed using a policy gradient algorithm under the current policy, and determining the target gradient using a backpropagation algorithm, the target reward value, the probability value, and a preset expected operation; Wherein, the state is the environmental state at the target time step under the current strategy; the target action is the action generated by the preset large model at the target time step; The target gradient is used to update the model parameters of the preset large model to obtain an adjusted large model.

2. The structured query language generation method according to claim 1, characterized in that: The determining of 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: Synonymically transcribe the initial text template and preset business terms through an open source big model to generate the target text template and term dictionary; Obtaining a database-related library, table, and field from the target text template to determine a corresponding first target entity, and matching the first target entity with the preset business term to obtain a corresponding relationship; A preset database knowledge graph is constructed based on the term dictionary, the first target entity and the corresponding relationship, and the mapping relationship between the corresponding user query intention and the database structure is determined using the structured data and the preset database knowledge graph.

3. The structured query language generation method according to claim 1, characterized in that: The process of determining the target gradient using the back propagation algorithm, the target reward value, the probability value, and the preset expected operation further includes: Iteratively determining a current gradient using a backpropagation algorithm, the target reward value, the probability value, and a preset expected operation, and determining a maximum number of iterations based on a result of each iteration; The current gradient is updated based on the back propagation algorithm, the target reward value, the probability value, the preset expected operation, the early stopping mechanism, and the maximum number of iterations to obtain a target gradient.

4. The structured query language generation method according to any one of claims 1 to 3, characterized in that: The generating of a target structured query language using the target large model includes: Single-table query, conditional filtering, and field aggregation are identified as the first query requirement, and multi-table join, nested subquery, and dynamic schema change are identified as the second query requirement; 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; When the query requirement is the second query requirement, a target structured query language is generated based on the target big model, the preset database knowledge graph and the prompt project.

5. A structured query language generation device, characterized in that: include: A language parsing module is used to obtain the natural language input by the target user based on the query requirements and parse the natural language using a preset large model to obtain structured data; A data query module is used to determine the mapping relationship between the corresponding user query intention and the database structure based on the structured data and the preset database knowledge graph, and to determine an initial structured query language based on the preset large model and the mapping relationship, and to 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 a policy gradient algorithm to obtain an adjusted large model; A language generation module is used to evaluate the adjusted large model 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 using the target large model. Wherein, the model adjustment module includes: an entity determination unit, configured to obtain, from the structured data, libraries, tables, and fields related to the database to determine a corresponding second target entity; a score determining unit, configured to determine a precision and a recall based on the second target entity, and determine an F1 score using the precision and the recall; a first reward value determining unit, configured to determine a first reward value using a first preset weight coefficient, the F1 score, the precision rate, and the recall rate; a time determination unit, configured to determine a query success rate based on the query result, and determine a query time from initiating a query request to obtaining the query result; a second reward value determining unit, configured to determine a second reward value using a second preset weight coefficient, the query success rate, and the query time; a target reward value determining unit, configured to determine a target reward value based on the first reward value and the second reward value; a gradient determination unit, configured to determine the large model parameters used in generating the initial structured query language as a current policy, determine the probability value of taking a target action when the state is fixed using a policy gradient algorithm under the current policy, and determine the target gradient using a backpropagation algorithm, the target reward value, the probability value, and a preset expected action; Wherein, the state is the environmental state at the target time step under the current strategy; the target action is the action generated by the preset large model at the target time step; The model adjustment unit is used to update the model parameters of the preset large model using the target gradient to obtain an adjusted large model.

6. An electronic device, characterized in that: include: Memory, used to store computer programs; A processor is configured to execute the computer program to implement the structured query language generation method according to any one of claims 1 to 4.

7. A computer-readable storage medium, characterized in that Used to store a computer program; wherein, when the computer program is executed by a processor, the structured query language generation method according to any one of claims 1 to 4 is implemented.

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