Query methods based on multi-agent collaboration

By using a multi-agent collaborative query method to generate and validate natural language query statements, the problem of poor user experience in traditional database query technology is solved, and efficient and accurate query results are achieved.

CN121301386BActive Publication Date: 2026-03-06GUANGZHOU SMART SOFTWARE CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511851168.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-12-10
Publication Date
2026-03-06
Estimated Expiration
2045-12-10

AI Technical Summary

Technical Problem

Traditional database query technology has a technical barrier for non-professional users. When natural language is converted into database query language, there are grammatical and logical errors, resulting in inaccurate query results and failing to meet complex query needs.

Method used

A multi-agent collaborative query method is adopted. By acquiring the natural language query text input by the user, a structured representation of the query is generated. Combined with the constellation data model of the target database, a pre-set query statement generation agent generates candidate query statements, performs verification, correction and confidence evaluation, and finally selects the query statement with the highest confidence to execute the query.

Benefits of technology

It improves the accuracy and reliability of natural language queries, ensuring that query results meet user needs and providing a convenient and efficient query experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121301386B_ABST
    Figure CN121301386B_ABST
Patent Text Reader

Abstract

This application relates to a query method based on multi-agent collaboration. After obtaining the natural language query text input by the user, a structured representation of the query is generated, and a related database structure representation is obtained by combining it with a constellation data model of the target database. The structured query representation and the database structure representation are input into a pre-set query statement generation agent to generate several first candidate query statements. The first candidate query statements are validated and corrected, and those that do not meet the corrected conditions are iteratively validated and corrected. Through the iterative correction mechanism, second candidate query statements with higher reliability are obtained. Subsequently, the confidence level of each second candidate query statement is evaluated, and the one with the highest confidence level is selected as the target query statement, and the query is executed in the target database. The embodiments of this application improve the accuracy, reliability, and usability of natural language queries, providing users with a more convenient, efficient, and accurate query experience.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of natural language query technology, and in particular to a query method based on multi-agent collaboration. Background Technology

[0002] With the rapid development of information technology, fields such as big data analytics and business intelligence are booming, and users' data query needs are becoming increasingly complex and diverse. Traditional database query techniques mainly rely on structured query languages ​​(such as SQL) to retrieve data. However, this query method presents a significant technical barrier for non-professional users. Non-professional users are often unfamiliar with the syntax rules and structure of the SQL language, making it difficult to accurately write query statements that meet their needs. This leads to significant difficulties in obtaining the required data, severely impacting user experience and data utilization efficiency.

[0003] While existing database query technologies attempt to translate natural language into database query language to lower the query threshold, numerous problems remain in the conversion process. For example, the diversity and ambiguity of natural language expression mean that generated queries may not accurately reflect the user's true intent, and often contain syntactic or logical errors, leading to inaccurate results or even unexecutable queries. These problems severely limit the practicality and reliability of natural language to database query language conversion technologies, failing to meet the increasingly complex query needs of users. Therefore, a technical solution to improve the accuracy and executability of natural language query generation is urgently needed. Summary of the Invention

[0004] Therefore, the purpose of this application is to provide a query method based on multi-agent collaboration to ensure the correctness and reliability of query statements, thereby improving the accuracy of query results.

[0005] The query method based on multi-agent collaboration described in this application includes the following steps:

[0006] Obtain the natural language query text input by the user;

[0007] Generate a structured representation of the natural language query text; based on the structured representation of the query, obtain the associated database structure representation from the constellation data model; wherein, the constellation data model is a structured representation of the target database;

[0008] The query structure representation and the database structure representation are input into a preset query statement generation agent to obtain a candidate query statement set; the candidate query statement set includes several first candidate query statements generated by the preset query statement generation agent.

[0009] For any first candidate query statement in the candidate query statement set, a verification and correction step is performed: the first candidate query statement is verified to obtain a verification result; when the verification result indicates that the first candidate query statement has a problem, the first candidate query statement and the verification result are input to a preset query statement correction agent to obtain a corrected first candidate query statement and a corresponding first confidence level; when the first confidence level does not meet the preset confidence condition and the number of corrections is less than the preset maximum number of corrections, the verification and correction step is continued for the corrected first candidate query statement; until the first confidence level meets the preset confidence condition or the number of corrections is equal to the preset maximum number of corrections, the corrected first candidate query statement is determined as the second candidate query statement; when the verification result indicates that the first candidate query statement has no problem, the first candidate query statement is determined as the second candidate query statement.

[0010] The confidence level of each second candidate query statement is evaluated, and the second candidate query statement with the highest confidence level is determined as the target query statement; based on the target query statement, a query is executed in the target database to obtain the query results of the natural language query text.

[0011] This application's embodiments achieve significant and comprehensive technical improvements throughout the entire process of converting natural language to database query language and executing queries through a unique multi-agent collaborative mechanism. In the initial query stage, after acquiring the user's input natural language query text, a structured representation of the query is generated, and the associated database structure representation is obtained by combining it with the constellation data model of the target database. The structured query representation and the database structure representation are input into a pre-set query statement generation agent to generate several first candidate query statements. In the candidate query statement processing stage, each first candidate query statement is first validated. If a problem is found, a pre-set query statement correction agent specifically corrects the problematic statement and outputs the first confidence level corresponding to the corrected statement. If the first confidence level does not reach the preset confidence condition and the number of corrections does not exceed the preset maximum number of corrections, the corrected query statement is iteratively validated and corrected until the condition is met or the maximum number of corrections is reached. This iterative correction mechanism greatly improves the accuracy of the query statements, ensuring that the final determined second candidate query statements have high reliability. Subsequently, the confidence level of each second candidate query statement is evaluated, and the statement with the highest confidence level is selected as the target query statement. By comprehensively considering the quality of multiple candidate statements, the optimal one is selected for execution, ensuring that the final query results obtained in the target database can meet the user's needs to the greatest extent. This application embodiment improves the accuracy, reliability, and usability of natural language queries, providing users with a more convenient, efficient, and accurate query experience in complex and ever-changing data query scenarios.

