Natural language database query method and system based on semantic closed-loop verification

By using a semantic closed-loop verification mechanism, the system automatically identifies and corrects natural language query intent, solving the SQL generation errors caused by the semantic gap in existing technologies and improving the accuracy and efficiency of natural language database queries.

CN121786050APending Publication Date: 2026-04-03JIANGSU FINANCIAL DIGITAL GROUP ARTIFICIAL INTELLIGENCE TECHNOLOGY CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-12-11
Publication Date
2026-04-03

AI Technical Summary

Technical Problem

In existing technologies, there is a semantic gap between natural language understanding of database schema and user's true intent, which causes the system to fail to accurately identify the user's query intent and generate incorrect SQL statements.

Method used

A semantic closed-loop verification mechanism is adopted. The query elements are extracted through a semantic parsing model, SQL statements are generated and reverse parsed into natural language for consistency comparison. The correction loop is automatically triggered until semantic consistency is achieved, and an accurate SQL statement is generated.

Benefits of technology

It effectively identifies and corrects ambiguities in natural language or comprehension biases in large language models, improving the accuracy of SQL generation, enhancing system reliability and efficiency, and eliminating the need for manual intervention.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121786050A_ABST
    Figure CN121786050A_ABST
Patent Text Reader

Abstract

The invention discloses a natural language database query method and system based on semantic closed-loop verification. The method comprises the following steps: receiving an initial natural language; semantic analysis is carried out through the semantic analysis model, and query elements are extracted; generating a first SQL statement based on the query element; reversely analyzing the first SQL statement into a first natural language; performing consistency comparison on the first natural language and the initial natural language; and when the semantics are inconsistent, generating comparison difference information, triggering a correction cycle step, and if the comparison results are consistent or the comparison results become consistent after one or more correction cycles, executing a corresponding SQL statement to obtain a query result. Through a closed-loop verification mechanism of SQL reverse analysis and semantic consistency comparison, SQL generation errors caused by natural language ambiguity or LLM understanding deviation can be effectively recognized and corrected, and the problem of poor accuracy is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of artificial intelligence and database technology, and in particular to a natural language database query method and system based on semantic loop closure verification. Background Technology

[0002] In the era of artificial intelligence, interacting with databases through natural language has become an important human-computer interaction method, primarily achieved through Text-to-SQL technology. This technology has evolved from early rule-based systems to intelligent solutions that integrate large language models.

[0003] In the process of developing the existing technology, the inventors discovered that: The inherent ambiguity of natural language, along with the complex context and specialized knowledge involved in user queries, makes large language models prone to biases when understanding database schemas and users' true intentions. For example, if a user enters "query the banks with the highest loan amounts last quarter," the system might incorrectly interpret it as "query all banks and sort them on the client side," instead of using ORDER BY and LIMIT at the database level to find the highest value. Once such errors due to semantic gaps occur, existing systems lack effective mechanisms to detect and correct them, leaving users with incorrect query results and casting doubt on the system's reliability.

[0004] Therefore, this application provides a technical solution to improve the accuracy of natural language systems, in order to solve the technical problem that the semantic gap in natural language systems leads to inaccurate recognition in the prior art. Summary of the Invention

[0005] This application provides a technical solution to improve the accuracy of natural language systems, thereby solving the technical problem in the prior art where the semantic gap in natural language leads to inaccurate recognition.

[0006] Specifically, a natural language database query method based on semantic loop closure verification includes the following steps: Receive initial natural language; Semantic parsing is performed using a semantic parsing model to extract query elements; Based on the query elements, generate the first SQL statement; The first SQL statement is parsed in reverse into the first natural language. Perform a consistency comparison between the first natural language and the initial natural language; When semantic inconsistencies occur, comparison difference information is generated, triggering a correction loop step, which includes: Based on the comparison difference information, the initial natural language is re-parsed to obtain updated query elements; Based on the updated query elements, generate a second SQL statement; The second SQL statement is parsed in reverse into the second natural language. Perform a semantic consistency comparison between the second natural language and the initial natural language; If the comparison results are consistent, or if the comparison results become consistent after one or more correction cycles, then the corresponding SQL statement is executed to obtain the query results.

