A text-to-structured query implementation method and device and a storage medium

By constructing a knowledge graph of data tables and training a language model with a reinforced dataset, the problem of high error rate in structured query parsing in existing technologies is solved, achieving more accurate structured query generation and enhancing the logical parsing capability of the language model.

CN121579509BActive Publication Date: 2026-03-31KOLUDEO (SHANDONG) ENERGY TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-01-26
Publication Date
2026-03-31

AI Technical Summary

Technical Problem

Existing technologies suffer from high parsing error rates when dealing with multi-table joins, nested conditions, and complex operations involving structured query languages. Furthermore, the language model may miss key constraints, leading to logical defects in the generated structured queries.

Method used

By constructing a knowledge graph of data tables, training a language model using a reinforced dataset, identifying the most relevant data tables, and generating natural language explanations of structured queries based on overall and local logical chains, the language model's ability to understand and generate structured queries is improved by using cross-entropy loss and model parameter fine-tuning techniques.

Benefits of technology

It enhances the language model's logical parsing ability for structured queries, improves the accuracy of generated structured queries and their relevance to natural language problems, reduces parsing errors, and ensures accuracy in application databases.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121579509B_ABST
    Figure CN121579509B_ABST
Patent Text Reader

Abstract

The present application relates to a kind of text to structured query implementation method, device and storage medium, it is related to structured query generation technical field.The present application trains language model using text to structured query reinforcement dataset;When applying, the data table knowledge graph of the total data table of application database is constructed, according to the proposed target natural language question and data table knowledge graph, the most relevant several data tables are identified;The overall logical chain natural language explanation of the structured query of target natural language question is directly predicted according to target natural language question by trained language model in the scope of relevant knowledge table and its knowledge graph;And according to target natural language question and the most relevant table, multiple candidate structured queries are predicted;The natural language explanation of the overall logical chain of candidate structured query is obtained using operation explanation template;According to the similarity of two overall logical chain natural language explanations, the optimal candidate structured query is selected.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of text-to-structured query implementation, and more particularly to a method, apparatus, and storage medium for implementing text-to-structured queries. Background Technology

[0002] The statements in this section are merely background information relating to this disclosure and do not necessarily constitute prior art.

[0003] Structured Query Language (SCL) is a professional database interaction tool. Its application requires a certain level of expertise, aiming to transform natural language problems into executable structured queries, enabling interaction between natural language and databases. Text-to-structured query generation technology bridges the gap between natural language and databases, allowing users to query data using SCL without needing specialized knowledge. Text-to-structured queries require converting diverse natural language expressions into standardized, formatted structured queries. Large language models have been applied to the field of text-to-structured query languages, significantly advancing its development. However, challenges remain when handling queries involving multi-table joins, nested conditions, and complex operations. Existing methods typically rely on multi-stage pipelines or task chains to improve the accuracy of language models in generating complex queries, supporting business users in obtaining structured queries from natural language data. However, in practical applications, misalignment between training data and business logic leads to high parsing error rates. Furthermore, language models may overlook key constraints when generating structured queries, causing application problems. Summary of the Invention

[0004] To solve the above-mentioned technical problems, or at least partially solve them, the present invention provides a method, apparatus, and storage medium for implementing text-to-structured queries.

[0005] This invention provides a method for implementing text-to-structured queries, comprising:

[0006] Train a language model specifically for text-to-structured query tasks using a reinforced dataset of text-to-structured queries;

[0007] When applied, a data table knowledge graph is constructed based on the semantic information of all data tables in the database. The data table knowledge graph is used to match the most relevant data tables based on natural language questions to clarify the query scope.

[0008] Based on the proposed target natural language question and the knowledge graph of the data tables, the most relevant data tables are identified;

[0009] Within the scope of relevant knowledge tables and knowledge graphs, the trained language model directly predicts the overall logical chain of the structured query for the target natural language question and provides a natural language interpretation based on the target natural language question.

[0010] The trained language model predicts multiple candidate structured queries based on the target natural language question and the most relevant table.

[0011] The operation interpretation template of the structured query language is used to obtain the natural language interpretation of the overall logical chain of the candidate structured query; the optimal candidate structured query is selected based on the similarity score between the natural language interpretation of the overall logical chain of the target natural language question's structured query and the natural language interpretation of the overall logical chain of the candidate structured query.

[0012] Furthermore, the process of training a language model specifically for text-to-structured query tasks includes:

[0013] We construct a text-to-structured query reinforcement dataset using a text-to-structured query dataset. Each set of data in the reinforcement dataset contains: a natural language question and a corresponding structured query's overall logical chain natural language explanation from the text-to-structured query dataset, and a predicted natural language question and a corresponding structured query, wherein the predicted natural language question is generated by a language model based on the overall logical chain and local logical chain of the structured query.

