A method, system, storage medium, and computer equipment for generating Text-to-SQL based on multi-agent collaboration.

By employing a multi-agent collaborative approach, multiple semantic rewriting problems are generated and validated. Table column information is filtered and comprehensively scored, and the target SQL statement is finally output. This solves the problem of intent understanding and execution in complex queries in existing Text-to-SQL methods, and improves the accuracy and robustness of generated SQL.

CN122309546APending Publication Date: 2026-06-30SOUTH CHINA UNIV OF TECH

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
SOUTH CHINA UNIV OF TECH
Filing Date
2026-04-29
Publication Date
2026-06-30

AI Technical Summary

Technical Problem

Existing Text-to-SQL methods suffer from issues such as uncertainty in intent understanding, table column recall errors, and non-executability of generated results when handling complex queries, making it difficult to meet user intent and database constraints.

Method used

A multi-agent collaborative approach is adopted, which generates multiple semantic rewriting problems by rewriting agents, filters table and column information by agents, sorts agents for comprehensive scoring, generates candidate SQL statements, and verifies and corrects them by using the ReAct loop of the correcting agent, and finally outputs the target SQL statement.

Benefits of technology

It improves the accuracy and robustness of complex queries, reduces table column selection errors, enhances the executability and semantic consistency of generated SQL, and improves the user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122309546A_ABST
    Figure CN122309546A_ABST
Patent Text Reader

Abstract

This invention discloses a text-to-SQL generation method, system, storage medium, and computer device based on multi-agent collaboration. The method first employs a rewriting agent to generate multiple semantically rewritten questions from the input question to alleviate ambiguity; then, a filtering agent links the execution patterns of the original and rewritten questions, filtering relevant table column information; next, a sorting agent selects the top k question-database pattern information pairs through clustering and sorting mechanisms; then, a generation agent generates multiple candidate SQL statements based on these information pairs; finally, a correction agent executes and verifies the candidate SQL statements, and uses a ReAct mechanism based on the execution results for error diagnosis and logic repair, outputting the final SQL statement. The technical solution of this invention can effectively solve query processing problems in complex database environments, improving the accuracy and robustness of SQL generation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to language processing technology, specifically to a method, system, storage medium, and computer device for generating Text-to-SQL based on multi-agent collaboration. Background Technology

[0002] Text-to-SQL (TTO) tasks aim to automatically convert natural language questions into executable SQL queries, enabling users without SQL writing skills to access relational databases in a more intuitive way. It is widely used in data analysis, database question answering, and data query interfaces of various business systems. With the continuous growth of structured data across industries, TTO plays a crucial role in data analysis, automated question answering, and industrial database applications.

[0003] Early Text-to-SQL methods primarily relied on manually designed grammar rules and keyword matching. While offering strong determinism within specific, closed domains, they were heavily dependent on grammar rules and domain knowledge, lacking flexibility and generalization ability when facing complex queries (such as multi-table joins, nested queries, implicit aggregations, and time-range constraints). With the development of deep learning technology, end-to-end neural generative models emerged that directly "translate" natural language into SQL. However, they still have shortcomings in cross-domain transfer and complex constraint expression (such as multi-table joins, nested queries, implicit aggregations, and complex filtering conditions), and the stability and interpretability of the generated results are limited. In recent years, large language models such as GPT, Qwen, and DeepSeek have demonstrated outstanding performance in natural language understanding and code generation tasks, driving Text-to-SQL tasks towards a "no-fine-tuning" prompt engineering paradigm. This paradigm generates SQL statements under zero-sample or few-sample conditions by constructing prompt contexts, thereby reducing reliance on labeled corpora and model training.

[0004] However, existing Text-to-SQL methods based on large language models still face several challenges in practical implementation: First, natural language intent is often ambiguous and vague. Real user queries often contain unclear referents, ambiguous time ranges, or ambiguous aggregation criteria, leading to uncertainty in the system's understanding of user intent and making it difficult to accurately capture complex query constraints, thus affecting subsequent SQL generation. Second, the schema information of real enterprise databases often contains a large number of tables and columns with complex primary and foreign key relationships. When mapping natural language to the database structure, insufficient or incorrect recall of relevant table columns can easily lead to errors in field selection, join relationships, or filter condition settings, resulting in unexecutable SQL or results deviating from expectations. Third, in complex query scenarios, the types of errors in generated SQL are diverse and coupled, which may manifest as syntax errors or semantic deviations such as table column selection, join relationships, and filtering / aggregation constraints. This makes the generated results uncertain in terms of executability and semantic consistency, and the root cause of the error is often difficult to locate and distinguish, making it difficult for the generated results to consistently meet user intent constraints and database schema constraints. Summary of the Invention