[0012] To better understand and implement this application, the following detailed description is provided in conjunction with the accompanying drawings. Attached Figure Description

[0013] Figure 1 This is a flowchart illustrating the multi-agent collaborative query method according to an embodiment of this application.

[0014] Figure 2 This is a schematic diagram illustrating the steps of constructing a structured representation of a query based on natural language query text in an embodiment of this application;

[0015] Figure 3 This is a schematic diagram illustrating the steps of validating the first candidate query statement in an embodiment of this application. Detailed Implementation

[0016] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings. Wherein, when the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements.

[0017] It should be understood that the embodiments described below do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without inventive effort are within the scope of protection of this application.

[0018] The terminology used in this application is for the purpose of describing particular embodiments only and is not intended to be limiting of the application. The singular forms “a,” “the,” and “the” used in this application are also intended to include the plural forms unless the context clearly indicates otherwise. Furthermore, in the description of this application, unless otherwise stated, “a plurality” means two or more. It should also be understood that the term “and / or” as used herein refers to and includes any or all possible combinations of one or more associated listed items; for example, A and / or B can represent: A alone, A and B together, and B alone. The character “ / ” generally indicates that the preceding and following objects are in an “or” relationship.

[0019] It should be understood that although the terms first, second, third, etc., may be used in this application to describe various information, this information should not be limited to these terms, and these terms are only used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence, nor should they be construed as indicating or implying relative importance. Those skilled in the art can understand the specific meaning of the above terms in this application according to the specific circumstances. Depending on the context, the word "if" as used in this application can be interpreted as "when," "when," or "in response to determination."

[0020] This application provides a query method based on multi-agent collaboration to ensure the correctness and reliability of query statements and improve the accuracy of query results.

[0021] Please refer to Figure 1 The query method based on multi-agent collaboration described in this application includes the following steps:

[0022] S101: Obtain the natural language query text input by the user;

[0023] S102: Generate a structured representation of the natural language query text; based on the structured representation of the query, obtain the associated database structure representation from the constellation data model; wherein, the constellation data model is a structured representation of the target database;

[0024] S103: Input the structured representation of the query and the structured representation of the database into a preset query statement generation agent to obtain a candidate query statement set; the candidate query statement set includes several first candidate query statements generated by the preset query statement generation agent.

[0025] S104: For any of the first candidate query statements in the candidate query statement set, perform a verification and correction step: verify the first candidate query statement to obtain a verification result; when the verification result indicates that the first candidate query statement has a problem, input the first candidate query statement and the verification result into a preset query statement correction agent to obtain a corrected first candidate query statement and a corresponding first confidence level; when the first confidence level does not meet the preset confidence condition and the number of corrections is less than the preset maximum number of corrections, continue to perform the verification and correction step for the corrected first candidate query statement; until the first confidence level meets the preset confidence condition or the number of corrections is equal to the preset maximum number of corrections, determine the corrected first candidate query statement as the second candidate query statement; when the verification result indicates that the first candidate query statement has no problem, determine the first candidate query statement as the second candidate query statement.

[0026] S105: Evaluate the confidence level of each second candidate query statement, and determine the second candidate query statement with the highest confidence level as the target query statement; execute a query on the target database based on the target query statement to obtain the query result of the natural language query text.

[0027] This application embodiment achieves a comprehensive improvement in query statement generation accuracy and result reliability during the conversion and execution of natural language to database query language through a multi-agent collaborative mechanism. In the initial query stage, after obtaining the user-input natural language query text, a structured representation of the query is generated, and the associated database structure representation is obtained by combining it with the constellation data model of the target database. The structured query representation and the database structure representation are input into a pre-set query statement generation agent to generate several first candidate query statements. In the candidate query statement processing stage, when a problem is found in the first candidate query statement, a pre-set query statement correction agent corrects the query statement and outputs the corresponding first confidence level. Based on the first confidence level and the number of corrections, the query statement is iteratively verified and corrected. This greatly improves the accuracy of the query statement and ensures that the final determined second candidate query statement has high reliability. Subsequently, the confidence level of each second candidate query statement is evaluated, and the one with the highest confidence level is selected as the target query statement. By comprehensively considering the quality of multiple candidate statements, the optimal one is selected for execution, ensuring that the final query results obtained in the target database can meet the user's needs to the greatest extent. The embodiments of this application improve the accuracy, reliability and practicality of natural language queries, providing users with a more convenient, efficient and accurate query experience in complex and ever-changing data query scenarios.

[0028] The query method based on multi-agent collaboration described in this application uses a computer as the execution subject, and the following will describe each step in detail.

[0029] For step S101, obtain the natural language query text input by the user.

[0030] Natural language query text is text content that users input in the form of natural language used in daily communication to express their data query needs, such as "find products with sales exceeding 1 million in the past month".

[0031] This step receives the user's query text in natural language via a user interface or other input method, providing raw data for subsequent processing. For example, a user might enter "Query information on the top five best-selling products this week" into the search box.

[0032] For step S102, a query structured representation of the natural language query text is generated; based on the query structured representation, an associated database structured representation is obtained from the constellation data model; wherein, the constellation data model is a structured representation of the target database;

[0033] Structured query representation is a representation of natural language query text that has a clear structure and semantic information. It can more clearly present the key elements and logical relationships in the query, making it easier to match with the database structure later.

[0034] The constellation data model is a structured representation of the target database. It describes the database's architecture, table relationships, field information, etc., in a specific and standardized way, much like drawing a detailed map of the database. This helps the system understand the database's structural characteristics and thus accurately obtain relevant data.

[0035] This step involves in-depth analysis of the acquired natural language query text, extracting key information such as word segmentation, entities, and query intent, and transforming it into a structured representation of the query. Then, based on this structured representation, it searches for related database structure information in a pre-built constellation data model (a structured representation of the target database), such as determining the database tables, fields, and relationships between them. For example, for the text querying the top five best-selling products this week, after analysis, a structured representation containing elements such as product, sales volume, and time range is generated, and the corresponding product table, sales record table, and related fields are found in the constellation data model.

