LLM-based complex database query method, apparatus and device, and storage medium
By constructing the minimum adjacency matrix and correlation calculation, the accuracy of the LLM model generating SQL query statements is optimized, and the problem of LLM model generating inaccurate statements in complex database queries is solved, and more efficient natural language database operations and cost savings are achieved.
Patent Information
- Application Number
- CN202411255681.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-09
- Publication Date
- 2024-12-27
AI Technical Summary
When handling complex database queries in the prior art, the LLM model generates SQL query statements inaccurately due to the limited number of tokens and complex table structure, and the association relationship between tables cannot be fully understood, resulting in low query accuracy.
By obtaining database information, building a minimum adjacency matrix set, traversing the table structure to calculate the correlation between the table and the problem, selecting direct and indirect correlation tables, combining the association table sets, constructing a propt prompt word to reduce the number of LLM input tokens, and increasing the SQL query statement accuracy.
It improves the accuracy of natural language generation of SQL query statements, reduces the number of input tokens of LLM, lowers the threshold for users to operate databases, and saves the cost of interacting with LLM.
Smart Images

Figure CN119201971A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of natural language processing, and in particular to a method, device, equipment and storage medium for complex database query based on LLM. Background Art
[0002] As LLM (Large Language Model) such as GPT-4 has made significant progress in natural language understanding and generation, LLM has strong context understanding and generation capabilities, can handle complex natural language queries, and convert natural language into SQL query statements to perform database query operations. However, due to the complex database table structure and the limited number of tokens (the smallest processing unit of text) that LLM can receive, the accuracy of database queries is low. This limitation is mainly manifested in several aspects; first, the complex database table structure means multi-table association analysis and the processing of a large number of fields and records; due to the complex table structure, LLM cannot fully understand the association between all tables, thereby generating inaccurate SQL query statements. In addition, the limited number of tokens processed by LLM means that the amount of knowledge injected into LLM is limited. If the user asks a question and the table structure is too long, it exceeds the maximum injection amount of the LLM model, resulting in incomplete or truncated model knowledge injection. When LLM processes complex queries or complex databases, the above two situations are likely to affect the accuracy of the generated SQL query statements.
[0003] Chinese patent application CN118260327A proposes a natural language processing method, device, equipment, medium and program product; first, the natural language information input by the user is vectorized to generate a first word vector; secondly, the target database model information is vectorized to generate a second word vector; then the first word vector and the second word vector are concatenated to generate an input word vector of the LLM; finally, the LLM generates an SQL query statement based on the input word vector; the above method is currently a common method for implementing a natural language query database, but does not consider the processing of complex databases. For complex databases, first, the target database model information is vectorized to generate a second word vector. The number of tokens in the vector will be very large, and there is a risk of exceeding the maximum number of input tokens of the LLM; secondly, the first word vector and the second word vector are concatenated into an LLM input word vector. For user query problems, the LLM input word vector contains a large amount of invalid redundant information, which interferes with the LLM model and affects the accuracy of generating SQL query statements.
[0004] Chinese patent application CN118093634A proposes a method and system for automatically generating SQL and question responses; it comprehensively applies vector database and knowledge enhancement technology to improve the conversion process from natural language to SQL statements. First, the table building information of business data, including data source, table, column name and remarks, is vectorized and stored in the vector database; secondly, the user query statement is vectorized, and similar table names and fields are retrieved from the vector database; finally, the user query statement and the retrieved table name and field are constructed as a prompt word, input into LLM, and SQL query statements are generated. Although the above method can achieve the purpose of generating SQL query statements from natural language, it is inevitable that tables and fields will be missed when retrieving table names and fields based on user questions, resulting in missing table structure information in the constructed prompt word and errors in the generated SQL query statement. Summary of the invention
[0005] In order to solve at least one technical problem in the prior art, the embodiments of the present invention provide a method, apparatus, device and storage medium for complex database query based on LLM, which can improve the accuracy of SQL query statements generated based on natural language and can greatly reduce the number of input tokens of LLM. In order to achieve the above technical objectives, the technical solution adopted by the embodiments of the present invention is: In a first aspect, an embodiment of the present invention provides a method for complex database query based on LLM, comprising the following steps: Acquire database information, the database information including table structure, index and table association relationship; vectorize the database information to obtain at least a table vector and a table field vector; Traverse all tables in the database, build a minimum adjacency matrix for each table based on the table association relationship of the current table, and obtain a minimum adjacency matrix set; the elements in the minimum adjacency matrix set are represented as the minimum adjacency matrix of the current table and the current table; Receive the user's natural language question, vectorize the natural language question, and obtain a question vector; Traverse all tables in the database, and select tables directly related to the problem according to the correlation between the problem vector and the table vector; obtain a set of directly related tables; Traverse the set of directly associated tables, obtain the minimum adjacency matrix of the table directly associated with the problem from the set of minimum adjacency matrices, obtain the table indirectly associated with the problem, and form a set of indirectly associated tables; Merge the set of directly associated tables and the set of indirectly associated tables to obtain a set of associated tables associated with the problem; Traverse the associated table set associated with the question, select the table field associated with the question according to the degree of association between the question vector and the table field vector, and obtain the table field set associated with the question; Construct a prompt word, wherein the prompt word includes an associated table associated with the question in an associated table set associated with the question, a table field associated with the question in a table field set associated with the question, a user question, and an output information format; interact with the LLM through the prompt word, and the LLM generates an SQL query statement based on at least the table structure and the index.
[0006] Specifically, the table structure includes table name, field name, field type, and constraint conditions.
[0007] Furthermore, selecting a table directly associated with the problem according to the correlation between the problem vector and the table vector includes: calculating the correlation between the problem vector and the table vector, and selecting a table with a correlation greater than a preset threshold k as the table directly associated with the problem.
[0008] Furthermore, 0.5<k<1.
[0009] Furthermore, the obtaining of the table indirectly associated with the question and forming the indirectly associated table set further comprises removing duplicates from the obtained table indirectly associated with the question and then forming the indirectly associated table set.
[0010] Furthermore, selecting the table field associated with the question according to the correlation between the question vector and the table field vector includes: calculating the correlation between the question vector and the table field vector, and selecting the table field with a correlation greater than a preset threshold j as the table field associated with the question.
[0011] Furthermore, 0.5<j<1.
[0012] In a second aspect, an embodiment of the present invention provides a device for complex database query based on LLM, including: A database information processing module is used to obtain database information, wherein the database information includes table structure, index and table association relationship; vectorize the database information to obtain at least a table vector and a table field vector; The minimum adjacency matrix construction module is used to traverse all tables in the database, and based on the table association relationship of the current table, construct the minimum adjacency matrix for each table to obtain a minimum adjacency matrix set; the elements in the minimum adjacency matrix set are represented as the minimum adjacency matrix of the current table and the current table; A user question processing module is used to receive natural language questions from users, vectorize the natural language questions, and obtain question vectors; The directly associated table calculation module is used to traverse all tables in the database, select the table directly associated with the problem according to the degree of association between the problem vector and the table vector, and obtain a set of directly associated tables; The indirect association table calculation module is used to traverse the direct association table set, obtain the minimum adjacency matrix of the table directly associated with the problem from the minimum adjacency matrix set, obtain the table indirectly associated with the problem, and form an indirect association table set; A merging module, used for merging the set of directly associated tables and the set of indirectly associated tables to obtain a set of associated tables associated with the problem; The associated table field calculation module is used to traverse the associated table set associated with the question, select the table field associated with the question according to the degree of association between the question vector and the table field vector, and obtain the table field set associated with the question; The prompt word construction and SQL query statement generation module is used to construct prompt words, wherein the prompt words include an associated table associated with the question in an associated table set associated with the question, a table field associated with the question in a table field set associated with the question, a user question, and an output information format; the prompt word interacts with the LLM, and the LLM generates an SQL query statement based on at least the table structure and index.
[0013] In a third aspect, an embodiment of the present invention provides a device for complex database query based on LLM, including: a memory storing a computer program; The processor is used to run the computer program, and the computer program executes the steps of the method for complex database query based on LLM as described above when running.
[0014] In a fourth aspect, an embodiment of the present invention provides a storage medium, in which a computer program is stored. The computer program is configured to execute the steps of the method for complex database query based on LLM as described above when running.
[0015] The technical solution provided by the embodiment of the present invention has the following beneficial effects: 1) It enables users to operate the database using natural language, lowering the threshold for users to search the database based on SQL query statements.
[0016] 2) For complex databases, it can improve the accuracy of SQL query statements generated from natural language.
[0017] 3) When using online methods such as API to interact with LLM, fees need to be calculated based on tokens; this application can reduce the number of input tokens for LLM, thereby saving costs. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Figure 1 The figure is a schematic diagram of the association relationship between tables in an embodiment of the present invention.
[0019] Figure 2The figure is a flow chart of a method for complex database query based on LLM in an embodiment of the present invention.
[0020] Figure 3 Schematic diagram of a device for complex database query based on LLM in an embodiment of the present invention.
[0021] Figure 4 Schematic diagram of a device for complex database query based on LLM in an embodiment of the present invention. DETAILED DESCRIPTION
[0022] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0023] In the description of the embodiments of the present invention, it should be noted that the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inner", "outer", etc., indicating the orientation or positional relationship, are based on the orientation or positional relationship shown in the drawings, and are only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as limiting the present invention. In addition, the terms "first", "second", and "third" are used for descriptive purposes only and cannot be understood as indicating or implying relative importance.
[0024] In the description of the embodiments of the present invention, it should be noted that, unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or it can be indirectly connected through an intermediate medium, it can also be the internal connection of two components, it can be a wireless connection, or it can be a wired connection. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0025] In addition, the technical features involved in the different embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.
[0026] like Figure 1 As shown, an embodiment of the present invention proposes a method for complex database query based on LLM, comprising the following steps: Step S10, obtaining database information, the database information including table structure, index and table association relationship; vectorizing the database information to obtain at least a table vector and a table field vector; Specifically, the table structure includes table name, field name, field type, constraint conditions, etc.; wherein the constraint conditions refer to the value range and time range of the field; Specifically, the index includes index name, index type, index field, etc.; Specifically, the table association relationship includes foreign key constraints and table connection relationships; In a specific embodiment, an adjacency matrix can be used to represent the association relationship between tables. Figure 2 In one embodiment, there are six tables, namely A, B, C, D, E, and F; the table association relationships of the tables are A->B, A->C, D->E, and E->F; When the database information is vectorized, the index vector and table association vector are also obtained; Step S20, traversing all tables in the database, and constructing a minimum adjacency matrix for each table based on the table association relationship of the current table to obtain a minimum adjacency matrix set; the elements in the minimum adjacency matrix set are represented as the minimum adjacency matrix of the current table and the current table; In a specific embodiment, the obtained minimum adjacency matrix set is recorded as mam, which is a json data structure. In mam, the current table and the minimum adjacency matrix of the current table are represented as key-value pairs, where key is the name of the current table and value is the minimum adjacency matrix of the current table. For example, if there are m tables, m minimum adjacency matrices are constructed, and the number of tables in each minimum adjacency matrix is n, where 0<n<m; for Figure 2 In the example shown, the elements in the obtained minimum adjacency matrix set mam are represented as {A:[B,C]}, {B:[A,C]}, {C:[A,B]}, {D:[E,F]}, {E:[D,F]}, {F:[D,E]}; the first element {A:[B,C]} represents the table A and its minimum adjacency matrix [B,C], and the rest are similar; Step S30, receiving a natural language question from a user, vectorizing the natural language question, and obtaining a question vector; In a specific embodiment, for a received natural language question, it is first converted into text information, then the text information is segmented, and then vectorized; Step S40, traverse all tables in the database, select a table directly related to the question according to the correlation between the question vector and the table vector, and obtain a set of directly related tables; In a specific embodiment, selecting a table directly associated with the problem according to the correlation between the problem vector and the table vector includes: calculating the correlation between the problem vector and the table vector, and selecting a table with a correlation greater than a preset threshold k as the table directly associated with the problem; Preferably, 0.5<k<1; In a specific embodiment, the obtained direct association table set is denoted as dct. Figure 2 In the example shown, for example, dct is [A,B]; Step S50, traversing the set of directly associated tables, obtaining the minimum adjacency matrix of the table directly associated with the problem from the set of minimum adjacency matrices, obtaining the table indirectly associated with the problem, and forming a set of indirectly associated tables; More preferably, the step of obtaining the table indirectly related to the problem and forming the set of indirectly related tables further comprises removing duplicates from the obtained table indirectly related to the problem and then forming the set of indirectly related tables; for Figure 2 In the example shown, assuming that the set of directly associated tables dct is [A, B], then the minimum adjacency matrix of table A obtained from mam is [B, C], and the minimum adjacency matrix of table B is [A, C]. After removing the duplicates of [B, C] and [A, C], we get the table [C] indirectly associated with the problem, forming a set of indirectly associated tables, denoted as ict; Step S60, merging the direct association table set and the indirect association table set to obtain an association table set associated with the question; In the above embodiment, the set of association tables associated with the problem obtained after merging dct and ict is [A, B, C], denoted as ct; Step S70, traversing the associated table set associated with the question, selecting the table field associated with the question according to the degree of association between the question vector and the table field vector, and obtaining the table field set associated with the question; In a specific embodiment, selecting the table field associated with the question according to the correlation between the question vector and the table field vector includes: calculating the correlation between the question vector and the table field vector, and selecting the table field with a correlation greater than a preset threshold j as the table field associated with the question; Preferably, 0.5<j<1; In a specific embodiment, traverse ct, calculate the correlation between the question vector and the table field vector, select the table field with a correlation greater than a preset threshold j, and obtain a set of table fields associated with the question, which is recorded as cf; Step S80, constructing a prompt word, wherein the prompt word includes an associated table associated with the question in an associated table set associated with the question, a table field associated with the question in a table field set associated with the question, a user question, and an output information format; interacting with the LLM through the prompt word, the LLM at least generating an SQL query statement according to the table structure and the index; In a specific embodiment, the prompt word includes an association table associated with the question in ct, a table field associated with the question in cf, a user question, and an output information format; The method for complex database query based on LLM proposed in the embodiment of the present invention is based on the minimum adjacency matrix algorithm, and calculates the tables directly related to the question and the tables indirectly related to the question respectively, and then calculates the table fields related to the question, and constructs prompt words based on this, so that the prompt words include all data tables related to the user's question, exclude data table information that is not related to the user's question, and avoid invalid interference information affecting the LLM model analysis. It can improve the accuracy of generating SQL query statements based on natural language and can greatly reduce the number of input tokens of LLM.
[0027] like Figure 3 As shown, the embodiment of the present invention also proposes a device for complex database query based on LLM, including: A database information processing module is used to obtain database information, wherein the database information includes table structure, index and table association relationship; vectorize the database information to obtain at least a table vector and a table field vector; The minimum adjacency matrix construction module is used to traverse all tables in the database, and based on the table association relationship of the current table, construct the minimum adjacency matrix for each table to obtain a minimum adjacency matrix set; the elements in the minimum adjacency matrix set are represented as the minimum adjacency matrix of the current table and the current table; A user question processing module is used to receive natural language questions from users, vectorize the natural language questions, and obtain question vectors; The directly associated table calculation module is used to traverse all tables in the database, select the table directly associated with the problem according to the degree of association between the problem vector and the table vector, and obtain a set of directly associated tables; The indirect association table calculation module is used to traverse the direct association table set, obtain the minimum adjacency matrix of the table directly associated with the problem from the minimum adjacency matrix set, obtain the table indirectly associated with the problem, and form an indirect association table set; A merging module, used for merging the set of directly associated tables and the set of indirectly associated tables to obtain a set of associated tables associated with the problem; The associated table field calculation module is used to traverse the associated table set associated with the question, select the table field associated with the question according to the degree of association between the question vector and the table field vector, and obtain the table field set associated with the question; The prompt word construction and SQL query statement generation module is used to construct prompt words, wherein the prompt words include an associated table associated with the question in an associated table set associated with the question, a table field associated with the question in a table field set associated with the question, a user question, and an output information format; the prompt word interacts with the LLM, and the LLM generates an SQL query statement based on at least the table structure and index.
[0028] like Figure 3 As shown, an embodiment of the present invention also proposes a device for complex database query based on LLM, including: a processor and a memory; the processor and the memory communicate with each other, for example, they are connected and communicate with each other through a communication bus; a computer program is stored in the memory; the processor is used to run the computer program, and when the computer program runs, the steps of the method for complex database query based on LLM as described above are executed; the processor can be a CPU, or other general-purpose processors, digital signal processors (Digital Signal Processor, DSP), application specific integrated circuits (Application Specific Integrated Circuit, ASIC), field programmable gate arrays (Field-Programmable Gate Array, FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components and other devices, or a combination of the above-mentioned various types of chips, devices or circuits; the memory can include volatile memory, such as random access memory; the memory can also include non-volatile memory, such as flash memory, hard disk or solid-state drive; the memory can also include a combination of the above-mentioned types of memory.
[0029] An embodiment of the present invention further proposes a storage medium, in which a computer program is stored, and the computer program is configured to execute the steps of the method for complex database query based on LLM as described above when running; the storage medium can be a magnetic disk, an optical disk, a read-only memory (ROM), a random access memory (RAM), a flash memory (Flash Memory), a hard disk (HDD) or a solid-state drive (SSD), etc.; the storage medium can also include a combination of the above-mentioned types of memory.
[0030] Finally, it should be noted that the above specific implementation methods are only used to illustrate the technical solution of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the embodiments, those skilled in the art should understand that the technical solution of the present invention can be modified or replaced by equivalents without departing from the spirit and scope of the technical solution of the present invention, which should be included in the scope of the claims of the present invention.
Claims
1. A method for complex database query based on LLM, characterized in that: The following steps are involved: Acquire database information, the database information including table structure, index and table association relationship; vectorize the database information to obtain at least a table vector and a table field vector; Traverse all tables in the database, build a minimum adjacency matrix for each table based on the table association relationship of the current table, and obtain a minimum adjacency matrix set; The elements in the minimum adjacency matrix set are represented as the minimum adjacency matrix of the current table and the current table; Receive the user's natural language question, vectorize the natural language question, and obtain a question vector; Traverse all tables in the database and select the table directly related to the problem based on the correlation between the problem vector and the table vector; Get the set of directly related tables; Traverse the set of directly associated tables, obtain the minimum adjacency matrix of the table directly associated with the problem from the set of minimum adjacency matrices, obtain the table indirectly associated with the problem, and form a set of indirectly associated tables; Merge the set of directly associated tables and the set of indirectly associated tables to obtain a set of associated tables associated with the problem; Traverse the associated table set associated with the question, select the table field associated with the question according to the degree of association between the question vector and the table field vector, and obtain the table field set associated with the question; Construct a prompt word, wherein the prompt word includes an associated table associated with the question in an associated table set associated with the question, a table field associated with the question in a table field set associated with the question, a user question, and an output information format; interact with the LLM through the prompt word, and the LLM generates an SQL query statement based on at least the table structure and the index.
2. The method for complex database query based on LLM as claimed in claim 1, characterized in that: The table structure includes table name, field name, field type, and constraint conditions.
3. The method for complex database query based on LLM as claimed in claim 1, characterized in that: The selecting a table directly associated with the question according to the correlation between the question vector and the table vector includes: calculating the correlation between the question vector and the table vector, and selecting a table with a correlation greater than a preset threshold k as the table directly associated with the question.
4. The method for complex database query based on LLM as claimed in claim 3, characterized in that: 0.5<k<1。 5. The method for complex database query based on LLM as claimed in claim 1, characterized in that: The obtaining of the table indirectly associated with the question and forming the indirectly associated table set further comprises removing duplicates from the obtained table indirectly associated with the question and then forming the indirectly associated table set.
6. The method for complex database query based on LLM as claimed in claim 1, characterized in that: The selecting the table field associated with the question according to the correlation between the question vector and the table field vector includes: calculating the correlation between the question vector and the table field vector, and selecting the table field with a correlation greater than a preset threshold j as the table field associated with the question.
7. The method for complex database query based on LLM as claimed in claim 1, characterized in that: 0.5<j<1。 8. A device for complex database query based on LLM, characterized in that: include: A database information processing module, used to obtain database information, the database information includes table structure, index and table association relationship; Vectorize the database information to at least obtain a table vector and a table field vector; The minimum adjacency matrix construction module is used to traverse all tables in the database, and based on the table association relationship of the current table, construct the minimum adjacency matrix for each table to obtain the minimum adjacency matrix set; The elements in the minimum adjacency matrix set are represented as the minimum adjacency matrix of the current table and the current table; A user question processing module is used to receive natural language questions from users, vectorize the natural language questions, and obtain question vectors; The directly associated table calculation module is used to traverse all tables in the database, select the table directly associated with the problem according to the degree of association between the problem vector and the table vector, and obtain a set of directly associated tables; The indirect association table calculation module is used to traverse the direct association table set, obtain the minimum adjacency matrix of the table directly associated with the problem from the minimum adjacency matrix set, obtain the table indirectly associated with the problem, and form an indirect association table set; A merging module, used for merging the set of directly associated tables and the set of indirectly associated tables to obtain a set of associated tables associated with the problem; The associated table field calculation module is used to traverse the associated table set associated with the question, select the table field associated with the question according to the degree of association between the question vector and the table field vector, and obtain the table field set associated with the question; The prompt word construction and SQL query statement generation module is used to construct prompt words, wherein the prompt words include an associated table associated with the question in an associated table set associated with the question, a table field associated with the question in a table field set associated with the question, a user question, and an output information format; the prompt word interacts with the LLM, and the LLM generates an SQL query statement based on at least the table structure and index.
9. A device for complex database query based on LLM, characterized in that: include: a memory storing a computer program; The processor is used to run the computer program, and when the computer program is run, the steps of the LLM-based complex database query method according to any one of claims 1 to 7 are executed.
10. A storage medium, characterized in that: The storage medium stores a computer program, and the computer program is configured to execute the steps of the LLM-based complex database query method according to any one of claims 1 to 7 when running.
Citation Information
Patent Citations
Method and device for generating SQL statements according to natural languages
CN110888897A
Natural language query method based on deep learning
CN111522839A
Method for automatically generating database query statement based on NLP language model
CN116991869A
Data analysis method and system based on natural language, electronic equipment and storage medium
CN118132587A
System and method for automatic document management
US8996350B1
Cited By
Query statement generation method and device, computer program product and computer readable storage medium
CN122045227A
Non-standard table data structuring and intelligent question and answer method and system based on large language model and medium
CN122195988A
