A natural language intelligent query method and device based on multi-agent interaction

CN118012900BActive Publication Date: 2026-09-25ZHEJIANG UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311766938.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-12-21
Publication Date
2026-09-25
Estimated Expiration
2043-12-21

AI Technical Summary

Technical Problem

然而,将单一的LLM作为NL-to-SQL框架的底层实现会面临很多限制,包括无法复用历史查询结果、难以对错误生成结果进行纠正等

Benefits of technology

[0048]基于LLM能力构建功能更为完善且具有交互能力的智能体可以有效缓解上述缺陷。本发明提出的基于多智能体交互方法能够克服单一LLM的局限性。其主要思想为将NL-to-SQL任务中的提示词构建分散到各个模块中。作为智能查询流程核心的代码生成组件在收到SQL生成请求后会首先在知识库中基于历史查询记录获取和本次查询近似的查询,作为LLM输入的上下文。在LLM完成SQL生成后,执行器根据SQL的执行结果向代码修正模块输出。代码修正模块主要从语义层面对生成SQL进行检查并在必要时进行修正。本方法泛用性较强,各个模块之间通过插件化的方式相互配合,可以根据需要灵活更改各个组件的底层实现,除了本发明主要针对的NL-to-SQL问题,对于其他生成式问题,该方法都有很好的适应性,具有广泛的应用价值。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118012900B_ABST
    Figure CN118012900B_ABST
Patent Text Reader

Abstract

A natural language intelligent query method and device based on multi-agent interaction, an agent based on a large language model (LLM) is responsible for outputting a corresponding structured data query statement (SQL) after receiving a database related structure description and a natural language described query problem, the method comprising: 1) an agent verifier receives user data query description and database structure information, and constructs a prompt word; 2) the verifier sends the prompt word to the agent executor, and the executor generates and executes SQL according to the prompt word; 3) the executor sends a binary tuple containing the SQL execution result and the SQL to the verifier, and the verifier corrects the SQL according to the execution result. Compared with the original LLM, the application has more excellent performance on the natural language to structured query language conversion (NL-to-SQL) task.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of data analysis and relates to a natural language intelligent query method and device based on multi-agent interaction. Technical Background

[0002] Data extraction and analysis based on databases and other data storage components often require users to manually write corresponding Structured Query Language (SQL). As the amount of data increases and the data structure becomes more complex, the complexity of the corresponding SQL also increases. For ordinary users without relevant knowledge background, the learning threshold for independently querying and processing data also increases. At the same time, as the complexity of SQL increases, the probability of errors in manual writing also increases.

[0003] NL-to-SQL is not a new problem. Traditional solutions have typically relied on machine learning models related to natural language processing. However, with the rise of large language models, many works have emerged that use LLMs for NL-to-SQL. Emerging LLMs like GPT4 and Llama2 naturally have advantages over traditional deep learning models like BERT and T5 in terms of model size and training corpora, achieving better results in code generation problems like NL-to-SQL. However, using a single LLM as the underlying implementation of an NL-to-SQL framework faces many limitations, including the inability to reuse historical query results and the difficulty in correcting erroneous generated results. Summary of the Invention

[0004] The present invention aims to overcome the above-mentioned shortcomings of the prior art and provide a natural language intelligent query method based on multi-agent interaction.

[0005] This invention helps users achieve structured data querying and analysis through natural language descriptions. It utilizes a multi-agent interaction method to convert natural language to structured query language (NL-to-SQL). This invention primarily targets application scenarios where users describe their data query needs using natural language, and through conversational interaction with an agent, ultimately obtain query results that meet their requirements. Throughout the query process, users do not need specialized data processing knowledge; they only need to express their needs and provide feedback in a conversational manner using natural language to achieve data querying and analysis that previously required manually writing complex SQL.

[0006] To achieve its objectives, the present invention employs the following technical solution:

[0007] The first aspect of this invention relates to a natural language intelligent query method based on multi-agent interaction, which converts the user's natural language-described data query requirements into a structured query language and returns the execution results, including the following steps:

[0008] 1) The intelligent agent verifier receives user data query descriptions and database structure information, and constructs prompt words, including the following steps:

[0009] 11) The verifier sends the user data query description and database structure information to the knowledge base module. The knowledge base performs a keyword masking operation on the database-related keywords in the data query description based on the keyword masking algorithm.

[0010] 12) The knowledge base module finds several historical query records with similar descriptions to the masked user data query from the vector database based on semantic similarity, as well as the corresponding structured query language SQL.

[0011] 13) The knowledge base module retrieves the user's SQL generation preferences from the key-value store based on the current username;

[0012] 14) The knowledge base module sends historical query records and user-generated preferences to the verifier. The verifier combines the historical query records, user-generated preferences, user data query descriptions, and database structure information to generate prompt words.

[0013] 15) The verifier sends the prompt to the knowledge base module, which records the prompt as the LLM dialogue context;

[0014] 2) The verifier sends the prompt to the agent executor, which generates and executes SQL based on the prompt:

[0015] 21) The executor sends the SQL to the code generation module, which calls the relevant LLM interface and guides the LLM to generate the target SQL based on the generated prompts;

[0016] 22) The executor sends the SQL and database structure information to the executor module. The executor module executes the SQL and obtains the execution results, specifically including:

[0017] 221) The executor module selects the corresponding database based on the database structure information and establishes a connection with it;

[0018] 222) The executor executes the SQL on the data and saves the execution result in the form of a tuple. The tuple contains a boolean value that determines whether the SQL was executed correctly. If the SQL was executed correctly, the other value in the tuple is the data returned after the SQL was executed; otherwise, it contains the SQL execution error message. The executor returns the tuple to the agent executor.

[0019] 3) The executor sends a tuple containing the SQL execution result and the SQL to the verifier. The verifier modifies the SQL based on the execution result, including:

[0020] 31) The executor sends the tuple and SQL to the code correction module, which generates SQL modification suggestions, specifically including:

[0021] 311) If the boolean value in the tuple is false, the code correction module calls the underlying LLM interface to instruct the LLM to generate a corresponding natural language description for the SQL execution error information in the tuple, and generates SQL modification suggestions based on the description.

[0022] 312) If the boolean value in the tuple is true, the code correction module calls the underlying LLM related interface to guide the LLM to interpret the execution semantics of the SQL and output the corresponding natural language description. The description and the user data query description are used as input to the LLM to guide the LLM to determine whether the semantics of the two are consistent. If they are inconsistent, the corresponding SQL correction suggestion is output. If they are consistent, the natural language description of the SQL execution semantics is output.

[0023] 32) The code correction module sends the SQL correction suggestions generated by the LLM in the above steps to the verifier. The verifier decides whether to return the result to the user or perform SQL correction based on the specific content of the correction suggestions, which includes:

[0024] 321) If the suggested correction is empty, the verifier sends the SQL, the SQL execution result, and its natural language description to the user. If the user is satisfied with the generated result, the intelligent query process terminates. Otherwise, proceed to step 322).

[0025] 322) When the correction suggestion is not empty or the user feedback in step 321) is not empty, execute this step. The verifier calls the knowledge base module interface to obtain the LLM historical dialogue context, and concatenates the correction suggestion or user feedback as new dialogue content after it as the prompt words for the new round of SQL generation. Then jump to step 2) and repeat the above steps until the maximum number of iterations is reached or the query process terminates in step 321).

[0026] In the code generation module described in step (21), the LLM prompts consist of four parts: generation instructions, generation examples, problem descriptions, and database structures. In addition to the fixed-format prompts, the generation instructions also include historical error-related information obtained from interaction with the knowledge base, which helps the LLM avoid generating specific errors. The generation examples are also provided by the knowledge base, and their purpose is to improve the quality of generated SQL by utilizing the context learning capabilities of the LLM. The database structure is recorded in the form of a data description language, and its purpose is to fully include structural information such as primary keys and foreign keys to ensure the generation quality of complex SQL.

[0027] In the knowledge base module described in step (1), a historical query matching algorithm based on word masking is designed. The historical query matching algorithm based on word masking first takes multiple word sequences of different lengths as units, obtains word sequences that match the database table and column names in the problem description according to the string matching algorithm, performs masking operation on them, performs vector embedding operation on them through the language model, and then selects the historical descriptions with the highest matching degree with the historical data in the vector database based on semantic similarity and KNN algorithm as generated examples.

