Multi-round dialogue database query method and device
By employing a multi-agent collaborative workflow, the problem of insufficient contextual understanding and complex queries in multi-turn dialogue database queries is solved, achieving highly accurate SQL generation and a user-friendly interactive experience.
Patent Information
- Application Number
- CN202511454459.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-13
- Publication Date
- 2025-11-11
AI Technical Summary
Existing technologies suffer from insufficient contextual understanding, illusion problems in complex queries, inefficient use of similar cases, and lack of process interpretability and secondary processing of results in database queries in multi-turn dialogue scenarios, resulting in a poor user experience.
The process of building a multi-agent collaborative workflow includes an intent recognition and question rewriting agent, a similar case retrieval agent, an SQL generation agent, and an answer summary and visualization agent. Through chain thinking and dynamic case retrieval, natural language questions are gradually transformed into executable SQL queries and processed intelligently.
It significantly improves the accuracy of understanding multi-turn dialogues, enhances the quality of complex SQL generation, strengthens the system's robustness and self-healing capabilities, and improves the system's interpretability and user experience.
Smart Images

Figure CN120929486A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of human-computer interaction technology, and in particular to a multi-turn dialogue database query method and apparatus. Background Technology
[0002] Currently, the technology of transforming natural language problems into database query languages (SQL, Text-to-SQL) is a research hotspot in the field of human-computer interaction. Its mainstream implementation methods can be broadly divided into two categories: traditional rule-based or syntax tree-based methods and methods based on deep learning, especially large language models (LLMs).
[0003] However, existing technologies still have significant shortcomings when dealing with database queries in multi-turn dialogue scenarios: 1. Insufficient context understanding; 2. The "illusion" problem of complex queries; 3. Inefficient use of similar cases; 4. Lack of process interpretability and secondary processing of results. Summary of the Invention
[0004] In view of this, the purpose of the present invention is to provide a multi-turn dialogue database query method and apparatus, which constructs a process in which multiple agents work together. This process can gradually and accurately transform the user's natural language questions, combined with dialogue history and external knowledge, into executable SQL queries, and intelligently process the results.
[0005] In a first aspect, embodiments of the present invention provide a multi-turn dialogue database query method, the method comprising: obtaining a natural language question currently input by a user; generating input information based on the natural language question and dialogue history; determining a rewriting reason and a rewriting question based on the input information; determining a reference pair corresponding to the rewriting question from a preset vector library; responding to an instruction to perform chained thinking; determining an SQL reason and an SQL query statement based on the rewriting reason, the rewriting question, pattern information of the target database to be queried, and the reference pair; and executing the SQL query statement to query the target database to obtain query results.
[0006] In an optional embodiment of this application, the step of generating input information based on natural language questions and dialogue history includes: obtaining the user's dialogue history from the dialogue manager for the most recent rounds; wherein the dialogue history includes: historical questions, generated SQL query statements and query result summaries; and concatenating the natural language questions and dialogue history to generate input information.
[0007] In an optional embodiment of this application, the steps of determining the rewriting reason and the rewriting question based on the input information include: inputting the input information into a language model; understanding the user's intent of the natural language question through the language model to obtain the rewriting reason; converting the colloquial natural language question into a standard query question through the language model, and using the standard query question as the rewriting question.
[0008] In an optional embodiment of this application, the vector library pre-stores vectors of multiple standard query questions and corresponding SQL query statements. The step of determining reference pairs corresponding to rewriting questions from the preset vector library includes: converting the rewriting questions into vectors; determining from the vector library whether there are vectors of target standard query questions whose similarity to the vectors of the rewriting questions is greater than a preset threshold; if so, determining several target standard query questions in descending order of similarity; and generating several reference pairs of question-SQL query statements based on the several target standard query questions and the corresponding SQL query statements.
[0009] In an optional embodiment of this application, the aforementioned schema information includes: table name, field name, field type, and primary / foreign key relationship; the step of determining the SQL reason based on the rewrite reason, rewrite problem, schema information of the target database to be queried, and reference pair includes: generating analysis text based on the rewrite reason, rewrite problem, schema information of the target database to be queried, and reference pair; wherein, the analysis text is used to explain how to construct an SQL query statement based on the rewrite problem, database schema, and reference case; and the analysis text is used as the SQL reason.
[0010] In an optional embodiment of this application, the step of executing an SQL query statement on the target database to perform data query and obtain query results includes: determining whether the SQL query statement contains a preset dangerous operation; if not, executing an SQL query statement on the target database to perform data query and obtain query results.
[0011] In optional embodiments of this application, the above method further includes: if the execution of the SQL query statement on the target database fails, obtaining the error information returned by the target database; re-determining the SQL reason and SQL query statement based on the error information, the rewriting reason, the rewriting problem, the schema information of the target database to be queried, and the reference pair; and reporting the error information to the user if the number of times the execution of the SQL query statement on the target database fails reaches the preset maximum number of retries.
[0012] In optional embodiments of this application, the above method further includes: generating a summary description based on the query results; determining whether to perform visualization based on the query results; and if so, generating chart information.
[0013] In an optional embodiment of this application, the method further includes: storing the interaction record of the current round in a dialogue manager; wherein the interaction record includes: natural language questions, rewriting questions, SQL query statements, query results, summary descriptions, and chart information.
[0014] Secondly, embodiments of the present invention also provide a multi-turn dialogue database query device, comprising: a user input receiving and dialogue history fusion module, used to acquire the natural language question currently input by the user, and generate input information based on the natural language question and dialogue history; an intent recognition and question rewriting module, used to determine the rewriting reason and the rewriting question based on the input information; a similar case retrieval module, used to determine the reference pair corresponding to the rewriting question from a preset vector library; an SQL generation module, used to respond to the instruction to perform chained thinking, and determine the SQL reason and SQL query statement based on the rewriting reason, the rewriting question, the pattern information of the target database to be queried, and the reference pair; and an SQL execution and verification module, used to execute the SQL query statement on the target database to perform data query and obtain query results.
[0015] The embodiments of the present invention bring the following beneficial effects: This invention provides a method and apparatus for multi-turn dialogue database querying. The method involves acquiring a natural language question currently input by a user, generating input information based on the natural language question and dialogue history, determining a rewriting reason and a rewriting question based on the input information, identifying a reference pair corresponding to the rewriting question from a preset vector library, and, in response to a chain-thinking instruction, determining an SQL reason and an SQL query statement based on the rewriting reason, the rewriting question, the pattern information of the target database to be queried, and the reference pair. The method then executes the SQL query statement to query the target database and obtain the query results. This approach significantly improves the accuracy of understanding multi-turn dialogues, greatly enhances the quality of complex SQL generation, strengthens the system's robustness and self-healing capabilities, and improves the system's interpretability and user experience.
[0016] Other features and advantages of this disclosure will be set forth in the following description, or some features and advantages may be inferred from the description or determined without doubt, or may be learned by practicing the techniques described above.
[0017] To make the above-mentioned objects, features and advantages of this disclosure more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description
[0018] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0019] Figure 1 A schematic diagram illustrating a mainstream implementation of SQL technology provided in an embodiment of the present invention; Figure 2 A flowchart illustrating a multi-turn dialogue database query method provided in an embodiment of the present invention; Figure 3 A schematic diagram illustrating a multi-turn dialogue database query method provided in an embodiment of the present invention; Figure 4 A flowchart illustrating another multi-turn dialogue database query method provided in an embodiment of the present invention; Figure 5 This is a schematic diagram of the structure of a multi-turn dialogue database query device provided in an embodiment of the present invention; Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention. Detailed Implementation
[0020] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions 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, 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.
[0021] Currently, see Figure 1 The diagram illustrates one of the mainstream implementations of SQL technology. These mainstream implementations can be broadly categorized into two types: 1. Traditional rule-based or syntax tree-based methods: Early Text-to-SQL systems typically relied on complex natural language parsing, semantic mapping rules, and SQL syntax tree construction. These methods require customizing a large number of rules for specific database schemas and language styles, resulting in high development and maintenance costs, poor generalization ability, and difficulty in adapting to different domains and complex natural language expressions.
[0022] 2. Deep Learning-Based Approaches, Especially Large Language Models (LLMs): In recent years, with the rise of large language models (such as the GPT series), Text-to-SQL technology has made groundbreaking progress. The mainstream approach directly takes the user's natural language question and the database's schema information (table structure, field names, etc.) as input, allowing the LLM to directly generate SQL queries. This end-to-end approach greatly simplifies the development process. To improve accuracy, some advanced methods employ "In-Context Learning" techniques, providing a few relevant "question-SQL" examples (few-shots) in the input to guide the LLM in generating more accurate queries.
[0023] However, existing technologies still have significant shortcomings when handling database queries in multi-turn dialogue scenarios. Firstly, in practical applications, they cannot solve complex problems, and users cannot trust the results generated by large models. Furthermore, users often ask follow-up questions based on the results of the previous query, such as, "What are the three most expensive items?" In such cases, the system must understand that the current follow-up question relies on previous dialogue history in order to generate the correct SQL.
[0024] Therefore, despite the progress made in existing technologies, they still face one or more of the following drawbacks in practical applications, especially in complex multi-turn dialogue scenarios: (1) Insufficient contextual understanding: While existing LLM methods can incorporate dialogue history when handling multi-turn dialogues, they often struggle to accurately capture contextual referential relationships (such as "among them," "they"), omitted information, and shifts in user intent. This frequently leads to misunderstandings of follow-up questions and the generation of incorrect SQL. For example, if a user asks "Query the sales figures for department A" and then asks "What about department B?", the model may not correctly interpret the follow-up question "What about...?" as a request with the same structure as the previous query.
[0025] (2) The "illusion" problem of complex queries: For queries that require complex logic (such as multi-table joins, nested subqueries, window functions, etc.), LLM is prone to "illusion" without sufficient relevant knowledge, that is, generating SQL statements that are syntactically correct but logically incorrect. Simply relying on the model's own knowledge base cannot guarantee a deep understanding of specific and complex database patterns.
[0026] (3) Inefficient use of similar cases: Although the Few-shot method provides some references, these examples are usually fixed and cannot dynamically and adaptively retrieve the most relevant and valuable "problem-SQL" pairs from the historical case library based on the specific content of the current problem. This makes it difficult for the model to learn from past successful experiences to solve new problems that are highly similar but slightly different.
[0027] (4) Lack of process interpretability and secondary processing of results: Most methods directly output the final SQL statement and query results, making the whole process like a "black box". The system does not provide a "reason" for generating SQL, making it difficult to debug when SQL errors occur. At the same time, the returned raw data tables are not user-friendly for non-technical users, lacking automated summarization and visualization, which reduces the user experience.
[0028] Based on this, the present invention provides a multi-turn dialogue database query method and apparatus, specifically providing a multi-turn dialogue database query method that integrates dialogue history and retrieval cases. Its core idea is to construct a process in which multiple agents work together. This process can gradually and accurately transform the user's natural language questions, combined with dialogue history and external knowledge, into executable SQL queries, and intelligently process the results.
[0029] To facilitate understanding of this embodiment, a multi-turn dialogue database query method disclosed in this embodiment of the invention will first be described in detail.
[0030] Example 1: This invention provides a multi-turn dialogue database query method, see [link to relevant documentation]. Figure 2 The flowchart shown illustrates a multi-turn dialogue database query method, which includes the following steps: Step S202: Obtain the natural language question currently input by the user, and generate input information based on the natural language question and dialogue history.
[0031] See Figure 3 The diagram illustrates a multi-turn dialogue database query method. This embodiment can receive user input and integrate dialogue history.
[0032] In some embodiments, the user's recent dialogue history can be obtained from the dialogue manager; wherein the dialogue history includes: historical questions, generated SQL query statements and query result summaries; the natural language questions and the dialogue history are concatenated to generate input information.
[0033] The system in this embodiment can receive the natural language question currently input by the user. Simultaneously, the system retrieves the dialogue history (including historical questions, generated SQL query statements, and query result summaries) from the dialogue manager for the most recent N rounds. The current question (i.e., the natural language question currently input by the user) is concatenated with the dialogue history to form a complete input information rich in context.
[0034] In addition to simply concatenating the dialogue history of the most recent N rounds, this embodiment can also perform more intelligent "summary compression" on the dialogue history. That is, after each round of dialogue, a specialized language model extracts the core information of that round (such as the queried entity, the imposed constraints, and the summary of the results) into a short summary sentence and stores it in the history. This can reduce the length of the context input to the model while preserving key information, thereby reducing processing costs and potential noise interference.
[0035] Step S204: Determine the reason for rewriting and the rewriting problem based on the input information.
[0036] like Figure 3 As shown, the system can also perform intent recognition and question rewriting. The Agent can pass input information, which incorporates the dialogue history, to the "intent recognition and question rewriting Agent." This Agent is a specially fine-tuned language model whose core task is to use the "chain-of-thought" approach.
[0037] In some embodiments, input information can be fed into a language model; the language model can be used to understand the user's intent regarding the natural language question and obtain the reason for rewriting; the language model can be used to transform the colloquial natural language question into a standard query question, and the standard query question can be used as the rewriting question.
[0038] In this embodiment, the "Intent Identification and Question Rewriting Agent" can be executed in two steps: 1. Generating a rewrite rationale: First, the model needs to output an analysis text explaining its understanding of the user's intent. For example: "Analysis: The user's current question 'What about Beijing?' is a follow-up to the previous question 'Query the sales figures of all stores in Shanghai.' The intent is to query data on the same topic (sales figures) but for different entities (Beijing). Therefore, 'Beijing' needs to be replaced with 'Shanghai' while maintaining the core structure of the query." 2. Generate and rewrite the question: After providing the reasons, rewrite the original, colloquial question into an independent, unambiguous, and complete "standard query question." For example, rewrite it as: "Query the sales figures of all stores in Beijing."
[0039] In this embodiment, the "Intent Recognition and Question Rewriting Agent" can be implemented using model fine-tuning. The prompt will explicitly instruct the model: "Please first analyze and explain your understanding of the user's intent, and then rewrite the last question into a clear, independently executable query request." A sample of the fine-tuning data is as follows: enter: Human: How do I check the sales figures for all stores in Shanghai? Assistant: The total sales revenue of all stores in Shanghai this year is 2 million yuan.
[0040] Human: What about Beijing? Output: <think>The user previously queried the sales figures for all stores in Shanghai. This time, the user's question is: "What about Beijing?" The user is likely continuing from the previous question, replacing Shanghai with Beijing, and querying the sales figures for all stores.
[0041] < / think> How can I check the sales figures for all stores in Beijing? Step S206: Determine the reference pair corresponding to the rewrite problem from the preset vector library.
[0042] like Figure 3 As shown, the rewritten "standard query question" (i.e., the rewritten question) can be used as input to start the "similar case retrieval agent" and determine the reference pair corresponding to the rewritten question from the preset vector library.
[0043] In some embodiments, the vector library pre-stores vectors of multiple standard query questions and corresponding SQL query statements; the rewrite question can be converted into a vector; it is determined from the vector library whether there are vectors of target standard query questions whose similarity to the vectors of the rewrite question is greater than a preset threshold; if so, several target standard query questions are determined in descending order of similarity; several question-SQL query statement reference pairs are generated based on the several target standard query questions and their corresponding SQL query statements.
[0044] In this embodiment, the "Similar Case Retrieval Agent" can be constructed and retrieved: the system pre-maintains a "Question-SQL" vector library. This library stores a large number of high-quality "standard query questions" and their corresponding verified correct SQL statements. When a new standard query question is input, the Agent converts it into a vector using a text embedding model (such as BERT (Bidirectional Encoder Representations from Transformers, a pre-trained language model with a bidirectional Transformer structure), Sentence-BERT (a sentence embedding model based on BERT), etc.), and then performs a similarity search (such as cosine similarity) in the vector library to retrieve the Top-K most similar "Question-SQL" reference pairs.
[0045] In this embodiment, the vector library can employ solutions such as FAISS (an open-source, high-efficiency similarity search library), Milvus (a cloud-native open-source vector database), and Weaviate (an open-source vector search engine). This step is a typical application of Search Augmentation Generation (RAG). During retrieval, only the similarity of the questions is matched, and the top-K questions and SQL pairs of similar questions are assembled as a reference for generating subsequent SQL query statements.
[0046] In addition to using vector similarity retrieval alone, this embodiment can also employ "Hybrid Search" technology. This combines semantic-based vector retrieval with traditional keyword-based sparse retrieval (such as the BM25 algorithm), using the combined scores of both to rank the results. This helps handle queries containing specific, rare keywords, improving both recall and precision.
[0047] Step S208, in response to the instruction to perform chain thinking, determines the SQL reason and SQL query statement based on the rewriting reason, the rewriting question, the schema information of the target database to be queried, and the reference pair.
[0048] like Figure 3 As shown, the "SQL Generation Agent" in this embodiment receives the following five pieces of information as input: 1. The rewritten "Standard Query Problem".
[0049] 2. The reasons for rewriting the question provide a deeper level of intent context for the model.
[0050] 3. Schema information of the target database, including table names, field names, field types, primary and foreign key relationships, etc.
[0051] 4. The top-K similar "question-SQL" reference pairs retrieved.
[0052] 5. A specially designed instruction template that requires the model to engage in "chain thinking".
[0053] In some embodiments, schema information includes: table name, field name, field type, and primary / foreign key relationship; analysis text can be generated based on the rewrite reason, rewrite problem, schema information of the target database to be queried, and reference pairs; wherein, the analysis text is used to explain how to construct SQL query statements based on the rewrite problem, database schema, and reference cases; the analysis text is used as the SQL reason.
[0054] In this embodiment, the "SQL generation agent" is typically a powerful, general-purpose large language model that can perform the following tasks: 1. SQL Generation Reasoning: First, according to the instructions, the model needs to output an analysis text explaining how it will construct the SQL based on the problem, database schema, and reference case. For example: "Based on the user's problem, tables A and B need to be queried. Tables A and B can be joined using field C. Reference case 1 provides a similar multi-table join query paradigm. Therefore, I will construct a query containing JOIN…ON…". 2. Generate SQL: After providing the reason, the model generates the final SQL query statement.
[0055] Step S210: Execute an SQL query statement on the target database to retrieve data and obtain the query results.
[0056] like Figure 3 As shown, this embodiment can also perform SQL execution and verification (including automatic loop correction). The system can obtain the generated SQL query statement and connect to the target database for execution.
[0057] Furthermore, regarding the "SQL generation agent," this embodiment could use a single general-purpose large language model as the SQL generation agent, or it could adopt a "Mixture of Experts" (MoE) architecture that integrates multiple expert models. For example, a lightweight model specifically designed for handling simple queries, a model specifically designed for handling multi-table joins, and a model specifically designed for handling complex window functions could be trained. Distributing the problem to the most suitable expert model for processing through a routing network can balance efficiency and effectiveness.
[0058] In some embodiments, it can be determined whether the SQL query statement contains a preset dangerous operation; if not, the SQL query statement is executed on the target database to query the data and obtain the query results.
[0059] This embodiment first performs a security check: before executing the SQL query statement, the system performs a security scan to check for predefined dangerous operations such as "DROP" and "DELETE" to prevent malicious queries. If the SQL query statement executes successfully, the query results are obtained.
[0060] In some embodiments, if executing an SQL query statement on the target database fails, the error information returned by the target database is obtained; based on the error information, the reason for rewriting, the problem of rewriting, the schema information of the target database to be queried, and the reference pair, the SQL reason and the SQL query statement are redefined; if the number of times the SQL query statement is executed on the target database fails reaches the preset maximum number of retries, the error information is reported to the user.
[0061] If the SQL query fails to execute, the system will capture the error message returned by the database (such as "column not found", "table name ambiguous", etc.). This will trigger a Self-Correction Loop: the system will resubmit the error message along with all the original input to the "SQL Generation Agent" with an instruction such as: "The previous SQL execution failed, the error message is '[Insert error message here]'. Please analyze the cause of the error and generate a corrected SQL." This correction process can be configured with a maximum number of retries (e.g., 2) to prevent infinite loops. If the maximum number of retries is reached and the query still fails, an error message will be reported to the user.
[0062] This invention provides a multi-turn dialogue database query method. The method involves obtaining the user's current natural language question, generating input information based on the natural language question and dialogue history, determining the rewriting reason and rewriting question based on the input information, identifying the reference pair corresponding to the rewriting question from a preset vector library, and, in response to a chain-thinking instruction, determining the SQL reason and SQL query statement based on the rewriting reason, rewriting question, pattern information of the target database to be queried, and the reference pair. The method then executes the SQL query statement to query the target database and obtain the query results. This approach significantly improves the understanding accuracy of multi-turn dialogues, greatly enhances the generation quality of complex SQL, strengthens the system's robustness and self-healing capabilities, and improves the system's interpretability and user experience.
[0063] Compared with existing technologies, the embodiments of the present invention, through their unique multi-agent collaborative workflow design, bring the following significant benefits: 1. Significantly improves the accuracy of understanding multi-turn dialogues: Through a specialized "intent recognition and question rewriting agent," this invention can accurately parse complex linguistic phenomena such as pronouns and ellipsis in the dialogue context, and rewrite ambiguous follow-up questions into clear and independent query tasks. This effectively solves the problem of SQL generation errors caused by insufficient context understanding in existing technologies.
[0064] 2. Significantly improves the quality of complex SQL generation: This invention creatively combines two enhancement mechanisms: first, it dynamically provides the model with the most relevant success examples (RAG) through a "Similar Case Retrieval Agent"; second, it allows the model to output analytical reasons before generating SQL through "chain thinking." The combination of these two mechanisms significantly reduces the probability of the model experiencing "illusions" when processing complex queries (such as multi-table joins and nested queries).
[0065] 3. Enhanced system robustness and self-healing capabilities: The unique "automatic correction loop" mechanism enables the system to attempt self-repair when faced with database execution errors, rather than simply failing and returning an error. This mechanism makes the system more robust in real-world and ever-changing environments.
[0066] 4. Improved system interpretability and user experience: This invention not only generates SQL, but also generates easy-to-understand "reasons", so that the entire decision-making process is no longer a "black box", which is convenient for developers to debug and optimize, and can also show the reasons to users, allowing users to feel the reasoning path of the model.
[0067] Example 2: This embodiment provides another method for multi-turn dialogue database querying, which is implemented based on the above embodiment, focusing on describing the working method of the "answer summary and visualization agent". See also Figure 4 The flowchart shown represents another multi-turn dialogue database query method, which includes the following steps: Step S402: Obtain the natural language question currently input by the user, and generate input information based on the natural language question and dialogue history.
[0068] Step S404: Determine the reason for rewriting and the rewriting problem based on the input information.
[0069] Step S406: Determine the reference pair corresponding to the rewrite problem from the preset vector library.
[0070] Step S408, in response to the instruction to perform chain thinking, determines the SQL reason and SQL query statement based on the rewriting reason, the rewriting question, the schema information of the target database to be queried, and the reference pair.
[0071] Step S410: Execute an SQL query statement on the target database to retrieve data and obtain the query results.
[0072] Step S412: Generate a summary description based on the query results; determine whether to perform visualization based on the query results; if so, generate chart information.
[0073] like Figure 3 As shown, the data returned after successful SQL execution (usually in tabular form) can be passed to the "Answer Summary and Visualization Agent".
[0074] The "Answer Summary and Visualization Agent" in this embodiment can perform the following tasks: 1. Answer Summary: The agent will summarize the query results in natural language based on the user's original question. For example, it can summarize a table containing multiple rows of data as: "Department A's total sales are 1 million yuan, and the top salesperson is Zhang San." 2. Chart Generation Suggestions: Based on the data structure and type of the query results, this Agen will determine whether the data is suitable for visualization. If suitable, it will generate configuration information for rendering the chart (such as the JSON configuration required for using libraries like ECharts and Plotly). For example, if the result is time series data, it suggests generating a line chart; if it is categorical data, it suggests generating a bar chart or pie chart.
[0075] Therefore, the "Answer Summary and Visualization Agent" in this embodiment can transform cold, hard data into humanized text summaries and intuitive charts, greatly enhancing the end-user's interactive experience.
[0076] In some embodiments, the interaction records of the current round can also be stored in the dialogue manager; wherein, the interaction records include: natural language questions, rewriting questions, SQL query statements, query results, summary descriptions and chart information.
[0077] like Figure 3 As shown, this embodiment can also update the dialogue history. The system will record the interaction of the current round completely, including the user's original question, the rewritten standard question, the generated SQL, the returned result summary and chart information, and store them in the dialogue manager to provide context for the next round of dialogue.
[0078] In addition to the collaborative workflow model for Agents, Figure 3 In addition to the fixed-sequence "pipeline" workflow shown, a "Router Agent" or "Planner Agent" model can also be used. In this model, a central control agent dynamically determines which functional agent (such as question rewriting, case retrieval, SQL generation, etc.) should be invoked next based on the current dialogue status and the question. It can even invoke multiple agents in parallel, thereby achieving a more flexible task processing flow.
[0079] In summary, the embodiments of the present invention mainly provide the following: 1. A Text-to-SQL processing method based on multi-agent workflow, which decomposes complex query tasks into multiple sequential sub-tasks handled by different agents, such as intent recognition, question rewriting, case retrieval, SQL generation, execution correction, and answer summarization.
[0080] 2. A two-level chain thinking mechanism that includes "reason generation" forces the model to generate and analyze reasons in the two key steps of "problem rewriting" and "SQL generation" before generating the final output (the rewritten problem or SQL statement), and uses the reasons of the previous stage as the input of the next stage to form a deeply connected reasoning chain.
[0081] 3. A SQL generation method that combines dynamic case retrieval and error correction. When generating SQL, it uses similar "problem-SQL" pairs retrieved from the vector library as a reference, and triggers an automatic correction loop using error information when SQL execution fails.
[0082] 4. A query post-processing system that integrates answer summarization and chart suggestions, which can automatically summarize the results in natural language after the SQL query is executed, and generate corresponding visualization chart configurations based on data characteristics.
[0083] Example 3: This embodiment provides another method for multi-turn dialogue database query, which is implemented based on the above embodiment, and focuses on describing a specific scenario case.
[0084] Scenario setting: Suppose we have a sales database containing the following three tables: employees (employee table): id, name, department; stores (store table): id, store_name, city; sales (sales record table): id, product_name, amount, sale_date, employee_id, store_id.
[0085] User: A sales manager.
[0086] (a) First round of dialogue: The user entered: "What was the total sales volume in Shanghai in the first half of 2024?" System processing flow: Step 1: Receive user input and integrate dialogue history.
[0087] Current question: "What was the total sales volume in Shanghai for the first half of 2024?" Dialogue history: (blank).
[0088] Step 2: Intent Identification and Question Rewriting Agent.
[0089] The generated reason for the rewrite is: "Analysis: This is a new query request. The core intent is to calculate the sum (total sales). It includes two filtering conditions: time range (first half of 2024, i.e., from 2024-01-01 to 2024-06-30) and geographical location (Shanghai area). The query needs to join the sales record table and the store table." The generated rewrite question is: "Calculate the total sales amount of all stores located in Shanghai between January 1, 2024 and June 30, 2024." Step 3: Similar Case Search Agent.
[0090] The system vectorizes the rewritten question and retrieves the most similar case from the "Question-SQL" database: Similar cases found: {"question":"Query the total sales amount in Guangzhou in 2023", "sql":"SELECT SUM(s.amount) FROM sales s JOIN stores st ON s.store_id = st.id WHEREst.city = 'Guangzhou' AND s.sale_date BETWEEN '2023-01-01' AND '2023-12-31';"}.
[0091] Step 4: Generate Agent using SQL.
[0092] The Agent receives the rewritten problem, the reason for the rewrite, the database schema, and similar cases.
[0093] Reason for generating SQL: "Analysis: Based on the user's revised question, it is necessary to calculate the total sales amount, i.e., SUM(sales.amount). The stores table needs to be filtered for 'Shanghai' using the city field, and the sales table needs to be filtered for time using the sales_date field. The sales and stores tables can be joined using sales.store_id=stores.id. The reference case provides the correct SUM and JOIN syntax." Generate SQL: SELECT SUM(s.amount) FROM sales s JOIN stores st ON s.store_id = st.id WHERE st.city = 'Shanghai' AND s.sale_date BETWEEN '2024-01-01' AND '2024-06-30'.
[0094] Step 5: SQL execution and verification.
[0095] The SQL statement passed the security check and was successfully executed in the database.
[0096] Execution result: [{"SUM(s.amount)": 5500000}].
[0097] Step 6: Summarize the answers and visualize the Agent.
[0098] Answer summary: "Okay, the query results are as follows: In the first half of 2024, the total sales in Shanghai amounted to 5.5 million yuan." Chart generation suggestion: (If the result is a single numerical value, it is not suitable for chart display, so no suggestion will be generated).
[0099] Step 7: Update the conversation history.
[0100] Store the complete interaction information for this round in the dialogue history.
[0101] (ii) Second round of dialogue (demonstrating multi-round dialogue handling capabilities).
[0102] The user typed: "What about the Beijing area?" System processing flow: Step 1: Receive user input and integrate dialogue history.
[0103] Current question: "What about the Beijing area?" Dialogue history: (Contains all information from the first round of dialogue).
[0104] Step 2: Intent Identification and Question Rewriting Agent.
[0105] The generated reason for the rewrite is: "Analysis: This is a follow-up question based on the previous round of dialogue. The user's 'What about...?' sentence indicates that the query structure (calculating total sales) and time range (first half of 2024) remain unchanged, but the geographical location filter is changed from 'Shanghai' to 'Beijing'." The generated rewrite question is: "Calculate the total sales amount of all stores located in Beijing between January 1, 2024 and June 30, 2024." Steps 3 and 4: Case retrieval and SQL generation.
[0106] The system then performs the retrieval and generation process again based on the revised question.
[0107] Reason for generating SQL: "Analysis: Based on the user's revised question and reason for revision, the structure of this query is the same as the previous one. Only the city in the WHERE clause needs to be replaced from 'Shanghai' to 'Beijing'." Generate SQL: SELECT SUM(s.amount) FROM sales s JOIN stores st ON s.store_id = st.id WHERE st.city = 'Beijing' AND s.sale_date BETWEEN '2024-01-01' AND '2024-06-30'.
[0108] Step 5: SQL Execution and Verification (Show the ability of automatic correction).
[0109] Suppose the initially generated SQL is incorrect: SELECT SUM(amount) FROM sales s JOIN stores st ONs.store_id = st.id WHERE city = 'Beijing'... (There are ambiguous columns amount and city).
[0110] The database returns an error: Error: column "city" is ambiguous.
[0111] Trigger the automatic correction loop: The system submits the error message together with the original input to the SQL generation Agent again.
[0112] The Agent generates a correction reason: "Analysis: The execution failed, and the error message indicates that the column name 'city' is ambiguous. This is because there may be columns named city in both the sales and stores tables (assumed here), or the model forgot to add a table prefix. st.city should be specified explicitly." Generate the corrected SQL: SELECT SUM(s.amount) FROM sales s JOIN stores st ONs.store_id = st.id WHERE st.city = 'Beijing' AND s.sale_date BETWEEN '2024-01-01' AND '2024-06-30'; Execute again: The SQL is successfully executed.
[0113] Execution result: [{"SUM(s.amount)": 4800000}].
[0114] Steps 6 and 7: Answer Summary and History Update.
[0115] Answer summary: "The total sales in the Beijing area during the same period was 4.8 million yuan." The system updates the conversation history.
[0116] Example 4: Corresponding to the above method embodiments, this invention provides a multi-turn dialogue database query device, see [link to relevant documentation]. Figure 5 The diagram shown illustrates the structure of a multi-turn dialogue database query device, which includes: The user input receiving and dialogue history fusion module 51 is used to obtain the natural language question currently input by the user and generate input information based on the natural language question and dialogue history. The intent recognition and question rewriting module 52 is used to determine the rewriting reason and the rewriting question based on the input information; The similar case retrieval module 53 is used to determine the reference pairs corresponding to the rewriting problem from the preset vector library; SQL generation module 54 is used to respond to instructions for chain thinking by determining the SQL reason and SQL query statement based on the rewriting reason, rewriting question, schema information of the target database to be queried, and reference pairs. The SQL execution and verification module 55 is used to execute SQL query statements on the target database to query data and obtain query results.
[0117] This invention provides a multi-turn dialogue database query device that acquires the user's current input natural language question, generates input information based on the natural language question and dialogue history, determines the rewriting reason and rewriting question based on the input information, determines the reference pair corresponding to the rewriting question from a preset vector library, and, in response to a chain-thinking instruction, determines the SQL reason and SQL query statement based on the rewriting reason, rewriting question, pattern information of the target database to be queried, and the reference pair, and executes the SQL query statement to query the target database to obtain the query results. This method can significantly improve the understanding accuracy of multi-turn dialogues, greatly improve the generation quality of complex SQL, enhance the robustness and self-healing ability of the system, and improve the interpretability and user experience of the system.
[0118] The aforementioned user input receiving and dialogue history fusion module is used to obtain the user's recent dialogue history from the dialogue manager; wherein, the dialogue history includes: historical questions, generated SQL query statements and query result summaries; the natural language questions and dialogue history are concatenated to generate input information.
[0119] The aforementioned intent recognition and question rewriting module is used to input input information into a language model; understand the user's intent in the natural language question through the language model to obtain the reason for rewriting; transform the colloquial natural language question into a standard query question through the language model, and use the standard query question as the rewriting question.
[0120] The aforementioned vector library pre-stores vectors of multiple standard query questions and corresponding SQL query statements. The aforementioned similar case retrieval module is used to convert the rewrite question into a vector; determine from the vector library whether there exists a vector of a target standard query question whose similarity to the vector of the rewrite question is greater than a preset threshold; if so, determine several target standard query questions in descending order of similarity; and generate several question-SQL query statement reference pairs based on the several target standard query questions and their corresponding SQL query statements.
[0121] The aforementioned schema information includes: table name, field name, field type, and primary / foreign key relationships; the aforementioned SQL generation module is used to generate analysis text based on the rewrite reason, rewrite problem, schema information of the target database to be queried, and reference pairs; wherein, the analysis text is used to explain how to construct SQL query statements based on the rewrite problem, database schema, and reference cases; the analysis text serves as the SQL reason.
[0122] The SQL execution and verification module described above is used to determine whether the SQL query statement contains any preset dangerous operations; if not, it executes the SQL query statement on the target database to retrieve the query results.
[0123] The aforementioned device further includes: an SQL loop execution module, used to obtain error information returned by the target database if the execution of an SQL query statement to the target database fails; to redetermine the SQL reason and SQL query statement based on the error information, rewriting reason, rewriting problem, schema information of the target database to be queried, and reference pair; and to report error information to the user if the number of times the execution of the SQL query statement to the target database fails reaches the preset maximum number of retries.
[0124] The aforementioned device also includes: a visualization module, used to generate a summary description based on the query results; determine whether to perform visualization based on the query results; and if so, generate chart information.
[0125] The aforementioned device further includes: an interaction record storage module, used to store the interaction record of the current round in the dialogue manager; wherein, the interaction record includes: natural language questions, rewriting questions, SQL query statements, query results, summary descriptions and chart information.
[0126] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the multi-turn dialogue database query device described above can be referred to the corresponding process in the embodiments of the aforementioned multi-turn dialogue database query method, and will not be repeated here.
[0127] Example 5: This invention also provides an electronic device for running the above-described multi-turn dialogue database query method; see also Figure 6 The diagram shows the structure of an electronic device, which includes a memory 100 and a processor 101. The memory 100 is used to store one or more computer instructions, which are executed by the processor 101 to implement the aforementioned multi-turn dialogue database query method.
[0128] Furthermore, Figure 6 The electronic device shown also includes a bus 102 and a communication interface 103, with the processor 101, the communication interface 103 and the memory 100 connected via the bus 102.
[0129] The memory 100 may include high-speed random access memory (RAM) and may also include non-volatile memory, such as at least one disk storage device. Communication between this system network element and at least one other network element is achieved through at least one communication interface 103 (which can be wired or wireless), such as the Internet, wide area network, local area network, metropolitan area network, etc. The bus 102 may be an ISA bus, PCI bus, or EISA bus, etc. The bus can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 6 The symbol is represented by a single double-headed arrow, but this does not mean that there is only one bus or one type of bus.
[0130] Processor 101 may be an integrated circuit chip with signal processing capabilities. In implementation, each step of the above method can be completed by the integrated logic circuitry in the hardware of processor 101 or by instructions in software form. Processor 101 can be a general-purpose processor, including a Central Processing Unit (CPU), a Network Processor (NP), etc.; it can also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this invention. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of this invention can be directly manifested as execution by a hardware decoding processor, or execution by a combination of hardware and software modules in the decoding processor. The software module can reside in a readily available storage medium in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, or registers. This storage medium is located in memory 100, and processor 101 reads information from memory 100 and, in conjunction with its hardware, completes the steps of the method described in the foregoing embodiments.
[0131] This invention also provides a computer-readable storage medium storing computer-executable instructions. When these computer-executable instructions are invoked and executed by a processor, they cause the processor to implement the aforementioned multi-turn dialogue database query method. For specific implementation details, please refer to the method embodiments, which will not be repeated here.
[0132] The computer program product of the multi-turn dialogue database query method and apparatus provided in the embodiments of the present invention includes a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the methods in the preceding method embodiments. For specific implementation, please refer to the method embodiments, which will not be repeated here.
[0133] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the system and / or device described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0134] Furthermore, in the description of the embodiments of the present invention, unless otherwise explicitly specified and limited, the terms "installation," "connection," and "linking" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal connection of two components. Those skilled in the art can understand the specific meaning of the above terms in the present invention based on the specific circumstances.
[0135] If a function is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0136] In the description of this invention, it should be noted that the terms "center," "upper," "lower," "left," "right," "vertical," "horizontal," "inner," and "outer," etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are used only for the convenience of describing the invention and for simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on the invention. Furthermore, the terms "first," "second," and "third" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.
[0137] Finally, it should be noted that the above-described embodiments are merely specific implementations of the present invention, used to illustrate the technical solutions of the present invention, and not to limit it. The scope of protection of the present invention is not limited thereto. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments within the technical scope disclosed in the present invention, or make equivalent substitutions for some of the technical features; and these modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be covered within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A multi-turn dialogue database query method, characterized in that, The method includes: Obtain the natural language question currently input by the user, and generate input information based on the natural language question and the dialogue history; Based on the input information, determine the reasons for rewriting and the rewriting issues; Determine the reference pair corresponding to the rewriting problem from the preset vector library; In response to the instruction to perform chain thinking, the SQL reason and SQL query statement are determined based on the rewriting reason, the rewriting problem, the schema information of the target database to be queried, and the reference pair; The SQL query statement is executed on the target database to retrieve data and obtain the query results.
2. The method according to claim 1, characterized in that, The steps for generating input information based on the natural language question and dialogue history include: Retrieve the user's recent conversation history from the conversation manager; wherein the conversation history includes: historical questions, generated SQL query statements, and a summary of query results; The natural language question and the dialogue history are concatenated to generate input information.
3. The method according to claim 1, characterized in that, The steps for determining the rewriting reason and rewriting problem based on the input information include: The input information is then input into the language model; The language model is used to understand the user's intent regarding the natural language problem and to derive the reasoning for rewriting. The language model transforms the colloquial natural language problem into a standard query problem, and then uses the standard query problem as a rewriting problem.
4. The method according to claim 3, characterized in that, The vector library pre-stores vectors of multiple standard query questions and corresponding SQL query statements for the standard query questions. The step of determining the reference pair corresponding to the rewriting problem from a preset vector library includes: Transform the rewriting problem into vectors; Determine from the vector library whether there exists a target standard query question vector whose similarity to the vector of the rewriting question is greater than a preset threshold; If they exist, determine several target standard query questions based on the similarity in descending order; Based on several target standard query questions and the corresponding SQL query statements, several question-SQL query statement reference pairs are generated.
5. The method according to claim 1, characterized in that, The schema information includes: table name, field name, field type, and primary / foreign key relationship; The steps for determining the SQL reason based on the rewriting reason, the rewriting problem, the schema information of the target database to be queried, and the reference pair include: An analysis text is generated based on the stated rewrite reason, the stated rewrite problem, the schema information of the target database to be queried, and the reference pair; wherein, the analysis text is used to explain how to construct an SQL query statement based on the rewrite problem, the database schema, and the reference case; Use the analyzed text as the SQL reason.
6. The method according to claim 1, characterized in that, The steps of executing the SQL query statement on the target database to retrieve data and obtain query results include: Determine whether the SQL query statement contains any pre-set dangerous operations; If it does not exist, execute the SQL query statement on the target database to retrieve the data and obtain the query results.
7. The method according to claim 1, characterized in that, The method further includes: If the execution of the SQL query statement on the target database fails, obtain the error information returned by the target database; Based on the error message, the reason for rewriting, the rewriting problem, the schema information of the target database to be queried, and the reference pair, the SQL reason and SQL query statement are redefined. If the number of failed SQL query attempts to the target database reaches the preset maximum number of retries, an error message will be reported to the user.
8. The method according to any one of claims 1-7, characterized in that, The method further includes: A summary description is generated based on the query results; Based on the query results, determine whether to perform a visualization display; if so, generate chart information.
9. The method according to claim 8, characterized in that, The method further includes: The interaction records of the current round are stored in the dialogue manager; wherein, the interaction records include: the natural language question, the rewriting question, the SQL query statement, the query result, the summary description, and the chart information.
10. A multi-turn dialogue database query device, characterized in that, The device includes: The user input receiving and dialogue history fusion module is used to obtain the natural language question currently input by the user and generate input information based on the natural language question and dialogue history. The intent recognition and question rewriting module is used to determine the rewriting reason and the rewriting question based on the input information. A similar case retrieval module is used to determine the reference pair corresponding to the rewriting problem from a preset vector library; The SQL generation module is used to respond to instructions for chain thinking by determining the SQL reason and SQL query statement based on the rewriting reason, the rewriting problem, the schema information of the target database to be queried, and the reference pair. The SQL execution and verification module is used to execute the SQL query statement on the target database to query data and obtain the query results.
Citation Information
Patent Citations
Natural language intelligent query method and device based on multi-agent interaction
CN118012900A
Underground coal mine early warning data query method and system based on language large model
CN118733608A
System and method for managing artificial conversational entities enhanced by social knowledge
US20180075335A1
Cited By
Query statement generation method, electronic equipment and storage medium
CN121387937A