A method, apparatus, device, and medium for NL2SQL optimization based on a large model.

By constructing a target metadata knowledge base and a large model to complete the SQL elements in natural language query requests, and performing syntax and logic verification, the semantic parsing problem of NL2SQL technology in complex scenarios is solved, and the accurate generation and execution of SQL statements are achieved.

CN120743939BActive Publication Date: 2026-07-31SHANDONG LANGCHAO YUNTOU INFORMATION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SHANDONG LANGCHAO YUNTOU INFORMATION TECH CO LTD
Filing Date
2025-07-04
Publication Date
2026-07-31

AI Technical Summary

Technical Problem

Existing NL2SQL technology suffers from inaccurate semantic parsing when handling complex scenarios, leading to missing or incorrect SQL elements. Furthermore, it lacks sufficient ability to integrate metadata about database table structures and relationships, resulting in SQL statements that cannot be executed correctly.

Method used

Construct a target metadata knowledge base, identify target entities in natural language query requests through a large model, complete missing SQL elements, perform syntax and logic checks, and generate accurate SQL statements.

Benefits of technology

It improves the accuracy and reliability of NL2SQL, reduces the cost of manual intervention, and provides efficient and robust technical support for enterprise-level data interaction.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120743939B_ABST
    Figure CN120743939B_ABST
Patent Text Reader

Abstract

This application discloses a method, apparatus, device, and medium for NL2SQL optimization based on a large model, relating to the field of artificial intelligence technology. The method includes: constructing a target metadata knowledge base and obtaining an initial natural language query request; determining each target entity corresponding to the initial natural language query request, and determining the missing target SQL elements in the initial natural language query request based on each target entity; generating first prompt words based on the initial natural language query request, target SQL elements, and the target metadata knowledge base, and using a target large model to complete the target SQL elements based on the first prompt words to obtain the target natural language query request; generating several candidate SQL statements corresponding to the target natural language query request using the target large model, validating each candidate SQL statement, and determining the target SQL statement from the candidate SQL statements based on the validation results. This application can improve the accuracy of NL2SQL by utilizing a large model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of artificial intelligence technology, and in particular to a method, apparatus, device, and medium for NL2SQL optimization based on a large model. Background Technology

[0002] NL2SQL (Natural Language to SQL) technology aims to automatically convert users' natural language queries into SQL statements, lowering the technical barrier to database operations and enabling non-technical users to easily interact with databases. With the rapid development of LLM (Large Language Model), its powerful text generation and semantic understanding capabilities have brought new breakthroughs to NL2SQL technology. However, because natural language queries often contain ambiguous expressions, elliptical clauses, or domain-specific terminology, LLM is prone to inaccurate contextual semantic parsing, leading to missing or incorrect SQL elements when handling complex scenarios such as multi-table joins, nested aggregate functions, and combined conditional logic. Furthermore, existing methods lack sufficient integration capabilities for database table structure, field types, and relationship metadata; LLM may ignore field constraints or inter-table dependencies when generating SQL, resulting in SQL statements that cannot be executed correctly.

[0003] In conclusion, how to leverage large models to improve the accuracy of NL2SQL is a pressing technical problem that needs to be solved. Summary of the Invention

[0004] In view of this, the purpose of this invention is to provide a method, apparatus, device, and medium for NL2SQL optimization based on a large model, which can improve the accuracy of NL2SQL by utilizing a large model. The specific solution is as follows:

[0005] Firstly, this application provides an NL2SQL optimization method based on a large model, including:

[0006] A target metadata knowledge base is constructed based on the data in the target database, and the initial natural language query request input by the user is obtained;

[0007] Identify each target entity corresponding to the initial natural language query request, and determine the missing target SQL element in the initial natural language query request based on each target entity;

[0008] Based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, a corresponding first prompt word is generated. Then, using a pre-trained target big model, the missing target SQL element in the initial natural language query request is completed based on the first prompt word to obtain the target natural language query request.

[0009] The target large model is used to generate several candidate SQL statements corresponding to the target natural language query request, and each candidate SQL statement is subjected to preset syntax verification and preset logic verification. Based on the verification results, the target SQL statement corresponding to the target natural language query request is determined from each candidate SQL statement.

[0010] Optionally, constructing a target metadata knowledge base based on data in the target database includes:

[0011] Extract the table names, field names, data types, and comments of each field from the target database to obtain the metadata of the target database.

[0012] Obtain the business document corresponding to the target business scenario, and parse the data dictionary corresponding to the business document and the metadata, so as to map each target business term in the business document to the corresponding calculation logic based on the data dictionary;

[0013] Based on the target business terms and the corresponding computational logic, a target domain terminology library is constructed, and the aggregation function mapping table and time expression conversion rules corresponding to each target business term are determined, so as to generate corresponding target business rules based on the target domain terminology library, the aggregation function mapping table and the time expression conversion rules;

[0014] The target big model is used to convert the metadata and the target business rules into corresponding target vectors, and the target vectors are stored in a preset vector database to obtain the target metadata knowledge base.

[0015] Optionally, determining the target entities corresponding to the initial natural language query request includes:

[0016] Extract each initial entity from the initial natural language query request using a preset natural language processing library and a preset business dictionary;

[0017] The first fuzzy entity in each initial entity is determined based on the preset time fuzzy word definition rule, and the second fuzzy entity in each initial entity is determined based on the preset business indicator fuzzy word definition rule, and the first fuzzy entity and the second fuzzy entity are filtered out from each initial entity;

[0018] The first fuzzy entity is converted into the corresponding target time entity, and the second fuzzy entity is converted into the corresponding target computation logic;

[0019] Based on the target time entity, the target calculation logic, and the filtered initial entities, the target entities corresponding to the initial natural language query request are determined.

[0020] Optionally, determining the missing target SQL element in the initial natural language query request based on the target entities includes:

[0021] A corresponding target SQL statement template framework is generated by a preset intent classification model, and the target entities are compared based on the target SQL statement template framework.

[0022] Based on the comparison results, the missing target SQL element in the initial natural language query request is determined, and the missing target element prompt information corresponding to the target SQL element is generated;

