Natural language-to-structured query language conversion method giving consideration to accuracy and reasoning cost
By employing multi-dimensional scoring and data augmentation techniques, combined with dual-channel vector similarity retrieval and hierarchical reasoning, the high reasoning cost and generation error issues in natural language to structured query language methods are resolved, achieving efficient and accurate SQL generation.
Patent Information
- Application Number
- CN202511568772.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-30
- Publication Date
- 2026-01-09
AI Technical Summary
Existing methods for converting natural language to structured query language suffer from high inference costs, insufficient example hints, lack of self-correction capabilities, and lack of hierarchical task complexity, leading to wasted computational resources and generation errors.
By classifying query task complexity using a multi-dimensional scoring method, and combining data augmentation and dual-channel vector similarity retrieval, large language models with different parameter amounts are dynamically invoked, and hierarchical reasoning and automatic correction are performed to generate SQL statements with correct structure and consistent semantics.
It reduces inference costs, improves generation accuracy, and enables precise handling of simple, medium, and complex tasks, ensuring the stability and efficiency of the system.
Smart Images

Figure CN121301384A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of natural language processing, database query and artificial intelligence large language model application, in particular to a natural language to structured query language method considering accuracy and reasoning cost. BACKGROUND
[0002] With the rapid development of artificial intelligence and large language model technologies such as GPT model and Ali Tongyiqian model, more and more users hope to query information and analyze data through natural language, so as to realize efficient query and data analysis. Natural language to structured query language SQL conversion technology emerges as the times require, and its goal is to automatically convert natural language input into structured query language statements. A mainstream conversion method at present is a generation method based on neural network or large language model, which learns the corresponding relationship between natural language and SQL through large-scale corpus, and has strong language understanding ability and semantic expression ability.
[0003] However, such methods have the following problems in actual application. First, the reasoning cost is high. Although large large language models can improve the accuracy of complex tasks, they are still uniformly called when processing simple queries, causing waste of computing resources and response delay. In addition, the example prompt is insufficient. Some NL2SQL methods based on few-shot learning rely on example prompts, but the existing example selection methods are mostly based on text similarity, ignoring database structure matching degree, resulting in incorrect SQL structure or field inconsistency. Third, there is a lack of self-correction ability. After the existing model generates SQL, if there is a syntax error or semantic deviation, manual intervention is often required to regenerate, lacking an automatic detection and correction mechanism, which reduces system stability. Fourth, the task complexity is not graded. The existing methods do not consider the difference in query difficulty in the model calling strategy, which cannot control the reasoning cost while ensuring the accuracy. SUMMARY
[0004] The purpose of the present application is to overcome the shortcomings and deficiencies of the prior art, and to provide a natural language to structured query language method considering accuracy and reasoning cost, which realizes dynamic grading processing and model calling control of natural language query task by combining complexity judgment, example library enhancement, semantic structure fusion retrieval, graded reasoning and automatic correction, thereby effectively reducing the reasoning cost and improving the generation accuracy.
[0005] To achieve the above purpose, the technical scheme provided by the present application is as follows: a natural language to structured query language method considering accuracy and reasoning cost, comprising the following steps: S1: For the input natural language query sentence, a multi-dimensional scoring method is used to analyze the semantic expression characteristics and potential structural intent of the natural language query sentence, comprehensively evaluate the complexity of the query task, and divide it into simple, medium or complex levels accordingly; wherein the multi-dimensional scoring method is a method combining the complexity judgment model based on RoBERTa encoder and heuristic rules, which comprehensively judges the complexity level of the query task by fusing the semantic, structural and operator feature scores of the natural language query sentence; For historical natural language query sentence and structured query language SQL pairing samples, use data enhancement method to generate diversified sample data to form an enhanced sample library; S2: Based on the enhanced sample library generated in step S1, perform semantic vector retrieval to select the K most relevant examples to the input natural language query sentence, and obtain a set of scene consistent prompt examples related to the current query; wherein the semantic vector retrieval adopts a double-channel vector similarity calculation method, which completes sample similarity fusion ranking based on semantic channel and SQL structure channel respectively; S3: According to the complexity level output by step S1 and the scene consistent prompt example set output by step S2, execute the hierarchical structured query language SQL generation operation, and the specific hierarchical reasoning path selection strategy is: For query tasks with simple complexity, use a large language model with small parameter quantity based on rule templates for direct SQL statement generation; For query tasks with medium complexity, combine the scene consistent prompt example set in step S2, and use a large language model with medium parameter quantity to generate SQL statements; For query tasks with complex complexity, combine the scene consistent prompt example set in step S2, and use a large language model with large parameter quantity to perform step-by-step chain thinking reasoning to generate SQL statements; S4: Perform structure correctness verification and semantic consistency verification on the SQL statements generated in step S3 respectively; wherein the structure correctness verification judges whether the SQL statements generated in step S3 are legal in the structure level, and the semantic consistency verification judges whether the SQL statements generated in step S3 and the input natural language query sentence are consistent in the semantic level; if found illegal or inconsistent, construct a correction prompt Prompt, and call a large language model with medium parameter quantity to regenerate SQL statements, and finally output SQL statements with correct structure and accurate semantics.
[0006] Further, in step S1, the multi-dimensional scoring method includes the following steps: S11: Calculate the semantic feature score, that is, encode the natural language query statement based on the RoBERTa encoder through the complexity judgment model, and extract semantic dimension features such as sentence length, number of logical connectors, nesting structure depth, and conditional statement complexity, and output the semantic feature score. S12: Calculate the structural feature score, that is, extract keywords and perform syntactic structure analysis on the input natural language query statement based on the database meta-information using the heuristic rules, identify the number of tables involved, the number of fields and the relationship between fields, estimate the structural complexity, and output the structural feature score. S13: Calculate the operator feature score, that is, identify the aggregate functions, sorting control and nested operation high-level SQL operators in the natural language query statement through the heuristic rules, estimate the operator complexity, and output the operator feature score; S14: The semantic feature score, structural feature score, and operator feature score are weighted and fused to output the complexity level.
[0007] Furthermore, in step S1, the data augmentation method includes: Perform synonym replacement, near-synonym rewriting, word order adjustment, redundancy elimination, and text perturbation operations on historical natural language queries to generate diverse expressions; For SQL statements paired with historical natural language query statements, variant SQL can be generated by replacing table names, fields, rewriting filter conditions, replacing aggregation and sorting functions, or reconstructing nested structures. Define natural language query and SQL pairing templates designed by experts, and automatically generate large-scale new pairing samples by populating entities, fields, and operators based on common query task types.
[0008] Furthermore, in step S2, the specific operation steps of the dual-channel vector similarity calculation method are as follows: S21: Perform semantic channel matching, that is, use the pre-loaded embedding model to encode the input natural language query statement and the sample questions in the enhanced sample library into semantic vectors respectively, and use the vector similarity function to calculate the semantic similarity between the natural language query statement and each sample question. S22: Perform structure channel matching, that is, for each sample in the enhanced sample library, extract its corresponding database structure information, including table name, field name and field description text; use the pre-loaded embedding model to encode the input natural language query statement and the database structure information corresponding to the sample in the enhanced sample library into structure vectors respectively, and use the vector similarity function to calculate the structure similarity between the natural language query statement and the database structure information corresponding to each sample. S23: Perform similarity fusion and sorting, that is, calculate the weighted fusion value of semantic similarity and structural similarity for each sample, which is the fusion similarity. The fusion weight is a preset parameter. Sort the samples from high to low according to the fusion similarity, and select the top K samples to form a prompt example set, so as to obtain a prompt example set that is consistent with the current query scenario.
[0009] Furthermore, in step S3, the hierarchical reasoning path selection strategy includes the following steps: S31: Based on the complexity level output in step S1, the input natural language queries are divided into three categories: simple, medium, and complex, which serve as the basis for the construction of large language model calls and prompts. S32: Generate different SQL generation prompts based on the different complexity levels of the query task. The Prompt constructor is as follows: For simple query tasks, the prompts are basic prompts that only include a task description and a data table description. The task description explains the query target, and the data table description includes the table name, field names, and field function descriptions. There is no need to use a consistent prompt example set for the scenario. For query tasks of medium complexity, the prompt words include task description, data table description and example description. The task description is used to explain the query target. The data table description includes table name, field name and field function description. The example description is based on the scenario-consistent prompt example set in step S2. Each example in the scenario-consistent prompt example set contains a natural language question and a corresponding SQL statement, which guides the large language model to generate SQL in the form of a few sample example prompts. For query tasks with high complexity, the prompts include task description, data table description, example description, and thought chain prompts. Each example includes a natural language question and a corresponding SQL statement, as well as a thought chain annotation based on manual annotation, which is used to demonstrate the reasoning process from semantics to SQL generation, so as to guide the large language model to think and generate SQL step by step. S33: Based on the task category, invoke a large language model with corresponding parameter size to generate SQL, as detailed below: For simple query tasks, a lightweight language model with fewer than 100 million parameters is invoked, using the prompt words constructed in step S32 as input and output SQL statements. For query tasks with medium complexity, a medium-sized language model with between 100 million and 1 billion parameters is invoked, and the prompt words constructed in step S32 are used as input and output SQL statements. For complex query tasks, a large language model with more than 1 billion parameters is invoked, and the prompt words constructed in step S32 are used as input and output SQL statements.
[0010] Furthermore, the large language model refers to a computational model trained through deep learning for processing natural language input and generating corresponding output. The large language model processes input information in units of tokens, with a token being the smallest processing unit of the model, including character-level units, sub-word-level units, and word-level units.
[0011] Furthermore, step S4 includes the following steps: S41: Perform structural verification, that is, perform structural verification on the SQL statement generated in step S3, and check in turn whether the SQL syntax conforms to the standard syntax rules, whether the table names and fields involved in the SQL statement exist in the database, so as to determine whether the SQL statement generated in step S3 is valid at the structural level. S42: Perform semantic consistency verification, that is, use a large language model with a small number of parameters to transcribe the SQL statement generated in step S3 into a natural language description, and use the large language model to determine whether the natural language description is consistent with the semantic expression of the input natural language query statement, so as to determine whether the SQL statement generated in step S3 is consistent with the input natural language query statement at the semantic level. S43: Perform automatic correction. If the verification result of step S41 is invalid or the verification result of step S42 is inconsistent, construct a correction prompt. The correction prompt includes the task description, the input natural language query statement, the SQL statement output by step S3, the verification result output by step S41, and the verification result output by step S42. Call a large language model with a medium number of parameters, and use the correction prompt as input to output the final SQL statement.
[0012] Furthermore, based on the complexity of the query task and the database pattern, upper limits are set for the length of prompt words and the number of examples to control the total number of input tokens for the language model, thereby controlling the inference cost.
[0013] Compared with the prior art, the present invention has the following advantages and beneficial effects: 1. By combining a complexity judgment model based on RoBERTa encoder with a multi-dimensional scoring method that integrates heuristic rules, the semantic expression complexity, structural complexity, and SQL operator complexity of natural language query statements are comprehensively evaluated. This enables the classification of tasks into simple, medium, and complex levels, providing accurate basis for subsequent inference paths and model calling strategies, and avoiding redundant resource consumption caused by calling high-computing-power large language models for low-complexity tasks.
[0014] 2. By performing data augmentation techniques such as synonym rewriting, structural perturbation, and field replacement on historical natural language query statements and SQL pairing samples, and combining them with template generation strategies, a more comprehensive and expressive augmented sample library is formed, providing a high-quality corpus foundation for subsequent example prompts.
[0015] 3. By encoding natural language similarity through the semantic channel and database structure similarity through the structural channel, and by fusing semantic and structural matching information, example retrieval with both semantic and structural consistency is achieved, thereby improving the accuracy and representativeness of example selection.
[0016] 4. Dynamically call large language models with different parameter sizes according to the task complexity level, and generate prompt input structures that match different levels of complexity. This realizes the strategy of using lightweight models for simple tasks, medium-sized models for medium tasks, and large models for complex tasks, thereby reducing the overall consumption of computing resources.
[0017] 5. The prompt word contains task description, data table description, example pairs, and thought chain content. It not only provides semantic goals to the model, but also clarifies the database table structure and field function semantics, ensuring that the language model fully understands the task intent and data context.
[0018] 6. The generated SQL is validated through structural verification and semantic backtracking. Combined with error type location and prompt guidance, a low-cost language model is called to perform local repair and regeneration, forming an automated error correction closed loop and improving the quality of the final SQL output.
[0019] 7. Supports automatic setting of Prompt length and example quantity thresholds to ensure that the input tokens of large language models do not exceed the set upper limit, achieving fine-grained inference cost control and possessing good engineering adaptability and deployability.
[0020] In summary, the method proposed in this invention improves the accuracy of natural language to SQL conversion while significantly reducing the inference cost of large language models through task grading and resource scheduling strategies, balancing efficiency and effectiveness, and possessing high application value and industrialization prospects. Attached Figure Description
[0021] Figure 1 This is a framework diagram of the method of the present invention. Detailed Implementation
[0022] The present invention will be further described in detail below with reference to the embodiments and accompanying drawings, but the embodiments of the present invention are not limited thereto.
[0023] like Figure 1As shown, this embodiment discloses a natural language to structured query language method that balances accuracy and inference cost. The method first analyzes the difficulty of the input natural language query task, calculates the task complexity level through a complexity judgment model and heuristic rules, and simultaneously uses data augmentation techniques to construct diverse historical natural language query and SQL pairing examples, forming an augmented example library. Next, based on dual-channel semantic and structural fusion retrieval, several highly relevant examples that best match the current query semantics and database structure are selected from the augmented example library to generate a scenario-consistent prompt example set. Subsequently, large language models with different parameter sizes are adaptively selected according to the complexity level, and the constructed prompt words are input into the model for hierarchical inference to generate SQL statements. Finally, through automatic correction and output, the generated SQL statements are validated for syntax, field validity, and semantic consistency. If errors are detected, corrective prompt words are automatically constructed and regenerated until a final SQL statement with correct structure and consistent semantics is output. Specifically, it includes the following steps: S1: For the input natural language query statement, a multi-dimensional scoring method is used to analyze the semantic expression features and potential structural intent of the natural language query statement, and to comprehensively evaluate the complexity of the query task, classifying it into simple, medium, or complex levels accordingly. The multi-dimensional scoring method is a combination of a complexity judgment model based on the RoBERTa encoder and heuristic rules. This method integrates the semantic, structural, and operator feature scores of the natural language query statement to comprehensively judge the complexity level of the query task. For historical natural language query statements paired with structured query language (SQL) samples, data augmentation methods are used to generate diverse sample data, forming an augmented sample library.
[0024] Specifically, the multi-dimensional scoring method includes the following steps: S11: Calculate the semantic feature score, that is, encode the natural language query statement based on the RoBERTa encoder through the complexity judgment model, and extract semantic dimension features such as sentence length, number of logical connectors, nesting structure depth, and conditional statement complexity, and output the semantic feature score. S12: Calculate the structural feature score, that is, extract keywords and perform syntactic structure analysis on the input natural language query statement based on the database meta-information using the heuristic rules, identify the number of tables involved, the number of fields and the relationship between fields, estimate the structural complexity, and output the structural feature score. S13: Calculate the operator feature score, that is, identify the aggregate functions, sorting control and nested operation high-level SQL operators in the natural language query statement through the heuristic rules, estimate the operator complexity, and output the operator feature score; S14: The semantic feature score, structural feature score, and operator feature score are weighted and fused to output the complexity level.
[0025] Specifically, the data augmentation method includes: Perform synonym replacement, near-synonym rewriting, word order adjustment, redundancy elimination, and text perturbation operations on historical natural language queries to generate diverse expressions; For SQL statements paired with historical natural language query statements, variant SQL can be generated by replacing table names, fields, rewriting filter conditions, replacing aggregation and sorting functions, or reconstructing nested structures. Define natural language query and SQL pairing templates designed by experts, and automatically generate large-scale new pairing samples by populating entities, fields, and operators based on common query task types.
[0026] S2: Based on the enhanced sample library generated in step S1, perform semantic vector retrieval, select the K most relevant samples to the input natural language query statement, and obtain a set of scenario-consistent prompts related to the current query; wherein, the semantic vector retrieval adopts a dual-channel vector similarity calculation method, and completes the sample similarity fusion and sorting based on the semantic channel and the SQL structure channel respectively.
[0027] Specifically, the specific operation steps of the dual-channel vector similarity calculation method are as follows: S21: Perform semantic channel matching, that is, use the pre-loaded embedding model to encode the input natural language query statement and the sample questions in the enhanced sample library into semantic vectors respectively, and use the vector similarity function to calculate the semantic similarity between the natural language query statement and each sample question. S22: Perform structure channel matching, that is, for each sample in the enhanced sample library, extract its corresponding database structure information, including table name, field name and field description text; use the pre-loaded embedding model to encode the input natural language query statement and the database structure information corresponding to the sample in the enhanced sample library into structure vectors respectively, and use the vector similarity function to calculate the structure similarity between the natural language query statement and the database structure information corresponding to each sample. S23: Perform similarity fusion and sorting, that is, calculate the weighted fusion value of semantic similarity and structural similarity for each sample, which is the fusion similarity. The fusion weight is a preset parameter. Sort the samples from high to low according to the fusion similarity, and select the top K samples to form a prompt example set, so as to obtain a prompt example set that is consistent with the current query scenario.
[0028] S3: Based on the complexity level output in step S1 and the scenario-consistent example set output in step S2, perform a hierarchical structured query language (SQL) generation operation. The specific hierarchical reasoning path selection strategy is as follows: For simple query tasks, SQL statements can be directly generated using rule templates or large language models with a small number of parameters. For query tasks with medium complexity, based on the scenario-consistent prompt example set in step S2, a large language model with a moderate number of parameters is used to prompt and generate SQL statements. For query tasks with high complexity, the SQL statement is generated by using a large language model with a large number of parameters to perform step-by-step chain-like reasoning, based on the consistent prompt example set in step S2.
[0029] Specifically, the hierarchical reasoning path selection strategy includes the following steps: S31: Based on the complexity level output in step S1, the input natural language queries are divided into three categories: simple, medium, and complex, which serve as the basis for the construction of large language model calls and prompts. S32: Generate different SQL generation prompts based on the different complexity levels of the query task. The Prompt constructor is as follows: For simple query tasks, the prompts are basic prompts that only include a task description and a data table description. The task description explains the query target, and the data table description includes the table name, field names, and field function descriptions. There is no need to use a consistent prompt example set for the scenario. For query tasks of medium complexity, the prompt words include task description, data table description and example description. The task description is used to explain the query target. The data table description includes table name, field name and field function description. The example description is based on the scenario-consistent prompt example set in step S2. Each example in the scenario-consistent prompt example set contains a natural language question and a corresponding SQL statement, which guides the large language model to generate SQL in the form of a few sample example prompts. For query tasks with high complexity, the prompts include task description, data table description, example description, and thought chain prompts. Each example includes a natural language question and a corresponding SQL statement, as well as a thought chain annotation based on manual annotation, which is used to demonstrate the reasoning process from semantics to SQL generation, so as to guide the large language model to think and generate SQL step by step. S33: Based on the task category, invoke a large language model with corresponding parameter size to generate SQL, as detailed below: For simple query tasks, a lightweight language model with fewer than 100 million parameters is invoked, using the prompt words constructed in step S32 as input and output SQL statements. For query tasks with medium complexity, a medium-sized language model with between 100 million and 1 billion parameters is invoked, and the prompt words constructed in step S32 are used as input and output SQL statements. For query tasks with high complexity, a large language model with more than 1 billion parameters is invoked, and the prompt words constructed in step S32 are used as input and output SQL statements. The large language model refers to a computational model trained through deep learning that processes natural language input and generates corresponding output. The large language model processes input information in units of tokens, where a token is the smallest processing unit of the model, including but not limited to character-level units, sub-word-level units, and word-level units.
[0030] S4: Perform structural correctness verification and semantic consistency verification on the SQL statement generated in step S3. The structural correctness verification determines whether the SQL statement generated in step S3 is valid at the structural level, and the semantic consistency verification determines whether the SQL statement generated in step S3 is semantically consistent with the input natural language query statement. If invalidity or inconsistency is found, a correction prompt is constructed, and a large language model with a moderate number of parameters is called to regenerate the SQL statement, ultimately outputting a structurally correct and semantically accurate SQL statement. This specifically includes the following steps: S41: Perform structural verification, that is, perform structural verification on the SQL statement generated in step S3, and check in turn whether the SQL syntax conforms to the standard syntax rules, whether the table names and fields involved in the SQL statement exist in the database, so as to determine whether the SQL statement generated in step S3 is valid at the structural level. S42: Perform semantic consistency verification, that is, use a large language model with a small number of parameters to transcribe the SQL statement generated in step S3 into a natural language description, and use the large language model to determine whether the natural language description is consistent with the semantic expression of the input natural language query statement, so as to determine whether the SQL statement generated in step S3 is consistent with the input natural language query statement at the semantic level. S43: Perform automatic correction. If the verification result of step S41 is invalid or the verification result of step S42 is inconsistent, construct a correction prompt. The correction prompt includes the task description, the input natural language query statement, the SQL statement output by step S3, the verification result output by step S41, and the verification result output by step S42. Call a large language model with a medium number of parameters, and use the correction prompt as input to output the final SQL statement.
[0031] Specifically, the upper limit thresholds for prompt word length and example number are determined based on the query task complexity and database pattern to control the total number of input tokens for the language model, thereby controlling the inference cost.
[0032] The above embodiments are preferred embodiments of the present invention, but the embodiments of the present invention are not limited to the above embodiments. Any changes, modifications, substitutions, combinations, or simplifications made without departing from the spirit and principle of the present invention shall be considered equivalent substitutions and shall be included within the protection scope of the present invention.
Claims
1. A natural language to structured query language method that balances accuracy and inference cost, characterized in that: Includes the following steps: S1: For the input natural language query statement, a multi-dimensional scoring method is used to analyze the semantic expression features and potential structural intent of the natural language query statement, and to comprehensively evaluate the complexity of the query task, classifying it into simple, medium, or complex levels accordingly. The multi-dimensional scoring method is a combination of a complexity judgment model based on the RoBERTa encoder and heuristic rules. This method integrates the semantic, structural, and operator feature scores of the natural language query statement to comprehensively judge the complexity level of the query task. For historical natural language query statements paired with structured query language (SQL) samples, data augmentation methods are used to generate diverse sample data, forming an augmented sample library. S2: Based on the enhanced sample library generated in step S1, perform semantic vector retrieval, select the K most relevant samples to the input natural language query statement, and obtain a set of scenario-consistent prompts related to the current query; wherein, the semantic vector retrieval adopts a dual-channel vector similarity calculation method, and completes the sample similarity fusion and sorting based on the semantic channel and the SQL structure channel respectively; S3: Based on the complexity level output in step S1 and the scenario-consistent example set output in step S2, perform a hierarchical structured query language (SQL) generation operation. The specific hierarchical reasoning path selection strategy is as follows: For simple query tasks, SQL statements can be directly generated using rule templates or large language models with a small number of parameters. For query tasks with medium complexity, based on the scenario-consistent prompt example set in step S2, a large language model with a moderate number of parameters is used to prompt and generate SQL statements. For query tasks with high complexity, the SQL statement is generated by using a large language model with a large number of parameters to perform step-by-step chain-like reasoning, based on the scenario-consistent prompt example set in step S2. S4: Perform structural correctness verification and semantic consistency verification on the SQL statement generated in step S3 respectively; wherein, the structural correctness verification determines whether the SQL statement generated in step S3 is valid at the structural level, and the semantic consistency verification determines whether the SQL statement generated in step S3 is consistent with the input natural language query statement at the semantic level; if invalidity or inconsistency is found, a correction prompt is constructed, and a large language model with a medium number of parameters is called to regenerate the SQL statement, and finally output a SQL statement with correct structure and accurate semantics.
2. The natural language to structured query language method that balances accuracy and inference cost according to claim 1, characterized in that, In step S1, the multi-dimensional scoring method includes the following steps: S11: Calculate the semantic feature score, that is, encode the natural language query statement based on the RoBERTa encoder through the complexity judgment model, and extract semantic dimension features such as sentence length, number of logical connectors, nesting structure depth, and conditional statement complexity, and output the semantic feature score. S12: Calculate the structural feature score, that is, extract keywords and perform syntactic structure analysis on the input natural language query statement based on the database meta-information using the heuristic rules, identify the number of tables involved, the number of fields and the relationship between fields, estimate the structural complexity, and output the structural feature score. S13: Calculate the operator feature score, that is, identify the aggregate functions, sorting control and nested operation high-level SQL operators in the natural language query statement through the heuristic rules, estimate the operator complexity, and output the operator feature score; S14: The semantic feature score, structural feature score, and operator feature score are weighted and fused to output the complexity level.
3. The natural language to structured query language method that balances accuracy and inference cost according to claim 1, characterized in that, In step S1, the data augmentation method includes: Perform synonym replacement, near-synonym rewriting, word order adjustment, redundancy elimination, and text perturbation operations on historical natural language queries to generate diverse expressions; For SQL statements paired with historical natural language query statements, variant SQL can be generated by replacing table names, fields, rewriting filter conditions, replacing aggregation and sorting functions, or reconstructing nested structures. Define natural language query and SQL pairing templates designed by experts, and automatically generate large-scale new pairing samples by populating entities, fields, and operators based on common query task types.
4. The natural language to structured query language method that balances accuracy and inference cost according to claim 1, characterized in that, In step S2, the specific operation steps of the dual-channel vector similarity calculation method are as follows: S21: Perform semantic channel matching, that is, use the pre-loaded embedding model to encode the input natural language query statement and the sample questions in the enhanced sample library into semantic vectors respectively, and use the vector similarity function to calculate the semantic similarity between the natural language query statement and each sample question. S22: Perform structure channel matching, that is, for each sample in the enhanced sample library, extract its corresponding database structure information, including table name, field name and field description text; use the pre-loaded embedding model to encode the input natural language query statement and the database structure information corresponding to the sample in the enhanced sample library into structure vectors respectively, and use the vector similarity function to calculate the structure similarity between the natural language query statement and the database structure information corresponding to each sample. S23: Perform similarity fusion and sorting, that is, calculate the weighted fusion value of semantic similarity and structural similarity for each sample, which is the fusion similarity. The fusion weight is a preset parameter. Sort the samples from high to low according to the fusion similarity, and select the top K samples to form a prompt example set, so as to obtain a prompt example set that is consistent with the current query scenario.
5. The natural language to structured query language method that balances accuracy and inference cost according to claim 1, characterized in that, In step S3, the hierarchical reasoning path selection strategy includes the following steps: S31: Based on the complexity level output in step S1, the input natural language queries are divided into three categories: simple, medium, and complex, which serve as the basis for the construction of large language model calls and prompts. S32: Generate different SQL generation prompts based on the different complexity levels of the query task. The Prompt constructor is as follows: For simple query tasks, the prompts are basic prompts that only include a task description and a data table description. The task description explains the query target, and the data table description includes the table name, field names, and field function descriptions. There is no need to use a consistent prompt example set for the scenario. For query tasks of medium complexity, the prompt words include task description, data table description and example description. The task description is used to explain the query target. The data table description includes table name, field name and field function description. The example description is based on the scenario-consistent prompt example set in step S2. Each example in the scenario-consistent prompt example set contains a natural language question and a corresponding SQL statement, which guides the large language model to generate SQL in the form of a few sample example prompts. For query tasks with high complexity, the prompts include task description, data table description, example description, and thought chain prompts. Each example includes a natural language question and a corresponding SQL statement, as well as a thought chain annotation based on manual annotation, which is used to demonstrate the reasoning process from semantics to SQL generation, so as to guide the large language model to think and generate SQL step by step. S33: Based on the task category, invoke a large language model with corresponding parameter size to generate SQL, as detailed below: For simple query tasks, a lightweight language model with fewer than 100 million parameters is invoked, using the prompt words constructed in step S32 as input and output SQL statements. For query tasks with medium complexity, a medium-sized language model with between 100 million and 1 billion parameters is invoked, and the prompt words constructed in step S32 are used as input and output SQL statements. For complex query tasks, a large language model with more than 1 billion parameters is invoked, and the prompt words constructed in step S32 are used as input and output SQL statements.
6. The natural language to structured query language method that balances accuracy and inference cost according to claim 5, characterized in that, The large language model refers to a computational model trained through deep learning that processes natural language input and generates corresponding output. The large language model processes input information in units of tokens, which are the smallest processing units of the model and include character-level units, sub-word-level units, and word-level units.
7. The natural language to structured query language method that balances accuracy and inference cost according to claim 1, characterized in that, Step S4 includes the following steps: S41: Perform structural verification, that is, perform structural verification on the SQL statement generated in step S3, and check in turn whether the SQL syntax conforms to the standard syntax rules, whether the table names and fields involved in the SQL statement exist in the database, so as to determine whether the SQL statement generated in step S3 is valid at the structural level. S42: Perform semantic consistency verification, that is, use a large language model with a small number of parameters to transcribe the SQL statement generated in step S3 into a natural language description, and use the large language model to determine whether the natural language description is consistent with the semantic expression of the input natural language query statement, so as to determine whether the SQL statement generated in step S3 is consistent with the input natural language query statement at the semantic level. S43: Perform automatic correction. If the verification result of step S41 is invalid or the verification result of step S42 is inconsistent, construct a correction prompt. The correction prompt includes the task description, the input natural language query statement, the SQL statement output by step S3, the verification result output by step S41, and the verification result output by step S42. Call a large language model with a medium number of parameters, and use the correction prompt as input to output the final SQL statement.
8. The natural language to structured query language method that balances accuracy and inference cost according to any one of claims 1-7, characterized in that, The upper limit thresholds for prompt word length and example number are determined based on query task complexity and database pattern to control the total number of input tokens for the language model, thereby controlling inference cost.
Citation Information
Cited By
Large model Text2SQL data construction and detection method, equipment and medium
CN121542288A
Zero sample Text2SQL (Structured Query Language) generation method based on structure perception and adaptive routing
CN121958318A