Cross-source structured query generation method and device based on multi-agent cooperation, and medium
By leveraging multi-agent collaboration and graph neural networks, cross-source schema relationships are dynamically retrieved. Combined with historical SQL samples, this solves the complex query problem across heterogeneous data sources, achieving highly accurate and user-friendly cross-source structured query generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- STATE GRID HEBEI ELECTRIC POWER CO LTD
- Filing Date
- 2025-12-22
- Publication Date
- 2026-04-17
AI Technical Summary
Existing technologies suffer from low accuracy and poor adaptability when handling queries involving multiple table joins, complex schema structures, or multiple heterogeneous data sources, and non-technical personnel find it difficult to interact with the database using natural language.
A multi-agent collaborative approach is adopted, which uses graph neural networks to dynamically retrieve cross-source schema relationships and combines hybrid RAG to retrieve relevant historical SQL samples to generate structured queries, including query analysis, dynamic retrieval, planning and generating agents, and performing syntax and semantic verification.
It improves query accuracy and cross-source adaptability, reduces generation time, enhances user-friendliness, and can handle cross-source queries with multiple table joins and complex conditions.
Smart Images

Figure CN121880358A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of artificial intelligence and database technology, and in particular relates to a method, device and medium for generating cross-source structured queries based on multi-agent collaboration. Background Technology
[0002] With the surge in data volume and the widespread adoption of data-driven decision-making, the need for users to interact with databases through natural language is becoming increasingly urgent. Traditional database queries require users to be proficient in SQL syntax and database schema details, posing a significant barrier for non-technical users. To address this issue, researchers have developed various Text-to-SQL methods, including rule-based mapping, statistical models, and machine learning-based semantic parsing techniques. However, existing methods generally suffer from low accuracy and poor adaptability when handling queries involving multiple table joins, complex schema structures, or across multiple heterogeneous data sources.
[0003] In recent years, retrieval-enhanced generative techniques have significantly improved the ability of large language models (LLMs) to process external knowledge. Retrieval-enhanced generative algorithms (RAGs) enable LLMs to utilize knowledge beyond the scope of their training data by retrieving relevant external information before generating a response. For example, RAG-based frameworks have been successfully applied to build domain-specific knowledge assistants. However, traditional RAG systems are primarily geared towards text generation tasks and are rarely directly applied to structured query generation. Furthermore, existing RAG systems lack the ability to dynamically select the optimal data source and generation strategy when handling complex database queries.
[0004] Agent technology has demonstrated strong potential in decomposing complex tasks. For example, the MAC-SQL framework employs multi-agent collaboration for text-to-SQL conversion, comprising three core agents: a selector, responsible for simplifying the database schema and preserving the table structure relevant to the user query; a decomposer, which breaks down complex user queries into simpler subproblems and solves them step by step; and a refiner, which verifies and corrects the generated SQL queries to ensure accuracy. This framework performs exceptionally well when handling complex database structures and user queries, achieving an execution accuracy of 59.59% on the BIRD test set. However, MAC-SQL is primarily designed for single or specific database environments and fails to fully leverage RAG's dynamic retrieval capabilities to obtain cross-source contextual information. Summary of the Invention
[0005] To address the shortcomings of existing technologies, embodiments of the present invention provide a method, device, and medium for generating cross-source structured queries based on multi-agent collaboration.
[0006] In a first aspect, embodiments of the present invention provide a method for generating cross-source structured queries based on multi-agent collaboration, the method comprising the following steps: Extract {entity, operation, condition} triples from the original natural language query text; A graph neural network is constructed based on the data source. Entities in the {entity, operation, condition} triples are mapped to nodes in the graph neural network, including: calculating the matching score between candidate nodes and entities in the graph neural network, and using the candidate node with the highest matching score as the anchor point; obtaining candidate paths connecting the anchor points corresponding to each entity, scoring the candidate paths based on path connection strength, path complexity, and cross-data source cost, and using the candidate path with the highest score as the optimal path; generating physical connection schemes based on the optimal path, and searching for relevant historical SQL samples. Based on the {entity, operation, condition} triple, physical join scheme, and relevant historical SQL samples, a structured query skeleton is set; in response to the structured query skeleton, physical join scheme, and relevant historical SQL samples, an SQL query is generated; and the SQL query is subjected to syntax validation and semantic validation.
[0007] Secondly, embodiments of the present invention provide a cross-source structured query generation system based on multi-agent cooperation. The system is used to implement the aforementioned cross-source structured query generation method based on multi-agent cooperation. The system includes: A query analysis agent is used to extract {entity, operation, condition} triples from raw natural language query text. A dynamic retrieval agent is used to construct a graph neural network based on a data source. It maps entities in the {entity, operation, condition} triple to nodes in the graph neural network, including: calculating the matching score between candidate nodes and entities in the graph neural network, and using the candidate node with the highest matching score as the anchor point; obtaining candidate paths connecting the anchor points corresponding to each entity, scoring the candidate paths based on path connection strength, path complexity, and cross-data source cost, and using the candidate path with the highest score as the optimal path; generating a physical connection scheme based on the optimal path, and searching for relevant historical SQL samples. Plan the intelligent agent to set up a structured query skeleton based on the {entity, operation, condition} triple, physical connection scheme, and relevant historical SQL samples; Generate an intelligent agent to generate SQL queries based on the structured query skeleton, physical connection scheme, and relevant historical SQL samples; A dual-validation agent is used to perform syntax and semantic validation on SQL queries. The incremental update module is used to correct SQL queries when the similarity is less than a threshold, and to store the original natural language query text and the corrected SQL query as a sample pair in the historical sample knowledge base.
[0008] Thirdly, embodiments of the present invention provide an electronic device, including: At least one processor; and A memory communicatively connected to the at least one processor; wherein, The memory stores one or more computer programs that can be executed by the at least one processor, and the one or more computer programs are executed by the at least one processor to enable the at least one processor to perform the above-described cross-source structured query generation method based on multi-agent cooperation.
[0009] Fourthly, embodiments of the present invention provide a computer-readable storage medium having a computer program stored thereon, characterized in that the computer program, when executed by a processor, implements the above-described method for generating cross-source structured queries based on multi-agent cooperation.
[0010] Fifthly, embodiments of the present invention provide a computer program product, including a computer program / instruction, characterized in that, when the computer program / instruction is executed by a processor, it implements the above-described method for generating cross-source structured queries based on multi-agent cooperation.
[0011] Compared with the prior art, the beneficial effects of the present invention are as follows: (1) Improve query accuracy: This invention retrieves cross-source schema relationships through dynamic graph neural networks and combines them with hybrid RAG to retrieve relevant historical SQL samples. The generated SQL queries can more accurately reflect the user's complex intent and are particularly good at handling multi-table joins, cross-source queries and complex conditions.
[0012] (2) Enhance cross-source adaptability and flexibility: This invention adapts to different database schemas, query types and new data sources through multi-agent collaboration. Dynamic schema modeling and path planning driven by graph neural networks can discover and establish the relationship between heterogeneous data sources.
[0013] (3) Improve generation efficiency: Multiple agents cooperate and some steps can be processed in parallel (such as schema retrieval and context retrieval), which significantly reduces the end-to-end query generation time.
[0014] (4) User-friendly: Users do not need to master SQL syntax or have in-depth knowledge of the underlying database schema details. They can complete complex cross-source data queries simply by using natural language. Attached Figure Description
[0015] To more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0016] Figure 1 This is a flowchart illustrating the cross-source structured query generation method based on multi-agent collaboration provided in an embodiment of the present invention. Figure 2 A structural diagram of the cross-source structured query generation method based on multi-agent collaboration provided in an embodiment of the present invention; Figure 3 This is a schematic diagram of an electronic device provided in an embodiment of the present invention. Detailed Implementation
[0017] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0018] It should be noted that, unless otherwise specified, the features in the following embodiments and implementation methods can be combined with each other.
[0019] like Figure 1 As shown, this embodiment of the invention provides a method for generating cross-source structured queries based on multi-agent collaboration, the method comprising the following sub-steps: Step S1: In response to the natural language query text, the query analysis agent outputs a triple of {entity, operation, condition}.
[0020] Specifically, this example uses a large language model to parse natural language query text, identify and standardize three core elements: entities, operations, and conditions. Based on a predefined SQL mapping table, the core elements are converted into standardized SQL elements or expressions, and the output is a triplet of {entity, operation, condition}.
[0021] Further, step S1 includes the following sub-steps: Step S101: Obtain the natural language query text input by the user, parse the query text using a large language model, and identify and standardize the three core elements: entities, operations, and conditions.
[0022] For example, the natural language query text input by the user is such as "Top three suppliers of electronic products in Beijing in 2024". Entities: Core data objects involved in the query, such as "product", "region", "time", and "supplier"; Actions: Aggregations or calculations performed on the data, such as "sum", "sort", "count", and "restriction"; Conditions: Limitations for data filtering, such as "time range", "numerical threshold", and "equality filtering".
[0023] Step S102: Convert the core elements into standardized SQL elements or expressions according to the predefined SQL mapping table, such as mapping "top three" to "RANK()OVER(ORDER BY ... DESC) + LIMIT 3", "sales amount" to "SUM()", and "Beijing region" to "region='Beijing'".
[0024] Step S103: Output the structured triples {entity, operation, condition}.
[0025] The example prompts for the large model are as follows: # System Role Definition You are a professional database query and parsing expert, and you need to output the results strictly in JSON format.
[0026] # Task decomposition instructions ## Step 1: Identify the core elements - Entities: Data objects involved in user queries (such as products / regions / times / suppliers). - Operations: Aggregations or calculations performed on data (such as summation / sorting / counting / limiting). - Conditions: Data filtering criteria (such as time range / numerical threshold / equivalence filtering) ## Step 2: Semantic Standardization (Refer to the mapping table) ## Step 3: Generate output that strictly follows the JSON format Input: {input_query} # Example Reference Enter: "Top 5 Clothing Sales in Shanghai in 2023" { "entities": ["Shanghai area", "clothing"], "actions": ["SUM()", "RANK()"], "conditions": ["year=2023", "LIMIT 5"] } Step S2: Construct a graph neural network based on the data source; map the entities in the {entity, operation, condition} triples to nodes in the graph neural network, including: calculating the matching score between candidate nodes and entities in the graph neural network, and taking the candidate node with the highest matching score as the anchor point; obtaining candidate paths connecting the anchor points corresponding to each entity, scoring the candidate paths based on path connection strength, path complexity, and cross-data source cost, and taking the candidate path with the highest score as the optimal path; generating a physical connection scheme based on the optimal path, and searching for relevant historical SQL samples.
[0027] Specifically, step S2 includes the following sub-steps: Step S201: Construct a graph neural network based on the data source.
[0028] Furthermore, in this example, metadata from all data sources is retrieved, including: table name, field name, primary key (PK), foreign key (FK), data type, and comments. A graph neural network is constructed using database tables as first-level nodes, such as `aws_schema.sales`; table fields as second-level nodes, such as `aws_schema.sales.amount`; the inclusion relationships between tables and fields are used as structural edges; explicitly defined foreign key references between fields are used as foreign key edges; and semantic similarity based on field names or comments is used as semantic edges. For example, a cosine similarity greater than 0.85 establishes a connection.
[0029] Step S202, mapping the entities in the {entity, operation, condition} triple to nodes in the graph neural network, includes: calculating the matching score between candidate nodes and entities in the graph neural network, and using the candidate node with the highest matching score as the anchor point.
[0030] The matching score between candidate nodes and entities in the graph neural network is calculated. The candidate node with the highest matching score is used as the anchor point, thereby mapping the entities in the {entity, operation, condition} triple to nodes in the graph neural network. The semantic matching degree between the node name and the entity, as well as the node importance of the candidate node, are calculated. The expression is as follows: In the formula, AnchorNode represents a graph node, v represents a candidate node in the graph neural network, and V represents the set of nodes in the graph neural network. Indicates embedding similarity, Represents an entity, Indicates the name of the candidate node v (derived from the schema metadata). This indicates the importance of candidate node v.
[0031] It should be noted that entities need to undergo standardization processing before calculation, such as synonym replacement and stop word removal. Degree(v) is calculated by comprehensively considering the number and weight of structural edges and foreign key edges.
[0032] Step S203: Start a walking path from each anchor point. Each move selects adjacent edges based on the movement probability. The walking path terminates when it connects all anchor points, returning all candidate paths. The movement probability is calculated based on the edge weights between pairs of nodes, the data source difference between pairs of nodes, and the cross-source penalty factor. The expression is as follows: In the formula, Let represent the probability that node i moves to node j. This represents the edge weight from node i to node j. This represents the cross-source penalty factor (set to 0.7 in this example; the larger the cross-source penalty factor, the heavier the penalty for crossing data sources). This indicates the difference in data sources between node i and node j (0 for same source, 1 for different source). This represents the edge weight from node i to node k.
[0033] Step S204: Score the candidate paths based on path connection strength, path complexity, cross-source penalty coefficient, and the number of different data sources crossed by the candidate path, and select the candidate path with the highest score as the optimal path.
[0034] Specifically, the calculation expression is as follows: In the formula, γ represents the edge weight; HopCount represents the number of hops in the path, with a logarithmic penalty applied, meaning the more hops, the heavier the penalty; γ represents the cross-source penalty coefficient (e.g., 0.5); and CrossSourceCount represents the number of different data sources the path crosses.
[0035] Step S205: Generate a physical connection scheme based on the optimal path.
[0036] Specifically, the cross-source connection logic is generated based on the optimal path, considering two cases: In the case of same origin: list nodes in path order, generate JOIN expressions according to edge type, where foreign key edges use INNER JOIN and semantic edges use LEFT JOIN; In cross-source scenarios, identify cross-source related fields (usually fields with high semantic similarity and compatible data types), use UNION ALL to build a virtual unified view based on these related fields, and return the interface definition of the view.
[0037] Step S206: Locate relevant historical SQL samples based on the optimal path.
[0038] Specifically, acquire a historical sample knowledge base; Calculate the text similarity between the original natural language query text and each historical SQL sample in the historical sample knowledge base; The original natural language query text is encoded using a graph neural network to obtain the topological structure vector corresponding to the original natural language query text; in this example, the graph neural network uses GraphSAGE; the cross-source schema connection relationships involved in the current query are compressed into a fixed-dimensional numerical vector; Calculate the topological similarity between the topological structure vector corresponding to the original natural language query text and the topological structure vector corresponding to each historical SQL sample in the historical sample knowledge base; We perform a weighted summation of text similarity and topological similarity, then sort the results, and select the top K historical SQL samples as relevant historical SQL samples. The expression is as follows: In the formula, Indicates weight, Indicates text similarity. This indicates the degree of topological similarity.
[0039] It should be noted that this invention encodes the original natural language query text using a graph neural network to obtain the topological structure vector corresponding to the original natural language query text, thereby possessing the structured cognitive ability for cross-source queries and solving the matching dilemma of "same semantics but different structures" or "different semantics but same structure" in traditional methods.
[0040] Step S3: Based on the {entity, operation, condition} triple, physical join scheme, and relevant historical SQL samples, set up the structured query skeleton.
[0041] Specifically, step S3 includes the following sub-steps: Step S301: Based on the physical connection scheme, determine which JOIN type or cross-source union operation to use, thereby determining the table.
[0042] Step S302: The standardized entities, operations, and conditions in the triples are precisely mapped to the physical table fields determined in the join scheme. For example, "sales amount" is mapped to cross_cloud_sales.amount, thereby determining the fields.
[0043] Step S303: Determine the core components such as the SELECT clause, FROM + table clause, WHERE + condition clause, GROUP BY clause, ORDER BY clause, and LIMIT clause, and output the structured query skeleton.
[0044] Step S4: In response to the structured query skeleton, physical join scheme, and relevant historical SQL samples, generate an SQL query. Specifically, the structured query skeleton, physical connection scheme, and relevant historical SQL samples are input into the large language model, and the large language model outputs SQL queries that conform to the ANSI SQL standard.
[0045] Here is an example of a prompt word template: # You are a professional SQL generation expert. Please generate a query that strictly conforms to the ANSI SQL-92 standard based on the following information: ## Database Schema Connection Relationships: {schema_graph_description OR physical_join_scheme} ## Example of related historical query: {sample_sql_1} {sample_sql_2} ... ## Query skeleton: {query_skeleton} ## Generation Requirements: 1. Accurately fill in the skeleton to generate complete SQL.
[0046] 2. Use standard syntax and functions.
[0047] 3. Ensure that the table name and field name are referenced correctly.
[0048] Step S5: Perform syntax and semantic validation on the SQL query.
[0049] Specifically, step S5 includes the following steps: The SQL query is validated using an SQL syntax parser (Apache Calcite is used in this example) to detect syntax errors such as spelling errors, keyword misuse, and structural errors. The SQL query is translated into reverse natural language query text using a large model. The similarity between the original natural language query text and the reverse natural language query text is calculated. If the similarity is less than a threshold, the semantic verification fails. When the similarity is less than the threshold, the SQL query is corrected by experts, and the original natural language query text and the corrected SQL query are stored as sample pairs in the historical sample knowledge base.
[0050] In addition, this example can dynamically adjust relevant parameters based on the error type and the characteristics of newly added samples.
[0051] For example, if the error stems from semantic mapping (such as mapping "phone" to category='phone' instead of 'mobile'), then when processing similar queries, the weight of text similarity is increased, making the system more inclined to recall historical samples containing the correct mapping. If the error stems from the cross-source join strategy, then the weight parameters in the GNN path planning are adjusted (such as increasing the cross-source penalty factor β).
[0052] The following examples illustrate the implementation process of this invention, and provide the relevant SQL and database schema for ease of understanding.
[0053] Implementation environment: A distributed system deployed on a Kubernetes cluster. The agent is developed based on the LangChain framework. The GNN model uses GraphSAGE. The semantic embedding model is text-embedding-3-large. Threshold parameters: The semantic verification similarity threshold is 0.85, and the cross-source penalty factor β is 0.7.
[0054] Example 1: Cross-source complex query scenario The original natural language query text was: "Top three suppliers of electronic products in Beijing in 2024". Step S1: Extract {entity, operation, condition} triples from the original natural language query text: { "entities": ["Beijing area", "electronic products", "suppliers"], "actions": ["SUM()", "RANK()", "LIMIT 3"], "conditions": ["region='Beijing'", "category='electronics'", "sale_dateBETWEEN '2024-01-01' AND '2024-12-31'"] } Step S2: Construct a graph neural network based on the data source; map entities in the {entity, operation, condition} triples to nodes in the graph neural network, including: calculating the matching score between candidate nodes and entities in the graph neural network, and taking the candidate node with the highest matching score as the anchor point; obtaining candidate paths connecting the anchor points corresponding to each entity, scoring the candidate paths based on path connection strength, path complexity, and cross-data source cost, and taking the candidate path with the highest score as the optimal path; generating physical connections based on the optimal path. Entity anchor point location: "Electronic Products" is located at aws_schema.products.category and gcp_dataset.items.product_type (which exist in different data sources); "Suppliers" is located at aws_schema.suppliers.name and gcp_dataset.vendors.vendor_name Cross-source path planning: The optimal path is aws_schema.sales →[FK] → aws_schema.suppliers and gcp_dataset.transactions →[FK] → gcp_dataset.vendors. The cross-source join scheme is to create a unified view based on the semantic edge category ≈ product_type, as shown in the SQL below: CREATE VIEW cross_cloud_sales AS SELECT 'AWS' AS source, amount, category, name AS supplier, region,sale_date FROM aws_schema.sales s JOIN aws_schema.products p ON s.product_id = p.product_id JOIN aws_schema.suppliers sup ON s.supplier_id = sup.supplier_id UNION ALL SELECT 'GCP' AS source, sales AS amount, product_type AS category, vendor_name AS supplier, region, txn_date AS sale_date FROM gcp_dataset.transactions t JOIN gcp_dataset.items i ON t.item_id = i.item_id JOIN gcp_dataset.vendors v ON t.vendor_code = v.vendorcode; Step S3: Locate relevant historical SQL samples based on the optimal path; Retrieve historical SQL samples related to the top 3, example SQL is as follows: / * Sample: Top 5 Clothing Sales in Shanghai in 2023 * / SELECT supplier, SUM(amount) FROM cross_cloud_sales WHERE region='Shanghai' AND category='Clothing' AND YEAR(sale_date)=2023 GROUP BY supplier ORDER BY SUM(amount) DESC LIMIT 5; Step S4: Based on the {entity, operation, condition} triples, physical join scheme, and relevant historical SQL samples, set the structured query skeleton. SELECT [Supplier field], SUM([Sales field]) AS total_sales FROM [Cross-Source View] WHERE [Regional Conditions] AND [Product Conditions] AND [Time Conditions] GROUP BY [supplier field] ORDER BY total_sales DESC LIMIT 3; Step S5: In response to the structured query skeleton, physical connection scheme, and relevant historical SQL samples, generate an SQL query; SELECT supplier, SUM(amount) AS total_sales FROM cross_cloud_sales WHERE region='Beijing' AND category='electronics' AND sale_date BETWEEN '2024-01-01' AND '2024-12-31' GROUP BY supplier ORDER BY total_sales DESC LIMIT 3; Step S6: Perform syntax and semantic validation on the SQL query.
[0055] The SQL query was syntax-validated using an SQL syntax parser, and the parsing was successful. The SQL query was translated into reverse natural language query text "Get the top three suppliers of electronic products in Beijing in 2024" using a large model. The similarity between the original natural language query text and the reverse natural language query text was calculated to be 0.93, which is greater than the threshold (> 0.85), thus passing semantic verification.
[0056] Example 2: Incremental Learning Triggering Scenario User input: "Mobile phone inventory" Error SQL generated (due to historical sample mapping deviation): SELECT SUM(stock) FROM inventory WHERE category='phone'; -- should be 'mobile' Semantic verification failed: The reverse description is "calculate the total inventory of telephone type", the similarity calculation is 0.72 < 0.85, triggering the incremental learning mechanism.
[0057] Manual correction: SELECT SUM(quantity) FROM inventory WHERE category='mobile'; -- Correct field name and value Add new historical SQL sample pairs to the historical sample knowledge base: {"NLQ": "Mobile Phone Inventory", "SQL": "SELECT SUM(quantity) FROM inventoryWHERE category='mobile'"} New additions to the thesaurus: mobile phone Example 3: Dynamically Adapting to New Data Sources Added data source: Snowflake table snowflake_analytics.inventory Original natural language query text: "Compare the electronic inventory of AWS and Snowflake". The processing steps are as follows: GNN Schema Dynamic Expansion: Load Snowflake metadata: GNN.add_node("snowflake_inventory", type="table", source="snowflake") GNN.add_node("snowflake_inventory.qty", type="column", parent="snowflake_inventory") GNN.add_edge("snowflake_inventory.sku", "product_catalog.sku", type="FK") # Automatically recognize foreign keys Cross-source path planning: Related fields: aws_schema.products.category ≈ snowflake_analytics.product_catalog.category Generate a UNION ALL scheme, and finally generate the SQL: SELECT 'AWS' AS source, SUM(i.stock_qty) AS inventory FROM aws_schema.inventory i JOIN aws_schema.products p ON i.product_id = p.product_id WHERE p.category = 'electronics' UNION ALL SELECT 'Snowflake' AS source, SUM(i.qty) AS inventory FROM snowflake_analytics.inventory i JOIN snowflake_analytics.product_catalog p ON i.sku = p.sku WHERE p.category = 'electronics'; In summary, this invention provides multi-agent collaborative decision-making, dynamic graph neural network retrieval of cross-source schema relationships, and hybrid RAG retrieval of the most relevant context and historical samples. It creatively solves the problem of generating structured SQL from complex natural language queries across heterogeneous data sources. The SQL queries generated by this invention can more accurately reflect the user's complex intent, and are particularly good at handling multi-table joins, cross-source queries, and complex conditions.
[0058] On the other hand, embodiments of the present invention provide a cross-source structured query generation system based on multi-agent cooperation. The system is used to implement the aforementioned cross-source structured query generation method based on multi-agent cooperation. The system includes: A query analysis agent is used to extract {entity, operation, condition} triples from raw natural language query text. A dynamic retrieval agent is used to construct a graph neural network based on a data source. It maps entities in the {entity, operation, condition} triple to nodes in the graph neural network, including: calculating the matching score between candidate nodes and entities in the graph neural network, and using the candidate node with the highest matching score as the anchor point; obtaining candidate paths connecting the anchor points corresponding to each entity, scoring the candidate paths based on path connection strength, path complexity, and cross-data source cost, and using the candidate path with the highest score as the optimal path; generating a physical connection scheme based on the optimal path, and searching for relevant historical SQL samples. Plan the intelligent agent to set up a structured query skeleton based on the {entity, operation, condition} triple, physical connection scheme, and relevant historical SQL samples; Generate an intelligent agent to generate SQL queries based on the structured query skeleton, physical connection scheme, and relevant historical SQL samples; A dual-validation agent is used to perform syntax and semantic validation on SQL queries. The incremental update module is used to correct SQL queries when the similarity is less than a threshold, and to store the original natural language query text and the corrected SQL query as a sample pair in the historical sample knowledge base.
[0059] Regarding the system in the above embodiments, the specific ways in which each module performs operations have been described in detail in the embodiments related to the method, and will not be elaborated here.
[0060] For the system embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to in the description of the method embodiments. The system embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this application according to actual needs. Those skilled in the art can understand and implement this without creative effort.
[0061] Accordingly, this application also provides an electronic device, including: one or more processors; a memory for storing one or more programs; and when the one or more programs are executed by the one or more processors, causing the one or more processors to implement the cross-source structured query generation method based on multi-agent cooperation as described above. Figure 3 The diagram shown is a hardware structure diagram of any device with data processing capabilities for the cross-source structured query generation method based on multi-agent collaboration provided in this embodiment of the invention, except... Figure 3In addition to the processor, memory, and network interface shown, any data processing device in the embodiment may also include other hardware depending on the actual function of the data processing device, which will not be described in detail here.
[0062] Accordingly, this application also provides a computer-readable storage medium storing computer instructions, which, when executed by a processor, implement the cross-source structured query generation method based on multi-agent cooperation as described above. The computer-readable storage medium can be an internal storage unit of any data-processing device as described in any of the foregoing embodiments, such as a hard disk or memory. The computer-readable storage medium can also be an external storage device, such as a plug-in hard disk, smart media card (SMC), SD card, flash card, etc., equipped on the device. Furthermore, the computer-readable storage medium can include both internal storage units of any data-processing device and external storage devices. The computer-readable storage medium is used to store the computer program and other programs and data required by the data-processing device, and can also be used to temporarily store data that has been output or will be output.
[0063] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the disclosure herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and embodiments are to be considered exemplary only.
[0064] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope.
Claims
1. A cross-source structured query generation method based on multi-agent cooperation, characterized in that, The method includes the following steps: Extract {entity, operation, condition} triples from the original natural language query text; A graph neural network is constructed based on the data source. Entities in the {entity, operation, condition} triples are mapped to nodes in the graph neural network, including: calculating the matching score between candidate nodes and entities in the graph neural network, and using the candidate node with the highest matching score as the anchor point; obtaining candidate paths connecting the anchor points corresponding to each entity, scoring the candidate paths based on path connection strength, path complexity, and cross-data source cost, and using the candidate path with the highest score as the optimal path; generating physical connection schemes based on the optimal path, and searching for relevant historical SQL samples. Based on the {entity, operation, condition} triple, physical join scheme, and relevant historical SQL samples, a structured query skeleton is set; in response to the structured query skeleton, physical join scheme, and relevant historical SQL samples, an SQL query is generated; and the SQL query is subjected to syntax and semantic validation.
2. The method of claim 1, wherein the method is based on multi-agent cooperation. The process of constructing a graph neural network based on a data source includes: A graph neural network is constructed using database tables as first-level nodes, table fields as second-level nodes, the inclusion relationship between tables and fields as structural edges, the explicitly defined foreign key reference relationship between fields as foreign key edges, and the semantic similarity based on field names or annotations as semantic edges.
3. The method for generating cross-source structured queries based on multi-agent collaboration according to claim 1, characterized in that, The process of mapping entities in the {entity, operation, condition} triple to nodes in a graph neural network includes: Calculate the matching score of candidate nodes to entities in the graph neural network, and use the candidate node with the highest matching score as the anchor point to map the entity in the {entity, operation, condition} triple to the node in the graph neural network. In this process, the semantic matching degree between the node name and the entity of the candidate node in the graph neural network, as well as the node importance of the candidate node, are calculated.
4. The method for generating cross-source structured queries based on multi-agent collaboration according to claim 1, characterized in that, The process of obtaining candidate paths connecting the anchor points corresponding to each entity, scoring the candidate paths based on path connection strength, path complexity, and cross-data source cost, and selecting the candidate path with the highest score as the optimal path includes: The traversal path starts from each anchor point. Each move selects an adjacent edge based on the move probability. The traversal path terminates when it connects all anchor points and returns all candidate paths. The move probability is calculated based on the edge weight between each pair of nodes, the data source difference between each pair of nodes, and the cross-source penalty factor. Candidate paths are scored based on path connectivity strength, path complexity, cross-source penalty coefficient, and the number of different data sources traversed by the candidate path, and the candidate path with the highest score is selected as the optimal path.
5. The method for generating cross-source structured queries based on multi-agent collaboration according to claim 1, characterized in that, The process of finding relevant historical SQL samples based on the optimal path includes: Obtain a historical sample knowledge base; Calculate the text similarity between the original natural language query text and each historical SQL sample in the historical sample knowledge base; The original natural language query text is encoded using a graph neural network to obtain the topological structure vector corresponding to the original natural language query text; Calculate the topological similarity between the topological structure vector corresponding to the original natural language query text and the topological structure vector corresponding to each historical SQL sample in the historical sample knowledge base; The text similarity and topological similarity are weighted and summed, and then sorted. The top K historical SQL samples are taken as relevant historical SQL samples.
6. The method for generating cross-source structured queries based on multi-agent collaboration according to claim 1, characterized in that, The process of performing syntax and semantic validation on SQL queries includes: The SQL query is syntax-validated using an SQL syntax parser; The SQL query is translated into reverse natural language query text using a large model. The similarity between the original natural language query text and the reverse natural language query text is calculated. If the similarity is less than a threshold, the semantic verification fails. When the similarity is less than the threshold, the SQL query is corrected, and the original natural language query text and the corrected SQL query are stored as a sample pair in the historical sample knowledge base.
7. A cross-source structured query generation system based on multi-agent collaboration, characterized in that, The system is used to implement the cross-source structured query generation method based on multi-agent cooperation as described in any one of claims 1-6, and the system includes: A query analysis agent is used to extract {entity, operation, condition} triples from raw natural language query text. A dynamic retrieval agent is used to construct a graph neural network based on a data source. It maps entities in the {entity, operation, condition} triple to nodes in the graph neural network, including: calculating the matching score between candidate nodes and entities in the graph neural network, and using the candidate node with the highest matching score as the anchor point; obtaining candidate paths connecting the anchor points corresponding to each entity, scoring the candidate paths based on path connection strength, path complexity, and cross-data source cost, and using the candidate path with the highest score as the optimal path; generating a physical connection scheme based on the optimal path, and searching for relevant historical SQL samples. Plan the intelligent agent to set up a structured query skeleton based on the {entity, operation, condition} triple, physical connection scheme, and relevant historical SQL samples; Generate intelligent agents to generate SQL queries based on structured query skeletons, physical connection schemes, and relevant historical SQL samples; A dual-validation agent is used to perform syntax and semantic validation on SQL queries. The incremental update module is used to correct SQL queries when the similarity is less than a threshold, and to store the original natural language query text and the corrected SQL query as a sample pair in the historical sample knowledge base.
8. An electronic device, characterized in that, include: At least one processor; as well as A memory communicatively connected to the at least one processor; wherein, The memory stores one or more computer programs that can be executed by the at least one processor, the one or more computer programs being executed by the at least one processor to enable the at least one processor to perform the cross-source structured query generation method based on multi-agent cooperation as described in any one of claims 1-6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the cross-source structured query generation method based on multi-agent cooperation as described in any one of claims 1-6.
10. A computer program product comprising a computer program / instructions, characterized in that, When the computer program / instruction is executed by the processor, it implements the cross-source structured query generation method based on multi-agent cooperation as described in any one of claims 1-6.