Database analysis method and device, electronic equipment and computer readable storage medium
By employing read-only queries and local controlled sandboxes in database analysis, the problems of poor reproducibility and low security in traditional database analysis are solved, resulting in safe and reliable database analysis results.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-22
- Publication Date
- 2026-04-10
AI Technical Summary
Existing technologies in scenarios such as finance, operations, manufacturing, and retail suffer from problems such as poor reproducibility, low security, and high compliance risks in traditional database analysis methods. In particular, when large language models are directly applied to the database, intermediate results are difficult to trace, and there is a risk of accidental deletion or modification. Furthermore, the chaotic context management of large models leads to unstable analysis results.
By acquiring database structure information, using the large language model generation tool to call the plan, executing read-only database queries and saving the results as data objects with unique reference numbers, and combining this with local controlled sandbox analysis, we ensure data security and reproducibility. We also use data preview tools to limit the scale of data visible to the large language model and avoid long prompts being truncated.
It achieves security and reproducibility of database analysis, reduces the risk of data leakage, improves the accuracy and interpretability of analysis results, and reduces inference costs and latency.
Smart Images

Figure CN121833774A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of database analysis, in particular to a database analysis method and device, an electronic device and a computer readable storage medium. BACKGROUND
[0002] In financial, operational, manufacturing, retail, Internet of Things and other wide-ranging scenarios, exploratory data analysis is carried out around internal or local databases of an organization, and users need to query, clean, aggregate and repeatedly experiment with visualization and feature construction among multiple tables. The traditional mode relies on a splicing workflow of "manual SQL + script / notebook + temporary file", which has problems such as poor reproducibility, uncontrollable environment, data security and compliance risks, etc.
[0003] At present, some solutions generate SQL or Python code through natural language interaction to directly act on the database, which reduces the technical threshold, but has significant engineering defects: the temporary tables or data views generated in the interaction process are usually randomly named with timestamps, and it is difficult to trace after the analysis session ends, resulting in weak result reproducibility; there is a lack of read-only constraints, and there is a risk of accidental deletion or modification; the model can access full-row-level data, leading to privacy leakage and compliance risks; the prompt words often contain complete data sets, which can easily exceed the context length limit of large models, causing information truncation and reasoning illusion. SUMMARY
[0004] Therefore, the purpose of the present application is to provide a database analysis method, device, electronic device and computer readable storage medium to improve the security, reproducibility and result reliability of database analysis.
[0005] In order to achieve the above-mentioned purpose, the technical solutions adopted by the embodiments of the present application are as follows: In a first aspect, the present application provides a database analysis method, which comprises: After receiving a question raised by a user, obtaining the structure information of a currently available database; sending the question and the structure information of the currently available database to a large language model to make the large language model generate a tool invocation plan; when the tool invocation plan represents a database query operation, invoking a read-only database query tool indicated by the tool invocation plan to perform the database query operation, and saving the database query result as a data reference object with a unique reference number in the case where the database query operation is successfully executed; extracting part of the data from the database query result to form preview data, and sending the preview data and the reference number corresponding to the database query result to the large language model to make the large language model generate a next tool invocation plan; After performing the at least one database query operation, if the latest tool invocation plan obtained represents performing a data analysis operation, performing an analysis operation on at least one database query result in a local controlled sandbox according to a code execution tool indicated by the latest tool invocation plan, and saving an analysis result as a new data reference object in a case where the analysis operation is successfully performed; extracting part of data from the analysis result to form a preview result, and sending the preview result and a reference number corresponding to the analysis result to the large language model, so that the large language model generates a final answer corresponding to the question; obtaining the final answer and returning it to the user.
[0006] In an optional implementation, before executing the read-only database query tool indicated by the tool invocation plan to perform a database query operation, the method further comprises: performing a security check on the SQL statements in the tool invocation plan to filter out unsafe SQL statements.
[0007] In an optional implementation, the extracting part of data from the database query result to form preview data comprises: calling a preset data preview tool to extract part of data from the database query result to form preview data; the extracting part of data from the analysis result to form a preview result comprises: calling a preset data preview tool to extract part of data from the analysis result to form a preview result.
[0008] In an optional implementation, the method further comprises: in a case where the database query operation fails or the analysis operation fails, sending a failure result to the large language model, so that the large language model regenerates a tool invocation plan.
[0009] In an optional implementation, the obtaining of the structure information of the currently available database comprises: calling a preset database structure obtaining tool to obtain the structure information of the currently available database.
[0010] In a second aspect, the present application provides a database analysis device, which comprises: a database structure obtaining module, configured to obtain the structure information of the currently available database after receiving a question raised by a user; The processing module sends the question and the structure information of the currently available database to a large language model to enable the large language model to generate a tool calling plan; when the tool calling plan represents a database query operation, a read-only database query tool indicated by the tool calling plan is called to perform the database query operation, and a database query result is saved as a data reference object with a unique reference number in a case where the database query operation is successfully performed; part of data is extracted from the database query result to form preview data, and the preview data and the reference number corresponding to the database query result are sent to the large language model to enable the large language model to generate a next tool calling plan; after at least one database query operation is performed, if a latest tool calling plan represents a data analysis operation, a code execution tool indicated by the latest tool calling plan is used to perform an analysis operation on at least one database query result in a local controlled sandbox, and an analysis result is saved as a new data reference object in a case where the analysis operation is successfully performed; part of data is extracted from the analysis result to form preview result, and the preview result and the reference number corresponding to the analysis result are sent to the large language model to enable the large language model to generate a final answer corresponding to the question. The sending module is configured to obtain the final answer and return the final answer to the user.
[0011] In an optional implementation, the processing module is further configured to perform a security check on an SQL statement in the tool calling plan before the read-only database query tool indicated by the tool calling plan is called to perform the database query operation, to filter out unsafe SQL statements.
[0012] In an optional implementation, the processing module is configured to call a preset data preview tool to extract part of data from the database query result to form the preview data, and call the preset data preview tool to extract part of data from the analysis result to form the preview result.
[0013] In a third aspect, the present application provides an electronic device, which comprises a processor, a memory, and a computer program stored in the memory and executable on the processor, and the computer program, when executed by the processor, implements the steps of the database analysis method according to any one of the preceding embodiments.
[0014] In a fourth aspect, the present application provides a computer readable storage medium, which stores a computer program, and the computer program, when executed by a processor, implements the steps of the database analysis method according to any one of the preceding embodiments.
[0015] The database analysis method and device, electronic equipment and computer readable storage medium provided by the embodiment of the present application, the method comprises: after receiving a question raised by a user, obtaining the structure information of the currently available database; sending the question and the structure information of the currently available database to a large language model to enable the large language model to generate a tool calling plan; when the tool calling plan represents a database query operation, calling a read-only database query tool indicated by the tool calling plan to execute the database query operation, and saving the database query result as a data reference object with a unique reference number in the case of successful execution of the database query operation; extracting part of the data from the database query result to form preview data, and sending the preview data and the reference number corresponding to the database query result to the large language model to enable the large language model to generate a next tool calling plan; after executing at least one database query operation, if the latest tool calling plan represents a data analysis operation, executing an analysis operation on at least one database query result in a local controlled sandbox according to a code execution tool indicated by the latest tool calling plan, and saving the analysis result as a new data reference object in the case of successful execution of the analysis operation; extracting part of the data from the analysis result to form a preview result, and sending the preview result and the reference number corresponding to the analysis result to the large language model to enable the large language model to generate a final answer corresponding to the question; obtaining the final answer and returning it to the user. In terms of security, the large language model in the present application can only see the database structure and a small amount of preview data, and the complete data set is saved locally; all database queries are executed through a read-only tool to prevent miswriting and misdeletion; the analysis code runs in a local controlled sandbox to ensure the safety of the computing environment; in terms of reproducibility, the intermediate results generated by each query or analysis are saved with a unique reference number, so that the intermediate results can be stably reused across steps, ensuring the playback ability of the process. In terms of result reliability, the prompt is constructed based on the database structure and a small amount of preview data, which significantly reduces the token occupation, avoids the omission and illusion caused by the silent truncation of long prompts, improves the reasoning focus, accuracy and explainability, and effectively reduces the delay and reasoning cost.
[0016] In order to make the above-mentioned purposes, features and advantages of the present application more obvious and easy to understand, the following preferred embodiments are described in detail below, and the accompanying drawings are described as follows. BRIEF DESCRIPTION OF DRAWINGS
[0017] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the drawings needed to be used in the embodiments. It should be understood that the following drawings only show some embodiments of the present application, and therefore should not be regarded as a limitation on the scope. For those skilled in the art, other related drawings can also be obtained without creative labor on the basis of these drawings.
[0018] Figure 1A flow chart of the database analysis method provided by the embodiment of the present application is shown; Figure 2 A diagram showing the content returned by the Agent to the LLM after executing the SQL statement is shown; Figure 3 A functional module diagram of the database analysis apparatus provided by the embodiment of the present application is shown; Figure 4 A block diagram of the electronic device provided by the embodiment of the present application is shown.
[0019] Icon: 100-electronic device; 110-memory; 120-processor; 130-communication module; 600-database analysis apparatus; 610-database structure acquisition module; 620-processing module; 630-sending module. DETAILED DESCRIPTION
[0020] The technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, but not all the embodiments. The components of the embodiments of the present application described and shown in the accompanying drawings can be arranged and designed in various different configurations.
[0021] Therefore, the detailed description of the embodiments of the present application provided in the accompanying drawings below is not intended to limit the scope of the claimed present application, but only represents selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present application.
[0022] It should be noted that the relational terms such as "first" and "second" and the like are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that these entities or operations have any such actual relationship or order. Moreover, the terms "include", "contain" or any other variants thereof are intended to cover non-exclusive inclusion, so that the processes, methods, articles or devices including a series of elements not only include those elements, but also include other elements not explicitly listed or inherent to such processes, methods, articles or devices. Without more limitations, the element defined by the statement "including a" does not exclude the presence of another identical element in the process, method, article or device including the element.
[0023] Existing solutions usually generate SQL or arbitrary Python code through dialogue, directly act on the database, and gradually improve the analysis process in multiple rounds of interaction. The intermediate results produced are named with timestamps and stored separately, lacking stable intermediate data reference mechanisms and systematic product associations. These solutions have the following problems: no read-only constraints are set, which may lead to accidental deletion or modification of data; intermediate results cannot be stably referenced across steps, resulting in poor reproducibility of the analysis process; the Python execution environment is not controlled, posing a security risk. At the same time, the principle of "minimum data visibility and local non-transmission" is not followed, and a large amount of row-level detailed data is often spliced into the prompt, which not only brings privacy leakage and compliance risks, but also significantly increases bandwidth consumption and large model inference cost. In addition, due to the lack of effective management of the context length limit on large models, long prompts are prone to context overflow or silent truncation, leading to missing fields, incorrect aggregation, or illusory explanations, ultimately making the analysis results unstable and difficult to trace and hold accountable.
[0024] Based on this, the embodiment of the present application provides a database analysis method, device, electronic equipment and computer readable storage medium, the method comprises: after receiving the question proposed by the user, the structure information of the currently available database is obtained; the question and the structure information of the currently available database are sent to the large language model to make the large language model generate tool calling plan; when the tool calling plan represents the execution of the database query operation, the read-only database query tool indicated by the tool calling plan is called to execute the database query operation, and the database query result is saved as a data reference object with a unique reference number in the case of successful execution of the database query operation; part of the data in the database query result is extracted to form preview data, and the preview data and the reference number corresponding to the database query result are sent to the large language model to make the large language model generate the next tool calling plan; after executing at least one database query operation, if the latest tool calling plan represents the execution of the data analysis operation, the code execution tool indicated by the latest tool calling plan is used to execute the analysis operation on at least one database query result in the local controlled sandbox, and the analysis result is saved as a new data reference object in the case of successful execution of the analysis operation; part of the data in the analysis result is extracted to form preview result, and the preview result and the reference number corresponding to the analysis result are sent to the large language model to make the large language model generate the final answer corresponding to the question; the final answer is obtained and returned to the user. In terms of security, the large language model in the present application can only see the database structure and a small amount of preview data, and the complete data set is saved locally; all database queries are executed through read-only tools to prevent miswriting and misdeletion; the analysis code runs in the local controlled sandbox to ensure the safety of the computing environment; in terms of reproducibility, the intermediate results generated by each query or analysis are saved with a unique reference number, so that the intermediate results can be stably reused across steps, ensuring the process playback capability. In terms of result reliability, the prompt is constructed based on the database structure and a small amount of preview data, which significantly reduces the token occupation, avoids the omission and illusion caused by the silent truncation of long prompts, improves the reasoning focus, accuracy and explainability, and effectively reduces the delay and reasoning cost.
[0025] In the following, the embodiments of the present application will be described in detail with reference to the accompanying drawings.
[0026] Please refer to Figure 1 , a flowchart of the database analysis method provided by the embodiment of the present application. It should be noted that the database analysis method of the present application does not necessarily Figure 1and the specific sequence below is limited. It should be understood that in other embodiments, the order of some steps of the database analysis method of the present application can be exchanged according to actual needs, or some steps can be omitted or deleted. The database analysis method can be executed by an agent installed on an electronic device, which is a task executor based on an LLM (Large Language Model). Figure 1 The specific process shown in the figure will be described in detail below.
[0027] Step S101, after receiving the question raised by the user, the structure information of the currently available database is obtained.
[0028] In this embodiment, the agent provides the following tools (a set of restricted capabilities called by the agent): run_sql (a read-only database query tool), run_python (a code execution tool for executing python code for data analysis and report generation), show (a data preview tool), and get_db_schema (a database structure acquisition tool).
[0029] The user first raises a natural language question to the agent, for example: "Which products have the fastest sales growth in the last three months?" The agent can obtain the structure information of the currently available database by calling the preset database structure acquisition tool, for example, the agent calls get_db_schema to obtain the structure information of the currently available database, which includes which tables are in the database, which fields are in each table, and the like.
[0030] Step S102, the question and the structure information of the currently available database are sent to the large language model to make the large language model generate a tool call plan.
[0031] In this embodiment, after obtaining the question and the structure information of the database, the agent initiates a small context containing only the question and the structure information of the database to the LLM, and requests the LLM to complete intent analysis and step planning. The LLM does not directly execute any database query or local calculation, but outputs a tool call plan. The tool call plan can include the tools that the agent needs to call, the SQL statements or Python code that need to be executed, and the like.
[0032] Step S103, when the tool call plan represents a database query operation, the read-only database query tool indicated by the tool call plan is called to execute the database query operation, and the database query result is saved as a data reference object with a unique reference number in the case of successful execution of the database query operation.
[0033] In this embodiment, after the Agent receives the tool call plan returned by the LLM, it finds that the tool call plan includes a read-only database query tool (run_sql) and a SQL statement that needs to be executed, determines that a database query operation needs to be performed, and then calls run_sql to execute the SQL statement to perform a database query operation. In the case where the database query operation is successfully executed, the database query result is saved as a data reference object with a unique reference number. For example, the intermediate result (database query result) generated by the first call to the tool is saved as a local variable and named Out[1].
[0034] Step S104: Extract part of the data from the database query result to form preview data, and send the preview data and the reference number corresponding to the database query result to the large language model, so that the large language model generates the next tool call plan.
[0035] In this embodiment, the Agent can call a preset data preview tool (show) to extract part of the data from the database query result to form preview data, and implement previewing of the data corresponding to Out[1] in a limited number of rows, which facilitates manual or model verification and strictly limits the data size visible to the LLM. In one example, the content returned by the Agent to the LLM can refer to Figure 2 , mainly including execution status, preview data, and reference number, etc.
[0036] That is to say, the Agent does not feed back the complete database query result to the LLM, but extracts part of the data therefrom, for example, only a few rows of data (such as 3 rows) are taken out for desensitization filtering processing (such as hiding the specific numerical value of the amount or replacing it with an asterisk), to form preview data returned to the LLM, while telling the LLM that the complete database query result can be referenced using the variable Out[1], so that the LLM can generate Python code for subsequent calculation, analysis, and processing using this variable.
[0037] It can be understood that the Agent can perform multiple database query operations, and after each execution of a database query operation, the reference number Out[n] corresponding to the database query result and the preview data are fed back to the LLM in the above-mentioned manner, so that the LLM determines whether to continue to perform a database query operation next. When the LLM determines that all the data to be obtained has been obtained, it will plan the next step of performing analysis and processing of the data.
[0038] Step S105: After executing at least one database query operation, if the latest tool call plan indicates the execution of a data analysis operation, then the code execution tool indicated by the latest tool call plan performs an analysis operation on at least one database query result in a local controlled sandbox, and saves the analysis result as a new data reference object if the analysis operation is successfully executed.
[0039] In this embodiment, after the Agent performs at least one database query operation, the LLM determines the next data analysis operation to be performed and generates a corresponding tool invocation plan, which is returned to the Agent. This tool invocation plan includes a code execution tool (run_python) and the Python code to be executed. The Agent calls run_python to execute the Python code in a local controlled sandbox, performing analysis operations on at least one database query result, including data cleaning, transformation, and visualization.
[0040] In this embodiment, the local controlled sandbox can pre-configure commonly used libraries such as pandas, numpy, and matplotlib, and can also pre-configure some functions, such as store(df), to store the calculation results for later use. For example, if the analysis operation is executed successfully, the Agent can use the store(df) function to generate a new Out[n] from the analysis results returned by the local controlled sandbox and save it.
[0041] Step S106: Extract some data from the analysis results to form a preview result, and send the preview result and the citation number corresponding to the analysis result to the large language model so that the large language model can generate the final answer to the question.
[0042] In this embodiment, the Agent can call a preset data preview tool (show) to extract partial data from the analysis results to form a preview result. This facilitates manual or model verification while strictly limiting the scale of data visible to the LLM. After receiving the preview result returned by the Agent and the citation number corresponding to the analysis result, the LLM determines that the current analysis task has been completed and no further processing is required. It can then generate a summary answer (the final answer to the question) and return it to the Agent.
[0043] Step S107: Obtain the final answer and return it to the user.
[0044] In this embodiment, after receiving the final answer output by the large language model, the Agent returns the final answer to the user.
[0045] It can be seen that in terms of security, the large language model can only see the database structure and a small amount of preview data, and the complete data set is saved locally; all database queries are executed through a read-only tool to prevent accidental writing and deletion; the analysis code runs in a local controlled sandbox to ensure the safety of the computing environment; in terms of reproducibility, the intermediate results generated by each query or analysis are saved with a unique reference number, allowing stable reuse of intermediate results across steps and ensuring process playback capability. In terms of result reliability, the database structure and a small amount of preview data are used to construct prompts, significantly reducing token occupation, avoiding omissions and illusions caused by silent truncation of long prompts, improving reasoning focus, accuracy and explainability, and effectively reducing delay and reasoning cost. In this way, the problems of high data security risk, non-reproducible analysis results, chaotic context management, and lack of controllability in existing database analysis methods based on large language models are effectively solved.
[0046] In an embodiment, before the Agent calls the read-only database query tool indicated by the tool call plan to execute the database query operation, the database analysis method can further include: Performing security checks on the SQL statements in the tool call plan to filter out unsafe SQL statements.
[0047] That is, after the Agent obtains the tool call plan given by the LLM, it does not blindly execute, but first checks the security, such as whether it contains dangerous statements such as DELETE and UPDATE, and whether it tries to access tables outside the permissions. After filtering out unsafe SQL statements, the database query operation is started.
[0048] In an embodiment, the database analysis method can further include: In the case of failure of the database query operation or the analysis operation, the execution failure result is sent to the large language model to make the large language model regenerate the tool call plan.
[0049] That is, after the Agent executes the corresponding operation according to the tool call plan output by the LLM, whether it is successful or not, it will feedback the execution result to the LLM. When the Agent fails to execute the database query operation or the analysis operation, the LLM will regenerate the tool call plan based on the received execution failure result, so that the Agent executes the next round of database query operation or analysis operation.
[0050] In an embodiment, for each database analysis task, a corresponding task directory is created, and a series of intermediate results (such as Out[1], Out[2], Out[3], etc.), output products (charts, files, etc.), and execution logs generated by the Agent during task execution are saved in the task directory. In this way, the reproducibility, traceability, and auditability of the analysis process can be ensured.
[0051] It can be understood that in the present embodiment, the relationship between the Agent and the LLM can be summarized as the division of "planner— executor and supervisor". The LLM is responsible for understanding the requirements, proposing the order of tool calls and parameter suggestions under the premise of "minimum visibility"; the Agent is responsible for the actual execution and compliance supervision. This relationship ensures that the LLM does not directly access the full data, and it only sees the database structure information and the controlled sample or aggregated summary after being trimmed; similarly, the database query and local execution do not directly trust the LLM string, but the Agent constructs a secure query and sandbox execution environment locally.
[0052] After receiving the tool call plan, the Agent performs security checks, and then calls the restricted tools for execution by the Agent itself. The intermediate results generated by the execution are registered as local variables, such as Out[n], and the Agent calls the show tool to preview the intermediate results and feeds back to the LLM, promoting the next round of more detailed planning. In this way, a closed-loop mechanism of "LLM gives a verifiable plan → Agent audits and executes → promotes the next step of planning with minimum visibility feedback" can be formed, effectively limiting the visibility range of the LLM.
[0053] In the present embodiment, the database access layer of the Agent can be compatible with SQLite, PostgreSQL, MySQL, DuckDB, etc. In actual application, the database backend can be replaced by a cloud database or an object storage query engine (such as external tables, Parquet, Delta Lake, etc.), but the read-only strategy and Out[n] data reference protocol still need to be adhered to. The computing framework relied on by run_python can switch or coexist with pandas, polars, pyarrow, or an embedded SQL engine (such as DuckDB), provided that the behavior constraints of the local controlled sandbox and automatic saving of intermediate products are maintained. If named datasets or versioned handles are used instead of Out[n], the stability and replayability across steps also need to be ensured.
[0054] In summary, the database analysis method proposed in the embodiments of the present application is a general database exploratory analysis method with "restricted tools, data reference protocol, read-only database access, local controlled sandbox, and unified task directory" as the core, which realizes exploratory analysis driven by LLM under the premise that local data is not exported. LLM completes reasoning through database structure information and controlled examples, completely eliminates direct exposure of complete data sets, and fundamentally reduces security and compliance risks. At the same time, around the fixed limitation of the context length of the large language model, the prompt is constructed with the database structure and a small amount of preview data, which significantly reduces the token occupation, avoids the omission and illusion caused by the silent truncation of long prompts, improves the reasoning focus, accuracy and interpretability, and effectively reduces the delay and reasoning cost. Out[n] makes the intermediate results stable and reusable across steps, combined with the solidification products of the task directory and the local controlled sandbox execution, which realizes high reproducibility and auditability. Read-only access, parameterized query and concurrent read strategy improve the overall robustness and efficiency; standardized progress record and error classification make problem positioning more direct; multi-backend compatibility and local deployment reduce the migration threshold and operation cost.
[0055] In order to perform the corresponding steps in the above embodiments and various possible manners, an implementation manner of a database analysis apparatus is given below. Please refer to Figure 3 A functional module diagram of the database analysis apparatus 600 provided by the embodiments of the present application is given. It should be noted that the database analysis apparatus 600 provided by the present embodiment has the same basic principles and technical effects as the above embodiments, and for brief description, the part not mentioned in the present embodiment can be referred to the corresponding content in the above embodiments. The database analysis apparatus 600 comprises a database structure acquisition module 610, a processing module 620 and a sending module 630.
[0056] The database structure acquisition module 610 is configured to acquire the structure information of the currently available database after receiving the question raised by the user.
[0057] It can be understood that the database structure acquisition module 610 can perform the above step S101.
[0058] The processing module 620 sends the question and the structure information of the currently available database to the large language model to make the large language model generate a tool invocation plan; when the tool invocation plan represents a database query operation, the read-only database query tool indicated by the tool invocation plan is invoked to perform the database query operation, and the database query result is saved as a data reference object with a unique reference number in the case of successful execution of the database query operation; the preview data is formed by extracting part of the data from the database query result, and the preview data and the reference number corresponding to the database query result are sent to the large language model to make the large language model generate the next tool invocation plan; after performing at least one database query operation, if the latest tool invocation plan represents a data analysis operation, the code execution tool indicated by the latest tool invocation plan is used to perform an analysis operation on at least one database query result in a local controlled sandbox, and the analysis result is saved as a new data reference object in the case of successful execution of the analysis operation; the preview result is formed by extracting part of the data from the analysis result, and the preview result and the reference number corresponding to the analysis result are sent to the large language model to make the large language model generate the final answer corresponding to the question.
[0059] It can be understood that the processing module 620 can perform the above step S102.
[0060] The sending module 630 is configured to obtain the final answer and return it to the user.
[0061] It can be understood that the sending module 630 can perform the above step S103.
[0062] Optionally, the database structure acquisition module 610 is specifically configured to invoke a preset database structure acquisition tool to acquire the structure information of the currently available database.
[0063] Optionally, the processing module 620 is further configured to perform a security check on the SQL statement in the tool invocation plan before the read-only database query tool indicated by the tool invocation plan is invoked to perform the database query operation, so as to filter out unsafe SQL statements.
[0064] Optionally, the processing module 620 is configured to invoke a preset data preview tool to extract part of the data from the database query result to form the preview data, and invoke the preset data preview tool to extract part of the data from the analysis result to form the preview result.
[0065] Optionally, the processing module 620 is further configured to send the execution failure result to the large language model in the case of failure of the database query operation or the analysis operation, so that the large language model re-generates the tool invocation plan.
[0066] Please refer to Figure 4This is a block diagram of an electronic device 100 provided in an embodiment of the present invention. The electronic device 100 includes a memory 110, a processor 120, and a communication module 130. The memory 110, processor 120, and communication module 130 are electrically connected to each other directly or indirectly to realize data transmission or interaction. For example, these components can be electrically connected to each other through one or more communication buses or signal lines.
[0067] The memory 110 is used to store programs or data. The memory 110 may be, but is not limited to, random access memory (RAM), read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), etc.
[0068] The processor 120 is used to read / write data or programs stored in the memory 110 and to perform corresponding functions. For example, when a computer program stored in the memory 110 is executed by the processor 120, the database analysis method disclosed in the above embodiments can be implemented.
[0069] The communication module 130 is used to establish a communication connection between the electronic device 100 and other devices via a network, and to send and receive data via the network.
[0070] It should be understood that, Figure 4 The structure shown is only a schematic diagram of the electronic device 100. The electronic device 100 may also include components that are larger than... Figure 4 The more or fewer components shown, or having the same Figure 4 The different configurations shown. Figure 4 The components shown can be implemented using hardware, software, or a combination thereof.
[0071] This invention also provides a computer-readable storage medium storing a computer program thereon, which, when executed by processor 120, implements the database analysis method disclosed in the above embodiments.
[0072] In several embodiments provided in the present application, it should be understood that the disclosed apparatus and method can also be implemented by other manners. The apparatus embodiments described above are merely illustrative, for example, the flowcharts and block diagrams in the drawings show the possible implementation architecture, function and operation of the apparatus, method and computer program product according to the embodiments of the present application. In this regard, each block in the flowcharts or block diagrams can represent a module, a program segment or a part of code, which contains one or more executable instructions for implementing the specified logic function. It should also be noted that in some alternative implementations, the functions noted in the blocks can occur in different orders from those noted in the drawings. For example, two consecutive blocks can actually be executed substantially in parallel, and they can also be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and the combination of blocks in the block diagrams and / or flowcharts, can be implemented by a dedicated hardware-based system for executing the specified functions or actions, or can be implemented by a combination of dedicated hardware and computer instructions.
[0073] In addition, each functional module in the embodiments of the present application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0074] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer readable storage medium. Based on this understanding, the technical solutions of the present application can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in the embodiments of the present application. The aforementioned storage medium includes various media that can store program codes, such as a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0075] The above only describes the preferred embodiments of the present application and is not intended to limit the present application. For those skilled in the art, the present application can have various modifications and changes. Any modification, equivalent replacement, improvement, etc. made within the spirit and principles of the present application shall be included in the protection scope of the present application.
Claims
1. A database analysis method, characterized in that, The method includes: After receiving a question from a user, obtain the structure information of the currently available database; The problem and the structure information of the currently available database are sent to the large language model so that the large language model generation tool can invoke the plan. When the tool invocation plan represents the execution of a database query operation, the read-only database query tool indicated by the tool invocation plan is invoked to execute the database query operation. If the database query operation is successfully executed, the database query result is saved as a data reference object with a unique reference number. A portion of data is extracted from the database query results to form preview data. The preview data and the reference number corresponding to the database query results are sent to the large language model so that the large language model can generate the next tool call plan. After executing at least one database query operation, if the latest tool invocation plan indicates the execution of a data analysis operation, then the code execution tool indicated by the latest tool invocation plan performs an analysis operation on at least one database query result in a local controlled sandbox, and if the analysis operation is successfully executed, the analysis result is saved as a new data reference object; A portion of the data is extracted from the analysis results to form a preview result. The preview result and the citation number corresponding to the analysis result are sent to the large language model so that the large language model can generate the final answer to the question. Obtain the final answer and return it to the user.
2. The database analysis method according to claim 1, characterized in that, Before invoking the read-only database query tool indicated by the tool invocation plan to perform a database query operation, the method further includes: The SQL statements in the tool's call plan are subjected to security checks to filter out unsafe SQL statements.
3. The database analysis method according to claim 1, characterized in that, The step of extracting partial data from the database query results to form preview data includes: A preset data preview tool is invoked to extract a portion of the data from the database query results to form preview data; The step of extracting partial data from the analysis results to form a preview result includes: A preset data preview tool is invoked to extract a portion of the data from the analysis results and generate a preview result.
4. The database analysis method according to claim 1, characterized in that, The method further includes: In the event that the database query operation or the analysis operation fails, the failure result is sent to the large language model so that the large language model can regenerate the tool invocation plan.
5. The database analysis method according to any one of claims 1-4, characterized in that, The step of obtaining the structure information of the currently available database includes: Use the preset database structure retrieval tool to obtain the structure information of the currently available databases.
6. A database analysis device, characterized in that, The device includes: The database structure acquisition module is used to obtain the structure information of the currently available databases after receiving a question from a user. The processing module sends the question and the structure information of the currently available database to a large language model, enabling the large language model to generate a tool invocation plan. When the tool invocation plan indicates the execution of a database query operation, it invokes a read-only database query tool indicated by the tool invocation plan to execute the database query operation. If the database query operation is successful, the database query result is saved as a data reference object with a unique reference number. A portion of the data is extracted from the database query result to form preview data, and the preview data and the reference number corresponding to the database query result are sent to the large language model, enabling the large language model to generate the next tool invocation plan. After executing at least one database query operation, if the latest tool invocation plan indicates the execution of a data analysis operation, the code execution tool indicated by the latest tool invocation plan performs an analysis operation on at least one database query result in a local controlled sandbox, and if the analysis operation is successful, the analysis result is saved as a new data reference object. A portion of the data is extracted from the analysis result to form a preview result, and the preview result and the reference number corresponding to the analysis result are sent to the large language model, enabling the large language model to generate the final answer to the question. The sending module is used to obtain the final answer and return it to the user.
7. The database analysis apparatus according to claim 6, characterized in that, The processing module is also used to perform a security check on the SQL statements in the tool invocation plan before executing a database query operation by the read-only database query tool indicated by the tool invocation plan, so as to filter out unsafe SQL statements.
8. The database analysis apparatus according to claim 6, characterized in that, The processing module is used to call a preset data preview tool to extract some data from the database query results to form preview data; and to call a preset data preview tool to extract some data from the analysis results to form preview results.
9. An electronic device, characterized in that, It includes a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the steps of the database analysis method as described in any one of claims 1-5.
10. A computer-readable storage medium, characterized in that, A computer program is stored on the computer-readable storage medium, which, when executed by a processor, implements the steps of the database analysis method as described in any one of claims 1-5.