[0014] By using a reinforced dataset and model parameter fine-tuning techniques, a language model is trained to predict the overall logical chain of natural language interpretations of corresponding structured queries based on natural language questions from a dataset of text-to-structured queries. By using a reinforced dataset and model parameter fine-tuning techniques, a language model is trained to generate and predict structured queries based on natural language questions.

[0015] Furthermore, the loss used for training is a weighted sum of the following two tasks:

[0016] The cross-entropy loss between the predicted overall logistic chain natural language interpretation output by the language model and the actual overall logistic chain natural language interpretation. ;

[0017] The cross-entropy loss between the predicted structured query output by the language model and the actual structured query. ;

[0018] The training objective is to minimize the weighted sum of losses from the two tasks:

[0019] ;

[0020] in, To balance the weights.

[0021] Furthermore, in the early stages of training, weight balancing is implemented. The values ​​are the same, and as the training process progresses, the values ​​are given... Higher weight, i.e. This is to guide language models to focus more on text-to-structured query tasks;

[0022] Furthermore, throughout the training process, the weights are dynamically adjusted based on the performance of each task on the validation set, including: if the interpretation task converges too quickly while the structured query task stagnates, the weights are reduced. improve If the structured task converges too quickly while the interpretation task stagnates, it reduces... improve .

[0023] Furthermore, the construction of the enhanced text-to-structured query dataset using the text-to-structured query dataset includes:

[0024] Traverse the dataset of text to structured queries, pairwise natural language questions and corresponding structured queries;

[0025] For any target structured query that is traversed, the target structured query is transformed into a syntax tree, and the target sub-structured query and target sub-constraint are extracted using the syntax tree.

[0026] Based on the syntactic order of operations in the Structured Query Language rules, and according to the operations contained in the syntax tree, target sub-constraints are organized to form several overall logical chains from the target sub-constraints to obtain the target structured query, and several local logical chains from the target sub-constraints to obtain the target sub-structured query; and corresponding natural language interpretations are generated for the overall logical chains and local logical chains according to the operation interpretation template of the Structured Query Language.

[0027] The control language model generates a prediction natural language problem based on the global logic chain and the local logic chain;

[0028] Pair natural language questions from a dataset of text-to-structured queries with natural language interpretations of the overall logical chain, and pair predicted natural language questions with corresponding structured queries.

[0029] Furthermore, the process of extracting the target sub-structured query and target sub-constraint of the target structured query using a syntax tree includes:

[0030] Parse any target structured query in a dataset of text-to-structured queries into the corresponding syntax tree form;

[0031] Traverse each node in the syntax tree and obtain the type of the node;

[0032] Traverse the subtrees formed by adjacent levels of the syntax tree, check each traversed subtree, whether the type of the root node of the subtree is a structured query language operation, and whether all the child nodes of the root node of the subtree are non-structured query language operations. If so, the subtree is a sub-constraint subtree. Reconstruct multiple target sub-constraints of the target structured query from the sub-constraint subtree.

[0033] In the syntax tree of the isolated target structured query, generate multiple sets of sub-syntax trees for any target sub-constraint, and obtain the sub-structured queries corresponding to the multiple sets of sub-syntax trees as the target sub-structured queries of the target structured query.

[0034] Furthermore, the language model generates a prompt word for predicting a natural language problem based on the overall logical chain and local logical chains. The prompt word includes the following: "As an expert in interpreting structured queries using natural language, the task is to: obtain the constraints of the overall logical chain, which are derived from the set formed by the differences between the overall logical chain and each local logical chain; focus on each constraint of the overall logical chain; and generate a clear and concise question that captures the intent of the overall logical chain, as the natural language problem to be predicted." The prompt word provides at least one structured query, the overall logical chain and local logical chains of the structured query, an example of finding constraints using the differences between the overall and local logical chains, and an example of the predicted natural language. This allows the language model to refer to the example and the given prompt to generate a natural language problem based on the overall and local logical chains.

[0035] Furthermore, for any data table in the database, the semantic information of the data table is obtained. This semantic information includes: the data table name, the data table description, the data table column names, the data table column descriptions, the data table key connection information, the knowledge domain involved in the data table, and the query of the example. Then, for any data table, the data table knowledge graph contains the nodes of other data tables associated with it, the knowledge domain nodes involved in it, the column content nodes it has, and the query of the example it is associated with.

[0036] Secondly, the present invention provides an apparatus for implementing text-to-structured queries, comprising at least one storage unit, a processing unit, and a computer program stored on the storage unit and executable on the processing unit, wherein the processing unit executes the computer program to implement the method for implementing text-to-structured queries.

[0037] Thirdly, the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the program, when executed by a processor, implements the method for implementing text-to-structured queries.

[0038] The technical solutions provided in the embodiments of the present invention have the following advantages compared with the prior art:

[0039] This invention further parses the syntax tree of any structured query to obtain complete and partial logical chains, and then forms a natural language interpretation in the corresponding natural language form according to the logical parsing. This provides a bridge for language models that realize text-to-structured query conversion, enabling them to understand the logic of structured queries in natural language form. This application utilizes a language model to generate predicted natural language questions based on multiple partial and overall logical chains. The differences between these chains actually constitute the constraint set of the structured query. The language model uses this constraint set to generate the predicted natural language questions for the corresponding structured queries, which helps the model understand all the constraints in the structured query and avoids logical defects in the predicted natural language questions due to constraint omissions. It enhances the language model's sensitivity to all constraints related to the natural language question, enabling better conversion of constraints into corresponding structured queries. It improves the language model's ability to generate and logically interpret structured queries, making the intent of the natural language question more closely related to the generated predicted structured query.

[0040] To ensure the accuracy of the language model in the application database domain, this application constructs a data table knowledge graph. First, the most relevant data tables are obtained by matching the user's target natural language question with the data table knowledge graph, thus determining the query scope. Then, the language model makes predictions based on the prior information in the most relevant data tables, and selects the optimal candidate structured query based on the similarity score of the natural language interpretations of the overall logical chain of the two tasks. This improves the prediction performance in the application database domain and avoids the language model's "illusion" in the application database domain, which could affect accuracy. Attached Figure Description

[0041] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.

[0042] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0043] Figure 1 A flowchart illustrating a method for implementing text-to-structured query according to an embodiment of the present invention;

[0044] Figure 2 An architecture diagram of a text-to-structured query implementation method provided in an embodiment of the present invention;

[0045] Figure 3This is a flowchart illustrating how to construct an enhanced dataset for text-to-structured queries using a dataset from a text-to-structured query dataset, as provided in an embodiment of the present invention.

[0046] Figure 4 This is a flowchart of the training process provided in an embodiment of the present invention;

[0047] Figure 5 This is a schematic diagram of the text-to-structured query implementation device provided in an embodiment of the present invention. Detailed Implementation

[0048] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0049] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0050] Example 1

[0051] like Figure 1 and Figure 2 As shown, the implementation method of text-to-structured query of the present invention includes:

[0052] S100, train a language model specifically for text-to-structured query tasks using a text-to-structured query reinforcement dataset. The process includes: constructing a text-to-structured query reinforcement dataset using the existing dataset; using the reinforcement dataset, and based on model parameter fine-tuning techniques, training a language model to predict the overall logical chain natural language interpretation of the corresponding structured query based on natural language questions from the text-to-structured query dataset; and using the reinforcement dataset, and based on model parameter fine-tuning techniques, training a language model to generate and predict structured queries based on natural language questions.

[0053] Each set of data in the augmented dataset contains: a natural language question and a corresponding structured query's overall logical chain natural language interpretation from a text-to-structured query dataset, and a predicted natural language question and corresponding structured query, wherein the predicted natural language question is generated by a language model based on the overall and local logical chains of the structured query.

[0054] In the specific implementation process, the dataset for text-to-structured queries includes natural language questions and corresponding structured queries related to the training database. For example: Natural language question: Find users older than 30 from the (training database) user table. The corresponding structured query is: "SELECT * FROM users WHERE age>30;"; Natural language question: Find users from Beijing from the (training database) user table, sorted in descending order of age. The corresponding structured query is: "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC;".

[0055] like Figure 3 As shown, constructing a text-to-structured query enhancement dataset using a text-to-structured query dataset includes:

[0056] S110, traverse the dataset of text to structured queries, pairwise natural language questions and corresponding structured queries.

[0057] S120: For any target structured query that is traversed, the target structured query is transformed into a syntax tree, and the target sub-structured query and target sub-constraint are extracted using the syntax tree.

[0058] In the specific implementation process, existing syntax tree generation tools are used to transform any target structured query that is traversed into a corresponding syntax tree. Taking "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC;" as an example, the root node of its syntax tree is the query task represented by the structured query. The root node of the query task contains four operation sub-nodes of the structured query language, namely SELECT, FROM, WHERE, and ORDER BY. Among them, the sub-node of the SELECT operation is all users that meet the requirements; the sub-node of the FROM operation is the user table users; the sub-node of the WHERE operation is the city Beijing; and the sub-node of the ORDER BY operation contains two sub-nodes, namely the sorting field age and the sorting method DESC.

[0059] In the above process, such as Figure 3As shown, the process of extracting the target sub-structured query and target sub-constraint of the target structured query using a syntax tree includes:

[0060] S121, parse any target structured query in the text-to-structured query dataset into the corresponding syntax tree form;

[0061] S122, traverse each node in the syntax tree of the target structured query and obtain the node type; taking "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC;" as an example, the root node of its syntax tree is the query task type, and the SELECT, FROM, WHERE, and ORDER BY child nodes of the root node are the operation types, among which the child nodes of the SELECT, FROM, WHERE, and ORDER BY child nodes are parameter types.