[0007] Furthermore, the step of reversing the first SQL statement into the first natural language specifically includes: Parse the syntax structure of the first SQL statement; Map the components in the grammatical structure to predefined natural language templates; Based on the mapping, the first natural language is generated.

[0008] Furthermore, the consistency comparison specifically includes: Calculate the semantic similarity between the first natural language and the initial natural language; When the semantic similarity is lower than a preset threshold, it is determined to be inconsistent.

[0009] Furthermore, the step of re-parseing the initial natural language based on the comparison difference information to obtain updated query elements specifically includes: Based on the comparison difference information, the parameters of the semantic parsing model are adjusted to generate updated query elements; Alternatively, based on the comparison difference information, the updated query elements can be directly corrected and output.

[0010] Furthermore, the semantic parsing model is implemented using a Large Language Model (LLM).

[0011] This application also provides a natural language database query system based on semantic loop closure verification, including: The acquisition module is configured to receive the initial natural language. The LLM parsing module is configured to perform semantic parsing using a semantic parsing model to extract query elements. The SQL generation module is configured to generate a first SQL statement based on the query elements; The SQL reverse parsing module is configured to reverse parse the first SQL statement into the first natural language. A consistency verification module is configured to perform a consistency comparison between the first natural language and the initial natural language. The iterative control module is configured to generate alignment difference information and trigger a correction loop step when semantic inconsistencies occur. The correction loop step includes: Based on the comparison difference information, the initial natural language is re-parsed to obtain updated query elements; Based on the updated query elements, generate a second SQL statement; The second SQL statement is parsed in reverse into the second natural language. Perform a semantic consistency comparison between the second natural language and the initial natural language; If the comparison results are consistent, or if the comparison results become consistent after one or more correction cycles, then the corresponding SQL statement is executed to obtain the query results.

[0012] Furthermore, the system also includes a query module configured to execute the corresponding SQL statement to obtain query results when the semantics are consistent.

[0013] Furthermore, the semantic parsing is implemented using a large language model (LLM).

[0014] The technical solution provided in this application has at least the following beneficial effects: Through a closed-loop verification mechanism of "SQL reverse parsing and semantic consistency comparison", it can effectively identify and correct SQL generation errors caused by natural language ambiguity or LLM comprehension bias, thus solving the problem of poor accuracy.

[0015] When the verification fails, a correction loop is automatically triggered based on the comparison difference information, eliminating the need for manual modification and improving the efficiency of closed-loop verification. Attached Figure Description

[0016] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings: Figure 1 A flowchart illustrating a natural language database query method based on semantic loop closure verification, provided for an embodiment of this application.

[0017] Figure 2 This is a schematic diagram of a natural language database query system based on semantic loop closure verification, provided as an embodiment of this application. Detailed Implementation

[0018] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below in conjunction with specific embodiments and corresponding drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0019] Please refer to Figure 1 This application discloses a natural language database query method based on semantic loop closure verification, including the following steps: S110: Receive initial natural language.

[0020] The system receives initial natural language input from the user through a user interface (such as a client, voice assistant, etc.). It is understood that the natural language referred to here includes at least common languages ​​such as Chinese and English.

[0021] S120: Semantic parsing is performed using a semantic parsing model to extract query elements.

[0022] Specifically, the semantic parsing model is implemented using a Large Language Model (LLM). A Large Language Model (LLM) is a deep learning-based artificial intelligence model that learns rich linguistic knowledge, grammatical structures, and contextual understanding through training on massive amounts of text data. Typical LLMs (such as the GPT series, BERT, and LLaMA) employ the Transformer architecture, with self-attention as its core mechanism, enabling them to efficiently understand and generate human language.

[0023] The core capability of LLM lies in its powerful semantic understanding and content generation capabilities. It can not only identify keywords in text, but also deeply understand the nuances of language, contextual logic, and the user's underlying intent. This makes LLM ideal as the core engine for Natural Language Processing (NLP) tasks such as translation, summarization, question answering, and, as discussed in this invention, the conversion from natural language to structured query statements (Text-to-SQL).