[0028] In the code correction module described in step (3), specific prompt words are designed to guide the LLM to convert the SQL generated in the code generation module into a description in natural language form; and based on the semantic understanding ability of the LLM, it is guided to obtain modification suggestions by comparing the original problem description and combining the SQL execution results of the executor, which are used to guide the next round of SQL generation in the code generation module.

[0029] Four modules are combined to form an intelligent agent, a verifier, and an executor. The intelligent agents interact to implement an intelligent query process. Users input data query requirements described in natural language through a specified interface, specify a particular database, and ultimately directly obtain the execution results of the SQL corresponding to the data query requirements.

[0030] A second aspect of the present invention relates to a natural language intelligent query system based on multi-agent interaction, comprising the following modules:

[0031] Module 1: Code generation module

[0032] Leveraging the generative capabilities of the Large Language Model (LLM), the corresponding Structured Query Language (SQL) is generated based on the input data query description and database structure information.

[0033] Module 2: Actuator

[0034] Execute the SQL generated in the code generation module on the corresponding database and record the execution information, including the SQL execution results and error messages.

[0035] Module 3: Knowledge Base

[0036] It is responsible for storing historical query information in the intelligent query process, including historical question descriptions and SQL, historical error information, and interacting with other modules in the SQL generation process. The knowledge base is also responsible for selecting the most matching queries from historical query information based on the current query as generation examples; the underlying storage implementation of the knowledge base is a vector database.

[0037] Module 4: Code Correction Module

[0038] The code correction module generates evaluations and modification suggestions for the SQL generated in the code generation module based on the semantic understanding capabilities of LLM, and generates modification suggestions in natural language form in combination with the execution information of the executor. Based on the specific modification suggestions, the code correction module decides to return the execution result of the current SQL to the user or return to the code generation module for the next round of SQL generation based on the modification suggestions to optimize the SQL generated in module 1.

[0039] Furthermore, in the code generation module, the LLM prompts consist of four parts: generation instructions, generation examples, problem descriptions, and database structures. In addition to fixed-format prompts, the generation instructions also include historical error-related information obtained from interaction with the knowledge base, which helps the LLM avoid generating specific errors. The generation examples are also provided by the knowledge base, and their purpose is to leverage the LLM's contextual learning capabilities to improve the quality of generated SQL. The database structure is recorded in the form of a data description language, and its purpose is to fully include structural information such as primary keys and foreign keys to ensure the quality of generated complex SQL.

[0040] Furthermore, in the knowledge base, a historical query matching algorithm based on lexical masking is designed. The historical query matching algorithm based on lexical masking first takes multiple lexical sequences of different lengths as units, obtains lexical sequences in the question description that match the database table and column names according to the string matching algorithm, performs a masking operation on them, performs vector embedding operation on them through a language model, and then selects the several historical descriptions with the highest matching degree with the historical data in the vector database based on semantic similarity and the KNN algorithm as generated examples.

[0041] Furthermore, in the code correction module, specific prompts are designed to guide the LLM to convert the SQL generated in the code generation module into a description in natural language form; and based on the LLM's semantic understanding capabilities, it is guided to obtain modification suggestions by comparing the original problem description and combining the SQL execution results of the executor, which are used to guide the next round of SQL generation in the code generation module.

[0042] Furthermore, in the intelligent query process implemented by the combination of four modules in the natural language intelligent query method, the user inputs the data query requirements described in natural language through a specified interface, specifies a specific database, and finally directly obtains the execution result of the SQL corresponding to the data query requirements.

[0043] A third aspect of the present invention provides a natural language intelligent query device based on multi-agent interaction, comprising:

[0044] One or more processors;

[0045] Storage device for storing one or more programs;

[0046] When the one or more programs are executed by the one or more processors, the one or more processors perform the methods described above.

[0047] A fourth aspect of the present invention provides a computer-readable medium having a computer program stored thereon, characterized in that the program, when executed by a processor, implements the methods described above.

