NL2SQL optimization method and device based on large model, equipment and medium

By building a target metadata knowledge base and a large model to complete the missing elements in natural language query requests, combined with syntax and logical verification, the problem of inaccurate semantic parsing in NL2SQL technology is solved, and efficient and accurate SQL generation and execution are achieved.

CN120743939AActive Publication Date: 2025-10-03SHANDONG LANGCHAO YUNTOU INFORMATION TECH CO LTD

Patent Information

Application Number
CN202510920287.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-04
Publication Date
2025-10-03
Estimated Expiration
2045-07-04

AI Technical Summary

Technical Problem

The existing NL2SQL technology has inaccurate semantic parsing when processing complex scenarios, resulting in missing or incorrect SQL elements, and insufficient metadata integration capabilities for database table structures and relationships, resulting in the inability to correctly execute the generated SQL statements.

Method used

Build a target metadata knowledge base, use the big model to identify target entities and missing SQL elements in natural language query requests, generate prompt words and complete the query requests, combine syntax and logic verification to generate accurate SQL statements, and optimize the big model through feedback data.

Benefits of technology

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

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120743939A_ABST
    Figure CN120743939A_ABST
Patent Text Reader

Abstract

The invention discloses an NL2SQL optimization method and device based on a large model, equipment and a medium, and relates to the technical field of artificial intelligence, the method comprises the following steps: 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 missing target SQL elements in the initial natural language query request based on each target entity; generating a first cue word based on the initial natural language query request, the target SQL element and the target metadata knowledge base, and complementing the target SQL element based on the first cue word by utilizing the target large model to obtain a target natural language query request; and generating a plurality of candidate SQL statements corresponding to the target natural language query request by using the target large model, verifying each candidate SQL statement, and determining a target SQL statement from each candidate SQL statement based on a verification result. According to the method, the accuracy of the NL2SQL can be improved by utilizing a large model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a large model-based NL2SQL optimization method, device, equipment and medium. Background Art

[0002] NL2SQL (Natural Language to SQL) technology aims to automatically convert users' natural language queries into SQL statements, lowering the technical threshold for database operations and enabling non-technical users to interact with the database conveniently. With the rapid development of LLM (Large Language Model), its powerful text generation and semantic understanding capabilities have brought new breakthroughs to NL2SQL technology. Because natural language queries often contain ambiguous expressions, omitted 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 conditional logic combinations. Existing methods also lack the ability to integrate metadata such as database table structures, field types, and relationships. When generating SQL, LLM may ignore field constraints or dependencies between tables, resulting in the inability to correctly execute the generated SQL statements.

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

[0004] In view of this, the present invention aims to provide a large-model-based NL2SQL optimization method, apparatus, device, and medium, which can utilize the large model to improve the accuracy of NL2SQL. The specific solution is as follows:

[0005] In a first aspect, the present application provides an NL2SQL optimization method based on a large model, comprising:

[0006] Build 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;

[0007] Determine each target entity corresponding to the initial natural language query request, and determine a target SQL element missing from the initial natural language query request based on the target entities;

[0008] generating a corresponding first prompt word based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, and using a pre-trained target large model to complete the target SQL element missing from the initial natural language query request based on the first prompt word to obtain a target natural language query request;

[0009] The target big 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 a preset syntax check and a preset logic check, and a target SQL statement corresponding to the target natural language query request is determined from each candidate SQL statement based on the check results.

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

[0011] Extracting the table name of each data table in the target database, the field name of each data table, the data type of each field in each data table, and the comment of each field to obtain metadata of the target database;

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

[0013] Building a target domain terminology library based on the target business terms and the calculation logic corresponding to the target business terms, and determining an aggregation function mapping table and a time expression conversion rule corresponding to the target business terms, 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;

[0014] The metadata and the target business rules are converted into corresponding target vectors using the target macro model, and the target vectors are stored in a preset vector database to obtain the target metadata knowledge base.

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

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

[0017] Determining a first fuzzy entity among the initial entities based on a preset time fuzzy word definition rule, determining a second fuzzy entity among the initial entities based on a preset business indicator fuzzy word definition rule, and filtering the first fuzzy entity and the second fuzzy entity from the initial entities;

[0018] Converting the first fuzzy entity into a corresponding target time entity, and converting the second fuzzy entity into a corresponding target calculation logic;

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

[0020] Optionally, determining a target SQL element missing from the initial natural language query request based on each target entity includes:

[0021] Generate a corresponding target SQL statement template framework through a preset intent classification model, and compare the target entities based on the target SQL statement template framework;