[0036] Please refer to Figure 2 In one embodiment, step S102, which involves generating a query structured representation of the natural language query text, includes:

[0037] Step S1021: Perform word segmentation, entity recognition, referential resolution, time standardization or numerical standardization, and query intent recognition operations sequentially on the natural language query text to obtain word segmentation, entity, referential resolution information, time standardization or numerical standardization information, and query intent.

[0038] The natural language query text is segmented into words, breaking the sentence down into meaningful words or phrases. For example, for the query "find popular movies in the last week," the segmentation result might be "find / recent / week / within / the / popular / movies." Entity recognition identifies entities with specific meanings from the natural language text, such as names of people, places, organizations, times, dates, and values. These entities are usually the basic units that carry important information in the text. This entity recognition step helps to clarify the key objects involved in the query, providing more accurate information for subsequent understanding of the query intent.

[0039] Reference resolution addresses the specific objects that pronouns refer to in natural language. In text, pronouns (such as "he," "she," "it," "this," "that," etc.) are often used to refer to previously mentioned nouns. Reference resolution aims to determine which specific entity these pronouns refer to.

[0040] Time standardization or numerical standardization refers to converting various forms of time or numerical values ​​in query text into a unified standard format. For example, converting "August 15, 2023" to "2023-08-15", and "one hundred yuan" to "100".

[0041] Query intent recognition refers to analyzing natural language query text to determine the specific query purpose that a user wants to achieve through that text. For example, for the query "find product information with a price below 50 yuan and an inventory of more than 10", the query intent is to obtain product information that meets the price and inventory conditions.

[0042] Step S1022: Construct the structured representation of the query based on the word segmentation results, the entity, the referential resolution information, the time standardization or numerical standardization information, and the query intent.

[0043] Structured query representation refers to representing key query information in a structured form, including tokenization, entities, referential resolution information, time-normalized or numerical-normalized information, and query intent. This allows subsequent query generation operations to more accurately understand and process the query content. For example, it can be constructed as a data structure with multiple fields, such as a dictionary or object, where each field corresponds to a type of information. In this structured representation, tokenization results can be stored in a list, entities can be stored as key-value pairs, referential resolution information records the correspondence between pronouns and their referents, time-normalized or numerical-normalized information is stored in a standard format, and query intent is stored as a specific identifier or description. Through this structured representation, the subsequent query generation agent can more easily obtain and understand the key query information, thereby generating accurate query statements.

[0044] This embodiment utilizes word segmentation to enable further analysis and processing of text; entity recognition clarifies the key objects involved in the query; pronoun referencing resolution solves the problem of ambiguous pronoun references, avoiding information ambiguity; time standardization or numerical standardization unifies different forms of expression, facilitating subsequent query processing; and query intent recognition accurately grasps the user's query purpose. Integrating this information to construct a structured query representation allows the subsequent intelligent agent to more efficiently and accurately understand the query content and generate query statements that meet user needs. This structured representation not only improves the accuracy and efficiency of query processing but also enhances the system's scalability and maintainability, better adapting to various complex natural language query scenarios.

[0045] In one embodiment, the constellation data model is constructed from the metadata of the target database; the metadata includes at least tables, columns, indexes, and data constraints.

[0046] Metadata is data that describes the structure and attributes of a database, including table structure (such as table name and column name), column attributes (data type and whether null is allowed), index information (index type and index column), and data constraints (primary key constraints, foreign key constraints, uniqueness constraints, etc.), which form the basis for building a structured representation of a database.

[0047] The constellation data model is a structured representation model built on the target database metadata. By integrating metadata information such as tables, columns, indexes, and data constraints, it forms a global structural view of the database, making it easier for the system to quickly locate and query relevant database structural elements.

[0048] Step S102, which involves obtaining the associated database structure representation from the constellation data model based on the query structure representation, includes:

[0049] Step S1023: Traverse the tables and columns in the constellation data model, and filter out the tables, columns, and corresponding indexes and data constraints associated with the entities in the query structured representation; generate the associated database structure representation based on the filtering results.

[0050] First, the metadata information of all tables and columns in the constellation data model is traversed. An entity matching algorithm is used to map the entities in the structured representation of the query (such as "product" and "sales revenue") to the tables and columns in the database. For example, when the query intent involves "product sales ranking," the product table containing the "product" entity, the sales record table containing the "sales revenue" field, and the indexes (such as the product ID index) and data constraints (such as sales revenue must be non-negative) associated with these tables are automatically selected. Then, the selected table structures, column attributes, index types, and constraints are integrated into a related database structure representation, providing a precise structured basis for generating candidate query statements.

[0051] This embodiment achieves precise alignment between query intent and database structure through the structured integration of target database metadata using a constellation data model. Specifically, the constellation data model built based on metadata comprehensively reflects the physical structure and logical constraints of the database, while the database structure representation obtained through entity association filtering eliminates irrelevant and redundant information, ensuring that the candidate query statement generation agent can focus on structural elements directly related to the user's query. This approach not only improves the accuracy of query statement generation and avoids syntax errors or semantic deviations caused by missing structural information, but also optimizes query execution efficiency through the introduction of indexes and constraints. This enables the rapid generation of database query statements that match user intent and are performance-optimized in complex query scenarios, ultimately achieving end-to-end precise conversion from natural language queries to efficient database operations.

[0052] For step S103, the query structured representation and the database structured representation are input into a preset query statement generation agent to obtain a candidate query statement set; the candidate query statement set includes several first candidate query statements generated by the preset query statement generation agent.

[0053] The pre-defined query statement generation agent is an intelligent program module with pre-defined rules and algorithms. It can generate candidate query statements that conform to the database query language specification according to specific logic based on the input query structure representation and database structure representation.

[0054] The candidate query statement set includes several first candidate query statements generated by the pre-defined query statement generation agent. The first candidate query statements are used to represent the user's query intent, and they may contain problems such as syntax errors and semantic inaccuracies.

