An end-to-end batch generation method for SQL statements based on multi-agent

Through the multi-agent system, the role allocation, decision-making, execution and evaluation agents are built, which solves the problems of low resource utilization and long response time of SQL generation tasks in the existing technology, and realizes efficient and accurate batch generation of complex SQL statements.

CN119179711BActive Publication Date: 2025-04-08NINGBO BODEN AI TECHNOLOGY CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411698527.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-26
Publication Date
2025-04-08
Estimated Expiration
2044-11-26

AI Technical Summary

Technical Problem

When generating complex SQL statements, the existing technology has low resource utilization, long response time, limited processing capacity, lack of flexibility and batch generation capabilities, resulting in poor system performance bottlenecks and poor user experience.

Method used

Multi-agent system (MAS) is adopted to realize end-to-end batch generation from natural language query to complex SQL statements by building role allocation agents, decision-making agents, execution agents, and evaluation agents. The system utilizes the parallel processing capabilities of multiple large language models to dynamically adjust resource allocation, optimize resource utilization and task processing.

Benefits of technology

It significantly improves the accuracy and efficiency of batch SQL generation, improves resource utilization, reduces response time, and ensures that efficient processing speed can be maintained under high concurrent requests.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119179711B_ABST
    Figure CN119179711B_ABST
Patent Text Reader

Abstract

The present invention discloses a multi-agent-based end-to-end batch generation method for SQL statements, which relates to the field of artificial intelligence and data processing technology, including: S100, text data preparation; S200, task description prompt word definition; S300, role allocation agent construction; S400, decision-making agent construction; S500, execution agent construction; S600, evaluation agent construction; S700, batch generation of SQL statements. The present invention realizes the conversion from natural language query requests to complex SQL statements, significantly improves the accuracy and efficiency of batch SQL generation, significantly improves resource utilization, and reduces response time.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence and data processing technology, and in particular to a multi-agent-based end-to-end batch generation method for SQL statements. Background Art

[0002] With the advent of the big data era, data management and analysis are becoming increasingly important. In data processing tasks, converting natural language queries into SQL statements (i.e., Text-to-SQL) is a key link, but the needs of modern data processing go far beyond this. Many application scenarios require the system to be able to generate a variety of text content, including but not limited to SQL queries, reports, summaries, code snippets, etc.

[0003] Before the existing technology generates SQL statements, a large amount of high-quality text data is required, which needs to be collected from multiple channels, including public data sets, query records submitted by users, professional databases, etc. The data quality and availability of these channels vary, which increases the difficulty of data collection. Secondly, the collected text data is noisy, erroneous or inconsistent, and cleaning and preprocessing this data requires additional time and resources. Thirdly, the anonymization and compliance use of data is a complex task, which increases the difficulty of data collection. Finally, the text data required to generate complex SQL usually requires manual annotation. Since the annotators are not clear about the special business logic, the annotation process is time-consuming, labor-intensive, costly, and prone to annotation errors.

[0004] Traditional SQL generation methods mainly rely on rule matching and template generation. This method is simple to implement and easy to maintain. The generated SQL statements are standardized and easy to understand. They perform well when processing simple and standardized queries. However, their coverage and flexibility are limited when processing complex and diverse natural language queries. For unforeseen query types or grammatical structures, these methods often fail to provide accurate SQL generation results. And the maintenance cost is high. As the query types increase, the number of rules and templates will increase rapidly, and the cost of managing and maintaining these rules and templates will also increase accordingly. This not only requires a lot of time and manpower investment, but may also lead to inconsistencies and errors in rules and templates.

[0005] The Sequence-to-Sequence (Seq2Seq) model is a typical application of early deep learning in Text-to-SQL. Through the encoder-decoder architecture, it can handle complex language structures and semantic relationships. However, when processing long texts and complex queries, the processing ability of long dependencies is limited, and it is easy to generate incoherent results. When generating complex SQL statements, the processing speed is slow, especially in high-concurrency requests, and the performance bottleneck is obvious. This will affect the response time of the system and user experience; when processing complex queries in specific fields, overfitting may occur, which may cause the generated SQL statements to not meet expectations, affecting the accuracy and efficiency of the query.

[0006] Large language models (such as BERT, GPT series, and QWEN series) have powerful language understanding and generation capabilities through pre-training on large-scale corpora. These models can generate high-quality and diverse SQL statements and are suitable for a variety of 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, there are significant deficiencies in resource scheduling and task processing:

[0007] 1. Low resource utilization. When processing high concurrent requests, resource utilization is low, which easily leads to performance bottlenecks and inability to effectively utilize multi-core or multi-machine computing resources, resulting in low overall processing efficiency.

[0008] 2. Long response time. When generating complex SQL statements, the large language model processes slowly, especially under high concurrent requests, the response time increases significantly, affecting the user experience.

[0009] 3. Limited processing power. Large language models have limited processing power when processing complex and diverse natural language queries. When faced with complex SQL statement generation tasks, they cannot effectively process all subtasks, resulting in inaccurate or incomplete generated SQL statements.

