Natural language data query method, device and equipment based on multi-agent cooperation
Patent Information
- Application Number
- CN202611311988.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-27
- Publication Date
- 2026-10-16
AI Technical Summary
[0003]本发明实施例提供了基于多智能体协作的自然语言数据查询方法、装置及设备,旨在解决现有技术中传统的数据库查询方式需要依赖数据分析师或研发人员编写SQL查询语句,对用户专业性要求高导致数据查询结果获取效率较低,即使通过结合人工智能模型实现了自然语言到SQL的方式,其转换准确率受限也同样导致数据查询结果获取效率较低的问题
[0008] This invention provides a method, apparatus, and device for natural language data querying based on multi-agent collaboration. The method includes: in response to a data query command sent by a user terminal, acquiring natural language query text; inputting the natural language query text into a query statement generating agent to generate a current SQL statement; inputting the current SQL statement into a query statement executing agent to obtain a current query result; inputting the current query result into a result interpreting agent to obtain a current natural language interpretation result corresponding to the current query result; and sending the current query result and the current natural language interpretation result to the user terminal. This invention enables the user-input natural language query text to obtain query results and result interpretations through the collaboration of three agents, eliminating the need for users to write SQL statements, reducing the professional requirements for users, and improving the efficiency of data query result acquisition.
Smart Images