[0055] This step takes the structured representation of the query and the structured representation of the database as input and passes them to the pre-defined query statement generation agent. Based on its own pre-defined rules and algorithms, and in conjunction with the database's syntax, this agent generates multiple first-line candidate query statements that may match the user's query intent, forming a candidate query statement set. For example, for the previous query requirement, the agent might generate multiple first-line candidate query statements such as "SELECT Product Name FROM Product Table JOIN Sales Record Table ON Product Table.Product ID = Sales Record Table.Product ID WHERE Sales Date BETWEEN 'Start Date of This Week' AND 'End Date of This Week' ORDER BY Sales Volume DESC LIMIT 5".

[0056] In one embodiment, the pre-set query statement generation agent in step S103 includes at least a first generation agent based on a pre-trained language model, a second generation agent based on template retrieval and filling, and a third generation agent based on syntax-driven generation; the candidate query statement set includes first candidate query statements generated by the first generation agent, the second generation agent, and the third generation agent, respectively.

[0057] Among them, the pre-trained language model is a language model that has been pre-trained through unsupervised learning on large-scale text data. It can capture the grammatical, semantic and contextual information of language and has the ability to understand and generate natural language.

[0058] The first generative agent based on a pre-trained language model is an intelligent program module built around a pre-trained language model. It utilizes the pre-trained language model's ability to understand and generate natural language and database query language to generate candidate query statements that conform to the database query language specification based on the input query structure representation and database structure representation.

[0059] Template retrieval and population refers to the pre-definition of a series of query statement templates suitable for different query scenarios. These templates contain fixed syntax structures and variable parameter parts. When generating query statements, based on the input query structure representation and database structure representation, a suitable template is retrieved from the template library, and then the specific query objects, conditions, and other parameters are filled into the corresponding positions of the template to generate candidate query statements.

[0060] The second generative agent based on template retrieval and filling is an intelligent program module constructed according to the template retrieval and filling method. It manages and uses a preset template library and combines it with input information to complete the task of generating query statements.

[0061] Syntax-driven refers to constructing query statements step by step, guided by the syntax rules of database query languages ​​and following the hierarchical structure and generation rules of the syntax.

[0062] The syntax-driven third generative agent is an intelligent program module built in a syntax-driven manner. Based on the syntax rules of the database query language, and combined with the input query structure representation and database structure representation, it gradually generates candidate query statements from the syntax level.

[0063] This embodiment employs three different types of generative agents—based on a pre-trained language model, template retrieval and completion, and syntax-driven—to generate candidate query statements, greatly enriching the generation methods and diversity of candidate query statements. The first generative agent, based on a pre-trained language model, leverages its powerful language understanding and generation capabilities to produce relatively natural and flexible query statements. The second generative agent, based on template retrieval and completion, can quickly generate statements conforming to common query patterns, improving generation efficiency. The third generative agent, based on syntax-driven generation, ensures the grammatical accuracy of the generated statements. These three agents complement each other, and the generated candidate query statement set includes various styles of query statements, providing abundant resources for subsequent verification, correction, and selection of the optimal target query statement.

[0064] In one embodiment, before step S103, which involves inputting the query structure representation and the database structure representation into a preset query statement generation agent to obtain a set of candidate query statements, the method further includes the following step:

[0065] Step S1030: Based on the query structured representation and the database structured representation, obtain relevant query information from a preset information retrieval database; the query information includes relevant historical query statements and target query statements corresponding to relevant historical natural language query texts;

[0066] Step S103, which involves inputting the structured representation of the query and the structured representation of the database into a preset query statement generation agent to obtain a set of candidate query statements, further includes:

[0067] Step S1031: Input the query structure representation, the database structure representation, and the relevant query information into the preset query statement generation agent to obtain a set of candidate query statements; the relevant query information is used to assist in the generation of the first candidate query statement.

[0068] The preset information retrieval database is a structured knowledge base that stores historical query data. It includes users' historical natural language query text, corresponding target query statements, execution results, and contextual information, providing semantic reference and pattern guidance for new queries.

[0069] Related query information is historical data that is similar to the current query intent and matched from a preset information retrieval database. This includes historical natural language query text and its corresponding target query statement, which is used to help generate candidate query statements that are more in line with user habits and semantic consistency.

[0070] This embodiment, based on the entity and intent features in the structured representation of the query, performs semantic similarity matching in a pre-set information retrieval database to retrieve historical query information highly relevant to the current query intent. For example, when a user queries "sales revenue in East China in the last three months," it may retrieve the target query statement "sales revenue in North China in the last three months" and its corresponding natural language text from the past. This historical information serves as a contextual reference, providing semantic patterns and structural templates for subsequent intelligent agents. In addition to the original input, the pre-set query statement generation agent receives relevant query information as auxiliary input. Three types of generating agents (pre-trained model, template retrieval, and syntax-driven) analyze the structural features and semantic patterns of historical query information to generate candidate query statements similar to the structure of historical successful cases. For example, the first generating agent based on the pre-trained model can refer to the field combination methods of historical statements to generate statements that better match the user's historical expression habits; the template retrieval agent can prioritize matching frequently used templates in the past for filling; and the syntax-driven agent can learn from the grammatical structure of historical statements to optimize the generation logic of the current statement. By introducing historical query information retrieval and a multimodal input mechanism, the accuracy of candidate query statement generation and the matching degree of user intent are significantly improved.

[0071] In one embodiment, after step S101, which involves obtaining the natural language query text input by the user, the method further includes the following step:

[0072] Obtain historical dialogue information; determine whether the current dialogue is a multi-turn dialogue scenario based on the historical dialogue information; if it is determined to be a multi-turn dialogue scenario, obtain historical dialogue records; the historical dialogue records include historical natural language query text and corresponding target query statements and query results.

[0073] Among them, historical dialogue information is the context data of past dialogues stored during user interaction. It includes the user's historical query text, system response and query results, etc., and is a key basis for understanding the intent of multi-turn dialogues.

[0074] Multi-turn dialogue scenarios refer to dialogue modes in which users interact with the system multiple times in succession. There are logical connections between each turn of dialogue (such as condition supplementation and intent refinement), and it is necessary to understand the dialogue context of the current query intent in combination with the context.