[0022] Determine the target SQL element missing from the initial natural language query request based on the comparison result, and generate target missing element prompt information corresponding to the target SQL element;

[0023] Accordingly, the generating of 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, performing preset syntax verification on each candidate SQL statement includes:

[0026] Using the target SQL parsing library to convert each of the candidate SQL statements into a corresponding abstract syntax tree;

[0027] Traversing each node in the abstract syntax tree, checking whether there are spelling errors in the keywords corresponding to each node, checking whether the number of function nodes meets a preset quantity condition, and checking whether the identifier node meets the target query condition corresponding to the target natural language query request;

[0028] Extract the table name and the field name corresponding to the table name in 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 verify whether the field name exists in the data table corresponding to the table name.

[0029] Optionally, a preset logic check is performed on each candidate SQL statement, including:

[0030] If the candidate SQL statement meets the preset multi-table query condition, then check whether the target associated field exists in the candidate SQL statement. If not, mark the candidate SQL statement as having a Cartesian product risk.

[0031] Determine a non-aggregate field in the candidate SQL statement, and determine whether the non-aggregate field is included in a target grouping field;

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

[0033] Optionally, the large model-based NL2SQL optimization method further includes:

[0034] Acquire feedback data corresponding to the target SQL statement and abnormal data corresponding to the execution result based on the execution result corresponding to the target SQL statement;

[0035] Deduplication and validity verification are performed on the feedback data and the abnormal data to obtain correction data, and target abnormal data that meets a preset high-frequency error condition is determined in the abnormal data;

[0036] generating a second prompt word corresponding to the target abnormal data, generating a plurality of business data based on the second prompt word, and updating 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 condition and the updated training data, and the target metadata knowledge base is updated based on the feedback data.

[0038] In a second aspect, the present application provides an NL2SQL optimization device based on a large model, comprising:

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

[0040] a target SQL element determination module, configured to determine target entities corresponding to the initial natural language query request, and determine target SQL elements missing from the initial natural language query request based on the target entities;

[0041] a target natural language query request determination module, configured to generate a corresponding first prompt word based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, and to use a pre-trained target macro model to complete the target SQL element missing from the initial natural language query request based on the first prompt word to obtain a target natural language query request;

[0042] A target SQL statement determination module is used to use the target large model to generate several candidate SQL statements corresponding to the target natural language query request, perform preset syntax verification and preset logic verification on each of the candidate SQL statements, and 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.

[0043] In a third aspect, the present application provides an electronic device, comprising:

[0044] Memory, used to store computer programs;

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

[0046] In a fourth aspect, the present application provides a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, the aforementioned large model-based NL2SQL optimization method is implemented.

[0047] In this application, a target metadata knowledge base is first constructed based on data in a target database, and an initial natural language query request input by a user is obtained; then, each target entity corresponding to the initial natural language query request is determined, and based on each target entity, a target SQL element missing from the initial natural language query request is determined; then, 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 target SQL element missing from the initial natural language query request based on the first prompt word to obtain a 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 a preset syntax check and a preset logic check are performed on each candidate SQL statement, and based on the check results, a 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, in this application, the target metadata knowledge base corresponding to the target database is first constructed; then, the target SQL elements missing in the initial natural language query request are determined by identifying each target entity in the initial natural language query request; then, a first prompt word is generated based on the initial natural language query request, the target SQL element and the target metadata knowledge base, and the large model is used to complete the missing target SQL elements in the initial natural language query request according to the first prompt word to obtain the target natural language query request; then, the candidate SQL statements generated based on the target natural language query request are verified to obtain the target SQL statements. 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 reliability of SQL execution is guaranteed by a dual verification mechanism that combines 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 the overall improvement of the accuracy, adaptability and user experience of natural language and database interaction. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are merely embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.

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

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

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

[0052] Figure 4 This is a structural diagram of an electronic device provided in this application. DETAILED DESCRIPTION

[0053] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.

[0054] NL2SQL technology aims to automatically convert users' natural language queries into SQL statements, lower the technical threshold for database operations, and enable non-technical users to interact with the database conveniently. With the rapid development of LLM, its powerful text generation and semantic understanding capabilities have brought new breakthroughs to NL2SQL technology. Since natural language queries often contain ambiguous expressions, omitted sentences or domain-specific terms, LLM is prone to missing or incorrect SQL elements due to inaccurate contextual semantic parsing when dealing with complex scenarios such as multi-table associations, nested aggregate functions, and conditional logical combinations. In addition, existing methods have insufficient integration capabilities for metadata such as database table structures, field types, and associations. When generating SQL, LLM may ignore field constraints or dependencies between tables, resulting in the inability to execute the generated SQL statements correctly. To this end, the present application provides an NL2SQL optimization solution based on a large model, which can use the large model to improve the accuracy of NL2SQL.

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

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

