A Smart Data Query Method for Large Model Indicators

By constructing an auxiliary information base and a consistency alignment mechanism, combined with few-sample hints and self-learning feedback, the problems of low accuracy and insufficient self-improvement in text-to-SQL generation are solved, achieving efficient generation of accurate SQL queries and improving system adaptability.

CN120336360BActive Publication Date: 2025-11-14INSPUR SOFTWARE TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510830537.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-06-20
Publication Date
2025-11-14
Estimated Expiration
2045-06-20

AI Technical Summary

Technical Problem

Existing text-to-SQL methods have low accuracy in small sample scenarios and lack the ability to verify results and improve themselves. This results in generated SQL statements that may not conform to the database structure or deviate from the user's intent, making it difficult to accurately obtain the required information.

Method used

An auxiliary information base is built, containing database structure information and historical question-and-answer examples. SQL queries are generated through few-sample hints and consistency alignment mechanisms, and self-learning is performed through a feedback mechanism to ensure that the generated queries are consistent with user questions and database schemas.

Benefits of technology

It significantly improves the accuracy of SQL queries and the system's self-improvement capabilities, enabling large models to adapt quickly and reliably across different databases and problem domains.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120336360B_ABST
    Figure CN120336360B_ABST
Patent Text Reader

Abstract

This invention provides an intelligent question-and-answer method for large-scale model index querying, belonging to the fields of natural language processing and database querying. This invention constructs an auxiliary information base to store database structures and historical question-and-answer examples. By processing natural language questions into a structured format, key entities and fields are extracted. Using a small number of examples, a large language model is guided to generate intermediate expressions close to SQL syntax and multiple SQL query candidates. A consistency alignment mechanism is designed to verify the candidate SQLs in terms of field matching, aggregate functions, and data tables to select the optimal SQL query. The finally generated correct SQL and its corresponding question-and-answer pair are fed back to the auxiliary information base, achieving self-learning reinforcement. This method improves the accuracy and scalability of large language models generating SQL queries from text with only a few training examples.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of natural language processing and database querying, and in particular to an intelligent query method for querying large model indicators. Background Technology

[0002] With the rapid development of database technology and artificial intelligence, users hope to directly query databases to obtain the information they need using everyday language. Text-to-SQL (TTO) technology has emerged to address this need, enabling computers to convert natural language questions into Structured Query Language (SQL) statements for database retrieval. Existing TTO methods typically rely on model training on large-scale labeled data or require model fine-tuning for specific database schemas. However, large pre-trained language models (LLMs) often encounter problems when directly generating SQL queries without sufficient training data or contextual information. For example, the generated SQL statements may not conform to the actual database structure, leading to incorrect query execution; or the generated queries may deviate from the user's intent, failing to accurately obtain the required "metric" information. Furthermore, many current solutions lack verification mechanisms for the generated SQL results, making it difficult to correct field errors or semantic inconsistencies in the model's output queries in a timely manner, reducing system reliability. To address these issues, the industry has proposed several improvements, such as incorporating database schema information for schema alignment during SQL generation, or employing few-shot learning to improve the relevance of model generation by providing examples in prompts. However, these improvements are typically used independently, lacking a universal approach that combines few-sample examples, high-level language models, and result consistency checks. Furthermore, existing techniques rarely consider enabling the system to learn from newly generated correct queries to continuously improve the performance of subsequent query generation. Therefore, it is necessary to propose a novel technical solution that comprehensively utilizes a small number of training examples, formatting hints, and consistency alignment mechanisms to significantly improve the accuracy of large-model text-to-SQL generation and enable the system to continuously improve itself. Summary of the Invention

[0003] To address the aforementioned technical challenges, this invention provides an intelligent query method for large-scale model metrics, overcoming the shortcomings of existing text-to-SQL generation methods, such as low accuracy in small-sample scenarios, lack of result verification, and insufficient self-improvement capabilities. It utilizes a limited amount of example data to guide a large language model in generating accurate SQL queries, and ensures that the generated queries match the user's question and database schema through consistency alignment checks, thereby significantly improving query generation accuracy. Furthermore, this invention employs a feedback mechanism that allows the system to use each successfully generated query as a new learning sample, progressively enhancing the model's adaptability and scalability across different databases and question domains. Even with scarce training data, it enables the pre-trained large language model to accurately understand user intent and generate effective SQL queries, continuously improving performance through automatic verification and ongoing learning mechanisms.

[0004] The technical solution of this invention is:

[0005] A method for intelligent question-and-answer querying of large-scale model metrics includes the following steps: Constructing an auxiliary information database containing target database structure information and historical question-and-answer examples; performing structured formatting on the acquired natural language questions, extracting the entities, fields, and query conditions involved; using the example data in the auxiliary information database to provide few-sample hints to the large language model, guiding it to generate a SQL-like intermediate expression and multiple SQL query candidate statements corresponding to the natural language questions; performing consistency alignment verification on the SQL query candidates, selecting the optimal SQL query that is consistent with the semantics of the natural language questions and the database structure; outputting the optimal SQL query and storing it along with the corresponding natural language questions in the auxiliary information database as new example data for self-learning reinforcement.

[0006] Furthermore,

[0007] The auxiliary information base contains schema metadata of the target database, including the names of one or more tables, the field names and types of each table, the relationships between tables, and pre-collected example pairs of natural language questions and corresponding SQL queries, which are used to provide domain context and reference examples for the large language model.

[0008] Furthermore,

[0009] The steps for structuring and formatting natural language questions include: using predefined parsing rules or named entity recognition technology to identify the entity names, constraint values, and target fields involved in the question text input by the user, and determining the corresponding database tables and their relationships to generate a structured representation of the query intent.

[0010] Furthermore,

[0011] The step of using examples from the auxiliary information base to provide few-shot prompts to guide the large language model in generating queries includes: selecting several natural language questions and their corresponding SQL examples that are semantically related to the current question from the auxiliary information base as prompt samples, inputting them into the context of the large language model, so that the model can generate a SQL-like expression corresponding to the natural language question based on the prompt samples, thereby generating an abstract syntax tree, and outputting multiple SQL query candidate schemes according to the characteristics of the target database.

[0012] Furthermore,

[0013] The consistency alignment check includes a matching check on the components of the candidate SQL query: verifying whether the data tables and fields in the candidate SQL correspond to the entities and indicators mentioned in the natural language question, whether the aggregate functions or filtering conditions used in the candidate SQL meet the intent requirements of the user's question, performing a syntax check, and whether the data source targeted by the candidate SQL belongs to the target database range in the auxiliary information base, thereby filtering out semantically inconsistent or unexecutable query candidates, selecting the SQL statement that meets all alignment conditions as the final output result, and recording the error information in the filtered query candidates as counterexamples stored in the expanded example dataset.

[0014] Furthermore,

[0015] After outputting the optimal SQL query, the SQL query and its corresponding natural language question pair are stored in the auxiliary information database to expand the example dataset. The large language model can then use the newly added examples for reference during subsequent query generation, thereby continuously optimizing the accuracy of query generation and achieving self-learning enhancement of the system.

[0016] Furthermore,

[0017] The consistency alignment check also includes field alignment: checking whether the tables and fields used in each candidate SQL match the entities and metrics requirements mentioned in the natural language question, and whether relevant fields are omitted or misused. The check also calculates whether the set of columns F(Si) used in the candidate query matches the expected set of columns F in the question. ref The degree of matching. A field alignment score F can be defined. field (Si) is:

[0018] ,

[0019] Its value range is [0,1], indicating the proportion of the target columns that the candidate SQL correctly includes. If the candidate references a column that is not mentioned or does not exist in the question (an incorrect column), then F... field Decrease. Also check if any key columns have been missed.

[0020] Operation alignment: Check whether the aggregate functions, sorting, filtering conditions, and other operations used in the candidate SQL statements are consistent with the user's question. Figure 1 For example, if a user's question requests statistical summaries, the correct aggregate function should be used in the SQL; if the question contains filtering conditions, the WHERE clause in the SQL should correctly reflect those conditions. Define the operation alignment score F. op (Si) represents the similarity between the set of operations used by the candidate and the set of operations expected by the problem. This can be calculated, for example, by the ratio of the number of matches to the expected number of operations. If the problem requires "summarizing," but the candidate uses an incorrect aggregation function or does not aggregate, then F... op It will be lower.

[0021] Dataset alignment: Verify that the database tables (or views) targeted by the SQL queries are correct, ensuring that the selected data source is consistent with the data domain involved in the user's problem. For example, when the auxiliary information base provides schema information for multiple tables, it is necessary to confirm that the SQL extracts data from the correct tables or joins the corresponding tables. Define a table alignment score F. table (Si) is set to 1 when the selected candidate table matches the problem domain, and 0 otherwise. Further verification can be performed for cases involving multiple table joins to confirm the correct JOIN relationship.

