A method and system for intelligent SQL query generation based on large language model

Through multi-agent collaboration mechanism and chain reasoning technology, it solves the problem of performance degradation of existing Text-to-SQL methods when dealing with large-scale databases and complex user problems, and achieves efficient and accurate SQL query generation and optimization.

CN118861081BActive Publication Date: 2025-01-21SHANDONG INSPUR SCI RES INST CO LTD

Patent Information

Application Number
CN202411345131.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-26
Publication Date
2025-01-21
Estimated Expiration
2044-09-26

AI Technical Summary

Technical Problem

Existing Text-to-SQL methods based on large language models significantly degrade when dealing with large-scale databases and complex user problems that require multi-step inference, and fail to fully utilize the collaboration potential of LLMs with other tools and models.

Method used

A multi-agent collaboration mechanism is adopted, including a trainer agent, a task planner agent and a SQL executor agent. The large language model is fine-tuned through the LLAMA-Factory framework and Qlora fine-tuning technology, and chain reasoning and external tools are used to execute SQL. Query, implement SQL query generation and optimization.

Benefits of technology

Significantly improves SQL query generation performance in large-scale databases and complex user problem scenarios, improves automation and accuracy, and reduces data collection and maintenance costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118861081B_ABST
    Figure CN118861081B_ABST
Patent Text Reader

Abstract

The present invention proposes an intelligent SQL query generation method and system based on a large language model, which belongs to the field of artificial intelligence technology, including: collecting Text-to-SQL related data through a trainer agent, fine-tuning the large language model based on the LLAMA-Factory framework and the Qlora fine-tuning technology, and converting the Text-to-SQL related data into SQL statements using the fine-tuned large language model; gradually decomposing the complex text problem of the SQL statement into simpler sub-problems through a task planner agent, and generating SQL queries by chain reasoning; correcting the erroneous SQL query according to feedback through a SQL executor, and transmitting the result obtained by the corrected SQL query to the user; the method and system are suitable for various application scenarios requiring automatic SQL query generation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of artificial intelligence technology, and in particular relates to an intelligent SQL query generation method and system based on a large language model. Background Art

[0002] With the rapid development of information technology, the widespread application of database technology has put forward more stringent requirements for data retrieval and analysis. The traditional SQL query method relies on users to have certain knowledge of database query language, which not only limits the popularity and application scope of database technology, but also increases the difficulty of use for non-professional users. In order to improve the accessibility and ease of use of databases, researchers have been exploring how to automatically convert natural language text into structured SQL queries. This process is called the Text-to-SQL task. In this way, users can interact with the database like talking to humans, greatly reducing the technical threshold.

[0003] In recent years, the rise of deep learning technology has brought revolutionary progress to the field of natural language processing (NLP). Large language models (LLMs) learn rich language representations and potential semantic information through pre-training on massive data sets, enabling machines to understand natural language more deeply and perform complex language tasks. However, existing LLM-based Text-to-SQL methods often show a significant drop in performance when faced with large-scale databases and complex user questions that require multi-step reasoning. In addition, most existing methods fail to fully utilize the collaborative potential of LLMs with other tools and models, limiting their application effects. Summary of the invention

[0004] The present invention aims to solve the problems in the prior art. The present invention relates to an intelligent SQL query generation method and system based on a large language model, aiming to provide a solution that can improve the SQL query generation performance through multi-agent collaboration and instruction tuning, and improve the automation and accuracy of database queries. The technology is particularly suitable for processing large-scale databases and complex user problems that require multi-step reasoning, and can significantly improve performance in these scenarios.

[0005] In order to achieve the above-mentioned purpose, the technical solution adopted in this application is: an intelligent SQL query generation method based on a large language model, comprising: collecting Text-to-SQL related data through a trainer agent, fine-tuning the large language model based on the LLAMA-Factory framework and Qlora fine-tuning technology, and converting the Text-to-SQL related data into SQL statements using the fine-tuned large language model;

[0006] The task planner agent gradually decomposes the complex text problem of SQL statements into simpler sub-problems and generates SQL queries using chain reasoning;

[0007] The SQL executor agent is used to identify and remove database patterns in SQL queries that are not related to the problem, and external tools are used to execute SQL queries and obtain feedback. The erroneous SQL queries are corrected based on the feedback, and the results obtained from the corrected SQL queries are transmitted to the user.

[0008] Further, the construction of the trainer agent includes: the trainer agent includes a data collection and preprocessing module and a model fine-tuning module;

[0009] The data collection and preprocessing module collects database query logs and user interaction data, including natural language queries, SQL statements, and query results, and converts them into the format required for model training through preprocessing technology. At the same time, it obtains a large number of SQL statements from GitHub to enrich domain knowledge.

