An intelligent data query system and method based on natural language processing

By constructing a pattern knowledge graph and introducing error detection and ambiguity recognition mechanisms, combined with gating repair and continuous optimization modules, the accuracy and stability issues in natural language database query technology are solved, achieving high-quality SQL statement generation and cross-scenario adaptability.

CN121255832BActive Publication Date: 2026-07-21JIANGSU RED NET TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
JIANGSU RED NET TECH CO LTD
Filing Date
2025-09-17
Publication Date
2026-07-21

AI Technical Summary

Technical Problem

Existing natural language database query technologies have shortcomings in terms of accuracy, disambiguation capabilities, error correction, continuous optimization, and interactivity, resulting in unstable generated SQL statements that are difficult to promote in enterprise applications and complex data environments.

Method used

By constructing a pattern knowledge graph, introducing error detection and ambiguity recognition mechanisms, and combining gating repair and continuous optimization modules, the accuracy and stability of natural language to SQL conversion are improved.

Benefits of technology

It enables the generation of high-quality query statements without requiring professional SQL knowledge, reduces the error rate, improves the accuracy and stability of queries, and enhances the system's cross-scenario generalization ability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121255832B_ABST
    Figure CN121255832B_ABST
Patent Text Reader

Abstract

The application discloses an intelligent data query system and method based on natural language processing, and relates to the technical fields of natural language processing and database query.The application receives natural language input and database schema information, constructs schema knowledge representation in combination with historical query logs, generates a query skeleton on the basis, calls a fine-tuned large language model to generate a candidate SQL statement, detects errors and identifies ambiguities of the candidate statement, triggers interactive clarification when necessary, and updates the query result according to user feedback.Meanwhile, the application performs local repair on the error clause through a gating mechanism, re-generates when multiple repair failures occur, and thus guarantees the correctness of the query statement.Finally, the final SQL is executed in the database, and the result is fed back to the user.In addition, the system records the generation and repair track during operation, and continuously optimizes the model based on reward shaping, contrastive learning and self-game training, so as to improve the generalization ability in different business scenarios.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of natural language processing and database query technology, and in particular to an intelligent data query system and method based on natural language processing. Background Technology

[0002] With the widespread application of big data and database technologies, users' demand for complex data queries is constantly increasing. Traditional database queries mainly rely on Structured Query Language (SQL), which presents a high barrier to entry for users lacking professional knowledge. Therefore, academia and industry have gradually proposed database query methods based on natural language processing, which automatically generate SQL statements from natural language input to reduce user costs.

[0003] However, due to the diversity and ambiguity of user expressions, existing systems often struggle to generate semantically correct SQL statements, easily leading to issues such as incorrect field selection, missing conditions, or improper join paths. The accuracy of the natural language-to-SQL mapping is limited. When fields with the same name or multiple feasible join paths exist in the database, existing methods typically lack effective disambiguation mechanisms, resulting in unstable generated results or requiring manual intervention. Most solutions lack systematic error detection and correction methods after SQL generation; once logical or pattern errors exist in the generated statement, they often cannot be automatically corrected, reducing the practicality and reliability of the query. Existing systems largely rely on single-trained language models, whose performance tends to degrade when applied across databases and scenarios, lacking the ability to dynamically optimize and continuously learn the model. While some research attempts to incorporate user feedback, the lack of effective interaction design and information gain measurement mechanisms results in inefficient interaction processes, making it difficult to promote in real-world business environments.

[0004] In summary, existing natural language database query technologies still have significant room for improvement in terms of accuracy, disambiguation capabilities, error correction, continuous optimization, and interactivity. These shortcomings limit the promotion and application of natural language queries in enterprise-level applications and complex data environments, necessitating the development of a new solution to significantly enhance system stability, availability, and intelligence. Summary of the Invention

[0005] To address the above problems, this invention proposes an intelligent data query system and method based on natural language processing. By improving the model training and generation mechanism, the accuracy and stability of natural language to SQL conversion are significantly improved.

[0006] The present invention achieves the above objectives through the following technical solutions:

[0007] An intelligent data query system based on natural language processing, the system comprising:

[0008] The interface module is used to receive natural language queries input by the user and extract database schema information from the target database.

[0009] The prior modeling module is used to construct a pattern knowledge graph based on historical query logs and the database pattern information to uniformly represent tables, fields, and query relationships.

[0010] The SQL generation module is used to generate a query skeleton based on the natural language query and the pattern knowledge graph, call a fine-tuned large language model to generate candidate SQL statements under the constraints of the query skeleton, and perform error detection and ambiguity recognition on the candidate SQL statements.

[0011] The interactive clarification module is used to calculate the ambiguity metric and expected information gain based on the pattern knowledge graph according to the ambiguity identification result, determine whether to trigger the clarification interaction, obtain user feedback after triggering to form a clarification result, update the query skeleton or candidate SQL statement to generate a clarified SQL draft.

[0012] The gated repair module is used to control the generation and repair intensity of candidate SQL statements or clauses in the initial SQL draft through a dual-state gating mechanism based on error detection results. It performs local repairs on clauses under the constraint of a repair attempt count threshold. When the repair attempt count reaches the threshold, it reverts to the SQL generation module to reconstruct the query skeleton and candidate SQL statements. After the repair is completed, the final SQL is output.

[0013] The execution output module is used to receive the final SQL and execute it in the target database, and generate the execution result to output to the user;

[0014] The continuous optimization module is used to receive the execution trajectories of the SQL generation module, interactive clarification module and gated repair module during the generation, clarification and repair processes, and continuously optimize the large language model in the SQL generation module based on clause-level reward shaping, counterfactual contrast learning and self-game training mechanisms.

[0015] Preferably, the prior modeling module includes:

[0016] The schema parsing unit is used to parse database schema information to extract table names, field names, data types, and foreign key constraints;

[0017] The log extraction unit is used to extract field access records, association conditions, and aggregation operation rules from historical query logs;

[0018] The graph modeling unit is used to create table nodes and field nodes, and set data type attributes and access frequency attributes for table nodes and field nodes respectively; it creates edges based on foreign key constraints and association conditions, and sets weights for the number of times the edges are connected;

[0019] Enhanced modeling units are used to add time attributes to table nodes, field nodes, and edges to represent structural changes and query behavior evolution; historical queries are clustered based on semantic similarity of historical query logs to generate query intent pattern nodes, and associations are established with table nodes or field nodes; cardinality and aggregation usage counts are recorded on field nodes, and table cluster nodes are added to the graph structure to represent business themes;

[0020] The knowledge graph output unit is used to output pattern knowledge graphs for use by the SQL generation module and the interactive clarification module.

[0021] Preferably, the SQL generation module includes:

[0022] The pattern retrieval unit is used to locate relevant table nodes and field nodes in the pattern knowledge graph based on natural language queries, and extract node attributes and edge weights as constraint features.

[0023] The skeleton building unit is used to generate a query skeleton based on the semantic vector representation of the natural language query and the embedded representation of the pattern knowledge graph, and to calculate dynamic weights based on the similarity between the query semantics and the pattern nodes to adjust the priority of table nodes and field nodes in the query skeleton.

[0024] The candidate generation unit is used to call a fine-tuned large language model under the constraints of the query skeleton, fill in specific fields, operators and aggregation methods to generate candidate SQL statements, and control the diversity of candidate SQL based on the sampling strategy.