[0048] Building more sophisticated and interactive agents based on LLM capabilities can effectively alleviate the aforementioned shortcomings. The multi-agent interaction method proposed in this invention overcomes the limitations of a single LLM. Its main idea is to distribute the construction of prompt words in the NL-to-SQL task across various modules. The code generation component, the core of the intelligent query process, upon receiving an SQL generation request, first retrieves queries similar to the current query from the knowledge base based on historical query records, serving as the context for LLM input. After the LLM completes SQL generation, the executor outputs the SQL execution result to the code correction module. The code correction module primarily checks the generated SQL semantically and corrects it when necessary. This method is highly versatile; the modules cooperate through a plug-in approach, allowing for flexible modification of the underlying implementation of each component as needed. Besides the NL-to-SQL problem primarily addressed in this invention, this method is well-adapted to other generative problems and has broad application value.

[0049] In summary, the present invention has the following characteristics:

[0050] 1. This paper proposes for the first time a multi-agent interaction mechanism to enhance the NL-to-SQL capabilities of LLM, enabling intelligent querying of structured data based on natural language. Furthermore, based on the proposed method, two agents, an executor and a verifier, are constructed to build a complete NL-to-SQL application.

[0051] 2. In the knowledge base component of the agent in the method, a word masking method based on string matching is used to process the historical problem description, and a semantic similarity calculation method is performed based on the masked sequence.

[0052] 3. A dynamic LLM prompt word format based on historical query data is proposed. Attached Figure Description

[0053] Figure 1 This is a schematic diagram of the method modules and process of the present invention.

[0054] Figure 2 The prompt word format used by the code generation module.

[0055] Figure 3 Example of using prompt words for the code correction module

[0056] Figure 4 This is a flowchart of multi-agent interaction. Detailed Implementation

[0057] The technical solution of the present invention will be further described below with reference to the accompanying drawings.

[0058] Example 1

[0059] Reference Figures 1-4 This embodiment relates to a natural language intelligent query method based on multi-agent interaction, which converts the user's natural language description of data query requirements into structured query language and returns the execution results, including the following steps:

[0060] 1) The intelligent agent verifier receives user data query descriptions and database structure information, and constructs prompt words, including the following steps:

[0061] 11) The verifier sends the user data query description and database structure information to the knowledge base module. The knowledge base performs a keyword masking operation on the database-related keywords in the data query description based on the keyword masking algorithm.

[0062] 12) The knowledge base module finds several historical query records with similar descriptions to the masked user data query from the vector database based on semantic similarity, as well as the corresponding structured query language SQL.

[0063] 13) The knowledge base module retrieves the user's SQL generation preferences from the key-value store based on the current username;

[0064] 14) The knowledge base module sends historical query records and user-generated preferences to the verifier. The verifier combines the historical query records, user-generated preferences, user data query descriptions, and database structure information to generate prompt words.

[0065] 15) The verifier sends the prompt to the knowledge base module, which records the prompt as the LLM dialogue context;

[0066] 2) The verifier sends the prompt to the agent executor, who then performs the following steps:

[0067] 21) The executor sends the SQL to the code generation module, which calls the relevant LLM interface and guides the LLM to generate the target SQL based on the generated prompts;

[0068] 22) The executor sends the SQL and database structure information to the executor module, which then performs the following steps:

[0069] 221) The executor module selects the corresponding database based on the database structure information and establishes a connection with it;

[0070] 222) The executor executes the SQL on the data and saves the execution result in the form of a tuple. The tuple contains a boolean value that determines whether the SQL was executed correctly. If the SQL was executed correctly, the other value in the tuple is the data returned after the SQL was executed; otherwise, it contains the SQL execution error message. The executor returns the tuple to the agent executor.

[0071] 3) The executor sends a tuple containing the SQL execution result and the SQL to the verifier, who then performs the following steps:

[0072] 31) The executor sends the tuple and SQL to the code correction module, which then performs the following steps:

[0073] 311) If the boolean value in the tuple is false, the code correction module calls the underlying LLM interface to instruct the LLM to generate a corresponding natural language description for the SQL execution error information in the tuple, and generates SQL modification suggestions based on the description.