[0023] Accordingly, the step of generating the corresponding first prompt word based on the initial natural language query request, the target SQL element, and the target metadata knowledge base includes:

[0024] The first prompt word is generated based on the initial natural language query request, the target missing element prompt information, and the target metadata knowledge base.

[0025] Optionally, the step of performing preset syntax validation on each of the candidate SQL statements includes:

[0026] The candidate SQL statements are converted into corresponding abstract syntax trees using the target SQL parsing library.

[0027] Traverse each node in the abstract syntax tree, verify whether there are spelling errors in the keywords corresponding to each node, verify whether the number of function nodes meets the preset quantity condition, and verify whether the identifier nodes meet the target query conditions corresponding to the target natural language query request.

[0028] Extract the table name and the corresponding field name from the abstract syntax tree, and query the target metadata knowledge base to verify whether the table name and the field name exist in the target metadata knowledge base, and to verify whether the field name exists in the data table corresponding to the table name.

[0029] Optionally, a preset logical validation is performed on each of the candidate SQL statements, including:

[0030] If the candidate SQL statement meets the preset multi-table query conditions, then check whether there is a target related field in the candidate SQL statement. If not, mark the candidate SQL statement as having the risk of Cartesian product.

[0031] Identify the non-aggregate fields in the candidate SQL statements, and determine whether the non-aggregate fields are included in the target grouping fields;

[0032] In a homogeneous environment, the candidate SQL statements are executed using the target sandbox database to verify whether there are any runtime errors.

[0033] Optionally, the NL2SQL optimization method based on large models further includes:

[0034] Based on the execution result of the target SQL statement, obtain the feedback data corresponding to the target SQL statement, and the exception data corresponding to the execution result;

[0035] The feedback data and the abnormal data are deduplicated and their validity is verified to obtain corrected data, and the target abnormal data that meets the preset high-frequency error conditions is identified in the abnormal data.

[0036] Generate a second prompt word corresponding to the target abnormal data, generate several business data based on the second prompt word, and update the training data corresponding to the target large model based on the correction data and the business data;

[0037] The parameters of the target large model are fine-tuned based on the preset parameter update ratio conditions and the updated training data, and the target metadata knowledge base is updated based on the feedback data.

[0038] Secondly, this application provides an NL2SQL optimization device based on a large model, comprising:

[0039] The target metadata knowledge base construction module is used to build a target metadata knowledge base based on data in the target database and to obtain the initial natural language query request input by the user.

[0040] The target SQL element determination module is used to determine each target entity corresponding to the initial natural language query request, and to determine the missing target SQL element in the initial natural language query request based on each target entity;

[0041] The target natural language query request determination module is used to generate corresponding first prompt words based on the initial natural language query request, the target SQL element and the target metadata knowledge base, and use a pre-trained target big model to complete the missing target SQL element in the initial natural language query request based on the first prompt words to obtain the target natural language query request.

[0042] The target SQL statement determination module is used to generate several candidate SQL statements corresponding to the target natural language query request using the target large model, perform preset syntax checks and preset logic checks on each candidate SQL statement, and determine the target SQL statement corresponding to the target natural language query request from each candidate SQL statement based on the check results.

[0043] Thirdly, this application provides an electronic device, comprising:

[0044] Memory, used to store computer programs;

[0045] A processor is used to execute the computer program to implement the aforementioned large-model-based NL2SQL optimization method.

[0046] Fourthly, this application provides a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the aforementioned NL2SQL optimization method based on a large model.

[0047] In this application, firstly, a target metadata knowledge base is constructed based on data in the target database, and an initial natural language query request input by the user is obtained; then, each target entity corresponding to the initial natural language query request is determined, and the missing target SQL element in the initial natural language query request is determined based on each target entity; subsequently, a corresponding first prompt word is generated based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, and a pre-trained target big model is used to complete the missing target SQL element in the initial natural language query request based on the first prompt word to obtain the target natural language query request; finally, several candidate SQL statements corresponding to the target natural language query request are generated using the target big model, and preset syntax and logic checks are performed on each candidate SQL statement, and the target SQL statement corresponding to the target natural language query request is determined from each candidate SQL statement based on the check results. As can be seen from the above, this application first constructs a target metadata knowledge base corresponding to the target database; then, it identifies the missing target SQL elements in the initial natural language query request by recognizing each target entity in the initial natural language query request; next, it generates first prompt words based on the initial natural language query request, the target SQL elements, and the target metadata knowledge base, and uses a large model to complete the missing target SQL elements in the initial natural language query request based on the first prompt words to obtain the target natural language query request; finally, it validates the candidate SQL statements generated based on the target natural language query request to obtain the target SQL statement. In this way, this application can improve the accuracy of NL2SQL by completing the missing elements in the natural language query request. At the same time, the dual validation mechanism combining syntax and logic validation ensures the reliability of SQL execution, significantly reducing the cost of manual intervention and operational risks, providing efficient and robust technical support for enterprise-level data interaction, and promoting a comprehensive improvement in the accuracy, adaptability, and user experience of natural language and database interaction. Attached Figure Description

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

[0049] Figure 1 A flowchart of an NL2SQL optimization method based on a large model is provided for this application;

[0050] Figure 2 A flowchart illustrating a specific NL2SQL optimization method based on a large model is provided for this application;

[0051] Figure 3 A schematic diagram of the structure of an NL2SQL optimization device based on a large model provided in this application;

[0052] Figure 4 This application provides a structural diagram of an electronic device. Detailed Implementation

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

[0054] NL2SQL technology aims to automatically convert users' natural language queries into SQL statements, lowering the technical barrier to database operations and enabling non-technical users to easily interact with the database. With the rapid development of LLM (Limited Language Management), its powerful text generation and semantic understanding capabilities have brought new breakthroughs to NL2SQL technology. However, because natural language queries often contain ambiguous expressions, elliptical sentences, or domain-specific terminology, LLM is prone to missing or incorrect SQL elements due to inaccurate contextual semantic parsing when handling complex scenarios such as multi-table joins, nested aggregate functions, and combined conditional logic. Furthermore, existing methods lack sufficient integration capabilities for database table structure, field types, and relationship metadata; LLM may ignore field constraints or inter-table dependencies when generating SQL, resulting in incorrectly executed SQL statements. Therefore, this application provides an NL2SQL optimization scheme based on a large model, which can improve the accuracy of NL2SQL by leveraging the large model.