[0010] 4. Lack of flexibility. Large language models are difficult to dynamically adjust resource allocation according to the complexity and type of tasks. They lack flexibility and adaptability. When faced with different types of queries, performance and accuracy are difficult to guarantee.

[0011] 5. Insufficient batch generation capabilities. In different application scenarios, for example, business personnel need to generate reports regularly; development teams need a large number of test cases; intelligent customer service needs to process users' natural language queries, etc. These all require converting a large number of natural language queries into SQL statements, so batch generation of SQL statements is particularly important.

[0012] Therefore, technicians in this field are committed to developing an end-to-end batch generation method of SQL statements based on multi-agent. Summary of the invention

[0013] In view of the above-mentioned defects of the prior art, the technical problem to be solved by the present invention is how to improve resource utilization and reduce response time of SQL generation tasks.

[0014] Multi-agent system (MAS) is a distributed computing paradigm that involves the interaction and collaboration between multiple agents to complete common tasks or solve problems. Each agent is an autonomous computing entity with a certain degree of autonomy and decision-making ability, which can independently handle specific subtasks and jointly complete the overall task through collaboration and communication mechanisms. Specifically, the parallel processing capability of the multi-agent system can significantly improve the throughput and response speed of the system; task decomposition and collaboration mechanism can improve the processing efficiency and flexibility of the system; dynamic adaptability can optimize resource utilization; fault tolerance and robustness can ensure the stability and reliability of the system; the professionalism and expertise of each entity improve the completion quality and reliability of specific tasks.

[0015] In one embodiment of the present invention, a method for end-to-end batch generation of SQL statements based on multi-agents is provided, comprising:

[0016] S100, preparing text data, exporting table structure information of all tables from the database for normalization, using a large language model to generate a table description of each table, adding the table description to the normalized table structure information, inputting the large language model to generate text data, and structuring the text data;

[0017] S200, defining task description prompt words, defining SQL statements to generate task description prompt words, wherein the SQL statements to generate task description prompt words include task requirements and structured text data;

[0018] S300, role allocation agent construction, constructing a role allocation agent, generating task description prompt words according to SQL statements to recruit roles;

[0019] S400, decision-making intelligent agent construction, building a decision-making intelligent agent, generating SQL statements based on SQL statements to generate task description prompt words and parsing and saving;

[0020] S500, executing intelligent agent construction, building an executing intelligent agent, connecting to a database and executing SQL statements, capturing and saving the execution results of the SQL statements;

[0021] S600, constructing an evaluation agent, constructing an evaluation agent, evaluating the execution result of the SQL statement, and feeding back the evaluation result to the role allocation agent and the decision-making agent;

[0022] S700, batch generate SQL statements, establish the communication and collaboration environment between the role allocation agent, decision making agent, execution agent and evaluation agent, and complete the SQL statement generation task.

[0023] Optionally, in the multi-agent-based end-to-end batch generation method for SQL statements in the above embodiment, step S100 includes:

[0024] S110, exporting table structure information, exporting table structure information of all tables from the database, the table structure information including index information and field information;

[0025] S120, normalizing table structure information, normalizing table structure information, the table structure information including table name, index fields, and non-index fields;

[0026] S130, generating a table description, inputting the normalized table structure information into the large language model, summarizing each table, generating a table description for each table, and then adding the table description to the normalized table structure information to obtain the final table structure information;

[0027] S140, text data generation, inputting the final table structure information as prompt words into the large language model to generate text data in batches, the text data including a natural language query question based on the current database structure and related table names that may be used to answer the question;

[0028] S150, text data is structured, and related table names are converted into specific table structure information to obtain structured text data, including questions, table names, table descriptions, index fields, and non-index fields.

[0029] Furthermore, in the multi-agent-based end-to-end batch generation method for SQL statements in the above embodiment, in step S110, if there is no database, a database is first established, and then a table is created.

[0030] Furthermore, in the end-to-end batch generation method of SQL statements based on multi-agents in the above embodiment, the database meets the following conditions:

[0031] S111. The number of tables shall not be less than 50;

[0032] S112. The field type of the table structure should select the appropriate data type;

[0033] S113. Create indexes for fields that need to be frequently queried according to application scenarios.

[0034] S114. Add explanation information to table fields.

[0035] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiment, the index information includes the table name, field name, index name and index sequence number, and the field information includes the 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 embodiment, the large language model selects Qwen2.5-72B.

[0037] Furthermore, in the multi-agent-based end-to-end batch generation method of SQL statements in the above embodiment, the index field includes field name, field type, field explanation and index information, and the non-index field includes field name, field type and field explanation.

[0038] Furthermore, in the end-to-end batch generation method of SQL statements based on multi-agent in the above embodiment.

[0039] Optionally, in the method for end-to-end batch generation of SQL statements based on multi-agents in any of the above embodiments, step S140 includes:

[0040] S141, establishing a vector database and setting the amount of generated text data;