[0025] The error detection unit is used to perform syntax checks, pattern consistency checks, and logic checks on candidate SQL statements, and output error detection results including error categories.

[0026] The ambiguity identification unit is used to identify cases in candidate SQL statements where fields have multiple sources, multiple solutions to connection paths, or non-unique logical interpretations based on pattern knowledge graphs, and to generate an ambiguity score for each type of ambiguity as the ambiguity identification result.

[0027] Preferably, the ambiguity identification unit generates an ambiguity score for each type of ambiguity, calculated using the following formula:

[0028] Score a =λ1·count(a)+λ2·freq(a)-λ3·conf(a);

[0029] In the formula, Score a represents the ambiguity score of ambiguity type a; count(a) is the current number of ambiguity candidates; freq(a) is the frequency of use of ambiguity a in the historical query log; conf(a) is the confidence of the large language model for ambiguity a; λ1, λ2, and λ3 are weighting coefficients.

[0030] Preferably, the interactive clarification module includes:

[0031] The ambiguity measurement unit is used to calculate the ambiguity measure based on the ambiguity score output by the ambiguity identification unit, the overlap of candidate options in the pattern knowledge graph, and the confidence score output by the large language model.

[0032] The information gain calculation unit is used to calculate the degree to which the uncertainty of the ambiguity set is reduced under different user answers to the candidate clarification question, so as to obtain the expected information gain;

[0033] The trigger determination unit is used to determine whether to trigger a clarification interaction based on the joint result of ambiguity measurement and expected information gain.

[0034] The clarification problem optimization unit is used to select the problem with the highest expected information gain and the lowest user interaction cost from the candidate clarification problems.

[0035] The clarification interaction unit is used to output the question selected by the clarification question optimization unit to the user when triggered and to receive feedback results;

[0036] The query update unit is used to update the query skeleton or candidate SQL statement based on the feedback result to generate a clarified initial SQL draft.

[0037] Preferably, the gated repair module includes:

[0038] The workload gate unit is used to calculate the gate value for fields and connection paths in candidate SQL statements or SQL drafts based on historical query logs and statistical features of pattern knowledge graphs, and to enable the field or path as a repair reference when the gate value exceeds a preset threshold.

[0039] Clause gate units are used to set repair weights for each clause based on error detection results, and to determine the repair priority of the clauses by combining error category weights;

[0040] The repair execution unit is used to generate multiple repair candidate schemes for the corresponding clause when the repair priority exceeds the threshold, and select the optimal scheme from the repair candidate schemes to perform local repair. The repair methods include field replacement, condition correction or connection path adjustment.

[0041] The repair constraint unit is used to stop local repair when the number of repair attempts reaches the repair attempt threshold, and to pass the failed repair records as a failure log as a negative sample to the SQL generation module to avoid repeating errors during rollback;

[0042] The result output unit is used to output the final SQL after the repair is completed.

[0043] Preferably, the continuous optimization module includes:

[0044] The trajectory recording unit is used to receive the execution trajectories of the SQL generation module, interactive clarification module, and gated repair module during the generation, clarification, and repair processes, and to form a trajectory dataset.

[0045] The reward shaping unit is used to assign clause-level reward scores to each SQL clause in the trajectory dataset, and adjust the reward signal based on the clause's syntactic correctness, pattern consistency, and logical accuracy.

[0046] The contrastive learning unit is used to construct counterfactual samples based on the trajectory dataset, compare and train erroneous clauses with correct clauses, and perform cross-stage contrastive learning based on the comparison results between the candidate SQL statement before clarification and the initial SQL draft after clarification.

[0047] The error pattern mining unit is used to identify recurring error patterns in the trajectory dataset and inject the error patterns as negative sample constraints into the training process.

[0048] Cross-query migration unit is used to build query clusters based on query semantic similarity and migrate optimization experience within the query clusters during continuous optimization.

[0049] The self-play training unit is used to train the generative model and the discriminative model in a multi-agent environment by playing against each other, and the role switching controller enables the generative model and the discriminative model to take turns to assume the adversarial or guiding roles in different tasks.

[0050] The optimization and update unit is used to update the parameters of the large language model in the SQL generation module based on the results of the reward shaping unit, contrastive learning unit, error pattern mining unit, cross-query migration unit, and self-game training unit, so as to achieve continuous optimization.

[0051] Preferably, the system further includes a fine-tuning training module, the fine-tuning training module being used for:

[0052] Based on problem data from real business scenarios, we analyze problem prototypes and abstract business rules and logic to form standardized query templates.

[0053] The training data is expanded based on the standardized query template to generate training samples covering multiple business scenarios;

[0054] Introduce diverse natural language expressions, including synonyms and colloquial questions, into the training samples to improve the model's expressive robustness.

[0055] The training samples are integrated into a fine-tuning dataset, and supervised fine-tuning is performed on the general large language model to obtain a large language model suitable for the NL2SQL task.

[0056] A method for intelligent data querying based on natural language processing, the method comprising:

[0057] It receives natural language queries input by users and extracts database schema information from the target database;

[0058] A schema knowledge graph is constructed based on the database schema information and historical query logs;

[0059] A query skeleton is generated based on the natural language query and the pattern knowledge graph. Under the constraints of the query skeleton, a fine-tuned large language model is called to generate candidate SQL statements, and error detection and ambiguity recognition are performed on the candidate SQL statements.

[0060] Based on the ambiguity identification results, the ambiguity metric and expected information gain are calculated based on the pattern knowledge graph to determine whether to trigger a clarification interaction; when triggered, user feedback is obtained to form a clarification result, and the query skeleton or candidate SQL statement is updated based on the clarification result to generate a first draft of the clarified SQL.

[0061] Based on the error detection results, the generation and repair intensity of the candidate SQL statement or the clause in the initial SQL draft are controlled through a dual-state gating mechanism. The clause is partially repaired under the constraint of the repair attempt count threshold. When the repair attempt count reaches the repair attempt count threshold, the process reverts to the SQL generation step to reconstruct the query skeleton and candidate SQL statement. After the repair is completed, the final SQL is output.

[0062] Execute the final SQL in the target database and output the execution result to the user;

[0063] The execution trajectory generated during SQL generation, interactive clarification, and gating repair is collected, and the large language model is continuously optimized based on clause-level reward shaping, counterfactual contrast learning, and self-game training mechanisms.

[0064] The beneficial effects of this invention are as follows: By combining natural language queries with database schema information and historical query logs, intelligent mapping between user input and database queries is achieved, enabling the generation of high-quality query statements without requiring specialized SQL knowledge. By introducing error detection, ambiguity identification, and interactive clarification mechanisms, the error rate caused by multi-source fields and multiple path solutions is effectively reduced, improving query accuracy and stability. The gating repair mechanism can automatically repair syntax or logical errors, avoiding generation failures or error propagation and improving the reliability of query execution. Furthermore, by collecting trajectory information during execution and combining it with reward shaping, contrastive learning, and self-game training, the model is continuously optimized, enabling the system to continuously improve its capabilities and enhancing its cross-scenario generalization and long-term application adaptability. Attached Figure Description

[0065] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the following description of the embodiments will be briefly introduced. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0066] in:

[0067] Figure 1 This is a schematic diagram of the modular structure of the system of the present invention;

[0068] Figure 2 This is a flowchart of the method in an embodiment of the present invention. Detailed Implementation