[0057] In this embodiment, the metadata of the target database can be parsed first. Specifically, the above-mentioned construction of the target metadata knowledge base based on the data in the target database can include: first extracting the table name of each data table in the target database, the field name of each data table, the data type of each field in the data table, and the annotation of each field to obtain the metadata of the target database; then obtaining the business document corresponding to the target business scenario, and parsing the business document and the data dictionary corresponding to the metadata, so as to map each target business term in the business document to a corresponding calculation logic based on the data dictionary; then constructing a target domain terminology library based on each target business term and the calculation logic corresponding to each target business term, and determining the aggregation function mapping table and time expression conversion rule corresponding to each target business term, so as to generate the corresponding target business rule based on the target domain terminology library, the aggregation function mapping table and the time expression conversion rule; finally, using the target big model to convert the metadata and the target business rule into a corresponding target vector, and storing the target vector 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 the table names, field names, data types, and field annotations of each table in the target database can be extracted through a database interface, such as MySQL's INFORMATION_SCHEMA, or SQL queries. A standardized metadata structure is then constructed to obtain the metadata for the target database. To determine the relationships between tables, foreign keys in the target database can first be parsed and an ER diagram (Entity-Relationship Diagram) constructed to generate the corresponding inter-table association matrix R to clarify the primary-foreign key relationships connecting multiple tables. For tables without explicit foreign keys, the similarity between field names in different tables can then be determined. If the similarity is greater than or equal to a threshold, such as 0.8, then, combined with business rules such as "orders must be associated with products," a potential relationship between the two tables can be inferred, thus avoiding missing associations due to irregular database design.