[0062] S123: Traverse the subtrees formed by adjacent levels of the syntax tree. Check each traversed subtree to see if the root node of the subtree is a structured query language operation and all child nodes of the root node are non-structured query language operations. If so, the subtree is a sub-constraint subtree. Reconstruct multiple target sub-constraints of the target structured query from the sub-constraint subtrees. Taking "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC;" as an example, the subtrees corresponding to SELECT *, FROM users, WHERE city = 'Beijing', ORDER BY age (which defaults to ascending order), and ORDER BY age DESC are all corresponding sub-constraints.

[0063] S124: In the syntax tree of the isolated target structured query, generate multiple sets of sub-syntax trees by taking any target sub-constraint, and obtain the sub-structured queries corresponding to the multiple sets of sub-syntax trees as the target sub-structured queries of the target structured query.

[0064] S130, according to the operation order in the structured query language rules, organize the target sub-constraints according to the operations defined by the syntax tree and sub-syntax tree, forming several overall logical chains of target structured queries obtained from all target sub-constraints, and several local logical chains of target sub-structured queries obtained from setting target sub-constraints.

[0065] S140, Generate a corresponding natural language interpretation for the overall logical chain according to the operation interpretation template of the structured query language; the operation interpretation template is pre-built and comes from the operation interpretation of the structured query language. For example, if the structured query language specifies that ORDER BY is followed by a field and a sorting method, the corresponding ORDER BY interpretation template is "sorted by a certain field and sorted by a certain sorting method".

[0066] S150 uses prompt words to control a language model to generate a predictive natural language problem based on the overall logic chain and local logic chains.

[0067] The prompts include the following: As an expert in interpreting structured queries using natural language, the task is to: obtain the constraints of the overall logical chain, which is derived from the set formed by the differences between the overall logical chain and each local logical chain; focus on each constraint of the overall logical chain; and generate a clear and concise question that captures the intent of the overall logical chain, serving as a natural language prediction question. The prompts provide at least one structured query, its overall logical chain and local logical chains, examples of constraints found using the differences between the overall and local logical chains, and examples of predicted natural language. This allows the language model to generate a natural language prediction question based on the overall and local logical chains, referencing the examples and the given prompts.

[0068] An example structured query: "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher' ORDER BY age DESC;"

[0069] The target sub-structured queries of an exemplary structured query include:

[0070] "SELECT * FROM users;"

[0071] "SELECT * FROM users WHERE city = 'Beijing' ;";

[0072] SELECT * FROM users WHERE job = 'Teacher';

[0073] "SELECT * FROM users ORDER BY age DESC"

[0074] “SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher';”

[0075] “SELECT * FROM users WHERE job = 'Teacher' WHERE city = 'Beijing';”

[0076] "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC;";

[0077] "SELECT * FROM users WHERE job = 'Teacher' ORDER BY age DESC;";

[0078] SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher' ORDER BY age DESC

[0079] Its overall logical chain includes:

[0080] The query "SELECT * FROM users", "SELECT * FROM users WHERE city = 'Beijing';", "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher';", and "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher' ORDER BY age DESC" form a complete logical chain.

[0081] The query "SELECT * FROM users;", "SELECT * FROM users WHERE city = 'Beijing';", "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC;", and "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'teacher' ORDER BY age DESC" form a complete logical chain.

[0082] The query "SELECT * FROM users", "SELECT * FROM users WHERE job = 'Teacher';", "SELECT * FROM users WHERE job = 'Teacher' WHERE city = 'Beijing';", and "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher' ORDER BY age DESC" form a complete logical chain.

[0083] The following statements form a logical chain: “SELECT * FROM users;”, “SELECT * FROM users WHERE job = 'Teacher' ;”, “SELECT * FROM users WHERE job = 'Teacher' ORDER BY age DESC;”, and “SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher' ORDER BY age DESC”.

[0084] The query "SELECT * FROM users;", "SELECT * FROM users WHERE city = 'Beijing';", "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC;", and "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'teacher' ORDER BY age DESC" form a complete logical chain.

[0085] The query "SELECT * FROM users;", "SELECT * FROM users ORDER BY age DESC", "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC", and "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'teacher' ORDER BY age DESC" form a complete logical chain.

[0086] The query "SELECT * FROM users;", "SELECT * FROM users ORDER BY age DESC", "SELECT * FROM users WHERE job = 'Teacher' ORDER BY age DESC", and "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher' ORDER BY age DESC" form a complete logical chain.

[0087] Local logical chains: The local logical chain of the target sub-structured query "SELECT * FROM users;" is "SELECT * FROM users;"; the local logical chains of the target sub-structured query "SELECT * FROM users WHERE city = 'Beijing' ;" are "SELECT * FROM users;" and "SELECT * FROM users WHERE city = 'Beijing' ;".