[0005] In order to overcome the above-mentioned shortcomings and deficiencies of the prior art, the first objective of this invention is to provide a Text-to-SQL generation method based on multi-agent collaboration. This method improves the accuracy and robustness of text-to-SQL generation tasks and enhances the performance and error correction capabilities of LLM in handling complex database queries.

[0006] The second objective of this invention is to provide a Text-to-SQL generation system based on multi-agent collaboration.

[0007] A third objective of the present invention is to provide a storage medium.

[0008] A fourth objective of this invention is to provide a computer device.

[0009] The first objective of this invention is achieved through the following technical solution: This Text-to-SQL generation method based on multi-agent collaboration is characterized by comprising the following steps:

[0010] S1. A rewritten agent receives the original question and database pattern information, and generates multiple semantic rewriting questions through diversified prompting strategies.

[0011] S2. The filtering agent performs pattern linking on the original problem and the semantic rewriting problem, and filters relevant table and column information from the target database.

[0012] S3. Based on the filtered table and column information, the ranking agent performs noise reduction and uses a self-consistent voting mechanism combined with the confidence scores generated by the large language model to perform weighted calculations, obtain the comprehensive score of each candidate pair, and sort them, selecting the top k candidate pairs for output.

[0013] S4. Based on the k optimal candidate pairs, the agent generates K candidate SQL statements through a large language model.

[0014] S5. The corrective agent executes and verifies the candidate SQL statement through the database execution environment. Based on the execution verification feedback, it performs structured error diagnosis through the "think-act-observe" ReAct loop and combines real-time sampled data from the database for logical repair, finally outputting the target SQL statement.

[0015] Step S1 includes the following steps:

[0016] S11. Perform schema analysis and preprocessing on the target database to construct a simplified database structure description.

[0017] S12. Combine the original question with the database structure description information, and combine it with the preset deconstruction prompts to construct a task context to guide the large language model to perform ambiguity recognition.

[0018] S13. Drive the large language model to identify at least one type of ambiguity in the original problem, so as to generate n semantic rewriting problems with different focuses;

[0019] S14. Output a list of questions containing the original question and the semantic rewriting question.

[0020] Step S2 includes the following specific steps:

[0021] S21. Encode the original problem, each semantic rewriting problem, and database schema information using a vectorization model to generate semantic vectors;

[0022] S22. For the original question and each semantic rewriting question, calculate the cosine similarity between its semantic vector and the database pattern vector, filter out pattern items with semantic relevance higher than a preset threshold, and form a preliminary semantic recall set.

[0023] S23. For the original question and each semantic rewriting question, use fuzzy matching or sparse retrieval strategies to extract entity fragments in the question, compare them with the actual column values ​​in the database, identify potential attribute mapping relationships, and form a value retrieval recall set.

[0024] S24. The initial semantic recall set and the value retrieval recall set are merged, deduplicated, and noise is removed to select the final candidate table set and candidate column set, and output a database information list containing "problem - filtered database information".

[0025] Step S3 includes the following steps:

[0026] S31. Count the frequency of each candidate pattern set in the pattern linking results of each semantic rewriting problem, and calculate the self-consistency voting score.

[0027] S32. Calculate the matching reliability score for each candidate pair using a large language model, and then weight and sum the confidence score with the self-consistency voting score to obtain the comprehensive score for each candidate pair:

[0028]

[0029] in, , For candidate pattern set The number of times it appears in the pattern linking results of n semantic rewriting problems. The total number of candidate patterns in the set; The output of the large language model (the problem) pattern set Match confidence score; and The preset weighting coefficients satisfy the following conditions: ;

[0030] S33. Sort the candidate pairs based on the comprehensive score, and then select the top k candidate pairs.

[0031] Step S4 includes the following specific steps:

[0032] S41. Receive k candidate pairs output by the sorting agent, and use a parallel processing strategy to map each candidate pair to an independent generation task context;