[0041] S142, inputting a large language model, randomly adjusting the amount of the final table structure information, inputting the large language model, and generating text data;

[0042] S143, calculating the similarity, calculating the similarity between the text data and each piece of data generated historically in the vector database;

[0043] S144, completing the text data generation, when the similarity is less than the set similarity threshold, embedding the generated text data into the vector database, otherwise re-execute S142-S144 until the text data embedded into 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 embodiment, 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 prompt words are required to: clearly express task requirements and avoid ambiguous expressions; emphasize table structure information so that the large language model can understand the database structure.

[0046] Optionally, in the method for end-to-end batch generation of SQL statements based on multi-agents in any of the above embodiments, step S300 includes:

[0047] S310, defining a role allocation agent, defining the type of the agent as role allocation, and naming the role allocation agent;

[0048] S320, defining a pre-prompt word template for a role allocation agent, defining a pre-prompt word template for a role allocation agent, including requirements and suggestions for recruiting roles to solve SQL statement generation tasks, where the requirements for SQL statement generation tasks include prompt words for describing the SQL statement generation task;

[0049] S330, defining a post-prompt word template of a role allocation agent, defining a post-prompt word template of a role allocation agent, including a response format and a response example;

[0050] S340, configuring a large language model of the role allocation agent, configuring the large language model of the role allocation agent, including the name, temperature and maximum number of tokens of the large language model of the role allocation agent;

[0051] S350, defining a method for parsing a role allocation agent response, defining a method for parsing a role allocation agent response;

[0052] S360, recruiting roles, generating task description prompt words according to SQL statements to recruit roles, and separating the information of the roles.

[0053] Furthermore, in the end-to-end batch generation method of SQL statements based on multiple agents in any of the above embodiments, the name of the large language model of the role allocation agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the role allocation agent is a decimal between 0 and 1, and the maximum number of tokens of the large language model of the role allocation agent is an integer between 0 and 32768.

[0054] Furthermore, in the multi-agent-based end-to-end batch generation method for SQL statements in any of the above embodiments, the temperature of the large language model of the role allocation agent is 0, and the maximum number of tokens of the large language model of the role allocation agent is 4096.

[0055] Furthermore, in the multi-agent-based end-to-end batch generation method for SQL statements in any of the above embodiments, the requirements for the SQL statement generation task of the pre-prompt word template of the role assignment agent include task description, number of recruits and suggestions.

[0056] Optionally, in the method for end-to-end batch generation of SQL statements based on multi-agents in any of the above embodiments, step S400 includes:

[0057] S410, defining a decision-making agent, defining the type of the agent as a solver, and naming the decision-making agent;

[0058] S420, defining a pre-prompt word template of a decision-making agent, defining a pre-prompt word template of a decision-making agent, including input data description, the data description including SQL statement generation task description prompt words;

[0059] S430, defining a post-prompt word template of a decision-making agent, defining a post-prompt word template of a decision-making agent, including step prompt words, suggestions, response formats, and response requirements;

[0060] S440, configuring a large language model of a decision-making agent, configuring the large language model of the decision-making agent, including a name, temperature, and maximum number of tokens of the large language model of the decision-making agent;

[0061] S450, defining a method for parsing a decision-making agent response, defining a method for parsing a decision-making agent response;

[0062] S460, generate SQL statements, the decision-making agent generates SQL statements according to the SQL statement generation task prompt words, and parses and saves them.

[0063] Furthermore, in the multi-agent-based end-to-end batch generation method for SQL statements in any of the above embodiments, the step prompt words include:

[0064] S431. Write SQL statements based on business logic issues and table structures;

[0065] S432, explain the reasoning process and step by step explain how to construct SQL statements;

[0066] S433. Ensure the grammatical and logical correctness of SQL statements.

[0067] Further, in the multi-agent-based end-to-end batch generation method for 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 end-to-end batch generation method of SQL statements based on multiple agents 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 method for end-to-end batch generation of SQL statements based on multi-agents in any of the above embodiments, step S500 includes:

[0070] S510, defining an execution agent, defining the agent type as an executor, and naming the execution agent;

[0071] S520, configuring the large language model of the executing agent, configuring 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 connection;

[0073] S540, defining a method for parsing the execution result of the execution agent, defining a method for parsing the execution result of the execution agent;

[0074] S550, execute SQL statements, execute the intelligent agent to execute SQL statements, parse and save the execution results, and the execution results include SQL query results or error information during the execution process.

[0075] Furthermore, in the end-to-end batch generation method of SQL statements based on multiple agents in the above embodiment, 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 end-to-end batch generation method of SQL statements based on multiple agents in the above embodiment, 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.

[0077] Optionally, in the method for end-to-end batch generation of SQL statements based on multi-agents in any of the above embodiments, step S600 includes:

[0078] S610, defining an evaluation agent, defining the agent type as an evaluator, and naming the evaluation agent;

[0079] S620, defining an evaluation strategy, defining an evaluation strategy for evaluating an intelligent agent;

