Multi-agent-based end-to-end batch generation method for SQL statements
By collaborating with a multi-agent system, the problems of low resource utilization and long response time in SQL generation tasks are solved, achieving efficient and accurate batch generation of SQL statements, adapting to the complexity and type of different query types, and improving the system's flexibility and stability.
Patent Information
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- NINGBO BODEN AI TECHNOLOGY CO LTD
- Filing Date
- 2025-11-06
- Publication Date
- 2026-06-04
AI Technical Summary
Existing technologies suffer from low resource utilization, long response times, limited processing power, lack of flexibility, and insufficient batch generation capabilities when generating SQL statements, especially under high concurrency requests.
By employing a multi-agent system, through the collaboration of agents in role assignment, decision making, execution, and evaluation, end-to-end batch generation of natural language queries and complex SQL statements is achieved. The parallel processing capabilities and dynamic resource allocation of the large language model are utilized to optimize resource utilization and response speed.
It significantly improves the accuracy and efficiency of SQL generation, reduces response time, ensures efficient processing under high concurrency requests, and dynamically adjusts resource allocation to avoid performance bottlenecks.
Smart Images

Figure CN2025132908_04062026_PF_FP_ABST
Abstract
Description
A Multi-Agent-Based End-to-End Batch Generation Method for SQL Statements Technical Field
[0001] This invention relates to the fields of artificial intelligence and data processing technology, and in particular to an end-to-end batch generation method for SQL statements based on multi-agent systems. Background Technology
[0002] With the advent of the big data era, data management and analysis have become increasingly important. Converting natural language queries into SQL statements (Text-to-SQL) is a crucial step in data processing tasks, but the demands of modern data processing go far beyond this. Many applications require systems to generate diverse text content, including but not limited to SQL queries, reports, summaries, and code snippets.
[0003] Current technologies require a large amount of high-quality text data to generate SQL statements, necessitating collection from various sources, including public datasets, user-submitted query records, and specialized databases. The varying quality and availability of data from these sources increase the difficulty of data collection. Secondly, the collected text data often contains noise, errors, or inconsistencies, requiring additional time and resources for cleaning and preprocessing. Thirdly, anonymizing and ensuring compliant use of the data is a complex task, further complicating data collection. Finally, the text data needed to generate complex SQL statements typically requires manual annotation. Annotators, lacking familiarity with the specific business logic, find the annotation process time-consuming, labor-intensive, costly, and prone to errors.
[0004] Traditional SQL generation methods primarily rely on rule matching and template generation. These methods are simple to implement and maintain, producing standardized and easily understandable SQL statements, performing well when handling simple and standard queries. However, their coverage and flexibility are limited when dealing with complex and diverse natural language queries. For unforeseen query types or syntactic structures, these methods often fail to provide accurate SQL generation results. Furthermore, maintenance costs are high. As query types increase, the number of rules and templates grows rapidly, and the cost of managing and maintaining these rules and templates increases accordingly. This not only requires significant time and manpower but can also lead to inconsistencies and errors in rules and templates.
[0005] Sequence-to-sequence (Seq2Seq) models were a typical early application of deep learning in Text-to-SQL. Through an encoder-decoder architecture, they could handle complex language structures and semantic relationships. However, when processing long texts and complex queries, their ability to handle long dependencies is limited, easily leading to incoherent generated results. Furthermore, they are slow in generating complex SQL statements, especially under high concurrency, where performance bottlenecks become apparent. This affects system response time and user experience. When handling complex queries in specific domains, overfitting may occur, causing the generated SQL statements to deviate from expectations, impacting query accuracy and efficiency.
[0006] Large language models (such as BERT, GPT series, and QWEN series) possess powerful language understanding and generation capabilities through pre-training on large-scale corpora. These models can generate high-quality, diverse SQL statements, suitable for various complex query scenarios. However, for complex tasks, such as end-to-end generation of complex SQL statements, if large language models lack effective organization, management, and interaction, they suffer from the following significant shortcomings in resource scheduling and task processing:
[0007] 1. Low resource utilization: When handling high-concurrency requests, the resource utilization is low, which can easily lead to performance bottlenecks. It is impossible to effectively utilize the computing resources of multiple cores or multiple machines, resulting in low overall processing efficiency.
[0008] 2. Long response time: When generating complex SQL statements, the large language model processes them slowly, especially under high concurrency requests, which significantly increases the response time and affects the user experience.
[0009] 3. Limited processing capacity: When dealing with complex and diverse natural language queries, large language models have limited processing capacity. When faced with complex SQL statement generation tasks, they cannot effectively process all sub-tasks, resulting in inaccurate or incomplete generated SQL statements.
[0010] 4. Lack of flexibility: Large language models struggle to dynamically adjust resource allocation based on task complexity and type, lacking flexibility and adaptability. When faced with different types of queries, performance and accuracy are difficult to guarantee.
[0011] 5. Insufficient batch generation capability. In different application scenarios, such as business personnel needing to generate reports regularly; development teams needing a large number of test cases; and intelligent customer service needing to process users' natural language queries, etc., all of these require converting a large number of natural language queries into SQL statements. Therefore, batch generation of SQL statements is particularly important.
[0012] Therefore, those skilled in the art are dedicated to developing an end-to-end batch generation method for SQL statements based on multi-agent systems. Summary of the Invention
[0013] In view of the above-mentioned deficiencies of the prior art, the technical problem to be solved by the present invention is how to improve resource utilization and reduce response time in SQL generation tasks.
[0014] Multi-agent systems (MAS) are a distributed computing paradigm involving the interaction and collaboration among multiple agents to accomplish common tasks or solve problems. Each agent is an autonomous computing entity with a degree of autonomy and decision-making ability, capable of independently handling specific sub-tasks and collectively completing the overall task through cooperation and communication mechanisms. Specifically, the parallel processing capability of MAS can significantly improve system throughput and response speed; task decomposition and cooperation mechanisms can improve system processing efficiency and flexibility; dynamic adaptability can optimize resource utilization; fault tolerance and robustness can ensure system stability and reliability; and the specialization and expertise of each agent improves the quality and reliability of specific task completion.
[0015] In one embodiment of the present invention, a method for end-to-end batch generation of SQL statements based on multi-agent systems is provided, comprising:
[0016] S100. Text data preparation: Export the table structure information of all tables from the database and normalize it. Use a large language model to generate a table description for each table. Add the table description to the normalized table structure information. Input the large language model to generate text data and structure the text data.
[0017] S200, Task description prompt definition: Define the SQL statement to generate task description prompts. The SQL statement to generate task description prompts includes task requirements and structured text data.
[0018] S300, Role Allocation Agent Construction: Construct a role allocation agent to recruit roles by generating task description prompts based on SQL statements;
[0019] S400, Decision-making intelligent agent construction: Construct a decision-making intelligent agent, generate SQL statements based on task description prompts, parse and save them;
[0020] S500: Construct execution agent, connect to database and execute SQL statements, capture and save the execution results of SQL statements;
[0021] S600, Evaluation Agent Construction: Construct an evaluation agent to evaluate the execution results of SQL statements and feed the evaluation results back to the role assignment agent and the decision-making agent;
[0022] S700 generates SQL statements in batches, defines the communication and collaboration environment between role-assigning agents, decision-making agents, execution agents, and evaluation agents, and completes the SQL statement generation task.
[0023] Optionally, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, step S100 includes:
[0024] S110, Export table structure information: Export the table structure information of all tables in the database. The table structure information includes index information and field information.
[0025] S120. Standardize table structure information. Standardize table structure information, which includes table name, indexed fields and non-indexed fields.
[0026] S130. Generate table descriptions: Input the standardized table structure information into the large language model, summarize each table, generate a table description for each table, and then add the table descriptions to the standardized table structure information to obtain the final table structure information.
[0027] S140. Text data generation: Input the final table structure information as prompt words into the large language model to generate text data in batches. The text data includes a natural language query question based on the current database structure and the relevant table names that may be used to answer the question.
[0028] S150. Text data structuring: Convert relevant table names into specific table structure information to obtain structured text data, including questions, table names, table descriptions, indexed fields, and non-indexed fields.
[0029] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, in step S110, if there is no database, a database is created first, and then the tables are created.
[0030] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the database satisfies the following condition:
[0031] S111, The number of tables shall not be less than 50;
[0032] S112. The field types of the table structure should be selected with appropriate data types;
[0033] S113. Create indexes for fields that are frequently queried, based on the application scenario;
[0034] S114. Add explanatory information to table fields.
[0035] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the index information includes table name, field name, index name and index sequence number, and the field information includes table name, field name and field explanation.
[0036] Preferably, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the large language model selected is Qwen2.5-72B.
[0037] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the indexed fields include field name, field type, field explanation and index information, and the non-indexed fields include field name, field type and field explanation.
[0038] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments.
[0039] Optionally, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, step S140 includes:
[0040] S141. Establish a vector database and set the amount of text data to be generated;
[0041] S142. Input the large language model, randomly adjust the amount of the final table structure information, input the large language model, and generate text data;
[0042] S143. Calculate similarity: calculate the similarity between the text data and each piece of data generated historically in the vector database;
[0043] S144. Complete the text data generation. When the similarity is less than the set similarity threshold, embed the generated text data into the vector database. Otherwise, repeat S142-S144 until the number of text data embedded in the vector database reaches the set number of generated text data.
[0044] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the similarity threshold is 0.9.
[0045] Optionally, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, the SQL statement generation task description prompts are required to: clearly express the task requirements and avoid ambiguous expressions; and emphasize table structure information so that the large language model can understand the database structure.
[0046] Optionally, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, step S300 includes:
[0047] S310, Define the role assignment agent, define the agent type as role assignment, and name the role assignment agent;
[0048] S320, Definition of Pre-Prompt Template for Role Assignment Agent: Defines the pre-prompt template for role assignment agent, including the requirements and suggestions for recruiting roles to solve SQL statement generation tasks, and the requirements for SQL statement generation tasks include SQL statement generation task description prompts.
[0049] S330, Definition of Post-Prompt Template for Role Assignment Agent: Defines the post-prompt template for the role assignment agent, including response format and response example;
[0050] S340, Configuration of the large language model of the role-assigning agent: Configure the large language model of the role-assigning agent, including the name, temperature and maximum number of tokens of the large language model of the role-assigning agent.
[0051] S350, Definition of a method for parsing the responses of role-assigned agents;
[0052] S360, Recruit Roles: Generate task description prompts based on SQL statements to recruit roles, and separate the role information.
[0053] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, the name of the large language model of the role-assigning agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the role-assigning agent is a decimal between 0 and 1, and the maximum number of tokens of the large language model of the role-assigning agent is an integer between 0 and 32768.
[0054] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, the temperature of the large language model of the role-assigning agent is 0, and the maximum number of tokens of the large language model of the role-assigning agent is 4096.
[0055] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, the requirements for the SQL statement generation task of the role-assigning agent's pre-prompt word template include task description, number of recruits, and suggestions.
[0056] Optionally, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, step S400 includes:
[0057] S410. Define the decision-making agent, define the agent type as a solver, and name the decision-making agent.
[0058] S420, Definition of Pre-Cue Word Template for Decision-Making Agent: Define the pre-cue word template for the decision-making agent, including input data description, which includes SQL statement generation task description prompt words;
[0059] S430, Definition of post-prompt word template for decision-making agent: Define the post-prompt word template for decision-making agent, including step prompts, suggestions, response format and response requirements;
[0060] S440. Configure the large language model of the decision-making agent, including the name, temperature and maximum number of tokens of the large language model of the decision-making agent;
[0061] S450, Definition of the method for parsing decision-making and formulating agent response;
[0062] S460. Generate SQL statement: The decision-making agent generates SQL statement based on task prompts, parses and saves the SQL statement.
[0063] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, the step prompts include:
[0064] S431. Write SQL statements based on business logic problems and table structures;
[0065] S432. Explain the reasoning process and step by step how to construct the SQL statement;
[0066] S433. Ensure that the SQL statements are syntactically and logically correct.
[0067] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, the name of the large language model of the decision-making agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the decision-making agent is a decimal between 0 and 1, and the maximum number of tokens of the large language model of the decision-making agent is an integer between 0 and 32768.
[0068] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, the temperature of the large language model of the decision-making agent is 0, and the maximum number of tokens of the large language model of the decision-making agent is 4096.
[0069] Optionally, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, step S500 includes:
[0070] S510. Define the agent type as executor and name the agent.
[0071] S520, Configure the large language model of the executing agent, including the name, temperature and maximum number of tokens of the large language model of the executing agent;
[0072] S530, Database connection information configuration, including the IP address, port and name of the database to be connected;
[0073] S540. Definition of a method for parsing the execution result of an intelligent agent;
[0074] S550: Execute SQL statements. The execution agent executes SQL statements, parses and saves the execution results, which include SQL query results or error messages during the execution process.
[0075] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the name of the large language model of the executing agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the executing agent is a decimal between 0 and 1, and the maximum number of tokens of the large language model of the executing agent is an integer between 0 and 32768.
[0076] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the temperature of the large language model executing the agent is 0, and the maximum number of tokens of the large language model executing the agent is 4096.
[0077] Optionally, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, step S600 includes:
[0078] S610. Define the evaluation agent, define the agent type as evaluator, and name the evaluation agent;
[0079] S620, Evaluation Approach Definition: Define the evaluation approach for the evaluation agent;
[0080] S630, Definition of Pre-Evaluation Prompt Template for Evaluating the Agent: Defines the pre-evaluation prompt template for the evaluating agent, including the description of the SQL statement generation task, the solution result, and the evaluation approach. The description of the SQL statement generation task includes prompts for the SQL statement generation task, and the solution result includes the SQL statement generated by S400 and the execution result of S500.
[0081] S640, Definition of post-prompt template for evaluating agent: Define the post-prompt template for evaluating agent, including response format and response requirements;
[0082] S650, Evaluate the large language model configuration of the agent, configure the large language model of the agent, including the name, temperature and maximum number of tokens of the large language model of the agent.
[0083] S660, Definition of a method for parsing and evaluating agent response;
[0084] S670. Evaluate the execution result of the SQL statement. The evaluation agent evaluates the execution result of the SQL statement and feeds back the evaluation result to the role-assigning agent and the decision-making agent. Based on the evaluation result, update the pre-prompt word template of the role-assigning agent and the post-prompt word template of the decision-making agent, respectively.
[0085] Furthermore, in the multi-agent-based end-to-end batch generation method for SQL statements in the above embodiments, the evaluation approach for evaluating agents includes:
[0086] S621. Is the syntax of the original SQL query statement correct?
[0087] S622. Does the SQL execution result match the requirements of the business logic problem? Empty query results are allowed.
[0088] S623. Does the SQL execution result accurately answer the business logic question?
[0089] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the name of the large language model of the evaluation agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the evaluation agent is a decimal between 0 and 1, and the maximum number of tokens of the large language model of the evaluation agent is an integer between 0 and 32768.
[0090] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the temperature of the large language model of the agent is 0.3, and the maximum number of tokens of the large language model of the agent is 4096.
[0091] Optionally, in the multi-agent-based end-to-end batch generation method of SQL statements in any of the above embodiments, step S700 includes:
[0092] S710. Create a thread pool, submit a specified number of SQL statements to generate tasks to the thread pool, and define the maximum number of threads.
[0093] S720. Define a single SQL statement generation task, define the maximum number of rounds for a single SQL statement generation task, limit the number of roles that the role allocation agent can recruit, and read the text data of the current SQL statement generation task.
[0094] S730. Initialize the agent, initialize the pre-prompt word template of the role assignment agent, and initialize the post-prompt word template of the decision-making agent.
[0095] S740: Integrate the role allocation agent, integrate the task description prompts of the task generated by a single SQL statement and the pre-prompt template of the role allocation agent as system prompts, and the post-prompt template of the role allocation agent as query prompts. The role allocation agent starts recruiting roles based on the query prompts and system prompts, parses the recruited roles, and saves the recruited role information.
[0096] S750: Integrate the decision-making intelligent agent, integrate the task description prompts of a single SQL statement generation task, the pre-prompt template of the decision-making intelligent agent, and the role information recruited by the role allocation intelligent agent as system prompts, and the post-prompt template of the decision-making intelligent agent as query prompts. The decision-making intelligent agent makes decisions based on the query prompts and system prompts, that is, generates SQL statements and saves them.
[0097] S760: Save the execution results. The agent connects to the database, executes SQL statements, and saves the execution results, including SQL query results and errors during the execution process.
[0098] S770. The integrated evaluation agent integrates the task description prompts for a single SQL statement generation task, the role information recruited by the role allocation agent, and the pre-prompt template of the evaluation agent as system prompts, and the post-prompt template of the evaluation agent as query prompts. The evaluation agent evaluates whether the SQL statement generation task is successful based on the query prompts and system prompts, and saves the evaluation results.
[0099] S780. Complete all SQL statement generation tasks. If the current SQL statement generation task is evaluated as successful by the evaluation agent or exceeds the maximum number of rounds for a single SQL statement generation task, return to step S720 until the specified number of SQL statement generation tasks are completed. If the current SQL statement generation task is evaluated as failed by the evaluation agent, return to step S740.
[0100] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the specified number of SQL statement generation tasks is 5000.
[0101] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the maximum number of threads is 6, that is, the maximum number of SQL generation tasks processed at the same time is 6.
[0102] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the maximum number of rounds for a single SQL statement generation task is 5.
[0103] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the number of roles that the role allocation agent can recruit is limited to 2.
[0104] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the suggestion in the pre-prompt word template for role assignment agents is "There are currently no suggestions".
[0105] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiments, the suggestion in the post-prompt word template of the decision-making agent is "There are currently no suggestions".
[0106] This invention achieves the transformation from natural language query requests to complex SQL statements by constructing role-assigning agents, decision-making agents, execution agents, and evaluation agents, and establishing a communication and collaboration environment among these agents. This significantly improves the accuracy and efficiency of batch SQL generation. Through multi-agent operation, leveraging the parallel processing capabilities of multiple large language models, this invention significantly improves resource utilization and reduces response time. The multi-agent system can dynamically adjust resource allocation based on task complexity and type, avoiding performance bottlenecks and ensuring high processing speed even under high concurrency requests.
[0107] The following will further explain the concept, specific structure, and technical effects of the present invention in conjunction with the accompanying drawings, so as to fully understand the purpose, features, and effects of the present invention. Attached Figure Description
[0108] Figure 1 is a flowchart illustrating a multi-agent-based end-to-end batch generation method for SQL statements according to an exemplary embodiment;
[0109] Figure 2 is a flowchart illustrating the batch generation of SQL statements according to an exemplary embodiment. Detailed Implementation
[0110] The following description, with reference to the accompanying drawings, illustrates several preferred embodiments of the present invention to make its technical content clearer and easier to understand. The present invention can be embodied in many different forms, and the scope of protection of the present invention is not limited to the embodiments mentioned herein.
[0111] In the accompanying drawings, components with the same structure are indicated by the same numerical designation, and components with similar structures or functions are indicated by similar numerical designations. The dimensions and thicknesses of each component shown in the drawings are arbitrary, and the present invention does not limit the dimensions and thicknesses of each component. To make the illustrations clearer, the thickness of components is schematically exaggerated in some places in the drawings.
[0112] The inventors designed an end-to-end batch generation method for SQL statements based on multi-agent systems, as shown in Figure 1, including:
[0113] S100. Text data preparation: Export the table structure information of all tables from the database and normalize it. Use a large language model to generate a table description for each table. Add the table descriptions to the normalized table structure information. Input the large language model to generate text data and structure the text data. The large language model selected is Qwen2.5-72B. Specifically, this includes:
[0114] S110. Export table structure information: Export the table structure information of all tables from the database. The table structure information includes index information and field information. The index information includes the table name, field name, index name, and index number. The field information includes the table name, field name, and field description. If the database does not exist, create it first, and then create the tables. The database must meet the following conditions:
[0115] S111, The number of tables shall not be less than 50;
[0116] S112. The field types of the table structure should be selected with appropriate data types;
[0117] S113. Create indexes for fields that are frequently queried, based on the application scenario;
[0118] S114. Add explanatory information to table fields.
[0119] S120. Standardize table structure information. Standardize table structure information, which includes table name, indexed fields and non-indexed fields. Indexed fields include field name, field type, field explanation and index information. Non-indexed fields include field name, field type and field explanation.
[0120] S130. Generate table descriptions: Input the standardized table structure information into the large language model, summarize each table, generate a table description for each table, and then add the table descriptions to the standardized table structure information to obtain the final table structure information.
[0121] S140. Text data generation: Input the final table structure information as prompt words into the large language model to generate text data in batches. The text data includes a natural language query question based on the current database structure and the relevant table names that may be used to answer the question; specifically including:
[0122] S141. Establish a vector database and set the amount of text data to be generated;
[0123] S142. Input the large language model, randomly adjust the amount of the final table structure information, input the large language model, and generate text data;
[0124] S143. Calculate similarity: calculate the similarity between the text data and each piece of data generated historically in the vector database;
[0125] S144. Complete the text data generation. When the similarity is less than the set similarity threshold, embed the generated text data into the vector database. Otherwise, repeat S142-S144 until the number of text data embedded in the vector database reaches the set number of generated text data. The similarity threshold is 0.9.
[0126] S150. Text data structuring: Convert relevant table names into specific table structure information to obtain structured text data, including questions, table names, table descriptions, indexed fields, and non-indexed fields.
[0127] S200, Task Description Prompt Definition: Define the task description prompts generated by SQL statements. The task description prompts generated by SQL statements include task requirements and structured text data. When defining the task description prompts generated by SQL statements, the following requirements must be met: clearly express the task requirements and avoid ambiguous expressions; emphasize table structure information so that the large language model can understand the database structure.
[0128] S300, Role Assignment Agent Construction: Construct a role assignment agent to recruit roles by generating task description prompts based on SQL statements; specifically including:
[0129] S310, Define the role assignment agent, define the agent type as role assignment, and name the role assignment agent;
[0130] S320, Definition of Pre-Cue Word Template for Role Assignment Agent: Define the pre-cue word template for the role assignment agent, including the requirements and suggestions for recruiting roles to solve the SQL statement generation task. The requirements for the SQL statement generation task include the SQL statement generation task description prompt. The requirements for the SQL statement generation task in the pre-cue word template of the role assignment agent include the task description, the number of recruits, and suggestions.
[0131] S330, Definition of Post-Prompt Template for Role Assignment Agent: Defines the post-prompt template for the role assignment agent, including response format and response example;
[0132] S340, Configuration of the large language model of the role-assigning agent: Configure the large language model of the role-assigning agent, including the name, temperature and maximum number of tokens of the large language model of the role-assigning agent. The name of the large language model of the role-assigning agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the role-assigning agent is 0, and the maximum number of tokens of the large language model of the role-assigning agent is 4096.
[0133] S350, Definition of a method for parsing the responses of role-assigned agents;
[0134] S360, Recruit Roles: Generate task description prompts based on SQL statements to recruit roles, and separate the role information.
[0135] S400, Construction of Decision-Making Agent: Constructing a decision-making agent, generating SQL statements based on task description prompts, parsing and saving them; specifically including:
[0136] S410. Define the decision-making agent, define the agent type as a solver, and name the decision-making agent.
[0137] S420, Define the pre-prompt word template for the decision-making agent, which includes an input data description, and the data description includes SQL statement generation task description prompts.
[0138] S430. Definition of post-cue word template for decision-making agent: Define the post-cue word template for decision-making agent, including step cue words, suggestions, response format, and response requirements; step cue words include:
[0139] S431. Write SQL statements based on business logic problems and table structures;
[0140] S432. Explain the reasoning process and step by step how to construct the SQL statement;
[0141] S433. Ensure that the SQL statements are syntactically and logically correct.
[0142] S440. Configure the large language model of the decision-making agent. Configure the large language model of the decision-making agent, including the name, temperature and maximum number of tokens of the large language model of the decision-making agent. The name of the large language model of the decision-making agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the decision-making agent is 0, and the maximum number of tokens of the large language model of the decision-making agent is 4096.
[0143] S450, Define the method for analytical decision-making to formulate an agent's response.
[0144] S460. Generate SQL statement: The decision-making agent generates SQL statement based on task prompts, parses and saves the SQL statement.
[0145] S500: Constructing the execution agent, connecting to the database and executing SQL statements, capturing and saving the execution results of the SQL statements; specifically including:
[0146] S510. Define the agent type as executor and name the agent.
[0147] S520, Configure the large language model of the executing agent. Configure the large language model of the executing agent, including the name, temperature and maximum number of tokens of the large language model of the executing agent. The name of the large language model of the executing agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the executing agent is 0, and the maximum number of tokens of the large language model of the executing agent is 4096.
[0148] S530, Database connection information configuration, including the IP address, port and name of the database to be connected;
[0149] S540. Definition of a method for parsing the execution result of an intelligent agent;
[0150] S550: Execute SQL statements. The execution agent executes SQL statements, parses and saves the execution results, which include SQL query results or error messages during the execution process.
[0151] S600, Construction of the Evaluation Agent: This involves constructing an evaluation agent to assess the execution results of SQL statements and feeding back the assessment results to the role-assigning agent and the decision-making agent; specifically including:
[0152] S610. Define the evaluator agent, define the agent type as evaluator, and name the evaluator agent.
[0153] S620. Evaluation Approach Definition: Define the evaluation approach of the evaluation agent. The evaluation approach of the evaluation agent includes:
[0154] S621. Is the syntax of the original SQL query statement correct?
[0155] S622. Does the SQL execution result match the requirements of the business logic problem? Empty query results are allowed.
[0156] S623. Does the SQL execution result accurately answer the business logic question?
[0157] S630, Definition of Pre-Evaluation Prompt Template for Evaluating the Agent: Defines the pre-evaluation prompt template for the evaluating agent, including the description of the SQL statement generation task, the solution result, and the evaluation approach. The description of the SQL statement generation task includes prompts for the SQL statement generation task, and the solution result includes the SQL statement generated by S400 and the execution result of S500.
[0158] S640, Define the post-prompt template for evaluating the agent, including the response format and response requirements.
[0159] S650, Configure the large language model of the evaluation agent. Configure the large language model of the evaluation agent, including the name, temperature and maximum number of tokens of the large language model of the evaluation agent. The name of the large language model of the evaluation agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the evaluation agent is 0.3, and the maximum number of tokens of the large language model of the evaluation agent is 4096.
[0160] S660, Definition of a method for parsing and evaluating agent response;
[0161] S670. Evaluate the execution result of the SQL statement. The evaluation agent evaluates the execution result of the SQL statement and feeds back the evaluation result to the role-assigning agent and the decision-making agent. Based on the evaluation result, update the pre-prompt word template of the role-assigning agent and the post-prompt word template of the decision-making agent, respectively.
[0162] S700: Batch generate SQL statements, define the communication and collaboration environment between role-assigning agents, decision-making agents, execution agents, and evaluation agents, and complete the SQL statement generation task; as shown in Figure 2, specifically including:
[0163] S710. Create a thread pool, submit a specified number of SQL statement generation tasks to the thread pool, define the maximum number of threads, the specified number of SQL statement generation tasks is 5000, the maximum number of threads is 6, that is, the maximum number of SQL generation tasks that can be processed at the same time is 6.
[0164] S720. Define a single SQL statement generation task, define the maximum number of rounds of a single SQL statement generation task as 5, limit the number of roles that the role allocation agent can recruit to 2, and read the text data of the current SQL statement generation task.
[0165] S730. Initialize the agent, initialize the pre-prompt word template of the role assignment agent, the suggestion in the pre-prompt word template of the role assignment agent is "There are no suggestions at present", initialize the post-prompt word template of the decision-making agent, the suggestion in the post-prompt word template of the decision-making agent is "There are no suggestions at present".
[0166] S740: Integrate the role allocation agent, integrate the task description prompts of the task generated by a single SQL statement and the pre-prompt template of the role allocation agent as system prompts, and the post-prompt template of the role allocation agent as query prompts. The role allocation agent starts recruiting roles based on the query prompts and system prompts, parses the recruited roles, and saves the recruited role information.
[0167] S750: Integrate the decision-making intelligent agent, integrate the task description prompts of a single SQL statement generation task, the pre-prompt template of the decision-making intelligent agent, and the role information recruited by the role allocation intelligent agent as system prompts, and the post-prompt template of the decision-making intelligent agent as query prompts. The decision-making intelligent agent makes decisions based on the query prompts and system prompts, that is, generates SQL statements and saves them.
[0168] S760: Save the execution results. The agent connects to the database, executes SQL statements, and saves the execution results, including SQL query results and errors during the execution process.
[0169] S770. The integrated evaluation agent integrates the task description prompts for a single SQL statement generation task, the role information recruited by the role allocation agent, and the pre-prompt template of the evaluation agent as system prompts, and the post-prompt template of the evaluation agent as query prompts. The evaluation agent evaluates whether the SQL statement generation task is successful based on the query prompts and system prompts, and saves the evaluation results.
[0170] S780. Complete all SQL statement generation tasks. If the current SQL statement generation task is evaluated as successful by the evaluation agent or exceeds the maximum number of rounds for a single SQL statement generation task, return to step S720 until the specified number of SQL statement generation tasks are completed. If the current SQL statement generation task is evaluated as failed by the evaluation agent, return to step S740.
[0171] To verify the technical effectiveness of this application, the applicant conducted an experiment. Index information and field information in Excel format were exported from the database. Using these two tables, text data was obtained via S100, and then SQL statements were generated in batches via S200-S700. The index information includes table name, field name, index name, and index number. Below is a sample of some data from the index information, illustrated using a single table from the database:
[0172]
[0173] The field information table includes the table name, field names, and field descriptions. Below is a partial display of data from the field information, illustrated using a single table in the database:
[0174]
[0175] The text data includes a natural language query question based on the current database structure and the names of relevant tables that may be used to answer the question. These table names are then converted into specific table structure information to obtain structured text data. Below is an example of a structured text data entry:
[0176] Natural Language Query Question: In the sub-project with project ID 12345, which sampled packages have a sampling ratio of 20% and more than 100 files?
[0177] Related table name: tb_sample_package
[0178] Table Description: The tb_sample_package table is used to manage information about sample packages, including sampling ratio, package type, creation and modification time, user ID, sampling type, and association with the acceptance pool. It also tracks the status of packages and the number of files, and supports soft deletion functionality.
[0179] Index fields:
[0180] - 'pool_id' (type: bigint, index name: pool_id, single-column index): Acceptance pool ID
[0181] - 'id' (type: bigint, index name: PRIMARY, single-column index)
[0182] Non-indexed fields:
[0183] - 'create_at' (type: timestamp): Creation time
[0184] - 'proportion' (type: varchar): Sampling ratio
[0185] - 'package_type' (type: int): Sample package type
[0186] - 'package_index' (type: int): Package index
[0187] - 'package_name' (type: varchar): Sample package name
[0188] - 'update_by' (type: bigint): The person who made the update.
[0189] - 'pool_type' (type: int): Acceptance pool type
[0190] - 'is_release' (type: int):
[0191] - 'package_status' (type: int):
[0192] - 'is_delete' (type: int): Deletion flag: 0 means not deleted / 1 means deleted
[0193] - 'file_num' (type: int): Number of files
[0194] - 'user_id' (type: bigint): User ID
[0195] - 'update_at' (type: timestamp): Modified time
[0196] - 'create_by' (type: bigint): The creator
[0197] - 'root_project_id' (type: bigint): Subproject ID
[0198] - 'sampling_type' (type: int): Sampling type
[0199] Based on the structured text data in the above example, the final generated SQL statements are summarized as follows:
[0200] [Natural Language Query Problem]
[0201] In the sub-project with project ID 12345, which sampled packages have a sampling ratio of 20% and more than 100 files?
[0202] [SQL statement]
[0203] SELECT
[0204] id,
[0205] package_name,
[0206] proportion,
[0207] file_num
[0208] FROM
[0209] tb_sample_package
[0210] WHERE
[0211] root_project_id = 12345
[0212] AND proportion = '20%'
[0213] AND file_num > 100
[0214] AND is_delete = 0
[0215] The preferred embodiments of the present invention have been described in detail above. It should be understood that those skilled in the art can make numerous modifications and variations based on the concept of the present invention without creative effort. Therefore, all technical solutions that can be obtained by those skilled in the art based on the concept of the present invention through logical analysis, reasoning, or limited experimentation on the basis of existing technology should be within the scope of protection defined by the claims.
Claims
1. A method for end-to-end batch generation of SQL statements based on multi-agent systems, characterized in that, include: S100. Text data preparation: Export the table structure information of all tables from the database and normalize it; use a large language model to generate a table description for each table; add the table description to the normalized table structure information; input the large language model to generate text data; and structure the text data. S200. Define SQL statement generation task description prompts. When defining the SQL statement generation task description prompts, the following requirements must be met: clearly express the task requirements and avoid ambiguous expressions; emphasize table structure information so that the large language model can understand the database structure. S300. Construct a role allocation agent to recruit roles by generating task description prompts based on the SQL statement; S400: Construct a decision-making intelligent agent, generate SQL statements based on the SQL statements, generate task description prompts, parse and save them; S500: Construct an execution agent, connect to the database and execute the SQL statement, capture the execution result of the SQL statement and save it; S600. Construct an evaluation agent to evaluate the execution results and feed the evaluation results back to the role allocation agent and the decision-making agent; S700, Batch generate SQL statements, define the communication and collaboration environment between the role allocation agent, the decision-making agent, the execution agent, and the evaluation agent, and complete the SQL statement generation task; specifically including: S710. Create a thread pool and submit a specified number of SQL statement generation tasks to the thread pool. S720. Define a single SQL statement generation task, define the maximum number of rounds for a single SQL statement generation task, limit the number of roles that the role allocation agent can recruit, and read the text data of the current SQL statement generation task. S730. Initialize the pre-prompt word template of the role allocation agent and initialize the post-prompt word template of the decision-making agent. S740. Integrate the role allocation agent, integrate the task description prompt words of the single SQL statement generating task and the pre-prompt word template of the role allocation agent as system prompt words, and the post-prompt word template of the role allocation agent as query prompt words. The role allocation agent starts recruiting roles according to the query prompt words and system prompt words, parses the recruited roles, and saves the role information. S750. Integrate the decision-making intelligent agent, integrate the task description prompt words of the single SQL statement generation task, the pre-prompt word template of the decision-making intelligent agent, and the role information recruited by the role allocation intelligent agent as system prompt words, and the post-prompt word template of the decision-making intelligent agent as query prompt words. The decision-making intelligent agent makes decisions based on the query prompt words and system prompt words, that is, generates SQL statements and saves them. S760. Save the execution result. The execution agent connects to the database, executes the SQL statement, and saves the execution result. S770. Integrate the evaluation agent, which evaluates whether the SQL statement generation task was successful based on the query prompt and the system prompt, and saves the evaluation result; S780. Complete all SQL statement generation tasks. If the evaluation result is successful or exceeds the maximum number of rounds for a single SQL statement generation task, return to step S720 until the specified number of SQL statement generation tasks are completed. If the evaluation result is unsuccessful, return to step S740.
2. The end-to-end batch generation method for SQL statements based on multi-agent systems as described in claim 1, characterized in that, The large language model selected is Qwen2.5-72B.
3. The end-to-end batch generation method for SQL statements based on multi-agent systems as described in claim 1 or 2, characterized in that, Step S100 includes: S110. Export table structure information: Export the table structure information of all tables from the database. S120. Standardize the table structure information; standardize the table structure information. S130. Generate table descriptions: Input the standardized table structure information into the large language model, summarize each table, generate a table description for each table, and then add the table descriptions to the standardized table structure information to obtain the final table structure information. S140. Text data generation: Input the final table structure information as prompt words into the large language model to generate text data in batches. The text data includes a natural language query question based on the current database structure and the relevant table names that may be used to answer the question. S150. Text data structuring: The relevant table names are converted into specific table structure information to obtain structured text data.
4. The end-to-end batch generation method for SQL statements based on multi-agent systems as described in claim 3, characterized in that, Step S140 includes: S141. Establish a vector database and set the amount of text data to be generated; S142. Input the large language model, randomly adjust the amount of the final table structure information, input the large language model, and generate text data; S143. Calculate similarity: calculate the similarity between the text data and each piece of data generated historically in the vector database; S144. Complete the text data generation. When the similarity is less than the set similarity threshold, embed the text data into the vector database. Otherwise, repeat S142-S144 until the number of text data embedded in the vector database reaches the set number of generated text data.
5. The end-to-end batch generation method for SQL statements based on multi-agent systems as described in claim 3, characterized in that, Step S300 includes: S310. Define the type of the intelligent agent as role assignment, and name the role assignment intelligent agent; S320. Define the pre-completion word template for the role allocation agent, including the needs and suggestions for recruiting roles to solve the SQL statement generation task, and the needs for the SQL statement generation task include SQL statement generation task description prompts. S330. Define the post-prompt word template for the role assignment agent, including the response format and response example; S340. Configure the large language model of the role assignment agent, including the large language model name, temperature and maximum number of tokens of the role assignment agent; S350. Define the method for parsing the role assignment agent response; S360. Recruit roles: Generate task description prompts to recruit roles based on the SQL statement, and separate the role information.
6. The end-to-end batch generation method for SQL statements based on multi-agent systems as described in claim 5, characterized in that, Step S400 includes: S410. Define the type of intelligent agent as a solver, and name the decision-making intelligent agent; S420. Define a pre-completion prompt template for the decision-making agent, including an input data description, the data description including prompts for SQL statement generation task description; S430. Define the post-prompt word template for the decision-making agent, including step prompts, suggestions, response format, and response requirements; S440. Configure the large language model of the decision-making agent, including the name, temperature and maximum number of tokens of the large language model of the decision-making agent; S450. Define a method for parsing the decision and formulating the agent's response; S460. The decision-making agent generates an SQL statement based on the SQL statement, generates task prompt words, parses and saves the SQL statement.
7. The end-to-end batch generation method for SQL statements based on multi-agent systems as described in claim 6, characterized in that, Step S500 includes: S510. Define the agent type as executor and name the executing agent; S520. Configure the large language model of the executing agent; S530, Database connection information configuration, including the IP address, port and name for connecting to the database; S540. Define a method for parsing the execution result of the executing agent; S550. The execution agent executes the SQL statement, parses the execution result, and saves it.
8. The end-to-end batch generation method for SQL statements based on multi-agent systems as described in claim 7, characterized in that, Step S600 includes: S610. Define the evaluation agent, define the agent type as evaluator, and name the evaluation agent; S620. Evaluation Approach Definition: Define the evaluation approach of the evaluation agent. S630, Define the pre-prompt word template for the evaluation agent, defining the pre-prompt word template for the evaluation agent, including the evaluation SQL statement generation task description, the solution result and the evaluation approach, the evaluation SQL generation task description includes SQL statement generation task description prompt words, and the solution result includes the SQL statement generated by S400 and the execution result of S500; S640. Definition of post-prompt template for evaluation agent: Define the post-prompt template for the evaluation agent, including response format and response requirements; S650, Configure the large language model of the evaluation agent; S660, Definition of a method for parsing and evaluating the agent's response, defining the method for parsing the evaluation agent's response; S670. Evaluate the execution result of the SQL statement. The evaluation agent evaluates the execution result and provides feedback on the evaluation result to the role-assigning agent and the decision-making agent.
9. The end-to-end batch generation method for SQL statements based on multi-agent systems as described in claim 8, characterized in that, The maximum number of threads is 6.