[0033] S42. Based on each question and the corresponding database information, use vector similarity or semantic matching algorithms to search for the m most similar samples from the sample library.

[0034] S43. Using the retrieved samples, questions, database information, and prompts as prompts, multiple candidate SQL statements with different logical structures are generated in parallel through diverse sampling strategies or large language models.

[0035] Step S5 includes the following steps:

[0036] S51. Receive the candidate SQL set and perform verification in the database execution environment, and obtain the raw feedback in real time, including execution status code, error information and preliminary query result set;

[0037] S52. Thinking and Reasoning Stage: Based on the original feedback, perform structured error diagnosis, identify error types, and actively call data sampling tools to obtain real-time sampled example values ​​or enumerated value statistics of relevant columns in the target database;

[0038] S53, Execution Correction Phase: Combining diagnostic evidence and sampled data, the table join logic, filtering predicates or aggregate functions of candidate SQL are logically reconstructed by calling the preset SQL repair toolset, and the corresponding corrected SQL is generated;

[0039] S54, Environment Feedback Phase: Submit the revised SQL to the database execution environment. By observing the environment feedback information after execution, determine whether the current SQL meets the original intent constraints.

[0040] S55. Dynamically determine the iterative backtracking path based on environmental feedback results and error diagnosis depth:

[0041] If the error is determined to be a local grammatical or simple logical error, an internal self-correction loop is triggered, returning the correction agent to the thinking and reasoning stage to continue the correction.

[0042] If it is determined that the pattern information is missing or the association relationship is incorrect, the pattern feedback loop is triggered, and the error characteristics are returned to the filtering agent or the sorting agent to re-execute the pattern linking and filtering.

[0043] If the SQL expression style or the few-sample strategy is determined to be ineffective, a generation feedback loop is triggered, and the generated agent is returned.

[0044] If the original intent is determined to be misunderstood, a global reconstruction loop is triggered, and the agent is rewritten.

[0045] Iterative execution continues until a preset termination condition is met. This termination condition includes one or more of the following: the number of iterations reaches the upper limit, an executable SQL statement that satisfies the constraints is obtained, or the SQL statement does not change for two consecutive rounds.

[0046] After the iteration is completed, the large language model selects the optimal SQL as the final SQL statement output to obtain the target SQL.

[0047] The second objective of this invention is achieved through the following technical solution: a Text-to-SQL generation system based on multi-agent collaboration, comprising:

[0048] The rewritten agent module is used to perform intent reconstruction. It receives the original question and database pattern information and generates multiple semantic rewriting questions through diversified prompting strategies.

[0049] The filtering agent module is used to perform pattern linking, filtering relevant table and column information from the target database for the original problem and each semantic rewriting problem respectively;

[0050] The ranking agent module is used to perform candidate denoising. Based on the filtered table and column information, it uses a self-consistent voting mechanism combined with the confidence scores generated by the large language model to perform weighted calculations, obtain the comprehensive score of the candidate pairs, rank them, and select the top k candidate pairs for output.

[0051] The intelligent agent generation module generates multiple candidate SQL statements in parallel using a large language model based on the top k candidate pairs.

[0052] The correction agent module executes and verifies candidate SQL statements through the database execution environment. Based on the execution feedback results, it performs structured error diagnosis through a ReAct loop of "thinking-action-observation". It also performs logical repair by combining real-time sampled data from the database and initiates backtracking requests to upstream agents according to the error depth, and finally outputs the target SQL statement.

[0053] The third objective of this invention is achieved through the following technical solution: a storage medium storing a program, which, when executed by a processor, implements the Text-to-SQL generation method based on multi-agent collaboration as described in the first objective.

[0054] The fourth objective of this invention is achieved through the following technical solution: a computer device, including a processor and a memory for storing processor-executable programs, wherein when the processor executes the program stored in the memory, it implements the Text-to-SQL generation method based on multi-agent collaboration as described in the first objective.

[0055] Compared with the prior art, the present invention has the following advantages and beneficial effects:

[0056] 1. This invention generates multiple interpretations of the original question and forms a multi-perspective semantic rewrite, which can alleviate the impact of unclear referents, semantic ambiguity and vague time range on intent understanding and reduce the omission of query constraints due to intent deviation.