[0075] Historical dialogue records are a complete collection of information about each specific round of dialogue in a multi-turn dialogue scenario. They include historical natural language query text, corresponding generated target query statements, and query results returned after the query is executed, forming a semantically coherent dialogue history chain.

[0076] This step automatically retrieves the user's historical dialogue information after receiving the current natural language query text. By analyzing the contextual semantic relationships in the historical dialogues, such as the logical continuity between the previous query "Query Q1 2024 sales" and the current query "Please supplement data for East China," it determines whether the current scenario is a multi-turn dialogue. For example, when it detects that the user repeatedly asks "Can you further categorize by product type?", it is identified as a multi-turn dialogue scenario. After confirming the multi-turn dialogue scenario, the complete historical dialogue record is extracted, including the field usage habits of historical queries (such as frequently querying "sales" and "order volume"), condition setting patterns (such as time range "last three months"), and result feedback characteristics. These records serve as contextual input to help the generated agent understand the user's long-term query preferences and the direction of current intent.

[0077] Step S103, which involves inputting the structured representation of the query and the structured representation of the database into a preset query statement generation agent to obtain a set of candidate query statements, includes:

[0078] Step S1032: Input the query structure representation, the database structure representation, and the historical dialogue records into the preset query statement generation agent to obtain a set of candidate query statements; the historical dialogue records are used to assist in the generation of the first candidate query statement.

[0079] This step involves a pre-defined query generation agent that simultaneously receives three modal inputs: a structured representation of the query, a structured representation of the database, and historical dialogue records. The agent analyzes historical dialogue records to aid in understanding the structured representation of the current natural language query text, thereby generating an accurate first-choice query statement that fits the context of the dialogue.

[0080] This embodiment achieves an intelligent upgrade from "single-turn static query" to "multi-turn dynamic understanding" through deep integration of multi-turn dialogue scenario recognition and historical dialogue records. Historical dialogue information provides a "memory" of users' long-term query behavior, enabling the generative agent to generate candidate statements that better meet actual needs based on users' historical preferences and contextual logic, avoiding semantic ambiguity or logical breaks caused by a lack of context. Simultaneously, successful query patterns in historical dialogue records provide "experience references" for the generation process, significantly improving the initial generation quality of candidate statements and reducing the number of iterations for subsequent verification and correction. This historical dialogue-driven generation mechanism not only enhances the coherence and intelligence level in multi-turn dialogues but also improves the accuracy of query intent matching through contextual understanding optimization. Ultimately, it achieves efficient and accurate conversion from natural language query to database operation throughout the entire process, significantly improving user experience satisfaction and system reliability in complex multi-turn query scenarios.

[0081] For step S104, for any of the first candidate query statements in the candidate query statement set, a verification and correction step is performed: the first candidate query statement is verified to obtain a verification result; when the verification result indicates that the first candidate query statement has a problem, the first candidate query statement and the verification result are input to a preset query statement correction agent to obtain a corrected first candidate query statement and a corresponding first confidence level; when the first confidence level does not meet the preset confidence condition and the number of corrections is less than the preset maximum number of corrections, the verification and correction step is continued for the corrected first candidate query statement; until the first confidence level meets the preset confidence condition or the number of corrections is equal to the preset maximum number of corrections, the corrected first candidate query statement is determined as the second candidate query statement; when the verification result indicates that the first candidate query statement has no problem, the first candidate query statement is determined as the second candidate query statement.

[0082] The pre-set query statement correction agent is used to modify and improve the first candidate query statement based on the input first candidate query statement with problems and the verification result, and output the corrected first candidate query statement and the corresponding first confidence level.

[0083] The first confidence level is a quantitative evaluation metric used by the pre-defined query correction agent to assess the accuracy and reliability of the corrected first candidate query statement. It measures the reliability of the second candidate query statement itself and can be determined based on factors such as the syntactic structure and semantic rationality of the second candidate query statement. The first confidence level typically ranges from 0 to 1, with higher values ​​indicating greater accuracy and reliability.

[0084] The pre-set confidence condition is a pre-defined threshold for the first confidence level. When the first confidence level of the corrected first candidate query statement reaches or exceeds this threshold, the statement is considered to meet the accuracy requirements.

[0085] The preset maximum number of corrections is a pre-set limit on the maximum number of times the first candidate query statement can be corrected to avoid infinite corrections.

[0086] The second candidate query statement is a relatively accurate and reliable query statement determined after the verification and correction steps. It includes the original first candidate query statement that has no verification problems and the first candidate query statement that meets the conditions after correction.

[0087] This step validates each first candidate query statement, checking whether it conforms to the syntax rules of the database query language and accurately expresses the user's query intent, obtaining a validation result. When the validation result indicates that the first candidate query statement has a problem, the statement and the validation result are input into a preset query statement correction agent. The correction agent modifies and improves the statement according to the input information using preset correction rules, outputting the corrected first candidate query statement and its corresponding first confidence score. If the first confidence score does not meet the preset confidence condition and the number of corrections is less than the preset maximum number of corrections, the validation and correction steps continue to be executed for the corrected first candidate query statement. This process is repeated until the first confidence score meets the preset confidence condition or the number of corrections reaches the preset maximum number of corrections. At this point, the final corrected first candidate query statement is determined as the second candidate query statement. When the validation result indicates that the first candidate query statement has no problems, it is directly determined as the second candidate query statement.

[0088] Please refer to Figure 3 In one embodiment, step S104, which involves validating the first candidate query statement to obtain a validation result, includes:

[0089] Step S1041: Convert the first candidate query statement into an abstract syntax tree.

[0090] Abstract Syntax Tree (AST) is a data representation that parses query statements into a tree structure according to syntactic rules. Nodes correspond to syntactic units of the statement (such as keywords, fields, and operators). It is the core tool for syntax analysis and facilitates structured validation.

[0091] This step parses the first candidate query statement into an abstract syntax tree, clarifying the syntax hierarchy and relationships between the syntax units through a tree structure. For example, "SELECT product_name FROM products WHERE price>100" will be parsed into a tree structure containing SELECT, FROM, and WHERE nodes, with each node carrying the corresponding field name, table name, and condition expression.