[0055] See Figure 1 As shown, this embodiment of the invention discloses an NL2SQL optimization method based on a large model, which may include:

[0056] Step S11: Construct a target metadata knowledge base based on the data in the target database, and obtain the initial natural language query request input by the user.

[0057] In this embodiment, the metadata of the target database can first be parsed. Specifically, the above-mentioned construction of a target metadata knowledge base based on the data in the target database can include: firstly, extracting the table names, field names, data types, and comments of each field in the target database to obtain the metadata of the target database; then, obtaining the business documents corresponding to the target business scenario and parsing the data dictionary corresponding to the business documents and the metadata to map each target business term in the business documents to corresponding computational logic based on the data dictionary; subsequently, constructing a target domain terminology library based on each target business term and the computational logic corresponding to each target business term, and determining the aggregation function mapping table and time expression conversion rules corresponding to each target business term to generate corresponding target business rules based on the target domain terminology library, the aggregation function mapping table, and the time expression conversion rules; finally, using the target big model to convert the metadata and the target business rules into corresponding target vectors and storing the target vectors in a preset vector database to obtain the target metadata knowledge base.

[0058] Specifically, in this embodiment, to extract metadata from the target database, basic information such as table names, field names, data types, and field comments of each table can be extracted through database interfaces, such as MySQL's INFORMATION_SCHEMA, or SQL queries. A standardized metadata structure is then constructed to obtain the target database's metadata. To determine the relationships between tables, foreign keys in the target database are first parsed, and an ER diagram (Entity-Relationship Diagram) is constructed to generate the corresponding inter-table association matrix R, clarifying the primary key-foreign key relationships in multi-table joins. Then, for tables without explicit foreign keys, the similarity of field names in different tables can be determined. If the similarity is greater than or equal to a threshold, such as 0.8, a potential relationship between the two tables is inferred by combining business rules, such as "orders must be associated with products," thus avoiding missing associations due to non-standard database design.

[0059] It should be noted that in this embodiment, each target business term in the business document needs to be mapped to its corresponding calculation logic. For example, "monthly active users" is mapped to `register_time >= DATE_SUB(CURRENT_DATE(),INTERVAL 1 MONTH)`, "repurchase rate" is defined as `COUNT(DISTINCT CASE WHEN order_count >= 2THEN user_id END) / COUNT(DISTINCT user_id)`, and "average order value" is defined as `total_amount / order_count`, forming a "business term-calculation logic" lookup table to obtain the corresponding target domain terminology library, which serves as the key basis for subsequent semantic completion. Furthermore, a "natural language keyword-aggregate function" mapping table can be constructed, i.e., an aggregation function mapping table, for example, "average" is mapped to `AVG()`, and "sum" is mapped to `SUM()`. Then, time expression conversion rules can be determined, for example, "recent N days" is converted to `field >= CURDATE() - INTERVAL N DAY`. Finally, based on the "business terminology-computational logic" lookup table (target domain terminology database), the "natural language keyword-aggregate function" mapping table (aggregate function mapping table), and time expression conversion rules, corresponding target business rules can be generated. As the core of business knowledge, these business rules guide the LLM in generating SQL fragments that conform to business logic during semantic completion.

[0060] Finally, in this embodiment, based on the relationships between data tables, metadata and target business rules can be converted into natural language descriptions of Prompt templates. The target large model then uses these Prompt templates to convert the metadata and target business rules into corresponding target vectors, which are stored in the Faiss (Facebook AI Similarity Search) vector database, forming a searchable "metadata + business knowledge" knowledge base—the target metadata knowledge base. This target metadata knowledge base supports semantic association retrieval during subsequent queries. In this way, by integrating business rules into metadata, the LLM can directly invoke business rules when processing user queries. For example, when a user enters "query users with an average order value exceeding 200 yuan," the system automatically parses "average order value" as `AVG(order_amount)` and generates the condition `HAVING AVG(order_amount) > 200`, avoiding parsing errors caused by terminological ambiguity and improving the accuracy of semantic mapping from the source.

[0061] Step S12: Determine each target entity corresponding to the initial natural language query request, and determine the missing target SQL element in the initial natural language query request based on each target entity.

[0062] In this embodiment, determining the target entities corresponding to the initial natural language query request may include: extracting each initial entity from the initial natural language query request using a preset natural language processing library and a preset business dictionary; determining a first fuzzy entity among the initial entities based on preset time fuzzy word definition rules, and determining a second fuzzy entity among the initial entities based on preset business indicator fuzzy word definition rules; filtering the first fuzzy entity and the second fuzzy entity from the initial entities; converting the first fuzzy entity into a corresponding target time entity, and converting the second fuzzy entity into a corresponding target calculation logic; and determining the target entities corresponding to the initial natural language query request based on the target time entity, the target calculation logic, and the filtered initial entities. Specifically, the spaCy open-source natural language processing library can be used, combined with a custom business dictionary, to extract each initial entity from the initial natural language query request. Initial entities can be divided into explicit entities and implicit business entities, where explicit entities include, for example, table name: "User". `users`, field name: "Order Amount" `order_amount`, time expression: "Q4 of 2023" The period is from October 1, 2023 to December 31, 2023. Implicit business entities are those inferred through business knowledge. For example, "repeat purchase user" needs to be resolved to "user with at least 2 order records," corresponding to the condition `HAVING COUNT(order_id) >= 2`. Then, the first fuzzy entity in each initial entity can be determined and converted into the corresponding target time entity. For example, "last month" is calculated based on the current time, such as April 2025. March 1, 2025 - March 31, 2025. Identify the second fuzzy entity among each initial entity and convert the second fuzzy entity into the corresponding target computation logic. For example: "High-spending user". According to the business definition, if the cumulative consumption amount is >10,000 yuan, it will be converted to `total_spend > 10,000`.