[0088] The local logical chain of the target sub-structured query "SELECT * FROM users WHERE job = 'Teacher' ;" is "SELECT * FROM users;" and "SELECT * FROM users WHERE job = 'Teacher' ;"; the local logical chain of the target sub-structured query "SELECT * FROM users ORDER BY age DESC" is "SELECT * FROM users;" and "SELECT * FROM users ORDER BY age DESC"; the local logical chain of the target sub-structured query "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher' ;" is "SELECT * FROM users;", "SELECT * FROM users WHERE job = 'Teacher' ;", "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher' ;" or "SELECT * FROM users;", "SELECT * FROM users WHERE city = 'Beijing';", "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher'"; The partial logical chain of the target substructured query "SELECT * FROM users WHERE job = 'Teacher' WHERE city = 'Beijing' ;" is either "SELECT * FROM users;", "SELECT * FROM users WHERE job = 'Teacher' ;", or "SELECT * FROM users;", "SELECT * FROM users WHERE city = 'Beijing' ;", or "SELECT * FROM users WHERE city = 'Beijing' ;". The partial logical chain of the target substructured query "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC;" is also either "SELECT * FROM users;", "SELECT * FROM users WHERE city = 'Beijing' ;".", "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC;", or "SELECT * FROM users;", "SELECT * FROM users ORDER BY age DESC;", "SELECT * FROM users WHERE city = 'Beijing' ORDER BY age DESC;"; The local logical chain of the target sub-structured query "SELECT * FROM users WHERE job = 'teacher' ORDER BY age DESC;" is "SELECT * FROM users;", "SELECT * FROM users WHERE job = 'teacher';", "SELECT * FROM users WHERE job = 'teacher' ORDER BY age DESC;" or "SELECT * FROM users;", "SELECT * FROM users ORDER BY age DESC;", "SELECT * FROM users WHERE job = 'teacher' ORDER BY age DESC;".;

[0089] Examples of constraints for the overall logical chain "SELECT * FROM users", "SELECT * FROM users WHERE city = 'Beijing';", "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'teacher';", "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'teacher' ORDER BY age DESC":

[0090] Compared to the local logical chains "SELECT * FROM users;" and "SELECT * FROM users WHERE city = 'Beijing';", the constraints obtained are "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher';" and "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher' ORDER BYage DESC". This constraint limits the semantic logical progression that the language model focuses on to: from "selecting content with the city of Beijing and the job title of 'Teacher'" to "selecting content with the city of Beijing and the job title of 'Teacher' and sorting the content in descending order of age".

[0091] Compared to the local logical chains "SELECT * FROM users;" and "SELECT * FROM users WHERE job = 'Teacher';", the constraints obtained are "SELECT * FROM users WHERE city = 'Beijing';", "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher';" and "SELECT * FROM users WHERE city = 'Beijing' WHERE job = 'Teacher' ORDER BY age DESC". This constraint limits the semantic logical progression that the language model focuses on to: "selecting content with the city of Beijing" to "selecting content with the city of Beijing and the job title of 'Teacher'" and then to "selecting content with the city of Beijing and the job title of 'Teacher' and sorting the content in descending order of age".

[0092] Example 1 of predicted natural language: From the users table, find entries where the city is Beijing and the job title is teacher, and sort them in descending order by age.

[0093] Example 2 of predicted natural language: After sorting the contents of the users table in descending order by age, filter out the contents with the city of Beijing and the job title of teacher while retaining the order.

[0094] In the specific implementation process, the syntax tree is decomposed into sub-constraints and the sub-constraints are used to construct the overall logic chain and local logic chain. The differences between the overall logic chain and the local logic chain are used to obtain all the constraints of the overall logic chain, which enhances the language model's ability to understand all constraints, that is, enhances the language model's ability to understand the logical relationship between sub-constraints. This enables the language model to generate the corresponding predictive language problem based on all constraints, avoiding the problem of missing constraints.

[0095] S160, pair the natural language questions from the text-to-structured query dataset with the natural language interpretation of the overall logical chain, and pair the predicted natural language questions with the corresponding structured queries from the text-to-structured query dataset to obtain a set of data for the enhanced dataset.

[0096] Repeat the above process to obtain an enhanced dataset of text-to-structured queries.

[0097] like Figure 4 The training process shown includes:

[0098] The text-to-structured query reinforcement dataset is divided into two parts: training and validation.

[0099] Configure the training hyperparameters and set the training loss.

[0100] The loss used for training is a weighted sum of the results from the following two tasks:

[0101] The cross-entropy loss between the predicted overall logistic chain natural language interpretation output by the language model and the actual overall logistic chain natural language interpretation. ;

[0102] The cross-entropy loss between the predicted structured query output by the language model and the actual structured query. .

