Database query and visualization method and system based on large language model
By employing a database query and visualization method based on a large language model, end-to-end automation from natural language questions to database queries is achieved, solving the problems of insufficient flexibility and security in existing technologies and providing an efficient and reliable enterprise-level database query solution.
Patent Information
- Application Number
- CN202511949031.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-23
- Publication Date
- 2026-01-23
AI Technical Summary
Existing technologies lack flexibility and semantic understanding when transforming natural language problems into database queries, making it difficult to achieve end-to-end automation. They also lack reliability and security, especially in sensitive data environments, and existing solutions are difficult to meet the needs of enterprise-level private deployments.
By adopting a large language model-based approach, the system achieves end-to-end automation from natural language problems to database queries through a complete process of intent recognition, table column information formatting, structured query statement generation and optimization, query execution and result return. It combines the OmniSQL model and the general large language model, and improves the system's robustness through asynchronous session mechanisms and error correction processes.
It enables efficient and reliable automation of natural language to database queries in enterprise-level private environments, improving query success rate and compatibility, ensuring data security and controllability, and lowering the technical threshold.
Smart Images

Figure CN121387948A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of data analysis and natural language processing, and in particular to a database query and visualization method, system and server cluster system based on a large language model. BACKGROUND
[0002] Current technologies for converting natural language questions into database queries mainly exist in two paths: one is a template filling method based on fixed rules, which has limited flexibility and semantic understanding ability; the other is an end-to-end conversion relying on a single model (such as a general large language model or a special Text-to-SQL model), whose generation quality is restricted by the model's own ability.
[0003] The above existing technical solutions all lack a complete, stable and collaborative optimization automatic processing flow. Neither the template method nor the single model method can systematically solve the whole-link problems from intent understanding, query generation to syntax adaptation and execution, resulting in the entire query process being easily interrupted or erroneous at some link, and unable to reliably realize the end-to-end automation of "natural language input to query result return". Especially in financial, government, medical and other enterprise-level private environments involving sensitive data, database queries face double challenges: on the one hand, business personnel have difficulty in efficiently obtaining information using complex SQL language; on the other hand, due to data security and compliance requirements, raw data must be prevented from being leaked, and all query behaviors must be controllable and auditable. Existing natural language-to-query solutions are mostly focused on public clouds or general scenarios, lacking systematic design for private deployment, sensitive data flow control and deep integration with enterprise permission systems, and are difficult to meet the application requirements of high-security-level environments. SUMMARY
[0004] The technical problem to be solved by the embodiments of the present application is to provide a database query and visualization method based on a large language model, which reduces the technical threshold of database query and improves efficiency and success rate under the premise of ensuring the closed-loop circulation of sensitive data in the internal network.
[0005] To solve the above technical problems, the embodiments of the present application provide a database query and visualization method based on a large language model, comprising: A natural language question receiving step: receiving a natural language question from a terminal device; An intent recognition and table selection step: determining at least one target data table from a connected database based on the natural language question; A table column information formatting step: obtaining column information of the target data table and formatting it into a preset unified format; The first version of the structured query statement generation step: generating a first version of the structured query statement according to the formatted column information and the natural language question; The second version of the structured query statement optimization generation step: performing field verification and database syntax compatibility conversion on the first version of the structured query statement to generate a second version of the structured query statement; The query execution and result return step: executing the second version of the structured query statement and returning the query result. Thus, end-to-end automated data query is realized.
[0006] Further, the first version of the structured query statement generation step specifically includes: a formatting sub-step of formatting the column information of the target data table into a preset format consistent with the OmniSQL model training data; a first prompt word construction sub-step of constructing a first prompt word template containing the formatted column information and the natural language question; an OmniSQL model calling sub-step of inputting the first prompt word template into the OmniSQL model; and a first version of the statement extraction sub-step of extracting the first version of the structured query statement based on the SQLite syntax from the output of the OmniSQL model. Thus, the generation quality and logical traceability of the initial SQL are ensured.
[0007] Further, the second version of the structured query statement optimization generation step specifically includes: a second prompt word construction sub-step of constructing a second prompt word template containing the formatted column information, the natural language question, and the first version of the structured query statement; a general large language model calling and verification sub-step of inputting the second prompt word template into the general large language model to perform field existence and table connection legality verification on the first version of the structured query statement; and a syntax conversion and generation sub-step of converting the syntax of the verified statement from the SQLite syntax to the syntax of the target database type through the general large language model to generate the second version of the structured query statement. Thus, the compatibility, robustness, and execution success rate of the generated SQL are improved.
[0008] Further, after the second version of the structured query statement optimization generation step and before the query execution and result return step, there is further included: a query statement parsing step of calling the general large language model to parse the second version of the structured query statement according to a preset framework to generate a structured interpretation text containing a query object, a filtering condition, and grouping and sorting information; and a parsing result sending step of sending the structured interpretation text to the terminal device for display.
[0009] Further, the intent recognition and table selection step specifically includes: The vectorization processing substep uses the fine-tuned Embedding model to vectorize the natural language question and the preconfigured candidate table description text in the database; the similarity calculation substep calculates the similarity scores between the natural language question vector and each candidate table description vector; and the target table selection substep selects at least one candidate table as the target data table according to the similarity scores from high to low. In this way, the accuracy and anti-interference capability of intent recognition are improved.
[0010] Further, the training samples used for fine-tuning include a sample set composed of positive sample pairs and difficult negative sample pairs, and the difficult negative sample pairs are constructed by screening candidate tables by combining a sparse retrieval algorithm and domain rules. In this way, the accuracy of the general vector model in a similar table scenario is higher.
[0011] Further, the query execution and result returning step executes the second version of the structured query language statement through an asynchronous session mechanism, and converts the original result data after successful execution into a dictionary list format and returns it.
[0012] Further, the query execution and result returning step further includes an error reporting and repairing sub-process: an error information capturing sub-step that captures error information returned by the database when the second version of the structured query language statement fails to execute; a repair prompt word constructing sub-step that combines the error information, the formatted column information and the second version of the structured query language statement into a repair prompt word; and a repair statement generating and retrying sub-step that calls the general large language model to generate a repaired query statement and re-executes it.
[0013] Correspondingly, the embodiment of the application also provides a database query and visualization system based on a large language model, which comprises a database for storing at least one data table; and a server in communication connection with the database, wherein the server is configured to receive a natural language question from a terminal device, call an intent recognition module, determine at least one target data table from the database based on the natural language question, and obtain column information thereof, call a first SQL generation module comprising an OmniSQL model to generate a first version of a SQLite syntax query statement according to the column information and the natural language question, call a second SQL generation and optimization module comprising a general large language model to verify and convert the syntax of the first version of the SQLite syntax query statement and generate a second version of a target database syntax query statement, call a query execution engine to execute the second version of the target database syntax query statement, and return the result to the terminal device.
[0014] Further, the present application also provides a server cluster system for database query and visualization, comprising: a database server for storing data tables; an application server in communication connection with the database server, deployed with an OmniSQL model, a general large language model and a business logic program, for executing the method according to any one of 1 to 10; and a visualization server in private deployment, in communication connection with the application server, deployed with a model context protocol service and a chart generation tool, for receiving the standardized chart parameters sent by the application server and generating a visualization chart.
[0015] The embodiment of the present application proposes a database query and visualization method based on a large language model, first receives a natural language question of a user, determines a target data table from a database through a precise matching method based on vectorization processing. Then, the system formats the obtained table column information, and generates a preliminary SQL statement with explainability by using a special text-to-SQL model. Next, the general large language model is called to verify the field existence and logical legality of the statement, and convert it into the syntax of the target database to form an optimized executable SQL. In this process, the system can automatically generate a structured explanation of the SQL statement to assist the user in understanding. The asynchronous mechanism is adopted in the query execution stage to improve the efficiency, and the error diagnosis and repair process based on the large language model is automatically triggered when an error occurs. Because the complete technical chain from intent recognition, staged SQL generation and optimization to automated execution and repair is adopted, the technical problems of low accuracy, poor compatibility, logic opacity and fragile and easily interrupted execution link in the natural language query conversion process are overcome, and the technical effect of realizing high-reliable, high-compatible and user-understandable end-to-end automated database query is achieved. BRIEF DESCRIPTION OF DRAWINGS
[0016] Fig. 1 is the overall flowchart of the database query and visualization method based on a large language model of an embodiment of the present application.
[0017] Fig. 2 is a system architecture schematic diagram of the database query and visualization system based on a large language model of an embodiment of the present application.
[0018] Fig. 3 is a system architecture schematic diagram of the server cluster system of an embodiment of the present application. DETAILED DESCRIPTION
[0019] It should be noted that the embodiments in the present application and the features in the embodiments can be combined with each other without conflict, and the present application will be further described in detail below in combination with the drawings and specific embodiments.
[0020] The directionality indication (such as up, down, left, right, front, back, etc.) in the embodiments of the present application is only used to explain the relative positional relationship, motion condition, etc. between components in a certain specific posture (as shown in the drawings), and if the specific posture changes, the directionality indication also changes accordingly.
[0021] In addition, the description of "first", "second" and the like in the present application is only for the purpose of description, and cannot be understood as indicating or implying the relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first", "second" can be explicitly or implicitly included at least one of the features.
[0022] Embodiment one: database query method based on large language model Please refer to Figs. 1-3 The present embodiment provides a most basic implementation framework. The method runs on one or more application servers, which are in communication connection with a database server storing business data, and receives requests from user terminal devices (such as personal computers, mobile devices). The method includes the following steps: S101 natural language question receiving step: the server receives the natural language question sent from the user terminal device through the Web service interface or API deployed by it, for example, "query the product with the highest sales in the last month".
[0023] S102 intent recognition and table selection step: the server calls the intent recognition module. The module first performs preliminary semantic understanding on the user question, and then filters out the candidate data table set based on the pre-configured table permissions and business domains in the database. In a basic implementation, the initial table selection can be completed by matching the problem keywords and table names, table annotations.
[0024] S103 table column information formatting step: the server accesses the selected target data table through the database connection driver to obtain its column information (including field name, data type, whether it can be empty, etc.). Then, the information is converted into a predetermined, structured text format, for example: "table name (field1 data type, field2 data type, …)".
[0025] S104 first version of structured query statement generation step: the server splices the formatted column information and the user's original question to form a prompt (Prompt) and inputs it into a model dedicated to text-to-SQL (for example, a Fine-tuned GPT model) to generate the first version of SQL query statement. The statement may be based on a certain general database syntax.
[0026] S105 Second edition structured query language optimization generation step. Considering that the first edition SQL may have syntax errors or be incompatible with the target database, the server organizes the first edition SQL, column information, and problems again into new prompts, inputs them into a general large language model (such as GPT-4, ChatGLM, etc.), requests it to perform syntax checking, optimization, and conversion to a syntax compatible with the backend database (such as MySQL), and outputs the second edition optimized SQL statement.
[0027] S106 Query execution and result return step: the server executes the second edition SQL statement through its database query engine (such as SQLAlchemy, JDBC, etc.). After successful execution, the query result set is encapsulated as structured data (such as JSON format) and returned to the user terminal device through the API for display.
[0028] The beneficial effects of the embodiment are that by connecting the intent recognition, SQL generation and optimization, query execution, and other steps, a complete automated query closed loop is constructed, thereby solving the problem of low efficiency and high threshold of traditional manual SQL writing, and realizing the basic ability to directly operate the database with natural language.
[0029] Embodiment two: two-step SQL generation and parsing containing OmniSQL and general LLM This embodiment further refines the specific technical solutions of SQL generation, optimization, and interpretation based on embodiment one, specifically including: S104 (refinement): First edition structured query language generation step: this step uses a proprietary OmniSQL model as the core. First, the first prompt word construction sub-step is performed: construct a templated prompt, for example: "You are a SQL expert. Given the database table structure as follows: {formatted column information}. Please generate the corresponding SQL query statement according to the following question: '{natural language question}'. Please first give your thinking process (Chain-of-Thought), and then output the SQL." Where {} is a placeholder. Then execute the OmniSQL model calling sub-step: input the constructed prompt into the locally deployed OmniSQL-7B model. This model is trained based on a million SQL samples and is good at generating SQL containing logical reasoning chains (CoT). Finally, the first edition statement extraction sub-step is performed: parse the model output and extract the SQLite syntax statement starting with the SELECT keyword as the first edition SQL.
[0030] S105 (refinement): Second edition structured query statement optimization generation step. This step uses a general LLM for finishing. First, a second prompt word construction sub-step is performed: build an optimization prompt word, for example: "Existing table structure: {column information}, user question: '{question}', the first edition SQL (SQLite syntax) has been generated: {first edition SQL}. Please complete: 1. Verify whether all fields and table names in the SQL exist in the above table structure; 2. Convert the SQL to a syntax compatible with MySQL 8.0, especially pay attention to the conversion of date functions (such as DATE()); 3. Simplify and optimize the SQL. Please directly output the final executable MySQL SQL." Then execute the general large language model calling and checking sub-step and the syntax conversion and generation sub-step: input the prompt word into the general large language model (such as through API calling cloud GPT-4), and the model directly outputs the second edition MySQL SQL after completing field verification and syntax conversion.
[0031] New step S107: Query statement parsing step: After S105, call the general large language model to parse the second edition SQL, and the prompt word is: "Please parse the following SQL: {second edition SQL}. Output according to the following structure: query object: [explain the fields and aggregation logic of the query]; filter condition: [explain the WHERE and HAVING clauses]; grouping and sorting: [explain GROUP BY, ORDER BY]; table association: [explain JOIN conditions]." The model outputs a structured explanation text.
[0032] New step S108: Parsing result sending step: return the structured explanation text generated by S107 to the terminal device together with the query result of S106. The user interface can display both the data table and the SQL interpretation, reducing the understanding threshold.
[0033] The beneficial effects of this embodiment are that the powerful CoT reasoning capability of the OmniSQL model is used to generate a logically clear first edition SQL, thereby solving the problem of opaque logic generated by black box models; then a general LLM is used for accurate cross-database syntax conversion and verification, thereby overcoming the compatibility bottleneck of the OmniSQL model supporting only SQLite; finally, through automatic SQL parsing, the understanding and trust of non-technical users for complex queries are improved.
[0034] Embodiment three: Precise intent recognition and table selection based on fine-tuned embedding model This embodiment is based on embodiment one or two and describes in detail the core technology implementation of the intent recognition and table selection step.
[0035] S102 (refinement): Intent recognition and table selection step: This step uses a domain fine-tuned embedding (Embedding) model to achieve precise matching.
[0036] Hardware and data preparation: Deploy open-source Chinese text embedding model such as BGE-large-zh-v1.5 on the application server. Collect natural language query logs and the corresponding data tables of the final successful queries in the history period of the enterprise to form the original sample.
[0037] Training sample construction: For each query (Query), the correct table (Table) of the query is the positive sample (Pos). The construction of hard negative sample (HardNegative) is the key: first, use sparse retrieval algorithms such as BM25 to retrieve non-correct tables with high matching degree of query text word frequency from all candidate tables; second, combine domain rules, such as selecting tables that belong to the same business module and have similar field names but different business meanings (such as "user order table" vs. "user refund table"); combine the two to select 1-3 tables with the highest confusion as hard negative samples (Neg). Thus, high-quality (Query, Pos, Neg1, Neg2, …) training triplets or multi-triplets are constructed.
[0038] Model fine-tuning: Use the contrastive learning (ContrastiveLearning) objective function, such as InfoNCELoss, to fine-tune the BGE-large-zh model on the collected training data. The goal is to pull the vectors of the query and its correct table description closer in the vector space, while pushing the vectors of the query and the hard negative sample table description further apart.
[0039] Online service: When the online application is executed, the vectorization processing sub-step is performed: using the fine-tuned Embedding model, the user's natural language problem and the description of all candidate tables (such as table name + table annotation) are converted into high-dimensional vectors. The similarity calculation sub-step is performed: calculate the cosine similarity score of the problem vector and each table description vector. The target table selection sub-step is performed: sort the scores in descending order, select the top table with the highest score, or the top N tables that exceed the preset threshold, as the target data table.
[0040] The vectorization processing and similarity calculation are performed on the basis that the user has passed identity authentication and preliminary permission verification. The system uses the pre-configured user-data table access permission list as a pre-filter, only performs vector matching and selection on candidate tables that the user has access to, ensuring data security isolation from the query source.
[0041] The beneficial effects of the present embodiment are that by introducing field-specific difficult samples to fine-tune the general embedding model, the model can deeply understand the subtle differences between business terminologies, and can accurately distinguish in scenarios with similar table structures and similar names, thereby improving the accuracy of intent recognition on complex test sets by about 15%, solving the core pain point of inaccurate table selection of general models in professional scenarios.
[0042] Embodiment Four: SQL Execution Optimization Scheme with Fault Tolerance and Interaction Capability The present embodiment is based on Embodiments One, Two or Three, and elaborates on the efficient mechanism of SQL execution, error repair and user interactive modification, which is a "double optimization scheme".
[0043] S106 (Refinement): Query Execution and Result Return Step. This step uses an asynchronous session mechanism. The application server uses an asynchronous database driver (such as asyncpg for PostgreSQL, aiomysql for MySQL) to execute SQL queries in a separate asynchronous I / O event loop, thereby avoiding synchronous query blocking of the main thread and improving the throughput and response speed of the server in high-concurrency scenarios. After the query result is returned by the database driver in the native record format, the server program automatically converts it to a Python dictionary list or JavaMap list, which is easy to serialize to JSON format, and then returns it to the front end. S106a: Error Repair Sub-process. When SQL execution fails in S106, this process is triggered.
[0044] 1. Error Information Capture Sub-step: Capture specific error information returned by the database engine, such as "(1054, "Unknown column 'product_name' in 'fieldlist'")".
[0045] 2. Repair Prompt Word Construction Sub-step: Assemble the repair prompt word: "SQL execution error, error information: {error information}. The relevant table structure is: {formatted column information}. The error SQL is: {second edition SQL}. Please analyze the error reason and generate the corrected SQL that can be directly executed." 3. Repair Statement Generation and Retry Sub-step: Call the general large language model (such as GPT-4) to generate the repaired SQL according to the prompt word. The system re-sends the repair SQL to S106 for execution. The maximum number of retries (such as 3 times) can be set. At the same time, the error information and repair process are recorded to the log database as training data for subsequent optimization of SQL generation prompt words. S109: On-demand Modification Sub-process. When the user sees the query result and SQL analysis in the terminal interface, they can make a modification request (such as "sort by sales in descending order").
[0046] 4. Modification request receiving step: the front end sends the user's modification description text to the server.
[0047] 5. Modification prompt word construction step: the server constructs the modification prompt word: "table structure: {column information}. The original query requirement is: '{original natural language question}'. The current SQL is: {current SQL}. The user's new requirement is: '{modification request}'. Please modify directly based on the current SQL and output a new and complete SQL." 6. Modification statement generation and jump step: call the general large language model to generate the modified SQL. The system then jumps to the S106 query execution and result return step to execute the new SQL and return the result without re-executing the entire process from S101.
[0048] The beneficial effects of the embodiment are that the system performance is improved through asynchronous execution; the robustness of the system is significantly enhanced through the automatic error repair cycle, reducing the query failure rate caused by simple syntax or field errors; through the context-based quick modification process, the efficiency and flexibility of human-computer interaction are greatly improved, allowing users to iteratively refine data queries like conversations, and the overall query success rate is improved. Using the asynchronous session mechanism to execute queries not only improves concurrency performance, but more importantly, provides an independent execution sandbox for complex queries that run for a long time, avoiding malicious or abnormal queries that occupy core database connections for a long time, and ensuring the overall stability and availability of the database system.
[0049] Embodiment five: LLM-driven and private deployment visualization presentation scheme This embodiment is based on any of the above embodiments and details the complete technical chain of automatic, secure and visual query results.
[0050] After successfully obtaining the query result (denoted as result_data) in S106, the following steps are performed: S110: Visualization parameter generation step. The application server calls the general large language model. The prompt word is designed as: "The user's question is: '{natural language question}'. The data structure returned by the query is as follows: {result_data sample}. Please select the most suitable chart type from 'line chart', 'column chart', 'pie chart', and 'table' according to the user's question intent and data characteristics, and generate a standard option configuration object that meets the requirements of ECharts or ApacheECharts library. Please ensure that the time, numerical value, and other fields in the data are correctly mapped and formatted." The LLM outputs a standardized chart parameter configuration object (JSON format) based on the question semantics (such as "trend" corresponding to a line chart and "proportion" corresponding to a pie chart) and data structure.
[0051] S111: Chart parameter sending step. A Model Context Protocol (MCP) client is deployed on the application server. The client sends the standardized chart parameters generated in S110 to a private deployment visualization service (VIS service) through the streamable_http protocol. The VIS service is deployed on a separate visualization server or a server connected with the object storage, and is isolated from the Internet.
[0052] S112: Visualization result returning step. The private VIS service internally integrates a chart rendering engine (such as a Node.js-based ECharts rendering service). After receiving the parameters, it generates a chart picture (PNG format) or an interactive chart HTML fragment on the server side, stores the file in the private object storage of the company, and generates a private URL accessible in the internal network. The URL is returned to the application server through the MCP protocol, and finally returned to the user terminal device for display by the application server.
[0053] The embodiment has the beneficial effects that the LLM dynamically understands the query intent and generates visualization parameters, thereby completely eliminating the dependence on preset static templates and realizing intelligent adaptation of visualization forms; by privately deploying the MCP service and the chart generation service, it is ensured that all query data and generated chart files are circulated and stored in the internal network, thereby completely avoiding the data privacy leakage risk that may be caused by using third-party public chart services, and meeting the strict requirements of enterprise-level applications on data security.
[0054] Embodiment six: system and server cluster architecture embodiment This embodiment describes the implementation of the application from the hardware system and architecture. A database query and visualization system based on a large language model includes: Database: a MySQL relational database server is used to store business data tables.
[0055] Server (application server): one or more Linux physical machines or virtual machines are used, and a Python runtime environment is deployed thereon. The server is connected with the database server through a local area network. The program modules running on the server include: Intention recognition module: the fine-tuned BGE embedding model in embodiment three is loaded.
[0056] First SQL generation module: the OmniSQL-7B model deployed locally is encapsulated.
[0057] Second SQL generation and optimization module: integrated with API client accessing cloud general large language model (such as ChatGPT).
[0058] Query execution engine: an asynchronous query service developed based on aiomysql and SQLAlchemy libraries.
[0059] MCP client: used for communication with visualization service.
[0060] The server is configured to perform any one of the method processes described in embodiments one to five. A preferred server cluster system adopts a distributed and decoupled architecture: Database server: a dedicated high-performance database server responsible for data storage.
[0061] Application server cluster: a group of stateless application servers (such as containers managed using K8s) carrying all the above NL2SQL related logic. They receive user requests through a load balancer and access the database server to execute queries.
[0062] Visualization server: a server or service cluster deployed independently and privately. The MCP server (receiving chart requests) and chart generation service (such as ECharts rendering service) are deployed on it. The server is configured with an internal network object storage. The application server cluster sends chart parameters to the visualization server through an internal network dedicated line. The visualization server generates charts and stores them in the internal network object storage, and returns the internal network URL. The entire visualization process does not touch the public network.
[0063] To achieve the security requirement that sensitive data does not leave the enterprise internal network at all, the core components of this solution, including the OmniSQL model, general large language model, and visualization chart generation service, are deployed privately within the enterprise firewall. All data processing, SQL generation, and chart rendering processes are completed in the internal server cluster, which is physically isolated from the public network environment, completely eliminating the risk of data leakage.
[0064] The beneficial effects of this embodiment are that by combining software and hardware in a clear description, the algorithm process is solidified into specific system components and server architecture, effectively avoiding the risk of being identified as "pure algorithm rules"; through the distributed and private deployment design of the server cluster, the high availability and high concurrency of the system are realized, and the safety and compliance of the entire data processing process are strictly guaranteed.
[0065] In addition, those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by programs instructing relevant hardware, and the programs can be stored in a computer readable storage medium. When the programs are executed, the processes of the above-mentioned embodiment methods can be included. The storage medium can be a magnetic disc, an optical disc, a read-only memory (ROM) or a random access memory (RAM), etc. In addition, part of the present application can be applied as a computer program product, for example, computer program instructions, when executed by a computer, through the operation of the computer, the method and / or technical solutions according to the present application can be called or provided. The program instructions for calling the method of the present application can be stored in a fixed or removable recording medium, and / or transmitted through a data stream in a broadcast or other signal bearing medium, and / or stored in the working memory of the computer device running according to the program instructions. Here, according to one embodiment of the present application, the device includes a memory for storing computer program instructions and a processor for executing program instructions, wherein when the computer program instructions are executed by the processor, the device is triggered to run the method and / or technical solutions based on the above-mentioned embodiments according to the present application.
[0066] Although the embodiments of the present application have been shown and described, those skilled in the art can understand that various changes, modifications, replacements and variations can be made to the embodiments without departing from the principles and spirits of the present application, and the scope of the present application is defined by the appended claims and their equivalent ranges.
Claims
1. A database query and visualization method based on a large language model, applied to a server, and having the characteristics that, The method comprises the following steps: a natural language question receiving step: receiving a natural language question from a terminal device; an intent recognition and table selection step: determining at least one target data table from a connected database based on the natural language question; a table column information formatting step: obtaining column information of the target data table and formatting it into a preset uniform format; a first version of structured query statement generation step: generating a first version of structured query statement according to the formatted column information and the natural language question; a second version of structured query statement optimization generation step: performing field verification and database syntax compatibility conversion on the first version of structured query statement to generate a second version of structured query statement; a query execution and result returning step: executing the second version of structured query statement and returning the query result.
2. The method of claim 1, wherein, The first version of structured query statement generation step specifically comprises: a formatting sub-step of formatting the column information of the target data table into a preset format consistent with OmniSQL model training data; a first prompt word construction sub-step of constructing a first prompt word template containing the formatted column information and the natural language question; an OmniSQL model calling sub-step of inputting the first prompt word template into an OmniSQL model; a first version of statement extraction sub-step of extracting the first version of structured query statement based on SQLite syntax from the output of the OmniSQL model.
3. The method of claim 2, wherein, The second version of structured query statement optimization generation step specifically comprises: a second prompt word construction sub-step of constructing a second prompt word template containing the formatted column information, the natural language question, and the first version of structured query statement; a general large language model calling and verification sub-step of inputting the second prompt word template into a general large language model to perform field existence and table connection legality verification on the first version of structured query statement; a syntax conversion and generation sub-step of converting the syntax of the verified statement from SQLite syntax to the syntax of the target database type through the general large language model to generate the second version of structured query statement.
4. The method of claim 3, wherein, After the second version of structured query statement optimization generation step and before the query execution and result returning step, the method further comprises: a query statement parsing step of calling the general large language model to parse the second version of structured query statement according to a preset framework to generate a structured interpretation text containing query objects, filtering conditions, and grouping and sorting information; a parsed result sending step of sending the structured interpretation text to the terminal device for display.
5. The method of claim 1, wherein, The intent recognition and table selection step specifically comprises: a vectorization processing sub-step of using a fine-tuned Embedding model to vectorize the natural language question and preconfigured candidate table description texts in the database; a similarity calculation sub-step of calculating similarity scores between the natural language question vector and each candidate table description vector; a target table selection sub-step of selecting at least one candidate table as the target data table according to the similarity scores from high to low.
6. The method of claim 5, wherein, The training samples used in the fine-tuning include a sample set composed of positive sample pairs and difficult negative sample pairs, the difficult negative sample pairs being constructed by screening from candidate tables in combination with a sparse retrieval algorithm and domain rules.
7. The method of claim 1, wherein, The query execution and result returning step executes the second version of the structured query language statement through an asynchronous session mechanism and returns the original result data after successful execution in the form of a dictionary list.
8. The method of claim 7, wherein, The query execution and result returning step further includes an error reporting and repair sub-process: An error information capturing sub-step captures error information returned by the database when the execution of the second version of the structured query language statement fails; A repair prompt word constructing sub-step combines the error information, the formatted column information and the second version of the structured query language statement into a repair prompt word; A repair statement generating and retrying sub-step calls the general large language model to generate a repaired query statement and re-executes it. 9.A database query and visualization system based on large language models, characterized in that, It comprises: a database for storing at least one data table; a server in communication connection with the database, the server is configured to receive a natural language question from a terminal device, call an intent recognition module to determine at least one target data table from the database based on the natural language question and obtain its column information, call a first SQL generation module, the first SQL generation module comprising an OmniSQL model, for generating a first version of a SQLite syntax query statement according to the column information and the natural language question, call a second SQL generation and optimization module, the second SQL generation and optimization module comprising a general large language model, for verifying and converting the syntax of the first version of the SQLite syntax query statement to generate a second version of a target database syntax query statement, call a query execution engine to execute the second version of the target database syntax query statement and return the result to the terminal device.
10. A server cluster system for database query and visualization, comprising: It comprises: a database server for storing data tables; an application server in communication connection with the database server, deployed with an OmniSQL model, a general large language model and a business logic program, for executing the method of any one of 1 to 8; a visualization server privately deployed in communication connection with the application server, deployed with a model context protocol service and a chart generation tool, for receiving standardized chart parameters sent by the application server and generating a visual chart.
Citation Information
Patent Citations
Automatic NL2SQL data set construction method and system based on large language model
CN120631915A
SQL statement generation system
CN120929481A
Data analysis system and method based on artificial intelligence
CN121092559A
Enterprise number asking system and method based on combination of large language model and NL2SQL
CN121166718A