[0092] Step S1042: Perform syntax correctness verification, keyword integrity verification, and field reference validity verification based on the abstract syntax tree to obtain static verification results.

[0093] This step performs static validation, which is a validation method that analyzes the syntax tree structure without actually executing the query statement. It primarily verifies syntax rule compliance, keyword usage correctness, and field reference validity. Specifically, it performs three types of static validation based on the abstract syntax tree: 1. Syntax correctness validation: checking whether the statement conforms to SQL syntax rules, such as whether the keyword order is correct and whether parentheses match. 2. Keyword completeness validation: verifying whether necessary keywords such as SELECT and FROM are used to avoid omissions that could invalidate the statement. 3. Field reference validity validation: confirming that the fields referenced in the statement exist and are accessible in the target database; for example, checking whether "product_name" is a valid field in the "products" table.

[0094] This embodiment parses the query statement into an AST and performs static validation, achieving structured validation of syntax, keywords, and field references. This provides accurate basic data for subsequent intelligent correction and confidence evaluation, effectively improving the accuracy and reliability of query statement generation.

[0095] Step S1043: Decompose the first candidate query statement into substatements to obtain a set of substatements; the set of substatements includes several substatements; the substatements include, but are not limited to, query column selection substatements, table association substatements, conditional filtering substatements, grouping statistics substatements, and sorting restriction substatements; perform small-scale sample data running verification and / or predicate evaluation verification on each substatement in the set of substatements to obtain dynamic verification results.

[0096] The sub-statement set is a collection of functional modular units that break down the complete query statement, such as query column selection (SELECT part), table join (JOIN part), condition filtering (WHERE part), etc. Each sub-statement corresponds to an independent functional module of the query logic.

[0097] This method validates partial query logic based on actual or simulated data, verifying the semantic correctness and execution feasibility of the statements through the execution results. It primarily includes small-scale sample data validation and predicate evaluation validation. The former uses a small amount of real or simulated data to execute sub-statements, verifying their execution effect in specific data scenarios, such as checking whether conditional filtering sub-statements can correctly filter data. The latter performs mathematical or logical calculations to validate conditional expressions (such as logical expressions in the WHERE clause), ensuring the correctness of the expressions in terms of syntax and semantics.

[0098] Specifically, after decomposing the query statement into a set of sub-statements, each sub-statement is dynamically validated: 1. Validation using small-scale sample data, for example, executing the conditional filtering sub-statement "price>100" with sample data containing different price values ​​to verify whether it can correctly filter records with prices greater than 100. 2. Predicate evaluation validation, performing logical evaluation on the conditional expression, for example, verifying whether "price>100 AND stock>50" returns the expected result under a given data scenario.

[0099] Step S1044: Obtain the verification result based on the static verification result and the dynamic verification result.

[0100] The results of static and dynamic validations are combined to form the final validation result. If any validation step finds a problem, such as a syntax error or execution exception, the candidate statement is marked as problematic; if all validations pass, the statement is confirmed to be problem-free.

[0101] This embodiment achieves comprehensive quality control of candidate query statements through a dual static and dynamic verification mechanism. Abstract syntax tree transformation parses the statements in a structured manner, providing a precise tree-like analysis foundation for syntax verification. Static verification filters basic errors from the perspectives of syntax rules and field validity, ensuring the "formal legality" of the statements. Dynamic verification verifies the "functional correctness" and "execution feasibility" of the statements through sub-statement decomposition and small-scale data execution. The combination of these two mechanisms forms a closed-loop verification system, avoiding semantic execution problems that pure static verification cannot detect, while ensuring verification efficiency through the lightweight design of dynamic verification. This verification mechanism significantly improves the accuracy and reliability of candidate query statements, ensuring that the final generated target query statement conforms to the user's query intent in both syntax and semantics, and can execute efficiently to return correct results, fundamentally solving the statement quality problem in the process from natural language queries to database operations.

[0102] In one embodiment, the verification result includes the error type, error location, and error level;

[0103] When the preset query statement correction agent determines that the error type is a syntax error, it corrects the first candidate query statement based on preset query statement syntax rules; when it determines that the error type is a field error, it replaces the erroneous field of the first candidate query statement with the corresponding correct field based on the database structure representation; the field includes table name or column name; when it determines that the error type is a logical error, it corrects the first candidate query statement according to the query intent.