[0063] It should be noted that the above-mentioned determination of the missing target SQL elements in the initial natural language query request based on the target entities may include: firstly, generating a corresponding target SQL statement template framework through a preset intent classification model, and comparing the target entities based on the target SQL statement template framework; then, determining the missing target SQL elements in the initial natural language query request based on the comparison results, and generating target missing element prompt information corresponding to the target SQL elements. Specifically, the target SQL statement template framework is generated through the intent classification model, such as `SELECT [field] FROM [table] [JOIN condition] WHERE [condition] GROUP BY [grouping] HAVING [aggregation condition]`, and then each target entity is identified based on the target SQL statement template framework to determine whether there are missing SELECT fields, JOIN conditions, aggregate functions, or other target SQL elements, and corresponding target missing element prompt information can be generated.

[0064] Step S13: Generate corresponding first prompt words based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, and use the pre-trained target big model to complete the missing target SQL element in the initial natural language query request based on the first prompt words to obtain the target natural language query request.

[0065] It should be noted that the target large model in this embodiment needs to be pre-trained. Specifically, a multi-round Prompt containing business knowledge can be designed to guide the LLM to generate query statements that conform to real business scenarios. That is, based on the schema and business rules of the target database, 10 "original problem-rewritten problem" query pairs involving multi-table joins can be generated. Setting `temperature=0.8` can improve the diversity of generated query pairs, and `max_tokens=512` can control the complexity of SQL statements to ensure that the generated data covers core business scenarios, such as order analysis and user segmentation. For the generated basic natural language query requests, the Prompt guides the LLM to generate 2-3 semantically equivalent variants to obtain the corresponding generated data. In this way, the large model can learn different natural language expressions of the same business logic, improving its adaptability to diverse user input. Afterwards, data augmentation can be performed on the generated data through business synonym replacement and business scenario parameterization. Specifically, when performing synonym replacement, industry dictionaries can be used to replace business terms, such as replacing "user" with "customer" and "sales revenue" with "revenue amount," ensuring the large model's generalization ability to business terminology. When parameterizing business scenarios, time and numerical variables in the generated data can be abstracted into parameters, such as abstracting "recent {N} days" into "recent 30 days" and "recent 90 days," generating multiple sets of query pairs with the same structure but different parameters to cover common parameterized query scenarios in the business. Then, for the data-enhanced generated data, corresponding SQL statements are generated, and SQLGlot is used to parse the generated SQL statements to verify whether they conform to the target database syntax, such as MySQL's (My Structured Query Language) date function format and PostgreSQL's JOIN syntax. The generated SQL statements can then be executed in a sandbox database to compare with expected business results, such as whether the "average order value" calculation result is positive, whether the time filter correctly filters the data, and eliminating samples with logical errors. For example, if the generated SQL statement does not correctly join data tables, resulting in a Cartesian product, or if the aggregate function is used incorrectly, it is marked as an invalid SQL statement. After eliminating invalid SQL statements, the dataset can be further constructed. Specifically, the dataset can be divided into three layers based on business complexity, and business knowledge can be injected into each layer in turn:

[0066] (1) Basic layer (40%): SQL statements involve single table queries, such as "query the names of users who registered in 2023", focusing on the direct mapping between business terms and fields.

[0067] (2) Advanced level (40%): SQL statements involving multi-table joins and aggregations, such as “total order amount by user gender”, introducing business applications of inter-table join rules and aggregate functions.

[0068] (3) Complex layer (20%): SQL statements involve subqueries + window functions + nested business rules, such as "querying regions where the average order value is higher than the national average", which includes multi-hop reasoning, that is, first calculate the national average, and then filter the combination of regions and business indicators, i.e., average order value = total amount / number of orders.

[0069] In this way, by training large models with datasets constructed in layers, the large models can gradually transition from learning simple business scenarios to complex logic, avoiding a decline in learning efficiency due to business knowledge overload.

[0070] In this embodiment, a first prompt word, i.e., "Promot," can be generated based on the initial natural language query request, the target missing element hint information corresponding to the target SQL element, and the target metadata knowledge base. Then, the missing semantic information can be dynamically filled in using the text completion technology of a pre-trained target large model, thus obtaining the target natural language query request.

[0071] Step S14: Generate several candidate SQL statements corresponding to the target natural language query request using the target large model, perform preset syntax verification and preset logic verification on each candidate SQL statement, and determine the target SQL statement corresponding to the target natural language query request from each candidate SQL statement based on the verification results.

[0072] In this embodiment, a target large model can be used to generate several candidate SQL statements corresponding to the target natural language query request based on business rules. Then, syntax and logic checks can be performed on the candidate SQL statements to filter out executable target SQL statements. The aforementioned preset syntax check on each candidate SQL statement can include: first, using a target SQL parsing library to convert each candidate SQL statement into a corresponding abstract syntax tree; then, traversing each node in the abstract syntax tree to check if there are spelling errors in the keywords corresponding to each node, whether the number of function nodes meets a preset quantity condition, and whether the identifier nodes meet the target query conditions corresponding to the target natural language query request; then, extracting the table name and the field names corresponding to the table name from the abstract syntax tree, and querying the target metadata knowledge base to check if the table name and the field names exist in the target metadata knowledge base, and to check if the field names exist in the data table corresponding to the table name. Specifically, the candidate SQL statements can be converted into an abstract syntax tree using an SQL parsing library. The nodes of the abstract syntax tree include the following types:

[0073] (1) Statement nodes: such as SELECT, INSERT, UPDATE.

[0074] (2) Clause nodes: such as FROM, WHERE, GROUP BY.

[0075] (3) Expression nodes: such as column references, function calls, and operators.

[0076] (4) Identifier nodes: such as table name, field name, alias.

[0077] (5) Literal nodes: such as strings, numbers, and booleans.

[0078] Next, we can verify whether there are spelling errors in the keywords corresponding to each node, verify the number of function nodes, and verify whether the identifier nodes are correct. Then, we can compare the field names and table names in the abstract syntax tree with the metadata knowledge base, verify whether the table names and field names exist in the target metadata knowledge base, and verify whether the field names exist in the data table corresponding to the table names. At the same time, we can also verify whether the data types corresponding to the fields match.