[0080] S630, defining a pre-prompt word template for the evaluation agent, defining a pre-prompt word template for the evaluation agent, including an evaluation SQL statement generation task description, a solution result, and an evaluation idea, wherein the evaluation SQL statement generation task description includes a SQL statement generation task description prompt word, and the solution result includes the SQL statement generated in S400 and the execution result in S500;

[0081] S640, defining a post-prompt word template for the evaluation agent, defining a post-prompt word template for the evaluation agent, including a response format and response requirements;

[0082] S650, configuring the large language model of the evaluation agent, configuring 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.

[0083] S660, defining a method for analyzing and evaluating the response of an intelligent agent, defining a method for analyzing and evaluating the response of an intelligent agent;

[0084] S670, evaluate the SQL statement execution result, the evaluation agent evaluates the SQL statement execution result, feeds back the evaluation result to the role assignment agent and the decision-making agent, and updates the pre-prompt word template of the role assignment agent and the post-prompt word template of the decision-making agent according to the evaluation result.

[0085] Furthermore, in the end-to-end batch generation method of SQL statements based on multiple agents in the above embodiment, the evaluation ideas of the evaluation agent include:

[0086] S621. Is the syntax of the original SQL query statement correct?

[0087] S622: Whether the SQL execution result matches the requirements of the business logic problem, and whether the query result is allowed to be empty;

[0088] S623. Whether the SQL execution result accurately answers the business logic question.

[0089] Furthermore, in the end-to-end batch generation method of SQL statements based on multiple agents in the above embodiment, 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 for SQL statements in the above embodiment, 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.

[0091] Optionally, in the method for end-to-end batch generation of SQL statements based on multi-agents in any of the above embodiments, step S700 includes:

[0092] S710, create a thread pool, submit a specified number of SQL statement generation tasks to the thread pool, and define the maximum number of threads;

[0093] S720, define a single SQL statement generation task, define the maximum round of the single SQL statement generation task, limit the number of roles that can be recruited by the role allocation agent, and read the text data of the current SQL statement generation task;

[0094] S730, initializing the agent, initializing the pre-prompt word template of the role assignment agent, and initializing the post-prompt word template of the decision-making agent;

[0095] S740, integrating the role allocation agent, integrating the task description prompt words of the single SQL statement generated task and the pre-prompt word template of the role allocation agent as the system prompt words, and the post-prompt word template of the role allocation agent as the query prompt words, the role allocation agent starts recruiting roles according to the query prompt words and the system prompt words, and parses the recruited roles, and saves the recruited role information;

[0096] S750, integrating the decision-making agent, integrating the task description prompt words of the single SQL statement generation task, the pre-prompt word template of the decision-making agent, and the role information recruited by the role allocation agent as the system prompt words, and the post-prompt word template of the decision-making agent as the query prompt words, and the decision-making agent makes a decision according to the query prompt words and the system prompt words, that is, generates an SQL statement, and saves it;

[0097] S760, save the execution result, execute the agent to connect to the database, execute the SQL statement, and save the execution result, which includes the SQL query result and the error during the execution process;

[0098] S770, integrating the evaluation agent, integrating the task description prompt words of a single SQL statement generation task, the role information recruited by the role allocation agent, and the pre-prompt word template of the evaluation agent into the system prompt words, and the post-prompt word template of the evaluation agent into the query prompt words, the evaluation agent evaluates whether this SQL statement generation task is successful based on the query prompt words and the system prompt words, and saves the evaluation result;

[0099] S780. Complete all SQL statement generation tasks. If the SQL statement generation task is evaluated as successful by the evaluation agent or exceeds the maximum rounds of a single SQL statement generation task, return to step S720 until the specified number of SQL statement generation tasks are completed; if the 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 embodiment, the specified number of SQL statement generation tasks is 5,000.

[0101] Furthermore, in the multi-agent-based end-to-end batch generation method for SQL statements in the above embodiment, the maximum number of threads is 6, that is, the maximum number of SQL generation tasks processed simultaneously is 6.

[0102] Furthermore, in the multi-agent-based end-to-end batch generation method for SQL statements in the above embodiment, the maximum rounds of a single SQL statement generation task is 5.

[0103] Furthermore, in the end-to-end batch generation method of SQL statements based on multiple agents in the above embodiment, the number of roles that can be recruited by the role allocation agent is limited to 2.

[0104] Furthermore, in the multi-agent-based end-to-end batch generation method for SQL statements in the above embodiment, the suggestion in the pre-prompt word template of the role allocation agent is "there are currently no suggestions."

[0105] Furthermore, in the multi-agent-based end-to-end batch generation method for SQL statements in the above embodiment, the suggestion in the post-prompt word template of the decision-making agent is "there are currently no suggestions."