[0057] 2. This invention improves the completeness and accuracy of relevant table column information retrieval by integrating semantic recall and value retrieval recall through a multi-path pattern linking strategy. It significantly reduces table / field misselection, omission, and errors in understanding multi-table associations, improves pattern linking accuracy, and thus enhances the correctness of SQL generation direction.

[0058] 3. This invention employs a hybrid decision-making ranking mechanism combining self-consistent voting and matching confidence to denoise and ensure consistent selection among multi-path candidates. This enables more reliable selection of the optimal "problem-pattern" candidate pair even when the quality of multiple candidate results fluctuates, thus improving the stability of candidate selection. Furthermore, by combining multi-candidate SQL generation with database execution verification, the system can expand the search space during the inference phase and verify the results with executable constraints, thereby improving the executability and semantic consistency of the generated SQL. During the execution phase, an iterative correction mechanism based on execution feedback can continuously optimize for errors in syntax, joins, filtering, and aggregation, and combines database content information to assist in constraint verification, reducing invalid iterations and improving error correction convergence.

[0059] 4. This invention improves the accuracy, consistency, and robustness of SQL generation for complex query tasks without relying on large-scale labeled data training or frequent fine-tuning, thereby improving the user experience and engineering feasibility in real-world database query scenarios. Attached Figure Description

[0060] Figure 1 This is a schematic diagram of the architecture of a Text-to-SQL system based on multi-agent collaboration, according to an embodiment of the present invention.

[0061] Figure 2 This is a flowchart illustrating the Text-to-SQL generation method based on multi-agent collaboration, as described in an embodiment of the present invention. Detailed Implementation

[0062] The present invention is further described below through specific embodiments, but the scope of protection of the present invention is not limited thereto.

[0063] like Figure 1 and Figure 2 As shown, the Text-to-SQL generation method based on multi-agent collaboration is characterized by the following steps:

[0064] S1. Rewrite the agent to receive the original problem. Based on the database schema information, multiple semantic rewriting questions are generated through diverse prompting strategies. In this embodiment, there are n semantic questions, where n is a natural number, in order to alleviate unclear references, semantic ambiguity, and vague time ranges in user questions;

[0065] Step S1 includes the following steps:

[0066] S11. Perform schema analysis and preprocessing on the target database to extract metadata including table names, column names, and primary and foreign key relationships, and construct a simplified database structure description. Specifically, in order to control the context length, the rewritten agent can simplify the schema expression, for example, compress the column information of each table into structured text such as "table name: column1 / column2 / ..., foreign key: A.col→B.col", or organize it into a semi-structured description in the form of key-value pairs.

[0067] S12, the original problem By concatenating the database structure description information with pre-defined deconstruction hints, a task context is constructed to guide the large language model in ambiguity recognition; wherein, the deconstruction hints are used to guide the large language model to explicitly focus on and decompose ambiguities. Key elements include the query object, filtering conditions (such as time range, numerical range, enumeration value), aggregation criteria (such as statistical dimensions, indicators, grouping methods), sorting and Top-N constraints, etc.

[0068] S13. Drive the large language model to identify at least one type of ambiguity in the original problem, in order to generate n semantic rewriting problems with different focuses. Ambiguity types may include, but are not limited to: unclear entity boundaries due to ambiguous referencing, unclear filtering boundaries due to unclear time range descriptions, unclear statistical expressions due to unclear aggregation or comparison criteria, and unclear field references due to synonymous expressions or abbreviations. n can be a preset constant or dynamically determined based on the ambiguity identification results.

[0069] S14, Output includes the original problem and semantic rewriting problem A list of questions.

[0070] S2. The filtering agent performs pattern linking on the original problem and the semantic rewriting problem, and filters relevant table and column information from the target database.

[0071] Step S2 includes the following specific steps:

[0072] S21. The original problem, each semantic rewriting problem, and database schema information are encoded using a vectorization model to generate semantic vectors. Specifically, in this embodiment, the database schema information may include table names, column names, and their descriptions in the schema, and table-column combinations may also be encoded as independent schema items to improve fine-grained matching capabilities.

[0073] S22. For the original question and each semantic rewriting question, calculate the cosine similarity between its semantic vector and the database pattern vector, and filter pattern items with semantic relevance higher than a preset threshold to form a preliminary semantic recall set. Specifically, the preset threshold can be configured and can be adaptively adjusted according to the database size or question length to avoid generating too many irrelevant candidates under a large-scale schema.