[0079] It should be noted that the aforementioned pre-defined logical verification of each candidate SQL statement may include: if the candidate SQL statement meets the pre-defined multi-table query conditions, then verify whether the candidate SQL statement contains a target related field; if not, mark the candidate SQL statement as having a Cartesian product risk; determine the non-aggregate fields in the candidate SQL statement and determine whether the non-aggregate fields are included in the target grouping field; execute the candidate SQL statement in a homogeneous environment using the target sandbox database to verify whether there are any runtime errors. Specifically, in this embodiment, during multi-table queries, if no `JOIN` condition or `WHERE` related field is detected, it is marked as a Cartesian product risk. For example, `SELECT * FROM users, orders` has no related condition and is directly rejected. Then, it is necessary to verify whether the `GROUP BY` field contains all non-aggregate fields in `SELECT`, such as `SELECT user_id, region, SUM(amount)` must `GROUPBY user_id, region`. In addition, for calculated metrics such as "average order value," check whether the correct denominator is included. For example, to avoid division by zero errors when the order quantity is 0, preprocessing is performed using `HAVING COUNT(order_id) > 0`. Finally, each candidate SQL statement can be pre-executed in a sandbox database that is isomorphic to the production environment to catch runtime errors such as null pointer exceptions and type mismatches.

[0080] In this way, by performing syntax and logic checks on each candidate SQL statement, we can eliminate candidate SQL statements that cannot be executed and obtain the target SQL statement.

[0081] As can be seen from the above, in this embodiment, a target metadata knowledge base is first constructed based on the data in the target database, and an initial natural language query request input by the user is obtained. Then, the target entities corresponding to the initial natural language query request are determined, and the missing target SQL elements in the initial natural language query request are determined based on the target entities. Subsequently, a corresponding first prompt word is generated based on the initial natural language query request, the target SQL elements, and the target metadata knowledge base. Using a pre-trained target big model, the missing target SQL elements in the initial natural language query request are completed based on the first prompt word to obtain the target natural language query request. Finally, the target big model is used to generate several candidate SQL statements corresponding to the target natural language query request, and preset syntax and logic checks are performed on each candidate SQL statement. Based on the check results, the target SQL statement corresponding to the target natural language query request is determined from each candidate SQL statement. As can be seen from the above, this embodiment first constructs a target metadata knowledge base corresponding to the target database; then, it identifies the missing target SQL elements in the initial natural language query request by recognizing each target entity in the initial natural language query request; next, it generates first prompt words based on the initial natural language query request, the target SQL elements, and the target metadata knowledge base, and uses a large model to complete the missing target SQL elements in the initial natural language query request based on the first prompt words to obtain the target natural language query request; finally, it verifies the candidate SQL statements generated based on the target natural language query request to obtain the target SQL statement. In this way, this embodiment can improve the accuracy of NL2SQL by completing the missing elements in the natural language query request. At the same time, this embodiment ensures the reliability of SQL execution through a dual verification mechanism combining syntax verification and logical verification, significantly reducing the cost of manual intervention and operational risks, providing efficient and robust technical support for enterprise-level data interaction, and promoting a comprehensive improvement in the accuracy, adaptability, and user experience of natural language and database interaction.

[0082] As described in the previous embodiment, this application can complete the missing elements in the initial natural language query request to obtain the target natural language query request, and verify the candidate SQL statements corresponding to the target natural language query request to filter out the target SQL statement, thereby improving the accuracy of NL2SQL. Next, this embodiment will describe in detail the optimization process of the target SQL statement and the target large model. See [link to documentation]. Figure 2 As shown, this embodiment of the invention further discloses an NL2SQL optimization method based on a large model, which may include:

[0083] Step S21: Construct a target metadata knowledge base based on the data in the target database, and obtain the initial natural language query request input by the user.

[0084] Step S22: Determine each target entity corresponding to the initial natural language query request, and determine the missing target SQL element in the initial natural language query request based on each target entity.

[0085] Step S23: Generate corresponding first prompt words based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, and use the pre-trained target big model to complete the missing target SQL element in the initial natural language query request based on the first prompt words to obtain the target natural language query request.

[0086] Step S24: Generate several candidate SQL statements corresponding to the target natural language query request using the target large model, perform preset syntax verification and preset logic verification on each candidate SQL statement, and determine the target SQL statement corresponding to the target natural language query request from each candidate SQL statement based on the verification results.

[0087] In this embodiment, after obtaining the target SQL statement corresponding to the target natural language query request, the target SQL statement can be further optimized. Specifically, firstly, feedback data corresponding to the target SQL statement and abnormal data corresponding to the execution result are obtained based on the execution result of the target SQL statement; the feedback data and abnormal data are deduplicated and validated to obtain corrected data, and target abnormal data that meets the preset high-frequency error conditions is identified among the abnormal data; second prompt words corresponding to the target abnormal data are generated, and several business data are generated based on the second prompt words, and the training data corresponding to the target large model is updated based on the corrected data and business data; the parameters of the target large model are fine-tuned based on the preset parameter update ratio conditions and the updated training data, and the target metadata knowledge base is updated based on the feedback data. It should be noted that the feedback data includes two aspects: explicit feedback data and implicit feedback data, i.e., abnormal data. Specifically, the displayed feedback data is shown below:

[0088] (1) Manual annotation: Users directly mark whether the generated target SQL statement is correct, or manually correct errors, such as changing the incorrect `INNER JOIN` to `LEFT JOIN` to retain users without orders.

[0089] (2) Business rule correction: When it is found that the definition of "average order price" is missing "deduplicated invalid orders", users can submit the corrected business rules through the modification interface, such as `SUM(valid_order_amount) / COUNT(DISTINCT order_id)`.

[0090] Implicit feedback data consists of exception data determined based on the execution result of the target SQL statement, as shown below:

[0091] (1) Abnormal execution result: If the target SQL statement returns an empty result, such as no data for querying “users registered in 2023”, but the business expects the data to exist, it may be due to a time parsing error.

[0092] (2) Abnormal statistical values: The aggregation results show unreasonable values, such as "average order amount" being 0, which may be due to the failure to exclude test orders.

[0093] (3) Performance feedback: If the execution time exceeds 30 seconds, it indicates that the associated conditions are redundant or the index is missing, and the SQL structure needs to be optimized.