Figure CN122925422A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence technology, and in particular to a method, apparatus, and device for natural language data query based on multi-agent collaboration. Background Technology
[0002] Currently, database queries involve users writing query statements (such as Structured Query Language, or SQL) to retrieve the desired data results from relational databases. However, with the continuous growth of enterprise data and the increasing frequency of data query demands from business personnel, traditional database query methods suffer from the following pain points: 1) SQL queries require a high level of expertise, meaning that business personnel usually need to rely on data analysts or R&D personnel to write SQL query statements, which results in high communication costs and long cycles; when business issues are urgent, waiting in line to write SQL queries seriously affects decision-making efficiency. 2) The accuracy of natural language to SQL conversion is limited. Existing Text-to-SQL solutions usually use a single AI model (AI stands for Artificial Intelligence) to directly convert natural language into SQL statements. However, in complex query scenarios (such as multi-table joins, nested subqueries, time window aggregation, etc.), the SQL statements generated by a single AI model are prone to syntax errors or logical deviations. Due to the lack of result verification and error feedback, erroneous SQL is directly exposed to users, resulting in a poor user experience. Summary of the Invention
[0003] This invention provides a natural language data query method, apparatus, and device based on multi-agent collaboration, aiming to solve the problem that traditional database query methods in the prior art require data analysts or R&D personnel to write SQL query statements, which has high requirements for user expertise and results in low efficiency in obtaining data query results. Even if natural language to SQL is achieved by combining artificial intelligence models, the limited conversion accuracy also leads to low efficiency in obtaining data query results.
[0004] In a first aspect, embodiments of the present invention provide a natural language data query method based on multi-agent collaboration, comprising: In response to a data query command sent by a user terminal, obtain the natural language query text; The natural language query text is input into the query statement generation agent to generate the current SQL statement; The current SQL statement is input into the query statement execution agent to obtain the current query result; The current query result is input into the result interpretation agent to obtain the current natural language interpretation result corresponding to the current query result; The current query result and the current natural language interpretation result are sent to the user terminal.
[0005] Secondly, embodiments of the present invention also provide a natural language data query device based on multi-agent collaboration, comprising: The query text acquisition unit is used to acquire natural language query text in response to a data query command sent by the user terminal; The query statement generation unit is used to input the natural language query text into the query statement generation agent to generate the current SQL statement. The query statement execution unit is used to input the current SQL statement into the query statement execution agent to obtain the current query result; The query result interpretation unit is used to input the current query result into the result interpretation agent to obtain the current natural language interpretation result corresponding to the current query result; The query result sending unit is used to send the current query result and the current natural language interpretation result to the user terminal.
[0006] Thirdly, embodiments of the present invention also provide a computer device, which includes a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the method described in the first aspect above.
[0007] Fourthly, embodiments of the present invention also provide a computer-readable storage medium storing a computer program, the computer program including program instructions that, when executed by a processor, can implement the method described in the first aspect above.
[0008] This invention provides a method, apparatus, and device for natural language data querying based on multi-agent collaboration. The method includes: in response to a data query command sent by a user terminal, acquiring natural language query text; inputting the natural language query text into a query statement generating agent to generate a current SQL statement; inputting the current SQL statement into a query statement executing agent to obtain a current query result; inputting the current query result into a result interpreting agent to obtain a current natural language interpretation result corresponding to the current query result; and sending the current query result and the current natural language interpretation result to the user terminal. This invention enables the user-input natural language query text to obtain query results and result interpretations through the collaboration of three agents, eliminating the need for users to write SQL statements, reducing the professional requirements for users, and improving the efficiency of data query result acquisition. Attached Figure Description
[0009] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the following description of the embodiments will be briefly introduced. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0010] Figure 1 A schematic diagram illustrating an application scenario of the natural language data query method based on multi-agent collaboration provided in an embodiment of the present invention; Figure 2 A flowchart illustrating the natural language data query method based on multi-agent collaboration provided in an embodiment of the present invention; Figure 3 This is a schematic diagram of the first sub-process of the natural language data query method based on multi-agent collaboration provided in an embodiment of the present invention; Figure 4 This is a schematic diagram of the second sub-process of the natural language data query method based on multi-agent collaboration provided in an embodiment of the present invention; Figure 5 This is a schematic diagram of the third sub-process of the natural language data query method based on multi-agent collaboration provided in an embodiment of the present invention; Figure 6 This is a schematic diagram of the fourth sub-process of the natural language data query method based on multi-agent collaboration provided in an embodiment of the present invention; Figure 7 This is a schematic diagram of the fifth sub-process of the natural language data query method based on multi-agent collaboration provided in an embodiment of the present invention; Figure 8 A schematic block diagram of a natural language data query device based on multi-agent collaboration provided in an embodiment of the present invention; Figure 9 A schematic block diagram of a computer device provided for an embodiment of the present invention. Detailed Implementation
[0011] 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, not all, of the embodiments of the present invention. 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.
[0012] It should be understood that, when used in this specification and the appended claims, the terms "comprising" and "including" indicate the presence of the described features, integrals, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or collections thereof.
[0013] It should also be understood that the terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to limit the invention. As used in this specification and the appended claims, the singular forms “a,” “an,” and “the” are intended to include the plural forms unless the context clearly indicates otherwise.
[0014] It should also be further understood that the term "and / or" as used in this specification and the appended claims refers to any combination of one or more of the associated listed items and all possible combinations, and includes such combinations.
[0015] Please also refer to Figure 1 and Figure 2 ,in Figure 1 This is a schematic diagram illustrating an application scenario of the natural language data query method based on multi-agent collaboration according to an embodiment of the present invention. Figure 2 This is a flowchart illustrating the natural language data query method based on multi-agent collaboration provided in an embodiment of the present invention. Figure 1 As shown, the natural language data query method based on multi-agent collaboration provided in this embodiment of the invention is applied to server 10. Server 10 is communicatively connected to user terminal 20, and user terminal 20 is a smart terminal such as a smartphone or tablet computer used by the user.
[0016] This application describes a technical solution with a server as the execution entity. To better understand the technical solution of this application, the overall system architecture of the data query platform deployed on the server is described in detail below. The data query platform includes a front-end interface layer, a QueryWorkflow orchestration layer, an intelligent agent layer, and a back-end service layer. The front-end page layer includes at least a natural language input box, an SQL editing area, a result table display area, a progress indicator, and an AI interpretation panel. The natural language input box is the area where users input natural language according to their data query needs; the SQL editing area is the area where the data query platform converts the natural language query input by the user into SQL statements, which the user can then customize and edit; the result table display area is the table display area for the query results obtained from the conversion of the natural language input by the user into SQL statements and the corresponding database; and the AI interpretation panel is the area that displays the intelligent interpretation of the query results through an artificial intelligence model.
[0017] The QueryWorkflow orchestration layer includes at least a step scheduler, an onStepComplete callback mechanism, and a state manager. The workflow object (i.e., the QueryWorkflow object) in the step scheduler is used to set the scheduling order among multiple agents in the agent layer. The onStepComplete callback mechanism is triggered after the execution of the step corresponding to each agent in the multiple agents orchestrated in the workflow order. The input parameters of the onStepComplete callback function are the step name (stepName) and the step result (result). The progress indicator in the front-end page layer updates the interface display in real time based on the output result of the onStepComplete callback function (such as displaying progress indicators such as generating SQL, executing SQL, and interpreting results). The state manager displays the specific values of the state machine corresponding to each agent in the multiple agents orchestrated in the workflow order, such as managing the specific values of the three core state fields: isRunning, error, and result.
[0018] The agent layer deploys multiple agents arranged in workflow order. Specifically, in this application, the agent layer deploys a query statement generation agent (e.g., represented by SqlGeneratorAgent), a query statement execution agent (e.g., represented by SqlExecutorAgent), and a result interpretation agent (e.g., represented by ResultExplainerAgent). These agents are arranged sequentially according to their workflow order to obtain the current complete processing chain. The backend service layer provides several loaders and API interfaces (API stands for Application Program Interface) for the multiple agents in the agent layer to call.
[0019] like Figure 2 As shown, the method includes the following steps S110 to S150.
[0020] S110, In response to the data query command sent by the user terminal, obtain the natural language query text.
[0021] In this embodiment, the technical solution is described with the server as the executing entity. After logging into the server's data query platform using a user terminal by entering user login information (such as user account and password), the user can input natural language query text in the natural language input box of the user interaction interface corresponding to the data query platform, based on their current actual query needs. When the user completes the input of the natural language query text in the natural language input box and clicks the corresponding submit button, a data query instruction is triggered. The server detects the data query instruction, obtains the corresponding natural language query text, and performs subsequent processing. The above process eliminates the need for the user to input SQL statements, reducing the difficulty of entering the user's initial query needs.
[0022] In one embodiment, prior to step S110, the method further includes: Construct a first intelligent agent base class corresponding to the intelligent agent that generates the query statement, a second intelligent agent base class corresponding to the intelligent agent that executes the query statement, and a third intelligent agent base class corresponding to the intelligent agent that interprets the result; wherein, the first intelligent agent base class, the second intelligent agent base class, and the third intelligent agent base class each include at least a state machine, an artificial intelligence model invocation method, a prompt word identifier backend loading mechanism, and a reset method; The workflow of generating an agent based on the query statement, executing the query statement, and interpreting the result is arranged sequentially to obtain the current complete processing link.
[0023] In this embodiment, the custom logic of multiple agents in the agent layer is specifically defined through the BaseAgent base class. After constructing the first agent base class corresponding to the agent that generates the query statement, the second agent base class corresponding to the agent that executes the query statement, and the third agent base class corresponding to the agent that interprets the result, the state machine, artificial intelligence model calling method, prompt word identifier backend loading mechanism, and reset method corresponding to these three agent base classes are all uniformly defined in the BaseAgent base class.
[0024] Specifically, the unified state machine corresponding to the three intelligent agent base classes mentioned above maintains three core state fields: isRunning, error, and result. The unified AI model invocation method corresponding to these three intelligent agent base classes incorporates the callAI() method, encapsulates HTTP requests, handles timeouts (AbortController), parses multi-format responses, and handles exceptions. In the unified prompt word identifier backend loading mechanism corresponding to these three intelligent agent base classes, the callAI() method does not carry the complete prompt word; it only sends the promptKey identifier (i.e., prompt word identifier) and the user's query content (i.e., natural language query text). The backend service layer loads the corresponding prompt word template based on the promptKey identifier, achieving physical isolation between the prompt word and the frontend code. The unified reset method corresponding to these three intelligent agent base classes is the reset() method, which restores the intelligent agent state (i.e., Agent state) to its initial value and supports workflow retries and reuse.
[0025] The three agents are arranged sequentially according to the workflow of generating the agent based on the query statement, executing the query statement, and interpreting the result. In the current complete processing chain, the output of the previous agent serves as the input of the next agent. Specifically, the output of the query statement generating agent, which is the natural language query text, serves as the input of the query statement executing agent, and the output of the query statement executing agent serves as the input of the result interpreting agent. When the processing of each agent is considered a processing step, the `onStepComplete` callback function is triggered after each step is completed. The input parameters of the `onStepComplete` callback function are the step name (`stepName`) and the step result (`result`). The progress indicator in the front-end page layer updates the interface display in real time based on the output of the `onStepComplete` callback function (e.g., displaying progress indicators such as "SQL being generated," "SQL being executed," and "Result being interpreted"). Furthermore, agents exchange data by sharing the `state.results` object. If any step fails, the workflow is immediately interrupted and an exception is thrown, while retaining the current step information and the results of completed steps for display in the front-end interface layer.
[0026] S120. Input the natural language query text into the query statement generation agent to generate the current SQL statement.
[0027] In this embodiment, after the natural language query text is input into the query statement generation agent, the query statement generation agent, as the first core agent, is responsible for generating the SQL statement. The process of generating the current SQL statement based on the natural language query text involves interface calls, query statement cleaning, and query statement formatting; the specific processing steps are described later.
[0028] In one embodiment, such as Figure 3 As shown, step S120 includes: S121. Call the preset artificial intelligence interface, send the natural language query text and the corresponding prompt word identifier to the artificial intelligence interface, the artificial intelligence interface obtains the current query statement prompt word based on the prompt word identifier and the natural language query text, and sends it to the corresponding large language model to execute the corresponding artificial intelligence model to generate the query statement; S122. Based on a preset cleaning strategy, mark the Markdown code blocks in the query statement generated by the artificial intelligence model to obtain the cleaned query statement; S123. The cleaned query statement is formatted and formatted according to a preset regular expression replacement strategy to obtain the current SQL statement.
[0029] In this embodiment, when calling the preset artificial intelligence interface, the prompt word loader is called from the backend service layer. The prompt word loader obtains the prompt word identifier (i.e., promptKey) corresponding to the natural language query text. After the backend service layer obtains the prompt word template from the preset prompt word template library, it is concatenated with the natural language query text to obtain the prompt word of the current query statement. Then, the prompt word of the current query statement is sent to the large language model called by the backend service layer, so as to obtain the query statement generated by the artificial intelligence model.
[0030] The query statements generated by the artificial intelligence model often include Markdown code block markers (such as SQL... etc. Markdown code block markers). Specifically, the Markdown code block markers in the query statements generated by the artificial intelligence model can be cleaned up using a cleanup strategy to obtain cleaned query statements, which are pure SQL text.
[0031] Afterwards, the cleaned query statement can be formatted and formatted according to the regular expression replacement strategy to obtain the current SQL statement. The regular expression replacement strategy mainly processes the cleaned query statement as follows: inserting newlines before keywords such as FROM, WHERE, GROUP BY, ORDER BY, HAVING, and LIMIT; inserting newlines and indentation before the AND / OR connectors in the WHERE clause; inserting newlines and indentation at comma separators in the field list after SELECT; and compressing redundant whitespace characters. After these processing steps, a more readable SQL statement is obtained.
[0032] In one embodiment, such as Figure 4 As shown, step S121 includes: S1211. Obtain the prompt word template corresponding to the prompt word identifier from the preset prompt word template library; S1212. Add the natural language query text to the prompt word template to generate the prompt word for the current query statement.
[0033] In this embodiment, a prompt word loader is invoked from the backend service layer. The prompt word loader obtains the prompt word identifier (i.e., promptKey) corresponding to the natural language query text. The backend service layer then calls the ClickHouse Query API interface to load the corresponding target clickstream data warehouse query initial statement (i.e., the target clickstream data warehouse SQL statement, or ClickHouse SQL). A prompt word template is retrieved from a preset prompt word template library and concatenated with the natural language query text to obtain the current query statement prompt word. This current query statement prompt word is then sent to the large language model invoked through the AI Chat API interface of the backend service layer, thereby obtaining the query statement generated by the artificial intelligence model. Because the preset prompt word template library is deployed at the backend service layer, compared to storing the complete system prompt word template library at the frontend page layer, users cannot modify the prompt words to bypass security restrictions (such as injection attacks, unauthorized queries, etc.), thus improving the security of prompt word management.
[0034] After the query statement generation agent generates the current SQL statement and displays it in the SQL editing area, the user can choose to either not adjust it or manually edit the SQL statement. After adjusting the current SQL statement displayed in the SQL editing area, clicking the "Execute" button will continue to the next step, S130. The manually edited SQL statement is no longer input into the query statement generation agent for secondary SQL statement generation. Instead, it undergoes execution detection and result interpretation by the query statement execution agent, ensuring that the manually modified SQL statement achieves the same processing quality as the SQL statement generated by the AI model. By retaining a human intervention mechanism after the query statement generation agent generates the current SQL statement, compared to existing Text-to-SQL systems where SQL statements are either directly executed or completely rewritten by the user, an intermediate interactive mode of manual fine-tuning is added between AI generation and re-execution. When the user is basically satisfied with the AI-generated SQL statement but needs to fine-tune a certain filtering condition or time range, the fine-tuning is highly efficient and does not require rewriting the SQL statement.
[0035] S130. Input the current SQL statement into the query statement execution agent to obtain the current query result.
[0036] In this embodiment, after the current SQL statement is input to the query statement execution agent, the query statement execution agent acts as the second core agent responsible for handling SQL statement execution and data querying. The process of executing the current SQL statement and retrieving the current query results from the corresponding database involves interface calls, multi-format parsing, and query statement formatting; the specific processing steps are described later.
[0037] In one embodiment, such as Figure 5 As shown, step S130 includes: S131. Send the current SQL statement to the clickstream data warehouse query interface corresponding to the query statement execution agent and execute it accordingly to obtain the current initial query result; S132. Based on a preset multi-format parsing strategy, perform multi-format response parsing on the current initial query result to obtain the current response parsing result; S133. The column names and field values of the current response parsing result are automatically extracted and rendered to the front-end table to obtain the current query result.
[0038] In this embodiment, when the query statement execution agent retrieves the current query result from the database corresponding to the data query platform, it first sends the current SQL statement to the clickstream data warehouse query interface (i.e., the query interface of the ClickHouse database) corresponding to the query statement execution agent and executes it accordingly to obtain the current initial query result. Then, the obtained current initial query result is parsed using the multi-format parsing strategy to adapt to the differences in response formats of API interfaces of different backend service layers. After completing the multi-format parsing, all field names are extracted from the first record in the result set corresponding to the current response parsing result as a column array (including multiple column names), and the field values corresponding to all column names are also extracted. This data can then be automatically rendered to the table display area of the front-end interface layer.
[0039] In one embodiment, such as Figure 6 As shown, step S132 includes: S1321. Determine whether result_rows[0].data in the current initial query result is an array or whether it contains the first parsed result of the rows field; S1322. If it is determined that the first parsing result is result_rows[0].data is an array or contains a rows field, then the current initial query result is parsed using the first response parsing format in the multi-format parsing strategy to obtain the current response parsing result; S1323. If it is determined that the first parsing result is result_rows[0].data is not an array and does not contain the rows field, then determine whether result_rows in the current initial query result is the second parsing result of the data array; S1324. If it is determined that the second parsing result is a data array result_rows, then the current initial query result is parsed using the second response parsing format in the multi-format parsing strategy to obtain the current response parsing result.
[0040] In this embodiment, when parsing the current initial query result using the multi-format parsing strategy, in addition to the first and second response parsing formats listed above, the multi-format parsing strategy also includes a third, fourth, and fifth response parsing formats. Specifically, if it is determined that the second parsing result is not a data array (result_rows), then it is determined whether the data field in the current initial query result is an array or a third parsing result containing a rows field. If it is determined that the third parsing result is that the data field is an array or contains a rows field, then the current initial query result is parsed using the third response parsing format in the multi-format parsing strategy to obtain the current response parsing result. If it is determined that the third parsing result is not a data field but an array and does not contain a rows field, then it is determined whether the top layer in the current initial query result is an array (fourth parsing result). If it is determined that the top layer in the current initial query result is an array, then the current initial query result is parsed using the fourth response parsing format in the multi-format parsing strategy to obtain the current response parsing result. If it is determined that the top-level element in the current initial query result is not an array, then it is determined whether the rows field of the top-level element in the current initial query result is an array (the fifth parsing result). If it is determined that the fifth parsing result is that the rows field of the top-level element in the current initial query result is an array, then the current initial query result is parsed using the fifth response parsing format in the multi-format parsing strategy to obtain the current response parsing result. Generally, through the above five-level degradation parsing strategy, it is possible to adapt to the differences in response formats of API interfaces of different versions of the backend service layer, ensuring that the system remains compatible during the evolution of the backend service layer's API interfaces.
[0041] In practice, the system can also intelligently detect whether the query execution agent is executing the current SQL statement and whether there is a timeout. It can identify timeout errors by scanning keywords (including keywords such as timeout, timeout, timed out) and matching combined patterns (such as the combination of execution time and too long / Maximum). Technical timeout information can be converted into user-friendly prompts such as "Query timeout: The data volume is too large. Please try to shorten the query time range (such as changing to query the most recent 1-3 days) or add more filtering conditions."
[0042] S140. Input the current query result into the result interpretation agent to obtain the current natural language interpretation result corresponding to the current query result.
[0043] In this embodiment, the current query result is input to the result interpretation agent. The agent then performs an intelligent interpretation based on the type of the current query result. For example, if the current query result contains an "error" field, it enters error interpretation mode, constructing error analysis prompts including error cause analysis, modification suggestions, and timeout handling suggestions, which are then fed into the large language model to generate an interpretation. If the current query result contains valid query results, it enters normal interpretation mode, extracting the first five lines of data as a summary to construct normal interpretation prompts, which are then fed into the large language model to generate a 1-2 sentence explanation of the business meaning. Therefore, through the above interpretation, the execution result of the entire SQL statement provides a clear and easily understandable natural language interpretation result for the user.
[0044] In one embodiment, such as Figure 7 As shown, step S140 includes: S141. If it is determined that the current query result corresponds to a valid query result, then the current filtered dataset with a preset number of data entries is obtained from the current query result, and the corresponding constructed normal explanation prompt words and the current filtered dataset are sent to the corresponding invoked large language model to obtain the corresponding natural language normal explanation result as the current natural language explanation result. S142. If it is determined that the current query result includes an error field, then according to the corresponding constructed error analysis prompt, the error analysis prompt and the current query result are sent to the corresponding invoked large language model to obtain the corresponding natural language error interpretation result as the current natural language interpretation result.
[0045] In this embodiment, if it is determined that the current query result includes an error field (i.e., includes the error field), it means that the execution of the current SQL statement has not successfully obtained the query result. It is necessary to construct an error analysis prompt word that includes error cause analysis, modification suggestions, and timeout handling suggestions, and send it to the large language model so that the large language model can output a new type of current natural language explanation result that includes artificial intelligence experts explaining the error cause, providing modification suggestions, and suggesting narrowing the query scope for timeout errors.
[0046] If it is determined that the current query result corresponds to a valid query result, it means that the current SQL statement was successfully executed and the query result was obtained. At this time, the current filtered dataset with a preset number of data entries (such as 5 entries, or other values can be flexibly adjusted according to user needs) is extracted from the current query result as a summary, normal explanation prompt words are constructed and sent to the large language model, so that the large language model can output 1-2 sentences explaining the meaning and business value of the data.
[0047] If the current query result is determined to include an error field, a pre-defined error self-healing mechanism can be used for corresponding processing. Specifically, the original error information is first captured and keywords such as "isTimeout" and "TooLongError" are detected. If these keywords are present, it indicates a timeout error, and a user-friendly prompt message can be returned, such as "query timed out," suggesting narrowing the query scope. If these keywords are not present, the original error information is returned directly. Then, an error analysis prompt word containing error cause analysis, modification suggestions, and timeout handling suggestions is constructed through the result interpretation agent and sent to the large language model. This prompt requests the large language model to output a new type of current natural language interpretation result, including explanations of the error cause from an AI expert, modification suggestions, and suggestions to narrow the query scope for timeout errors. After viewing the current natural language interpretation result, the user can manually modify the current SQL statement and, after clicking the execute button, re-execute steps S130-S140 to obtain the query result and interpretation result. By adopting the above-mentioned error self-healing mechanism, compared with the traditional approach of directly displaying the original database error information (such as Code:62. DB::Exception: Syntax error) to the user after the SQL statement execution fails, this application allows users to manually modify and edit the SQL statement after referring to the interpretation results of the natural language representation, thereby improving the error self-healing capability.
[0048] S150. Send the current query result and the current natural language interpretation result to the user terminal.
[0049] In this embodiment, the current query results are displayed in a table area on the front-end page of the data query platform, and the current natural language interpretation results are displayed through an AI interpretation panel. This method enables the current query results and the current natural language interpretation results to be sent to the user terminal for intuitive display. Therefore, this application decouples the three stages of query statement generation, execution, and interpretation into independent intelligent agents, each focusing on a single responsibility, thus improving the system's maintainability and scalability.
[0050] In one embodiment, the method further includes the following after step S150: If a complex query request sent by the user terminal is detected, the complex query natural language text corresponding to the complex query request is obtained. The complex query natural language text is input into a pre-deployed autonomous query agent to obtain the corresponding current complex query result.
[0051] In this embodiment, an autonomous query agent (e.g., ReActQueryAgent) can be deployed in the agent layer of the data query platform. This autonomous query agent processes complex queries through Think, Act, and Observe loops. In each loop, the large language model called by the autonomous query agent considers the next action and returns structured decision text (ACTION: QUERY or ACTION: ANSWER). After parsing the structured decision text, the corresponding SQL query is executed or the answer is returned directly. The Think, Act, and Observe loops can be configured with a maximum number of iterations (e.g., 5 times, or other values can be flexibly adjusted according to user needs). When the autonomous query agent reaches the maximum number of iterations, it returns a simplified problem suggestion. The final output query result can be summarized using the summarizeResult method and context is added to avoid passing a large amount of data to the large language model.
[0052] As can be seen, the implementation of this method can obtain query results and interpretations from the natural language query text input by the user through the collaboration of three intelligent agents, without requiring the user to write SQL statements, reducing the professional requirements of the user and improving the efficiency of data query result acquisition.
[0053] Corresponding to the above-described natural language data query method based on multi-agent cooperation, this invention also provides a natural language data query device based on multi-agent cooperation. This device includes a unit for executing the aforementioned natural language data query method based on multi-agent cooperation. For example... Figure 8 As shown, the natural language data query device 100 based on multi-agent collaboration includes a query text acquisition unit 110, a query statement generation unit 120, a query statement execution unit 130, a query result interpretation unit 140, and a query result sending unit 150.
[0054] The query text acquisition unit 110 is used to acquire natural language query text in response to a data query command sent by a user terminal.
[0055] In this embodiment, the technical solution is described with the server as the executing entity. After logging into the server's data query platform using a user terminal by entering user login information (such as user account and password), the user can input natural language query text in the natural language input box of the user interaction interface corresponding to the data query platform, based on their current actual query needs. When the user completes the input of the natural language query text in the natural language input box and clicks the corresponding submit button, a data query instruction is triggered. The server detects the data query instruction, obtains the corresponding natural language query text, and performs subsequent processing. The above process eliminates the need for the user to input SQL statements, reducing the difficulty of entering the user's initial query needs.
[0056] In one embodiment, the natural language data query device 100 based on multi-agent collaboration further includes: The intelligent agent base class construction unit is used to construct a first intelligent agent base class corresponding to the intelligent agent that generates the query statement, a second intelligent agent base class corresponding to the intelligent agent that executes the query statement, and a third intelligent agent base class corresponding to the intelligent agent that interprets the result; wherein, the first intelligent agent base class, the second intelligent agent base class, and the third intelligent agent base class each include at least a state machine, an artificial intelligence model invocation method, a prompt word identifier backend loading mechanism, and a reset method; The agent workflow orchestration unit is used to orchestrate the workflow sequence of generating agent according to the query statement, executing agent according to the query statement, and interpreting agent according to the result in order to obtain the current complete processing link.
[0057] In this embodiment, the custom logic of multiple agents in the agent layer is specifically defined through the BaseAgent base class. After constructing the first agent base class corresponding to the agent that generates the query statement, the second agent base class corresponding to the agent that executes the query statement, and the third agent base class corresponding to the agent that interprets the result, the state machine, artificial intelligence model calling method, prompt word identifier backend loading mechanism, and reset method corresponding to these three agent base classes are all uniformly defined in the BaseAgent base class.
[0058] Specifically, the unified state machine corresponding to the three intelligent agent base classes mentioned above maintains three core state fields: isRunning, error, and result. The unified AI model invocation method corresponding to these three intelligent agent base classes incorporates the callAI() method, encapsulates HTTP requests, handles timeouts (AbortController), parses multi-format responses, and handles exceptions. In the unified prompt word identifier backend loading mechanism corresponding to these three intelligent agent base classes, the callAI() method does not carry the complete prompt word; it only sends the promptKey identifier (i.e., prompt word identifier) and the user's query content (i.e., natural language query text). The backend service layer loads the corresponding prompt word template based on the promptKey identifier, achieving physical isolation between the prompt word and the frontend code. The unified reset method corresponding to these three intelligent agent base classes is the reset() method, which restores the intelligent agent state (i.e., Agent state) to its initial value and supports workflow retries and reuse.
[0059] The three agents are arranged sequentially according to the workflow of generating the agent based on the query statement, executing the query statement, and interpreting the result. In the current complete processing chain, the output of the previous agent serves as the input of the next agent. Specifically, the output of the query statement generating agent, which is the natural language query text, serves as the input of the query statement executing agent, and the output of the query statement executing agent serves as the input of the result interpreting agent. When the processing of each agent is considered a processing step, the `onStepComplete` callback function is triggered after each step is completed. The input parameters of the `onStepComplete` callback function are the step name (`stepName`) and the step result (`result`). The progress indicator in the front-end page layer updates the interface display in real time based on the output of the `onStepComplete` callback function (e.g., displaying progress indicators such as "SQL being generated," "SQL being executed," and "Result being interpreted"). Furthermore, agents exchange data by sharing the `state.results` object. If any step fails, the workflow is immediately interrupted and an exception is thrown, while retaining the current step information and the results of completed steps for display in the front-end interface layer.
[0060] The query statement generation unit 120 is used to input the natural language query text into the query statement generation agent to generate the current SQL statement.
[0061] In this embodiment, after the natural language query text is input into the query statement generation agent, the query statement generation agent, as the first core agent, is responsible for generating the SQL statement. The process of generating the current SQL statement based on the natural language query text involves interface calls, query statement cleaning, and query statement formatting; the specific processing steps are described later.
[0062] In one embodiment, the query statement generation unit 120 is used for: The system calls a preset artificial intelligence interface, sends the natural language query text and the corresponding prompt word identifier to the artificial intelligence interface, the artificial intelligence interface obtains the prompt word corresponding to the prompt word identifier and the natural language query text to generate the current query statement prompt word, and sends it to the corresponding large language model to execute the process of obtaining the corresponding artificial intelligence model to generate the query statement; Based on a preset cleanup strategy, the Markdown code blocks in the query statement generated by the artificial intelligence model are marked to obtain the cleaned query statement; The cleaned query statement is formatted and formatted using a preset regular expression replacement strategy to obtain the current SQL statement.
[0063] In this embodiment, the prompt word loader is called from the backend service layer. The prompt word loader obtains the prompt word identifier (i.e., promptKey) corresponding to the natural language query text. After the backend service layer obtains the prompt word template from the preset prompt word template library, it is concatenated with the natural language query text to obtain the prompt word of the current query statement. Then, the prompt word of the current query statement is sent to the large language model called by the backend service layer, so as to obtain the query statement generated by the artificial intelligence model.
[0064] The query statements generated by the artificial intelligence model often include Markdown code block markers (such as SQL... etc. Markdown code block markers). Specifically, the Markdown code block markers in the query statements generated by the artificial intelligence model can be cleaned up using a cleanup strategy to obtain cleaned query statements, which are pure SQL text.
[0065] Afterwards, the cleaned query statement can be formatted and formatted according to the regular expression replacement strategy to obtain the current SQL statement. The regular expression replacement strategy mainly processes the cleaned query statement as follows: inserting newlines before keywords such as FROM, WHERE, GROUP BY, ORDER BY, HAVING, and LIMIT; inserting newlines and indentation before the AND / OR connectors in the WHERE clause; inserting newlines and indentation at comma separators in the field list after SELECT; and compressing redundant whitespace characters. After these processing steps, a more readable SQL statement is obtained.
[0066] In one embodiment, obtaining the prompt words for the current query statement based on the prompt word identifier and the corresponding natural language query text includes: Obtain the prompt word template corresponding to the prompt word identifier from the preset prompt word template library; The natural language query text is added to the prompt word template to generate the prompt word for the current query statement.
[0067] In this embodiment, a prompt word loader is invoked from the backend service layer. The prompt word loader obtains the prompt word identifier (i.e., promptKey) corresponding to the natural language query text. The backend service layer then calls the ClickHouse Query API interface to load the corresponding target clickstream data warehouse query initial statement (i.e., the target clickstream data warehouse SQL statement, or ClickHouse SQL). A prompt word template is retrieved from a preset prompt word template library and concatenated with the natural language query text to obtain the current query statement prompt word. This current query statement prompt word is then sent to the large language model invoked through the AI Chat API interface of the backend service layer, thereby obtaining the query statement generated by the artificial intelligence model. Because the preset prompt word template library is deployed at the backend service layer, compared to storing the complete system prompt word template library at the frontend page layer, users cannot modify the prompt words to bypass security restrictions (such as injection attacks, unauthorized queries, etc.), thus improving the security of prompt word management.
[0068] After the query statement generation agent generates the current SQL statement and displays it in the SQL editing area, the user can choose to either not adjust it or manually edit the SQL statement. After adjusting the current SQL statement displayed in the SQL editing area, clicking the "Execute" button will continue to the next step, S130. The manually edited SQL statement is no longer input into the query statement generation agent for secondary SQL statement generation. Instead, it undergoes execution detection and result interpretation by the query statement execution agent, ensuring that the manually modified SQL statement achieves the same processing quality as the SQL statement generated by the AI model. By retaining a human intervention mechanism after the query statement generation agent generates the current SQL statement, compared to existing Text-to-SQL systems where SQL statements are either directly executed or completely rewritten by the user, an intermediate interactive mode of manual fine-tuning is added between AI generation and re-execution. When the user is basically satisfied with the AI-generated SQL statement but needs to fine-tune a certain filtering condition or time range, the fine-tuning is highly efficient and does not require rewriting the SQL statement.
[0069] The query statement execution unit 130 is used to input the current SQL statement into the query statement execution agent to obtain the current query result.
[0070] In this embodiment, after the current SQL statement is input to the query statement execution agent, the query statement execution agent acts as the second core agent responsible for handling SQL statement execution and data querying. The process of executing the current SQL statement and retrieving the current query results from the corresponding database involves interface calls, multi-format parsing, and query statement formatting; the specific processing steps are described later.
[0071] In one embodiment, the query statement execution unit 130 is used to: The current SQL statement is sent to the clickstream data warehouse query interface corresponding to the query statement execution agent and executed accordingly to obtain the current initial query result; Based on a preset multi-format parsing strategy, the current initial query result is parsed in multiple formats to obtain the current response parsing result; The column names and field values of the current response parsing result are automatically extracted and rendered to the front-end table to obtain the current query result.
[0072] In this embodiment, when the query statement execution agent retrieves the current query result from the database corresponding to the data query platform, it first sends the current SQL statement to the clickstream data warehouse query interface (i.e., the query interface of the ClickHouse database) corresponding to the query statement execution agent and executes it accordingly to obtain the current initial query result. Then, the obtained current initial query result is parsed using the multi-format parsing strategy to adapt to the differences in response formats of API interfaces of different backend service layers. After completing the multi-format parsing, all field names are extracted from the first record in the result set corresponding to the current response parsing result as a column array (including multiple column names), and the field values corresponding to all column names are also extracted. This data can then be automatically rendered to the table display area of the front-end interface layer.
[0073] In one embodiment, the step of parsing the current initial query result using a preset multi-format parsing strategy to obtain the current response parsing result includes: Determine whether result_rows[0].data in the current initial query result is an array or whether it contains the first parsed result of the rows field; If it is determined that the first parsing result is result_rows[0].data is an array or contains a rows field, then the current initial query result is parsed using the first response parsing format in the multi-format parsing strategy to obtain the current response parsing result; If it is determined that the first parsing result is not an array and does not contain a rows field, then it is determined whether the result_rows in the current initial query result is the second parsing result of the data array; If it is determined that the second parsing result is a data array, then the current initial query result is parsed using the second response parsing format in the multi-format parsing strategy to obtain the current response parsing result.
[0074] In this embodiment, when parsing the current initial query result using the multi-format parsing strategy, in addition to the first and second response parsing formats listed above, the multi-format parsing strategy also includes a third, fourth, and fifth response parsing formats. Specifically, if it is determined that the second parsing result is not a data array (result_rows), then it is determined whether the data field in the current initial query result is an array or a third parsing result containing a rows field. If it is determined that the third parsing result is that the data field is an array or contains a rows field, then the current initial query result is parsed using the third response parsing format in the multi-format parsing strategy to obtain the current response parsing result. If it is determined that the third parsing result is not a data field but an array and does not contain a rows field, then it is determined whether the top layer in the current initial query result is an array (fourth parsing result). If it is determined that the top layer in the current initial query result is an array, then the current initial query result is parsed using the fourth response parsing format in the multi-format parsing strategy to obtain the current response parsing result. If it is determined that the top-level element in the current initial query result is not an array, then it is determined whether the rows field of the top-level element in the current initial query result is an array (the fifth parsing result). If it is determined that the fifth parsing result is that the rows field of the top-level element in the current initial query result is an array, then the current initial query result is parsed using the fifth response parsing format in the multi-format parsing strategy to obtain the current response parsing result. Generally, through the above five-level degradation parsing strategy, it is possible to adapt to the differences in response formats of API interfaces of different versions of the backend service layer, ensuring that the system remains compatible during the evolution of the backend service layer's API interfaces.
[0075] In practice, the system can also intelligently detect whether the query execution agent is executing the current SQL statement and whether there is a timeout. It can identify timeout errors by scanning keywords (including keywords such as timeout, timeout, timed out) and matching combined patterns (such as the combination of execution time and too long / Maximum). Technical timeout information can be converted into user-friendly prompts such as "Query timeout: The data volume is too large. Please try to shorten the query time range (such as changing to query the most recent 1-3 days) or add more filtering conditions."
[0076] The query result interpretation unit 140 is used to input the current query result into the result interpretation agent to obtain the current natural language interpretation result corresponding to the current query result.
[0077] In this embodiment, the current query result is input to the result interpretation agent. The agent then performs an intelligent interpretation based on the type of the current query result. For example, if the current query result contains an "error" field, it enters error interpretation mode, constructing error analysis prompts including error cause analysis, modification suggestions, and timeout handling suggestions, which are then fed into the large language model to generate an interpretation. If the current query result contains valid query results, it enters normal interpretation mode, extracting the first five lines of data as a summary to construct normal interpretation prompts, which are then fed into the large language model to generate a 1-2 sentence explanation of the business meaning. Therefore, through the above interpretation, the execution result of the entire SQL statement provides a clear and easily understandable natural language interpretation result for the user.
[0078] In one embodiment, the query result interpretation unit 140 is used to: If it is determined that the current query result corresponds to a valid query result, then the current filtered dataset with a preset number of data entries is obtained from the current query result, and the corresponding constructed normal explanation prompt words and the current filtered dataset are sent to the corresponding invoked large language model to obtain the corresponding natural language normal explanation result as the current natural language explanation result; If it is determined that the current query result includes an error field, then based on the corresponding constructed error analysis prompt, the error analysis prompt and the current query result are sent to the corresponding invoked large language model to obtain the corresponding natural language error interpretation result as the current natural language interpretation result.
[0079] In this embodiment, if it is determined that the current query result includes an error field (i.e., includes the error field), it means that the execution of the current SQL statement has not successfully obtained the query result. It is necessary to construct an error analysis prompt word that includes error cause analysis, modification suggestions, and timeout handling suggestions, and send it to the large language model so that the large language model can output a new type of current natural language explanation result that includes artificial intelligence experts explaining the error cause, providing modification suggestions, and suggesting narrowing the query scope for timeout errors.
[0080] If it is determined that the current query result corresponds to a valid query result, it means that the current SQL statement was successfully executed and the query result was obtained. At this time, the current filtered dataset with a preset number of data entries (such as 5 entries, or other values can be flexibly adjusted according to user needs) is extracted from the current query result as a summary, normal explanation prompt words are constructed and sent to the large language model, so that the large language model can output 1-2 sentences explaining the meaning and business value of the data.
[0081] If the current query result is determined to include an error field, a pre-defined error self-healing mechanism can be used for corresponding processing. Specifically, the original error information is first captured and keywords such as "isTimeout" and "TooLongError" are detected. If these keywords are present, it indicates a timeout error, and a user-friendly prompt message can be returned, such as "query timed out," suggesting narrowing the query scope. If these keywords are not present, the original error information is returned directly. Then, an error analysis prompt word containing error cause analysis, modification suggestions, and timeout handling suggestions is constructed through the result interpretation agent and sent to the large language model. This prompt requests the large language model to output a new type of current natural language interpretation result, including explanations of the error cause from an AI expert, modification suggestions, and suggestions to narrow the query scope for timeout errors. After viewing the current natural language interpretation result, the user can manually modify the current SQL statement and, after clicking the execute button, re-execute steps S130-S140 to obtain the query result and interpretation result. By adopting the above-mentioned error self-healing mechanism, compared with the traditional approach of directly displaying the original database error information (such as Code:62. DB::Exception: Syntax error) to the user after the SQL statement execution fails, this application allows users to manually modify and edit the SQL statement after referring to the interpretation results of the natural language representation, thereby improving the error self-healing capability.
[0082] The query result sending unit 150 is used to send the current query result and the current natural language interpretation result to the user terminal.
[0083] In this embodiment, the current query results are displayed in a table area on the front-end page of the data query platform, and the current natural language interpretation results are displayed through an AI interpretation panel. This method enables the current query results and the current natural language interpretation results to be sent to the user terminal for intuitive display. Therefore, this application decouples the three stages of query statement generation, execution, and interpretation into independent intelligent agents, each focusing on a single responsibility, thus improving the system's maintainability and scalability.
[0084] In one embodiment, the natural language data query device 100 based on multi-agent collaboration further includes: A complex query request response unit is used to obtain the complex query natural language text corresponding to the complex query request if a complex query request sent by the user terminal is detected. The complex query result acquisition unit is used to input the complex query natural language text into a pre-deployed autonomous query agent to obtain the corresponding current complex query result.
[0085] In this embodiment, an autonomous query agent (e.g., ReActQueryAgent) can be deployed in the agent layer of the data query platform. This autonomous query agent processes complex queries through Think, Act, and Observe loops. In each loop, the large language model called by the autonomous query agent considers the next action and returns structured decision text (ACTION: QUERY or ACTION: ANSWER). After parsing the structured decision text, the corresponding SQL query is executed or the answer is returned directly. The Think, Act, and Observe loops can be configured with a maximum number of iterations (e.g., 5 times, or other values can be flexibly adjusted according to user needs). When the autonomous query agent reaches the maximum number of iterations, it returns a simplified problem suggestion. The final output query result can be summarized using the summarizeResult method and context is added to avoid passing a large amount of data to the large language model.
[0086] As can be seen, the embodiment of implementing this device can obtain query results and result interpretations from the natural language query text input by the user through the collaboration of three intelligent agents, without requiring the user to write SQL statements, reducing the professional requirements of the user and improving the efficiency of data query result acquisition.
[0087] The aforementioned natural language data query device based on multi-agent collaboration can be implemented as a computer program, which can, for example... Figure 9 It runs on the computer device shown.
[0088] Please see Figure 9 , Figure 9 This is a schematic block diagram of a computer device provided in an embodiment of the present invention. This computer device integrates any of the natural language data query devices based on multi-agent collaboration provided in the embodiments of the present invention.
[0089] See Figure 9 The computer device 400 includes a processor 402, a memory, and a network interface 405 connected via a system bus 401. The memory may include a storage medium 403 and internal memory 404.
[0090] The storage medium 403 may store an operating system 4031 and a computer program 4032. The computer program 4032 includes program instructions that, when executed, cause the processor 402 to perform a natural language data query method based on multi-agent cooperation.
[0091] The processor 402 provides computing and control capabilities to support the operation of the entire computer device.
[0092] The internal memory 404 provides an environment for the computer program 4032 in the storage medium 403 to run. When the computer program 4032 is executed by the processor 402, the processor 402 can execute the above-mentioned natural language data query method based on multi-agent cooperation.
[0093] This network interface 405 is used for network communication with other devices. Those skilled in the art will understand that... Figure 9 The structure shown is merely a block diagram of a portion of the structure related to the present invention and does not constitute a limitation on the computer device to which the present invention is applied. A specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0094] The processor 402 is used to run a computer program 4032 stored in the memory to implement the natural language data query method based on multi-agent cooperation as described above.
[0095] It should be understood that, in this embodiment of the invention, the processor 402 may be a Central Processing Unit (CPU), or it may be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.
[0096] It will be understood by those skilled in the art that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program includes program instructions and can be stored in a storage medium, which is a computer-readable storage medium. The program instructions are executed by at least one processor in the computer system to implement the process steps of the embodiments of the above methods.
[0097] Therefore, the present invention also provides a computer-readable storage medium. This computer-readable storage medium stores a computer program, wherein the computer program includes program instructions. When executed by a processor, the program instructions cause the processor to perform the multi-agent cooperative natural language data query method described above.
[0098] The storage medium can be any computer-readable storage medium that can store program code, such as a USB flash drive, external hard drive, read-only memory (ROM), magnetic disk, or optical disk.
[0099] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0100] In the several embodiments provided by this invention, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative. For example, the division of each unit is merely a logical functional division, and there may be other division methods in actual implementation. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed.
[0101] The steps in the method of this invention can be adjusted, merged, or reduced in order according to actual needs. The units in the device of this invention can be merged, divided, or reduced according to actual needs. Furthermore, the functional units in the various embodiments of this invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0102] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or 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, a terminal, or a network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention.
[0103] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any person skilled in the art can easily conceive of various equivalent modifications or substitutions within the technical scope disclosed in the present invention, and these modifications or substitutions 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 natural language data query method based on multi-agent collaboration, characterized in that, include: In response to a data query command sent by a user terminal, obtain the natural language query text; The natural language query text is input into the query statement generation agent to generate the current SQL statement; The current SQL statement is input into the query statement execution agent to obtain the current query result; The current query result is input into the result interpretation agent to obtain the current natural language interpretation result corresponding to the current query result; The current query result and the current natural language interpretation result are sent to the user terminal.
2. The method according to claim 1, characterized in that, The step of inputting the natural language query text into the query statement generation agent to generate the current SQL statement includes: The system calls a preset artificial intelligence interface, sends the natural language query text and the corresponding prompt word identifier to the artificial intelligence interface, the artificial intelligence interface obtains the prompt word corresponding to the prompt word identifier and the natural language query text to generate the current query statement prompt word, and sends it to the corresponding large language model to execute the process of obtaining the corresponding artificial intelligence model to generate the query statement; Based on a preset cleanup strategy, the Markdown code blocks in the query statement generated by the artificial intelligence model are marked to obtain the cleaned query statement; The cleaned query statement is formatted and formatted using a preset regular expression replacement strategy to obtain the current SQL statement.
3. The method according to claim 2, characterized in that, The step of obtaining the prompt words generated based on the prompt word identifier and the corresponding natural language query text includes: Obtain the prompt word template corresponding to the prompt word identifier from the preset prompt word template library; The natural language query text is added to the prompt word template to generate the prompt word for the current query statement.
4. The method according to claim 1, characterized in that, The step of inputting the current SQL statement into the query statement execution agent to obtain the current query result includes: The current SQL statement is sent to the clickstream data warehouse query interface corresponding to the query statement execution agent and executed accordingly to obtain the current initial query result; Based on a preset multi-format parsing strategy, the current initial query result is parsed in multiple formats to obtain the current response parsing result; The column names and field values of the current response parsing result are automatically extracted and rendered to the front-end table to obtain the current query result.
5. The method according to claim 4, characterized in that, The preset multi-format parsing strategy parses the current initial query result into a multi-format response to obtain the current response parsing result, including: Determine whether result_rows[0].data in the current initial query result is an array or whether it contains the first parsed result of the rows field; If it is determined that the first parsing result is result_rows[0].data is an array or contains a rows field, then the current initial query result is parsed using the first response parsing format in the multi-format parsing strategy to obtain the current response parsing result; If it is determined that the first parsing result is not an array and does not contain a rows field, then it is determined whether the result_rows in the current initial query result is the second parsing result of the data array; If it is determined that the second parsing result is a data array, then the current initial query result is parsed using the second response parsing format in the multi-format parsing strategy to obtain the current response parsing result.
6. The method according to claim 1, characterized in that, The step of inputting the current query result into the result interpretation agent to obtain the current natural language interpretation result corresponding to the current query result includes: If it is determined that the current query result corresponds to a valid query result, then the current filtered dataset with a preset number of data entries is obtained from the current query result, and the corresponding constructed normal explanation prompt words and the current filtered dataset are sent to the corresponding invoked large language model to obtain the corresponding natural language normal explanation result as the current natural language explanation result; If it is determined that the current query result includes an error field, then based on the corresponding constructed error analysis prompt, the error analysis prompt and the current query result are sent to the corresponding invoked large language model to obtain the corresponding natural language error interpretation result as the current natural language interpretation result.
7. The method according to claim 1, characterized in that, Before the step of obtaining the natural language query text in response to a data query command sent by a user terminal, the method further includes: Construct a first intelligent agent base class corresponding to the intelligent agent that generates the query statement, a second intelligent agent base class corresponding to the intelligent agent that executes the query statement, and a third intelligent agent base class corresponding to the intelligent agent that interprets the result; wherein, the first intelligent agent base class, the second intelligent agent base class, and the third intelligent agent base class each include at least a state machine, an artificial intelligence model invocation method, a prompt word identifier backend loading mechanism, and a reset method; The workflow of generating an agent based on the query statement, executing the query statement, and interpreting the result is arranged sequentially to obtain the current complete processing link.
8. A natural language data query device based on multi-agent collaboration, characterized in that, include: The query text acquisition unit is used to acquire natural language query text in response to a data query command sent by the user terminal; The query statement generation unit is used to input the natural language query text into the query statement generation agent to generate the current SQL statement. The query statement execution unit is used to input the current SQL statement into the query statement execution agent to obtain the current query result; The query result interpretation unit is used to input the current query result into the result interpretation agent to obtain the current natural language interpretation result corresponding to the current query result; The query result sending unit is used to send the current query result and the current natural language interpretation result to the user terminal.
9. A computer device, characterized in that, The computer device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, it implements the natural language data query method based on multi-agent cooperation as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, which includes program instructions that, when executed by a processor, can implement the natural language data query method based on multi-agent cooperation as described in any one of claims 1-7.