[0103] The training objective is to minimize the weighted sum of losses from the two tasks:

[0104] ;

[0105] in, To balance the weights.

[0106] Establish a balanced weight adjustment method, including: in the early stage of training, With the same value, since the ultimate goal is to generate a correct structured query, as the training process progresses, the given... Higher weight, i.e. This allows the language model to focus more on the text-to-structured query task. Furthermore, throughout the training process, the weights are dynamically adjusted based on the performance of each task on the validation set. For example, if the interpretation task converges too quickly while the structured query task stagnates, the weights can be appropriately reduced. improve If the structured task converges too quickly while the interpretation task stagnates, appropriately reduce the speed. improve .

[0107] The fine-tuning techniques for the language model are defined; the fine-tuning techniques used are not limited to Adapter, SFT, LoRA and QLoRA techniques.

[0108] The language model is trained to predict the overall logical chain of a structured query's natural language interpretation based on a natural language question from a dataset of text-to-structured queries. The language model is also trained to generate predicted structured queries based on the natural language question.

[0109] Evaluation criteria for language model training are defined as follows: The proportion of generated predicted structured queries that produce the same execution results as their corresponding real structured queries is statistically analyzed; a higher proportion indicates better performance. The ratio of the execution time of generated predicted structured queries to their corresponding real structured queries is also analyzed; a smaller ratio indicates better performance.

[0110] This application further parses the syntax tree of any structured query to obtain complete and partial logical chains, and then forms a natural language interpretation in the corresponding natural language form according to the logical parsing. This provides a bridge for language models that realize text-to-structured query conversion, enabling them to understand the logic of structured queries in natural language form. This application utilizes a language model to generate predicted natural language questions based on multiple partial and global logical chains. The differences between these chains actually constitute the constraint set of the structured query. The language model uses this constraint set to generate the predicted natural language questions for the corresponding structured queries, which helps the model understand all constraints within the structured query and avoids logical defects in the predicted natural language questions due to constraint omissions. This application enhances the language model's sensitivity to all constraints related to the natural language question, enabling better conversion into the corresponding structured query based on constraints. It improves the language model's ability to generate and logically interpret structured queries, making the intent of the natural language question more closely related to the generated predicted structured query.

[0111] S200, during application, a data table knowledge graph is constructed based on the semantic information of all data tables in the database. During application, the database encountered may exceed the scope of the training database. The data tables in the database are more complex than those in the training database; for example, multiple sets of identical data may appear in different data tables, affecting the determination of data tables related to natural language problems. To better determine the query scope, this application constructs a data table knowledge graph.

[0112] For any data table in the database, obtain the semantic information of the data table. The semantic information includes: data table name, data table description, data table column names, data table column descriptions, data table key join information, the knowledge domain involved in the data table, and example queries. Construct a data table knowledge graph based on the semantic information of all data tables in the database. Then, for any data table, the data table knowledge graph contains its associated other data table nodes, its involved knowledge domain nodes, its column content nodes, and the queries of its associated examples.

[0113] The data table knowledge graph is used to match the most relevant data tables based on the natural language question, thus defining the query scope. S300: Based on the proposed target natural language question and the data table knowledge graph, several of the most relevant data tables are identified.

[0114] S400 uses the trained language model to perform the following two tasks:

[0115] The trained language model, within the scope of the relevant data tables and knowledge graphs, directly predicts the overall logical chain of natural language interpretations of structured queries related to the target natural language question.

[0116] In the process of predicting the natural language interpretation of the overall logical chain, this application limits the prediction to the relevant data tables and their knowledge graphs. That is, the relevant data tables and their knowledge graphs are used as context-sensitive language models to predict the natural language interpretation of the overall logical chain within the scope of the context, so as to improve the adaptability of the predicted natural language interpretation of the overall logical chain to the application database.

[0117] The trained language model predicts multiple candidate structured queries based on the target natural language question and the most relevant table.

[0118] S500 uses the operation interpretation template of the structured query language to obtain a natural language interpretation of the overall logical chain of candidate structured queries.

[0119] S600: Select the optimal candidate structured query based on the similarity score between the natural language interpretation of the overall logical chain of the structured query related to the target natural language problem predicted in S400 and the natural language interpretation of the overall logical chain of the candidate structured query in S500.

[0120] To ensure the accuracy of the language model in the application database domain when applied to it, this application constructs a data table knowledge graph. First, it identifies the most relevant data tables by matching the user's target natural language question with the data table knowledge graph, thus determining the query scope. Then, the language model makes predictions based on the pre-defined information in the most relevant data tables, and selects the optimal candidate structured query based on the similarity score of the natural language interpretations of the overall logical chains of the two tasks. This improves the prediction performance in the application database domain and avoids the "illusion" effect of the language model in this domain.