[0010] The model fine-tuning module uses the Qwen2-14b pre-trained model as its cornerstone, relies on the LLAMA-Factory framework and Qlora fine-tuning technology to perform supervised training on the large language model, and also uses the PEFT library to perform unsupervised fine-tuning on the large language model.

[0011] Furthermore, the task planner agent includes:

[0012] The demand analysis and task definition module uses the ReAct method to analyze application scenarios, capture user intent, and define tasks by combining large language models with SQL table structure analysis;

[0013] The task decomposition and scheduling module uses a few-sample thinking chain combined with ReAct to decompose complex queries into ordered subtasks and schedule them intelligently;

[0014] Knowledge graph construction module, which builds domain knowledge graph and integrates database information and query mode;

[0015] The reasoning and decision optimization module integrates Bayesian networks and reinforcement learning to optimize task execution strategies and ensure efficient output of accurate SQL statements.

[0016] Furthermore, the SQL executor agent includes a query optimization module, a SQL execution and result integration module, and a feedback module; the query optimization module improves efficiency through index optimization and query reorganization; the SQL execution module uses connection pool technology to seamlessly connect to the database to ensure smooth execution; the result integration and feedback module summarizes and organizes data, pre-processes formatting, handles null values, aggregations and table connections, and generates detailed logs and visual reports.

[0017] Furthermore, the construction of the Bayesian network follows the following steps:

[0018] Determine the set of variables in the network and the dependencies between the sets of variables;

[0019] Define the conditional probability distribution, that is, the conditional probability of each node given its parent node;

[0020] Use Bayes' theorem to update the posterior probability of variables to reflect the impact of new evidence on the decision;

[0021] The formula is:

[0022] Among them, P(X|E) is the posterior probability of variable X given evidence E, P(E|X) is the likelihood of E given X, and P(X) is the prior probability of X. is the iteration variable, Representative data samples.

[0023] Further, reinforcement learning includes:

[0024] Define the state space, action space and reward function, and the agent chooses actions in each state to obtain rewards;

[0025] Use Q-learning or SARSA algorithm to update the agent's strategy; the Q-learning update formula is:

[0026] Where Q(s,a) is the value function of the state-action pair, α is the learning rate, r is the immediate reward after performing action a, γ is the discount factor, s′ is the new state, and a′ is the action in the new state.

[0027] Furthermore, the generation of SQL statements follows the following steps:

[0028] Determine the query target and required data fields;

[0029] Based on the task decomposition and decision results, construct a WHERE clause to filter data;

[0030] Use JOIN, GROUP BY, and ORDER BY clauses to organize data to meet query requirements;

[0031] Optimize SQL statements and use indexes to improve query efficiency.

[0032] In a second aspect, the present invention provides an intelligent SQL query generation system based on a large language model, comprising:

[0033] The trainer agent is configured to: collect Text-to-SQL related data, fine-tune the large language model based on the LLAMA-Factory framework and Qlora fine-tuning technology, and convert the Text-to-SQL related data into SQL statements using the fine-tuned large language model;

[0034] The task planner agent is configured to: gradually decompose the complex text problem of SQL statements into simpler sub-problems and generate SQL queries using chain reasoning;

[0035] The SQL executor agent is configured to: identify and remove database patterns in SQL queries that are irrelevant to the problem, execute SQL queries using external tools and obtain feedback, correct erroneous SQL queries based on the feedback, and transmit the results obtained by the corrected SQL queries to the user.

[0036] In a third aspect, the present invention provides a computer-readable storage medium, which includes a stored program, wherein when the program is running, the device where the computer-readable storage medium is located is controlled to execute the intelligent SQL query generation method based on a large language model described in the first aspect.

[0037] In a fourth aspect, the present invention provides an electronic device comprising a memory for storing computer program instructions and a processor for executing the program instructions, wherein when the computer program instructions are executed by the processor, the electronic device is triggered to execute an intelligent SQL query generation method based on a large language model according to the first aspect.

[0038] Compared with the prior art, the advantages and positive effects of the present invention are:

[0039] The intelligent SQL query generation method and system of the present invention can effectively handle complex database query tasks through multi-agent collaboration; secondly, the chain reasoning and instruction fine-tuning technology are used to improve the generation accuracy of the model; finally, the method does not need to rely on a large amount of manually labeled data, reducing the cost of data collection and maintenance; the present invention is suitable for a variety of application scenarios that require automated SQL query generation, including but not limited to database management and data analysis fields, and has broad market application prospects; the present invention can further promote the popularization and application of database technology, provide users with a more convenient and intelligent data processing experience, and bring revolutionary changes to the fields of database management and data analysis. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

[0041] Figure 1 It is a structural schematic diagram of an innovative intelligent SQL query generation system based on a large language model of the present invention. DETAILED DESCRIPTION