[0074] 312) If the boolean value in the tuple is true, the code correction module calls the underlying LLM related interface to guide the LLM to interpret the execution semantics of the SQL and output the corresponding natural language description. The description and the user data query description are used as input to the LLM to guide the LLM to determine whether the semantics of the two are consistent. If they are inconsistent, the corresponding SQL correction suggestion is output. If they are consistent, the natural language description of the SQL execution semantics is output.

[0075] 32) The code correction module sends the SQL correction suggestions generated by the LLM in the above steps to the verifier. If the correction suggestions are empty, the verifier performs the following steps:

[0076] 321) The verifier sends the SQL, the SQL execution result, and its natural language description to the user. If the user is satisfied with the generated result, the intelligent query process terminates. Otherwise, proceed to step 322).

[0077] 322) When the correction suggestion is not empty or the user feedback in step 321) is not empty, execute this step. The verifier calls the knowledge base module interface to obtain the LLM historical dialogue context, and concatenates the correction suggestion or user feedback as new dialogue content after it as the prompt words for the new round of SQL generation. Then jump to step 2) and repeat the above steps until the maximum number of iterations is reached or the query process terminates in step 321).

[0078] In the code generation module described in step (21), the LLM prompts consist of four parts: generation instructions, generation examples, problem descriptions, and database structures. In addition to the fixed-format prompts, the generation instructions also include historical error-related information obtained from interaction with the knowledge base, which helps the LLM avoid generating specific errors. The generation examples are also provided by the knowledge base, and their purpose is to improve the quality of generated SQL by utilizing the context learning capabilities of the LLM. The database structure is recorded in the form of a data description language, and its purpose is to fully include structural information such as primary keys and foreign keys to ensure the generation quality of complex SQL.

[0079] In the knowledge base module described in step (1), a historical query matching algorithm based on word masking is designed. The historical query matching algorithm based on word masking first takes multiple word sequences of different lengths as units, obtains word sequences that match the database table and column names in the problem description according to the string matching algorithm, performs masking operation on them, performs vector embedding operation on them through the language model, and then selects the historical descriptions with the highest matching degree with the historical data in the vector database based on semantic similarity and KNN algorithm as generated examples.

[0080] In the code correction module described in step (3), specific prompt words are designed to guide the LLM to convert the SQL generated in the code generation module into a description in natural language form; and based on the semantic understanding ability of the LLM, it is guided to obtain modification suggestions by comparing the original problem description and combining the SQL execution results of the executor, which are used to guide the next round of SQL generation in the code generation module.

[0081] Four modules are combined to form an intelligent agent, a verifier, and an executor. The intelligent agents interact to implement an intelligent query process. Users input data query requirements described in natural language through a specified interface, specify a particular database, and ultimately directly obtain the execution results of the SQL corresponding to the data query requirements.

[0082] This invention provides a set of general functional components for constructing customizable agents and generating and optimizing SQL based on the interaction and collaboration between agents. To more intuitively illustrate the role of each component in practical applications, consider the following scenario: A user wants to perform statistical analysis on a portion of data from a database, but lacks SQL expertise and can only describe their query requirements in natural language. For this scenario, this method can implement two agents—an executor and a verifier. The executor is responsible for generating and executing the specific SQL, while the verifier, acting as an intermediary between the user and the executor, communicates with the user and refines the executor's generated results based on user feedback. The architecture is as follows: Figure 4 As shown.

[0083] Example 2

[0084] Referring to the accompanying drawings, this embodiment relates to a natural language intelligent query system based on multi-agent interaction, including the following modules:

[0085] (1) Code generation module,

[0086] Its core is LLM (Local Level Management), taking as input the user's natural language query request and database structure (tables, columns, etc.) information, and outputting as SQL statements. Depending on the needs, the generation process may sample the LLM decoder output multiple times to obtain multiple candidate SQL statements, and then filter from them.

[0087] (2) Actuator

[0088] This module is responsible for executing the SQL generated by the code generation component. Besides interacting with the database, it also performs syntax checks on the generated SQL. After obtaining the execution results, it determines the execution logic of the subsequent code correction module based on whether error messages are included.

[0089] (3) Knowledge Base

[0090] It is responsible for storing historical query records, using a vector database as the underlying storage component. Besides data storage, it is also responsible for data processing, primarily including masking operations on related terms within the database structure information.

[0091] (4) Code Correction Module