[0069] 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, not all, of the embodiments of the present invention. All other embodiments obtained by those skilled in the art based on the described embodiments of the present invention are within the scope of protection of the present invention.

[0070] like Figure 1 As shown, this is an embodiment of the present invention, which provides an intelligent data query system based on natural language processing, including an interface module, a prior modeling module, an SQL generation module, an interactive clarification module, a gating repair module, an execution output module, and a continuous optimization module. The input-output relationships between the modules are as follows:

[0071] The interface module receives natural language queries input by the user, extracts database schema information (including table structure, field attributes, foreign key constraints, etc.) from the target database, provides the database schema information to the prior modeling module, and provides natural language queries to the SQL generation module.

[0072] The prior modeling module constructs a schema knowledge graph based on the database schema information provided by the interface module and combined with historical query logs. This graph is used to uniformly represent tables, fields, foreign key relationships, and high-frequency query patterns in the database. The schema knowledge graph is then passed as structured knowledge input to the SQL generation module and the interactive clarification module.

[0073] The SQL generation module generates a query skeleton based on natural language queries and pattern knowledge graphs. Under its constraints, it calls a finely tuned large language model to generate candidate SQL statements. It performs error detection (syntax errors, missing fields, logical inconsistencies, etc.) and ambiguity identification (fields with the same name, fuzzy constraints, multiple solutions, etc.) on the candidate SQL statements and outputs the candidate SQL statements and their error / ambiguity tags.

[0074] Based on the ambiguity identification results, the interactive clarification module calls the pattern knowledge graph to calculate the ambiguity metric (number of field conflicts, number of candidate connection paths, etc.) and expected information gain, determines whether to trigger the clarification interaction, and if triggered, presents the problem to the user (e.g., field selection, time interval limitation, etc.), obtains user feedback, forms the clarification result, updates the query skeleton or candidate SQL statement, and generates the first draft of the clarified SQL.

[0075] The gating and repair module receives a draft SQL statement (or candidate SQL statements if the clarification interaction is not triggered) and error detection results. It uses a dual-state gating mechanism to calculate the workload gate (W gate) and clause gate (C gate) to control the generation and repair strength of clauses. If the number of repair attempts does not exceed a preset threshold, partial repairs (such as field name replacement, condition correction, and JOIN path adjustment) are performed. If the number of repair attempts reaches the threshold, the module reverts to the SQL generation module, reconstructs the query skeleton, and generates candidate SQL statements. The final SQL statement is output after the repair is complete.

[0076] The execution output module receives the final SQL, executes the query in the target database, and generates the execution results, which are returned to the user in tabular or natural language form.

[0077] The continuous optimization module collects the execution trajectories of the SQL generation module, interactive clarification module, and gated repair module during the generation, clarification, and repair processes. It employs clause-level reward shaping, counterfactual contrastive learning, and self-game training mechanisms to continuously optimize the large language model in the SQL generation module, ensuring the system's generalization ability and stability under different databases and complex query conditions.

[0078] System workflow example:

[0079] Suppose a user enters the question: "Which company will have the highest net profit in 2025?"

[0080] The interface module receives queries and extracts database schema information (including company table, financial table, field information, etc.);

[0081] The prior modeling module constructs a pattern knowledge graph, which includes inter-table relationships and historically commonly used JOIN paths;

[0082] The SQL generation module generates the query skeleton: SELECT Company Name FROM… WHERE Year = 2025 ORDER BY Net Profit DESC LIMIT 1; The large language model fills in the candidate SQL statements and checks for field matching;

[0083] If the field "net profit" exists in multiple tables, the interactive clarification module calculates the ambiguity measure and expected information gain, triggering the user to confirm the table it belongs to;

[0084] User feedback indicated "from the financial table," prompting the system to update the framework and generate a draft SQL statement.

[0085] The gating repair module detected that the WHERE condition "net profit not null" was missing in the initial SQL draft. It repaired the clause using a C-gate and output the final SQL:

[0086] SELECT Company Name FROM Financial Table WHERE Year = '2025' AND Net Profit IS NOT NULL ORDER BY Net Profit DESC LIMIT 1;

[0087] The execution output module executes the final SQL in the database and returns the result "Company A", which is then displayed to the user.

[0088] The continuous optimization module records this execution trajectory for reinforcement learning to update the model.

[0089] The specific implementation of each module will be explained below.

[0090] (1) Interface module

[0091] It mainly includes a user input subunit and a database pattern extraction subunit. Its function is to standardize the user's natural language questions and database structure information into input data that can be used by subsequent modules.

[0092] 1) User input subunit: Receives natural language query requests from users and performs preprocessing.

[0093] Example of implementation steps:

[0094] Input methods: Queries can be received through a graphical user interface (web front-end, mobile application) or an application programming interface (API).

[0095] Text preprocessing: Cleaning and standardizing natural language issues, including removing redundant punctuation, uniform character encoding, word segmentation, and basic part-of-speech tagging.

[0096] Semantic representation generation: Invoking a lightweight language encoding model (such as a BERT embedder or SentenceTransformer) to transform the user question into a vector representation h. QThis serves as one of the inputs for subsequent modules.

[0097] Output: Standardized natural language query text and its corresponding semantic vector representation h Q .

[0098] 2) Database schema extraction sub-unit: Extract database schema information from the target database and encapsulate it into a unified data structure.

[0099] Example of implementation steps:

[0100] Database connection: Establish a connection with the target database through the JDBC / ODBC interface.

[0101] Pattern extraction: Execute the system query statement (such as SELECT table_name, column_name, data_type FROM information_schema.columns) to extract the table structure, field names, data types, and primary and foreign key relationships of the database.

[0102] Schema transformation: Converting raw schema information into standardized JSON or graph structures. For example:

[0103]

[0104] Vectorized representation: Table names and field names are embedded as vector representations for use by other modules.

[0105] Output: Database schema structure (JSON / graph structure) and corresponding schema vector representation.

[0106] Taking the previous system workflow example, the user input sub-unit receives the text "Which company had the highest net profit in 2025?", removes stop words to obtain standardized text, and then generates a semantic vector h through an embedding model. Q .

[0107] Database schema extraction sub-unit connects to the target database and automatically extracts schema information:

[0108] company_table(companyID, companyname, year)

[0109] finance_table(Company ID, Year, Net Profit)

[0110] Foreign key: company_table.companyID = finance_table.companyID.

[0111] The information is converted into JSON format and represented in a quantized manner.

[0112] The interface module outputs standardized natural language questions (including semantic vectors h). Q ) and database schema information (including schema vectors).

[0113] The output data is passed to the prior modeling module to construct the pattern knowledge graph and sent to the SQL generation module to generate the query skeleton.

[0114] (2) Prior modeling module

[0115] In one embodiment, the prior modeling module includes:

[0116] 1) Schema parsing unit: Parses the database schema information of the target database, extracting table names, field names, data types, and foreign key constraints. The implementation process is as follows:

[0117] Establish a connection to the target database and call the system metadata interface (such as information_schema or system table query);

[0118] The parsed schema information is used to organize table names, field names, and data types into structured entries;

[0119] Identify primary key and foreign key relationships and store them in the form of constraints for subsequent graph structure construction;

[0120] The output is a collection of schema entries containing tables, fields, data types, and foreign key relationships.

[0121] 2) Log extraction unit