[0042] In order to more clearly understand the above-mentioned purpose, features and advantages of the present invention, the present invention is further described below in conjunction with the accompanying drawings and embodiments. It should be noted that the embodiments of the present application and the features in the embodiments can be combined with each other without conflict.

[0043] In the following description, many specific details are set forth to facilitate a full understanding of the present invention. However, the present invention may also be implemented in other ways than those described herein. Therefore, the present invention is not limited to the specific embodiments of the following disclosure.

[0044] Embodiment 1, as Figure 1 As shown, the present invention proposes an innovative method and system for intelligent SQL query generation based on a large language model. The method and system effectively decompose complex Text-to-SQL tasks into more manageable subtasks through a multi-agent collaboration mechanism, and gradually solve these problems through chain reasoning. The core of this method is to introduce a multi-agent collaboration framework, which includes a trainer agent, a task planner agent, and an SQL executor agent. The trainer agent is responsible for collecting Text-to-SQL related data and fine-tuning the large language model to realize a dedicated model for Text-to-SQL; the task planner agent is responsible for gradually decomposing complex text problems into simpler sub-problems and generating SQL queries using chain reasoning. The SQL executor includes two auxiliary agents, namely a selector and a refiner. The selector agent reduces the interference of irrelevant information by identifying and removing database patterns that are not related to the problem; the refiner agent uses external tools to execute SQL and obtain feedback, thereby correcting incorrect SQL queries.

[0045] To achieve this goal, the present invention adopts a chain reasoning method with a small number of examples, and trains an open source model called SQL-Qwen through instruction fine-tuning. This model is based on Qwen2-32B, and through the ReAct agent construction method, it can not only complete tasks such as database simplification, problem decomposition, SQL generation and SQL correction, but also has high flexibility and scalability. Through this method, we can provide an efficient and accurate intelligent SQL query generation solution that can not only handle complex database query tasks, but also achieve high-performance natural language processing in resource-constrained environments.

[0046] The invention aims to promote the further popularization and application of database technology through intelligent agent collaboration and thought chain prompt engineering construction technology. This method can not only significantly improve the user experience, but also show its broad application prospects and practical value in various practical application scenarios. In this way, we hope to further promote the innovation and development of database technology and bring users a more convenient and intelligent data processing experience.

[0047] The present invention provides an intelligent SQL query generation method based on a large pre-trained language model, which aims to achieve efficient and accurate interaction between users and databases by building multiple agents based on the large language model and using multi-agent collaboration technology. The method uses deep learning technology, especially the application of large language models (LLMs), so that users can make query requests in the form of natural language, and the system can automatically convert them into accurate SQL query statements, thereby greatly improving the automation level of database queries and the convenience of user operations. The method specifically includes the following steps:

[0048] Step S1, trainer agent construction: focus on the development of special models in the field of Text2SQL. The agent consists of a data collection and preprocessing module and a model fine-tuning module. The data collection and preprocessing module collects database query logs and user interaction data extensively, covering natural language queries, SQL statements and query results, and converts them into the format required for model training through preprocessing technology. At the same time, a large number of SQL statements are obtained from sources such as github to enrich domain knowledge. The model fine-tuning module uses the Qwen2-14b pre-trained model as the cornerstone, relying on the LLAMA-Factory framework and Qlora fine-tuning technology to perform supervised training on the model to improve the conversion capability from natural language to SQL. In addition, the PEFT library is used to implement unsupervised fine-tuning of the model to enhance its understanding of language features and database query patterns in specific fields, thereby achieving more accurate Text2SQL conversion.

[0049] The specific process of constructing the trainer agent is as follows:

[0050] Step S11, data collection module: The intelligent agent first collects and organizes large-scale database query logs and user interaction data through the data collection module, including natural language queries, corresponding SQL query statements and query results. In order to increase the generalization ability of the model, data enhancement technology, back-translation, synonym replacement, etc. are used to expand the training data, so that the model can learn more diverse natural language expressions and SQL query patterns.

[0051] Step S12, application of preprocessing technology: After the data is collected, data preprocessing technology is used, such as word segmentation, removal of stop words, part-of-speech tagging, etc., to convert the original data into a (user natural language, SQL) pair format suitable for model training.

[0052] Step S13, data source expansion: The present invention also collects a large number of SQL statements from large website libraries such as GitHub, which facilitates the subsequent large language model to understand specific domain knowledge.

[0053] Step S14, model fine-tuning module starts: After the data is ready, the model fine-tuning module starts immediately and is dedicated to building a dedicated model in the Text2SQL field. After the data preprocessing and annotation work is successfully completed, the model fine-tuning module is activated immediately and focuses on building a dedicated model for the Text2SQL (natural language to SQL query conversion) field. The construction process of this dedicated model involves the following key technical contents:

[0054] Step S14a, domain adaptability enhancement: First, by analyzing the Text2SQL specific dataset, the domain-specific vocabulary, grammatical structure and common query patterns are identified. Subsequently, the domain-specific Masked Language Model (MLM) task is introduced during the model training process to enhance the model's ability to understand domain vocabulary and context.

[0055] Step S14b, SQL syntax-aware embedding: In order to improve the grammatical correctness of SQL statements generated by the model, SQL syntax-aware embedding technology is used to integrate the structural information of the SQL syntax tree into the embedding layer of the model, so that the model can take into account the grammatical rules of SQL statements when generating SQL.

[0056] Step S14c, multi-task learning framework: adopt the multi-task learning (MTL) framework, combine the main task of SQL statement generation with auxiliary tasks (SQL parsing, SQL execution result prediction, etc.) for joint training, so that the model can learn richer language representation and more comprehensive conversion knowledge.

[0057] Step S15, basic model selection: select the pre-trained large language model Qwen2-14b as the basic model to lay a solid foundation for subsequent fine-tuning.

[0058] Step S16, application of fine-tuning technology: Based on the LLAMA-Factory framework, Qlora fine-tuning technology is used to perform supervised fine-tuning training on the basic model to improve the model's conversion ability from natural language to SQL statements. Among them, the LLAMA-Factory framework is an extension and fine-tuning framework designed specifically for large language models. It provides a wealth of tools and interfaces to support researchers and developers to easily customize the training, evaluation and deployment of basic models. In the Text2SQL task, the LLAMA-Factory framework is responsible for managing the entire process of model loading, data preprocessing, training loops, model evaluation and result output. Qlora is a fine-tuning technology customized based on the LLaMA model, which is specially optimized for SQL generation tasks. Using supervised learning, the basic model is refined through a carefully designed cross-entropy loss function and regularization strategy L2. The formula for the cross-entropy loss function is as follows:

[0059] Where: M is the number of categories. y is a one-hot encoded vector representing the true label. p is the probability distribution vector predicted by the model. i: represents the index of the sample, i.e. the i-th sample. c: represents the index of the category, i.e. the c-th category.

[0060] The regular expression formula is as follows:

[0061] Where: λ is the regularization coefficient, which controls the strength of regularization. w is the weight vector of the model. D is the number of weights. N is the number of samples, j is the weight index, and R is the regular expression.

[0062] During the training process, Qlora technology focuses on improving the model's ability to understand the SQL statement structure and the generation accuracy, while reducing the risk of overfitting. In terms of specific implementation, Qlora uses SQL template matching and syntax tree edit distance as part of the training objectives to guide the model to generate query statements that are more in line with SQL syntax.

[0063] Step S17, deepening understanding of specific fields: In order to enable the model to better understand the language features and database query patterns of specific fields, the present invention implements unsupervised fine-tuning of the large language model through the PEFT library.

[0064] Step S18, model training and optimization: Through the collected data and fine-tuning technology, the model is continuously trained and optimized to ensure that the model can effectively convert natural language into SQL statements.

[0065] Step S19, result verification and output: Finally, the agent will verify the results to ensure that the trained model can meet the expected performance standards and output them for use by other modules or further research.

[0066] Step S110, through the collaborative work of these sub-modules, the intelligent agent of the present invention can efficiently perform its tasks, automatically train and fine-tune the dedicated model in the Text2SQL field, and ensure that the information processing capabilities provided to users are accurate and efficient.

[0067] Step S2, construction of the task planner agent: This agent is the core system, which aims to enhance the understanding and decomposition capabilities of complex Text2SQL tasks through the construction of the task planner agent. The system consists of four core modules: demand analysis and task definition, task decomposition and scheduling, knowledge graph construction, and reasoning and decision optimization. First, the ReAct method is used to analyze the application scenario, capture user intent, and define tasks by combining a large language model with SQL table structure analysis. Subsequently, the task decomposition module uses a few-sample thinking chain combined with ReAct to decompose complex queries into ordered subtasks and schedule them intelligently. At the same time, a domain knowledge graph is constructed to integrate database information and query patterns to enhance context understanding capabilities. Finally, the reasoning and decision-making module integrates Bayesian networks and reinforcement learning to optimize task execution strategies, ensure efficient output of accurate SQL statements, and achieve new breakthroughs in Text2SQL task planning. The specific process is as follows:

[0068] Step S21, demand analysis and task definition: The agent first conducts demand analysis, deeply analyzes the Text2SQL application scenario based on the ReAct method, determines user intent, and identifies key task requirements and user interaction patterns. It identifies user intent by building a prompting project similar to the ReAct method, that is, the think-action-observation model. The specific technical content is as follows:

[0069] Step S21a, thinking phase: The agent uses natural language processing (NLP) technology, especially the large language model Qwen2, to perform deep semantic analysis on the user's natural language query. Through entity recognition, relationship extraction and intent understanding, the agent can accurately capture key information in the user's query, such as database table name, field name, operation type (such as SELECT, WHERE, JOIN, etc.) and complex query logic. In the thinking phase, the agent uses a Qwen2-based attention mechanism to enhance the ability to capture key information and identify key entities in the query and the relationships between them.

[0070] At the same time, combined with knowledge graph (KG) technology, a semantic graph of the query statement is constructed to more intuitively represent the complex structure of the query.

[0071] Step S21b, action phase: Based on the analysis results of the thinking phase, the agent generates the corresponding SQL query statement. This process involves multiple steps such as template matching, slot filling and query optimization. The agent uses the existing template library to quickly generate the basic framework, and then fills the parsed entities and relationships into the template through slot filling technology. In addition, the agent also uses query optimization algorithms (query rewriting, index optimization) to improve the execution efficiency of the generated SQL statements.

[0072] Step S21c, observation phase: The agent executes the generated SQL query and observes the results returned by the database. By comparing the query results expected by the user with the actual results returned, the agent can evaluate the correctness and completeness of the query statement. If a mismatch is found, the agent will return to the thinking phase to re-analyze the user query and adjust the action strategy.

[0073] Step S22, task decomposition and scheduling module design: This module automatically decomposes complex user requests into multiple subtasks. Based on the thinking chain method of a few samples and combined with the ReAct method to build a task decomposer, it can identify and decompose complex user requests and schedule them according to task priority. The specific technical content is as follows:

[0074] Step S22a, task decomposer construction:

[0075] Step S22ai: Input preprocessing: Perform preprocessing operations such as word segmentation, part-of-speech tagging, and syntactic analysis on the complex natural language query submitted by the user, providing a basis for subsequent task decomposition.

[0076] Step S22aii: Task identification and division. Automatically identify subtasks in the query based on predefined task templates or using unsupervised learning methods (clustering algorithms). Each subtask corresponds to a specific SQL operation or data processing step.

[0077] Step S22aiii: Dependency construction. Use a graph structure to represent the dependencies between subtasks (e.g., perform a JOIN operation before performing a SELECT operation). By constructing a task dependency graph, the agent can clearly understand the execution order and dependencies between subtasks.

[0078] Step S22aiv: In the process of task identification and division, the agent uses rule-based methods (such as regular expression matching) or machine learning-based methods (such as decision trees) to identify subtasks. At the same time, a graph neural network (GNN) is used to construct and reason about the task dependency graph.

[0079] Step S22b, task scheduling:

[0080] Step S22bi, based on the task dependency graph, the agent uses algorithms such as priority queue or topological sorting to schedule the execution order of subtasks, giving priority to subtasks with no dependencies or few dependencies to improve the overall execution efficiency.

[0081] Step S22bii, during the execution process, the intelligent agent will also adjust the scheduling strategy based on real-time feedback, such as giving priority to high-priority tasks or re-planning the task execution path when encountering resource bottlenecks.

[0082] Step S23, knowledge graph construction: In order to further improve the task planning capability, the present invention constructs a domain knowledge graph. The knowledge graph includes database schema information (table structure, field type and relationship) and common query patterns, helping the agent to better understand the context of the database when planning tasks.

[0083] Step S24, reasoning and decision module optimization: After the task is decomposed, the agent is optimized through the reasoning and decision module to determine the best execution order and method for each subtask. The present invention optimizes the reasoning and decision module based on Bayesian network and reinforcement learning technology. The Bayesian network provides decision support through probabilistic reasoning, while reinforcement learning optimizes the task execution strategy through continuous trial and error. Specifically:

[0084] Step S24a, Bayesian network optimization: Bayesian network is a probabilistic graphical model that implements probabilistic reasoning by representing conditional dependencies between variables. In the present invention, Bayesian network is used to construct a dependency graph between tasks, and to evaluate the probabilistic impact of the execution order of different subtasks through Bayesian reasoning. The construction of Bayesian network usually follows the following steps:

[0085] Step S24ai, determining the set of variables in the network and the dependencies between them.

[0086] Step S24aii, defining a conditional probability distribution (CPD), that is, the conditional probability of each node given its parent node.

[0087] Step S24aiii, using Bayes' theorem to update the posterior probability of the variable to reflect the impact of new evidence on the decision.

[0088] Step S24aiv, the formula is expressed as:

[0089] Among them, P(X|E) is the posterior probability of variable X given evidence E, P(E|X) is the likelihood of E given X, and P(X) is the prior probability of X. is the iteration variable, Representative data samples, and n is the total number of samples.