[0092] Unlike the executor module, which primarily checks the generated SQL from a syntactic perspective, the code correction module corrects the SQL as needed based on semantics or provides guidance for the next generation. Its underlying implementation relies on LLM for semantic-related operations.

[0093] The code generation module accepts user queries and database structure information as input. To ensure the stability of LLM generation, its prompts follow a fixed format, including four parts: generation instructions, generation examples, problem description, and database structure information, as shown in Table 1.

[0094] Table 1 shows the fields and explanations included in the LLM code generation prompts.

[0095] Generate instructions Inform the LLM of a fixed description of the specific generation task. Instruction Generate Example Multiple natural language query-SQL pairs as examples Example Problem Description Natural language description of user query Question Database structure Data Description Language (DDL) representation of database structure Schema

[0096] The generation instruction, located at the beginning of the prompt, guides the SQL generation process in the LLM. It includes a task description and a summary of the subsequent prompts. Optionally, the generation instruction can be supplemented with relevant instructions based on a knowledge base to avoid historical errors; for example, it can guide the large model to avoid generating high-error-rate JOIN statements other than INNER JOIN. User feedback is also included in this field to ensure that the generated SQL aligns with user preferences.

[0097] The generated examples, provided by a knowledge base, contain several user queries described in natural language and their corresponding SQL statements. Their purpose is to help the LLM (Local Language Builder) generate more relevant SQL through few-shot learning. Unlike the natural language question descriptions, the database structure is represented in DDL (Data Definition Language) format. The aim is to preserve database structure information as completely as possible, including primary key information and foreign key relationships between tables. This helps the LLM generate complex SQL statements such as nested subqueries and JOIN operations. If the user is willing to trade longer execution time for better SQL generation quality, they can control the LLM to sample and generate multiple possible results in the decoder stage using top-k methods, and select the SQL statement with the highest consistency based on the execution results. Complete code generation prompts are as follows: Figure 2 As shown.

[0098] The knowledge base is responsible for storing historical query results, including question descriptions and corresponding SQL statements. The question descriptions are first processed based on a language model and then stored in a vector database as fixed-dimensional vectors. To enable finding the corresponding SQL statement from the question description, the knowledge base maintains an additional mapping between question descriptions and SQL statements. For a raw question description Q, before storing it as historical data, the knowledge base performs a word-level masking operation. Specifically, an n-gram matching-based method is used to find the correspondence between words in the question description and database structure information. The word-based question description is matched sequentially with column names and table names in the database structure information, using word sequences of length 1 to k. If a complete match is achieved or the word sequence can be a subsequence, the words in the sequence are replaced with the special word '[MASK]'. This process is illustrated in Algorithm 1.

[0099]

[0100] In addition to storing historical data, the knowledge base is also responsible for selecting a series of generated examples for each new problem description Q. The selection process comprehensively considers the semantic similarity between problem descriptions and between SQL statements. Specifically, for problem description Q, the knowledge base first vectorizes it based on the same language model. Then, using the KNN algorithm, it selects several historical problem descriptions with the highest matching degree from the vector database. For these candidate historical problem descriptions, the knowledge base performs a second round of filtering. First, the knowledge base generates an SQL statement for problem description Q based on another LLM, but without complex prompts or post-generation corrections. This SQL statement is used as an approximation of the final SQL statement for semantic similarity matching with the candidate sample SQL statements. The final selection of generated examples comprehensively considers the semantic similarity between the problem description and the SQL statement. This process is illustrated in Algorithm 2.

[0101]

[0102] Finally, the knowledge base is also responsible for storing error-related information from the historical generation process. This error information can be used as part of the prompt word generation instructions in the code generation component to guide the LLM to try to avoid similar error generation or to avoid some SQL paradigms with a high probability of error.

[0103] The executor is responsible for executing the SQL statements generated by the code generation module. It first checks the SQL at the syntax level. If the syntax check fails, it records the relevant error information, which is then used as input to the code correction module. Based on the SQL's execution performance, the executor determines the specific generation tasks for the code correction module. If the SQL executes successfully, the executor stores the execution result and, in conjunction with the code correction module's result, decides whether to use it as the final result.