[0074] S23. For the original question and each semantic rewriting question, fuzzy matching or sparse retrieval strategies are used to extract entity fragments from the question and compare them with the actual column values ​​in the database to identify potential attribute mapping relationships and form a value retrieval recall set. In this embodiment, entity fragments in the question (i.e., the original question and the semantic rewriting question) may include candidate value expressions such as entity names, numerical fragments, date fragments, and enumeration word fragments. To reduce retrieval overhead and improve stability, lightweight indexes or summary information can be established for the target database. For example, Top-N enumeration values ​​can be pre-statistically counted for high-frequency enumeration columns, example values ​​can be randomly sampled for character columns, and minimum / maximum ranges can be recorded for date columns, so that the filtering agent can quickly align candidate values ​​with columns within a controlled range.

[0075] S24. The initial semantic recall set and the value retrieval recall set are merged, deduplicated, and noise-removed to select the final candidate table set and candidate column set, and output a database information list containing "problem - filtered database information" for subsequent voting and sorting.

[0076] S3. Based on the filtered table and column information, the ranking agent performs noise reduction and uses a self-consistent voting mechanism combined with the confidence scores generated by the large language model to perform weighted calculations, obtain the comprehensive score of each candidate pair, and sort them, selecting the top k candidate pairs for output.

[0077] Step S3 includes the following steps:

[0078] S31. Count the frequency of each candidate pattern set in the pattern linking results of each semantic rewriting problem, and calculate the self-consistency voting score; specifically, in the voting ranking process, the ranking agent targets the database information list. The candidate results are processed by candidate denoising and sorting to filter out more reliable "problem-pattern" candidate pairs. (For the database information list...) The scoring is derived through self-consistent voting based on database information; the ranking agent sorts each question... The corresponding filtered database information is organized into a candidate pattern set. The candidate pattern set It can be represented as a combination of "candidate table set + candidate column set", or further as a table join subgraph that satisfies primary and foreign key connectivity constraints. The ranking agent can count the candidate pattern set. exist The number of times it appears in the pattern linking results of a semantic rewriting problem And calculate the voting score accordingly. This is to reflect the degree of consensus on the same set of patterns from different rewriting perspectives.

[0079] S32. Calculate the matching reliability score for each candidate pair using a large language model, and then weight and sum the confidence score with the self-consistency voting score to obtain the comprehensive score for each candidate pair:

[0080]

[0081] in, , For candidate pattern set The number of times it appears in the pattern linking results of n semantic rewriting problems. The total number of candidate patterns in the set; The output of the large language model (the problem) pattern set Matching confidence score, and can comprehensively consider factors such as whether the pattern set covers key constraint fields, whether there are obvious conflicts, and whether it has the inter-table relationship information required to answer the question; and The preset weighting coefficients satisfy the following conditions: ;

[0082] S33. Sort the candidate pairs based on the comprehensive score, and then select the top k candidate pairs. k can be preset as an integer from 1 to 5, or it can be dynamically determined according to the difference in the candidate distribution, so as to balance stability and computational cost.

[0083] S4. Based on the k optimal candidate pairs, the agent generates K candidate SQL statements through a large language model.

[0084] Step S4 includes the following specific steps:

[0085] S41. Receive k candidate pairs output by the ranking agent, and use a parallel processing strategy to map each candidate pair to an independent generation task context; each generation task context includes at least: question text (which can be...). Or a certain The database includes the corresponding filtered database information (candidate table columns and their relationships), as well as prompts for constraint generation. These prompts can require the generated SQL to use only candidate table columns, follow primary and foreign key relationships to construct join conditions, ensure consistency between aggregation and grouping fields, and output executable SQL code blocks.

[0086] S42. Based on each question and the corresponding database information, a retrieval is performed using vector similarity or semantic matching algorithms to find the m most similar samples from the sample library. In this embodiment, m=3. Specifically, the sample library in this embodiment is a high-quality sample library, which can be a pre-set general sample library or a collection of high-quality samples accumulated during historical interactions. The retrieved samples can be used to provide references for SQL skeletons, connection paths, and common constraint expression methods.

