A method and system for generating SQL intelligence for a service query
By constructing a query intent knowledge graph and semantic variant extensions of a large language model, combined with CoT chain-based reasoning guidance and intent completion modules, the accuracy problem of NL2SQL in complex business logic scenarios in existing technologies is solved, and high-precision SQL generation is achieved.
Patent Information
- Application Number
- CN202511387974.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-26
- Publication Date
- 2026-01-27
- Estimated Expiration
- 2045-09-26
AI Technical Summary
Existing NL2SQL methods struggle to effectively capture business domain-specific synonyms and complex logical relationships when dealing with complex business logic, specific terminology, and user queries that are often ambiguous or omitted, leading to the generation of incorrect SQL statements.
By constructing a query intent knowledge graph, extracting natural language templates and SQL templates containing placeholder variables, using a large language model for semantic variant expansion, and combining CoT chain-like reasoning guidance, generating structured query statements, and setting an intent completion module in the large language model for semantic completion and entity recognition.
It significantly improves the accuracy of understanding diverse business query intents and the precision of SQL generation, reduces the probability of errors caused by incomplete or ambiguous input, and ensures that the generated SQL statements meet business requirements.
Smart Images

Figure CN120873005B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application relates to an SQL intelligent generation method and system for business query, and belongs to the technical field of artificial intelligence. BACKGROUND
[0002] Business personnel often need to query specific information from complex data systems to support decision analysis. The traditional SQL query method requires users to have professional database knowledge and business table structure understanding, and has a high threshold. In recent years, the technology of automatically converting natural language queries (NLQ) into SQL statements (NL2SQL) using large language models (LLM) has developed rapidly, aiming to reduce the use threshold and improve query efficiency. Some existing NL2SQL methods, such as the Chinese invention patent application with publication number CN118939681A, disclose a SQL statement generation method, which matches historical query question pairs and vectorized table structure information to construct Prompt input LLM, and has certain effect in processing general or clear structure queries.
[0003] However, in the scene where business logic is complex, terms are specific, and user queries often have ambiguities or omissions, the similarity matching mechanism of the above-mentioned patent mainly relies on literal relevance, and it is difficult to effectively capture business domain-specific business intent synonyms and complex logic associations. When users use unconventional business terms or omit context, the retrieved historical question pairs may have low semantic relevance, resulting in a decrease in the reference value of the Prompt information. Moreover, the above-mentioned patent solution completely relies on the completeness of user input and the accuracy of search results. When there is a missing key parameter or ambiguous expression in user input, the mechanism cannot actively complete or disambiguate based on business logic, and can only rely on LLM to guess on its own with incomplete Prompt, which is prone to generate incorrect SQL. The Prompt of the above-mentioned patent is essentially a mechanical splicing of the retrieved historical question text and table structure description. This static splicing lacks structured step guidance for the LLM SQL generation process, and does not provide diversified example variants for business scenarios, making it difficult to constrain the reasoning path of LLM in complex multi-table association or condition nesting, resulting in a generated result deviating from the business expectation.
[0004] In summary, there is an urgent need for an SQL generation method that can overcome the limitations of text matching, actively analyze ambiguous semantics, and provide strong reasoning guidance. SUMMARY
[0005] In order to solve the problems existing in the prior art, the application provides an SQL intelligent generation method and system for business query.
[0006] The technical solution of the application is as follows:
[0007] In one aspect, the present application provides a SQL intelligent generation method for business query, comprising:
[0008] Obtaining query statement related data in a business system, including historical query statements, historical SQL statements and business requirements, constructing a query intent knowledge graph based on the query statement related data using semantic clustering;
[0009] Analyzing the structure of the historical SQL statements based on the query intent knowledge graph, extracting natural language templates containing placeholder variables and paired SQL templates; generating a few-shot example set by combining the SQL templates after expanding the natural language templates using a large language model;
[0010] Based on task setting guidance, examples in the few-shot example set and CoT chain thinking reasoning guidance, constructing a composite prompt word template;
[0011] Setting an intent completion module in the large language model, which is used to combine the user input natural language query statement with the context of the composite prompt word template to generate a structured query statement;
[0012] Based on the structured query statement, using the query intent knowledge graph and the composite prompt word template to generate the corresponding standard SQL statement.
[0013] Preferably, the construction of the query intent knowledge graph using semantic clustering is as follows:
[0014] Using a BERT model to perform semantic encoding on the historical query statements to generate a semantic vector set, performing division operation on the semantic vector set using a clustering algorithm to generate an intent cluster set; combining the business requirements and a predefined data table and field set, analyzing the query statements in the intent cluster set, and establishing a query intent knowledge graph including the mapping relationship between the intent cluster and the data table and field.
[0015] Preferably, the extraction of the natural language template containing the placeholder variable and the paired SQL template is as follows:
[0016] Analyzing the data table set and field set mapped by the intent cluster in the query intent knowledge graph; traversing the historical SQL statement set, and screening out historical SQL statements containing any data table in the data table set and at least two fields in the field set; performing syntax tree analysis on the screened historical SQL statements, extracting common structures, replacing table names and field names in the common structures with placeholder variables, and constructing SQL templates; based on the mapping relationship, reversely mapping the placeholder variables to natural language elements to generate paired natural language templates.
[0017] Preferably, the few-shot example set is generated by expanding the natural language template through semantic variants combined with the SQL template by a large language model, specifically as follows:
[0018] The word embedding layer of the large language model is called to calculate the word vector of the non-variable words in the natural language template, and the K-nearest neighbor synonyms are retrieved in the word vector space and replaced with the original words to generate word vector variants;
[0019] The syntactic analyzer of the large language model is called to generate sentence transformation variants, including adjusting the subject-predicate-object structure to passive voice, converting declarative sentences to interrogative sentences, and adding or deleting modifying adverbials;
[0020] According to the mapping relationship of the query intent knowledge graph, the placeholder variables in the word vector variants and the sentence transformation variants are replaced with specific field names to generate natural language variants and SQL variants;
[0021] The natural language variants and SQL variants are combined to form paired examples and stored in the few-shot example set.
[0022] Preferably, based on the task setting guide, the examples in the few-shot example set, and the CoT chain-of-thought reasoning guide, a composite prompt word template is constructed, specifically as follows:
[0023] A task setting guide sentence is added at the starting position of the composite prompt word template, which is used to clearly indicate the role and specific task of the large language model in the business data query scenario;
[0024] After the task setting guide sentence, a representative question-answer example selected from the few-shot example set is embedded, which covers different business scenarios and query types in the business system;
[0025] A CoT chain-of-thought reasoning guide sentence is added after the representative question-answer example, which is used to guide the large language model to step-by-step decompose the query intent of the user's natural language query sentence and perform reasoning, and finally output the SQL statement, the step-by-step including the data table involved in the query, the filtering field and condition, the aggregation method and output field, and the generation of the SQL statement.
[0026] Preferably, the intent completion module combines the user's input natural language query sentence with the context of the composite prompt word template to generate a structured query statement, specifically as follows:
[0027] The system uses a BiLSTM-CRF model to locate and identify key entities in user-input natural language queries, obtaining entity recognition results. Combining the entity recognition results with the context of the compound prompt word template and business logic, the system analyzes the semantics of the user-input natural language query and dynamically supplements missing key information. If, after semantic completion, the user-input natural language query still has missing required parameters, it is filled in according to preset rules to obtain a structured query.
[0028] Preferably, when generating structured query statements using the intent completion module, it also includes:
[0029] For natural language queries with ambiguous user input, a pre-trained fuzzy intent example mechanism is used to automatically infer the intent field, grouping dimension, and sorting conditions corresponding to the ambiguous user input natural language query based on the output of entity recognition and semantic completion, and generate the corresponding structured query.
[0030] On the other hand, this invention provides an intelligent SQL generation system for business queries. The system includes a query data collection and knowledge graph construction module, a template extraction and example set generation module, a compound prompt word template construction module, a structured query statement generation module, and a standard SQL statement generation module, wherein:
[0031] The query data collection and knowledge graph construction module is used to obtain query statement related data from the business system, including historical query statements, historical SQL statements and business requirements. Based on the query statement related data, a query intent knowledge graph is constructed using semantic clustering.
[0032] The template extraction and example set generation module is used to parse the historical SQL statement structure based on the query intent knowledge graph, extract natural language templates containing placeholder variables and paired SQL templates; after semantically expanding the natural language templates through a large language model, it combines the SQL templates to generate a few-shot example set.
[0033] The composite prompt word template construction module is used to construct composite prompt word templates based on task setting guidance, examples in the few-shot example set, and CoT chain-like reasoning guidance.
[0034] The structured query statement generation module is used to set up an intent completion module in the large language model. The intent completion module is used to combine the natural language query statement input by the user with the context of the compound prompt word template to generate a structured query statement.
[0035] The standard SQL statement generation module is used to generate corresponding standard SQL statements based on structured query statements, using query intent knowledge graphs and compound prompt word templates.
[0036] In another aspect, the present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement an SQL intelligent generation method for business queries as described in the present invention.
[0037] In another aspect, the present invention also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements an SQL intelligent generation method for business queries as described in the present invention.
[0038] The present invention has the following beneficial effects:
[0039] 1. This invention is a method and system for intelligent SQL generation for business queries. It utilizes the BERT model to perform deep semantic encoding and clustering of historical query statements to form intent clusters. Combined with business requirements and predefined data patterns, it establishes a structured mapping relationship graph between intent clusters and data tables and fields. This allows for a deep understanding and accurate modeling of business semantics. This design breaks through the superficial limitations of traditional text similarity matching and can effectively capture the synonymy of business terms and the essential relationship between complex query intents. It provides a solid business semantic foundation and data association basis for subsequent SQL generation, significantly improving the accuracy of understanding diverse business query intents.
[0040] 2. This invention is a method and system for intelligent SQL generation for business queries. It uses a BiLSTM-CRF model for entity recognition, combines the context of compound prompt word templates and business logic, dynamically analyzes the semantics of natural language queries input by users, actively identifies and supplements missing key information, and infers ambiguous intentions, reducing the probability of SQL generation errors caused by incomplete or ambiguous input, making the system closer to the natural and non-standard query habits of business personnel.
[0041] 3. This invention provides a method and system for intelligent SQL generation for business queries. By constructing few-shot examples and a composite Prompt template guided by CoT inference, it provides strongly structured inference guidance, ensuring the accuracy and business compliance of the generated SQL. The few-shot examples can more reliably handle complex multi-table join queries, nested condition judgments, and unique business calculation logic, significantly improving the syntactic correctness, semantic accuracy, and suitability of the generated SQL statements for business requirements. Attached Figure Description
[0042] Figure 1 This is a flowchart of the method of the present invention. Detailed Implementation
[0043] 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.
[0044] It should be understood that the step numbers used in the text are for ease of description only and are not intended to limit the order in which the steps are performed.
[0045] It should be understood that the terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to limit the invention. As used in this specification and the appended claims, the singular forms “a,” “an,” and “the” are intended to include the plural forms unless the context clearly indicates otherwise.
[0046] The terms “comprising” and “including” indicate the presence of the described feature, whole, step, operation, element and / or component, but do not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or collections thereof.
[0047] The term “and / or” refers to any combination of one or more of the associated listed items, as well as all possible combinations, and includes these combinations.
[0048] Example 1:
[0049] This embodiment provides a method for intelligently generating SQL for business queries. To better understand the method described in this embodiment, electricity marketing business is used as an example for illustration. The specific steps of the method are as follows:
[0050] S1. Obtain historical query statements, historical SQL statements, and business requirements from the power marketing system; perform semantic encoding on the historical query statements to obtain a set of semantic vectors; cluster the set of semantic vectors to divide the query intents corresponding to the historical query statements into intent clusters; predefine data tables and fields, and establish a query intent knowledge graph that includes the mapping relationship between intent clusters and data tables and fields.
[0051] S11. Collect historical query statements and historical SQL statements from the electricity marketing system. The historical query statements come from different user interaction scenarios, including typical business scenarios such as user electricity consumption trend analysis, time-of-use electricity price structure analysis, and bill verification. They are presented in natural language and represent users' data query needs expressed in everyday language. For example, in the electricity marketing system, users can enter a statement such as "query the electricity consumption of residential users in a certain region last month" to express the data information they want to obtain.
[0052] Historical query statements are expressed as formulas ,in, This is the first historical query statement. This is the second historical query statement, and so on. For the first 10 historical query statements For the first 10 historical query statements This represents the total number of historical query statements. Additionally, this embodiment records the relevant contextual information for each historical query statement, such as query time, query user, and query results, to enrich the data dimensions.
[0053] By engaging in in-depth communication with the electricity marketing department, we collected business requirements, categorized and organized these requirements, and formed a clear list of business requirements, providing business guidance for subsequent intent identification and clustering.
[0054] S12. In this embodiment, a BERT model based on the Transformer architecture is used to semantically encode historical query statements. The BERT model has powerful contextual understanding capabilities and can capture semantic information in natural language statements. Preferably, in this embodiment, the BERT model achieves contextual semantic capture through a stack of 12 encoder layers. The BERT model outputs a 768-dimensional semantic vector. In this embodiment, text from the power industry is used for incremental pre-training of the BERT model. The loss function is jointly optimized using the Masked Language Modeling (MLM) task and the Next Sentence Prediction (NSP) task. The Masked Language Modeling task involves randomly selecting some words in the input text during training and replacing them with special mask markers (e.g., [MASK]), and then letting the BERT model... The model predicts these masked words, and the corresponding loss function is to calculate the difference between the probability distribution of the masked words predicted by the model and the real words. In this embodiment, the cross-entropy loss function is used to measure this difference. The goal of the model is to minimize this loss, thereby improving the accuracy of the prediction. The next sentence prediction task is that between any two query statements, the model needs to determine whether the first query statement is the real next sentence of the second query statement. In the training data, in half the cases, the second query statement is the real next sentence of the first query statement, and in the other half the cases, the second query statement is a randomly selected sentence. In this embodiment, the cross-entropy loss function is also used to calculate the difference between the sentence relationship predicted by the model (whether it is the real next sentence or a random sentence) and the real label. The goal of the model is to minimize this loss and improve the accuracy of the judgment.
[0055] As a preferred embodiment of this invention, the preprocessing of the collected historical query statements includes removing special characters and stop words, performing lexical analysis and part-of-speech tagging, etc., to improve the accuracy of the encoding. Since this preprocessing operation is a conventional operation in the field, it will not be described in detail here.
[0056] The preprocessed historical query statements are input into the BERT model for semantic encoding, expressed as a formula:
[0057] ;
[0058] In the formula, For the first The semantic vector of each historical query statement; This represents the BERT encoding function in the BERT model;
[0059] The semantic vector set is obtained through the above encoding. ;
[0060] S13. In this embodiment, the K-Means algorithm is used to cluster the semantic vector set, and the semantic vector set is... Divided into intention cluster Each intent cluster corresponds to a type of electricity marketing data query intent, such that the sum of the squared distances from each semantic vector to the centroid of its cluster is minimized, expressed by the formula:
[0061] ;
[0062] In the formula, For the first intention cluster The centroid is the intention cluster. The mean of all semantic vectors in the dataset;
[0063] The centroids and cluster partitions are updated by iteratively optimizing the intent function (i.e., the sum of squared distances from each semantic vector to the centroid of its cluster) until the stopping condition is met (such as the centroid no longer changing or the preset maximum number of iterations is reached), thus obtaining the final intent cluster.
[0064] S14. In this embodiment, the predefined set of data tables is: The field set is ,in, The total number of data tables, This represents the total number of fields. For each intent cluster, by analyzing the query statements and business requirements, a mapping relationship is established between the intent cluster and the data table and fields. This provides a structural reference for the organization of subsequent few-shot examples and the embedding of prompt word templates, expressed as a formula:
[0065] ;
[0066] In the formula, For the first intention cluster With the Data Tables , No. fields The mapping relationship between them.
[0067] S2. For each intent cluster and its intent type, based on the mapping relationship between the intent cluster and the data table and fields, construct standardized natural language and SQL statement pairing templates to form a few-shot example set. Specifically, the construction process includes template extraction and variant expansion:
[0068] S21. The template extraction process involves parsing the structure of historical SQL statements, combining it with the mapping relationship established in step S1, identifying common field combinations and query logic in the query, and establishing a standard template in the form of placeholder variables. Specifically:
[0069] In step S1, historical query statements are obtained along with historical SQL statements. Historical SQL statements are codes written in accordance with the syntax rules of Structured Query Language (SQL) and are used to interact with the database to obtain, modify, or manage data. Corresponding to the natural language query requirement exemplified in step S1, the SQL statement may be "SELECT SUM(Electricity Consumption) FROM User Electricity Meter WHERE Region='City' AND User Type='Resident' AND Billing Month='Last Month';". SQL statements have strict syntax structure and keyword requirements.
[0070] Based on the set of data tables and fields mapped to intent clusters in the query intent knowledge graph (such as the data table "Electricity Statistics Table" and fields "Electricity", "Billing Month", "City", "Customer Type", etc. mapped to the intent cluster "User Electricity Statistics"), the historical SQL statement set is traversed, and historical SQL statements that contain any data table in the data table set (such as "Electricity Statistics Table") and at least two fields in the field set are analyzed.
[0071] The selected historical SQL statements are parsed using a syntax tree, and common structures (such as recurring keywords, table names, field names, and query conditions) are extracted as analysis results.
[0072] Based on the analysis results, natural language templates and SQL templates were created. The natural language template uses natural language to describe the query requirements and uses placeholder variables to represent the variable parts. The SQL template uses SQL syntax to construct query statements, also using placeholder variables to represent the variable parts. For example, combining the mapping relationship corresponding to intent clusters and the analysis results, we obtained the natural language template "Query the sum of {electricity / electricity cost} for {month}{region}{user type}" and the SQL template "SELECT SUM({field}) FROM {data table}".
[0073] The variable set is defined as {month, region, user type, electricity / electricity cost, field, data table}, where "field" and "data table" can be accurately replaced with specific values according to the mapping relationship. This process replaces table names and field names in common structures (such as specific field combinations in the WHERE condition) with placeholder variables to construct an SQL template; and based on the mapping relationship established in step S1, the placeholder variables (such as {region}{user type}) are back-mapped to the corresponding natural language elements (such as "region" and "user type"), thereby generating paired natural language templates.
[0074] In steps S22 and S21, the templates extracted yield standard query structures based on historical SQL statements. These structures have a certain degree of universality, but may have limitations when faced with diverse natural language queries. To better handle query requirements expressed in various natural language forms, this embodiment introduces a large language model. This large language model uses natural language templates as its foundation and leverages its powerful semantic understanding capabilities to analyze the semantic information of each part of the template. For example, for the natural language template "Query the sum of {electricity consumption / electricity cost} for {month}{region}{user type}", the large language model can identify the semantic meanings represented by "month", "region", "user type", and "electricity consumption / electricity cost", understanding that "month" represents the time dimension, "region" represents the geographical range, "user type" is used to distinguish different user groups, and "electricity consumption / electricity cost" is the specific query object.
[0075] Based on this understanding of semantic meanings, to enhance the generalization ability of large language models, semantic variant expansion is performed, including the automatic generation of diverse semantic expressions using word vector substitution and sentence transformation. Specifically:
[0076] During word vector replacement, the word embedding layer of the large language model is invoked to calculate word vectors for non-variable words (such as "query," "month," and "region") in the natural language template. K-nearest neighbor synonyms are retrieved from the word vector space and used to replace the original words, generating word vector variants. The large language model searches its lexicon for words semantically similar to those in the natural language template and replaces them. For example, "query" is replaced with "find," "get," or "search"; "month" is replaced with "month" or "date range"; "region" is replaced with "area," "region," or "location"; "user type" is replaced with "customer type" or "electricity user group"; and "electricity consumption / electricity cost" is replaced with "electricity consumption / electricity cost amount," etc. Through these word vector replacements, new natural language expressions are generated, such as "find the total {electricity consumption / electricity cost amount} of {month}{region}{electricity user group}" and "get the total {electricity consumption / electricity cost} of {date range}{location}{customer type}."
[0077] During sentence transformation, the syntactic analyzer of the large language model is invoked to generate sentence transformation variants, including adjusting subject-verb-object structures to passive voice, converting declarative sentences into interrogative sentences, and adding or deleting modifiers. The large language model generates different forms of expressions with similar semantics by adjusting sentence structure and word order. For example, "Query the total {electricity / electricity cost} for {month}{region}{user type}" is transformed into "What is the total {electricity / electricity cost} for {month}{region}{user type}" and "Total {electricity / electricity cost}, query {month}{region}{user type}", etc.
[0078] Based on the mapping relationship of the query intent knowledge graph, the placeholder variables in the generated word vector variants and sentence transformation variants are replaced with specific field names to ensure semantic consistency. For each generated natural language variant (word variable and sentence), the large language model accurately converts the natural language variant into the corresponding SQL variant according to the mapping relationship between the intent cluster and the data table and fields in step S1. For example, for the natural language variant "find the sum of {electricity consumption / electricity cost} of {month}{region}{electricity user group}", the corresponding SQL variant is "SELECT SUM({field}) FROM {data table}WHERE region='{region}' AND electricity user group='{electricity user group}' AND month='{month}';".
[0079] Through the semantic variant expansion described above, this embodiment generates a large number of semantically similar but differently expressed natural language-SQL statement pairing examples. These natural language variants are combined with SQL variants to form pairing examples and stored in the few-shot example set. The few-shot example set covers various natural language expressions and corresponding SQL statements, enabling the subsequent model based on few-shot learning to better understand and process various query requirements when handling user queries. This improves the model's adaptability and processing ability to diverse natural language queries, enhances the model's generalization ability, and thus more accurately converts user natural language queries into appropriate SQL statements for database queries. The few-shot example set can be expressed by the following formula:
[0080] ;
[0081] In the formula, For few-shot example sets; For the first A natural language statement; For the first One SQL statement; For the total number of examples, a natural language statement and its corresponding SQL statement are considered as one example.
[0082] S3. After completing the intent clustering of historical query statements, establishing the mapping relationship between intent and data tables and fields, and constructing the few-shot example set, in order to more effectively guide the large language model to accurately convert the user's natural language query statements into standard SQL queries, a composite prompt word template consisting of three parts—task setting guidance, few-shot examples, and inference guidance—is constructed. Specifically:
[0083] The task setting guidance statement is clearly set at the beginning of the compound prompt word template, such as "You are an electricity data analysis assistant. Please convert the natural language query statement into a standard SQL query according to the example". Through the task setting guidance statement, the large language model can quickly understand its role and responsibility in the current electricity marketing data query scenario, that is, focus on accurately converting the natural language query statement entered by the user into a standard SQL query that conforms to the operation specifications of the electricity marketing database according to the few-shot examples provided later.
[0084] Representative question-and-answer examples are selected from the few-shot example set constructed in step S2 and embedded into the compound prompt words. These question-and-answer examples cover various common business scenarios and query types in the power marketing system, helping the large language model learn the correspondence between different natural language expressions and SQL queries. For ease of understanding, the following examples illustrate the query types:
[0085] Example 1:
[0086] Natural language query statement: Query the total battery consumption for {month}{region}{user type};
[0087] SQL statement: SELECT SUM({Electricity field}) FROM {User Electricity Meter} WHERE Region = '{Region}' AND User Type = '{User Type}' AND Billing Month = '{Month}';
[0088] Example 1 demonstrates a simple single-table query involving statistical analysis of a specific electricity consumption metric based on a given time, region, and user type. The large language model learns from this how to determine filter conditions and aggregate functions in the SQL query based on key information such as time, location, and user type from the natural language. Simultaneously, the large language model understands the correspondence between placeholder variables in natural language and SQL statements, enabling accurate substitution when facing specific problems.
[0089] Example 2:
[0090] Natural language query statement: Find the average electricity consumption of {date range}{region}{electricity user group};
[0091] SQL statement: SELECT AVG({Electricity Consumption Field}) FROM {Electricity Statistics Table} WHERE Region = '{Region}' AND Electricity Consumption Group = '{Electricity Consumption Group}' AND Billing Date BETWEEN '{Date Range Start}' AND '{Date Range End}';
[0092] Example 2 not only involves time range queries (date ranges) but also uses average aggregation functions. The large language model learns from this how to handle time interval expressions in natural language and correctly use the BETWEEN keyword to filter data in SQL. Simultaneously, the large language model deepens its understanding of placeholder variable replacement rules in different scenarios.
[0093] Example 3:
[0094] Natural language query statement: Query the maximum {electricity cost} for each {district / county} and {customer type} within {city};
[0095] SQL statement: SELECT {district / county field}, MAX({electricity fee field}) FROM {electricity fee table} WHERE city = '{city}' AND customer type = '{customer type}' GROUPBY {district / county field};
[0096] Example 3 introduces the concept of grouped queries. The large language model learns from this how to perform grouped statistics in SQL using the GROUPBY keyword based on the grouping requirements (districts and counties) in natural language, and further becomes familiar with the application of placeholder variables in grouped queries, as well as how to accurately replace variables based on the mapping relationship between intent clusters and data tables and fields.
[0097] Based on the few-shot example above, this embodiment further introduces CoT chain-like reasoning guidance statements into the compound prompt word template. This guides the large language model to decompose the user's natural language query statement step by step, deconstructing the query intent and providing reasoning guidance, and outputting the SQL result. For example, the CoT chain-like reasoning guidance statement is: "Please analyze in sequence: (1) the data tables involved in the query; (2) the filtering fields and conditions; (3) the aggregation method and output fields; (4) generate the SQL statement." For ease of understanding, the reasoning guidance process is further illustrated with examples:
[0098] For complex multi-table join scenarios:
[0099] User query intent: To query the electricity bill details for {month} {region} {user type}, and also display the user's basic information (such as name and contact information);
[0100] The reasoning-guided analysis process of the large language model is as follows:
[0101] The data tables involved in the query are determined by combining the learning of the mapping relationship between different query intent types and data tables in the few-shot example, as well as the mapping relationship between intent clusters and data tables and fields established in step S1. In the query intent of "querying the electricity bill details of {month} {region} {user type} and displaying the user's basic information (such as name and contact information)", based on the knowledge and mapping relationship learned from the few-shot example, it is determined that the "electricity bill table" and "user information table" are involved.
[0102] Filtering fields and conditions: Based on the user's query intent and the pattern of the few-shot example, determine the filtering fields and conditions. That is, for "querying the electricity bill details for {month} {region} {user type}, and displaying the user's basic information", according to the rules learned from the few-shot example, the filtering fields include the electricity bill details fields in the "Electricity Bill Table" (such as electricity amount, billing items, etc.) and the user basic information fields in the "User Information Table" (such as name, contact information); the conditions are region = '{region}', user type = '{user type}', and billing month = '{month}'.
[0103] Aggregation methods and output fields: The few-shot example demonstrates the use cases and rules of different aggregation functions (such as SUM, AVG, MAX, etc.). In this step of inference guidance, the large language model determines whether an aggregation operation is needed and determines the output fields based on the query intent and the knowledge learned from the few-shot example. In this scenario, no aggregation operation is needed, and the output fields are the required electricity bill details and user basic information.
[0104] Generate SQL statements:
[0105] SELECT User Information Table.Name, User Information Table.Contact Information, Electricity Bill.Electricity Bill Amount, Electricity Bill.Billing Items
[0106] FROM electricity meter
[0107] JOIN User Information Table ON Meter.UserID = User Information Table.UserID
[0108] WHERE meter.region = '{region}' AND meter.user type = '{user type}' AND meter.billing month = '{month}';
[0109] For nested query scenarios:
[0110] User query intent: To query user information whose electricity consumption is higher than the annual average electricity consumption of {year};
[0111] The reasoning-guided analysis process of the large language model is as follows:
[0112] The data tables involved in the query are the same as those analyzed above, namely the "Power Meter" and the "User Information Table".
[0113] Filtering fields and conditions: The filtering fields are user information from the user information table (such as user name, address, etc.); the condition is that the user's electricity consumption is higher than the annual average electricity consumption of {year}, which needs to be calculated first;
[0114] Aggregation method and output field: The large language model learns the rules of grouped queries and using AVG functions from the few-shot examples, so as to correctly determine the aggregation method and output field in the inference guidance. The aggregation method is to calculate the average electricity consumption, and the output field is user information.
[0115] Generate SQL statements:
[0116] SELECT User Information Table.User Name, User Information Table.Address
[0117] FROM User Information Form
[0118] JOIN Battery Meter ON User Information Table.UserID = Battery Meter.UserID
[0119] WHERE Electricity Meter.Electricity Consumption>(SELECT AVG(Electricity Consumption) FROM Electricity Meter WHERE Billing Year='{Year}');”;
[0120] By using the aforementioned compound prompt word templates, the large language model is enabled to have logical reasoning capabilities when handling complex logic such as multi-table joins, thereby significantly improving structural accuracy and business usability.
[0121] S4. Generating SQL statements solely based on user-input natural language may result in incomplete information and ambiguous expressions. To enable the large language model to better handle these complexities, this embodiment incorporates an intent completion module. This module combines the user-input natural language query with the context of the compound prompt word template, performing comprehensive and detailed preprocessing to generate a clearly structured query with a specific intent. Specifically, the intent completion module performs entity recognition, semantic completion, and default parameter filling operations to address missing information.
[0122] Entity recognition based on BiLSTM-CRF includes the accurate location and recognition of key entities in natural language queries, such as identifying key entities in the field of electricity marketing, such as "electricity consumption", "user type" and "month".
[0123] The semantic completion includes analyzing the semantics of natural language query statements, supplementing missing key information, and making the semantics of the question more complete. For example, when a natural language query statement mentions "querying electricity consumption" but does not specify which time period the electricity consumption is, the semantic completion mechanism combines the context of the compound prompt word template and business logic to make reasonable inferences and supplements.
[0124] The default parameter filling is used to fill parameters according to preset rules when information is missing. For example, when the "month" is not specified in the natural language query statement, it is filled with "current month" by default; when the "user type" is not specified, it is filled with "all customers" first.
[0125] Besides addressing the issue of missing information, user-inputted natural language queries may also be ambiguous, such as "Which type of user has the highest battery level? Check the region with the fastest battery growth? See which city has slow payment collection?" To address natural language queries with ambiguous intents, this embodiment introduces an ambiguous intent example training mechanism, specifically:
[0126] A large number of fuzzy natural language queries were collected, and a corresponding standard answer was assigned to each query. The intent completion module was trained by comparing the queries with the preset standard answers. During training, the intent completion module identified the intent field, grouping dimension, and sorting condition in the query, and automatically inferred the complete query intent and SQL structure. For example, for the question "Which type of user has the highest battery level?", the intent completion module identified the intent field as "battery level", the grouping dimension as "user type", and the sorting condition as "battery level in descending order". Therefore, it automatically inferred the complete query intent as querying the type of user with the highest battery level among all user categories and generated the corresponding structured query. Through the intent completion module, the large language model's intelligent query-counting ability when facing fuzzy questions was significantly improved, enabling it to more accurately understand the user's true intent and generate structured query statements.
[0127] After the intent completion module outputs the structured query statement, based on the query intent knowledge graph constructed in step S1, the logical elements in the structured query are mapped to physical database objects: intent clusters are mapped to target data tables, for example, the intent cluster "user electricity consumption statistics" is mapped to the "power_consumption" table; natural language fields are mapped to physical field names, for example, "electricity consumption" is mapped to the "kwh" field; the standardized SQL template library constructed in step S2 is called, and placeholder variables are filled according to rules. For example, when the structured query contains the aggregate function SUM and the filter conditions "region" and "month", the system matches the template "SELECT SUM({field}) FROM {table} WHERE Region='{region value}' AND Billing month='{month value}'", and according to the mapping relationship of the knowledge graph, the placeholder {field} is replaced with the specific field name "kwh", and {table} is replaced with "power_consumption", generating the basic SQL statement "SELECT SUM(kwh) FROM power_consumption WHERE Region='certain region' AND The billing month is set to '2023-11'. Preferably, for complex logic such as multi-table joins, the system automatically adds JOIN clauses to implement table connections; for nested query requirements (such as the "above average" condition), it automatically constructs subquery structures. Finally, the structured query statement and the generated basic SQL draft are input into the compound prompt word template built by S3, triggering the large language model to perform syntax integrity checks and business logic verification, such as supplementing missing GROUP BY clauses and adapting to the target database dialect (e.g., converting date functions to Oracle's TO_DATE() format), ultimately outputting a standard SQL statement that conforms to syntax rules and can be directly executed.
[0128] S5. To evaluate the performance of the large language model with an intentional completion module after training with compound prompt word templates, this embodiment also establishes a set of evaluation quantitative indicators. These indicators cover multiple key evaluation metrics to assess the model's performance from different perspectives. Specifically, in this embodiment, the key evaluation metrics include semantic matching accuracy, structural matching accuracy, execution success rate, and business availability, wherein:
[0129] The semantic matching accuracy is used to measure whether the intent expressed by the SQL statement generated by the large language model completely matches the user's query intent. If the SQL statement generated by the large language model can accurately reflect the query intent of the user's natural language query, it is considered a successful semantic match. For example, if the user's natural language query is "query the electricity consumption of industrial users this month", and the SQL statement generated by the large language model can accurately filter out industrial users this month and count their electricity consumption, then the semantic matching accuracy will be improved accordingly. Preferably, in this embodiment, the semantic matching adopts a fuzzy matching method, comparing the key information of the user's natural language query obtained after semantic parsing with the key information of the SQL statement generated by the large language model to obtain the semantic matching degree. The semantic matching degree is compared with a preset semantic matching threshold. If it exceeds the preset semantic matching threshold, the semantic matching is considered successful.
[0130] The structure matching accuracy is used to measure the structural consistency between the SQL statements generated by the large language model and standard SQL statements. Standard SQL statements have specific grammatical rules and structures, and the SQL statements generated by the large language model need to conform to them in terms of grammatical structure. For example, the table names, field names, keyword usage, and overall structure of the SQL statement all need to conform to standard specifications. Preferably, in this embodiment, fuzzy matching is also used for structure consistency matching to obtain the structure matching degree. The structure matching degree is compared with a preset structure matching threshold. If it exceeds the preset structure matching threshold, the structure matching is considered successful.
[0131] The execution success rate is determined by whether the SQL statement generated by the large language model can be successfully executed in the real power marketing business database. Even if the SQL statement is correct in semantics and structure, if it cannot be executed in the database due to reasons such as database permissions or data type mismatch, then the SQL statement cannot be considered valid.
[0132] The aforementioned business availability is a manually evaluated metric used to measure whether the SQL statements output by the model can meet actual business needs. In electricity marketing, in addition to the correctness of the SQL statements, it is also necessary to consider whether they can provide valuable information for business decisions. For example, can the generated SQL query results help business personnel understand user electricity consumption and assess market trends?
[0133] The performance of the large language model is comprehensively evaluated by constructing evaluation formulas corresponding to key evaluation indicators, expressed as follows:
[0134] ;
[0135] In the formula, A comprehensive performance score for large language models; , , and These are the weighting coefficients for semantic matching accuracy, structural matching accuracy, execution success rate, and business availability, respectively; where:
[0136] ;
[0137] ;
[0138] ;
[0139] In the formula, For semantic matching accuracy, This represents the number of samples that successfully matched semantically. This represents the total number of test samples; For structure matching accuracy, This represents the number of samples that successfully matched the structure. To increase the success rate of execution, The number of samples in which SQL statements generated based on the large language model were successfully executed in a real electricity marketing business database; To score business availability, business experts were invited to rate the business availability of each sample. The scoring range was set from 0 to 10. Factors considered in the scoring included whether the generated SQL statement could accurately solve the business problem and whether it conformed to the business logic and process. The average availability score of all samples was obtained as the business availability score.
[0140] By evaluating the formula, the performance of the large language model in the natural language to SQL query conversion task can be comprehensively and accurately assessed. This allows for a more scientific comparison of the effects of different prompt word design schemes in A / B experiments, providing a more reliable basis for iterative optimization.
[0141] The A / B testing involved designing different suggestion word schemes, including different suggestion structures, example selections, and inference methods. These schemes were then applied to a large language model for testing. Changes in key evaluation metrics were recorded during testing to compare the effectiveness of different schemes. For example, Scheme A and Scheme B were designed. Scheme A has a more concise and clear suggestion structure, and its example selection focuses on common query scenarios; Scheme B has a more detailed suggestion structure, and its example selection covers more complex query situations. The A / B tests observed changes in metrics such as semantic matching accuracy, structural matching accuracy, execution success rate, and business availability score under different schemes. Based on the experimental results, the suggestion structure, example selection, and inference methods were fine-tuned to continuously optimize the composite suggestion word template, enabling it to better adapt to various business scenarios and achieving continuous optimization and scenario generalization of suggestion word engineering.
[0142] Example 2:
[0143] This embodiment is based on the same inventive concept as Embodiment 1. Building upon Embodiment 1, it further provides steps for optimizing semantic encoding and clustering using a domain-adaptive mechanism. Specifically:
[0144] In Example 1, the BERT model was incrementally pre-trained using text from the power sector. This example will further enhance this process to more effectively improve the encoding capability of business terms, including:
[0145] To expand the power sector texts in Example 1, we extensively collect more diverse types of power sector texts, including power industry standards and specifications, professional technical documents, and the latest power research reports. These texts cover a wealth of business terms and professional expressions, which can provide more comprehensive semantic information for the BERT model.
[0146] The expanded power sector texts were categorized according to topic and difficulty, and the BERT model was incrementally pre-trained in multiple stages. Different training parameters and training rounds were set for each stage. For example, for texts with higher difficulty and stronger professionalism, the number of training rounds and learning rate were appropriately increased to ensure that the model could fully learn the business terminology encoding features.
[0147] During incremental pre-training, the weights of the loss functions for the Masked Language Modeling (MLM) task and the Next Sentence Prediction (NSP) task are dynamically adjusted based on the training results at different stages. For example, in the early stages of training, the weights of the MLM task are appropriately increased in order to enable the BERT model to quickly learn the vocabulary and grammatical structures of the power industry; in the later stages of training, the weights of the NSP task are increased in order to improve the model's understanding of the semantic relationships between sentences.
[0148] Meanwhile, this embodiment differs from step S13 in Embodiment 1 in that it introduces the frequency of service demands as the basis for cluster weight allocation, enabling a more refined division of high-frequency service intent clusters. Specifically:
[0149] In real-world business scenarios, recent business needs often better reflect the current business priorities. Therefore, when calculating the frequency of business needs, a time decay factor is introduced, expressed by the formula:
[0150] ;
[0151] In the formula, This is the time decay factor corresponding to the query time of historical query statements; This represents the query time of the historical query statement; The current time; This is the attenuation coefficient, which can be adjusted according to the actual business situation.
[0152] For each business requirement, the corresponding weighted frequency is calculated, expressed by the formula:
[0153] ;
[0154] In the formula, For the first Weighted frequency of each business requirement For the first The total number of times each business requirement occurred; For the first The first occurrence The query time for each business requirement; For the first The first occurrence The time decay factor corresponding to the query time of each business requirement;
[0155] To make it easier to understand, here is an example:
[0156] For the "user battery consumption trend analysis" business requirement, there were 3 queries at different times, namely: , and Then its weighted frequency is .
[0157] Different business needs have varying degrees of importance within the overall business operations of an enterprise. In addition to considering frequency, this embodiment also introduces a business importance coefficient. To adjust the cluster weights, for the first... Each intent cluster contains a set of business requirements. The cluster weight of the intended cluster is expressed by the formula:
[0158] ;
[0159] In the formula, For the first Cluster weights of intent clusters; This represents the total number of business requirements.
[0160] For intent clusters with high cluster weights (i.e., exceeding a preset cluster weight threshold), this embodiment employs a more refined clustering method for secondary partitioning, including:
[0161] Assuming in this embodiment, the intended cluster with a high cluster weight (i.e., exceeding the preset cluster weight threshold) is... Initially, the intention cluster Each semantic vector is treated as a separate cluster, and cluster similarity is calculated. The two clusters with the highest cluster similarity are merged at each step until all semantic vectors are in one cluster, forming a clustering tree. The cluster similarity is expressed by the formula:
[0162] ;
[0163] ;
[0164] In the formula, semantic vector and Overall similarity between them; For clusters and Cluster-wide similarity between them; and Clusters and Semantic vectors in; clusters and For intention cluster The initial partitioning of individual clusters; and Clusters and The number of semantic vectors in the middle; semantic vector and Cosine similarity between them; semantic vector and The Euclidean distance between them; For intention cluster The maximum Euclidean distance among all semantic vectors in the dataset; semantic vector and The semantic relevance between them is obtained through a pre-trained semantic model; , and These are the cosine similarity weight, the Euclidean distance weight, and the semantic relevance weight, respectively.
[0165] To determine the final appropriate intention cluster partitioning, this embodiment employs the elbow rule to prune the clustering tree, including:
[0166] Starting from the root node of the current clustering tree, the number of clusters is currently... Calculate the corresponding intra-class sum of squares, whereby the formula for calculating the intra-class sum of squares is:
[0167] ;
[0168] In the formula, To be divided into Intra-class sum of squares for each cluster; For the cluster index; the first Each cluster is Its center of mass is ; For clusters semantic vectors within;
[0169] Each time, a merge operation is selected from the clustering tree for reverse splitting (i.e., pruning), resulting in a new partitioning scheme. The calculation of the partitioning scheme is then performed. Value and corresponding until a preset maximum number of clusters is reached. ;
[0170] The calculated differences value corresponding The values are plotted in a two-dimensional coordinate system, with the horizontal axis representing the number of clusters. The vertical axis is Under normal circumstances, with The increase, It will gradually decrease because the number of samples within each cluster decreases, and the distance from the sample to the centroid will decrease accordingly. However, when After increasing to a certain level, The rate of decrease will slow down, forming a turning point similar to an "elbow"; to find this elbow point more accurately, calculate the values of two adjacent points. Value rate of change Expressed as a formula:
[0171] ;
[0172] Set a threshold for the rate of change to decrease. Find the point where the rate of change begins to decline significantly. Value, when At that time, it was believed The corresponding partition is the relatively optimal partition, the optimal partition found according to the elbow rule. The value is used to perform pruning operations on the clustering tree to obtain the final intention cluster partitioning. That is, starting from the root node of the clustering tree, we go down the branches of the tree until we reach the desired cluster. Up to one cluster, these clusters constitute the final intention cluster.
[0173] Example 3:
[0174] This embodiment provides an intelligent SQL generation system for business queries. The system includes a query data collection and knowledge graph construction module, a template extraction and example set generation module, a compound prompt word template construction module, a structured query statement generation module, and a standard SQL statement generation module, wherein:
[0175] The query data collection and knowledge graph construction module is used to obtain query statement related data in the power marketing system, including historical query statements, historical SQL statements and business requirements. Based on the query statement related data, a query intent knowledge graph is constructed using semantic clustering.
[0176] The template extraction and example set generation module is used to parse the historical SQL statement structure based on the query intent knowledge graph, extract natural language templates containing placeholder variables and paired SQL templates; after semantically expanding the natural language templates through a large language model, it combines the SQL templates to generate a few-shot example set.
[0177] The composite prompt word template construction module is used to construct composite prompt word templates based on task setting guidance, examples in the few-shot example set, and CoT chain-like reasoning guidance.
[0178] The structured query statement generation module is used to set up an intent completion module in the large language model. The intent completion module is used to combine the natural language query statement input by the user with the context of the compound prompt word template to generate a structured query statement.
[0179] The standard SQL statement generation module is used to generate corresponding standard SQL statements based on structured query statements, using query intent knowledge graphs and compound prompt word templates.
[0180] Example 4:
[0181] This embodiment provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, it implements an SQL intelligent generation method for business queries as described in any embodiment of the present invention.
[0182] Example 5:
[0183] This embodiment provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements an SQL intelligent generation method for business queries as described in any embodiment of the present invention.
[0184] It is worth noting that the system, electronic device and computer-readable storage medium described in this invention are all based on the same inventive concept as the methods described in Embodiment 1 or 2 of this invention, and will not be described again here.
[0185] In this embodiment of the invention, "at least one" refers to one or more, and "more than one" refers to two or more. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent the existence of A alone, A and B simultaneously, or B alone. A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects have an "or" relationship. "At least one of the following" and similar expressions refer to any combination of these items, including any combination of singular or plural items. For example, at least one of a, b, and c can represent: a, b, c, a and b, a and c, b and c, or a and b and c, where a, b, and c can be single or multiple.
[0186] Those skilled in the art will recognize that the units and algorithm steps described in the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of electronic hardware and software. 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 implementations should not be considered beyond the scope of this invention.
[0187] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0188] In several embodiments provided by this invention, any function, if implemented as a software functional unit and sold or used as an independent product, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0189] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent structural or procedural transformations made based on the content of the present invention's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of the present invention.
Claims
1. A method for intelligently generating SQL for business queries, characterized in that, The method includes: Obtain query statement-related data from the business system, including historical query statements, historical SQL statements, and business requirements. Based on this query statement-related data, construct a query intent knowledge graph using semantic clustering, specifically: The historical query statements are semantically encoded using the BERT model to generate a set of semantic vectors. The set of semantic vectors is then divided using a clustering algorithm to generate a set of intent clusters. Based on the business requirements and predefined data tables and field sets, the query statements in the intent clusters are analyzed to establish a query intent knowledge graph that includes the mapping relationship between intent clusters and data tables and fields. Based on the query intent knowledge graph, the historical SQL statement structure is parsed, and natural language templates containing placeholder variables and paired SQL templates are extracted. After semantic variation expansion of the natural language templates using a large language model, they are combined with the SQL templates to generate a few-shot example set, specifically: The word embedding layer of the large language model is called to calculate word vectors for nonvariable words in the natural language template. K nearest neighbor synonyms are retrieved in the word vector space and replaced with the original words to generate word vector variants. The syntax analyzer of the large language model is invoked to generate sentence transformation variants, including adjusting subject-verb-object structures to passive voice, converting declarative sentences into interrogative sentences, and adding or deleting modifiers; Based on the mapping relationship of the query intent knowledge graph, placeholder variables in word vector variants and sentence transformation variants are replaced with specific field names to generate natural language variants and SQL variants; Natural language variants are combined with SQL variants to form paired examples and stored in a few-shot example set; Based on task setting guidance, examples in the few-shot example set, and CoT chain-like reasoning guidance, a compound prompt word template is constructed. An intent completion module is set in the large language model. The intent completion module is used to combine the natural language query statement input by the user with the context of the compound prompt word template to generate a structured query statement. Based on structured query statements, standard SQL statements are generated using query intent knowledge graphs and compound prompt word templates.
2. The SQL intelligent generation method for business queries according to claim 1, characterized in that, The specific steps for extracting natural language templates containing placeholder variables and paired SQL templates are as follows: The data table set and field set mapped by the intent cluster in the query intent knowledge graph are parsed; the historical SQL statement set is traversed, and historical SQL statements that contain any data table in the data table set and at least two fields in the field set are selected; the selected historical SQL statements are parsed using a syntax tree, common structures are extracted, and table names and field names in the common structures are replaced with placeholder variables to construct SQL templates; Based on the mapping relationship, placeholder variables are back-mapped to natural language elements to generate paired natural language templates.
3. The SQL intelligent generation method for business queries according to claim 2, characterized in that, Based on the task setting guidance, examples from the few-shot example set, and CoT chain-like reasoning guidance, a compound prompt word template is constructed, specifically as follows: Add a task setting guidance statement at the beginning of the compound prompt word template. The task setting guidance statement is used to clarify the role and specific task of the large language model in the business data query scenario. Following the task setting guidance statement, a representative question-and-answer example selected from the few-shot example set is embedded, which covers different business scenarios and query types in the business system; After the representative question-and-answer example, add the CoT chain-like reasoning guidance statement. The CoT chain-like reasoning guidance statement is used to guide the large language model to decompose the user's natural language query statement step by step and reason about it, and finally output the SQL statement. The step-by-step process includes querying the data tables involved, filtering fields and conditions, aggregation methods and output fields, and generating the SQL statement.
4. The SQL intelligent generation method for business queries according to claim 3, characterized in that, The intent completion module combines the user's input natural language query with the context of the compound prompt word template to generate a structured query, specifically: Based on the BiLSTM-CRF model, key entities are located and identified in the natural language query statements input by users to obtain entity recognition results. Combining the entity recognition results, the context of the compound prompt word template, and business logic, the semantics of the natural language query statements input by users are analyzed and missing key information is dynamically supplemented. If, after semantic completion, the user-input natural language query still has missing required parameters, it is filled in according to preset rules to obtain a structured query.
5. The SQL intelligent generation method for business queries according to claim 4, characterized in that, When generating structured query statements using the intent completion module, it also includes: For natural language queries with ambiguous user input, a pre-trained fuzzy intent example mechanism is used to automatically infer the intent field, grouping dimension, and sorting conditions corresponding to the ambiguous user input natural language query based on the output of entity recognition and semantic completion, and generate the corresponding structured query.
6. A SQL intelligent generation system for business queries, characterized in that, The system for an intelligent SQL generation method for business queries according to any one of claims 1 to 5 includes a query data collection and knowledge graph construction module, a template extraction and example set generation module, a compound prompt word template construction module, a structured query statement generation module, and a standard SQL statement generation module, wherein: The query data collection and knowledge graph construction module is used to obtain query statement related data from the business system, including historical query statements, historical SQL statements and business requirements. Based on the query statement related data, a query intent knowledge graph is constructed using semantic clustering. The template extraction and example set generation module is used to parse the historical SQL statement structure based on the query intent knowledge graph, extract natural language templates containing placeholder variables and paired SQL templates; after semantically expanding the natural language templates through a large language model, it combines the SQL templates to generate a few-shot example set. The composite prompt word template construction module is used to construct composite prompt word templates based on task setting guidance, examples in the few-shot example set, and CoT chain-like reasoning guidance. The structured query statement generation module is used to set up an intent completion module in the large language model. The intent completion module is used to combine the natural language query statement input by the user with the context of the compound prompt word template to generate a structured query statement. The standard SQL statement generation module is used to generate corresponding standard SQL statements based on structured query statements, using query intent knowledge graphs and compound prompt word templates.
7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements an SQL intelligent generation method for business queries as described in any one of claims 1 to 5.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that, When executed by the processor, the program implements an intelligent SQL generation method for business queries as described in any one of claims 1 to 5.
Citation Information
Patent Citations
SQL statement generation method and device, electronic equipment and storage medium
CN118939681A
ICL large language model data query generation method and system based on diversity SQL reinforcement
CN119692466A
Interactive data query and display method and system based on large language model
CN119903165A