[0122] Log extraction unit: Extracts field access records, join conditions, and aggregation operation rules from historical query logs. The implementation process is as follows:

[0123] Parse the historical query logs to identify the table names, field names, and conditional statements used.

[0124] Extract field access records that appear in historical query logs for use in generating subsequent access frequency attributes;

[0125] Extract the JOIN condition, WHERE filter condition, and GROUP BY aggregation field to form a set of query behavior patterns;

[0126] Count the number of times each join condition (referring to the join conditions between different tables in the historical query log, usually appearing in the JOIN or WHERE clause of SQL) appears in the historical query log, and use this as the number of times the join is used;

[0127] The output is a set of log features containing field access records, association conditions, and aggregation operation rules.

[0128] 3) Graph Modeling Unit: Based on the parsing and extraction results, a graph structure is built, including table nodes, field nodes, and edges. The implementation process is as follows:

[0129] Create table nodes and field nodes, and set data type attributes and access frequency attributes on the table nodes and field nodes respectively. The data type attribute comes from the schema parsing unit, and the access frequency attribute comes from the log extraction unit.

[0130] Edges are established based on foreign key constraints and association conditions, and the weight of the edge is determined by the number of times the connection is used provided by the log extraction unit.

[0131] The output is a preliminary graph structure containing nodes and edges.

[0132] 4) Enhanced Modeling Units: This involves expanding and enhancing the structure of the initial diagram, specifically including:

[0133] Add a time attribute to table nodes, field nodes, and edges to represent version changes in the database structure and the evolution of query behavior;

[0134] Based on the semantic similarity of historical query logs (e.g., encoding the question part of each historical query into a vector through a pre-trained language model and calculating the cosine similarity between any two queries), historical queries are clustered (using density-based clustering algorithms or vector space-based hierarchical clustering to divide queries with similarity higher than a preset threshold into the same cluster), query intent pattern nodes are generated and associated with table nodes or field nodes.

[0135] Record the field cardinality (the number of different values ​​of a field in the database; for example, the field "Year" in a financial table may have six different values: 2019, 2020, 2021, 2022, 2023, and 2024. The cardinality of this field is 6, which can be obtained by counting COUNT(DISTINCT field name) in the database column) and the number of times aggregation is used to enhance the description of query complexity and query habits.

[0136] Add table cluster nodes to the graph structure (perform community detection or clustering on table nodes to aggregate frequently connected tables into the same cluster) to represent business themes and aggregate related tables.

[0137] The output is an enhanced pattern knowledge graph.

[0138] 5) Knowledge Graph Output Unit: This unit encapsulates the pattern knowledge graph generated by the augmented modeling unit into a standardized data structure for use by the SQL generation and interactive clarification modules. The output format can be JSON, stored in a graph database, or embedded in a vector representation, ensuring direct referencing during subsequent SQL generation and disambiguation processes.

[0139] Taking a corporate financial database as an example:

[0140] The schema parsing unit parses the table company_table (fields: company ID, company name, year) and the table finance_table (fields: company ID, year, net profit), and identifies company ID as a foreign key constraint;

[0141] The log extraction unit identified that the "net profit" field frequently appeared in the aggregation calculation and the "year" field frequently appeared as a filtering condition from the historical logs;

[0142] The graph modeling unit creates table nodes company_table and finance_table, and field nodes such as "net profit" and "year", and sets the data type (integer, string) and access frequency (high, medium, low) for the field nodes; it also creates an edge "company_table.companyID→finance_table.companyID" and sets the weight (number of occurrences) for the edge;

[0143] The enhanced modeling unit adds time attributes to nodes and edges to identify differences in query behavior across different years; it generates a "Financial Indicator Analysis" query intent pattern node based on semantic clustering and associates it with the "Net Profit" field node; it records the cardinality and aggregation count in the "Net Profit" field node; and it adds a "Financial Analysis" table cluster node to include multiple financially related tables in this cluster.

[0144] The knowledge graph output unit ultimately outputs a pattern knowledge graph, which provides the SQL generation module with complete table structure, field usage habits, and query intent information to generate a query skeleton.

[0145] (3) SQL generation module

[0146] In one embodiment, the SQL generation module includes:

[0147] 1) Pattern retrieval unit: Locates table nodes and field nodes related to natural language queries in the pattern knowledge graph.

[0148] Input: The semantic vector representation h of the natural language query Q Embedding vector set of pattern knowledge graph in A vector representation of a table node or field node.

[0149] Calculation method: h is calculated using a similarity function. Q Similarity score with each pattern node:

[0150]

[0151] Where · represents the vector dot product, and |||| represents the vector norm.

[0152] Output: Sort by similarity, select the top k related table nodes and field nodes, and output the node set N. rel It also includes node attributes (data type, access frequency, cardinality) and edge weights (number of times a connection is used).

[0153] 2) Skeleton building unit: Generates query skeleton and calculates dynamic weights based on similarity.

[0154] Input: Natural language semantic vector h Q The set of related nodes N rel Pattern embedding set h S .

[0155] Skeleton generation: Based on the query intent, establish the main structure of the SQL query.

[0156] Skeleton={SELECT_,FROM_,WHERE_,GROUP BY_,ORDER BY_}

[0157] Each clause is represented as a placeholder and is not filled with specific fields or conditions.

[0158] Dynamic weight calculation: For each candidate node n i ∈N rel Calculate the dynamic weights:

[0159]

[0160] In the formula, Indicates semantic similarity; freq(n) i This indicates the access frequency (normalized value) of a field or table in the historical log; type(n) i The ) indicates the degree of matching between the field data type and the query requirements; α, β, and γ are adjustable weight parameters;

[0161] Output: A list of candidate nodes with the query skeleton and dynamic weight sorting.

[0162] 3) Candidate generation unit: Under skeleton constraints, the large language model is invoked to generate candidate SQL statements.

[0163] Input: query skeleton, a list of candidate nodes sorted by dynamic weights, and a large language model (fine-tuned).

[0164] Generation method: The query skeleton and candidate nodes are input as constraint prompts into the large language model, and the model generates several candidate SQL statements based on the context.

[0165] Diversity control: A sampling strategy (such as top-k or nucleus sampling) is adopted, and the output diversity is controlled by adjusting the temperature parameter.

[0166] Output: Set of candidate SQL statements j .

[0167] 4) Error detection unit: Performs error detection on candidate SQL statements and marks the error category.

[0168] Syntax check: Call the SQL parser to build a syntax tree for the statement. If the construction fails, it is marked as "syntax error".

[0169] Schema consistency check: Check whether the table names and field names referenced in the candidate SQL statement are included in the schema knowledge graph nodes. If they are inconsistent, mark them as "schema error".

[0170] Logical checks: Verify whether the aggregated field appears correctly in SELECT or GROUPBY, and verify whether the JOIN path is consistent with the edge in the pattern knowledge graph. If they do not match, mark them as "logical errors".

[0171] Output: A set of detection results containing candidate SQL statements and their corresponding error category labels.

[0172] 5) Ambiguity identification unit: detects whether there are ambiguities in the candidate SQL statement and quantifies the ambiguity score.

[0173] Target of detection:

[0174] Multiple source fields: Fields with the same name exist in multiple tables;

[0175] Multiple solutions: The condition can be satisfied by multiple JOIN paths;

[0176] Conditional ambiguity: Natural language conditions can be mapped to multiple logical expressions.