[0106] The present invention realizes the conversion from natural language query request to complex SQL statement by constructing role allocation agent, decision making agent, execution agent and evaluation agent, and formulating communication and cooperation environment between agents. The accuracy and efficiency of batch SQL generation are significantly improved. The present invention uses multiple agents and the parallel processing capability of multiple large language models to significantly improve resource utilization and reduce response time. Multiple agents can dynamically adjust resource allocation according to the complexity and type of tasks, avoid performance bottlenecks, and ensure that efficient processing speed can be maintained under high concurrent requests.

[0107] The concept, specific structure and technical effects of the present invention will be further described below in conjunction with the accompanying drawings to fully understand the purpose, characteristics and effects of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0108] Figure 1is a flowchart illustrating a method for end-to-end batch generation of SQL statements based on a multi-agent according to an exemplary embodiment;

[0109] Figure 2 is a flowchart illustrating batch generation of SQL statements according to an exemplary embodiment. DETAILED DESCRIPTION

[0110] The following describes several preferred embodiments of the present invention with reference to the drawings in the specification, so that the technical content is clearer and easier to understand. The present invention can be embodied in many different forms of embodiments, and the protection scope of the present invention is not limited to the embodiments mentioned in the text.

[0111] In the drawings, components with the same structure are indicated by the same numerical reference numerals, and components with similar structures or functions are indicated by similar numerical reference numerals. The size and thickness of each component shown in the drawings are arbitrarily shown, and the present invention does not limit the size and thickness of each component. In order to make the illustration clearer, the thickness of the components is schematically exaggerated in some places in the drawings.

[0112] The inventors have designed a multi-agent-based end-to-end batch generation method for SQL statements. Figure 1 As shown, including:

[0113] S100, text data preparation, exporting the table structure information of all tables from the database for normalization, using the large language model to generate a table description for each table, adding the table description to the normalized table structure information, inputting the large language model to generate text data, structuring the text data, and selecting Qwen2.5-72B for the large language model; specifically including:

[0114] S110, export table structure information, export table structure information of all tables from the database, the table structure information includes index information and field information, the index information includes table name, field name, index name and index sequence number, the field information includes table name, field name and field explanation; if there is no database, first create a database, and then create a table; the database meets the following conditions:

[0115] S111. The number of tables shall not be less than 50;

[0116] S112. The field type of the table structure should select the appropriate data type;

[0117] S113. Create indexes for fields that need to be frequently queried according to application scenarios.

[0118] S114. Add explanation information to table fields.

[0119] S120, normalize table structure information. Normalize table structure information. The table structure information includes table name, index fields and non-index fields. Index fields include field name, field type, field explanation and index information. Non-index fields include field name, field type and field explanation.

[0120] S130, generating a table description, inputting the normalized table structure information into the large language model, summarizing each table, generating a table description for each table, and then adding the table description to the normalized table structure information to obtain the final table structure information.

[0121] S140, text data generation, inputting the final table structure information as prompt words into the large language model, and generating 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, establishing a vector database and setting the amount of generated text data;

[0123] S142, inputting a large language model, randomly adjusting the amount of the final table structure information, inputting the large language model, and generating text data;

[0124] S143, calculating the similarity, calculating 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 re-execute S142-S144 until the text data embedded into the vector database reaches the set amount of generated text data, and the similarity threshold is 0.9.

[0126] S150, text data is structured, and related table names are converted into specific table structure information to obtain structured text data, including questions, table names, table descriptions, index fields, and non-index fields.

[0127] S200, task description prompt word definition, define SQL statement to generate task description prompt words, SQL statement to generate task description prompt words include task requirements and structured text data, SQL statement to generate task description prompt words when defining the requirements: clearly express task requirements, avoid ambiguous expressions; emphasize table structure information so that the large language model can understand the database structure.

[0128] S300, role allocation agent construction, constructing a role allocation agent, generating task description prompt words according to SQL statements to recruit roles; specifically including:

[0129] S310, defining a role allocation agent, defining the type of the agent as role allocation, and naming the role allocation agent;

[0130] S320, defining a pre-prompt word template for a role allocation agent, defining a pre-prompt word template for a role allocation agent, including requirements and suggestions for recruiting roles to solve a SQL statement generation task, the requirements for the SQL statement generation task including a SQL statement generation task description prompt word; the requirements for the SQL statement generation task of the pre-prompt word template for the role allocation agent include a task description, the number of people to be recruited, and suggestions;

[0131] S330, defining a post-prompt word template of a role allocation agent, defining a post-prompt word template of a role allocation agent, including a response format and a response example;

[0132] S340, configuring the large language model of the role allocation agent, configuring the large language model of the role allocation agent, including the name, temperature and maximum number of tokens of the large language model of the role allocation agent, the name of the large language model of the role allocation agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the role allocation agent is 0, and the maximum number of tokens of the large language model of the role allocation agent is 4096;

[0133] S350, defining a method for parsing a role allocation agent response, defining a method for parsing a role allocation agent response;

[0134] S360, recruiting roles, generating task description prompt words according to SQL statements to recruit roles, and separating the information of the roles.

