A natural language to SQL conversion method combining template and large language model hybrid driving
By constructing a pre-built SQL template library and a database schema information knowledge base, and combining it with a large language model, a hybrid approach of template and LLM is achieved. This solves the problems of syntax errors and lack of flexibility in SQL generation in existing technologies, generates high-quality SQL statements that conform to business logic, and improves the practicality and accuracy of NL2SQL technology.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- BEIJING XJ ELECTRIC
- Filing Date
- 2026-03-30
- Publication Date
- 2026-07-03
AI Technical Summary
In existing technologies, rule-based and template-based methods lack flexibility when handling complex queries, while pure large language model (LLM) methods are prone to syntax errors and unreliability when generating SQL. How to combine the advantages of both to generate SQL statements that are both syntactically correct and capable of handling complex queries has become an urgent problem to be solved.
We construct a pre-built SQL template library, a database schema information knowledge base, and a large language model engine. Through a hybrid approach of templates and LLM, we leverage templates to ensure syntactic correctness and combine them with the semantic understanding capabilities of LLM to accurately extract query entities and generate high-quality SQL statements.
The generated SQL statements not only guarantee syntactic correctness but also handle complex query conditions, significantly improving the practicality and accuracy of NL2SQL technology.
Smart Images

Figure CN122332415A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data processing technology, and in particular to a natural language to SQL method that combines templates and large language models for hybrid driving. Background Technology
[0002] Structured Query Language (SQL) is the standard language for manipulating relational databases. However, for business users without a technical background, writing complex SQL queries can be challenging, limiting their ability to directly retrieve and analyze data from the database. To address this issue, Natural Language to SQL (NL2SQL) technology was developed to automatically convert users' natural language queries into executable SQL queries.
[0003] Currently, there are two main technical approaches to implementing NL2SQL: one is based on rule-based and template matching mechanisms. This method predefines a series of SQL templates and extracts key information from natural language through rules such as keyword matching and syntactic analysis to fill in placeholders in the templates to generate SQL. The advantage of this method is that the generated SQL has high syntactic correctness, but the disadvantages are that the template coverage is limited, the ability to handle ambiguity in natural language is weak, the generalization ability is poor, and it is difficult to cope with complex and ever-changing query requirements.
[0004] Another approach is the end-to-end generation method based on Large Language Models (LLM). This method leverages the semantic understanding and text generation capabilities of LLM to directly convert natural language into SQL statements. The advantage of this type of method is its high flexibility, capable of handling complex query logic and diverse expressions. However, SQL generated purely by LLM may contain syntactic errors, such as misspelled table or field names, or missing JOIN conditions. Furthermore, it may generate "illusion" queries that do not conform to the database schema, making it difficult to guarantee business accuracy. In addition, the model inference process is opaque, making it difficult to trace the cause of errors.
[0005] In summary, rule-based and template-based methods ensure syntactic correctness but lack flexibility, while pure LLM methods offer flexibility but sacrifice reliability and interpretability. How to organically combine the advantages of both, ensuring SQL syntactic correctness while improving adaptability to complex queries, has become a pressing technical problem in this field. Summary of the Invention
[0006] This invention addresses the problems existing in the prior art by providing a method for converting natural language to SQL statements using a hybrid approach that combines templates and large language models, comprising the following steps: A pre-built SQL template library is constructed according to the query intent. The pre-built SQL template library stores information for each template, including at least the template ID, template category, fixed skeleton characters, and a list of placeholder definitions. The template category is constructed by classifying the templates and constructing the templates according to the categories. Each template contains a fixed skeleton and placeholders to be filled. When calling the large language model for matching, the information of the relevant templates is injected as context prompts. A database schema information knowledge base is constructed. The schema information knowledge base is used to record detailed information of all tables in the database. When calling the large language model for entity extraction, the detailed information of the relevant tables is used as the injection prompt words in the context. Build a large-scale natural language parsing and entity extraction engine, construct entity extraction prompt words, and realize deep parsing of users' natural language queries and extraction of entity information used to fill template placeholders; Build a large model template matching engine, construct template matching prompt words, and call LLM to match the corresponding target template from the SQL pre-set template library based on the entity information; A large model template filling engine is built, template filling prompts are generated, and the extracted entity information is filled into the corresponding placeholders of the selected target SQL template according to the template filling prompts, generating an executable SQL statement for seeding.
[0007] Furthermore, the template categories include, but are not limited to, simple query templates, aggregation query templates, grouping aggregation templates, multi-table join templates, sorting query templates, restricted query templates, and deduplication query templates.
[0008] Furthermore, the simple query template, used for the most basic conditional queries, has the following fixed framework and placeholders: SELECT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> ; The aggregation query template is used to calculate aggregation operations such as sums and averages. Its fixed skeleton and placeholders are as follows: SELECT<aggregation:agg_func> (<field:agg_field> FROM<table:table_name> WHERE<condition:where_clause> ; The grouping aggregation template adds a grouping function to the aggregation query, which is used to perform statistics after grouping by a certain field. Its fixed skeleton and placeholders are as follows: SELECT<field:group_field> ,<aggregation:agg_func> (<field:agg_field> FROM<table:table_name> WHERE<condition:where_clause> GROUP BY<field:group_field> ; The multi-table join template is used for queries that join multiple tables. Its fixed skeleton and placeholders are as follows: SELECT<field:select_fields> FROM <table:table1>JOIN <table:table2>ON<condition:join_condition> WHERE<condition:where_clause> ; The sorting query template is used to sort the results, and its fixed skeleton and placeholders are as follows: SELECT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> ORDER BY<field:order_field><order:order_direction> ; The query restriction template is used to limit the number of results returned. Its fixed skeleton and placeholders are as follows: SELECT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> LIMIT<limit:limit_value> ; The deduplication query template is used to remove duplicate records, and its fixed skeleton and placeholders are as follows: SELECT DISTINCT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> .
[0009] The placeholders use standardized naming conventions, with the format <type:name>; where<field:select_fields> This indicates the query fields that need to be filled.<table:table_name> Indicates the name of the table that needs to be filled.<condition:where_clause> This indicates the query conditions that need to be filled in.<limit:limit_value> Indicates a quantity limit.<field:order_field> Indicates the sorting field.<order:order_direction> Indicates the sorting direction.<condition:join_condition> Indicates the connection condition.<field:group_field> Indicates the grouping field.<aggregation:agg_func> Represents aggregate functions,<field:agg_field> This indicates an aggregated field.
[0010] Furthermore, the detailed information of the table includes, but is not limited to, table name, field name, field type, field description, and primary and foreign key relationships.
[0011] Furthermore, the entity extraction prompts include, but are not limited to, entity extraction task descriptions, database schema information, entity extraction requirements, and output format requirements; The entity extraction task is described as explicitly informing the large language model that it needs to extract entity information from the user's query question to generate SQL query statements. The database schema information inserts detailed information about the tables in the constructed knowledge base here as context, ensuring that the large language model uses the correct field names; The entity extraction requirements are explained one by one, outlining the meaning and extraction requirements for each entity type, and for query fields...<field:select_fields> The output should include a list of field names; for table names...<table:table_name> The query requires outputting a single table name; for the query conditions...<condition:where_clause> Each condition must include a field name, operator, and value, and the mapping relationship of the operators must be provided; for aggregate functions...<aggregation:agg_func> The output should be SUM, COUNT, AVG, MAX, or MIN; for grouping fields...<field:group_field> and aggregate fields<field:agg_field> The output should include the field names; for cases where the number of fields is limited.<limit:limit_value> This specifies the output format. The entity extraction output format requirements specify the output structure of the extracted entities.
[0012] Furthermore, the template matching prompts include a description of the template matching task, template library information, entity-template matching rule description, and the output format of the template matched by the large language model. The template matching task description explicitly tells the large language model that it needs to select the most suitable SQL template from the template library based on the extracted entity type combination. The template library information provides information on all templates in the constructed SQL pre-built template library to the large language model, helping the large language model understand the purpose and characteristics of each template; The entity-template matching rules are described in natural language as to which template should be matched for different combinations of entity types.
[0013] Furthermore, the entity-template matching rule description specifically includes: If the extracted entities only contain query fields, table names, and query conditions, and do not contain aggregate functions, grouping fields, multi-table information, etc., then a simple query template should be matched. If the extracted entity contains aggregate functions, aggregate fields, table names, and query conditions, but does not contain grouping fields, then the aggregate query template should be matched. If the extracted entity contains grouping fields, aggregate functions, aggregate fields, table names, and query conditions, then it should match the grouping aggregation template; If the extracted entity contains multiple table names and join conditions, then a multi-table join template should be matched. If the extracted entity contains a sorting field or sorting direction, it should match the sorting query template; If the extracted entities contain a limited number, then the limited query template should be matched; If the extracted entity contains a deduplication flag and it is true, then the deduplication query template should be matched. Furthermore, the template filling prompts include a description of the template filling task, template information, filling rule instructions, and template output format requirements; The template filling task description explicitly tells the large language model that entity information needs to be filled into the corresponding placeholders in the SQL template to generate a complete SQL statement; The template information provides the complete definition of the matched target template to the large language model, including the fixed skeleton string of the template and the detailed definition of each placeholder; The filling rules specify the filling methods for different types of placeholders; The template output format requirements stipulate that the large language model should only output the final generated SQL statement and should not include any other explanatory text.
[0014] Furthermore, the filling rules are specifically described as follows: For placeholders of field list type, concatenate the field name list into a comma-separated string; For placeholders of table name type, simply fill them with the table name value; For placeholders of query condition types, each condition needs to be converted to the format of "field operator value"; For placeholders of aggregate function type, simply fill them with the aggregate function value; For placeholders of grouping field type, simply fill them with the grouping field value; For sort clauses, use the sort field and sort direction to construct the format "ORDER BY field direction"; For placeholders in join condition types, convert them to the format "fields in the first table = fields in the second table"; For placeholders of the quantity type, simply use the quantity value to construct the "LIMIT quantity" clause.
[0015] The present invention has the following advantages: This invention provides a Natural Language to SQL (NL2SQL) method that combines templates and a large language model. By using a pre-built SQL template library, it quickly matches pre-built, syntactically correct SQL template skeletons, resolving syntax errors and illusions that may occur when generating SQL using pure LLM, thus ensuring the correctness of the SQL statements. By limiting entity extraction to the template framework and leveraging the powerful semantic understanding capabilities of LLM, it accurately extracts query entities. This utilizes the flexibility of LLM to handle complex business query conditions while ensuring the stability and reliability of the overall SQL structure through templates. Therefore, this invention can generate high-quality SQL statements that are both syntactically correct and conform to business logic, significantly improving the practicality and accuracy of NL2SQL technology. Attached Figure Description
[0016] Figure 1 The diagram shown is a schematic representation of the process of this invention. Figure 2 The diagram shown is a schematic representation of the implementation process of this invention. Detailed Implementation
[0017] The present invention will now be described in further detail with reference to the accompanying drawings. These drawings are simplified schematic diagrams, illustrating only the basic structure of the invention, and therefore only show the components relevant to the invention.
[0018] Please note that the terms "above", "below", "left", "right", "top", "top", "bottom", etc., used in this invention to describe positional relationships do not represent the absolute positional relationship between modules / components / assemblies / parts / components, but rather the relative positional relationship between modules / components / assemblies / parts / components.
[0019] like Figures 1-2 As shown, this invention provides an exemplary natural language to SQL method that combines templates with a large language model for hybrid driving. The method is described in detail below.
[0020] The implementation steps of this invention include: Step 1: Obtain the user's natural language query request.
[0021] The module receives users' natural language query requests.
[0022] Step 2: Build a pre-built SQL template library; A library of pre-built SQL templates is constructed based on query intent. Each template consists of two parts: a fixed skeleton and placeholders to be filled. The fixed skeleton is the syntactically complete SQL statement framework, while the placeholders are the variable parts that need to be filled according to the specific query.
[0023] The template library contains seven template categories, and the fixed skeleton definition for each category is as follows: Category 1: Simple query templates, used for the most basic conditional queries. The fixed structure is: SELECT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> ; The second type: Aggregate query templates, used for calculating sums, averages, and other aggregation operations. The fixed skeleton is: SELECT<aggregation:agg_func> (<field:agg_field> FROM<table:table_name> WHERE<condition:where_clause> ; The third type: Grouped aggregation templates, which add grouping functionality to aggregate queries, used for grouping by a specific field and then performing statistics. The fixed framework is: SELECT<field:group_field> ,<aggregation:agg_func> (<field:agg_field> FROM<table:table_name> WHERE<condition:where_clause> GROUP BY<field:group_field> ; The fourth type: multi-table join templates, used for queries that join multiple tables. The fixed structure is: SELECT<field:select_fields> FROM <table:table1>JOIN <table:table2>ON<condition:join_condition> WHERE<condition:where_clause> ; Category 5: Sorting query templates, used to sort results. The fixed structure is: SELECT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> ORDERBY<field:order_field><order:order_direction> ; Category 6: Restricted query templates, used to limit the number of results returned. The fixed skeleton is: SELECT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> LIMIT<limit:limit_value> ; Category 7: Deduplication query templates, used to remove duplicate records. The fixed skeleton is: SELECT DISTINCT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> .
[0024] The above fixed framework follows the basic structure and fixed syntax of SQL statements.
[0025] The placeholders mentioned above use standardized naming conventions, with the format <type:name>.<field:select_fields> : Indicates the query fields that need to be filled.<table:table_name> : Indicates the name of the table that needs to be filled.<condition:where_clause> : Indicates the query conditions that need to be filled in.<limit:limit_value> Indicates a limit on quantity,<field:order_field> Indicates sorting field,<order:order_direction> Indicates sorting direction,<condition:join_condition> Indicates connection conditions,<field:group_field> Indicates grouping field,<aggregation:agg_func> Represents aggregate functions,<field:agg_field> This indicates an aggregated field.
[0026] The template library is stored in a structured manner, with each template containing a template ID, template category, fixed skeleton string, and a list of placeholder definitions.
[0027] The template library is used in the template matching stage of the large language model. Each time the large language model is called to perform template matching, the relevant template information is injected as context prompts.
[0028] Step 3: Construct a database schema information knowledge base; The system pre-builds a database schema information knowledge base. The knowledge base records detailed information about all tables in the database, including table name, field names, field types, field descriptions, primary and foreign key relationships, etc.
[0029] Each time the large language model is invoked for entity extraction, the relevant table structure information is injected as contextual prompts.
[0030] Step 4: Build a large-scale natural language parsing and entity extraction engine; 1) First, construct prompt words based on large-scale natural language parsing and entity extraction to realize the function of deep parsing of users' natural language queries and extracting entity information used to fill template placeholders.
[0031] The entity extraction prompts first determine the entity types to be extracted based on the placeholder definitions in the SQL template.
[0032] Entity types correspond one-to-one with template placeholders, including<field:select_fields> ,<table:table_name> ,<condition:where_clause> ,<limit:limit_value> ,<field:order_field> ,<order:order_direction> ,<condition:join_condition> ,<field:group_field> ,<aggregation:agg_func> ,<field:agg_field> .
[0033] The entity extraction prompts must clearly inform the large language model of the task to be performed, the available database structure, and the expected output format. The prompts consist of the following parts: The first part is the task description, which clearly states that the large language model needs to extract entity information from the user's query to generate SQL query statements.
[0034] The second part is the database schema information, where the table structure information from the constructed knowledge base is inserted to ensure that the large language model uses the correct field names.
[0035] The third part is the entity extraction requirements, which explains the meaning and extraction requirements of each entity type.
[0036] For query fields<field:select_fields> The output should be a list of field names.
[0037] For table name<table:table_name> The requirement is to output a single table name.
[0038] For query conditions<condition:where_clause> Each condition must include a field name, an operator, and a value, and the mapping relationship of the operators must be given.
[0039] For aggregate functions<aggregation:agg_func> The output should be SUM, COUNT, AVG, MAX, or MIN.
[0040] For grouping fields<field:group_field> and aggregate fields<field:agg_field> The output should include the field names.
[0041] Regarding the quantity limit<limit:limit_value> This specifies the output format.
[0042] The fourth part is the output format requirements, which specifies the output structure of the extracted entities.
[0043] 2) Then, input the prompts obtained from the natural language parsing and entity extraction described above into the large language model. The large language model parses the natural language query requirements and extracts and outputs entity information according to the task requirements.
[0044] Step 5: Build a large model template matching engine; 1) First, construct prompt words based on large model template matching, and realize the function of matching the corresponding target SQL template from the template library through LLM according to the entity type combination extracted in step four.
[0045] The prompt word comprises the following parts: The first part is the task description, which clearly states that the large language model needs to select the most suitable SQL template from the template library based on the extracted entity type combination.
[0046] The second part is the template library information, which provides information about all templates in the constructed template library to the large language model, helping the large language model understand the purpose and characteristics of each template.
[0047] The third part describes the entity-template matching rules, using natural language to explain which templates should match different combinations of entity types. The specific rules are as follows: If the extracted entities only contain query fields, table names, and query conditions, and do not contain aggregate functions, grouping fields, multi-table information, etc., then a simple query template should be matched.
[0048] If the extracted entity contains aggregate functions, aggregate fields, table names, and query conditions, but does not contain grouping fields, then the aggregate query template should be matched.
[0049] If the extracted entity contains grouping fields, aggregate functions, aggregate fields, table names, and query conditions, then it should match the grouping aggregation template.
[0050] If the extracted entity contains multiple table names and join conditions, then a multi-table join template should be matched.
[0051] If the extracted entity contains a sorting field or sorting direction, it should match the sorting query template.
[0052] If the extracted entities contain a limited number, then the limited query template should be matched.
[0053] If the extracted entity contains a deduplication flag that is true, then the deduplication query template should be matched.
[0054] The fourth part involves inputting the extracted structured entity information.
[0055] Part 5 specifies the output format of the template matched by the large language model.
[0056] 2) Then, combine the above five parts into a complete template matching prompt and input it into the large language model. The large language model analyzes the combination of entity types in the current query, performs inference based on the provided matching rules, selects the most suitable template, and outputs the template information.
[0057] Step Six: Build a large model template filling engine; 1) First, construct a template filling module prompt based on a large model to fill the extracted entity information into the corresponding placeholders of the selected target SQL template and generate the final executable SQL statement.
[0058] The prompt word comprises the following parts: The first part is the task description, which clearly tells the large language model that it needs to fill the entity information into the corresponding placeholders in the SQL template to generate a complete SQL statement.
[0059] The second part is template information, which provides the complete definition of the matched target template to the large language model, including the template's fixed skeleton string and the detailed definition of each placeholder.
[0060] The third part explains the filling rules, specifying the filling methods for different types of placeholders. The specific filling rules are as follows: For placeholders of field list type, concatenate the field name list into a comma-separated string.
[0061] For placeholders of table name type, simply fill them with the table name value.
[0062] For placeholders of query condition types, each condition needs to be converted to the format of "field operator value".
[0063] For placeholders of aggregate function types, simply fill them with the aggregate function value.
[0064] For placeholders of grouping field type, simply fill them with the grouping field value.
[0065] For sort clauses, use the sort field and sort direction to construct the format "ORDER BY field direction".
[0066] For placeholders of join condition types, convert them to the format "field of the first table = field of the second table".
[0067] For placeholders of the quantity type, simply use the quantity value to construct the "LIMIT quantity" clause.
[0068] The fourth part involves inputting the structured entity information extracted from the three parts.
[0069] The fifth part is the output format requirements, which require the large language model to output only the final generated SQL statement and not include any other explanatory text.
[0070] 2) Then, input the above-mentioned prompts into the large language model. The large language model fills the entity information into the corresponding placeholders in the template one by one according to the filling rules, and finally generates an executable SQL statement.
[0071] Step 7: Output the final SQL statement; The generated SQL statement is returned to the user, completing the conversion process from natural language to SQL.
[0072] By employing the above method and utilizing a pre-built SQL template library, a pre-built, syntactically correct SQL template skeleton is quickly matched, resolving the syntax errors and illusions that may occur when generating SQL using pure LLM, thus ensuring the correctness of the SQL statements. By limiting the scope of entity extraction to the template framework and leveraging the powerful semantic understanding capabilities of LLM, query entities are accurately extracted. This utilizes the flexibility of LLM to handle complex business query conditions while ensuring the stability and reliability of the overall SQL structure through templates. Therefore, this invention can generate high-quality SQL statements that are both syntactically correct and conform to business logic, significantly improving the practicality and accuracy of NL2SQL technology.
[0073] Based on the above-described preferred embodiments of the present invention, and through the foregoing description, those skilled in the art can make various changes and modifications without departing from the inventive concept. The technical scope of this invention is not limited to the contents of the specification, but must be determined according to the scope of the claims.
Claims
1. A method for converting natural language to SQL statements using a hybrid approach combining templates and large language models, characterized in that: Includes the following steps: A pre-built SQL template library is constructed according to the query intent. The pre-built SQL template library stores information for each template, including at least the template ID, template category, fixed skeleton characters, and a list of placeholder definitions. The template category is constructed by classifying the templates and constructing the templates according to the categories. Each template contains a fixed skeleton and placeholders to be filled. When calling the large language model for matching, the information of the relevant templates is injected as context prompts. A database schema information knowledge base is constructed. The schema information knowledge base is used to record detailed information of all tables in the database. When calling the large language model for entity extraction, the detailed information of the relevant tables is used as the injection prompt words above. Build a large-scale natural language parsing and entity extraction engine, construct entity extraction prompt words, and realize deep parsing of users' natural language queries and extraction of entity information used to fill template placeholders; Build a large model template matching engine, construct template matching prompt words, and call LLM to match the corresponding target template from the SQL pre-set template library based on the entity information; A large model template filling engine is built, template filling prompts are generated, and the extracted entity information is filled into the corresponding placeholders of the selected target SQL template according to the template filling prompts, generating an executable SQL statement for seeding.
2. The natural language to SQL statement system based on a combination of templates and a large language model as described in claim 1, characterized in that: The template categories include, but are not limited to, simple query templates, aggregate query templates, grouped aggregate templates, multi-table join templates, sorted query templates, restricted query templates, and deduplication query templates.
3. The natural language to SQL statement method combining templates and large language models as described in claim 2, characterized in that: The simple query template is used for the most basic conditional queries, and its fixed framework and placeholders are as follows: SELECT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> ; The aggregation query template is used to calculate aggregation operations such as sums and averages. Its fixed skeleton and placeholders are as follows: SELECT<aggregation:agg_func> (<field:agg_field> FROM<table:table_name> WHERE<condition:where_clause> ; The grouping aggregation template adds a grouping function to the aggregation query, which is used to perform statistics after grouping by a certain field. Its fixed skeleton and placeholders are as follows: SELECT<field:group_field> ,<aggregation:agg_func> (<field:agg_field> FROM<table:table_name> WHERE<condition:where_clause> GROUP BY<field:group_field> ; The multi-table join template is used for queries that join multiple tables. Its fixed skeleton and placeholders are as follows: SELECT <field:select_fields>FROM <table:table1>JOIN <table:table2>ON<condition:join_condition> WHERE<condition:where_clause> ; The sorting query template is used to sort the results, and its fixed skeleton and placeholders are as follows: SELECT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> ORDER BY<field:order_field><order:order_direction> ; The query restriction template is used to limit the number of results returned. Its fixed skeleton and placeholders are as follows: SELECT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> LIMIT<limit:limit_value> ; The deduplication query template is used to remove duplicate records, and its fixed skeleton and placeholders are as follows: SELECT DISTINCT<field:select_fields> FROM<table:table_name> WHERE<condition:where_clause> ; The placeholders use standardized naming conventions, with the format <type:name>; where<field:select_fields> This indicates the query fields that need to be filled.<table:table_name> Indicates the name of the table that needs to be filled.<condition:where_clause> This indicates the query conditions that need to be filled in.<limit:limit_value> Indicates a quantity limit.<field:order_field> Indicates the sorting field.<order:order_direction> Indicates the sorting direction.<condition:join_condition> Indicates the connection condition.<field:group_field> Indicates the grouping field.<aggregation:agg_func> Represents aggregate functions,<field:agg_field> This indicates an aggregated field.
4. The natural language to SQL statement system combining templates and large language models as described in claim 1, characterized in that: The detailed information of the table includes, but is not limited to, table name, field name, field type, field description, and primary and foreign key relationships.
5. The natural language to SQL statement system based on a combination of templates and a large language model as described in claim 1, characterized in that: The entity extraction prompts include, but are not limited to, entity extraction task descriptions, database schema information, entity extraction requirements, and output format requirements; The entity extraction task is described as explicitly informing the large language model that it needs to extract entity information from the user's query question to generate SQL query statements; The database schema information inserts detailed information about the tables in the constructed knowledge base here as context, ensuring that the large language model uses the correct field names; The entity extraction requirements are explained one by one, outlining the meaning and extraction requirements for each entity type, and for query fields...<field:select_fields> The output should include a list of field names; for table names...<table:table_name> The query requires outputting a single table name; for the query conditions...<condition:where_clause> Each condition must include a field name, operator, and value, and the mapping relationship of the operators must be provided; for aggregate functions...<aggregation:agg_func> The output should be SUM, COUNT, AVG, MAX, or MIN; for grouping fields...<field:group_field> and aggregate fields<field:agg_field> The output should include the field names; for cases where the number of fields is limited.<limit:limit_value> This specifies the output format. The entity extraction output format requirements specify the output structure of the extracted entities.
6. The natural language to SQL method combining templates and large language models as described in claim 1, characterized in that: The template matching prompts include a description of the template matching task, template library information, entity-template matching rule description, and the output format of the template matched by the large language model. The template matching task description explicitly tells the large language model that it needs to select the most suitable SQL template from the template library based on the extracted entity type combination. The template library information provides information on all templates in the constructed SQL pre-built template library to the large language model, helping the large language model understand the purpose and characteristics of each template; The entity-template matching rules are described in natural language as to which template should be matched for different combinations of entity types.
7. The natural language to SQL method combining templates and a large language model as described in claim 7, characterized in that: The entity-template matching rules are specifically described as follows: If the extracted entities only contain query fields, table names, and query conditions, and do not contain aggregate functions, grouping fields, multi-table information, etc., then a simple query template should be matched. If the extracted entity contains aggregate functions, aggregate fields, table names, and query conditions, but does not contain grouping fields, then the aggregate query template should be matched. If the extracted entity contains grouping fields, aggregate functions, aggregate fields, table names, and query conditions, then it should match the grouping aggregation template; If the extracted entity contains multiple table names and join conditions, then a multi-table join template should be matched. If the extracted entity contains a sorting field or sorting direction, it should match the sorting query template; If the extracted entities contain a limited number, then the limited query template should be matched; If the extracted entity contains a deduplication flag that is true, then the deduplication query template should be matched.
8. The natural language to SQL method combining templates and large language models as described in claim 1, characterized in that: The template filling prompts include a description of the template filling task, template information, filling rule instructions, and template output format requirements; The template filling task description explicitly tells the large language model that entity information needs to be filled into the corresponding placeholders in the SQL template to generate a complete SQL statement; The template information provides the complete definition of the matched target template to the large language model, including the fixed skeleton string of the template and the detailed definition of each placeholder; The filling rules specify the filling methods for different types of placeholders; The template output format requirements stipulate that the large language model should only output the final generated SQL statement and should not include any other explanatory text.
9. The natural language to SQL method combining templates and a large language model as described in claim 8, characterized in that: The filling rules are specifically described as follows: For placeholders of field list type, concatenate the field name list into a comma-separated string; For placeholders of table name type, simply fill them with the table name value; For placeholders of query condition types, each condition needs to be converted to the format of "field operator value"; For placeholders of aggregate function type, simply fill them with the aggregate function value; For placeholders of grouping field type, simply fill them with the grouping field value; For sort clauses, use the sort field and sort direction to construct the format "ORDER BY field direction"; For placeholders in join condition types, convert them to the format "fields in the first table = fields in the second table"; For placeholders of the quantity type, simply use the quantity value to construct the "LIMIT quantity" clause.