[0059] It should be noted that in this embodiment, each target business term in the business document needs to be mapped to the 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` to form a "business term-calculation logic" comparison table and obtain the corresponding target domain term library as the key basis for subsequent semantic completion. In addition, a "natural language keyword-aggregation function" mapping table can be constructed, that is, an aggregate function mapping table, for example, "average" is mapped to `AVG()`, and "sum" is mapped to `SUM()`. Afterwards, the time expression conversion rules can be determined, for example, "the last N days" is converted to `field >= CURDATE() - INTERVAL N DAY`. Finally, the corresponding target business rules are generated based on the "business terminology - calculation logic" comparison table (i.e., the target domain terminology library), the "natural language keyword - aggregation function" mapping table (i.e., the aggregation function mapping table), and the time expression conversion rules. As the core of business knowledge, business rules guide LLM in generating SQL snippets that conform to business logic during semantic completion.

[0060] Finally, in this embodiment, metadata and target business rules can be converted into prompt templates described in natural language based on the relationships between data tables. Using the target macro model, these metadata and target business rules are converted into corresponding target vectors based on the prompt templates. These target vectors are then stored in the Faiss (Facebook AI Similarity Search) vector database, forming a searchable "metadata + business knowledge" knowledge base, known as the target metadata knowledge base. The target metadata knowledge base supports semantic association retrieval in subsequent queries. By integrating business rules into metadata, LLM can directly invoke these business rules when processing user queries. For example, when a user enters "Search for users with an average order value exceeding 200 yuan," the system automatically parses "average order value" into `AVG(order_amount)` and generates the condition `HAVING AVG(order_amount) > 200`. This avoids parsing errors caused by terminology ambiguity and improves semantic mapping accuracy from the source.

[0061] Step S12: Determine target entities corresponding to the initial natural language query request, and determine target SQL elements missing from the initial natural language query request based on the target entities.

[0062] In this embodiment, the above-mentioned determination of the target entities corresponding to the initial natural language query request may include: extracting the initial entities in the initial natural language query request using a preset natural language processing library and a preset business dictionary; determining the first fuzzy entity in the initial entities based on a preset time fuzzy word definition rule, and determining the second fuzzy entity in the initial entities based on a preset business indicator fuzzy word definition rule, and 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; 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 the initial entities in the initial natural language query request. Initial entities can be divided into displayed entities and implicit business entities, where displayed entities are such as: table name: "User" `users`, field name: "order amount" `order_amount`, time expression: "2023 Q4" `2023-10-01 to 2023-12-31`; Implicit business entities are implicit entities inferred by business knowledge. For example, "repeat purchase users" need to be resolved to "users with at least 2 order records", corresponding to the condition `HAVING COUNT(order_id) >= 2`. After that, the first fuzzy entity in each initial entity can be determined and converted into the corresponding target time entity. For example, "last month" calculates the specific month based on the current time, such as April 2025 March 1, 2025 - March 31, 2025. Determine the second fuzzy entity in each initial entity and convert the second fuzzy entity into the corresponding target calculation logic. For example: "high consumption user" According to the business definition, if the cumulative consumption amount is greater than 10,000 yuan, it will be converted to `total_spend > 10,000`.

[0063] It should be noted that the above-mentioned determination of the target SQL elements missing from the initial natural language query request based on the target entities may include: first, 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 target SQL elements missing from 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, a target SQL statement template framework is generated through an intent classification model, such as `SELECT [field] FROM [table] [JOIN condition] WHERE [condition] GROUP BY [grouping] HAVING [aggregation condition]`, and then, based on the target SQL statement template framework, each target entity is identified to determine whether there are missing SELECT fields, JOIN conditions, aggregate functions and other target SQL elements, and corresponding target missing element prompt information can be generated.

[0064] Step S13: Generate a corresponding first prompt word 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 target SQL element missing in the initial natural language query request based on the first prompt word to obtain a target natural language query request.

[0065] It should be noted that the target large model in this embodiment needs to be pre-trained. Specifically, multiple rounds of prompts containing business knowledge can be designed to guide LLM to generate query statements that conform to real business scenarios, that is, 10 "original question-rewritten question" query pairs involving multi-table associations can be generated based on the schema and business rules of the target database. By setting `temperature=0.8`, the diversity of generated query pairs can be improved, and the complexity of SQL statements can be controlled by `max_tokens=512` to ensure that the generated data covers core business scenarios, such as order analysis and user stratification. For the generated basic natural language query requests, prompts are used to guide LLM to generate 2-3 semantically equivalent variants to obtain corresponding generated data. In this way, the large model can learn different natural language expressions of the same business logic and improve its adaptability to diverse user inputs. The generated data can then be enhanced through business synonym replacement and business scenario parameterization. Specifically, when performing synonym replacement, business terms can be replaced using industry dictionaries, such as "user" with "customer" and "sales" with "revenue amount," ensuring the large model's generalizability to business terms. When parameterizing business scenarios, variables such as time and numbers in the generated data can be abstracted into parameters, such as "last {N} days" being abstracted into "last 30 days" and "last 90 days." This allows for the generation of multiple query pairs with the same structure but different parameters, covering common parameterized query scenarios. Then, for the generated data after data augmentation, corresponding SQL statements are generated and parsed using SQLGlot to verify compliance with the target database syntax, such as the date function format of MySQL (My Structured Query Language) and the JOIN syntax of PostgreSQL. The generated SQL statements can then be executed in a sandbox database to compare with expected business results, such as whether the calculated "average order value" is a positive number and whether the time filter correctly filters the data. This eliminates examples with logical errors. For example, if a generated SQL statement incorrectly associates tables, resulting in a Cartesian product, or uses an incorrect aggregate function, the statement will be marked as invalid. After eliminating invalid SQL statements, the dataset can be further constructed. Specifically, the data set can be divided into three layers according to business complexity, and business knowledge can be injected layer by layer:

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

[0067] (2) Advanced level (40%): SQL statements involve multi-table association + aggregation, such as "calculating the total order amount by user gender", which introduces business applications of inter-table association rules and aggregation functions.

[0068] (3) Complex layer (20%): SQL statements involve nested subqueries + window functions + business rules, such as "querying regions where the average customer spending is higher than the national average", which involves multi-hop reasoning, that is, first calculating the national average, and then filtering the combination of regions and business indicators, that is, average customer spending = total amount / order volume.

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

[0070] In this embodiment, a first prompt word, namely, Promote, is generated based on the initial natural language query request, target missing element prompt information corresponding to the target SQL element, and the target metadata knowledge base. The missing semantic information can then be dynamically filled in using text completion technology from a pre-trained target macro model to obtain the target natural language query request.

[0071] Step S14: Use the target big model to generate several candidate SQL statements corresponding to the target natural language query request, 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, the target big model can be used to generate several candidate SQL statements corresponding to the target natural language query request based on the business rules, and then the candidate SQL statements can be subjected to syntax verification and logic verification to screen the executable target SQL statements among the candidate SQL statements. The above-mentioned preset syntax verification of each candidate SQL statement can include: first, using the 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, checking whether there are spelling errors in the keywords corresponding to each node, whether the number of function nodes meets the preset quantity conditions, and whether the identifier node meets the target query conditions corresponding to the target natural language query request; then extracting the table name and the field name corresponding to the table name in the abstract syntax tree, and querying the target metadata knowledge base to verify whether the table name and the field name exist in the target metadata knowledge base, and verify whether the field name exists in the data table corresponding to the table name. Specifically, the SQL parsing library can be used to convert the candidate SQL statement into an abstract syntax tree, and each node of the abstract syntax tree includes the following types:

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

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

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

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

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

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

[0079] It should be noted that the above-mentioned preset logical verification of each candidate SQL statement may include: if the candidate SQL statement meets the preset multi-table query condition, then verifying whether the target associated field exists in the candidate SQL statement, and if not, marking the candidate SQL statement as having a Cartesian product risk; determining the non-aggregate fields in the candidate SQL statement, and determining whether the non-aggregate fields are included in the target grouping fields; in a homogeneous environment, executing the candidate SQL statement using the target sandbox database to verify whether there are runtime errors. Specifically, in this embodiment, when querying multiple tables, if the `JOIN` condition or the `WHERE` associated field is not detected, it is marked as a Cartesian product risk, such as `SELECT * FROMusers, orders` has no associated condition and is directly rejected. Afterwards, 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`. Additionally, for calculated metrics like average order value, we check whether the denominator is correct. For example, to avoid division by zero errors when the order volume is 0, we pre-process the results using `HAVING COUNT(order_id) > 0`. Finally, we can pre-execute each candidate SQL statement 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 verification and logic verification on each candidate SQL statement, it is possible to 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, each target entity corresponding to the initial natural language query request is determined, and based on each target entity, a target SQL element missing from the initial natural language query request is determined; then, 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 target SQL element missing from the initial natural language query request based on the first prompt word to obtain a 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 each candidate SQL statement is subjected to a preset syntax check and a preset logic check, and based on the check results, a 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, by identifying each target entity in the initial natural language query request, it determines the target SQL element missing from the initial natural language query request; then, based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, it generates a first prompt word, and uses the large model 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; then, it verifies the candidate SQL statement 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 that combines 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 comprehensive improvements in the accuracy, adaptability, and user experience of natural language and database interaction.