[0104] As the final module in the entire generation process, the code correction module primarily checks the SQL generated in the preceding processes at the semantic level and generates correction guidance information when necessary. Like the code generation module, its underlying implementation is an LLM (Local Language Management). If the executor's output indicates a syntax error in the SQL, the code correction module is responsible for converting the specific error message into a corresponding natural language description. In addition, the code correction module is responsible for semantically improving the generated SQL. Specifically, the LLM in the code correction module first takes the generated SQL as input, attempts to paraphrase its code logic in natural language, and determines whether there is a semantic difference between it and the problem description Q. If the LLM determines that there is a difference, it outputs targeted generation suggestions, which are passed to the code generation module as part of the prompts in the next round of generation. Its error message generation process is shown in Algorithm 3.

[0105]

[0106] Examples of suggestion words for generating improvement information for SQL in the code correction module are as follows: Figure 3 As shown

[0107] From the above description of the embodiments, those skilled in the art will clearly understand that the facilities of the present invention can be implemented using software plus necessary general-purpose hardware platforms. Embodiments of the present invention can be implemented using existing processors, or by dedicated processors used for this or other purposes for suitable systems, or by hardwired systems. Embodiments of the present invention also include non-transitory computer-readable storage media, comprising machine-readable media for carrying or having machine-executable instructions or data structures stored thereon; such machine-readable media can be any available medium accessible by a general-purpose or special-purpose computer or other machine with a processor. For example, such machine-readable media can include RAM, ROM, EPROM, EEPROM, CD-ROM or other optical disc storage, disk storage or other magnetic storage devices, or any other medium that can be used to carry or store the required program code in the form of machine-executable instructions or data structures and is accessible by a general-purpose or special-purpose computer or other machine with a processor. When information is transmitted or provided to a machine via a network or other communication connection (hardwired, wireless, or a combination of hardwired and wireless), that connection is also considered a machine-readable medium.

[0108] Example 3

[0109] This embodiment relates to a natural language intelligent query device based on multi-agent interaction, comprising:

[0110] One or more processors;

[0111] Storage device for storing one or more programs;

[0112] When the one or more programs are executed by the one or more processors, the one or more processors implement the method described in Embodiment 1.

[0113] Example 4

[0114] This embodiment provides a computer-readable medium having a computer program stored thereon, characterized in that: when the program is executed by a processor, it implements the method described in Embodiment 1.

[0115] The technical solution of the present invention has been described above with reference to the preferred embodiments shown in the accompanying drawings. However, it will be readily understood by those skilled in the art that the scope of protection of the present invention is obviously not limited to these specific embodiments. Without departing from the principles of the present invention, those skilled in the art can make equivalent changes or substitutions to the relevant technical features, and the technical solutions after such changes or substitutions will all fall within the scope of protection of the present invention.

Claims