[0094] Afterwards, duplicate and validity verification can be performed on the feedback and abnormal data to obtain corrected data, and high-confidence corrected data can be prioritized for labeling according to the active learning strategy. For example, queries that trigger Cartesian product warnings three times consecutively are prioritized for labeling. For high-frequency error target abnormal data, such as "the subquery does not use the business-defined indicator", more than 100 similar business data can be generated based on the second prompt word corresponding to the target abnormal data using the large model, strengthening the model's memory of the business logic. Then, the training data corresponding to the target large model can be updated using the corrected data and business data, and the parameters of the last 10% layer of the target large model can be fine-tuned using the training data, avoiding damage to general capabilities while enhancing the generation of business-specific logic, such as enhancing the "risk level" calculation rules in the financial field. Finally, if the user feedback data table changes, such as adding a `vip_users` table, the metadata parsing of the target database is automatically triggered, updating the table structure and relationships in the target metadata knowledge base. Or, when the definition of business terms is updated, such as "last 30 days" being changed to "within the calendar month", the time expression conversion rules in the metadata are updated synchronously, and the target metadata knowledge base is updated to ensure that subsequent SQL is generated according to the new rules.

[0095] For more detailed processing procedures of steps S21 to S23, please refer to the relevant content disclosed in the foregoing embodiments, which will not be repeated here.

[0096] As can be seen from the above, this embodiment first obtains the feedback data corresponding to the target SQL statement and the abnormal data in the execution result of the target SQL statement; then, it performs deduplication and validity verification on the feedback data and abnormal data to obtain corresponding correction data, and determines the target abnormal data that meets the preset high-frequency error conditions; then, it generates several business data corresponding to the target abnormal data, and uses the correction data and business data to fine-tune the parameters of the target large model, and updates the target metadata knowledge base based on the feedback data. In this way, this embodiment can transform the feedback data and abnormal data into targeted optimization signals, dynamically update the training data of the target large model, adjust the parameters of the target large model, and iterate the business rules, realizing rapid adaptation to new scenarios and continuous correction of historical problems, which can improve the accuracy of NL2SQL.

[0097] Accordingly, see Figure 3 As shown in the embodiments of this application, a large-model-based NL2SQL optimization device is also provided, which may include:

[0098] The target metadata knowledge base construction module 11 is used to construct the target metadata knowledge base based on the data in the target database and to obtain the initial natural language query request input by the user.

[0099] The target SQL element determination module 12 is used to determine each target entity corresponding to the initial natural language query request, and to determine the missing target SQL element in the initial natural language query request based on each target entity;

[0100] The target natural language query request determination module 13 is used to generate corresponding first prompt words based on the initial natural language query request, the target SQL element and the target metadata knowledge base, and use a pre-trained target big model to complete the missing target SQL element in the initial natural language query request based on the first prompt words to obtain the target natural language query request.

[0101] The target SQL statement determination module 14 is used to generate a number of candidate SQL statements corresponding to the target natural language query request using the target big model, and to perform preset syntax verification and preset logic verification on each of the candidate SQL statements, and to determine the target SQL statement corresponding to the target natural language query request from each of the candidate SQL statements based on the verification results.

[0102] As can be seen from the above, this application first constructs a target metadata knowledge base based on data in the target database and obtains the initial natural language query request input by the user; then, it determines each target entity corresponding to the initial natural language query request and determines the missing target SQL element in the initial natural language query request based on each target entity; subsequently, it generates corresponding first prompt words based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, and uses a pre-trained target big model to complete the missing target SQL element in the initial natural language query request based on the first prompt words to obtain the target natural language query request; finally, it uses the target big model to generate several candidate SQL statements corresponding to the target natural language query request, performs preset syntax checks and preset logic checks on each candidate SQL statement, and determines the target SQL statement corresponding to the target natural language query request from each candidate SQL statement based on the check results. As can be seen from the above, this application first constructs a target metadata knowledge base corresponding to the target database; then, it identifies the missing target SQL elements in the initial natural language query request by recognizing each target entity in the initial natural language query request; next, it generates first prompt words based on the initial natural language query request, the target SQL elements, and the target metadata knowledge base, and uses a large model to complete the missing target SQL elements in the initial natural language query request based on the first prompt words to obtain the target natural language query request; finally, it validates the candidate SQL statements generated based on the target natural language query request to obtain the target SQL statement. In this way, this application can improve the accuracy of NL2SQL by completing the missing elements in the natural language query request. At the same time, the dual validation mechanism combining syntax and logic validation ensures the reliability of SQL execution, significantly reducing the cost of manual intervention and operational risks, providing efficient and robust technical support for enterprise-level data interaction, and promoting a comprehensive improvement in the accuracy, adaptability, and user experience of natural language and database interaction.

[0103] In some specific embodiments, the target metadata knowledge base construction module 11 may include:

[0104] Metadata determination unit is used to extract the table name of each data table, the field name of each data table, the data type of each field in each data table, and the comments of each field in the target database to obtain the metadata of the target database.

[0105] The target business term mapping unit is used to obtain the business document corresponding to the target business scenario, and parse the data dictionary corresponding to the business document and the metadata, so as to map each target business term in the business document to the corresponding calculation logic based on the data dictionary;

[0106] The target business rule generation unit is used to construct a target domain terminology library based on each target business term and the calculation logic corresponding to each target business term, and to determine the aggregation function mapping table and time expression conversion rule corresponding to each target business term, so as to generate corresponding target business rules based on the target domain terminology library, the aggregation function mapping table and the time expression conversion rule;

[0107] The target metadata knowledge base construction unit is used to convert the metadata and the target business rules into corresponding target vectors using the target big model, and store the target vectors in a preset vector database to obtain the target metadata knowledge base.

[0108] In some specific embodiments, the target SQL element determination module 12 may include:

[0109] An initial entity extraction unit is used to extract each initial entity from the initial natural language query request using a preset natural language processing library and a preset business dictionary;

[0110] The fuzzy entity determination unit is used to determine a first fuzzy entity among the initial entities based on a preset time fuzzy word definition rule, and to determine a second fuzzy entity among the initial entities based on a preset business indicator fuzzy word definition rule, and to filter the first fuzzy entity and the second fuzzy entity from the initial entities.