[0082] Based on the previous embodiment, it can be seen that this application can complete the missing elements in the initial natural language query request, obtain the target natural language query request, and verify the candidate SQL statements corresponding to the target natural language query request, filter out the target SQL statements, and improve the accuracy of NL2SQL. Next, this embodiment will explain in detail the optimization process of the target SQL statement and the target large model. Figure 2 As shown, the embodiment of the present invention further discloses a large model-based NL2SQL optimization method, which may include:

[0083] Step S21: Build a target metadata knowledge base based on the data in the target database, and obtain an 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 target SQL element missing in the initial natural language query request based on the target entities.

[0085] Step S23: Generate a corresponding first prompt word 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 target SQL element missing in the initial natural language query request based on the first prompt word to obtain a target natural language query request.

[0086] Step S24: Use the target big model to generate several candidate SQL statements corresponding to the target natural language query request, 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, first, based on the execution result corresponding to the target SQL statement, the feedback data corresponding to the target SQL statement and the exception data corresponding to the execution result are obtained; the feedback data and the exception data are deduplicated and the validity is verified to obtain the correction data, and the target exception data that meets the preset high-frequency error conditions in the exception data is determined; a second prompt word corresponding to the target exception data is generated, and a number of business data are generated based on the second prompt word, and the training data corresponding to the target large model is updated 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 condition 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, that is, exception data. Specifically, the feedback data is displayed as follows:

[0088] (1) Manual marking: 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 modification: When it is found that the definition of "average order value" omits "deduplication of invalid orders", the user can submit the modified business rules through the modification interface, such as `SUM(valid_order_amount) / COUNT(DISTINCT order_id)`.

[0090] Implicit feedback data is abnormal 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 querying "registered users in 2023" and there is no data, but the business expects 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 the "average order amount" is 0, which may be because the test orders are not excluded.

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