[0024] It should be noted that deep semantic parsing is performed on the received initial natural language. This process aims to understand the core intent of the user's query and extract structured query elements from it.

[0025] For example, when a banking professional inputs the query "Could you please check which banks in Suzhou have issued more than 5 billion yuan in total 'first-time loans' and 'credit loans' since the beginning of this year, and sort them by amount from highest to lowest?", semantic parsing can extract query elements including, but not limited to: target entity (e.g., "bank"), filtering conditions (e.g., "region = Suzhou", "date >= beginning of this year"), aggregation operations (e.g., "total amount issued"), grouping conditions (e.g., "by bank name"), and sorting requirements (e.g., "amount from highest to lowest").

[0026] S130: Generate a first SQL statement based on the query elements.

[0027] Specifically, based on the extracted query elements, a standardized first SQL statement is dynamically combined and generated. Due to the complexity of natural language, misunderstandings may occur during the initial generation of this step.

[0028] Based on the elements of the above query, an incorrect first SQL statement may be generated. For example: 1. The generated first SQL statement (with errors): SELECT Bank Name, SUM(Amount Disbursed) AS Total Amount FROM table_first_loan -- Only applies to the 'first loan' table WHERE region = 'Suzhou' AND date >= '2025-01-01' GROUP BY Bank Name HAVING SUM (Amount Spent) > 500,000,000; This SQL query only counted the "first loan" data, completely missing the query and merging of the "credit loan" data, resulting in the final statistical amount being far lower than the total value requested by the user.

[0029] 2. The first generated SQL statement (contains errors): SELECT Bank Name FROM Loan Record Form WHERE region = 'Suzhou' AND date >= '2025-01-01' AND Loan Type = 'First-Time Loan' -- AND was used incorrectly. AND Loan Type = 'Credit Loan' -- The same field cannot be equal to two values ​​simultaneously; this condition is logically contradictory. GROUP BY Bank Name HAVING SUM (Amount Spent) > 500,000,000; This SQL statement contains a fundamental logical error in its WHERE clause. The loan type field cannot simultaneously be both "first-time loan" and "credit loan," resulting in an always empty query result. This typically reflects a model's misinterpretation of the logical relationships expressed by the conjunction "or" in natural language.

[0030] S140: Reverse parse the first SQL statement into the first natural language.

[0031] Specifically, after generating the first SQL statement, it is not immediately parsed into the first natural language. Instead, the syntax structure of the first SQL statement is parsed first, and the components in the syntax structure are mapped to predefined natural language templates. Based on the mapping, the first natural language is generated.

[0032] Specifically, the syntax structure of the first SQL statement is parsed, including the parsing of keywords or conditions such as SELECT, FROM, and WHERE. These keywords or conditions can be referred to as components. After parsing, these components are mapped to predefined natural language templates, ultimately generating the first natural language.

[0033] A predefined natural language template is a structured text rule base that defines the mapping relationship between SQL syntax elements and human-readable natural language fragments. Its working principle can be understood as a "fill-in" mechanism. A predefined natural language template consists of fixed text and variable slots. The fixed text is a standard phrase describing SQL operations, such as "query," "display," "filter," "group by," "sort by," etc. Variable slots are used to fill in specific elements parsed from the SQL statement, such as [table name], [field name], [condition value], etc.

[0034] Mapping and Generation Process: When the reverse parsing module receives an SQL statement, it first performs syntax analysis to identify each clause (such as SELECT, FROM, WHERE, GROUP BY, ORDER BY). Then, based on the identified clause types, it selects the corresponding template and fills the parsed components into the variable slots, thereby assembling a complete natural language description.

[0035] The first natural language parsed by this reverse parsing is generated based on the parsing of the first SQL statement. This means that if the first SQL statement has an error, the first natural language will also have an error. For example, the first natural language parsed from the first "generated first SQL statement (with errors)" above would be: "Query the data in the 'table_first_loan' table with the region 'Suzhou City' and the date after '2025-01-01', group by 'bank name', filter out groups with a total 'loan amount' greater than 5 billion, and sort them in descending order by 'total amount'." S150: Perform a consistency comparison between the first natural language and the initial natural language.