[0022] The consistency alignment mechanism can score each candidate SQL statement using rule validation and scoring. These alignment scores can be combined and weighted to obtain a comprehensive consistency score. The SQL statement with the highest score is selected as the optimal result based on the degree of matching. The weights $w_1$, $w_2$, and $w_3$ can be set according to actual needs.

[0023] .

[0024] The beneficial effects of this invention are:

[0025] Few-shot efficient learning: This invention utilizes a few-shot learning mechanism to guide a large language model in generating SQL queries. It significantly improves the model's understanding and generation capabilities for new natural language questions with only a small number of example question-answer pairs, reducing reliance on large-scale training data. This design allows the system to quickly adapt to new domains or databases, requiring only a very limited number of examples for deployment.

[0026] Structured hints enhance understanding: By formatting natural language questions in a structured way, extracting the entities, fields, and conditions involved, the model can obtain a clearer representation of the question than the original unprocessed text. This formatted hint helps to eliminate ambiguity, enabling the large model to more accurately understand the user's intent and thus generate SQL queries that better match the requirements.

[0027] Multiple Candidate Generation and Optimization: The method of this invention does not rely on a single path output, but instead allows the model to generate multiple query candidates and optimizes them through a consistency alignment mechanism. This diversified generation strategy increases the probability of finding the correct query, while automatic validation filters out candidates that do not meet semantic requirements, ensuring that the final output query statement is correct.

[0028] Consistency alignment ensures accuracy: The designed consistency alignment mechanism rigorously verifies the generated SQL candidates from multiple aspects, including fields, functions, and data sources, comparing them one by one with the user's question and database schema. This mechanism effectively prevents potential misuse or omission of knowledge in the model, ensuring that the output SQL is consistent with expectations in both semantics and syntax. Therefore, the SQL queries generated by this invention have higher accuracy and can be directly used to query the database to obtain the required indicator data, significantly improving reliability.

[0029] Self-learning and continuous improvement: By storing each successfully generated natural language question and its corresponding SQL pair in an auxiliary information base, the system's knowledge base is continuously enriched. Over time, the number of examples the model can refer to increases, expanding the coverage of various queries, thereby improving confidence and accuracy in generating SQL for new questions. This self-learning enhancement mechanism makes the system increasingly intelligent with use, gradually reducing the error rate and possessing strong continuous evolution capabilities without frequent manual intervention or model retraining.

[0030] Universality and Scalability: The method of this invention has universal applicability and does not rely on specific databases or data from fixed domains. Through the guidance and adaptive expansion of the auxiliary information database, the system can be flexibly applied to different database environments and business scenarios. Whether used for indicator analysis queries in business intelligence or database retrieval in a natural language interface, this method can be quickly deployed at a low cost and provide accurate query generation services.

[0031] In summary, this invention significantly improves the performance of large models in text-to-SQL tasks by innovatively combining few-sample examples, format guidance, and result alignment verification. It not only ensures the correctness of query results but also enables the system to self-evolve, demonstrating significant practical value and broad application prospects. Attached Figure Description

[0032] Figure 1 This is a schematic diagram of the workflow of the present invention. Detailed Implementation

[0033] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, 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 some embodiments of the present invention, but not all embodiments. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without creative effort are within the scope of protection of the present invention.

[0034] This invention provides an intelligent query method for large model indexes, such as... Figure 1 The diagram illustrates the complete process from natural language input to SQL query output and self-learning feedback. First, the user's natural language question is input into the system. The parsing module extracts structured information such as entity names, fields, and conditions. This information, along with relevant examples stored in the auxiliary information base, is then fed into the large language model for processing. Guided by a few sample examples, the large language model generates multiple candidate SQL-like query statements. These candidate queries are then validated and compared using a consistency alignment mechanism, selecting the SQL statement that best matches the user's intent and database schema as the final output. Finally, the system saves the optimal SQL statement and its corresponding natural language question pair back into the auxiliary information base as new learning samples to strengthen subsequent query generation processes. This closed-loop design ensures that the system can continuously optimize itself to improve the accuracy of query generation.

[0035] Specifically, the steps include the following:

[0036] Constructing an auxiliary information repository: An auxiliary information repository is established to store prior knowledge and example data related to the target database. This repository includes, but is not limited to, the database's structured metadata (such as table names, field names, field types, relationships, etc.) and several historical question-and-answer examples and their corresponding SQL queries. The auxiliary information repository provides the necessary context and reference for the large language model, enabling it to understand the database's schema and typical query methods.