[0177] Ambiguity score calculation:

[0178] Score a =λ1·count(a)+λ2·freq(a)-λ3·conf(a);

[0179] In the formula, Score a represents the ambiguity score of ambiguity type a; count(a) is the current number of ambiguity candidates; freq(a) is the frequency of use of ambiguity a in the historical query log; conf(a) is the confidence of the large language model for ambiguity a; λ1, λ2, and λ3 are weighting coefficients;

[0180] Output: The recognition result with ambiguity scores, used by the interactive clarification module to determine whether to trigger user interaction.

[0181] The weighting coefficients λ1, λ2, and λ3 are determined in the following way:

[0182] A sample set containing a large number of ambiguous instances was extracted from historical query logs, and each sample was manually labeled with the true severity of ambiguity.

[0183] The grid search method is used to traverse the preset parameter space and calculate the score using the ambiguity score formula. a The optimization objective is to maximize the correlation coefficient (such as Spearman's rank correlation coefficient) between the severity labels and manually labeled severity levels, and to find the optimal combination of coefficients.

[0184] As an optional implementation, the typical empirical value range obtained after the above optimization process is: λ1 is approximately 0.5, λ2 is approximately 0.3, and λ3 is approximately 0.2. This setting aims to balance the impact of the current number of ambiguities, historical frequency, and model confidence on the final score.

[0185] Workflow Example

[0186] A user enters the query: "Which company had the highest net profit in 2025?"

[0187] The pattern retrieval unit locates the field nodes "Net Profit" and "Year" and the table nodes "company_table" and "finance_table";

[0188] The skeleton building unit generates a query skeleton and calculates dynamic weights, prioritizing recommendations based on "year" and "net profit".

[0189] The candidate generation unit calls the large language model to generate candidate SQL statements:

[0190] SELECT Company Name FROM finance_table WHERE Year = 2025 ORDER BY Net Profit DESCLIMIT 1;

[0191] The error detection unit checks that all fields exist, the JOIN path is correct, and there are no syntax errors.

[0192] The ambiguity recognition unit detects that "net profit" appears in multiple tables, calculates an ambiguity score of 0.82, and outputs the ambiguity recognition result.

[0193] (4) Interactive Clarification Module

[0194] In one embodiment, the interactive clarification module includes an ambiguity measurement unit, an information gain calculation unit, a trigger determination unit, a clarification question optimization unit, a clarification interaction unit, and a query update unit, and its implementation process is as follows:

[0195] The ambiguity measurement unit calculates the ambiguity measure based on the ambiguity score output by the ambiguity identification unit, the overlap of candidate options in the pattern knowledge graph, and the confidence score output by the large language model. The ambiguity score, output by the ambiguity identification unit, represents the severity of ambiguity caused by a certain field or path in the candidate SQL statement. The ambiguity measurement unit combines this ambiguity score with the overlap of candidate options in the pattern knowledge graph (i.e., the proportion of multiple candidate options sharing a table or field) and the model confidence score (the degree of trustworthiness of the large language model in the candidate interpretation) to obtain a comprehensive ambiguity measure value D. a .

[0196] Example formula:

[0197] D a =α·Score a +β·Overlap a -γ·Conf a ;

[0198] In the formula, Score a This represents the ambiguity score output by the ambiguity recognition unit; Overlap a Indicates the degree of overlap between candidates in the pattern knowledge graph; Conf a α represents the confidence level of the large language model for the candidate; α, β, and γ are adjustable weight parameters.

[0199] The information gain calculation unit measures the reduction in uncertainty of the ambiguity set after posing a clarification question. Its inputs are the ambiguity set A and the candidate clarification question q. First, the entropy of the current ambiguity set is calculated. Where p i Let represent the probability of each candidate explanation, and n represent the number of candidate explanations in the current ambiguity set. For a candidate clarification question q, consider all possible user responses u and calculate the conditional entropy. Where p i|u Let represent the conditional probability of candidate explanation i given user answer u. The expected information gain is:

[0200] EIG(q)=H(A)-∑ u∈U P(u|q)·H(A|u);

[0201] Where P(u|q) is the probability that the user gives the answer u. The output is the expected information gain value for each candidate question.

[0202] The trigger determination unit is used to determine whether to trigger a clarification interaction based on the joint result of the ambiguity metric and the expected information gain. A clarification interaction is triggered when the ambiguity metric exceeds a first threshold and the expected information gain exceeds a second threshold. A trigger flag is output to indicate whether to enter the clarification interaction process.

[0203] The clarification problem optimization unit is used to select the optimal problem from a set of candidate problems. The input is the set of candidate clarification problems q. j Each question Value, cost of user interaction

[0204] The optimization function is: Score(q) = EIG(q) - λ·C(q), where Score(q) is the optimization score of the candidate clarification question q; C(q) represents the cost of asking question q (e.g., the cost of requiring user-provided text input is higher than that of multiple-choice input); and λ is an adjustment parameter. The output is the optimal clarification question.

[0205] The clarification interaction unit is used to interact with the user when triggered. Implementation methods include:

[0206] Generate optional questions, such as: "Does the field 'Net Profit' come from finance_table?";

[0207] Present the issue to the user and receive user feedback.

[0208] The output is a clarification result, indicating the fields or paths that the user needs to confirm.

[0209] The query update unit is used to apply the clarification results to the query skeleton or candidate SQL statement.

[0210] Implementation methods include:

[0211] If the ambiguity involves fields, then the fields selected by the user will be populated into the query skeleton;

[0212] If the ambiguity involves the connection path, then select the user-specified path;

[0213] If logical conditions are involved, the logical expression confirmed by the user shall be used.

[0214] The output is a clarified initial draft of SQL, which is then handed over to the gating repair module for further processing.

[0215] (5) Gated Repair Module

[0216] In one embodiment, the gated repair module includes a workload gate unit, a clause gate unit, a repair execution unit, a repair constraint unit, and a result output unit, which are used to repair clause-level errors and ensure the stability of the repair process.

[0217] In this embodiment, the repair weight g c Calculated by clause gate units, it represents the strength of repair required when an error occurs in a clause, typically ranging from [0,1]. The error category weight is δ. err Manually preset weights are used to differentiate the severity of different error categories. For example, a schema error (missing field) has a weight of 1.0, a logical error has a weight of 0.8, and a syntax error has a weight of 0.5. Repair priority p c It is calculated by combining the repair weight and the error category weight, and is used to sort the repair order of different clauses. The repair attempt threshold B is the maximum number of partial repair attempts allowed by the system; exceeding this number triggers a rollback. The failure log stores the clauses, error types, and candidate repair solutions that failed, and serves as negative samples for the SQL generation module.

[0218] The workload gate unit (W gate) is used to calculate the gating value g of fields and join paths in candidate SQL statements or SQL drafts based on historical query logs and statistical features of the pattern knowledge graph. wl (x). The input contains a natural language semantic vector h. Q Pattern knowledge graph vector h S Historical query vector h W The calculation formula is:

[0219] g wl (x)=σ(W wl [h Q h S h W ]+b wl );

[0220] In the formula, χ represents a field or connection path; σ is the Sigmoid function, used to normalize the result to [0,1]; W wl b wl These are the parameters obtained during training.

[0221] When g wl (x) When the value exceeds a preset threshold (e.g., 0.5), enable this field or path as a repair reference.

[0222] The clause gate unit (C-gate) is used to assign repair weights to each clause based on the error detection results, and to determine the repair priority by combining the error category weights. The input is the result of the error detection unit (the set of error clauses C). err and its error categories).