[0090] Step S24b, reinforcement learning optimization: reinforcement learning is a method that allows an agent to learn the optimal strategy through interaction with the environment. In the present invention, reinforcement learning is used to continuously adjust the task execution strategy to maximize the cumulative reward for task completion. Specific technologies include:

[0091] Step S24bi, define the state space, action space and reward function, and the agent selects an action in each state to obtain a reward.

[0092] Step S24bii, using algorithms such as Q-learning or SARSA to update the agent's strategy. Taking Q-learning as an example, its update formula is:

[0093] Where Q(s,a) is the value function of the state-action pair, α is the learning rate, r is the immediate reward after performing action a, γ is the discount factor, s′ is the new state, and a′ is the action in the new state.

[0094] Step S24biii, by combining the probabilistic reasoning of the Bayesian network and the strategy optimization of reinforcement learning, the reasoning and decision-making module can plan the execution order and method of the tasks more accurately and efficiently.

[0095] Step S25, SQL statement generation: Finally, the agent outputs the most optimized SQL statement to ensure the accuracy of task planning and the effectiveness of execution. The generation of SQL statements follows the following steps:

[0096] Step S25a, determining the query target and required data fields.

[0097] Step S25b, constructing a WHERE clause to filter data according to the task decomposition and decision results.

[0098] Step S25c, using clauses such as JOIN, GROUP BY, ORDER BY, etc. to organize data to meet query requirements.

[0099] Step S25d, optimize the SQL statement, such as using indexes, avoiding full table scans, etc., to improve query efficiency.

[0100] Step S26, performance evaluation and feedback: The agent also includes a performance evaluation mechanism to monitor and evaluate the effectiveness of task planning and execution, and provide feedback to further optimize the performance of the agent.

[0101] Step S27, user interface and interaction: The agent interacts with the user through a friendly user interface to ensure that the user can clearly understand the results of the task planning and make adjustments as needed.

[0102] Step S28, continuous learning and updating: The intelligent agent has the ability of self-learning and updating, and can continuously optimize its own task planning strategy based on new data and user feedback.

[0103] Through the collaborative work of these sub-modules, the agent is able to perform its tasks efficiently, automatically plan and execute complex tasks in the Text2SQL domain, and ensure that the task planning provided to the user is accurate and reliable.

[0104] Step S3, SQL executor agent: As the system execution engine, the SQL executor agent is responsible for efficiently converting the SQL queries generated by the planner into database operations and processing the results. Its core includes query optimization, SQL execution, and result integration and feedback modules. The query optimization module improves efficiency through index optimization and query reorganization; the SQL execution module uses connection pool technology to seamlessly connect to the database to ensure smooth execution; the result integration and feedback module summarizes and organizes data, pre-processes formatting, handles null values, aggregations and table connections, and generates detailed logs and visual reports. This agent not only completes the closed loop from natural language to database operations, but also optimizes the system through a feedback mechanism, improves user experience, and ensures that query requirements are met efficiently and accurately. The specific process is as follows:

[0105] Step S31, query optimization module: This module is responsible for receiving SQL queries from the task planner, analyzing and optimizing them. It selects appropriate indexes and rearranges the query order to improve query efficiency.

[0106] Step S32, SQL execution module: This module interacts with the database management system (such as MySQL, PostgreSQL, etc.) to execute the optimized SQL query. The connection pool technology is used to reduce the overhead caused by frequent database connections.

[0107] Step S33, result integration and feedback module: This module is responsible for summarizing, organizing and preprocessing the query results, converting the original query results into an intuitive and easy-to-read form through formatting operations, and feeding back the results to the user.

[0108] Step S34, data preprocessing: During the result integration process, it also includes null value processing, aggregation operations and table connections, etc., to ensure that the returned results are accurate and complete.

[0109] Step S35, query log recording: In order to improve the user experience, a query log is generated to record the query process and results for subsequent analysis and optimization.

[0110] Step S36, application of data visualization technology: The result integration and feedback module uses data visualization technology to provide result presentation in the form of charts, statistical graphs, etc., so that users can understand the query results more intuitively.

[0111] Step S37, performance monitoring and optimization: The SQL executor agent continuously optimizes system performance and improves user satisfaction through a data feedback mechanism.

[0112] Step S38, user interaction and feedback: The agent interacts with the user through a friendly user interface to ensure that the user can clearly understand the query results and make adjustments as needed.

[0113] Step S39, continuous learning and updating: The intelligent agent has the ability of self-learning and updating, and can continuously optimize its execution strategy based on new data and user feedback.