[0036] Specifically, the consistency comparison includes: calculating the semantic similarity between the first natural language and the initial natural language; when the semantic similarity is lower than a preset threshold, it is determined to be inconsistent.

[0037] The preset threshold is a key quantitative benchmark used by the consistency verification module to determine the semantic consistency between the user's initial natural language and the natural language description generated by the system. It is a configurable value between 0 and 1 (e.g., 0.85, 0.9, 0.95), representing the minimum passing score for semantic similarity.

[0038] In a preferred embodiment provided in this application, the preset threshold is 0.9. When the calculated semantic similarity is higher than or equal to the preset threshold, the system determines it as "semantically consistent," believing that the generated SQL statement accurately reflects the user's intent, and the process enters the query execution stage. When the semantic similarity is lower than the preset threshold, the system determines it as "semantically inconsistent," and then triggers a correction loop step.

[0039] For example, the system will find that in the user's original sentence, "'first loan' and 'credit loan' combined, only "first loan" (table_first_loan) remains in the reverse parsing result. This means that the semantic similarity is definitely far below the preset threshold, thus identifying the core difference that "credit loan" is missing.

[0040] S160: When semantic inconsistency occurs, comparison difference information is generated, triggering a correction loop step. The correction loop step includes: S1601: Based on the comparison difference information, the initial natural language is re-parsed to obtain updated query elements; S1602: Based on the updated query elements, a second SQL statement is generated; S1603: The second SQL statement is reverse-parsed into a second natural language; S1604: The second natural language and the initial natural language are semantically consistent; S1605: If the comparison results are consistent, or if the comparison results become consistent after one or more correction loops, the corresponding SQL statement is executed to obtain the query results.

[0041] Based on the comparison difference information, the initial natural language is re-parsed to obtain updated query elements. Specifically, this includes: adjusting the parameters of the semantic parsing model based on the comparison difference information to generate updated query elements; or, directly correcting and outputting the updated query elements based on the comparison difference information.

[0042] Specifically, in a preferred embodiment provided in this application, the comparison difference information will show that "credit loan query" is missing. Based on this information, the semantic parsing model adjusts its internal parameters to pay more attention to the ignored entities, or directly corrects and outputs updated query elements based on the difference information to ensure that this parsing can simultaneously include "first loan" and "credit loan".

[0043] After re-parsing, the updated query elements (including "first-time loan" and "credit loan") are obtained. Based on the query elements, a second SQL statement is generated. The second SQL statement is then parsed into a second natural language. The generation of the second SQL statement and the second natural language is the same as that of the first SQL statement and the first natural language, so it will not be described in detail further.

[0044] Using the second natural language, we obtained NL V2: "Query the data of 'first-time loan' and 'credit loan', for banks in the region 'Suzhou City'... with a total amount greater than 5 billion...".

[0045] It should be noted that if the second natural language is still inconsistent with the initial natural language after the second correction cycle, a correction cycle step can be performed here until the comparison results are consistent.

[0046] S161: Assuming the comparison results are consistent, execute the corresponding SQL statement to obtain the query results. Retrieve accurate query results from the database and then output the results.

[0047] Please refer to Figure 2 This application also provides a natural language database query system 100 based on semantic loop closure verification, comprising: Acquisition Module 10: Configured for receiving the user's initial natural language.

[0048] LLM parsing module 20: Connected to the acquisition module, configured to perform semantic parsing through the semantic parsing model to extract query elements.

[0049] SQL generation module 30: Connects to the LLM parsing module and is configured to generate SQL statements based on query elements.

[0050] SQL Reverse Parsing Module 40: Connected to the SQL Generation Module, configured to reverse parse the first SQL statement into the first natural language.

[0051] Consistency verification module 50: connected to the LLM parsing module and the SQL reverse parsing module respectively, and configured to perform consistency comparison between the first natural language and the initial natural language.

[0052] Iteration control module 60: Connected to the consistency verification module, configured to generate comparison difference information when semantics are inconsistent, trigger correction loop steps, and coordinate the LLM parsing module, SQL generation module, SQL reverse parsing module, and consistency verification module to perform iterative correction.