[0121] As shown in Table 1, Table 1 presents the accuracy of this application and different methods in processing text into structured query tasks, and compares the structured queries by dividing them into date-type operations, grouping operations, sorting operations, union operations, and positioning operations.

[0122] Table 1 shows the accuracy of this application and different comparison methods in handling different types of structured query transformation tasks;

[0123]

[0124] Example 2

[0125] like Figure 5 As shown, this embodiment of the invention provides an apparatus for implementing text-to-structured queries, including: at least one storage unit, a processing unit, and a computer program stored in the storage unit and executable on the processing unit. When the processing unit executes the program, it implements the method for implementing text-to-structured queries, including:

[0126] Train a language model specifically for text-to-structured query tasks using a reinforced dataset of text-to-structured queries;

[0127] When applied, a data table knowledge graph is constructed based on the semantic information of all data tables in the database. The data table knowledge graph is used to match the most relevant data tables based on natural language questions to clarify the query scope.

[0128] Based on the proposed target natural language question and the knowledge graph of the data tables, the most relevant data tables are identified;

[0129] Within the scope of relevant knowledge tables and knowledge graphs, the trained language model directly predicts the overall logical chain of the structured query for the target natural language question and provides a natural language interpretation based on the target natural language question.

[0130] The trained language model predicts multiple candidate structured queries based on the target natural language question and the most relevant table.

[0131] The operation interpretation template of the structured query language is used to obtain the natural language interpretation of the overall logical chain of the candidate structured query; the optimal candidate structured query is selected based on the similarity score between the natural language interpretation of the overall logical chain of the target natural language question's structured query and the natural language interpretation of the overall logical chain of the candidate structured query.

[0132] Of course, the computer program stored in the memory of the text-to-structured query implementation device provided in the embodiments of the present invention is not limited to the method operation described above, but can also execute related operations in the text-to-structured query implementation method provided in any embodiment of the present invention.

[0133] Example 3

[0134] This invention provides a computer-readable storage medium storing a computer program thereon. When executed by a processor, the program implements the method for text-to-structured query, comprising:

[0135] Train a language model specifically for text-to-structured query tasks using a reinforced dataset of text-to-structured queries;

[0136] When applied, a data table knowledge graph is constructed based on the semantic information of all data tables in the database. The data table knowledge graph is used to match the most relevant data tables based on natural language questions to clarify the query scope.

[0137] Based on the proposed target natural language question and the knowledge graph of the data tables, the most relevant data tables are identified;

[0138] Within the scope of relevant knowledge tables and knowledge graphs, the trained language model directly predicts the overall logical chain of the structured query for the target natural language question and provides a natural language interpretation based on the target natural language question.

[0139] The trained language model predicts multiple candidate structured queries based on the target natural language question and the most relevant table.

[0140] The operation interpretation template of the structured query language is used to obtain the natural language interpretation of the overall logical chain of the candidate structured query; the optimal candidate structured query is selected based on the similarity score between the natural language interpretation of the overall logical chain of the target natural language question's structured query and the natural language interpretation of the overall logical chain of the candidate structured query.

[0141] In the embodiments provided by this invention, it should be understood that the disclosed structures and methods can be implemented in other ways. For example, the structural embodiments described above are merely illustrative. For instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection through some interfaces, structures, or units, and may be electrical, mechanical, or other forms.

[0142] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0143] Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0144] The above description is merely a specific embodiment of the present invention, enabling those skilled in the art to understand or implement the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the present invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features claimed herein.

Claims

1. A method for implementing text-to-structured query, characterized in that, The application comprises: training a language model specialized for a text-to-structured query task using a reinforced dataset of text-to-structured query, the process comprising: constructing a reinforced dataset of text-to-structured query using a dataset of text-to-structured query, each set of data in the reinforced dataset containing: a natural language question and a corresponding structured query from the dataset of text-to-structured query, an overall logical chain natural language explanation of the structured query, and, a predicted natural language question and corresponding structured query, wherein the predicted natural language question is generated by the language model based on the overall logical chain and the local logical chain of the structured query; training the language model to predict the overall logical chain natural language explanation of the corresponding structured query from the natural language question using the reinforced dataset based on model parameter fine-tuning techniques; training the language model to generate a predicted structured query from the natural language question using the reinforced dataset based on model parameter fine-tuning techniques; wherein constructing a reinforced dataset of text-to-structured query using a dataset of text-to-structured query comprises: traversing the pairs of natural language questions and corresponding structured queries in the dataset of text-to-structured query; for any target structured query traversed, converting the target structured query into a syntax tree form, and extracting target sub-structured queries and target sub-constraints of the target structured query using the syntax tree; according to the operation order in the structured query language rule, organizing the target sub-constraints according to the operations defined by the syntax tree and sub-syntax trees to form several overall logical chains of the target structured query obtained by all target sub-constraints, and several local logical chains of the target sub-structured query obtained by setting target sub-constraints; generating corresponding natural language explanations for the overall logical chains according to the operation explanation templates of the structured query language; generating a predicted natural language question based on the overall logical chain and the local logical chain using the language model controlled by the prompt word; pairing the natural language question from the dataset of text-to-structured query with the natural language explanation of the overall logical chain, and pairing the predicted natural language question with the corresponding structured query from the dataset of text-to-structured query to obtain a set of data of the reinforced dataset; In application, a data table knowledge graph is constructed based on the semantic information of all data tables of the application database, and the data table knowledge graph is used to match the most relevant data table based on the natural language question and to clarify the query range; According to the proposed target natural language question and the data table knowledge graph, the most relevant data tables are identified; The trained language model directly predicts the overall logical chain natural language explanation of the structured query of the target natural language question within the scope of the relevant knowledge table and its knowledge graph based on the target natural language question; The trained language model predicts multiple candidate structured queries based on the target natural language question and the most relevant table. The natural language explanation of the overall logical chain of the candidate structured query is obtained by using an operation explanation template of a structured query language; and the optimal candidate structured query is selected according to the similarity score of the natural language explanation of the overall logical chain of the target structured query and the natural language explanation of the overall logical chain of the candidate structured query.