[0223] Repair weight g c It also consists of a fully connected layer connected to a Sigmoid function:

[0224] g c =σ(W c[h Q h S h W}+b c );W c b c These are the parameters obtained during training;

[0225] Repair priority P(c): P(c) = g c ·δ err ;

[0226] The output is a list of clauses sorted by repair priority.

[0227] The repair execution unit performs local repairs on each clause one by one according to the repair priority. The implementation process includes:

[0228] Repair candidate pool generation: For the erroneous clause c, generate multiple repair candidate solutions {fix1, fix2, ..., fix...} k}

[0229] Repair type:

[0230] Field replacement: If a field does not exist, a similar field is selected from the pattern knowledge graph to replace it.

[0231] Conditional correction: If the WHERE clause is missing a constraint, add a condition such as IS NOT NULL or a year condition.

[0232] Path adjustment: If the JOIN path is invalid, select the path with the highest probability in the pattern knowledge graph.

[0233] Optimal solution selection:

[0234] Assign a score to each fix candidate i ):

[0235] score(fix i ) = sim(fix i ,h Q )+η·freq(fix i );

[0236] In the formula, sim(fix) i ,h Q ) represents the similarity between the fix solution and the query semantics; freq(fix) i ) represents the frequency of occurrence of the scheme in the historical query log; η is the weighting factor.

[0237] Select socre (fix) i The largest possible repair candidate is taken as the final repair result.

[0238] Repair constraint units are used to limit the repair process and prevent infinite loops.

[0239] Counting mechanism: Each time a repair is completed, the repair attempt counter is incremented by 1.

[0240] Threshold determination: If the number of repair attempts reaches the threshold, the local repair will be stopped.

[0241] Failure log generation: Write the clauses that were not successfully repaired, the error category, and the failed repair candidates into the failure log.

[0242] Rollback mechanism: Failure logs are passed as negative samples to the SQL generation module to rebuild the query skeleton and candidate SQL, and to avoid repeating errors.

[0243] The result output unit outputs the final SQL when the repair is successful, and enters the execution output module; when the repair fails, a rollback is triggered, and the SQL generation module regenerates the candidate SQL.

[0244] (6) Execute output module

[0245] The execution output module receives the final SQL output by the gating repair module and sends it to the target database through a database connection interface (such as JDBC or ODBC); the database query engine executes the final SQL, generates a result set, and converts the result set into a standardized data structure, such as a two-dimensional table (JSON or DataFrame format).

[0246] If the user interface is a web front-end, the results will be rendered as a table or chart;

[0247] If the user makes a call via API, the result will be returned in JSON format;

[0248] If the query result is empty, the message "No results found" will be output.

[0249] (7) Continuous optimization module

[0250] In one specific embodiment of the present invention, the continuous optimization module includes a trajectory recording unit, a reward shaping unit, a comparative learning unit, an error pattern mining unit, a cross-query migration unit, a self-game training unit, and an optimization update unit, which are used to continuously optimize the large language model in the SQL generation module during system operation.

[0251] The trajectory recording unit is used to record the execution trajectory of each module in the system. The input consists of a natural language query q and candidate SQL statements SQL. cand Clarified initial SQL draft, final SQLSQLL final And the execution result y. The output is the trajectory entry {q, SQL cand SQL初稿 SQL final The system uses a log of y, where y = 1 indicates a correct result and y = 0 indicates an incorrect result. The system automatically logs the results during runtime and stores them in a database or file system.

[0252] The reward shaping unit is used to assign a reward signal to each SQL clause. An example formula is shown below:

[0253] R(c)=β1·Syntax(c)+β2·Schema(c)+β3·Logic(c)

[0254] In the formula, R(c) is the reward vector, which serves as the signal for reinforcement learning; if the clause syntax is correct, then Syntax(c) = 1, otherwise it is 0; if the fields and tables involved exist in the pattern knowledge graph, then Schema(c) = 1, otherwise it is 0; if the JOIN path and constraints conform to the pattern knowledge graph, then Logic(c) = 1, otherwise it is 0; β1, β2, and β3 are preset constants.

[0255] The contrastive learning unit trains a model to distinguish between correct and incorrect clauses by constructing counterfactual samples. The input is the incorrect clause c from the trajectory dataset. err With the correct clause c true Construct positive sample pairs {c true ,h Q} and negative sample pairs {c err ,h Q}, calculate the loss function L con :

[0256]

[0257] Where sim() is the vector similarity function;

[0258] The difference between the candidate SQL statement before clarification and the initial SQL draft after clarification is used as a new positive and negative sample pair to enhance the improvement effect brought about by the model learning interactive clarification.

[0259] The error pattern mining unit is used to identify recurring error types during system operation. The input is the error SQL from the trajectory dataset.

[0260] Example of data mining methods: Statistically analyze error frequency; summarize error SQL patterns, such as "missing filter conditions", "incorrect field replacement", "inconsistent JOIN tables"; and form an error pattern set.

[0261] The output is an error pattern constraint, which is injected as a negative sample into the subsequent training process to prevent the model from repeatedly generating the same type of error.

[0262] Cross-query migration units are used to migrate optimization experience within a query cluster. The input semantic vector h...Q The algorithm uses K-means or hierarchical clustering to aggregate queries into multiple clusters. When a query in a cluster is optimized, its generation strategy parameters are shared with other queries in that cluster, reducing cold start errors. The output consists of cluster-level shared parameters used to guide the generative model.

[0263] The self-play training unit employs an alternating training framework similar to Generative Adversarial Networks (GANs), but applied to sequence generation tasks. It enhances the capabilities of both sides by switching roles, and improves robustness through alternating adversarial competition between the generative and discriminative models. The generative model is a fine-tuned large language model within the SQL generation module, used to generate candidate SQL statements. Its goal is to generate correct, high-quality SQL statements that are difficult for the discriminative model to recognize. The discriminative model is an independent binary classification neural network model used to determine whether the SQL statement is correct. Its structure may contain several fully connected layers and a final sigmoid output layer, which outputs a scalar representing the probability that "the SQL statement is correct for a given query."

[0264] When the accuracy of the discrimination model exceeds a set threshold, it is allowed to generate negative SQL examples as "teachers," while the generation model temporarily assumes the role of discrimination, thereby enriching the training samples.

[0265] Training process:

[0266] Phase 1: Training the Discriminative Model (D). Fix the parameters of the generative model (G). Use G to generate a batch of SQL statements, including both historically generated correct statements and intentionally constructed or historically occurring erroneous statements, forming a labeled training set. Use this dataset to train the discriminative model D, enabling it to accurately distinguish between correct and erroneous SQL statements.

[0267] Phase Two: Training the Generative Model (G). The parameters of the discriminative model (D) are fixed. The generative model G generates new candidate SQL statements. For each statement generated by G, the discriminative model D scores it (i.e., the probability of it being correctly judged), and this score is used as a reward signal. This reward signal is backpropagated using a policy gradient method (such as REINFORCE or PPO) to update the parameters of the generative model G, incentivizing it to generate higher-quality SQL statements that are more likely to "fool" the discriminative model D.

[0268] Alternating cycles: Phase one and phase two are repeated, forming a game-like competition. As training progresses, the discriminative ability of the discriminative model D becomes stronger, while the generative ability of the generative model G also becomes stronger.

[0269] Role Switching Controller: "Role switching" refers to the alternation of the training phases described above. The triggering condition for switching can be a fixed number of iterations (e.g., training the generative model for 5 epochs for every 1 epoch of training the discriminative model), or it can be performance-based: when the accuracy of the discriminative model D on the validation set reaches a preset threshold (e.g., 95%), it automatically switches to the next stage of training the generative model G.

[0270] The optimization update unit integrates the results of reward shaping, contrastive learning, error pattern mining, cross-query migration, and self-game training to update the large language model in the SQL generation module.

[0271] Implementation method: Gradient update method based on reinforcement learning, such as REINFORCE or PPO.

[0272] Output: Updated model parameters that enable the generation of higher quality SQL in the future.

[0273] Preferably, in one embodiment of the present invention, the system further includes a fine-tuning training module for constructing diverse training data based on real business scenarios and performing supervised fine-tuning of a general large language model, thereby obtaining a dedicated large language model suitable for NL2SQL tasks.

[0274] 1) Input data preparation