[0094] Afterward, the feedback data and outlier data can be deduplicated and validated to generate corrected data. Active learning strategies prioritize annotating high-confidence corrected data. For example, queries that trigger Cartesian product warnings three times in a row are prioritized. For target outlier data with frequent errors, such as "business-defined metrics not used in a subquery," the large model can generate over 100 similar business data examples based on the second prompt word corresponding to the target outlier data, strengthening the model's memory of the business logic. The corrected data and business data can then be used to update the training data corresponding to the target large model. The training data can then be used to fine-tune the parameters of the last 10% of the target large model's layers, avoiding disruptions to general capabilities while enhancing business-specific logic generation, such as enhancing "risk level" calculation rules in the financial sector. Finally, if user feedback indicates changes to data tables, such as the addition of a new `vip_users` table, metadata parsing of the target database is automatically triggered, updating the table structure and relationships in the target metadata knowledge base. Alternatively, if the definition of a business term is updated, such as changing "last 30 days" to "calendar month," the time expression conversion rules in the metadata are updated simultaneously, and the target metadata knowledge base is updated to ensure that subsequent SQL generation follows the new rules.

[0095] For more specific processing procedures of the above steps S21 to S23, reference may be made to the corresponding contents disclosed in the above embodiments, which will not be described again here.

[0096] As can be seen from the above, this embodiment first obtains feedback data corresponding to the target SQL statement and exception data from the execution results corresponding to the target SQL statement; then, the feedback data and exception data can be deduplicated and validity verified to obtain corresponding correction data, and the target exception data that meets the preset high-frequency error conditions can be determined; then, a number of business data corresponding to the target exception data are generated, and the correction data and business data are used to fine-tune the parameters of the target large model, and the target metadata knowledge base is updated based on the feedback data. In this way, this embodiment can convert feedback data and exception 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 business rules, achieving 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, the embodiment of the present application further provides an NL2SQL optimization device based on a large model, which may include:

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

[0099] a target SQL element determination module 12, configured to determine target entities corresponding to the initial natural language query request, and determine target SQL elements missing from the initial natural language query request based on the target entities;

[0100] a target natural language query request determination module 13 configured to generate a corresponding first prompt word based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, and to use a pre-trained target macro model to complete the target SQL element missing from the initial natural language query request based on the first prompt word to obtain a target natural language query request;

[0101] The target SQL statement determination module 14 is used to use the target large model to generate several candidate SQL statements corresponding to the target natural language query request, and perform preset syntax verification and preset logic verification on each of the candidate SQL statements, and 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, in this application, 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, each target entity corresponding to the initial natural language query request is determined, and based on each target entity, the target SQL element missing in the initial natural language query request is determined; then, 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 target SQL element missing in the initial natural language query request based on the first prompt word to obtain a 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 each candidate SQL statement is subjected to a preset syntax check and a preset logic check, and 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, in this application, the target metadata knowledge base corresponding to the target database is first constructed; then, the target SQL elements missing in the initial natural language query request are determined by identifying each target entity in the initial natural language query request; then, a first prompt word is generated based on the initial natural language query request, the target SQL element and the target metadata knowledge base, and the large model is used to complete the missing target SQL elements in the initial natural language query request according to the first prompt word to obtain the target natural language query request; then, the candidate SQL statements generated based on the target natural language query request are verified to obtain the target SQL statements. 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 reliability of SQL execution is guaranteed by a dual verification mechanism that combines 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 the overall improvement of the accuracy, adaptability and user experience of natural language and database interaction.

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

[0104] a metadata determination unit, configured to extract the table name of each data table in the target database, the field name of each data table, the data type of each field in each data table, and the comment of each field, so as to obtain metadata of the target database;

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

[0106] a target business rule generating unit, configured to construct a target domain term library based on the target business terms and the calculation logic corresponding to the target business terms, and determine an aggregation function mapping table and a time expression conversion rule corresponding to the target business terms, so as to generate corresponding target business rules based on the target domain term 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 macro model, and store the target vectors in a preset vector database to obtain the target metadata knowledge base.

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

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

[0110] a fuzzy entity determining unit, configured to determine a first fuzzy entity among the initial entities based on a preset time fuzzy word definition rule, determine a second fuzzy entity among the initial entities based on a preset business indicator fuzzy word definition rule, and filter the first fuzzy entity and the second fuzzy entity from the initial entities;

[0111] a target calculation logic determination unit, configured to convert the first fuzzy entity into a corresponding target time entity, and convert the second fuzzy entity into a corresponding target calculation logic;

[0112] A target entity determination unit is configured 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 implementations, the target SQL element determination module 12 may include:

[0114] a target SQL statement template framework generating unit, configured to generate a corresponding target SQL statement template framework by using a preset intent classification model, and compare the target entities based on the target SQL statement template framework;

