Question and answer pair generation method and device, electronic equipment, medium and product
Patent Information
- Application Number
- CN202610724419.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-05-25
- Publication Date
- 2026-08-18
AI Technical Summary
然而,这种方式仅依赖提示词引导,缺乏对数据库底层结构约束的利用,生成的SQL语句常存在语法错误或语义冲突,得到的问答对质量参差不齐且大量不可用,从而降低了模型训练的效率
[0019] Fourthly, this application provides a readable storage medium storing a computer program, which, when executed by a processor, performs the question-answer pair generation method described in any one of the first aspects.
Smart Images

Figure CN122594318A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing technology, specifically to a question-and-answer pair generation method, apparatus, electronic device, readable storage medium, and computer program product. Background Technology
[0002] In the training process of natural language to SQL models, a large number of paired questions and SQL statements are needed as training data to support the model in understanding user queries and generating correct queries. Existing technologies typically synthesize training samples by directly generating them based on large language models. This involves using the large language model to directly output corresponding SQL statements and natural language questions based on database table structure prompts. However, this approach relies solely on prompts and lacks utilization of the underlying database structure constraints. The generated SQL statements often contain syntactic errors or semantic conflicts, resulting in inconsistent question-answer pairs, many of which are unusable, thus reducing the efficiency of model training. Summary of the Invention
[0003] In view of the above problems, this application provides a question-and-answer pair generation method, apparatus, electronic device, readable storage medium, and computer program product, which can solve the problem that the generated question-and-answer pairs are of inconsistent quality and a large number are unusable, thereby reducing the efficiency of model training.
[0004] Firstly, this application provides a question-answer pair generation method, including: Construct a foreign key relationship graph based on the metadata of the target database; Based on the foreign key relationship diagram, generate multiple executable SQL statements; Based on the metadata and the plurality of executable SQL statements, generate a plurality of semantic natural language questions that correspond one-to-one with the plurality of executable SQL statements; The multiple executable SQL statements are paired with the multiple semantic natural language questions to obtain multiple question-answer pairs.
[0005] In the above technical solution, the method can first sort out the data table association logic by constructing a foreign key relationship graph, laying a solid structural foundation for SQL generation; then rely on foreign key relationships to generate compliant and executable SQL statements, avoiding syntax and logic errors; at the same time, combine metadata generation with semantic issues of accurate matching of SQL query intent; finally, through pairing, obtain question-answer pairs with complete intent matching and accurate correspondence, thereby generating high-quality, highly matched text to SQL training data in batches without manual annotation.
[0006] In some implementations, the metadata is obtained based on the database standard system tables of the target database; The metadata includes multiple data tables and the foreign key constraints between the multiple data tables; The foreign key relationship graph is a directed relationship graph with the multiple data tables as nodes and the foreign key constraints as edges.
[0007] In the above technical solution, the method can accurately obtain standardized metadata based on the standard database system tables, clearly define the relationship between data tables and foreign key constraints, and intuitively and rigorously represent the relationship structure between tables through a directed relationship graph, providing reliable and standardized data structure support for subsequent SQL generation and question-answering pair construction.
[0008] In some implementations, generating multiple executable SQL statements based on the foreign key relationship graph includes: The foreign key relationship graph is traversed using a graph traversal algorithm to obtain multiple valid connection paths; Obtain multiple associated fields that correspond one-to-one with the multiple valid connection paths; Based on the multiple associated fields, multiple executable SQL statements are generated; wherein, the executable SQL statements include at least one of the following: SELECT clause, WHERE clause, GROUP BY clause, ORDER BY clause, and LIMIT clause.
[0009] In the above technical solution, the method can accurately obtain the legal connection path between tables through graph traversal algorithm, and then automatically generate executable SQL containing complete query clauses based on the corresponding related fields, thereby achieving the batch generation of standardized SQL while ensuring the syntactic legality and logical validity of the SQL statement.
[0010] In some implementations, obtaining the multiple associated fields corresponding one-to-one with the multiple valid connection paths includes: Based on the foreign key constraint relationship, determine multiple JOIN conditions that correspond one-to-one with the multiple legal connection paths; Obtain a set of multiple associated data tables that correspond one-to-one with the multiple valid connection paths; Based on the multiple sets of related data tables, obtain multiple SELECT fields that correspond one-to-one with the multiple valid connection paths; Obtain the multiple field types that correspond one-to-one with the multiple sets of associated data tables; Generate multiple WHERE operators that correspond one-to-one with the multiple valid connection paths based on the multiple field types; The associated fields include the JOIN condition, the SELECT field, and the WHERE operator.
[0011] In the above technical solution, the method can accurately determine the JOIN conditions corresponding to the legal join path based on the foreign key constraint relationship, obtain the corresponding related data table set and SELECT fields, and generate matching WHERE operators by combining the field types, thus fully constructing the various related fields required by the SQL.
[0012] In some implementations, generating multiple semantic natural language questions corresponding one-to-one with the multiple executable SQL statements based on the metadata and the multiple executable SQL statements includes: The metadata and the multiple executable SQL statements are aggregated to obtain aggregated data; Generate a natural language summary of the target database based on the aggregated data; Using the natural language summary as the global context, for each of the multiple executable SQL statements, generate multiple semantic natural language questions that correspond one-to-one with the multiple executable SQL statements.
[0013] In the above technical solution, the method can effectively improve the business relevance and semantic rationality of the generated natural language questions, ensure a high degree of matching between the questions and the SQL query intent, enhance the contextual coherence of the questions, and significantly improve the semantic consistency and training usability of the question-answer pairs.
[0014] In some implementations, generating a natural language summary of the target database based on the aggregated data includes: Information is extracted from the aggregated data to obtain target data; wherein, the target data includes core business entities, key analysis dimensions, and typical query patterns; Generate a business profile of the target database based on the target data; Based on the business profile, text is generated to obtain a natural language summary.
[0015] In the above technical solution, the method can accurately extract the core business information of the database, construct a business profile that fits the actual business scenario, and thus generate a standardized natural language summary that fits the business logic.
[0016] Secondly, this application provides a question-and-answer pair generation apparatus, comprising: Building units are used to construct foreign key relationship graphs based on the metadata of the target database; The first generation unit is used to generate multiple executable SQL statements based on the foreign key relationship diagram; The second generation unit is used to generate multiple semantic natural language questions that correspond one-to-one with the multiple executable SQL statements based on the metadata and the multiple executable SQL statements; The pairing unit is used to pair the plurality of executable SQL statements with the plurality of semantic natural language questions to obtain a plurality of question-answer pairs.
[0017] In the above technical solution, the device can first organize the data table association logic by constructing a foreign key relationship graph, laying a solid structural foundation for SQL generation; then rely on the foreign key relationship to generate compliant and executable SQL statements, avoiding syntax and logic errors; at the same time, combine metadata generation with semantic issues of accurate matching of SQL query intent; finally, through pairing, obtain question-answer pairs with complete intent matching and accurate correspondence, thereby generating high-quality, highly matched text to SQL training data in batches without manual annotation.
[0018] Thirdly, this application provides an electronic device including a memory and a processor, the memory storing a computer program, and the processor running the computer program to cause the electronic device to perform the question-and-answer pair generation method described in any one of the first aspects.
[0019] Fourthly, this application provides a readable storage medium storing a computer program, which, when executed by a processor, performs the question-answer pair generation method described in any one of the first aspects.
[0020] Fifthly, this application provides a computer program product comprising a computer program that, when executed by a processor, performs the question-and-answer pair generation method described in any one of the first aspects.
[0021] The beneficial effects of this application are as follows: it can generate training data by traversing foreign key relationship graphs, ensuring a balanced distribution of multi-table query samples from simple to complex, thereby improving the model's generalization ability to complex queries; it can also generate SQL statements through prior constraints, ensuring that all SQL syntax is correct and directly executable, avoiding invalid samples, and improving the stability of model output; it can also generate natural language questions through business summaries, ensuring that the questions and SQL query intent are perfectly matched, thereby improving the accuracy of model parsing; it can also automatically generate data adapted to different industry databases through the entire process, avoiding manual annotation and significantly reducing data production costs and migration difficulties; and it can generate training data adapted to different training stages and with adjustable difficulty through a hierarchical and controllable approach, thereby improving model learning efficiency and training effect. Attached Figure Description
[0022] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0023] Figure 1 This is a flowchart illustrating the question-and-answer pair generation method in some embodiments of this application; Figure 2 This is a schematic diagram of the structure of the question-and-answer pair generation device in some embodiments of this application; Figure 3 This is a schematic diagram of the structure of an electronic device in some embodiments of this application. Detailed Implementation
[0024] The embodiments of the technical solution of this application will now be described in detail with reference to the accompanying drawings. These embodiments are only used to more clearly illustrate the technical solution of this application and are therefore merely examples, and should not be used to limit the scope of protection of this application.
[0025] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains; the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the application; the terms “comprising” and “having”, and any variations thereof, in the specification, claims, and foregoing description of the drawings are intended to cover non-exclusive inclusion.
[0026] In the description of the embodiments of this application, technical terms such as "first" and "second" are used only to distinguish different objects and should not be construed as indicating or implying relative importance or implicitly specifying the number, specific order, or primary and secondary relationship of the indicated technical features. In the description of the embodiments of this application, "multiple" means two or more (including two), similarly, "multiple sets" refers to two or more sets (including two sets), and "multiple pieces" refers to two or more pieces (including two pieces) unless otherwise explicitly defined.
[0027] In this document, the term "embodiment" means that a particular feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.
[0028] In the description of the embodiments in this application, the term "and / or" is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, the character " / " in this document generally indicates that the preceding and following related objects have an "or" relationship.
[0029] To address the numerous shortcomings of existing NL2SQL (Natural Language to SQL) question-answering pair generation methods, such as high manual annotation costs, non-executable automatically generated SQL, low semantic matching between natural language questions and SQL, missing data in complex multi-table queries, uncontrollable generation process, and poor cross-domain adaptability, this application proposes an automated question-answering pair generation method.
[0030] This method leverages native database metadata and foreign key relationships to establish rigorous inter-table join rules, thereby standardizing SQL generation logic and avoiding invalid or erroneous SQL statements. Simultaneously, it constructs a unified semantic context based on the overall business information of the database, resolving the mismatch between natural language questions and SQL query intent. Ultimately, it achieves fully automated, high-quality question-answer pair generation, reduces dataset construction costs, and improves the training performance of the NL2SQL model.
[0031] like Figure 1 As shown, some embodiments of this application provide a question-and-answer pair generation method, which includes: S100. Construct a foreign key relationship graph based on the metadata of the target database.
[0032] In this embodiment, the metadata is obtained based on the database standard system tables of the target database; Metadata includes multiple data tables and foreign key constraints between them; A foreign key graph is a directed graph with multiple data tables as nodes and foreign key constraints as edges.
[0033] In this embodiment, a foreign key relationship graph refers to a directed graph constructed with database tables as nodes and foreign key constraints between tables as directed edges. It is used to intuitively and structurally represent the relationship and dependency between all tables in a database.
[0034] In this embodiment, the method constructs a directed graph structure with data tables as nodes and foreign key relationships as edges, thereby clearly outlining the relationship logic between database tables and providing a structural foundation for subsequent legal table combination traversal.
[0035] S200. Generate multiple executable SQL statements based on the foreign key relationship diagram.
[0036] In this embodiment, SQL (Structured Query Language) statements refer to standard instructions used to query and manipulate database data.
[0037] In this embodiment, the method adopts an SQL generation strategy based on executability prior constraints, placing executability constraints in advance during the SQL generation stage. It achieves structural instantiation of the closed domain through foreign key graph constraints, eliminating syntax errors and semantic conflicts from the source and ensuring the executability of the generated SQL.
[0038] S300: Based on metadata and multiple executable SQL statements, generate multiple semantic natural language questions that correspond one-to-one with the multiple executable SQL statements.
[0039] In this embodiment, the method can adopt an innovative architecture with schema digest (or database structure digest) as the semantic intermediate layer. A digest intermediate layer is added between SQL generation and question generation, and questions are generated based on the global business context to improve the semantic consistency between questions and SQL.
[0040] In this embodiment, the schema summary is used to integrate and refine all data tables, field information, relationships between tables, and business meanings in the database into a concise text summary, which serves as a unified semantic background to assist in generating natural language questions that match the meaning of SQL.
[0041] S400: Pair multiple executable SQL statements with multiple semantic natural language questions to obtain multiple question-answer pairs.
[0042] In this embodiment, the method can pair executable SQL queries with semantic questions to form standardized NL2SQL training question-answer pairs. This process requires no manual intervention and automatically generates high-quality training data.
[0043] Implementing this method allows for the automatic extraction of metadata from database system tables, completing the entire process without manual intervention. Specifically, from foreign key graph construction, path traversal, and SQL instantiation to summary generation and question generation, each step is completed automatically by the system, requiring no human intervention whatsoever. The core essence of this method is transforming the presentation of domain knowledge from "manual encoding" to "automatic structure parsing."
[0044] Among them, foreign key relationships carry domain knowledge of table associations, field types carry domain knowledge of operational constraints, and data distribution carries domain knowledge of summary content.
[0045] Based on this, after accessing a new database, this method can automatically generate question-answer pairs within hours, and the generated summaries and questions can automatically adapt to the specific business domain of the database. For example, summaries for financial risk control scenarios emphasize core indicators such as "credit score" and "default probability," while summaries for e-commerce scenarios emphasize key dimensions such as "user profile" and "repurchase rate."
[0046] Through reasoning and verification, when the cost of data synthesis is reduced to negligible levels, it is possible to quickly build exclusive training data for specific business scenarios, breaking down the barriers to the migration of NL2SQL models from general-purpose to domain-customized models, and significantly improving the efficiency and adaptability of the model in actual business scenarios.
[0047] In the above embodiments, the method first organizes the data table association logic by constructing a foreign key relationship graph, laying a solid structural foundation for SQL generation; then it generates compliant and executable SQL statements based on foreign key relationships, avoiding syntax and logic errors; at the same time, it combines metadata generation with semantic issues of accurate matching of SQL query intent; finally, it obtains question-answer pairs with complete intent matching and accurate correspondence through pairing, thereby generating high-quality, highly matched text to SQL training data in batches without manual annotation.
[0048] In some embodiments, step S200 may include: S210. The graph traversal algorithm is used to traverse the foreign key relationship graph to obtain multiple valid connection paths.
[0049] In this embodiment, the method can adopt a foreign key-driven multi-table combination system traversal mechanism, and systematically enumerate all legal connection paths from a single table to five tables through a graph traversal algorithm.
[0050] Foreign key driven refers to a technology that uses predefined foreign key constraints between database tables as the sole basis to control and guide the relationship between tables and the generation of SQL statements. The multi-table combination system traversal mechanism refers to the implementation method of comprehensively, orderly, and without omission traversing all legal combinations of multiple data tables in the database according to preset rules; Graph traversal algorithms refer to algorithms that, in order to find all valid association paths, visit nodes and edges in a graph consisting of data tables and foreign key relationships, following predetermined path rules; examples include depth-first traversal and breadth-first traversal algorithms. Systematic enumeration refers to listing all objects or combinations that meet the conditions in a complete, orderly, non-repetitive, and non-omitted manner, according to unified rules and fixed logic. Single table to five tables refers to the range of queries that cover all combinations of queries that use only one data table up to five related data tables simultaneously. This is the typical complexity range for common business queries. Generally speaking, when making training samples, it is sufficient to cover single table to five tables by default (single table basic, multi-table advanced, complex analysis). This number is preset, and its preset basis is the number of commonly used tables that users query in the database in actual business. A valid join path refers to a table join relationship that is formed solely based on foreign key constraints, conforms to the database structure rules, and allows for normal execution of join queries.
[0051] In this embodiment, the method strictly uses foreign key constraints as the sole basis for table joins, thereby retaining only connected subgraphs with foreign key paths and avoiding illegal JOIN operations on unrelated tables.
[0052] Illegal JOIN operations refer to the forced concatenation and join of data tables in the database that do not have foreign key constraints or logical relationships in business logic. Such concatenation does not conform to the database structure rules, which will cause query logic errors, data distortion, and will not be able to generate executable SQL statements.
[0053] In this embodiment, the method can effectively improve the structural integrity and distribution uniformity of training data across multiple table dimensions by controlling the traversal depth and balancing computational complexity and path coverage.
[0054] The traversal depth refers to the upper limit of the number of data tables included in a single association combination when traversing the path in the foreign key relationship graph. This method limits it to a single table to five tables. Balanced computational complexity refers to limiting the maximum number of data tables that can be traversed, in order to avoid the problem of excessive computation and resource consumption caused by unlimited traversal of too many table combinations. Path coverage refers to the completeness of the path that can be traversed to include all data table associations that conform to foreign key constraints.
[0055] In this embodiment, the method can transform the generation of complex queries involving three or more tables from random attempts as in the prior art to a deterministic traversal based on foreign key relationships.
[0056] By implementing this method, a graph structure constructed using foreign key relationships can be used to ensure that any valid combination of n tables must have at least n-1 foreign key association edges to guarantee connectivity between tables. This constraint can both filter out invalid random table combinations without business associations and guarantee the complete traversal of all valid table combinations.
[0057] Based on this, the sample distribution of different table count levels (from single to five tables) in the training data can be precisely controlled by the traversal depth parameter, achieving gradient full coverage of query complexity from simple to complex.
[0058] Inference analysis shows that when the model learns on structurally balanced training data, its understanding of JOIN operations can generalize from memorizing specific table pairs to understanding the transitive properties of foreign key relationships, thereby improving its parsing generalization ability to new table combinations not seen during training.
[0059] S220. Obtain multiple associated fields that correspond one-to-one with multiple valid connection paths.
[0060] In this embodiment, the method can match and generate corresponding complete association fields for each legal connection path, thereby providing complete field and condition support for the standardized generation of SQL statements.
[0061] To fully construct all the required relational fields for the SQL, step S220 may include: S221. Determine multiple JOIN conditions that correspond one-to-one with multiple valid join paths based on foreign key constraints.
[0062] In this embodiment, the method can uniquely determine the JOIN condition of the corresponding join path based on the foreign key constraint relationship, ensuring the legality and validity of the join between tables and preventing illegal associations.
[0063] The JOIN condition refers to the correspondence between two tables established by foreign keys. For example, the equality matching rule is that the user ID in the user table and the user ID in the order table are equal.
[0064] S222. Obtain a set of multiple associated data tables that correspond one-to-one with multiple valid connection paths.
[0065] In this embodiment, the method can determine all related data tables participating in the current SQL query based on a valid connection path, thus clarifying the scope of the SQL query.
[0066] S223. Obtain multiple SELECT fields that correspond one-to-one with multiple valid connection paths based on multiple sets of related data tables.
[0067] In this embodiment, the method can select the corresponding SELECT field from the column space of the associated data table to limit the legal range of the query field and avoid field reference errors.
[0068] The SELECT field refers to the data columns selected from the data tables with established legal relationships, which are to be queried, statistically analyzed, and displayed. It is the basic data object that constitutes the structured query statement.
[0069] S224. Obtain multiple field types that correspond one-to-one with multiple sets of associated data tables.
[0070] In this embodiment, the data type of the corresponding field in the associated data table is extracted to provide a type basis for generating a valid WHERE condition operator in the future.
[0071] The WHERE operator is an operator used in database queries to compare the size, match equality, limit range, and make logical judgments on the values of data table fields, and is used to filter out data records that meet specified conditions.
[0072] S225. Generate multiple WHERE operators that correspond one-to-one with multiple valid connection paths based on multiple field types.
[0073] In this embodiment, a compatible WHERE operator is generated based on the data type of the field to ensure the type validity of the query conditions and avoid semantic conflicts.
[0074] In this embodiment, the field type compatibility and matching with the WHERE condition operator means matching the appropriate condition operator based on the data type characteristics of the data table field, thereby avoiding data type conflicts with operation rules and ensuring that the query statement is legal and executable.
[0075] S226. Combine the JOIN condition, SELECT field, and WHERE operator into a related field.
[0076] In this embodiment, the associated fields include the JOIN condition, the SELECT field, and the WHERE operator.
[0077] In this embodiment, the method can generate and integrate JOIN conditions, SELECT fields, and WHERE operators step by step to form a complete system of related fields, providing full legal constraints for SQL generation.
[0078] For example, suppose the database has two tables: a user table and an order table. JOIN condition: Join two tables using a common user ID, such as: User table_User ID = Order table_User ID; SELECT fields: Select the content to be queried, such as: username, order time, order amount. The WHERE operator is used to filter data: greater than, equal to, or within a certain time range, such as: order amount > 0, order time within a specified range.
[0079] Based on this, the method can generate these three parts step by step and then combine them together to obtain a complete and valid association field (i.e., query rule).
[0080] S230. Generate multiple executable SQL statements based on multiple related fields; wherein the executable SQL statements include at least one of the following: SELECT clause, WHERE clause, GROUP BY clause, ORDER BY clause, and LIMIT clause.
[0081] In this embodiment, the SELECT clause is the core component of the structured query statement, used to specify the data table fields to be queried, calculated, aggregated, and returned, and to clarify the output content of the query statement; The WHERE clause is used to set the filtering conditions for data records. It uses conditional operators to match, compare and make logical judgments on field values, and only retains data records that meet the constraint rules to participate in subsequent query calculations. The GROUP BY clause is used to group the query result set according to a specified field, grouping data records with the same field value into the same group, and using aggregate functions to achieve grouping statistics and summary calculations; The ORDER BY clause is used to sort the final query result set in ascending or descending order according to a specified field, thus standardizing the display order of the query results; The LIMIT clause is used to limit the maximum number of data records returned by a query, controlling the output size of the result set and avoiding the return of excessive data.
[0082] In this embodiment, the method can generate an executable SQL statement containing complete standard query clauses based on complete related fields and prior constraints, thereby realizing the generation of closed domains for constraint satisfaction and ensuring that the SQL syntax is correct, the semantics are legal, and it can be directly executed.
[0083] This implementation transforms SQL generation from the traditional "generate first, then verify, then filter" post-processing model to a "constraints satisfied first, then structure instantiated" pre-processing model. Specifically, the JOIN path is uniquely determined by foreign key constraints, SELECT query fields are selected from the set of fields in the associated tables, aggregate functions are adapted to match numeric fields, and the WHERE condition operator maintains compatibility with the data types of the corresponding fields. Essentially, this implementation narrows the SQL generation space from a globally open subset of syntax to a set of legally executable instances within the current database schema.
[0084] Based on this, each SQL statement generated by this method simultaneously satisfies the three attributes of syntactic correctness, semantic legality, and direct execution, achieving zero invalid sample output.
[0085] Inference analysis shows that when the training data is 100% executable, the NL2SQL model can learn a deterministic mapping relationship from natural language expressions to legal SQL structures, rather than identifying boundaries in a scenario where legal and illegal samples are mixed. This can significantly improve the stability of the model output and greatly reduce the query execution failure rate when the model is actually deployed.
[0086] In the above embodiments, the method can accurately obtain the legal connection path between tables through graph traversal algorithm, and then automatically generate executable SQL containing complete query clauses based on the corresponding related fields, thereby achieving the batch generation of standardized SQL while ensuring the syntactic legality and logical validity of the SQL statement.
[0087] In this embodiment, the layered decoupled architecture adopted by the method is based on the core technical mechanism that the SQL layer, summary layer, and question layer are independent and do not interfere with each other. The output of each layer serves as the input basis for the next layer, and each layer has an interface that can be intervened. Specifically, the method can limit the generation of only content related to four-table aggregate queries in the SQL layer, strengthen the descriptive power of the time dimension in the summary layer, and specify trend-based sentence templates in the question layer.
[0088] The essence of this mechanism is to decouple the data generation space into three independent orthogonal dimensions of complexity context, thereby supporting Cartesian product-style combined sampling to achieve multi-dimensional and multi-scenario data generation.
[0089] Based on this, the method can generate matching data distributions for different stages of NL2SQL model training, thereby achieving progressive training in a course-like manner. In the early stage of model training, simple single-table query data is generated to help the model establish basic semantic mapping relationships; in the middle stage of training, multi-table query data is generated to help the model learn the logic of inter-table association reasoning; in the later stage of training, complex analytical query data is generated to enable the model to master the relevant methods of nested aggregation.
[0090] Through reasoning and verification, it has been found that the model's learning efficiency reaches its optimal level when the difficulty distribution of the training data is dynamically matched with the model's learning ability. Therefore, this approach can avoid slow model convergence caused by simple data redundancy, and also avoid training oscillations caused by introducing complex data too early, thus enabling the model to achieve a higher benchmark score with the same number of training steps.
[0091] In some embodiments, step S300 may include: S310. Aggregate metadata and multiple executable SQL statements to obtain aggregated data.
[0092] In this embodiment, the method can aggregate information from database metadata and all generated SQL statements, integrate database structure information and full query logic, and provide a data foundation for subsequent business summary extraction.
[0093] S320. Generate a natural language summary of the target database based on the aggregated data.
[0094] In this embodiment, the method can extract core business information from the database based on aggregated data, thereby generating a global natural language summary that fits the business scenario and constructing a unified business semantic context.
[0095] To generate standardized, business-logic-aligned natural language summaries, step S320 may include: S321. Extract information from the aggregated data to obtain target data; the target data includes core business entities, key analysis dimensions, and typical query patterns.
[0096] In this embodiment, the method can extract core business entities, key analysis dimensions, and typical query patterns from aggregated data, refine the core business connotation of the database, and eliminate redundant information.
[0097] Among them, aggregated data refers to a comprehensive business data carrier formed by integrating database metadata, the structure information of each data table, and a batch-generated set of SQL statements. The core business entity of a database refers to the data table and key fields that can represent the main body of the database business and carry core business information. It is the basic object that constitutes the business description. Key analytical dimensions refer to feature fields that can be used to classify, group, and statistically analyze business data, including dimensional information such as time, category, region, and level that can be used as statistical basis; Typical query patterns refer to the query logic and statistical methods that frequently occur in daily business queries, including common query types such as single-table queries, multi-table join queries, grouping and aggregation queries, and range filtering queries.
[0098] In this embodiment, the method can summarize the business domain, business process and key business statistics of the database from the complex table structure and query logic; and at the same time filter out table structure fields and invalid query logic that are irrelevant to the core business, redundant and without analytical value, thereby retaining effective business semantic information.
[0099] S322. Generate a business profile of the target database based on the target data.
[0100] In this embodiment, the method can construct a dedicated business profile for the corresponding database based on the extracted core target data, and fully characterize the business attributes and query features of the database.
[0101] S323. Generate text based on the business profile to obtain a natural language summary.
[0102] In this embodiment, the method can generate coherent and standardized natural language summaries based on business profiles, transforming structured database information into a global business context that can be used for semantic generation.
[0103] S330. Using natural language summaries as the global context, generate multiple semantic natural language questions that correspond one-to-one with each of the multiple executable SQL statements for each of the multiple executable SQL statements.
[0104] In this embodiment, the method can use the generated business summary as a global semantic context to provide a unified business reference for the subsequent generation of natural language questions, thus avoiding question fragmentation and lack of context.
[0105] In this embodiment, the method can use a Large Language Model (LLM) to generate semantic natural language questions. The LLM can use a natural language summary as a global context, and based on this, generate a corresponding semantic natural language question for each executable SQL statement.
[0106] In this embodiment, the method can also parse the query structure type of executable SQL (statistics, grouping, conditional filtering, multi-table joins, etc.) by pre-setting multiple types of natural language question templates, then match the corresponding templates and inject the schema summary business context, table names, field names, and dimension vocabulary to automatically generate semantically matched natural language questions. It should be understood that this method still strictly follows the semantic alignment logic between natural language and SQL and still falls within the scope of NL2SQL question-answering standard construction.
[0107] In this embodiment, the method uses a global business summary as context and combines the query logic of a single SQL statement to generate semantic natural language questions that accurately match the SQL query intent and fit the business scenario. This allows the question generation to have a global semantic reference, rather than translating a single SQL statement in isolation, thereby improving the naturalness and semantic matching of the question expression.
[0108] This implementation method first aggregates all generated SQL statements to extract a business profile of the database, including core business entities, key analysis dimensions, and typical query patterns. This business summary is then used as context in the question generation process, ensuring that the generation of individual natural language questions is based on the global business context, rather than solely on isolated SQL structures. Essentially, this operation constructs a complete and coherent mapping link from structured query logic to business semantics and then to natural language expression.
[0109] Based on this, the natural language questions generated by this method can accurately match the query intent, constraints and computational logic of SQL, greatly improving the semantic consistency between the questions and SQL.
[0110] Inference analysis shows that on training data with high semantic alignment, the model can learn stronger semantic alignment signals. Expressions such as "average", "categories", and "annual trend" in natural language can form a stable correspondence with structures such as AVG, GROUPBY, and YEAR() in SQL statements, avoiding erroneous mappings caused by noisy samples and effectively improving the semantic parsing accuracy of the model.
[0111] In the above embodiments, the method can effectively improve the business relevance and semantic rationality of the generated natural language questions, ensure a high degree of matching between the questions and the SQL query intent, enhance the contextual coherence of the questions, and significantly improve the semantic consistency and training usability of question-answer pairs.
[0112] like Figure 2 As shown, some embodiments of this application provide a schematic diagram of a question-and-answer pair generation device. It should be understood that this device is related to... Figure 1 The method executed in the middle corresponds to the steps involved in the aforementioned method. The specific functions and effects of the device can be found in the description above. To avoid repetition, detailed descriptions are omitted here.
[0113] The question-and-answer pair generation device includes: Construction unit 510 is used to construct a foreign key relationship graph based on the metadata of the target database; The first generation unit 520 is used to generate multiple executable SQL statements based on the foreign key relationship diagram; The second generation unit 530 is used to generate multiple semantic natural language questions that correspond one-to-one with the multiple executable SQL statements based on metadata and multiple executable SQL statements. The pairing unit 540 is used to pair multiple executable SQL statements with multiple semantic natural language questions to obtain multiple question-answer pairs.
[0114] In some embodiments, the metadata is obtained based on the database standard system tables of the target database; Metadata includes multiple data tables and foreign key constraints between them; A foreign key graph is a directed graph with multiple data tables as nodes and foreign key constraints as edges.
[0115] In some embodiments, the first generation unit 520 includes: Traverse subunit 521, which is used to traverse the foreign key relationship graph using a graph traversal algorithm to obtain multiple valid connection paths; Get sub-unit 522, which is used to retrieve multiple associated fields that correspond one-to-one with multiple valid connection paths; The first generation subunit 523 is used to generate multiple executable SQL statements based on multiple related fields; wherein the executable SQL statements include at least one of the following: SELECT clause, WHERE clause, GROUP BY clause, ORDER BY clause, and LIMIT clause.
[0116] In some embodiments, the subunit 522 is specifically used to determine multiple JOIN conditions that correspond one-to-one with multiple legal connection paths based on the foreign key constraint relationship; Retrieve a set of multiple associated data tables that correspond one-to-one with multiple valid connection paths; Retrieve multiple SELECT fields that correspond one-to-one with multiple valid connection paths from multiple sets of related data tables; Retrieve multiple field types that correspond one-to-one with multiple sets of associated data tables; Generate multiple WHERE operators that correspond one-to-one with multiple valid join paths based on multiple field types; The associated fields include the JOIN condition, the SELECT field, and the WHERE operator.
[0117] In some embodiments, the second generation unit 530 includes: Aggregation subunit 531 is used to aggregate metadata and multiple executable SQL statements to obtain aggregated data; The second generation subunit 532 is used to generate a natural language summary of the target database based on the aggregated data; The second generation subunit 532 is also used to generate multiple semantic natural language questions corresponding one-to-one with each of the multiple executable SQL statements, using natural language summaries as the global context.
[0118] In some embodiments, the second generation subunit 532 is specifically used to extract information from the aggregated data to obtain target data; wherein, the target data includes core business entities, key analysis dimensions and typical query patterns; Generate a business profile for the target database based on the target data; Text is generated based on the business profile to obtain a natural language summary.
[0119] like Figure 3As shown, this application provides an electronic device 600, which includes a processor 601 and a memory 602. The processor 601 and the memory 602 are interconnected and communicate with each other through a communication bus 603 and / or other forms of connection mechanism (not shown). The memory 602 stores a computer program that can be executed by the processor 601. When the computing device is running, the processor 601 executes the computer program to perform the method in any of the aforementioned optional implementations.
[0120] This application provides a computer-readable storage medium storing a computer program, which, when executed by a processor, performs the method in any of the aforementioned optional implementations.
[0121] The computer-readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as Static Random Access Memory (SRAM), Electrically Erasable Programmable Read-Only Memory (EEPROM), Erasable Programmable Read Only Memory (EPROM), Programmable Red-Only Memory (PROM), Read-Only Memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.
[0122] This application provides a computer program product, which includes a computer program that, when run by a processor, executes the method in any of the aforementioned optional implementations.
[0123] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and not to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. These modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application, and they should all be covered within the scope of the claims and specification of this application. In particular, as long as there is no conflict, the various technical features mentioned in the embodiments can be combined in any way. This application is not limited to the specific embodiments disclosed herein, but includes all technical solutions falling within the scope of the claims.
Claims
1. A question-answer pair generation method, characterized in that, include: Construct a foreign key relationship graph based on the metadata of the target database; Based on the foreign key relationship diagram, generate multiple executable SQL statements; Based on the metadata and the plurality of executable SQL statements, generate a plurality of semantic natural language questions that correspond one-to-one with the plurality of executable SQL statements; The multiple executable SQL statements are paired with the multiple semantic natural language questions to obtain multiple question-answer pairs.
2. The question-answer pair generation method according to claim 1, characterized in that, The metadata is obtained based on the database standard system tables of the target database; The metadata includes multiple data tables and the foreign key constraints between the multiple data tables; The foreign key relationship graph is a directed relationship graph with the multiple data tables as nodes and the foreign key constraints as edges.
3. The question-answer pair generation method according to claim 1, characterized in that, The step of generating multiple executable SQL statements based on the foreign key relationship graph includes: The foreign key relationship graph is traversed using a graph traversal algorithm to obtain multiple valid connection paths; Obtain multiple associated fields that correspond one-to-one with the multiple valid connection paths; Based on the multiple associated fields, multiple executable SQL statements are generated; wherein, the executable SQL statements include at least one of the following: SELECT clause, WHERE clause, GROUP BY clause, ORDER BY clause, and LIMIT clause.
4. The question-answer pair generation method according to claim 3, characterized in that, The step of obtaining multiple associated fields that correspond one-to-one with the multiple legal connection paths includes: Based on the foreign key constraint relationship, determine multiple JOIN conditions that correspond one-to-one with the multiple legal connection paths; Obtain a set of multiple associated data tables that correspond one-to-one with the multiple legal connection paths; Based on the multiple sets of related data tables, obtain multiple SELECT fields that correspond one-to-one with the multiple valid connection paths; Obtain the multiple field types that correspond one-to-one with the multiple sets of associated data tables; Generate multiple WHERE operators that correspond one-to-one with the multiple valid connection paths based on the multiple field types; The associated fields include the JOIN condition, the SELECT field, and the WHERE operator.
5. The question-answer pair generation method according to claim 1, characterized in that, The step of generating multiple semantic natural language questions, each corresponding one-to-one with the multiple executable SQL statements, based on the metadata and the multiple executable SQL statements includes: The metadata and the multiple executable SQL statements are aggregated to obtain aggregated data; Generate a natural language summary of the target database based on the aggregated data; Using the natural language summary as the global context, for each of the multiple executable SQL statements, generate multiple semantic natural language questions that correspond one-to-one with the multiple executable SQL statements.
6. The question-answer pair generation method according to claim 5, characterized in that, The step of generating a natural language summary of the target database based on the aggregated data includes: Information is extracted from the aggregated data to obtain target data; wherein, the target data includes core business entities, key analysis dimensions, and typical query patterns; Generate a business profile of the target database based on the target data; Based on the business profile, text is generated to obtain a natural language summary.
7. A question-and-answer pair generation device, characterized in that, The question-and-answer pair generation device includes: Building units are used to construct foreign key relationship graphs based on the metadata of the target database; The first generation unit is used to generate multiple executable SQL statements based on the foreign key relationship diagram; The second generation unit is used to generate multiple semantic natural language questions that correspond one-to-one with the multiple executable SQL statements based on the metadata and the multiple executable SQL statements; The pairing unit is used to pair the plurality of executable SQL statements with the plurality of semantic natural language questions to obtain a plurality of question-answer pairs.
8. An electronic device, characterized in that, The electronic device includes a memory and a processor, the memory being used to store a computer program, and the processor running the computer program to cause the electronic device to perform the question-answer pair generation method according to any one of claims 1 to 6.
9. A readable storage medium, characterized in that, The readable storage medium stores a computer program, which, when executed by a processor, performs the question-answer pair generation method according to any one of claims 1 to 6.
10. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, performs the question-answer pair generation method according to any one of claims 1 to 6.