[0275] Real business data: This includes natural language queries submitted by users in real-world scenarios and the corresponding database schema information (table names, field names, field types, and relationships).

[0276] Historical query logs: Store past user query intents and corresponding SQL statements, which can be used as a supplementary reference.

[0277] 2) Problem Prototype Analysis

[0278] Extract natural language questions from real business data and analyze their semantic structure and corresponding SQL logical rules;

[0279] Form a problem prototype, that is, an abstract question-and-answer pair template.

[0280] Example:

[0281] Which company had the highest [metric] in [year]?

[0282] "answer":"SELECT company_name FROM company_table WHERE year = '[year]' AND [indicator] IS NOT NULL ORDER BY [indicator] DESC LIMIT 1;"}

[0283] 3) Expand training data: Automatically replace parameters (such as year, index) based on the question prototype to generate diverse question-answer pairs.

[0284] Example:

[0285] Which company had the highest net profit in 2023?

[0286] "answer":"SELECT company_name FROM company_table WHERE year = '2023' AND net profit ISNOTNULL ORDER BY net profit DESC LIMIT 1;"

[0287] 4) Enhance data diversity; utilize natural language rewriting methods to generate multiple different expressions for the same SQL.

[0288] Example:

[0289] Which company had the highest net profit in 2023?

[0290] "answer":"SELECT company_name FROM company_table WHERE year = '2023' AND net profit ISNOTNULL ORDER BY net profit DESC LIMIT 1;"

[0291] 5) Fine-tuning dataset generation: Integrate the problem prototype extended samples with diverse rewritten samples to form a fine-tuning dataset. Where q i For natural language problems, SQL i This corresponds to the SQL.

[0292] The dataset should cover:

[0293] Different time spans (years, quarters, etc.);

[0294] Different business metrics (net profit, profit margin, sales revenue, etc.);

[0295] Different SQL structures (aggregation, nested queries, multi-table JOIN).

[0296] 6) Supervision and fine-tuning

[0297] Training objective: Minimize the difference between the SQL generated by the model and standard SQL. The loss function used is the cross-entropy loss function.

[0298] Implementation: Load a general-purpose large language model (such as LLaMA, GPT series, BLOOM and other open-source models), use the constructed fine-tuning dataset to conduct supervised training on it, and after training, obtain a fine-tuning model specifically for NL2SQL.

[0299] 7) Output

[0300] The output is a large language model specifically for NL2SQL, which can generate well-structured and logically sound SQL when inputting natural language questions and database schemas.

[0301] Compared to existing methods that rely solely on Prompt Engineering, this model offers significant improvements in stability and accuracy.

[0302] like Figure 2 As shown, another embodiment of the present invention provides an intelligent data query method based on natural language processing, including:

[0303] It receives natural language queries input by users and extracts database schema information from the target database;

[0304] A schema knowledge graph is constructed based on database schema information and historical query logs;

[0305] A query skeleton is generated based on the natural language query and the pattern knowledge graph. Under the constraints of the query skeleton, a fine-tuned large language model is called to generate candidate SQL statements, and error detection and ambiguity recognition are performed on the candidate SQL statements.

[0306] Based on the ambiguity identification results, the ambiguity metric and expected information gain are calculated using the pattern knowledge graph to determine whether to trigger a clarification interaction. When triggered, user feedback is obtained to form a clarification result, and the query skeleton or candidate SQL statement is updated based on the clarification result to generate a first draft of the clarified SQL.

[0307] Based on the error detection results, a dual-state gating mechanism is used to control the generation and repair intensity of clauses in candidate SQL statements or initial SQL drafts. Clauses are partially repaired under the constraint of a repair attempt count threshold. When the repair attempt count reaches the threshold, the process reverts to the SQL generation step to reconstruct the query skeleton and candidate SQL statements. After the repair is completed, the final SQL is output.

[0308] Execute the final SQL in the target database and output the execution result to the user;

[0309] The system collects the execution trajectory generated during SQL generation, interactive clarification, and gating repair, and continuously optimizes the execution of the large language model based on clause-level reward shaping, counterfactual contrast learning, and self-game training mechanisms.

[0310] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any person skilled in the art can easily conceive of various variations or substitutions within the technical scope disclosed in this application, and these should all be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. An intelligent data query system based on natural language processing, characterized in that, The system includes: The interface module is used to receive natural language queries input by the user and extract database schema information from the target database. The prior modeling module is used to construct a pattern knowledge graph based on historical query logs and the database pattern information to uniformly represent tables, fields, and query relationships. The SQL generation module is used to generate a query skeleton based on the natural language query and the pattern knowledge graph, call a fine-tuned large language model to generate candidate SQL statements under the constraints of the query skeleton, and perform error detection and ambiguity recognition on the candidate SQL statements. The interactive clarification module is used to calculate the ambiguity metric and expected information gain based on the pattern knowledge graph according to the ambiguity identification result, determine whether to trigger the clarification interaction, obtain user feedback after triggering to form a clarification result, update the query skeleton or candidate SQL statement to generate a clarified SQL draft. The gated repair module, based on error detection results, determines repair references and priorities through a dual-state gating mechanism including workload gating units and clause gating units. It then performs partial repairs on clauses in candidate SQL statements or initial SQL drafts under a repair attempt count threshold. When the repair attempt count reaches the threshold, it reverts to the SQL generation module to reconstruct the query skeleton and candidate SQL statements. After repair, it outputs the final SQL. The workload gating unit calculates gating values ​​for fields and join paths in candidate SQL statements or initial SQL drafts based on historical query logs and pattern knowledge graph statistical features, and uses the field or path as a repair reference when the gating value exceeds a preset threshold. The clause gating unit sets repair weights for each clause based on error detection results and determines the repair priority of the clauses by combining error category weights. The execution output module receives the final SQL, executes it in the target database, and generates execution results to output to the user. The continuous optimization module is used to receive the execution trajectories of the SQL generation module, interactive clarification module and gated repair module during the generation, clarification and repair processes, and continuously optimize the large language model in the SQL generation module based on clause-level reward shaping, counterfactual contrast learning and self-game training mechanisms.