[0115] a target missing element prompt information generating unit, configured to determine the target SQL element missing from 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 generating unit is configured 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] An abstract syntax tree conversion unit, configured to convert each candidate SQL statement into a corresponding abstract syntax tree using a target SQL parsing library;

[0120] a first syntax verification unit, configured to traverse each node in the abstract syntax tree, verify whether a keyword corresponding to each node has a spelling error, verify whether the number of function nodes meets a preset quantity condition, and verify whether an identifier node meets a target query condition 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 in 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 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] a target association field verification unit, configured to verify whether the target association field exists in the candidate SQL statement if the candidate SQL statement satisfies a preset multi-table query condition, and if not, mark the candidate SQL statement as having a Cartesian product risk;

[0124] A first logic verification unit is configured to determine a non-aggregate field in the candidate SQL statement and determine whether the non-aggregate field is included in a target grouping field;

[0125] The second logic verification unit is used to execute the candidate SQL statement using the target sandbox database in a homogeneous environment to verify whether there is an operation error.

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

[0127] A feedback data acquisition module, configured to acquire feedback data corresponding to the target SQL statement and abnormal data corresponding to the execution result based on the execution result corresponding to the target SQL statement;

[0128] a target abnormal data determination module, configured to perform deduplication and validity verification on the feedback data and the abnormal data to obtain correction data, and to determine target abnormal data in the abnormal data that meets a preset high-frequency error condition;

[0129] a training data updating module, configured to generate a second prompt word corresponding to the target abnormal data, generate a plurality of 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 updating module is used to fine-tune the parameters of the target large model based on the preset parameter update ratio condition and the updated training data, and to update the target metadata knowledge base based on the feedback data.

[0131] Furthermore, the embodiment of the present application also discloses an electronic device, Figure 4 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content in this diagram should not be construed as limiting the scope of use 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 is used to store a computer program, which is loaded and executed by the processor 21 to implement the relevant steps of the large model-based NL2SQL optimization method disclosed in any of the aforementioned 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 the external device. The communication protocol it follows is any communication protocol that can be applied to the technical solution of this application and is not specifically limited here; the input and output interface 25 is used to obtain external input data or output data to the outside world. 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 CD, etc. The resources stored thereon can include an operating system 221, a 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 hardware devices on the electronic device 20 and the computer program 222. The operating system 221 can be Windows Server, NetWare, Unix, Linux, etc. In addition to including computer programs capable of implementing the large model-based NL2SQL optimization method disclosed in any of the aforementioned embodiments and executed by the electronic device 20, the computer program 222 can further include computer programs capable of performing other specific tasks.

[0135] Furthermore, this application discloses a computer-readable storage medium for storing a computer program. When executed by a processor, the computer program implements the aforementioned large-model-based NL2SQL optimization method. The specific steps of this method can be found in the corresponding content disclosed in the aforementioned embodiments and will not be further elaborated here.

[0136] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from the other embodiments. Reference can be made to the descriptions of the identical or similar parts between the various embodiments. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple, and the relevant parts can be referred to the descriptions of the methods.

[0137] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may 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 may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in random access memory (RAM), internal memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, a hard disk, a removable disk, a 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, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of additional identical elements in the process, method, article, or device comprising the element.

[0140] The above is a detailed introduction to the technical solution provided by the present application. Specific examples are used herein to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea. At the same time, for those skilled in the art, according to the ideas of the present application, there may be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.

Claims

1. A NL2SQL optimization method based on a large model, characterized in that: include: Build 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; Determine each target entity corresponding to the initial natural language query request, and determine a target SQL element missing from the initial natural language query request based on the target entities; generating a corresponding first prompt word based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, and using a pre-trained target large model to complete the target SQL element missing from the initial natural language query request based on the first prompt word to obtain a target natural language query request; The target big 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 a preset syntax check and a preset logic check, and a target SQL statement corresponding to the target natural language query request is determined from each candidate SQL statement based on the check results.

2. The large model-based NL2SQL optimization method according to claim 1, characterized in that: The step of constructing a target metadata knowledge base based on the data in the target database includes: Extracting the table name of each data table in the target database, the field name of each data table, the data type of each field in each data table, and the comment of each field to obtain metadata of the target database; Acquire a business document corresponding to a target business scenario, and parse a data dictionary corresponding to the business document and the metadata to map each target business term in the business document to a corresponding calculation logic based on the data dictionary; Building a target domain terminology library based on the target business terms and the calculation logic corresponding to the target business terms, and determining an aggregation function mapping table and a time expression conversion rule corresponding to the target business terms, 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 metadata and the target business rules are converted into corresponding target vectors using the target macro model, and the target vectors are stored in a preset vector database to obtain the target metadata knowledge base.