[0037] Structured processing of natural language queries: After obtaining the user's input natural language query, the query is first preprocessed and parsed, transforming it into a structured representation. Specifically, using predefined parsing rules or named entity recognition technologies, key information such as potential entity names, constraint values, target indicator fields, and database column names are extracted from the query text, along with the type of operation involved in the user's query (such as filtering conditions, aggregation types, etc.). Through this formatting process, unstructured natural language is transformed into structured information containing entity and field correspondences, facilitating subsequent generation processes.

[0038] Few-shot example-guided query generation: The structured information mentioned above is combined with relevant content from the auxiliary information base and input into the large language model to generate preliminary query expressions. Specifically, a few-shot learning approach is used to guide the large model in generating SQL: for example, several natural language questions similar to the current problem and their SQL example pairs are selected from the auxiliary information base and added to the model input as prompt examples, forming a few-shot prompt. Guided by the prompt examples and structured information, the large language model first generates a SQL-like expression (an intermediate representation) that is close to SQL syntax, then generates an abstract syntax tree and validates the abstract syntax tree; then, it traverses the abstract syntax tree and generates multiple possible SQL query statements as candidate outputs based on the characteristics of the target database. This step leverages the powerful natural language generation capabilities of the large model and provides formatted examples to enable it to produce grammatically reasonable query candidates even in a small data environment.

[0039] Consistency Alignment Mechanism Filtering: For the multiple candidate SQL queries generated above, a consistency alignment mechanism is designed and executed to select the final query that best matches the user's intent and the actual database conditions. This mechanism verifies and compares the candidate SQL queries from the following aspects:

[0040] Field alignment: Check whether the tables and fields used in each candidate SQL match the entities and metrics required by the natural language question, whether any relevant fields are omitted or misused, and calculate the set of columns used in the candidate query F(Si) and the set of columns expected in the question F. ref The degree of matching. A field alignment score F can be defined. field (Si) is:

[0041] ,

[0042] Its value range is (-1, 1], indicating what proportion of the target columns the candidate SQL correctly includes. Furthermore, if the candidate references a column not mentioned or not present in the question (an erroneous column), then F... field (Si) decreases. Simultaneously check if any key columns have been missed.

[0043] Operation alignment: Check whether the aggregate functions, sorting, filtering conditions, and other operations used in the candidate SQL statements are consistent with the user's question. Figure 1 For example, if a user's question requests statistical summaries, the correct aggregate function should be used in the SQL; if the question contains filtering conditions, the WHERE clause in the SQL should correctly reflect those conditions. Define the operation alignment score F. op (Si) represents the similarity between the set of operations used by the candidate and the set of operations expected by the problem. This can be calculated, for example, by the ratio of the number of matches to the expected number of operations. If the problem requires "summarizing," but the candidate uses an incorrect aggregation function or does not aggregate, then F...op (Si) will be lower.

[0044] Dataset alignment: Verify that the database tables (or views) targeted by the SQL queries are correct, ensuring that the selected data source is consistent with the data domain involved in the user's problem. For example, when the auxiliary information base provides schema information for multiple tables, it is necessary to confirm that the SQL extracts data from the correct tables or joins the corresponding tables. Define a table alignment score F. table (Si) is set to 1 when the selected candidate table matches the problem domain, and 0 otherwise. Further verification can be performed for cases involving multiple table joins to confirm the correct JOIN relationship.

[0045] The consistency alignment mechanism can score each candidate SQL statement using rule validation and scoring. These alignment scores can be combined and weighted to obtain a comprehensive consistency score. The SQL statement with the highest score is selected as the optimal result based on the degree of matching. The weights $w_1$, $w_2$, and $w_3$ can be set according to actual needs.

[0046] ,

[0047] Candidates that are clearly inconsistent, such as referencing non-existent fields or omitting key conditions, are eliminated. This alignment and filtering process significantly reduces semantic or syntactic biases that might be introduced by large language models, ensuring that the output SQL query is highly semantically consistent with the input question and can be correctly executed on a real database.

[0048] Result Feedback Self-Learning: The final selected correct SQL query and its corresponding original natural language question are stored back into the auxiliary information database, accumulating as new example data. Through this result feedback mechanism, the method of this invention can continuously enrich the question-answer pair content of the auxiliary information database. As the system processes more questions, the auxiliary information database will accumulate more and more high-quality question-answer examples. When the large language model faces similar new questions, it can refer to more instances, thereby achieving self-learning enhancement. This incremental learning does not require retraining the large language model, but improves the overall system performance by expanding the example database, ensuring that the method has good scalability and continuous improvement capabilities.