[0111] The target calculation logic determination unit is used to convert the first fuzzy entity into a corresponding target time entity and the second fuzzy entity into a corresponding target calculation logic.

[0112] The target entity determination unit is used to determine the target entities corresponding to the initial natural language query request based on the target time entity, the target calculation logic, and the filtered initial entities.

[0113] In some specific embodiments, the target SQL element determination module 12 may include:

[0114] The target SQL statement template framework generation unit is used to generate a corresponding target SQL statement template framework through a preset intent classification model, and to compare the target entities based on the target SQL statement template framework.

[0115] The target missing element prompt information generation unit is used to determine the target SQL element missing in the initial natural language query request based on the comparison result, and generate target missing element prompt information corresponding to the target SQL element;

[0116] Accordingly, the target natural language query request determination module 13 may include:

[0117] The first prompt word generation unit is used to generate the first prompt word based on the initial natural language query request, the target missing element prompt information, and the target metadata knowledge base.

[0118] In some specific implementations, the target SQL statement determination module 14 may include:

[0119] Abstract syntax tree conversion unit is used to convert each of the candidate SQL statements into a corresponding abstract syntax tree using the target SQL parsing library;

[0120] The first syntax verification unit is used to traverse each node in the abstract syntax tree, verify whether there are spelling errors in the keywords corresponding to each node, verify whether the number of function nodes meets the preset quantity condition, and verify whether the identifier nodes meet the target query conditions corresponding to the target natural language query request.

[0121] The second syntax verification unit is used to extract the table name and the field name corresponding to the table name from the abstract syntax tree, and query the target metadata knowledge base to verify whether the table name and the field name exist in the target metadata knowledge base, and to verify whether the field name exists in the data table corresponding to the table name.

[0122] In some specific implementations, the target SQL statement determination module 14 may include:

[0123] The target related field verification unit is used to verify whether the target related field exists in the candidate SQL statement if the candidate SQL statement meets the preset multi-table query conditions; if it does not exist, the candidate SQL statement is marked as having a Cartesian product risk.

[0124] The first logical verification unit is used to determine the non-aggregate fields in the candidate SQL statement and to determine whether the non-aggregate fields are included in the target grouping field.

[0125] The second logical verification unit is used to execute the candidate SQL statement in a homogeneous environment using the target sandbox database and verify whether there are any runtime errors.

[0126] In some specific embodiments, the large-model-based NL2SQL optimization device may further include:

[0127] The feedback data acquisition module is used to acquire feedback data corresponding to the target SQL statement and abnormal data corresponding to the execution result based on the execution result of the target SQL statement.

[0128] The target abnormal data determination module is used to perform deduplication and validity verification on the feedback data and the abnormal data to obtain corrected data, and to determine the target abnormal data in the abnormal data that meets the preset high-frequency error conditions.

[0129] The training data update module is used to generate a second prompt word corresponding to the target abnormal data, generate several business data based on the second prompt word, and update the training data corresponding to the target large model based on the correction data and the business data.

[0130] The target metadata knowledge base update module is used to fine-tune the parameters of the target large model based on preset parameter update ratio conditions and the updated training data, and update the target metadata knowledge base based on the feedback data.

[0131] Furthermore, embodiments of this application also disclose an electronic device, Figure 4 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content of the diagram should not be construed as limiting the scope of this application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 stores a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the large-model-based NL2SQL optimization method disclosed in any of the foregoing embodiments. Furthermore, the electronic device 20 in this embodiment may specifically be an electronic computer.

[0132] In this embodiment, the power supply 23 is used to provide operating voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of this application, and is not specifically limited here; the input / output interface 25 is used to acquire external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.

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

[0134] The operating system 221 is used to manage and control the various hardware devices on the electronic device 20 and the computer program 222, which may be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program capable of performing the large-model-based NL2SQL optimization method executed by the electronic device 20 as disclosed in any of the foregoing embodiments, the computer program 222 may further include computer programs capable of performing other specific tasks.

[0135] Furthermore, this application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the aforementioned disclosed NL2SQL optimization method based on a large model. Specific steps of this method can be found in the corresponding content disclosed in the foregoing embodiments, and will not be repeated here.

[0136] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.

[0137] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

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

[0139] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, 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.

[0140] The technical solutions provided in this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the methods and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.

Claims

1. A large model-based NL2SQL optimization method, characterized in that, include: A target metadata knowledge base is constructed based on the data in the target database, and the initial natural language query request input by the user is obtained; Identify each target entity corresponding to the initial natural language query request, and determine the missing target SQL element in the initial natural language query request based on each target entity; Based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, a corresponding first prompt word is generated. Then, using a pre-trained target big model, the missing target SQL element in the initial natural language query request is completed based on the first prompt word to obtain the target natural language query request. The target large model is used to generate several candidate SQL statements corresponding to the target natural language query request, and each candidate SQL statement is subjected to preset syntax verification and preset logic verification, and the target SQL statement corresponding to the target natural language query request is determined from each candidate SQL statement based on the verification results. The construction of the target metadata knowledge base based on data in the target database includes: Extract the table names, field names, data types, and comments of each field from the target database to obtain the metadata of the target database. Obtain the business document corresponding to the target business scenario, and parse the data dictionary corresponding to the business document and the metadata, so as to map each target business term in the business document to the corresponding calculation logic based on the data dictionary; Based on the target business terms and the corresponding computational logic, a target domain terminology library is constructed, and the aggregation function mapping table and time expression conversion rules corresponding to each target business term are determined, so as to generate corresponding target business rules based on the target domain terminology library, the aggregation function mapping table and the time expression conversion rules; The target big model is used to convert the metadata and the target business rules into corresponding target vectors, and the target vectors are stored in a preset vector database to obtain the target metadata knowledge base; The preset syntax validation of each candidate SQL statement includes: The candidate SQL statements are converted into corresponding abstract syntax trees using the target SQL parsing library. Traverse each node in the abstract syntax tree, verify whether there are spelling errors in the keywords corresponding to each node, verify whether the number of function nodes meets the preset quantity condition, and verify whether the identifier nodes meet the target query conditions corresponding to the target natural language query request. Extract the table name and the field name corresponding to the table name from the abstract syntax tree, and query the target metadata knowledge base to verify whether the table name and the field name exist in the target metadata knowledge base, and to verify whether the field name exists in the data table corresponding to the table name; Perform preset logical checks on each of the candidate SQL statements, including: If the candidate SQL statement meets the preset multi-table query conditions, then check whether there is a target related field in the candidate SQL statement. If not, mark the candidate SQL statement as having the risk of Cartesian product. Identify the non-aggregate fields in the candidate SQL statements, and determine whether the non-aggregate fields are included in the target grouping fields; In a homogeneous environment, the candidate SQL statements are executed using the target sandbox database to verify whether there are any runtime errors.