3. The NL2SQL optimization method based on a large model according to claim 1, characterized in that: The determining target entities corresponding to the initial natural language query request includes: Extracting each initial entity in 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 a preset time fuzzy word definition rule, determining a second fuzzy entity among the initial entities based on a preset business indicator fuzzy word definition rule, and 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; The target entities corresponding to the initial natural language query request are determined based on the target time entity, the target calculation logic, and the filtered initial entities.

4. The large model-based NL2SQL optimization method according to claim 1, characterized in that: The determining, based on the target entities, target SQL elements missing from the initial natural language query request includes: Generate a corresponding target SQL statement template framework through a preset intent classification model, and compare the target entities based on the target SQL statement template framework; Determine the target SQL element missing from the initial natural language query request based on the comparison result, and generate target missing element prompt information corresponding to the target SQL element; Accordingly, the generating of 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.

5. The NL2SQL optimization method based on a large model according to claim 1, characterized in that: The performing preset syntax check on each candidate SQL statement includes: Using the target SQL parsing library to convert each of the candidate SQL statements into a corresponding abstract syntax tree; Traversing each node in the abstract syntax tree, checking whether there are spelling errors in the keywords corresponding to each node, checking whether the number of function nodes meets a preset quantity condition, and checking whether the identifier node meets the target query condition corresponding to the target natural language query request; Extract the table name and the field name corresponding to the table name in 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 verify whether the field name exists in the data table corresponding to the table name.

6. The NL2SQL optimization method based on a large model according to claim 1, characterized in that: Performing preset logic verification on each candidate SQL statement includes: If the candidate SQL statement meets the preset multi-table query condition, then check whether the target associated field exists in the candidate SQL statement. If not, mark the candidate SQL statement as having a Cartesian product risk. Determine a non-aggregate field in the candidate SQL statement, and determine whether the non-aggregate field is included in a target grouping field; In a homogeneous environment, the candidate SQL statement is executed using the target sandbox database to verify whether there are any runtime errors.

7. The large model-based NL2SQL optimization method according to any one of claims 1 to 6, characterized in that: Also includes: Acquire feedback data corresponding to the target SQL statement and abnormal data corresponding to the execution result based on the execution result corresponding to the target SQL statement; Deduplication and validity verification are performed on the feedback data and the abnormal data to obtain correction data, and target abnormal data that meets a preset high-frequency error condition is determined in the abnormal data; generating a second prompt word corresponding to the target abnormal data, generating a plurality of business data based on the second prompt word, and updating 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 condition and the updated training data, and the target metadata knowledge base is updated based on the feedback data.

8. A NL2SQL optimization device based on a large model, characterized in that: include: A target metadata knowledge base construction module is used to construct a target metadata knowledge base based on the data in the target database and obtain an initial natural language query request input by the user; a target SQL element determination module, configured to determine target entities corresponding to the initial natural language query request, and determine target SQL elements missing from the initial natural language query request based on the target entities; a target natural language query request determination module, configured to generate a corresponding first prompt word based on the initial natural language query request, the target SQL element, and the target metadata knowledge base, and to use a pre-trained target macro model to complete the target SQL element missing from the initial natural language query request based on the first prompt word to obtain a target natural language query request; A target SQL statement determination module is used to use the target large model to generate several candidate SQL statements corresponding to the target natural language query request, perform preset syntax verification and preset logic verification on each of the candidate SQL statements, and 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.

9. 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, and the computer program is loaded and executed by the processor to implement the large model-based NL2SQL optimization method according to any one of claims 1 to 7.

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

Citation Information

Patent Citations

  • Multi-round dialogue voice interaction method and system, storage medium and electronic equipment

    CN111680144A

  • NL2SQL method based on LLM multi-table adaptive selection

    CN117112732A

  • Unified sql processing method and device for heterogeneous relational database

    CN117971862A

  • Natural language processing integrated with database and data storage management

    US20180329993A1

Cited By

  • Text-to-SQL (Structured Query Language) generation method and equipment, medium and product

    CN120910088A

  • NL2SQL generation method based on large language model

    CN120910089A

  • Natural language query method and system for rail transit field

    CN120994693A

  • A natural language query method and system for rail transit field

    CN120994693B

  • Multi-source data analysis method and device, equipment and storage medium

    CN121029796A