[0104] Among them, error type refers to the category of statement problem identified in the validation result, including syntax error (such as misspelled keywords, grammatical structure violation), field error (such as incorrect or non-existent table / column name references), and logical error (such as conditional filtering logic not matching the user's query intent).

[0105] Error location is the specific syntactic unit in the query statement where the problem occurs. It can be precisely located by the node path or character offset in the abstract syntax tree, such as "the third conditional expression in the WHERE clause".

[0106] Error levels are classifications of validation results based on the severity of the problem, such as high priority (causing statements to fail to execute), medium priority (affecting query efficiency), and low priority (not affecting core functionality), used to correct priority sorting.

[0107] This embodiment achieves precision and intelligence in the query statement correction process through the synergy of structured validation results and classification correction strategies. Three-dimensional annotation of error type, location, and level provides clear guidance for correction, avoiding secondary errors caused by blind correction. Syntax correction relies on a rule base to ensure basic syntactic correctness, field correction ensures reference validity through database structure mapping, and logical correction ensures semantic accuracy through query intent alignment. This multi-dimensional, hierarchical correction mechanism improves the efficiency and accuracy of candidate query statement correction, ensuring that the final generated target query statement highly matches the user's query intent in terms of syntax, field references, and logical expression, and can efficiently execute and return correct results. Compared to traditional single correction methods, this embodiment, through refined classification of error types and targeted design of correction strategies, forms a closed-loop optimization link from problem discovery to precise correction, fundamentally solving the problem of query failure or result deviation caused by statement errors during natural language querying and database operations, significantly improving system reliability and user experience.

[0108] For step S105, a confidence evaluation is performed on each of the second candidate query statements, and the second candidate query statement with the highest confidence is determined as the target query statement; based on the target query statement, a query is executed in the target database to obtain the query result of the natural language query text.

[0109] Each second candidate query obtained after the verification and correction steps is evaluated for confidence, taking into account factors such as syntactic correctness, semantic accuracy, and match with the user's query intent, to arrive at a corresponding confidence score. Then, the second candidate query with the highest confidence score is selected as the target query. Finally, this target query is used to execute a query operation in the target database, retrieving data that matches the user's natural language query text requirements, and returning this data as the query result to the user.

[0110] In one embodiment, step S103, which involves inputting the query structure representation and the database structure representation into a preset query statement generation agent to obtain a set of candidate query statements, further includes:

[0111] Step S1033: Obtain the second confidence level corresponding to each first candidate query statement output by the preset query statement generating agent.

[0112] The second confidence level is an indicator used to measure the reliability of each first candidate query statement output by the pre-set query statement generating agent. It reflects the degree of trust that the generated first candidate query statements meet the expected query intent.

[0113] After inputting the structured representation of the query and the structured representation of the database into a pre-defined query statement generation agent and obtaining a set of candidate query statements, the second confidence level corresponding to each first candidate query statement output by the pre-defined query statement generation agent is further obtained. The purpose of this step is to conduct a preliminary assessment of the reliability of each first candidate query statement from the perspective of agent generation, providing data support for a more comprehensive evaluation later.

[0114] Step S105, which involves evaluating the confidence level of each second candidate query statement and determining the second candidate query statement with the highest confidence level as the target query statement, includes:

[0115] Step S1051: Obtain the first confidence level, the second confidence level, and the number of corrections for each second candidate query statement; perform a weighted calculation on the first confidence level, the second confidence level, and the number of corrections to obtain the comprehensive confidence level of the second candidate query statement; and determine the second candidate query statement with the highest comprehensive confidence level as the target query statement.

[0116] The overall confidence level is a comprehensive indicator calculated by weighting the first confidence level, the second confidence level, and the number of corrections. It is used to comprehensively measure the reliability and applicability of the second candidate query statement.

[0117] For each second candidate query statement, its corresponding first confidence level, second confidence level, and number of corrections are obtained. These indicators are then weighted and calculated. The weighting calculation involves adding the first confidence level, second confidence level, and number of corrections according to pre-defined weights to obtain the overall confidence level of the second candidate query statement. Finally, the second candidate query statement with the highest overall confidence level is selected as the target query statement. This step comprehensively considers the reliability of the second candidate query statement itself (first confidence level), the reliability of the agent generation (second confidence level), and the number of corrections (number of corrections), selecting the target query statement that best meets the query requirements through a comprehensive evaluation.

[0118] This embodiment, after obtaining the set of candidate query statements, not only acquires the second confidence level corresponding to the first candidate query statement output by the pre-set query statement generation agent, but also, when determining the target query statement, comprehensively considers the first confidence level, second confidence level, and number of corrections for each second candidate query statement and performs a weighted calculation to obtain a comprehensive confidence level, thereby determining the target query statement. This approach avoids the limitations of determining the target query statement based on only a single indicator, and can comprehensively evaluate the reliability and applicability of candidate query statements from multiple dimensions. The calculation method of the comprehensive confidence level makes the evaluation of second candidate query statements more scientific and reasonable, and can more accurately select the target query statement that best matches the query intent, thereby improving the accuracy and reliability of query statement generation, and thus improving the performance and efficiency of the entire query system, providing users with more accurate query results.

[0119] In one embodiment, before step S105, which involves executing a query on the target database based on the target query statement to obtain the query result of the natural language query text, the method further includes:

[0120] Step S1052: When it is determined that the target query statement involves sensitive data operations, permission approval information is generated based on the natural language query text and the target query statement, and a manual approval process is initiated based on the permission approval information.

[0121] Sensitive data operations refer to operations involving the querying, modification, or deletion of protected data in the database, such as user privacy information, trade secrets, and financial data. These operations require authorization approval to ensure data security and compliance.

[0122] Permission approval information is approval request data generated based on natural language query text and target query statement. It can include key information such as operation type (query / modify), data fields involved, user identity, and operation purpose, and is used for manual approval decision-making.

[0123] Step S1053: When the approval is approved, a query is executed in the target database based on the target query statement to obtain the query results of the natural language query text.

[0124] If the approval process returns an "approved" result, the execution restriction on the target query statement is lifted, it is sent to the target database for execution, and the query results are returned to the user interface. Simultaneously, an approval log (including approver, time, and operation details) is recorded for subsequent auditing and traceability.

[0125] Step S1054: If the approval fails, the target query statement is refused to be executed.

[0126] If the approval is rejected, a clear rejection message is returned to the user, such as "You do not have permission to query this data. Please contact the administrator to apply for permission," and the query process is terminated. A rejection log is generated simultaneously, recording the reason for rejection (such as "insufficient permissions" or "data protection policy restrictions") to ensure traceability of the operation.

[0127] This embodiment establishes a "double insurance" mechanism for data security by integrating sensitive data operation identification with manual approval processes. By automatically detecting sensitive operations and triggering approvals, it avoids the risk of data leakage due to access control vulnerabilities, significantly improving enterprises' ability to balance data compliance and business efficiency.

[0128] The above embodiments are merely illustrative of several implementation methods of this application, and their descriptions are relatively specific and detailed, but they should not be construed as limiting the scope of the invention patent. It should be noted that those skilled in the art can make several modifications and improvements without departing from the concept of this application, and this application also intends to include these modifications and variations.

Claims

1. A query method based on multi-agent cooperation, characterized in that, The method comprises the following steps: obtaining a natural language query text input by a user; generating a query structured representation of the natural language query text; based on the query structured representation, obtaining an associated database structure representation from a constellation data model; wherein the constellation data model is a structured representation of a target database; inputting the query structured representation and the database structure representation into a preset query statement generation agent to obtain a candidate query statement set; the candidate query statement set comprises a plurality of first candidate query statements generated by the preset query statement generation agent; for any first candidate query statement in the candidate query statement set, performing a verification and correction step: verifying the first candidate query statement to obtain a verification result; when the verification result indicates that the first candidate query statement has a problem, inputting the first candidate query statement and the verification result into a preset query statement correction agent to obtain a corrected first candidate query statement and a corresponding first confidence; when the first confidence does not satisfy a preset confidence condition and the number of corrections is less than a preset maximum number of corrections, continuing to perform the verification and correction step for the corrected first candidate query statement; until the first confidence satisfies the preset confidence condition or the number of corrections is equal to the preset maximum number of corrections, determining the corrected first candidate query statement as a second candidate query statement; when the verification result indicates that the first candidate query statement has no problem, determining the first candidate query statement as a second candidate query statement; evaluating the confidence of each second candidate query statement, and determining a second candidate query statement with the highest confidence as a target query statement; based on the target query statement, performing a query on the target database to obtain a query result of the natural language query text; the step of generating the query structured representation of the natural language query text comprises: performing the following operations on the natural language query text in sequence: word segmentation, entity recognition, coreference resolution, time standardization or numerical standardization, and query intent recognition, to obtain word segmentation, entity, coreference resolution information, time standardization or numerical standardization information, and query intent; constructing the query structured representation according to the word segmentation, the entity, the coreference resolution information, the time standardization or numerical standardization information, and the query intent; the constellation data model is constructed from metadata of the target database; the metadata at least includes tables, columns, indexes, and data constraints; the step of obtaining the associated database structure representation from the constellation data model based on the query structured representation comprises: traversing the tables and columns in the constellation data model, and screening out tables, columns, and corresponding indexes and data constraints associated with entities in the query structured representation; generating the associated database structure representation according to the screening result.

2. The query method based on multi-agent cooperation according to claim 1, wherein the step of inputting the query structured representation and the database structure representation into a preset query statement generation agent to obtain a candidate query statement set further comprises: ​ obtaining a second confidence degree corresponding to each first candidate query statement generated by the preset query statement generation agent; the step of evaluating the confidence degree of each second candidate query statement and determining the second candidate query statement with the highest confidence degree as the target query statement comprises: obtaining a first confidence degree, a second confidence degree and a correction frequency corresponding to each second candidate query statement; performing weighted calculation on the first confidence degree, the second confidence degree and the correction frequency to obtain a comprehensive confidence degree of the second candidate query statement; and determining the second candidate query statement with the highest comprehensive confidence degree as the target query statement.

3. The query method based on multi-agent collaboration according to claim 1, characterized in that: the step of verifying the first candidate query statement to obtain a verification result comprises: converting the first candidate query statement into an abstract syntax tree; performing syntax correctness verification, keyword integrity verification and field reference validity verification based on the abstract syntax tree to obtain a static verification result; performing sub-statement decomposition on the first candidate query statement to obtain a sub-statement set; the sub-statement set comprises a plurality of sub-statements; the sub-statements comprise query column selection sub-statements, table association sub-statements, condition filtering sub-statements, grouping and statistics sub-statements and sorting and limiting sub-statements; performing small-scale sample data running verification and / or predicate evaluation verification on each sub-statement of the sub-statement set to obtain a dynamic verification result; obtaining the verification result according to the static verification result and the dynamic verification result.

4. The query method based on multi-agent collaboration according to claim 3, characterized in that: the verification result comprises an error type, an error location and an error level; when judging that the error type is a syntax error, the preset query statement correction agent performs syntax correction on the first candidate query statement based on a preset query statement syntax rule; when judging that the error type is a field error, replacing the error field of the first candidate query statement with a corresponding correct field based on the database structure representation; the field comprises a table name or a column name; when judging that the error type is a logical error, correcting the first candidate query statement according to the query intention.

5. The query method based on multi-agent collaboration according to claim 1, characterized in that: the preset query statement generation agent comprises at least a first generation agent based on a pre-trained language model, a second generation agent based on template retrieval and filling, and a third generation agent based on syntax driving; the candidate query statement set comprises first candidate query statements generated by the first generation agent, the second generation agent and the third generation agent respectively.

6. The query method based on multi-agent collaboration according to any one of claims 1 to 5, characterized in that: before the step of inputting the query structured representation and the database structure representation into the preset query statement generation agent to obtain a candidate query statement set, the method further comprises the step of: obtaining relevant query information from a preset information retrieval library based on the query structured representation and the database structure representation; the query information includes relevant historical query statements, target query statements corresponding to relevant historical natural language query texts; the step of inputting the query structured representation and the database structure representation into a preset query statement generation agent to obtain a candidate query statement set further comprises: inputting the query structured representation, the database structure representation, and the relevant query information into a preset query statement generation agent to obtain a candidate query statement set; the relevant query information is used to assist the generation of the first candidate query statement.

7. The query method based on multi-agent collaboration according to any one of claims 1 to 5, characterized in that, after the step of obtaining the natural language query text input by the user, the method further comprises the steps of: obtaining historical dialogue information; and determining whether the current dialogue is a multi-round dialogue scenario based on the historical dialogue information; if it is determined that the current dialogue is a multi-round dialogue scenario, obtaining historical dialogue records; the historical dialogue records include historical natural language query texts and corresponding target query statements and query results; the step of inputting the query structured representation and the database structure representation into a preset query statement generation agent to obtain a candidate query statement set comprises: inputting the query structured representation, the database structure representation, and the historical dialogue records into a preset query statement generation agent to obtain a candidate query statement set; the historical dialogue records are used to assist the generation of the first candidate query statement.

8. The query method based on multi-agent collaboration according to claim 1, characterized in that, before the step of executing a query based on the target query statement in the target database to obtain the query result of the natural language query text, the method further comprises the steps of: when it is determined that the target query statement involves sensitive data operation, generating permission approval information based on the natural language query text and the target query statement, and initiating an artificial approval process based on the permission approval information; when the approval is passed, executing a query based on the target query statement in the target database to obtain the query result of the natural language query text; when the approval is not passed, rejecting to execute the target query statement.

Citation Information

Patent Citations

  • Natural language intelligent query method and device based on multi-agent interaction

    CN118012900A

  • Query method based on query auxiliary information

    CN120045582A