[0087] S43. Using the retrieved samples, questions, database information, and prompts as prompts, multiple candidate SQL statements with different logical structures are generated in parallel through diversified sampling strategies or large language models. In this embodiment, different sampling parameters (such as temperature, top-p, number of candidates, etc.) can be set to enhance the diversity of candidate structures, making the candidate SQLs differ in connection methods, filtering predicate expressions, or aggregation structures. After the candidate SQL set is output, it enters the execution feedback stage for verification and correction.

[0088] S5. The corrective agent executes and verifies the candidate SQL statement through the database execution environment. Based on the execution verification feedback, it performs structured error diagnosis through the "think-act-observe" ReAct loop and combines real-time sampled data from the database for logical repair, finally outputting the target SQL statement.

[0089] Step S5 includes the following steps:

[0090] S51. Receive the candidate SQL set and perform verification in the database execution environment, obtaining raw feedback in real time, including execution status codes, error messages, and a preliminary query result set. Specifically, the "database execution environment" refers to the database runtime environment used to execute candidate SQL statements and return execution feedback. It can be the target database itself or an equivalent verification environment consistent with the target database structure, data content, or query semantics, such as a lightweight SQLite database. The raw feedback information includes at least: execution status (success / failure), error messages (if present), and a preliminary query result summary (e.g., number of result rows, field names, or Top-N results). For candidate SQLs that fail to execute, the corrective agent uses the error message, the corresponding SQL, the problem text, and the filtered database information together as the subsequent inference context.

[0091] S52. Reasoning and Thinking Phase: Based on the original feedback, structured error diagnosis is performed to identify error types. Data sampling tools are actively invoked to obtain real-time sampled example values ​​or enumerated value statistics of relevant columns in the target database. Error types may include syntax errors, missing or conflicting table columns or aliases, inconsistent join relationships, missing or incorrect filter predicates, inconsistent aggregation and grouping, and successful execution with empty or unexpected results. To improve diagnostic effectiveness, the corrective agent can perform consistency checks between error behavior and schema candidate information, such as verifying whether the referenced column is in the candidate column set, whether the join path has a reachable foreign key link, and whether the filter value matches the column type.

[0092] S53. Execution Correction Phase: Combining diagnostic evidence and sampled data, the table join logic, filtering predicates, or aggregate functions of candidate SQL are logically reconstructed by calling a preset SQL repair toolkit to generate the corresponding corrected SQL. Logical reconstruction may include table join logic reconstruction, filtering predicate rewriting, aggregate function and grouping field correction, subquery structure adjustment, etc.

[0093] S54, Environment Feedback Phase: The revised SQL is submitted to the database execution environment. By observing the environment feedback information after execution, it is determined whether the current SQL meets the original intent constraints. Constraints may include: SQL is executable, the result is not empty or meets the consistency check rules, and there is no significant deviation between key filtering constraints and aggregation criteria. If the constraints are met, the current SQL is selected as a candidate optimal solution and enters the termination decision; if the constraints are not met, the next ReAct loop is entered.

[0094] S55. Dynamically determine the iterative backtracking path based on environmental feedback results and error diagnosis depth:

[0095] If the error is determined to be a local grammatical or simple logical error, an internal self-correction loop is triggered, returning the correction agent to the thinking and reasoning stage to continue the correction.

[0096] If it is determined that the pattern information is missing or the association relationship is incorrect, the pattern feedback loop is triggered, and the error characteristics are returned to the filtering agent or the sorting agent to re-execute the pattern linking and filtering.

[0097] If the SQL expression style or the few-sample strategy is determined to be ineffective, a generation feedback loop is triggered, and the generated agent is returned.

[0098] If the original intent is determined to be misunderstood, a global reconstruction loop is triggered, and the agent is rewritten.

[0099] Iterative execution continues until a preset termination condition is met. This termination condition includes one or more of the following: the number of iterations reaches the upper limit, an executable SQL statement that satisfies the constraints is obtained, or the SQL statement does not change for two consecutive rounds.

[0100] After the iteration is completed, the large language model selects the optimal SQL as the final SQL statement output to obtain the target SQL.

[0101] A multi-agent collaborative Text-to-SQL generation system includes:

[0102] The rewritten agent module is used to perform intent reconstruction. It receives the original question and database pattern information and generates multiple semantic rewriting questions through diversified prompting strategies.