[0135] S400, decision-making intelligent agent construction, building a decision-making intelligent agent, generating SQL statements based on SQL statements to generate task description prompt words and parsing and saving; specifically including:

[0136] S410, defining a decision-making agent, defining the type of the agent as a solver, and naming the decision-making agent.

[0137] S420, defining a pre-prompt word template of a decision-making intelligent agent, defining a pre-prompt word template of a decision-making intelligent agent, including input data description, wherein the data description includes SQL statement generation task description prompt words.

[0138] S430, defining a post-prompt word template for a decision-making agent, defining a post-prompt word template for a decision-making agent, including step prompt words, suggestions, response formats, and response requirements; step prompt words include:

[0139] S431. Write SQL statements based on business logic issues and table structures;

[0140] S432, explain the reasoning process and step by step explain how to construct SQL statements;

[0141] S433. Ensure the grammatical and logical correctness of SQL statements.

[0142] S440. Configuring the large language model of the decision-making agent. Configuring 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. Definition of a method for parsing the response of a decision-making agent, defining a method for parsing the response of a decision-making agent.

[0144] S460, generate SQL statements, the decision-making agent generates SQL statements according to the SQL statement generation task prompt words, and parses and saves them.

[0145] S500, executing intelligent agent construction, building an executing intelligent agent, connecting to a database and executing SQL statements, capturing and saving the execution results of SQL statements; specifically including:

[0146] S510, defining an execution agent, defining the agent type as an executor, and naming the execution agent;

[0147] S520, configure the large language model of the execution agent, configure the large language model of the execution agent, including the name, temperature and maximum number of tokens of the large language model of the execution agent, the name of the large language model of the execution agent is Qwen2_5-72B-gptq-int4, the temperature of the large language model of the execution agent is 0, and the maximum number of tokens of the large language model of the execution agent is 4096;

[0148] S530, database connection information configuration, including the IP address, port and name of the database connection;

[0149] S540, defining a method for parsing the execution result of the execution agent, defining a method for parsing the execution result of the execution agent;

[0150] S550, execute SQL statements, execute the intelligent agent to execute SQL statements, parse and save the execution results, and the execution results include SQL query results or error information during the execution process.

[0151] S600: construct an evaluation agent, construct an evaluation agent, evaluate the execution result of the SQL statement, and feed back the evaluation result to the role allocation agent and the decision-making agent; specifically, it includes:

[0152] S610, defining an evaluation agent, defining the agent type as an evaluator, and naming the evaluation agent.

[0153] S620, defining an evaluation strategy, defining an evaluation strategy for the evaluation agent, wherein the evaluation strategy for the evaluation agent includes:

[0154] S621. Is the syntax of the original SQL query statement correct?

[0155] S622: Whether the SQL execution result matches the requirements of the business logic problem, and whether the query result is allowed to be empty;

[0156] S623. Whether the SQL execution result accurately answers the business logic question.

[0157] S630. Definition of the pre-prompt word template of the evaluation agent. Define the pre-prompt word template of the evaluation agent, including the evaluation SQL statement generation task description, solution results and evaluation ideas. The evaluation SQL generation task description includes the SQL statement generation task description prompt words, and the solution results include the SQL statement generated by S400 and the execution result of S500.

[0158] S640, defining a post-prompt word template for the evaluation agent, defining a post-prompt word template for the evaluation agent, including a 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, defining a method for analyzing and evaluating the response of an intelligent agent, defining a method for analyzing and evaluating the response of an intelligent agent;

[0161] S670, evaluate the SQL statement execution result, the evaluation agent evaluates the SQL statement execution result, feeds back the evaluation result to the role assignment agent and the decision-making agent, and updates the pre-prompt word template of the role assignment agent and the post-prompt word template of the decision-making agent according to the evaluation result.

[0162] S700, batch generate SQL statements, establish a 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; Figure 2 As shown, 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 simultaneously is 6.

[0164] S720, define a single SQL statement generation task, define the maximum round of the 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, initializing the agent, initializing the pre-prompt word template of the role allocation agent, the suggestion in the pre-prompt word template of the role allocation agent is "there is no suggestion at present", initializing 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 is no suggestion at present";

[0166] S740, integrating the role allocation agent, integrating the task description prompt words of the single SQL statement generated task and the pre-prompt word template of the role allocation agent as the system prompt words, and the post-prompt word template of the role allocation agent as the query prompt words, the role allocation agent starts recruiting roles according to the query prompt words and the system prompt words, and parses the recruited roles, and saves the recruited role information;

[0167] S750, integrating the decision-making agent, integrating the task description prompt words of the single SQL statement generation task, the pre-prompt word template of the decision-making agent, and the role information recruited by the role allocation agent as the system prompt words, and the post-prompt word template of the decision-making agent as the query prompt words, and the decision-making agent makes a decision according to the query prompt words and the system prompt words, that is, generates an SQL statement, and saves it;

[0168] S760, save the execution result, execute the agent to connect to the database, execute the SQL statement, and save the execution result, which includes the SQL query result and the error during the execution process;