[0114] Through the collaborative work of these sub-modules, the SQL executor agent can efficiently perform its tasks, complete the conversion and execution from natural language to SQL statements, and continuously optimize system performance through a data feedback mechanism to ensure that the information provided to users is accurate, reliable and easy to understand.

[0115] The intelligent SQL query generation method and device of the present invention have the following advantages: first, through multi-agent collaboration, it can effectively handle complex database query tasks; second, by using chain reasoning and instruction fine-tuning technology, the generation accuracy of the model is improved; finally, the method does not need to rely on a large amount of manually labeled data, reducing the cost of data collection and maintenance. The present invention is suitable for a variety of application scenarios that require automated SQL query generation, including but not limited to database management and data analysis fields, and has broad market application prospects. The present invention can further promote the popularization and application of database technology, provide users with a more convenient and intelligent data processing experience, and bring revolutionary changes to the fields of database management and data analysis.

[0116] Embodiment 2: This embodiment provides an intelligent SQL query generation system based on a large language model, including:

[0117] The trainer agent is configured to: collect Text-to-SQL related data, fine-tune the large language model based on the LLAMA-Factory framework and Qlora fine-tuning technology, and convert the Text-to-SQL related data into SQL statements using the fine-tuned large language model;

[0118] The task planner agent is configured to: gradually decompose the complex text problem of SQL statements into simpler sub-problems and generate SQL queries using chain reasoning;

[0119] The SQL executor agent is configured to: identify and remove database patterns in SQL queries that are irrelevant to the problem, execute SQL queries using external tools and obtain feedback, correct erroneous SQL queries based on the feedback, and transmit the results obtained by the corrected SQL queries to the user.

[0120] Embodiment 3, this embodiment provides a computer-readable storage medium, the computer-readable storage medium includes a stored program, wherein when the program is running, the device where the computer-readable storage medium is located is controlled to execute the intelligent SQL query generation method based on a large language model described in Embodiment 3.

[0121] Embodiment 4. This embodiment provides an electronic device, comprising a memory for storing computer program instructions and a processor for executing program instructions, wherein when the computer program instructions are executed by the processor, the electronic device is triggered to execute an intelligent SQL query generation method based on a large language model in Embodiment 1.

[0122] The electronic device may include: a processor, a memory and a communication unit. These components communicate via one or more buses. Those skilled in the art will appreciate that the structure of the electronic device does not limit the embodiments of the present invention, and it may be a bus structure or a star structure, or a combination of certain components, or different component arrangements.

[0123] The communication unit is used to establish a communication channel so that the electronic device can communicate with other devices, receive user data sent by other devices or send user data to other devices.

[0124] The processor is the control center of the electronic device. It uses various interfaces and lines to connect various parts of the entire electronic device. It runs or executes software programs and / or modules stored in the memory, and calls data stored in the memory to perform various functions of the electronic device and / or process data. The processor can be composed of an integrated circuit (IC), for example, it can be composed of a single packaged IC, or it can be composed of multiple packaged ICs with the same or different functions. For example, the processor can only include a central processing unit (CPU). In an embodiment of the present invention, the CPU can be a single computing core or multiple computing cores.

[0125] The memory is used to store the execution instructions of the processor. The memory can be implemented by any type of volatile or non-volatile storage device or a combination of them, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.

[0126] When the execution instructions in the memory are executed by the processor, the electronic device is enabled to execute part or all of the steps of Embodiment 1.

[0127] The above description is only a preferred embodiment of the present invention and does not limit the present invention in other forms. Any technician familiar with the profession may use the technical content disclosed above to change or modify it into an equivalent embodiment with equivalent changes and apply it to other fields. However, any simple modification, equivalent change and modification made to the above embodiment based on the technical essence of the present invention without departing from the content of the technical solution of the present invention still falls within the protection scope of the technical solution of the present invention.

Claims

1. An intelligent SQL query generation method based on a large language model, characterized in that: include: Collect Text-to-SQL related data through the trainer agent, fine-tune the large language model based on the LLAMA-Factory framework and Qlora fine-tuning technology, and use the fine-tuned large language model to convert Text-to-SQL related data into SQL statements; The task planner agent gradually decomposes the complex text problem of SQL statements into simpler sub-problems and generates SQL queries using chain reasoning; Identify and remove database patterns that are not relevant to the problem in SQL queries through SQL executor agents, use external tools to execute SQL queries and obtain feedback, correct incorrect SQL queries based on the feedback, and transmit the results obtained from the corrected SQL queries to users; The mission planner agent includes: The demand analysis and task definition module uses the ReAct method to analyze application scenarios, capture user intent, and define tasks by combining large language models with SQL table structure analysis; The task decomposition and scheduling module uses a few-sample thinking chain combined with ReAct to decompose complex queries into ordered subtasks and schedule them intelligently; Knowledge graph construction module, which builds domain knowledge graph and integrates database information and query mode; The reasoning and decision optimization module integrates Bayesian networks and reinforcement learning to optimize task execution strategies and ensure efficient output of accurate SQL statements.