[0103] The filtering agent module is used to perform pattern linking, filtering relevant table and column information from the target database for the original problem and each semantic rewriting problem respectively;

[0104] The ranking agent module is used to perform candidate denoising. Based on the filtered table and column information, it uses a self-consistent voting mechanism combined with the confidence scores generated by the large language model to perform weighted calculations, obtain the comprehensive score of the candidate pairs, rank them, and select the top k candidate pairs for output.

[0105] The intelligent agent generation module generates multiple candidate SQL statements in parallel using a large language model based on the top k candidate pairs.

[0106] The correction agent module executes and verifies candidate SQL statements through the database execution environment. Based on the execution feedback results, it performs structured error diagnosis through a ReAct loop of "thinking-action-observation". It also performs logical repair by combining real-time sampled data from the database and initiates backtracking requests to upstream agents according to the error depth, and finally outputs the target SQL statement.

[0107] A storage medium stores a program that, when executed by a processor, implements the aforementioned multi-agent collaborative Text-to-SQL generation method. The storage medium can be any tangible medium capable of containing or storing a program that can be used by, or in conjunction with, an instruction execution system, apparatus, or device.

[0108] A computer device includes a processor and a memory for storing processor-executable programs, wherein when the processor executes the program stored in the memory, it implements the above-described multi-agent collaborative Text-to-SQL generation method.

[0109] Those skilled in the art will readily understand that the above description is merely an embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

Claims

1. A Text-to-SQL generation method based on multi-agent collaboration, characterized in that, Includes the following steps: S1. A rewritten agent receives the original question and database pattern information, and generates multiple semantic rewriting questions through diversified prompting strategies. S2. The filtering agent performs pattern linking on the original problem and the semantic rewriting problem, and filters relevant table and column information from the target database. S3. Based on the filtered table and column information, the ranking agent performs noise reduction and uses a self-consistent voting mechanism combined with the confidence scores generated by the large language model to perform weighted calculations, obtain the comprehensive score of each candidate pair, and sort them, selecting the top k candidate pairs for output. S4. Based on the k optimal candidate pairs, the agent generates K candidate SQL statements through a large language model. S5. The corrective agent executes and verifies the candidate SQL statements through the database execution environment. Based on the execution verification feedback, it performs structured error diagnosis through the "think-act-observe" ReAct loop and combines real-time sampled data from the database for logical repair, finally outputting the target SQL statement.

2. The Text-to-SQL generation method based on multi-agent collaboration according to claim 1, characterized in that, Step S1 includes the following steps: S11. Perform schema analysis and preprocessing on the target database to construct a simplified database structure description. S12. Combine the original question with the database structure description information, and combine it with the preset deconstruction prompts to construct a task context to guide the large language model to perform ambiguity recognition. S13. Drive the large language model to identify at least one type of ambiguity in the original problem, so as to generate n semantic rewriting problems with different focuses; S14. Output a list of questions containing the original question and the semantic rewriting question.

3. The Text-to-SQL generation method based on multi-agent collaboration according to claim 1, characterized in that, Step S2 includes the following specific steps: S21. Encode the original problem, each semantic rewriting problem, and database schema information using a vectorization model to generate semantic vectors; S22. For the original question and each semantic rewriting question, calculate the cosine similarity between its semantic vector and the database pattern vector, filter out pattern items with semantic relevance higher than a preset threshold, and form a preliminary semantic recall set. S23. For the original question and each semantic rewriting question, use fuzzy matching or sparse retrieval strategies to extract entity fragments in the question, compare them with the actual column values ​​in the database, identify potential attribute mapping relationships, and form a value retrieval recall set. S24. The initial semantic recall set and the value retrieval recall set are merged, deduplicated, and noise is removed to select the final candidate table set and candidate column set, so as to output a database information list containing "problem - filtered database information".

4. The Text-to-SQL generation method based on multi-agent collaboration according to claim 3, characterized in that, Step S3 includes the following steps: S31. Count the frequency of each candidate pattern set in the pattern linking results of each semantic rewriting problem, and calculate the self-consistency voting score. S32. Calculate the matching reliability score for each candidate pair using a large language model, and then weight and sum the confidence score with the self-consistency voting score to obtain the comprehensive score for each candidate pair: in, , For candidate pattern set The number of times it appears in the pattern linking results of n semantic rewriting problems. The total number of candidate patterns in the set; The matching confidence score output by the large language model; and The preset weighting coefficients satisfy the following conditions: ; S33. Sort the candidate pairs based on the comprehensive score, and then select the top k candidate pairs.

