Database query method based on natural language understanding of large language model
By combining the natural language understanding of the large language model and the domain-specific language, SQL query statements are generated, and the problem of insufficient convenience and accuracy of the large language model in complex database queries is solved, and efficient and accurate natural language to database query conversion is achieved.
Patent Information
- Application Number
- CN202510117545.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-24
- Publication Date
- 2025-05-23
AI Technical Summary
The existing large language models have shortcomings in understanding professional knowledge in the field and completing complex tasks, and the convenience and accuracy of natural languages are insufficient when conducting database queries.
The natural language understanding method based on the large language model is adopted, and the domain-specific language is generated through the clause and single-sentence parsing model, and SQL query statements are generated in combination with logical relationships to realize the conversion from natural language to database query.
It lowers the threshold for non-professional users to conduct complex data queries, improves the efficiency and accuracy of user data retrieval, provides intelligent modules for data analysis, and reduces technical requirements and training costs.
Smart Images

Figure CN120030056A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the field of artificial intelligence technology, and in particular to a database query method based on a large language model. Background Art
[0002] Large Language Models (LLMs) are a type of deep learning model with a large number of parameters. In the field of natural language processing (NLP), they learn language patterns, grammar, and semantics by processing large amounts of text data to understand and generate human language.
[0003] A Domain-Specific Language (DSL) is a programming language or description language that is specifically designed for a specific domain or problem. There are two types of DSLs: internal DSLs and external DSLs. Internal DSLs are built on existing programming languages, and their syntax and features utilize the extended functions of the language. For example, in Ruby, ActiveRecord in the Rails framework is a DSL. External DSLs are completely independent languages with their own syntax and interpreters. External DSLs are usually separated from the core logic of the application and are written and processed separately. XML, HTML, and SQL are all examples of external DSLs. SQL is an external DSL that is used to query and operate databases. When the application scenario of DSL is database query, DSLs like SQL can be used to operate and query database data. It simplifies database interaction through special syntax, so that developers do not need to write cumbersome code to operate the database.
[0004] With the rapid development of big data and artificial intelligence, improving the efficiency of data query and user experience has become an urgent problem to be solved. Although large language models have powerful natural language understanding and generation capabilities, the understanding of domain expertise and the completion of complex tasks are still unresolved problems. Databases have completely different field definitions, associations between tables, and data attributes of different fields in various fields. These domain knowledge cannot be supplemented by pre-training or fine-tuning in traditional solutions. Although Retrieval-Augmented Generation (RAG) can enable the model to dynamically obtain knowledge outside of training through context, the lack of logical reasoning ability of the existing model itself and the complexity of the SQL language itself make it difficult to achieve the high accuracy required by users in complex multi-table linkage query tasks. Summary of the invention
[0005] To sum up, the purpose of the present invention is to propose a database query method based on natural language understanding of a large language model in order to address the unresolved problems of existing large language models in understanding professional knowledge in a field and completing complex tasks, as well as the technical problems of insufficient convenience and accuracy when performing database queries using natural language.
[0006] In order to solve the technical problem proposed by the present invention, the technical solution adopted is: A database query method based on natural language understanding of a large language model, characterized in that the database query method comprises the following steps: (1) First, the logical relationship in the natural language question provided by the client is sorted out through the sentence-by-sentence large language model M1, and the sentence-by-sentence domain-specific language DSL-S1 representing the single sentence and the relationship between the single sentences is generated; (2) Parse the sentence domain-specific language DSL-S1 through the transfer module T1, extract the single sentence and send it to the single sentence parsing large language model M2 in parallel to generate the single sentence domain-specific language DSL-S2; (3) The switching module T1 integrates the single-sentence domain-specific language DSL-S2 according to the logical relationship determined in the clause domain-specific language DSL-S1 to generate the domain-specific language DSL-S3, and sends it to the switching module T2. The switching module T2 parses the subject in DSL-S3, matches it to the predefined SQL table and field through the configuration file, and combines the predicate, object and modifier to form an SQL query statement to execute the database query.
[0007] The technical features that further define the technical solution of the present invention include: The clause domain specific language DSL-S1 includes two types of information: sentences containing only a single subject, predicate, and object modifier, and logical relations between sentences expressed by And and Or.
[0008] The single-sentence domain-specific language DSL-S2 is single-sentence information including a subject, a predicate, an object and a modifier.
[0009] The subject list in the SQL table is manually summarized into a series of mappings from natural language to SQL table and field names based on SQL database fields.
[0010] The training data of the sentence-by-sentence large language model M1 are question-answer pairs from manually annotated original questions to sentence-by-sentence domain-specific language DSL-S1, and the training data of the single-sentence parsing large language model M2 are question-answer pairs from manually annotated sentence-by-sentence domain-specific language DSL-S1 to single-sentence domain-specific language DSL-S2.
[0011] The beneficial effects of the present invention are as follows: the present invention combines the natural language understanding ability of a large language model with a domain-specific language to achieve the conversion of natural language to database query statements, thereby lowering the threshold for non-professional users to perform complex data queries and improving the efficiency and accuracy of user data retrieval. The present invention provides data analysts with an intelligent module for obtaining required data in an SQL database using natural language. This module will greatly reduce the technical requirements for data analysis and the cost of training new employees, while providing an accurate and efficient tool for large language model agents to obtain data from a company's internal SQL database, thereby enabling the model to be empowered with external data and answer real-time questions. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] Figure 1 It is the work flow chart of the present invention. DETAILED DESCRIPTION
[0013] The method of the present invention is further described below in conjunction with the accompanying drawings and specific embodiments of the present invention.
[0014] The present invention discloses a database query method based on natural language understanding of a large language model. In brief, the natural language understanding ability of a large language model is combined with a domain-specific language (DSL) to achieve the conversion of natural language to database query statements, thereby lowering the threshold for non-professional users to perform complex data queries and improving the efficiency and accuracy of user data retrieval. The present invention also provides a reference for the implementation of large language model technology in high-accuracy scenarios in professional fields.
[0015] The present invention defines a domain specific language (DSL) as a medium for a large language model and a database, and uses a large language model to convert a user's natural language description into a DSL. Subsequently, the background system parses the DSL, generates and executes the corresponding SQL query statement, and returns the execution result to the user to implement the query of the database. The DSL consists of a single sentence consisting of a single subject, predicate, and object as a leaf node, and uses two logical relationships of AND and OR to connect the leaf nodes to express natural language questions of multiple subjects, predicates, and objects.
[0016] A large language model is used to convert ordinary natural language expressions into a language DSL for a specific domain that can be processed and understood by computers. The general steps include: first, understanding the core intent of the natural language description. This may include extracting information such as actions, conditions, goals, and objects. Secondly, determining the structure of the DSL. DSL usually has its own set of rules and grammatical structures. It is necessary to select a suitable DSL grammar according to the target domain and map the elements in the natural language to the corresponding DSL grammatical structure; finally, the sentences of the natural language are expressed using DSL rules; the goal of converting the natural language description to DSL is to abstract the intentions, conditions, actions and other information in human language into grammatical expressions for a specific domain so that the computer can perform the corresponding operations. The technical solution of the present invention adopts two large language models, the sentence large language model M1 and the single sentence large language model M2. The training data of the sentence large language model M1 is the question-answer pair from the manually annotated original question sentence to the sentence domain specific language DSL-S1, and the training data of the single sentence parsing large language model M2 is the question-answer pair from the manually annotated sentence domain specific language DSL-S1 to the single sentence domain specific language DSL-S2.
[0017] Reference Figure 1 As shown in, the database query method based on natural language understanding of a large language model disclosed in the present invention has the following specific steps: (1) First, the logical relationship in the natural language question provided by the client is sorted out through the specially fine-tuned sentence-by-sentence large language model M1, and a sentence-by-sentence domain-specific language DSL-S1 is generated to represent single sentences and the relationship between single sentences; the sentence-by-sentence domain-specific language DSL-S1 contains two types of information: sentences containing only a single subject, predicate, and object modifier, and the logical relationship between sentences represented by And and Or. For example, the question, "What cities are located in Hunan Province or Hubei Province when the temperature is greater than 20 degrees today?" can be represented by DSL-S1 as And (the temperature is greater than 20 degrees today, Or (located in Hunan Province, located in Hubei Province)).
[0018] (2) Parse the sentence domain-specific language DSL-S1 through the transfer module T1, and send the single sentence extraction in parallel to another single sentence parsing large language model M2 that is fine-tuned specifically to generate the single sentence domain-specific language DSL-S2; the single sentence domain-specific language DSL-S2 needs to contain the following information: subject, predicate, object and modifier. For example, in the single sentence: "Today's temperature is greater than 20 degrees", the subject is "temperature", the predicate is "greater than", the object is "20", and the modifier is "today". The single sentence: "In Hunan Province", the subject needs the model to infer the implicit meaning of "geographic location", the predicate is "equal to", the object is "Hunan Province", and the modifier is "none". Its subject and predicate must be within the range of the predefined subject list.
[0019] (3) The switching module T1 integrates the single-sentence domain-specific language DSL-S2 according to the logical relationship determined in the clause domain-specific language DSL-S1 to generate the domain-specific language DSL-S3, and sends it to the switching module T2. The switching module T2 parses the subject in DSL-S3, matches it to the predefined SQL table and field through the configuration file, combines the predicate, object and modifier to form an SQL query statement, and executes the database query. The domain-specific language DSL-S3 is a combination DSL of the single-sentence domain-specific language DSL-S2 according to the logical relationship defined in the clause domain-specific language DSL-S1. If there is only one single sentence in the clause domain-specific language DSL-S1, the single-sentence domain-specific language DSL-S2 is the same as the domain-specific language DSL-S3.
[0020] The present invention innovatively adopts the "three-step task decomposition method", that is, firstly, the logical relationship in the question is sorted out through the special fine-tuned sentence large language model M1, and the sentence domain specific language DSL-S1 representing the relationship between single sentences and single sentences is output; then the sentence domain specific language DSL-S1 is parsed through the switching module T1 module, and the single sentence extraction is sent in parallel to another single sentence parsing large language model M2 with targeted fine-tuning to parse these clauses into single sentence domain specific language DSL-S2 by parallel reasoning. Finally, the T1 module integrates the single sentence domain specific language DSL-S2 generated by M2 according to the logical relationship determined in DSL-S1, and forms a domain specific language DSL-S3, which is sent to the switching module T2 module to form the final query SQL statement. This DSL definition and disassembly algorithm effectively reduces the difficulty of a single model to complete the task, and improves the accuracy of the algorithm as a whole to generate SQL query statements and the conversion efficiency of long question queries. Among them, the subject list is manually summarized and summarized as a series of natural language to SQL table and field name mappings based on the SQL database fields. The natural language should be as consistent as possible with the language style of the question description to reduce the difficulty of large model extraction. The function of the transfer module T1 in the figure is to complete the task of converting the original user question and generating the final domain-specific language DSL-S3, and the function of the transfer module T2 is to complete the task of parsing the domain-specific language DSL-S3 into SQL query statements, executing query results, and completing data aggregation and returning it to the user according to the logical relationship definition of the domain-specific language DSL-S3. When parsing the subject in the domain-specific language DSL-S3, the transfer module T2 will match the predefined SQL table and field through the configuration file, and combine the predicate, object and modifier to form an SQL query statement.
[0021] The present invention combines the strong natural language understanding of the large language model, fine-tuning algorithms and custom DSL to achieve accurate conversion of natural language to SQL query statements. At the same time, the mapping of DSL to query statements is completed by traditional code writing to ensure high accuracy and flexibility. In addition, through special data set fine-tuning and task decomposition methods, the present invention further improves the accuracy of the large language model for downstream tasks in the database field, establishes a new practice of reducing model illusions, and provides a powerful tool for big data analysis and retrieval.
[0022] The present invention is applied to natural language query SQL system: in the data analysis scenario within the enterprise, it helps data analysts and product managers to obtain SQL data, effectively filter, and aggregate without knowing the details of the database table and technical details. The present invention can also be used as a large model data acquisition tool that can be called by the large model. When encountering user questions that require additional data to answer, it helps the model to accurately obtain data in the SQL library, and improves the ability of the large language model in real-time data question answering scenarios.
[0023] Social benefits: The application of the present invention will promote the convenience and accuracy of data query, reduce the difficulty of data query for non-professional users, and improve work efficiency. For enterprises and institutions, this means that data analysis can be performed more efficiently and more accurate data support can be provided for decision-making. At the same time, for individual users, it is also easier to obtain the required information and improve the efficiency of information acquisition.
[0024] Expected economic benefits: It is expected that the present invention will generate significant economic benefits in the data management and analysis software market. With the popularization of big data applications, the demand for efficient data query and analysis tools is growing. The efficiency and ease of use of the present invention make it have a wide range of market application prospects. It is expected to form large-scale sales in multiple fields and bring significant economic benefits to enterprises and institutions.
Claims
1. A database query method based on natural language understanding of a large language model, characterized in that The database query method comprises the following steps: (1) First, the logical relationship in the natural language question provided by the client is sorted out through the sentence-by-sentence large language model M1, and the sentence-by-sentence domain-specific language DSL-S1 representing the single sentence and the relationship between the single sentences is generated; (2) Parse the sentence domain-specific language DSL-S1 through the transfer module T1, extract the single sentence and send it to the single sentence parsing large language model M2 in parallel to generate the single sentence domain-specific language DSL-S2; (3) The switching module T1 integrates the single-sentence domain-specific language DSL-S2 according to the logical relationship determined in the clause domain-specific language DSL-S1 to generate the domain-specific language DSL-S3, and sends it to the switching module T2. The switching module T2 parses the subject in DSL-S3, matches it to the predefined SQL table and field through the configuration file, and combines the predicate, object and modifier to form an SQL query statement to execute the database query.
2. The database query method based on large language model natural language understanding according to claim 1, characterized in that: The clause domain specific language DSL-S1 includes two types of information: sentences containing only a single subject, predicate, and object modifier, and logical relations between sentences expressed by And and Or.
3. The database query method based on large language model natural language understanding according to claim 1, characterized in that: The single-sentence domain-specific language DSL-S2 is single-sentence information including a subject, a predicate, an object and a modifier.
4. The database query method based on large language model natural language understanding according to claim 1, characterized in that: The subject list in the SQL table is manually summarized into a series of mappings from natural language to SQL table and field names based on SQL database fields.
5. The database query method based on large language model natural language understanding according to claim 1, characterized in that: The training data of the sentence-by-sentence large language model M1 are question-answer pairs from manually annotated original questions to sentence-by-sentence domain-specific language DSL-S1, and the training data of the single-sentence parsing large language model M2 are question-answer pairs from manually annotated sentence-by-sentence domain-specific language DSL-S1 to single-sentence domain-specific language DSL-S2.