Text2SQL generation method and system driven by large language model
By constructing query graphs and graph representation models using large language models, and combining them with structured Prompt templates to generate SQL statements and perform error detection, the accuracy and adaptability issues of complex multi-table queries are solved, achieving efficient and accurate SQL generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-18
- Publication Date
- 2026-03-31
AI Technical Summary
Existing technologies suffer from low accuracy, high error rate in complex multi-table join queries, and poor cross-domain adaptability.
By identifying key entity relationships in natural language problems using a large language model, constructing a query graph, and combining it with the graph representation model of the target database, candidate subgraph sets are retrieved, a structured Prompt template is generated to guide the generation of SQL statements, and finally, syntax and semantic errors are detected and corrected.
It significantly improves the accuracy and cross-domain adaptability of multi-table join queries, ensuring that the generated SQL statements conform to business logic and database schema, and improving the efficiency and accuracy of large-scale complex data analysis.
Smart Images

Figure CN121144343B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of natural language processing technology, specifically to a large language model-driven Text2SQL generation method and system. Background Technology
[0002] With the rapid advancements in artificial intelligence and natural language processing technologies, the demand for interacting with databases using natural language, automatically generating corresponding SQL scripts, and outputting query results is becoming increasingly strong. The core objective of this demand is to simplify database operations by enabling dialogue with database systems through natural language, allowing more users to easily interact with and manage databases. In the database field, Text2SQL, also known as Text-to-SQL or NL2SQL, relies heavily on its semantic parsing technology. It converts natural language (Text) into Structured Query Language (SQL), with the core goal of allowing users to describe their database query needs using everyday language, thereby enabling database queries and operations without requiring prior knowledge of SQL syntax to generate executable SQL statements.
[0003] Currently, there are four main types of technical methods for generating SQL statements.
[0004] Category 1: Rule-based methods: Early research focused primarily on using predefined rules or semantic parsers to understand natural language queries and convert them into SQL queries. This mainly focused on single-table tasks.
[0005] The second category: Neural network-based methods. To address the limitations of rule-based methods, researchers began utilizing neural networks to solve NL2SQL tasks, such as sequence-to-sequence models or graph neural networks. These methods have evolved from single-table scenarios to multi-table scenarios, but the generalization ability of the models remains limited.
[0006] The third category: Methods based on pre-trained language models: With the emergence of pre-trained language models such as BERT and T5, PLM-based NL2SQL methods have achieved competitive performance on multiple benchmark datasets. However, they face challenges in handling complex patterns.
[0007] Category 4: The Era of Large Language Models: With the emergence of LLMs, NL2SQL technology has made significant progress. LLMs have excellent language understanding and emerging capabilities, such as using prompts to perform NL2SQL tasks. Summary of the Invention
[0008] This application provides a large language model-driven Text2SQL generation method and system, which can solve the technical problems existing in the prior art, such as low accuracy of complex multi-table join queries, high error rate of schema linking, and poor cross-domain adaptability.
[0009] Firstly, this application provides a large language model-driven Text2SQL generation method, comprising the following steps:
[0010] By using a large language model to identify the relationships between key entities in the natural language questions input by users, a query graph is constructed.
[0011] Obtain the target database related to the domain, and construct a hierarchical graph representation model based on the database schema of the target database;
[0012] Retrieve candidate subgraph sets from the constructed graph representation model that have the same structure as the query graph and are semantically related to the query graph;
[0013] Based on the retrieved candidate sub-graphets and the natural language questions input by the user, a structured Prompt template is constructed, and an initial SQL statement conforming to the target database pattern is generated as a guide using the structured Prompt template.
[0014] The generated initial SQL statement is subjected to syntax and semantic error detection. The SQL statement is then corrected based on the detected error results, and finally the query results for the natural language question are output.
[0015] Furthermore, the step of identifying the relationships between key entities in the user-input natural language question using a large language model and constructing a query graph specifically includes the following steps:
[0016] Based on the preset Prompt template, the natural language question is structured and parsed using a large language model to identify the relationships between key entities in the question and obtain a query graph composed of triples.
[0017] Furthermore, the acquisition of the domain-related target database and the construction of a hierarchical graph representation model based on the database schema of the target database specifically include the following steps:
[0018] Acquire the target database relevant to the domain;
[0019] The physical structure based on the target database is used to construct a hierarchical graph representation model through hierarchical mapping.
[0020] Furthermore, the step of retrieving candidate subgraph sets from the constructed graph representation model that have the same structure as the query graph and are semantically related to the query graph specifically includes the following steps:
[0021] Based on the VF2 algorithm, structural retrieval is performed in the graph representation model through graph isomorphic matching to obtain multiple candidate subgraphs that are consistent with the topological structure of the query graph.
[0022] The set of candidate subgraphs with semantic relevance is filtered by calculating the semantic relevance between the candidate subgraphs and the retrieval graph.
[0023] Furthermore, the step of filtering the set of semantically related candidate subgraphs by calculating the semantic relevance between the candidate subgraphs and the retrieval graph specifically includes the following steps:
[0024] The semantic similarity between the relation edges in the query graph and the corresponding edges in the candidate subgraph is calculated using cosine similarity.
[0025] The semantic similarity between the query graph node and the corresponding node in the candidate subgraph is calculated using the normalized Poincaré distance.
[0026] The comprehensive semantic similarity between the query graph and the candidate subgraph is obtained by using the semantic similarity of linearly weighted edges and the semantic similarity of nodes.
[0027] Based on the comprehensive semantic similarity between the query graph and the candidate subgraphs, a set of candidate subgraphs that are semantically related to the query graph is selected from multiple candidate subgraphs.
[0028] Furthermore, the step of constructing a structured Prompt template based on the retrieved candidate sub-graphets and the natural language question input by the user, and generating an initial SQL statement conforming to the target database pattern using the structured Prompt template as a guide, specifically includes the following steps:
[0029] Based on the retrieved candidate sub-graphets, a structured context is generated through triple recombination to obtain enhanced pattern linking information;
[0030] By integrating pattern linking information with natural language questions, target database patterns, and example pairs, a Prompt template containing complete context is constructed.
[0031] The Prompt template guides the large language model to initiate a chained reasoning mechanism, gradually deriving and generating initial SQL statements that conform to the target database pattern.
[0032] Furthermore, the process of performing syntactic and semantic error detection on the generated initial SQL statement, correcting the SQL statement based on the detected error detection results, and finally outputting the query results for the natural language problem specifically includes the following steps:
[0033] Detect syntax errors in the initial SQL statement;
[0034] Detect logical errors in the initial SQL statement of the query;
[0035] Detect schema linking errors in the initial SQL statement;
[0036] Error correction is performed on syntax and / or logical errors, and graph retrieval is re-performed on initial SQL statements with incorrect pattern linking to obtain query results for natural language questions.
[0037] Secondly, this application provides a large language model-driven Text2SQL generation system, including:
[0038] The query graph construction module is used to identify the relationships between key entities in the natural language questions input by users through a large language model, and to construct a query graph.
[0039] The graph knowledge base construction module is used to acquire domain-related target databases and construct hierarchical graph representation models based on the database schema of the target databases.
[0040] The candidate subgraph set acquisition module is communicatively connected to the query graph construction module and the graph knowledge base construction module, and is used to retrieve candidate subgraph sets from the constructed graph representation model that have the same structure as the query graph and are semantically related to the query graph;
[0041] The SQL generation module is communicatively connected to the candidate subgraph acquisition module. It is used to construct a structured Prompt template based on the retrieved candidate subgraph and the natural language question input by the user, and generate an initial SQL statement that conforms to the target database pattern based on the structured Prompt template.
[0042] The query result acquisition module is connected to the SQL generation module. It is used to perform syntax and semantic error detection on the generated initial SQL statement, correct the SQL statement based on the detected error detection results, and finally output the query result of the natural language question.
[0043] Furthermore, the candidate sub-graphet acquisition module includes:
[0044] The isomorphic matching unit is communicatively connected to the query graph construction module and the graph knowledge base construction module. It is used to perform structure retrieval in the graph representation model based on the VF2 algorithm through graph isomorphic matching to obtain multiple candidate subgraphs that are consistent with the topological structure of the query graph.
[0045] The candidate subgraph set filtering unit is communicatively connected to the isomorphic matching unit and is used to filter semantically related candidate subgraph sets by calculating the semantic relevance between the candidate subgraphs and the retrieval graph.
[0046] Thirdly, this application provides a computer-readable storage medium storing a large language model-driven Text2SQL generation program, wherein when the large language model-driven Text2SQL generation program is executed by a processor, it implements the steps of the large language model-driven Text2SQL generation method described above.
[0047] The beneficial effects of the technical solutions provided in this application include at least the following:
[0048] This approach involves constructing a graph knowledge base from traditional database systems to improve the accuracy and performance of SQL query generation. It includes a novel graph representation model knowledge system that stores and retrieves relevant tables and columns through a knowledge graph, guiding large models to generate SQL queries that better meet business needs and data structures. During the generation process, an adaptive learning mechanism based on output feedback is used to continuously optimize the accuracy of generated SQL statements through real-time feedback, especially in query scenarios involving large-scale and complex data, enabling continuous self-improvement and customization of the model. Attached Figure Description
[0049] Figure 1 This is a flowchart illustrating the Text2SQL generation method driven by the large language model in this application.
[0050] Figure 2 A flowchart illustrating the Text2SQL generation method provided in this application embodiment;
[0051] Figure 3 A schematic diagram illustrating the transformation of a four-layer database diagram provided in this application embodiment;
[0052] Figure 4 For the embodiments of this application in Figure 3 A diagram illustrating the transformation after relationship completion based on the above;
[0053] Figure 5 This is a schematic diagram of the candidate subgraph generation process provided in an embodiment of this application. Detailed Implementation
[0054] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present application.
[0055] The terms "comprising" and "having," and any variations thereof, in the specification, claims, and accompanying drawings of this application are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or apparatus that includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units not listed, or may optionally include other steps or units inherent to such process, method, product, or apparatus. The terms "first," "second," and "third," etc., are used to distinguish different objects, etc., and do not indicate a sequence, nor do they limit "first," "second," and "third" to different types.
[0056] In the description of the embodiments of this application, terms such as "exemplary," "for example," or "for instance" are used to indicate examples, illustrations, or explanations. Any embodiment or design described as "exemplary," "for example," or "for instance" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of terms such as "exemplary," "for example," or "for instance" is intended to present the relevant concepts in a concrete manner.
[0057] In the description of the embodiments of this application, unless otherwise stated, " / " means "or". For example, A / B can mean A or B. The "and / or" in the text is merely a description of the relationship between related objects, indicating that there can be three relationships. For example, A and / or B can mean: A exists alone, A and B exist simultaneously, and B exists alone. In addition, in the description of the embodiments of this application, "multiple" means two or more.
[0058] In some processes described in the embodiments of this application, multiple operations or steps are included in a specific order. However, it should be understood that these operations or steps may not be executed in the order they appear in the embodiments of this application, or they may be executed in parallel. The sequence number of the operation is only used to distinguish different operations, and the sequence number itself does not represent any execution order. In addition, these processes may include more or fewer operations, and these operations or steps may be executed sequentially or in parallel, and these operations or steps may be combined.
[0059] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.
[0060] Firstly, such as Figure 1 and Figure 2 As shown, this application provides a large language model-driven Text2SQL generation method for specific BI (Business Intelligence) scenarios and large-scale complex data, specifically including the following steps:
[0061] Step S1: Identify the relationships between key entities in the natural language question input by the user through a large language model, and construct a query graph for retrieving similar values in the database and pattern description;
[0062] Step S2: Obtain the target database related to the domain, and construct a hierarchical graph representation model based on the database schema of the target database;
[0063] Step S3: Retrieve candidate subgraph sets from the constructed graph representation model that have the same structure as the query graph and are semantically related to the query graph;
[0064] Step S4: Based on the retrieved candidate sub-graphets and the natural language questions input by the user, construct a structured Prompt template, and use the structured Prompt template as a guide to generate initial SQL statements that conform to the target database pattern.
[0065] Step S5: Perform syntax and semantic error detection on the generated initial SQL statement, correct the SQL statement based on the detected error detection results, and finally output the query results for the natural language problem.
[0066] This application achieves efficient conversion of natural language to SQL statements through the synergistic effect of query graphs and hierarchical graph representation models. It accurately captures the correspondence between user query intent and target database schema using graph structure matching mechanism, generates complex SQL queries that conform to business logic by combining structured Prompt templates, and ensures the correctness and executability of statements through multi-level validation mechanisms. This significantly improves the efficiency and accuracy of large-scale data analysis in BI scenarios, while also possessing good domain adaptability and scalability.
[0067] In one embodiment, step S1: identifying the relationships between key entities in the user-input natural language question using a large language model to construct a query graph, specifically includes the following steps:
[0068] By providing a sample Prompt template for LLM (Large Language Model) containing natural language questions and extracting query graphs, the system performs structured analysis on user-input natural language questions, identifies relationships between key entities in the question, and constructs a query graph output in a triple format corresponding to the semantics of the natural language question. This provides effective input for subsequent graph retrieval and reasoning processes. Specifically, firstly, specific prompts (such as thought linking prompts or role prompts) are designed to guide the large language model to decompose the user question task into two sub-tasks: identifying the relationships between entities and key entities. Subsequently, the semantics are reconstructed using (entity 1-relationship-entity 2) triples. For example, in a query containing "student" and "course", the triples would be represented as ("student", "selection", "course"), clearly reflecting the semantic association between the entities and key entities. The construction of the query graph relies on the LLM's ability to understand language and also requires the integration of domain-specific knowledge to accurately analyze the context and details of the question. To address this, a targeted Prompt Engineering approach was designed to optimize the LLM's performance in semantic understanding and entity relationship inference, thereby generating accurate and efficient query graph representations. The specific Prompt template is shown below:
[0069] 1. Interpret the natural language question {{QUERY}} input by the user, clarify the core points and specific details of the question, identify the key entities involved (such as time, place, people, etc.) and the relationships between the key entities.
[0070] 2. Return the extracted query graph structure in the following format:
[0071] {
[0072] "Triplet":[["Entity 1","Relation","Entity 2"],...
[0073] ]}
[0074] 3. Do not provide additional explanations or descriptions, such as "The following is the extracted query graph structure," etc. Example: Question: List the student IDs and names. Output: {
[0075] "Triplet":[["Student","Owns","Student ID"],["Student","Owns","Name"]
[0076] ]}
[0077] Follow the above precautions and examples step by step to extract the query graph structure based on the natural language problem.
[0078] In one embodiment, step S2: obtaining a domain-related target database and constructing a hierarchical graph representation model based on the database schema of the target database, specifically includes the following steps:
[0079] Step S21: Obtain the relevant target database based on the domain involved in the natural language question input by the user;
[0080] Step S22: Based on the physical structure of the target database, a multi-layered graph representation model is constructed through hierarchical mapping, namely GraphRAG (Graph-based Retrieval Enhancement Generative Model), or database graph, such as... Figure 3 As shown, in the multi-layer graph structure, the first layer is the database, the second layer is the table, and the third layer is the column. The specific data in each row is connected to the corresponding column node to form the fourth layer of the graph, forming the relationship between data rows and columns, ensuring that all information in the original SQL database is preserved. In this way, the graph structure not only preserves the hierarchical pattern of the database, but also ensures the close connection between each row of data and its column definition.
[0081] The graph representation model, built using the target database schema, fully preserves key information such as table and column names in the target database. A hierarchical modeling strategy is employed in designing the graph representation model, systematically mapping the hierarchical information of the target database through a four-level graph structure. Specifically, the graph structure is constructed as follows: the first level represents the database name nodes, used to identify the overall structure and scope of the database; the second level consists of table name nodes, used to represent the organizational structure of each table in the database; the third level contains column name nodes, providing precise descriptions of the fields in each table; and the fourth level contains the specific information nodes for each row in the database tables, such as... Figure 4 As shown, to represent the relationship between row data and table structure in the database, each row of data is represented as an independent node, and these nodes are connected to the corresponding column nodes through edges, thus forming the relationship between data rows and columns. This design can reflect all the information in the original relational database, covering not only the database's structural information but also the specific row data content. The advantage of the four-level graph structure is that it can tightly link the structural information and data content of the relational database while preserving the original semantics of the database. This method also has strong scalability and query flexibility. For example, column nodes can further include additional information such as field types, constraints (such as primary keys and foreign keys), and default values to enhance the semantic expressiveness of the graph representation model. At the same time, the addition of row data nodes allows queries to directly operate on specific data, supporting more complex retrieval tasks.
[0082] In one embodiment, such as Figure 5 As shown, step S3, retrieving candidate subgraph sets from the constructed graph representation model that have the same structure as the query graph and are semantically related to the query graph, specifically includes the following steps:
[0083] Step S31: Based on the VF2 algorithm, perform structure retrieval in the graph representation model through graph isomorphic matching to obtain K candidate subgraphs that are consistent with the topological structure of the query graph;
[0084] Step S32: Filter the set of semantically related candidate subgraphs by calculating the semantic relevance between the candidate subgraphs and the retrieval graph; specifically, this includes the following steps:
[0085] Step S321: For edge nodes, use the Nomic Embedding model for embedding, and use cosine similarity to calculate the semantic distance between the relation edge in the query graph and the corresponding edge in the candidate subgraph to obtain the semantic distance between edge pairs;
[0086] Step S322: To preserve the hierarchical information of nodes, the semantic distance between the query graph node and the corresponding node of the candidate subgraph is calculated using the normalized Poincaré distance to obtain the semantic distance between node pairs. The Poincaré distance ranges from 1 to infinity and is used to calculate the semantic distance of the entire graph in the next step. Therefore, the Poincaré distance is normalized.
[0087] Step S323: Based on the weighted summation of the semantic distance between edge pairs and the semantic distance between node pairs using PMD (Perona-Malik Diffusion, a mathematical model for image processing and feature enhancement), the comprehensive semantic similarity between the query graph and the candidate subgraph is obtained. That is, the comprehensive semantic similarity between the query graph and the candidate subgraph is measured by Pairwise Matching Distance.
[0088] Step S324: Based on comprehensive semantic similarity, select a set of candidate subgraphs with high relevance to the query graph whose semantic relevance is higher than the comprehensive semantic relevance threshold from multiple candidate subgraphs. Specifically, select the top N candidate subgraphs with the highest similarity and output them as triples, which are then used as the core content input to the LLM in the next step, Prompt. This method is not just about embedding a single node or edge, but rather mapping the entire graph into a low-dimensional vector space, capturing the global structural features of the graph. By expressing the overall features of the graph, graph embedding can better measure the similarity between the query graph and candidate subgraphs, thereby improving the accuracy of retrieval in complex semantic queries. This not only preserves the structural features of the query graph but also retains semantic information, making the similarity comparison of the query graph more efficient and allowing for more accurate selection of the tables and columns that best match the natural language problem. This global embedding is very suitable for handling complex cross-table queries, such as multi-table joins or nested queries.
[0089] In one embodiment, step S4: Based on the retrieved candidate sub-graphets and the natural language question input by the user, a structured Prompt template is constructed, and an initial SQL statement conforming to the target database pattern is generated using the structured Prompt template as a guide. This specifically includes the following steps:
[0090] Step S41: Based on the retrieved candidate sub-graph sets, a structured context is generated through triple recombination to obtain enhanced pattern link information;
[0091] Step S42: Integrate schema linking information with natural language questions, target database schemas, example pairs, and imperative prompts to construct a Prompt template containing complete context; Natural language questions: Directly include user-input natural language questions as the primary input for solving the problem; Convert candidate subgraphs retrieved from the GraphRAG knowledge base into triples, providing explicit entity, relation, and schema information. The entities in these triples contain the table column information required by Text2SQL, establishing schema links for text-to-SQL conversion and helping the model more accurately query the required tables and columns when generating SQL; Add schema information from the target database (such as table names, column names, foreign key relationships, etc.) to provide structural constraints for the generated SQL, ensuring that the query statement is syntactically consistent with the actual architecture of the target database. This constraint is particularly important when dealing with complex database scenarios, effectively reducing schema linking errors. Providing few-shot Text-to-SQL example pairs as examples in the Prompt template enhances the LLM's understanding and learning ability of the mapping relationship between problem semantics and SQL logic. This few-shot learning strategy can significantly improve the model's generation accuracy and generalization ability. Clear, imperative prompts are used to specify the task objective and expected output format, such as "Generate a query statement conforming to SQL standards based on the following information" or "Complete the missing query conditions based on triples and the database schema." Simultaneously, a chain of thought guides the LLM step-by-step. These instructions further reduce the ambiguity of the generated results and improve the model's goal consistency.
[0092] Step S43: The Prompt template guides the large language model to start the chain reasoning mechanism, gradually deriving and generating initial SQL statements that conform to the target database schema. Specifically, the initial SQL statements explicitly define the table structure, field types, and constraints (such as primary key and NOT NULL) through the data definition language, ensuring that they strictly match the logical model of the target database schema.
[0093] Through a multi-layered, multi-information fusion-based Prompt template design, LLM can comprehensively utilize the context of natural language questions, retrieved semantic triples, example guidance, and chains of thought to generate accurate and context-sensitive SQL statements. Compared with traditional methods, it effectively improves the accuracy, robustness, and task adaptability of SQL generation, providing strong technical support for natural language to SQL conversion tasks.
[0094] The specific Prompt template is shown below:
[0095] Task Description: You are a proficient SQL expert, skilled at understanding complex natural language problems and transforming them into structured SQL statements. You are now presented with a natural language problem and some triples containing database schema information. Based on the provided information, generate an SQL query that is semantically consistent with the problem and fully conforms to the database schema and SQL syntax rules.
[0096] Database schema:
[0097] {DATABASE_SCHEMA}
[0098] This includes the table names, column names, foreign key relationships, and their detailed descriptions in the target database. Ensure you understand this information to generate queries that match the database schema.
[0099] Triples retrieved:
[0100] {TRIPLES_FROM_GRAPH}
[0101] Entity nodes may contain table names, column names, and specific information for each row corresponding to the database schema; relations may contain the correspondence between table columns and entities; for example, ["Zhang San", "belongs to", "name"], ["name", "belongs to", "student table"], contains the information that Zhang San belongs to the "name" column, and the "name" column is in the "student table". Carefully reading this information can help you find the tables and columns needed to complete the task, especially when you cannot find the tables or columns mentioned in the natural language problem.
[0102] Problem description:
[0103] {USER_QUESTION}
[0104] The user's natural language question describes the specific query task that needs to be solved.
[0105] Example for reference:
[0106] {EXAMPLES} Some validated "Natural Language-Triples-SQL" examples can help understand the semantic and logical mappings for similar tasks. Please read and study them carefully.
[0107] Output requirements: Return the generated results in JSON format, with the following structure:
[0108] {"chain of thought_reasoning":"The logical approach to progressively transforming a natural language problem into a final SQL query.","SQL":"The generated SQL query statement, represented as a string."}
[0109] When a problem is complex, think step by step, break it down, and generate SQL queries gradually to ensure that the logic is clear and the syntax is correct.
[0110] The generated SQL query is consistent with the problem context.
[0111] Reward Mechanism:
[0112] Carefully read the input information and generate the correct SQL query as required. If all requirements are met, the generated result will be evaluated as high-quality.
[0113] In one embodiment, to ensure that the SQL statements generated by the Large Scale Language Model (LLM) conform to grammatical norms, logical rationality, and pattern consistency, a comprehensive error detection and classification correction mechanism is designed. This mechanism performs multi-level analysis and iterative optimization on the generated SQL, including error detection, classification processing, and adaptive error correction strategies, to improve the accuracy and reliability of the final output SQL. Step S5: Performing grammatical and semantic error detection on the generated initial SQL statement, correcting the SQL statement based on the detected error detection results, and finally outputting the query results for the natural language question, specifically includes the following steps:
[0114] Step S51: Detect syntax errors in the initial SQL statement, including basic syntax problems such as misspelled SQL keywords, incomplete statement structure (e.g., missing SELECT, WHERE clauses, etc.), and mismatched parentheses.
[0115] Step S52: Detect the semantic matching degree between the initial SQL statement and the natural language problem, such as logical contradictions in the conditional expression, unreasonable application of aggregate functions, misuse of column names or table names, etc., which may lead to logical errors in the query results that deviate from the expected semantics.
[0116] Step S53: Check the correctness of table / column references in the initial SQL statement, and detect schema linking errors caused by the inconsistency between the generated SQL and the target database schema (e.g., accessing non-existent tables or columns, ignoring foreign key relationships, etc.), which prevent the query statement from being correctly mapped to the actual database structure.
[0117] Step S54: Correct syntax and / or logical errors, re-perform graph retrieval on the initial SQL statement with pattern linking errors, and obtain query results for the natural language question; specifically:
[0118] Syntax error correction: Based on a predefined set of rules, it automatically identifies common SQL syntax errors and makes local adjustments based on the overall model. Examples include completing missing keywords, correcting spelling errors, or adjusting bracket matching.
[0119] Logical error correction: Leveraging the contextual understanding capabilities of the large model, and combining the semantics of user questions with the logical analysis of query statements, rules are formulated to correct logical inconsistencies. For example, the rationality of conditional expressions is re-validated, consistency between aggregate functions and GROUPBY clauses is ensured, and the model is guided to generate revised SQL statements.
[0120] Schema linking error correction: To address the inconsistency issue in the database schema, we return to the graph retrieval process in step S4. By matching more precise subgraphs, we obtain new schema information, update the query graph, and generate revised SQL. This iterative process ensures that the generated SQL statements are highly consistent with the structure of the target database at both the semantic and schema levels.
[0121] Secondly, this application provides a large language model-driven Text2SQL generation system, including:
[0122] The query graph construction module is used to identify the relationships between key entities in the natural language questions input by users through a large language model, and to construct a query graph.
[0123] The graph knowledge base construction module is used to acquire domain-related target databases and construct hierarchical graph representation models based on the database schema of the target databases.
[0124] The candidate subgraph set acquisition module is communicatively connected to the query graph construction module and the graph knowledge base construction module, and is used to retrieve candidate subgraph sets from the constructed graph representation model that have the same structure as the query graph and are semantically related to the query graph;
[0125] The SQL generation module is communicatively connected to the candidate subgraph acquisition module. It is used to construct a structured Prompt template based on the retrieved candidate subgraph and the natural language question input by the user, and generate an initial SQL statement that conforms to the target database pattern based on the structured Prompt template.
[0126] The query result acquisition module is connected to the SQL generation module. It is used to perform syntax and semantic error detection on the generated initial SQL statement, correct the SQL statement based on the detected error detection results, and finally output the query result of the natural language question.
[0127] In one embodiment, the graph knowledge base construction module includes:
[0128] The structure graph acquisition unit is used to construct a multi-layer graph structure based on the physical structure of the target database through hierarchical mapping, and obtain a hierarchical graph containing database name nodes, table name nodes, column name nodes and data row nodes.
[0129] The vector representation acquisition unit is used to embed the foreign key relationship edges between tables through the Nomic Embedding model to obtain the vector representation of the relationship edges;
[0130] The node representation acquisition unit is used to perform hyperbolic space embedding on the hierarchical nodes of the database-table-column-data row through the Poincaré Embedding model to obtain node representations that preserve the data hierarchy.
[0131] The graph knowledge base construction unit is communicatively connected to the structure graph acquisition unit, the vector representation acquisition unit, and the node representation acquisition unit, and is used to construct a hierarchical graph representation model based on the multi-layer graph structure, the vector representation of relation edges, and the node representation.
[0132] The functions of each module in the large language model-driven Text2SQL generation system mentioned above correspond to the steps in the above large language model-driven Text2SQL generation method embodiment, and their functions and implementation processes will not be described in detail here.
[0133] Thirdly, embodiments of this application provide a large language model-driven Text2SQL generation device, which can be a personal computer (PC), laptop computer, server, or other device with data processing capabilities.
[0134] The communication interface includes input / output (I / O) interfaces, physical interfaces, and logical interfaces used for interconnecting internal devices within the large language model-driven Text2SQL generation device, as well as interfaces used for interconnecting the large language model-driven Text2SQL generation device with other devices (such as other computing devices or user equipment). Physical interfaces can be Ethernet interfaces, fiber optic interfaces, ATM interfaces, etc.; user equipment can be displays, keyboards, etc.
[0135] Memory can be various types of storage media, such as random access memory (RAM), read-only memory (ROM), non-volatile RAM (NVRAM), flash memory, optical storage, hard disk, programmable ROM (PROM), erasable PROM (EPROM), electrically erasable PROM (EEPROM), etc.
[0136] The processor can be a general-purpose processor, which can call the large language model-driven Text2SQL generation program stored in memory and execute the large language model-driven Text2SQL generation method provided in the embodiments of this application. For example, the general-purpose processor can be a central processing unit (CPU). The method executed when the large language model-driven Text2SQL generation program is called can be referred to the various embodiments of the large language model-driven Text2SQL generation method of this application, and will not be repeated here.
[0137] Fourthly, embodiments of this application also provide a readable storage medium.
[0138] This application stores a large language model-driven Text2SQL generation program on a readable storage medium, wherein when the large language model-driven Text2SQL generation program is executed by a processor, it implements the steps of the large language model-driven Text2SQL generation method described above.
[0139] The method implemented when the large language model-driven Text2SQL generator is executed can be referred to in the various embodiments of the large language model-driven Text2SQL generation method of this application, and will not be repeated here.
[0140] It should be noted that the sequence numbers of the embodiments in this application are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0141] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) as described above, and includes several instructions to cause a terminal device to execute the methods described in the various embodiments of this application.
[0142] The above are merely preferred embodiments of this application and do not limit the patent scope of this application. Any equivalent structural or procedural transformations made using the content of this application's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of this application.
Claims
1. A large language model driven Text2SQL generation method, characterized in that, The method comprises the following steps: A query graph is constructed by identifying the relationship between key entities in the natural language question input by the user through a large language model; A target database related to the field is obtained, and a hierarchical graph representation model is constructed based on the database schema of the target database, wherein the first layer of the graph representation model is a database name node for identifying the overall structure and scope of the database; The second layer is a table name node for representing the organizational structure of each table in the database; the third layer is a column name node for describing the fields of each table, and the fourth layer is a specific information node of each row in the database table, which represents the specific data of each row as an independent node, and connects each row node to the corresponding column node through an edge to form the relationship between the data row and the column; A candidate subgraph set that is the same in structure and semantically associated with the query graph is retrieved from the constructed graph representation model; Based on the retrieved candidate subgraph set and the natural language question input by the user, a structured Prompt template is constructed, and an initial SQL statement conforming to the target database schema is generated guided by the structured Prompt template; The generated initial SQL statement is detected for errors in syntax and semantics, and the detected error detection results are corrected to finally output the query result of the natural language question; The retrieval of the candidate subgraph set that is the same in structure and semantically associated with the query graph from the constructed graph representation model comprises: Structural retrieval is performed in the graph representation model based on the VF2 algorithm through graph isomorphism matching to obtain multiple candidate subgraphs consistent with the topology structure of the query graph; The semantic association degree of the candidate subgraph and the retrieval graph is calculated to screen the candidate subgraph set associated in semantics; specifically, the semantic similarity of the relationship edges in the query graph and the corresponding edges of the candidate subgraph is calculated by cosine similarity; the semantic similarity of the query graph nodes and the corresponding nodes of the candidate subgraph is calculated by the normalized Poincare distance; the comprehensive semantic similarity between the query graph and the candidate subgraph is obtained by linearly weighting the semantic similarity of the edges and the semantic similarity of the nodes; and the candidate subgraph set associated in semantics with the query graph is screened from the multiple candidate subgraphs according to the comprehensive semantic similarity between the query graph and the candidate subgraph.
2. The large language model driven Text2SQL generation method of claim 1, wherein, The construction of the query graph by identifying the relationship between key entities in the natural language question input by the user through a large language model comprises the following steps: The natural language question is structured and parsed through a large language model based on a preset Prompt template to identify the relationship between key entities in the question and obtain a query graph composed of triples.
3. The large language model driven Text2SQL generation method of claim 1, wherein, The acquisition of the target database related to the field and the construction of the hierarchical graph representation model based on the database schema of the target database comprises the following steps: A target database related to the field is obtained; A hierarchical graph representation model is constructed based on the physical structure of the target database through hierarchical mapping.
4. The large language model-driven Text2SQL generation method of claim 1, wherein, Based on the retrieved candidate subgraph set and the natural language question input by the user, a structured Prompt template is constructed, and an initial SQL statement conforming to the target database schema is generated guided by the structured Prompt template; Generate structured context based on the retrieved candidate subgraph set through triple reorganization, and obtain enhanced schema linking information; Fuse schema linking information with natural language questions, target database schema and example pairs to build Prompt templates containing complete context; Guide large language models to start chain reasoning mechanism through the Prompt templates, and gradually deduce to generate initial SQL statements conforming to the target database schema.
5. The large language model-driven Text2SQL generation method of claim 1, wherein, The generated initial SQL statements are detected for syntax and semantic errors, and the detected error detection results are corrected to output the query results of the natural language question, specifically including the following steps: Detect syntax errors in the initial SQL statements; Detect logical errors in the query initial SQL statements; Detect schema linking errors in the initial SQL statements; Correct syntax and / or logical errors, and re-search graph for the initial SQL statements with schema linking errors to obtain the query results of the natural language question.
6. A large language model driven Text2SQL generation system, characterized in that, The method implementation of claim 1 comprises: A query graph construction module for identifying relationships between key entities in the user input natural language question through a large language model, and constructing a query graph; A graph knowledge base construction module for obtaining a domain-related target database, and constructing a hierarchical graph representation model based on the database schema of the target database; A candidate subgraph set acquisition module in communication connection with the query graph construction module and the graph knowledge base construction module, for retrieving a candidate subgraph set with the same structure as the query graph and associated with the query graph semantics from the constructed graph representation model; An SQL generation module in communication connection with the candidate subgraph set acquisition module, for constructing a structured Prompt template based on the retrieved candidate subgraph set and the user input natural language question, and generating initial SQL statements conforming to the target database schema guided by the structured Prompt template; A query result acquisition module in communication connection with the SQL generation module, for detecting syntax and semantic errors of the generated initial SQL statements, correcting the detected error detection results, and finally outputting the query results of the natural language question.
7. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a large language model driven Text2SQL generation program, wherein the large language model driven Text2SQL generation program is executed by the processor to implement the steps of the large language model driven Text2SQL generation method of any one of claims 1 to 5.
Citation Information
Patent Citations
Knowledge graph-based traffic engineering large model intelligent question-answering system and method
CN120407752A
Multi-table joint natural language query SQL generation method
CN120872982A
Large model query generation system and method combining graph structure analysis and execution
CN120950738A