5. The Text-to-SQL generation method based on multi-agent collaboration according to claim 3, characterized in that, Step S4 includes the following specific steps: S41. Receive k candidate pairs output by the sorting agent, and use a parallel processing strategy to map each candidate pair to an independent generation task context; S42. Based on each question and the corresponding database information, use vector similarity or semantic matching algorithms to search for the m most similar samples from the sample library. S43. Using the retrieved samples, questions, database information, and prompts as prompts, multiple candidate SQL statements with different logical structures are generated in parallel through diverse sampling strategies or large language models.

6. The Text-to-SQL generation method based on multi-agent collaboration according to claim 1, characterized in that, Step S5 Includes the following steps: S51. Receive the candidate SQL set and perform verification in the database execution environment, and obtain the raw feedback in real time, including execution status code, error information and preliminary query result set; S52. Thinking and Reasoning Stage: Based on the original feedback, perform structured error diagnosis, identify error types, and actively call data sampling tools to obtain real-time sampled example values ​​or enumerated value statistics of relevant columns in the target database; S53, Execution Correction Phase: Combining diagnostic evidence and sampled data, the table join logic, filtering predicates or aggregate functions of candidate SQL are logically reconstructed by calling the preset SQL repair toolset, and the corresponding corrected SQL is generated; S54, Environment Feedback Phase: Submit the revised SQL to the database execution environment. By observing the environment feedback information after execution, determine whether the current SQL meets the original intent constraints. S55. Dynamically determine the iterative backtracking path based on environmental feedback results and error diagnosis depth: If the error is determined to be a local grammatical or simple logical error, an internal self-correction loop is triggered, returning the correction agent to the thinking and reasoning stage to continue the correction. If it is determined that the pattern information is missing or the association relationship is incorrect, the pattern feedback loop is triggered, and the error characteristics are returned to the filtering agent or the sorting agent to re-execute the pattern linking and filtering. If the SQL expression style or the few-sample strategy is determined to be ineffective, a generation feedback loop is triggered, and the generated agent is returned. If the original intent is determined to be misunderstood, a global reconstruction loop is triggered, and the agent is rewritten.

7. The Text-to-SQL generation method based on multi-agent collaboration according to claim 6, characterized in that, Iterative execution continues until a preset termination condition is met. This termination condition includes one or more of the following: the number of iterations reaches the upper limit, an executable SQL statement that satisfies the constraints is obtained, or the SQL statement does not change for two consecutive rounds. After the iteration is completed, the large language model selects the optimal SQL as the final SQL statement output to obtain the target SQL.

8. A Text-to-SQL generation system based on multi-agent collaboration, characterized in that, include: The rewritten agent module is used to perform intent reconstruction. It receives the original question and database pattern information and generates multiple semantic rewriting questions through diversified prompting strategies. The filtering agent module is used to perform pattern linking, filtering relevant table and column information from the target database for the original problem and each semantic rewriting problem respectively; The ranking agent module is used to perform candidate denoising. Based on the filtered table and column information, it uses a self-consistent voting mechanism combined with the confidence scores generated by the large language model to perform weighted calculations, obtain the comprehensive score of the candidate pairs, rank them, and select the top k candidate pairs for output. The intelligent agent generation module generates multiple candidate SQL statements in parallel using a large language model based on the top k candidate pairs. The correction agent module executes and verifies candidate SQL statements through the database execution environment. Based on the execution feedback results, it performs structured error diagnosis through a ReAct loop of "thinking-action-observation". It also performs logical repair by combining real-time sampled data from the database and initiates backtracking requests to upstream agents according to the error depth, and finally outputs the target SQL statement.

9. A storage medium, characterized in that, The system stores a program that, when executed by a processor, implements the Text-to-SQL generation method based on multi-agent collaboration as described in any one of claims 1-7.

10. A computer device, comprising a processor and a memory for storing a processor-executable program, characterized in that, When the processor executes the program stored in the memory, it implements the Text-to-SQL generation method based on multi-agent collaboration as described in any one of claims 1-7.