2. The NL2SQL optimization method based on a large model according to claim 1, characterized in that, The process of determining the target entities corresponding to the initial natural language query request includes: Extract each initial entity from the initial natural language query request using a preset natural language processing library and a preset business dictionary; The first fuzzy entity in each initial entity is determined based on the preset time fuzzy word definition rule, and the second fuzzy entity in each initial entity is determined based on the preset business indicator fuzzy word definition rule, and the first fuzzy entity and the second fuzzy entity are filtered out from each initial entity; The first fuzzy entity is converted into the corresponding target time entity, and the second fuzzy entity is converted into the corresponding target computation logic; Based on the target time entity, the target calculation logic, and the filtered initial entities, the target entities corresponding to the initial natural language query request are determined.

3. The NL2SQL optimization method based on a large model according to claim 1, characterized in that, The step of determining the missing target SQL element in the initial natural language query request based on the target entities includes: A corresponding target SQL statement template framework is generated by a preset intent classification model, and the target entities are compared based on the target SQL statement template framework. Based on the comparison results, the missing target SQL element in the initial natural language query request is determined, and the missing target element prompt information corresponding to the target SQL element is generated; Accordingly, the step of generating the corresponding first prompt word based on the initial natural language query request, the target SQL element, and the target metadata knowledge base includes: The first prompt word is generated based on the initial natural language query request, the target missing element prompt information, and the target metadata knowledge base.

4. The NL2SQL optimization method based on a large model according to any one of claims 1 to 3, characterized in that, Also includes: Based on the execution result of the target SQL statement, obtain the feedback data corresponding to the target SQL statement, and the exception data corresponding to the execution result; The feedback data and the abnormal data are deduplicated and their validity is verified to obtain corrected data, and the target abnormal data that meets the preset high-frequency error conditions is identified in the abnormal data. Generate a second prompt word corresponding to the target abnormal data, generate several business data based on the second prompt word, and update the training data corresponding to the target large model based on the correction data and the business data; The parameters of the target large model are fine-tuned based on the preset parameter update ratio conditions and the updated training data, and the target metadata knowledge base is updated based on the feedback data.

5. A large-model-based NL2SQL optimization device, characterized in that, include: The target metadata knowledge base construction module is used to build a target metadata knowledge base based on data in the target database and to obtain the initial natural language query request input by the user. The target SQL element determination module is used to determine each target entity corresponding to the initial natural language query request, and to determine the missing target SQL element in the initial natural language query request based on each target entity; The target natural language query request determination module is used to generate corresponding first prompt words based on the initial natural language query request, the target SQL element and the target metadata knowledge base, and use a pre-trained target big model to complete the missing target SQL element in the initial natural language query request based on the first prompt words to obtain the target natural language query request. The target SQL statement determination module is used to generate several candidate SQL statements corresponding to the target natural language query request using the target big model, and to perform preset syntax verification and preset logic verification on each of the candidate SQL statements, and to determine the target SQL statement corresponding to the target natural language query request from each of the candidate SQL statements based on the verification results. The target metadata knowledge base construction module includes: Metadata determination unit is used to extract the table name of each data table, the field name of each data table, the data type of each field in each data table, and the comments of each field in the target database to obtain the metadata of the target database. The target business term mapping unit is used to obtain the business document corresponding to the target business scenario, and parse the data dictionary corresponding to the business document and the metadata, so as to map each target business term in the business document to the corresponding calculation logic based on the data dictionary; The target business rule generation unit is used to construct a target domain terminology library based on each target business term and the calculation logic corresponding to each target business term, and to determine the aggregation function mapping table and time expression conversion rule corresponding to each target business term, so as to generate corresponding target business rules based on the target domain terminology library, the aggregation function mapping table and the time expression conversion rule; The target metadata knowledge base construction unit is used to convert the metadata and the target business rules into corresponding target vectors using the target big model, and store the target vectors in a preset vector database to obtain the target metadata knowledge base; The target SQL statement determination module includes: Abstract syntax tree conversion unit is used to convert each of the candidate SQL statements into a corresponding abstract syntax tree using the target SQL parsing library; The first syntax verification unit is used to traverse each node in the abstract syntax tree, verify whether there are spelling errors in the keywords corresponding to each node, verify whether the number of function nodes meets the preset quantity condition, and verify whether the identifier nodes meet the target query conditions corresponding to the target natural language query request. The second syntax verification unit is used to extract the table name and the field name corresponding to the table name from the abstract syntax tree, and query the target metadata knowledge base to verify whether the table name and the field name exist in the target metadata knowledge base, and to verify whether the field name exists in the data table corresponding to the table name. The target related field verification unit is used to verify whether the target related field exists in the candidate SQL statement if the candidate SQL statement meets the preset multi-table query conditions; if it does not exist, the candidate SQL statement is marked as having a Cartesian product risk. The first logical verification unit is used to determine the non-aggregate fields in the candidate SQL statement and to determine whether the non-aggregate fields are included in the target grouping field. The second logical verification unit is used to execute the candidate SQL statement in a homogeneous environment using the target sandbox database and verify whether there are any runtime errors.

6. An electronic device, characterized in that, The electronic device includes a processor and a memory; wherein the memory is used to store a computer program, which is loaded and executed by the processor to implement the large model-based NL2SQL optimization method as described in any one of claims 1 to 4.

7. A computer-readable storage medium, characterized in that, Used to store a computer program, which, when executed by a processor, implements the NL2SQL optimization method based on a large model as described in any one of claims 1 to 4.