[0049] In summary, the method of this invention combines techniques such as few-shot hints, structured parsing, and result consistency verification, enabling the effective generation of accurate SQL queries even with limited training data. Its core lies in providing contextual examples to guide the generation of large models through an auxiliary information database, and automatically verifying model output using a consistency alignment mechanism, thereby significantly improving the reliability and accuracy of text-to-SQL conversion. The final feedback learning stage further ensures that the system becomes increasingly intelligent, accumulating experience and continuously optimizing its performance over time.

[0050] The above description is merely a preferred embodiment of the present invention and is used only to illustrate the technical solution of the present invention, and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention are included within the scope of protection of the present invention.

Claims

1. A method for intelligent querying of large-scale model indicators, characterized in that, Includes the following steps: Construct an auxiliary information database containing target database structure information and historical question-and-answer examples; The acquired natural language questions are processed in a structured and formatted manner to extract the entities, fields, and query conditions involved. Using the example data in the auxiliary information database, the large language model is given few-sample prompts to guide it in generating SQL-like intermediate expressions and several SQL query candidate statements corresponding to natural language problems; Consistency alignment checks are performed on the candidate SQL queries to select the optimal SQL query that is consistent with the semantics of the natural language problem and the database structure. The optimal SQL query is output and stored in the auxiliary information database along with the corresponding natural language question, serving as new example data for self-learning enhancement. The steps for providing few-shot suggestions to a large language model using examples from an auxiliary information base to guide it in generating queries include: Several natural language questions and their corresponding SQL examples that are semantically related to the current problem are selected from the auxiliary information library as prompt samples and input into the context of the large language model so that the model can generate the SQL-like expression corresponding to the natural language question based on the prompt samples, thereby generating an abstract syntax tree, and outputting several SQL query candidate schemes according to the characteristics of the target database. The consistency alignment check includes a matching degree check on the components of the candidate SQL query. Specifically, it includes: verifying whether the data tables and fields in the candidate SQL correspond to the entities and indicators mentioned in the natural language question, whether the aggregate functions or filtering conditions used in the candidate SQL meet the intent requirements of the user's question, performing a syntax check, and whether the data source targeted by the candidate SQL belongs to the target database range in the auxiliary information base, thereby filtering out semantically inconsistent or unexecutable query candidates, selecting the SQL statement that meets all alignment conditions as the final output result, and recording the error information in the filtered query candidates as a counterexample to be stored in the expanded example dataset. The consistency alignment check also includes field alignment: checking whether the tables and fields used in each candidate SQL match the entities and metrics requirements mentioned in the natural language question, and whether relevant fields are omitted or misused. The check also calculates whether the set of columns F(Si) used in the candidate query matches the expected set of columns F in the question. ref The degree of matching; Define field alignment score F field (Si) is: , Its value range is [0,1], indicating what proportion of the target columns the candidate SQL correctly includes; if the candidate references a column that is not mentioned or does not exist in the question (i.e., an erroneous column), then F... field Decrease; at the same time check if any key columns have been missed; The consistency alignment mechanism can score each candidate SQL statement for consistency through rule verification and scoring. The alignment scores can be combined and weighted to obtain a comprehensive consistency score. The SQL statement with the highest score is selected as the optimal result based on the degree of matching. The weights w1, w2, and w3 can be set according to actual needs. ; If there are candidates that are clearly inconsistent, they are removed.

2. The method according to claim 1, characterized in that, The auxiliary information base contains schema metadata of the target database, including the names of one or more tables, the field names and types of each table, the relationships between the tables, and pre-collected example pairs of natural language questions and corresponding SQL queries, which are used to provide domain context and reference examples for the large language model.

3. The method according to claim 1, characterized in that, The natural language query is processed in a structured and formatted manner. After obtaining the natural language query question input by the user, the question is first preprocessed and parsed to convert it into a structured representation. Specifically, using predefined parsing rules or named entity recognition technology, the entity names, constraint values ​​and target fields involved are identified from the question text input by the user, and the corresponding database tables and their relationships are determined to generate a structured query intent representation.

4. The method according to claim 1, characterized in that, After outputting the optimal SQL query, the SQL query and its corresponding natural language question pair are stored in the auxiliary information database to expand the example dataset. The large language model uses the newly added examples for reference in the subsequent query generation process, thereby continuously optimizing the accuracy of query generation and realizing the self-learning enhancement of the system.

Citation Information

Patent Citations

  • Large model SQL (Structured Query Language) generation method integrating few-sample prompt and multi-choice mechanism

    CN119088818A

  • Database query generation method and system based on multi-selection optimization and storage medium

    CN119690987A