[0053] The correction loop step includes: Based on the comparison difference information, the initial natural language is re-parsed to obtain updated query elements; Based on the updated query elements, generate a second SQL statement; The second SQL statement is parsed in reverse into the second natural language. Perform a semantic consistency comparison between the second natural language and the initial natural language; If the comparison results are consistent, or if the comparison results become consistent after one or more correction cycles, then the corresponding SQL statement is executed to obtain the query results.

[0054] Query module 61: Configured to execute the final SQL statement to obtain the query results when semantics are consistent. That is, assuming the comparison results are consistent, execute the corresponding SQL statement to obtain the query results. Retrieve accurate query results from the database and then output the results.

[0055] This application provides a computer device, characterized in that it includes a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of a natural language database query method based on semantic loop closure verification.

[0056] This application also provides a computer-readable storage medium storing computer-readable instructions, which, when executed by a computer's processor, cause the computer to perform a method in a natural language database query method based on semantic loop closure verification.

[0057] It should be noted that 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 process, method, article, or apparatus. Unless otherwise specified, 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.

[0058] The above description is merely an embodiment of this application and is not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.

Claims

1. A natural language database query method based on semantic loop closure verification, characterized in that, Includes the following steps: Receive initial natural language; Semantic parsing is performed using a semantic parsing model to extract query elements; Based on the query elements, generate the first SQL statement; The first SQL statement is parsed in reverse into the first natural language. Perform a consistency comparison between the first natural language and the initial natural language; When semantic inconsistencies occur, comparison difference information is generated, triggering a correction loop step, which includes: Based on the comparison difference information, the initial natural language is re-parsed to obtain updated query elements; Based on the updated query elements, generate a second SQL statement; The second SQL statement is parsed in reverse into the second natural language. Perform a semantic consistency comparison between the second natural language and the initial natural language; If the comparison results are consistent, or if the comparison results become consistent after one or more correction cycles, then the corresponding SQL statement is executed to obtain the query results.

2. The method according to claim 1, characterized in that, The process of reversing the first SQL statement into the first natural language specifically includes: Parse the syntax structure of the first SQL statement; Map the components in the grammatical structure to predefined natural language templates; Based on the mapping, the first natural language is generated.

3. The method according to claim 1, characterized in that, The consistency comparison specifically includes: Calculate the semantic similarity between the first natural language and the initial natural language; When the semantic similarity is lower than a preset threshold, it is determined to be inconsistent.

4. The method according to claim 1, characterized in that, The step of re-parseing the initial natural language based on the comparison difference information to obtain updated query elements specifically includes: Based on the comparison difference information, the parameters of the semantic parsing model are adjusted to generate updated query elements; Alternatively, based on the comparison difference information, the updated query elements can be directly corrected and output.

5. The method according to claim 1, characterized in that, The semantic parsing model is implemented using the Large Language Model (LLM).

6. A natural language database query system based on semantic loop closure verification, characterized in that, include: The acquisition module is configured to receive the initial natural language. The LLM parsing module is configured to perform semantic parsing using a semantic parsing model to extract query elements. The SQL generation module is configured to generate a first SQL statement based on the query elements; The SQL reverse parsing module is configured to reverse parse the first SQL statement into the first natural language. A consistency verification module is configured to perform a consistency comparison between the first natural language and the initial natural language. The iterative control module is configured to generate alignment difference information and trigger a correction loop step when semantic inconsistencies occur. The correction loop step includes: Based on the comparison difference information, the initial natural language is re-parsed to obtain updated query elements; Based on the updated query elements, generate a second SQL statement; The second SQL statement is parsed in reverse into the second natural language. Perform a semantic consistency comparison between the second natural language and the initial natural language; If the comparison results are consistent, or if the comparison results become consistent after one or more correction cycles, then the corresponding SQL statement is executed to obtain the query results.

7. The system according to claim 6, characterized in that, The system also includes a query module, configured to execute the corresponding SQL statement to obtain query results when the semantics are consistent.

8. The system according to claim 6, characterized in that, The semantic parsing is implemented using a large language model (LLM).

Citation Information

Cited By

  • A financial intelligence question method fusing semantic search and permission control

    CN122388114A