2. The method of claim 1, wherein, The loss used for training is a weighted sum of the following two tasks: cross-entropy loss between the natural language explanation of the predicted overall logical chain output by the language model and the natural language explanation of the true overall logical chain ; Cross-entropy loss between the predicted structured query output by the language model and the true structured query ; The training target is to minimize the weighted sum loss of the two tasks: ; wherein is an equalization weight.

3. The method of claim 2, wherein, At the beginning of training, the equal weights The same values, as the training process proceeds, give Higher weights, i.e. to guide the language model to focus more on the text-to-structured query task; and dynamically adjust the weights according to the performance of respective tasks on the validation set throughout the training process, including: if the explanation task converges too quickly and the structured query task stagnates, reduce improve ; If the structured task converges too quickly and the explanation task stalls, decrease increase .

4. The method of claim 1, wherein, The process of extracting the target sub-constraints and the target sub-structured queries of the target structured query by using the syntax tree comprises the following steps: Any target structured query in the text-to-structured query dataset is parsed into a corresponding syntax tree form; Each node in the syntax tree is traversed to obtain the type of the node; The sub-trees formed by adjacent levels of the syntax tree are traversed, and each traversed sub-tree is checked to determine whether the root node type of the sub-tree is an operation of the structured query language and all child nodes of the root node of the sub-tree are non-operations of the unstructured query language; if yes, the sub-tree is a sub-constraint sub-tree, and the target sub-constraints of the target structured query are restored from the sub-constraint sub-tree; Any target sub-constraint in the syntax tree of the target structured query is isolated to generate multiple groups of sub-syntax trees, and the sub-structured queries corresponding to the multiple groups of sub-syntax trees are obtained as the target sub-structured queries of the target structured query.

5. The method of claim 1, wherein, The content of the prompt word of the predicted natural language question generated by the control language model based on the overall logical chain and the local logical chain comprises: "As an expert who explains structured queries in natural language, the task is to obtain the constraints of the overall logical chain, the constraints of the overall logical chain come from the set formed by the difference between the overall logical chain and each local logical chain, pay attention to each constraint of the overall logical chain, generate a clear and concise question that can capture the intent of the overall logical chain as a predicted natural language question"; in the prompt word, at least one structured query, the overall logical chain and the local logical chain of the structured query, an example of finding constraints by using the difference between the overall logical chain and the local logical chain, and an example of the predicted natural language are given; so that the language model references the examples and the given prompt to generate a predicted natural language question based on the overall logical chain and the local logical chain.

6. The method of claim 1, wherein, For any data table of a database, semantic information of the data table is obtained, and the semantic information comprises: a data table name, a data table description, column names of the data table, column descriptions of the data table, key connection information of the data table, a knowledge field related to the data table, and an example query; for any one data table, the data table knowledge graph comprises other data table nodes associated with the data table, knowledge field nodes related to the data table, column content nodes of the data table, and example queries associated with the data table.

7. An apparatus for text-to-structured query implementation, comprising at least a storage unit, a processing unit, and a computer program stored on the storage unit and executable on the processing unit, wherein, The processing unit implements the text-to-structured query implementation method of any one of claims 1-6 when executing the computer program.

8. A computer-readable storage medium having stored thereon a computer program, characterized in that, The program implements the text-to-structured query implementation method of any one of claims 1-6 when executed by the processor.

Citation Information

Patent Citations

  • Text-to-structured query language conversion method and system, medium, equipment and product

    CN118193582A

  • Method, system and device for converting natural language to SQL (Structured Query Language) query statement and medium

    CN118779342A