[0169] S770, integrating the evaluation agent, integrating the task description prompt words of a single SQL statement generation task, the role information recruited by the role allocation agent, and the pre-prompt word template of the evaluation agent into the system prompt words, and the post-prompt word template of the evaluation agent into the query prompt words, the evaluation agent evaluates whether this SQL statement generation task is successful based on the query prompt words and the system prompt words, and saves the evaluation result;

[0170] S780. Complete all SQL statement generation tasks. If the SQL statement generation task is evaluated as successful by the evaluation agent or exceeds the maximum rounds of a single SQL statement generation task, return to step S720 until the specified number of SQL statement generation tasks are completed; if the SQL statement generation task is evaluated as failed by the evaluation agent, return to step S740.

[0171] To verify the technical effect of this application, the applicant conducted an experiment. The index information and field information in Excel format were exported from the database. The text data was obtained through S100 using these two tables, and then SQL statements were generated in batches through S200-S700. The index information includes the table name, field name, index name and index number. The following is an example of some data in the index information, using a table in the database as an example:

[0172]

[0173] The field information table includes table name, field name and field explanation. The following is a partial display of data in the field information, using a table in the database as an example:

[0174]

[0175] 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. The relevant table names are converted into specific table structure information to obtain structured text data. The following is an example of structured text data:

[0176] Natural language query question: Among the subprojects with project ID 12345, which sampling 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 the information of the sampling package, including the sampling ratio, package type, creation and modification time, user ID, sampling type, and association with the acceptance pool. It also tracks the status and number of files in the package and supports the soft delete function.

[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): sampling package name

[0188] - 'update_by' (type: bigint): Modifier

[0189] - 'pool_type' (type: int): acceptance pool type

[0190] - 'is_release' (type: int):

[0191] - 'package_status' (type: int):

[0192] - 'is_delete' (type: int): Delete flag: 0 not deleted / 1 deleted

[0193] - 'file_num' (type: int): number of files

[0194] - 'user_id' (type: bigint): user ID

[0195] - 'update_at' (type: timestamp): modification time

[0196] - 'create_by' (type: bigint): creator

[0197] - 'root_project_id' (type: bigint): subproject ID

[0198] - 'sampling_type' (type: int): sampling type

[0199] For the structured text data in the above example, the final generated SQL statement is summarized as follows:

[0200] [Natural language query problem]

[0201] Among the subprojects with project ID 12345, which sampling 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 specific embodiments of the present invention are described in detail above. It should be understood that a person skilled in the art can make many modifications and changes based on the concept of the present invention without creative work. Therefore, any technical solution that can be obtained by a person skilled in the art through logical analysis, reasoning or limited experiments based on the concept of the present invention on the basis of the prior art should be within the scope of protection determined by the claims.

Claims

1. A multi-agent-based end-to-end batch generation method for SQL statements, characterized in that: include: S100, preparing text data, exporting table structure information of all tables from a database for normalization, using a large language model to generate a table description of each table, adding the table description to the normalized table structure information, inputting the large language model to generate text data, and structuring the text data; S200, defining SQL statements to generate task description prompt words, wherein the SQL statements to generate task description prompt words require: clearly expressing task requirements and avoiding ambiguous expressions; emphasizing table structure information so that the large language model can understand the database structure; S300, constructing a role allocation agent, generating task description prompt words to recruit roles according to the SQL statement; S400, constructing a decision-making agent, generating a SQL statement based on the task description prompt words generated by the SQL statement, parsing and saving; S500, constructing an execution agent, connecting to a database and executing the SQL statement, capturing and saving the execution result of the SQL statement; S600, constructing an evaluation agent, evaluating the execution result, and feeding back the evaluation result to the role allocation agent and the decision-making agent; S700, batch generating SQL statements, establishing the communication and collaboration environment between the role allocation agent, the decision-making agent, the execution agent and the evaluation agent, and completing 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 round of the single SQL statement generation task, limit the number of roles that can be recruited by the role allocation agent, and read the text data of the current SQL statement generation task; S730, initializing the pre-prompt word template of the role assignment agent, and initializing the post-prompt word template of the decision-making agent; S740, integrating the role allocation agent, integrating the task description prompt word of the single SQL statement generation task and the pre-prompt word template of the role allocation agent into the system prompt word, and the post-prompt word template of the role allocation agent into the query prompt word, and the role allocation agent starts recruiting roles according to the query prompt word and the system prompt word, and analyzes the recruited roles, and saves the role information; S750, integrating the decision-making agent, integrating the task description prompt words of the single SQL statement generation task, the pre-prompt word template of the decision-making agent and the role information recruited by the role allocation agent into the system prompt words, and the post-prompt word template of the decision-making agent into the query prompt words, and the decision-making agent makes a decision according to the query prompt words and the system prompt words, that is, generates an SQL statement, and saves it; S760, saving the execution result, the execution agent connects to the database, executes the SQL statement, and saves the execution result; S770, integrating the evaluation agent, wherein the evaluation agent evaluates whether the SQL statement generation task is successful according to the query prompt word and the system prompt word, and saves the evaluation result; S780, complete all SQL statement generation tasks. If the evaluation result is successful or exceeds the maximum rounds of the 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 failure, return to step S740.