2. The intelligent data query system based on natural language processing according to claim 1, characterized in that, The prior modeling module includes: The schema parsing unit is used to parse database schema information to extract table names, field names, data types, and foreign key constraints; The log extraction unit is used to extract field access records, association conditions, and aggregation operation rules from historical query logs; The graph modeling unit is used to create table nodes and field nodes, and set data type attributes and access frequency attributes for table nodes and field nodes respectively; it creates edges based on foreign key constraints and association conditions, and sets weights for the number of times the edges are connected; Enhanced modeling units are used to add time attributes to table nodes, field nodes, and edges to represent structural changes and query behavior evolution; historical queries are clustered based on semantic similarity of historical query logs to generate query intent pattern nodes, and associations are established with table nodes or field nodes; cardinality and aggregation usage counts are recorded on field nodes, and table cluster nodes are added to the graph structure to represent business themes; The knowledge graph output unit is used to output pattern knowledge graphs for use by the SQL generation module and the interactive clarification module.

3. The intelligent data query system based on natural language processing according to claim 1, characterized in that, The SQL generation module includes: The pattern retrieval unit is used to locate relevant table nodes and field nodes in the pattern knowledge graph based on natural language queries, and extract node attributes and edge weights as constraint features. The skeleton building unit is used to generate a query skeleton based on the semantic vector representation of the natural language query and the embedded representation of the pattern knowledge graph, and to calculate dynamic weights based on the similarity between the query semantics and the pattern nodes to adjust the priority of table nodes and field nodes in the query skeleton. The candidate generation unit is used to call a fine-tuned large language model under the constraints of the query skeleton, fill in specific fields, operators and aggregation methods to generate candidate SQL statements, and control the diversity of candidate SQL based on the sampling strategy. The error detection unit is used to perform syntax checks, pattern consistency checks, and logic checks on candidate SQL statements, and output error detection results including error categories. The ambiguity identification unit is used to identify cases in candidate SQL statements where fields have multiple sources, multiple solutions to connection paths, or non-unique logical interpretations based on pattern knowledge graphs, and to generate an ambiguity score for each type of ambiguity as the ambiguity identification result.

4. The intelligent data query system based on natural language processing according to claim 3, characterized in that, The ambiguity identification unit generates an ambiguity score for each type of ambiguity, calculated using the following formula: ; In the formula, Indicates ambiguous types Ambiguity scores; This represents the current number of ambiguous candidates. Ambiguity in historical query logs Frequency of use; For large language models to address ambiguity Confidence level; , , These are the weighting coefficients.

5. The intelligent data query system based on natural language processing according to claim 3, characterized in that, The interactive clarification module includes: The ambiguity measurement unit is used to calculate the ambiguity measure based on the ambiguity score output by the ambiguity identification unit, the overlap of candidate options in the pattern knowledge graph, and the confidence score output by the large language model. The information gain calculation unit is used to calculate the degree to which the uncertainty of the ambiguity set is reduced under different user answers to the candidate clarification question, so as to obtain the expected information gain; The trigger determination unit is used to determine whether to trigger a clarification interaction based on the joint result of ambiguity measurement and expected information gain. The clarification problem optimization unit is used to select the problem with the highest expected information gain and the lowest user interaction cost from the candidate clarification problems. The clarification interaction unit is used to output the question selected by the clarification question optimization unit to the user when triggered and to receive feedback results; The query update unit is used to update the query skeleton or candidate SQL statement based on the feedback result to generate a clarified initial SQL draft.

6. The intelligent data query system based on natural language processing according to claim 3, characterized in that, The gated repair module also includes: The repair execution unit is used to generate multiple repair candidate schemes for the corresponding clause when the repair priority exceeds the threshold, and select the optimal scheme from the repair candidate schemes to perform local repair. The repair methods include field replacement, condition correction or connection path adjustment. The repair constraint unit is used to stop local repair when the number of repair attempts reaches the repair attempt threshold, and to pass the failed repair records as a failure log as a negative sample to the SQL generation module to avoid repeating errors during rollback; The result output unit is used to output the final SQL after the repair is completed.

7. The intelligent data query system based on natural language processing according to claim 1, characterized in that, The continuous optimization module includes: The trajectory recording unit is used to receive the execution trajectories of the SQL generation module, interactive clarification module, and gated repair module during the generation, clarification, and repair processes, and to form a trajectory dataset. The reward shaping unit is used to assign clause-level reward scores to each SQL clause in the trajectory dataset, and adjust the reward signal based on the clause's syntactic correctness, pattern consistency, and logical accuracy. The contrastive learning unit is used to construct counterfactual samples based on the trajectory dataset, compare and train erroneous clauses with correct clauses, and perform cross-stage contrastive learning based on the comparison results between the candidate SQL statement before clarification and the initial SQL draft after clarification. The error pattern mining unit is used to identify recurring error patterns in the trajectory dataset and inject the error patterns as negative sample constraints into the training process. Cross-query migration unit is used to build query clusters based on query semantic similarity and migrate optimization experience within the query clusters during continuous optimization. The self-play training unit is used to train the generative model and the discriminative model in a multi-agent environment by playing against each other, and the role switching controller enables the generative model and the discriminative model to take turns to assume the adversarial or guiding roles in different tasks. The optimization and update unit is used to update the parameters of the large language model in the SQL generation module based on the results of the reward shaping unit, contrastive learning unit, error pattern mining unit, cross-query migration unit, and self-game training unit, so as to achieve continuous optimization.

8. The intelligent data query system based on natural language processing according to claim 1, characterized in that, The system also includes a fine-tuning training module, which is used for: Based on problem data from real business scenarios, we analyze problem prototypes and abstract business rules and logic to form standardized query templates. The training data is expanded based on the standardized query template to generate training samples covering multiple business scenarios; Introduce diverse natural language expressions, including synonyms and colloquial questions, into the training samples to improve the model's expressive robustness. The training samples are integrated into a fine-tuning dataset, and supervised fine-tuning is performed on the general large language model to obtain a large language model suitable for the NL2SQL task.

9. A natural language processing-based intelligent data query method, applied to a natural language processing-based intelligent data query system as described in any one of claims 1-8, characterized in that, The method includes: It receives natural language queries input by users and extracts database schema information from the target database; A schema knowledge graph is constructed based on the database schema information and historical query logs; A query skeleton is generated based on the natural language query and the pattern knowledge graph. Under the constraints of the query skeleton, a fine-tuned large language model is called to generate candidate SQL statements, and error detection and ambiguity recognition are performed on the candidate SQL statements. Based on the ambiguity identification results, the ambiguity metric and expected information gain are calculated based on the pattern knowledge graph to determine whether to trigger a clarification interaction; when triggered, user feedback is obtained to form a clarification result, and the query skeleton or candidate SQL statement is updated based on the clarification result to generate a first draft of the clarified SQL. Based on the error detection results, a dual-state gating mechanism, including workload gate units and clause gate units, is used to determine the repair reference and repair priority. Under the constraint of the repair attempt count threshold, the clauses in the candidate SQL statement or the initial SQL draft are partially repaired. When the repair attempt count reaches the repair attempt count threshold, the process reverts to the SQL generation step to reconstruct the query skeleton and candidate SQL statements. After the repair is completed, the final SQL is output. Execute the final SQL in the target database and output the execution result to the user; The execution trajectory generated during SQL generation, interactive clarification, and gating repair is collected, and the large language model is continuously optimized based on clause-level reward shaping, counterfactual contrast learning, and self-game training mechanisms.