1. A natural language intelligent query method based on multi-agent interaction, which converts the user's natural language description of data query requirements into structured query language and returns the execution results, including the following steps: 1) The intelligent agent verifier receives user data query descriptions and database structure information, and constructs prompt words, including the following steps: 11) The verifier sends the user data query description and database structure information to the knowledge base module. The knowledge base performs a keyword masking operation on the database-related keywords in the data query description based on the keyword masking algorithm. 12) The knowledge base module finds several historical query records with similar descriptions to the masked user data query from the vector database based on semantic similarity, as well as the corresponding structured query language SQL. 13) The knowledge base module retrieves the user's SQL generation preferences from the key-value store based on the current username; 14) The knowledge base module sends historical query records and user-generated preferences to the verifier. The verifier combines the historical query records, user-generated preferences, user data query descriptions, and database structure information to generate prompt words. 15) The verifier sends the prompt to the knowledge base module, which records the prompt as the LLM dialogue context; 2) The verifier sends the prompt to the agent executor, which generates and executes SQL based on the prompt, including the following steps: 21) The executor sends the SQL to the code generation module, which calls the relevant LLM interface and guides the LLM to generate the target SQL based on the generated prompts; 22) The executor sends the SQL and database structure information to the executor module, which executes the SQL and obtains the execution result, including the following steps: 221) The executor module selects the corresponding database based on the database structure information and establishes a connection with it; 222) The executor executes the SQL on the data and saves the execution result in the form of a tuple. The tuple contains a boolean value that determines whether the SQL was executed correctly. If the SQL was executed correctly, the other value in the tuple is the data returned after the SQL was executed; otherwise, it contains the SQL execution error message. The executor returns the tuple to the agent executor. 3) The executor sends a tuple containing the SQL execution result and the SQL to the verifier. The verifier corrects the SQL based on the execution result, including the following steps: 31) The executor sends the tuple and SQL to the code correction module, which generates SQL modification suggestions, specifically including: 311) If the boolean value in the tuple is false, the code correction module calls the underlying LLM interface to instruct the LLM to generate a corresponding natural language description for the SQL execution error information in the tuple, and generates SQL modification suggestions based on the description. 312) If the boolean value in the tuple is true, the code correction module calls the underlying LLM related interface to guide the LLM to interpret the execution semantics of the SQL and output the corresponding natural language description. The description and the user data query description are used as input to the LLM to guide the LLM to determine whether the semantics of the two are consistent. If they are inconsistent, the corresponding SQL correction suggestion is output. If they are consistent, the natural language description of the SQL execution semantics is output. 32) The code correction module sends the SQL correction suggestions generated by the LLM in the above steps to the verifier. The verifier decides whether to return the result to the user or perform SQL correction based on the specific content of the correction suggestions, and performs the following steps: 321) If the correction suggestion is empty, the verifier sends the SQL, the SQL execution result, and its natural language description to the user. If the user is satisfied with the generated result, the intelligent query process terminates; otherwise, proceed to step 322). 322) When the correction suggestion is not empty or the user feedback in step 321) is not empty, execute this step. The verifier calls the knowledge base module interface to obtain the LLM historical dialogue context, and concatenates the correction suggestion or user feedback as new dialogue content after it as the prompt words for the new round of SQL generation. Then jump to step 2) and repeat the above steps until the maximum number of iterations is reached or the query process terminates in step 321).

2. The natural language intelligent query method based on multi-agent interaction according to claim 1, characterized in that: In the code generation module described in step (21), the LLM prompts consist of four parts: generation instructions, generation examples, problem descriptions, and database structures. In addition to the fixed-format prompts, the generation instructions also include historical error-related information obtained from interaction with the knowledge base, which helps the LLM avoid generating specific errors. The generation examples are also provided by the knowledge base, and their purpose is to improve the quality of generated SQL by utilizing the context learning capabilities of the LLM. The database structure is recorded in the form of a data description language, and its purpose is to fully include structural information such as primary keys and foreign keys to ensure the generation quality of complex SQL.

3. The natural language intelligent query method based on multi-agent interaction according to claim 1, characterized in that: In the knowledge base module described in step (1), a historical query matching algorithm based on word masking is designed. The historical query matching algorithm based on word masking first takes multiple word sequences of different lengths as units, obtains word sequences that match the database table and column names in the problem description according to the string matching algorithm, performs masking operation on them, performs vector embedding operation on them through the language model, and then selects the historical descriptions with the highest matching degree with the historical data in the vector database based on semantic similarity and KNN algorithm as generated examples.

4. The natural language intelligent query method based on multi-agent interaction according to claim 1, characterized in that: In the code correction module described in step (3), specific prompt words are designed to guide the LLM to convert the SQL generated in the code generation module into a description in natural language form; Furthermore, based on the semantic understanding capabilities of LLM, it guides the code generation module to obtain modification suggestions by comparing the original problem description and combining the SQL execution results of the executor, which are then used to guide the next round of SQL generation.

5. The natural language intelligent query method based on multi-agent interaction according to claim 1, characterized in that: The system consists of four modules: an agent, a verifier, and an executor. The agents interact to implement an intelligent query process. Users can input their data query requirements in natural language through a specified interface and specify a particular database. Ultimately, they can directly obtain the execution results of the SQL corresponding to their data query requirements.

6. A natural language intelligent query device based on multi-agent interaction, characterized in that: include: One or more processors; Storage device for storing one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any one of claims 1-5.

7. A computer-readable medium having a computer program stored thereon, characterized in that: When the program is executed by the processor, it implements the method as described in any one of claims 1-5.