2. The method for end-to-end batch generation of SQL statements based on multi-agents according to claim 1, characterized in that: The large language model selects Qwen2.5-72B.

3. The method for end-to-end batch generation of SQL statements based on multi-agents according to claim 1 or 2, characterized in that: The step S100 includes: S110, exporting table structure information, exporting table structure information of all tables from the database; S120, normalizing the table structure information, normalizing the table structure information; S130, generating a table description, inputting the normalized table structure information into the large language model, summarizing each table, generating a table description for each table, and then adding the table description to the normalized table structure information to obtain the final table structure information; S140, generating text data, inputting the final table structure information as prompt words into the large language model to generate text data in batches, wherein the text data includes a natural language query question based on the current database structure and related table names that may be used to answer the question; S150: Structuring the text data: converting the relevant table names into specific table structure information to obtain structured text data.

4. The method for end-to-end batch generation of SQL statements based on multi-agents as claimed in claim 3, characterized in that: The step S140 includes: S141, establishing a vector database and setting the amount of generated text data; S142, inputting a large language model, randomly adjusting the amount of the final table structure information, inputting the large language model, and generating text data; S143, calculating the similarity, calculating the similarity between the text data and each piece of data generated historically in the vector database; S144, completing the text data generation, when the similarity is less than the set similarity threshold, embedding the text data into the vector database, otherwise re-execute S142-S144 until the text data embedded into the vector database reaches the set amount of generated text data.

5. The method for end-to-end batch generation of SQL statements based on multi-agents as claimed in claim 3, characterized in that: The step S300 includes: S310, defining the type of the agent as role allocation, and naming the role allocation agent; S320, defining a pre-prompt word template for the role assignment agent, including requirements and suggestions for recruiting roles to solve SQL statement generation tasks, where the requirements for SQL statement generation tasks include SQL statement generation task description prompt words; S330, defining a post-prompt word template of the role allocation agent, including a response format and a response example; S340, configuring the large language model of the role allocation agent, including the large language model name, temperature and maximum number of tokens of the role allocation agent; S350, defining a method for parsing a role allocation agent response; S360, recruiting roles, generating a task description prompt word recruiting roles according to the SQL statement, and separating the role information.

6. The method for end-to-end batch generation of SQL statements based on multi-agents as claimed in claim 5, characterized in that: The step S400 includes: S410, defining the type of the agent as a solver, and naming the decision-making agent; S420, defining a pre-prompt word template for the decision-making agent, including input data description, the data description including SQL statement generation task description prompt words; S430, defining a post-prompt word template for the decision-making agent, including step prompt words, suggestions, response formats, and response requirements; S440, configuring 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, defining a method for parsing the response of the decision-making agent; S460, the decision-making agent generates an SQL statement based on the task prompt words of the SQL statement, and parses and saves it.

7. The method for end-to-end batch generation of SQL statements based on multi-agents as claimed in claim 6, characterized in that: The step S500 includes: S510, defining the agent type as an executor, and naming the executor agent; S520, configuring a large language model of the execution agent; S530, database connection information configuration, including the IP address, port and name of the database; S540, defining a method for analyzing the execution result of the execution agent; S550: The execution agent executes the SQL statement, parses the execution result and saves it.

8. The method for end-to-end batch generation of SQL statements based on multi-agents as claimed in claim 7, characterized in that: The step S600 includes: S610, defining an evaluation agent, defining the agent type as an evaluator, and naming the evaluation agent; S620, defining the evaluation strategy, defining the evaluation strategy of the evaluation agent: S630, defining a pre-prompt word template of the evaluation agent, defining a pre-prompt word template of the evaluation agent, including an evaluation SQL statement generation task description, a solution result, and an evaluation idea, wherein the evaluation SQL statement generation task description includes a SQL statement generation task description prompt word, and the solution result includes the SQL statement generated in S400 and the execution result of S500; S640, defining a post-prompt word template of the evaluation agent, defining a post-prompt word template of the evaluation agent, including a response format and response requirements; S650, configuring a large language model of the evaluation agent, configuring the large language model of the evaluation agent; S660, parsing the method definition of evaluating the response of the intelligent agent, defining the method of parsing the evaluation of the response of the intelligent agent; S670, evaluating the SQL statement execution result, the evaluation agent evaluating the execution result, and feeding back the evaluation result to the role assignment agent and the decision-making agent.

9. The method for end-to-end batch generation of SQL statements based on multi-agents as claimed in claim 8, characterized in that: The maximum number of threads is 6.

Citation Information

Patent Citations

  • Data analysis system based on multi-agent cooperation

    CN118210828A

  • Intelligent SQL query generation method and system based on large language model

    CN118861081A