2. According to claim 1, a method for generating intelligent SQL queries based on a large language model is characterized in that: The trainer agent construction includes: the trainer agent includes a data collection and preprocessing module and a model fine-tuning module; The data collection and preprocessing module collects database query logs and user interaction data, including natural language queries, SQL statements, and query results, and converts them into the format required for model training through preprocessing technology. At the same time, SQL statements are obtained from GitHub to enrich domain knowledge. The model fine-tuning module uses the Qwen2-14b pre-trained model as its cornerstone, relies on the LLAMA-Factory framework and Qlora fine-tuning technology to perform supervised training on the large language model, and also uses the PEFT library to perform unsupervised fine-tuning on the large language model.

3. The method for generating intelligent SQL queries based on a large language model according to claim 1, characterized in that: The SQL executor agent includes a query optimization module, a SQL execution and result integration module, and a feedback module; the query optimization module improves efficiency through index optimization and query reorganization; the SQL execution module uses connection pool technology to seamlessly connect to the database to ensure smooth execution; The result integration and feedback module summarizes and organizes data, pre-processes formatting, handles null values, aggregation and table joins, and generates detailed logs and visual reports.

4. The method for generating intelligent SQL queries based on a large language model according to claim 1, characterized in that: The construction of the Bayesian network follows the following steps: Determine the set of variables in the network and the dependencies between the sets of variables; Define the conditional probability distribution, that is, the conditional probability of each node given its parent node; Use Bayes' theorem to update the posterior probability of variables to reflect the impact of new evidence on the decision; The formula is: Among them, P(X|E) is the posterior probability of variable X given evidence E, P(E|X) is the likelihood of E given X, and P(X) is the prior probability of X. is the iteration variable, Representative data samples, and n is the total number of samples.

5. The method for generating intelligent SQL queries based on a large language model according to claim 1, characterized in that: The reinforcement learning includes: Define the state space, action space and reward function, and the agent chooses actions in each state to obtain rewards; Use Q-learning or SARSA algorithm to update the agent's strategy; the Q-learning update formula is: Where Q(s,a) is the value function of the state-action pair, α is the learning rate, r is the immediate reward after performing action a, γ is the discount factor, s′ is the new state, and a′ is the action in the new state.

6. The method for generating intelligent SQL queries based on a large language model according to claim 1, characterized in that: The generation of the SQL statement follows the following steps: Determine the query target and required data fields; Based on the task decomposition and decision results, construct a WHERE clause to filter data; Use JOIN, GROUP BY, and ORDER BY clauses to organize data to meet query requirements; Optimize SQL statements and use indexes to improve query efficiency.

7. An intelligent SQL query generation system based on a large language model, characterized in that: include: The trainer agent is configured to: collect Text-to-SQL related data, fine-tune the large language model based on the LLAMA-Factory framework and Qlora fine-tuning technology, and convert the Text-to-SQL related data into SQL statements using the fine-tuned large language model; The task planner agent is configured to: gradually decompose the complex text problem of SQL statements into simpler sub-problems and generate SQL queries using chain reasoning; The SQL executor agent is configured to: identify and remove database patterns in the SQL query that are not relevant to the problem, execute the SQL query using an external tool and obtain feedback, correct the erroneous SQL query based on the feedback, and transmit the results obtained by the corrected SQL query to the user; The mission planner agent includes: The demand analysis and task definition module uses the ReAct method to analyze application scenarios, capture user intent, and define tasks by combining large language models with SQL table structure analysis; The task decomposition and scheduling module uses a few-sample thinking chain combined with ReAct to decompose complex queries into ordered subtasks and schedule them intelligently; Knowledge graph construction module, which builds domain knowledge graph and integrates database information and query mode; The reasoning and decision optimization module integrates Bayesian networks and reinforcement learning to optimize task execution strategies and ensure efficient output of accurate SQL statements.

8. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored program, wherein when the program is running, the device where the computer-readable storage medium is located is controlled to execute the intelligent SQL query generation method based on a large language model as described in any one of claims 1-6.

9. An electronic device, characterized in that: It comprises a memory for storing computer program instructions and a processor for executing the program instructions, wherein when the computer program instructions are executed by the processor, the electronic device is triggered to execute an intelligent SQL query generation method based on a large language model as described in any one of claims 1-6.

Citation Information

Patent Citations

  • Method for calling data system interface through natural language

    CN117290429A

  • Structured query language generation method and device, electronic equipment and storage medium

    CN118093621A

Cited By

  • Metadata adaptive compression method and system for natural language model

    CN122614879A