Database query method and apparatus, electronic device, and non-volatile storage medium
By constructing a knowledge graph to represent the database structure and combining it with a large language model, the problem of poor SQL statement accuracy caused by the large amount of database field information and complex relationships is solved, and more efficient and accurate SQL generation is achieved.
Patent Information
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- CHINA TELECOM ARTIFICIAL INTELLIGENCE TECHNOLOGY (BEIJING) CO LTD
- Filing Date
- 2025-09-08
- Publication Date
- 2026-06-04
AI Technical Summary
In existing technologies, the accuracy of SQL statements generated based on large language models is poor, mainly due to the large number of database fields and their complex relationships.
By constructing a knowledge graph to represent tables, fields, and relationships in the database, and combining it with a large language model, the knowledge graph provides rich contextual information and relationships to generate SQL statements.
It improves the accuracy and reliability of SQL generation, and enhances the efficiency and accuracy of large models when processing complex database queries.
Smart Images

Figure CN2025119588_04062026_PF_FP_ABST
Abstract
Description
Database query methods, devices, electronic equipment, and non-volatile storage media
[0001] Related applications
[0002] This application claims priority to Chinese patent application filed on September 12, 2024, application number 202411280879X, entitled "Database query method, apparatus, electronic device and non-volatile storage medium", the entire contents of which are incorporated herein by reference. Technical Field
[0003] This application relates to the field of artificial intelligence technology, and more specifically, to a database query method, apparatus, electronic device, and non-volatile storage medium. Background Technology
[0004] Generative large language models are a powerful and popular technique in the field of natural language processing today. Based on massive amounts of data, deep attention networks based on Transformers, and reinforcement learning based on human feedback, generative large language models can learn rich latent semantics, thereby understanding natural language and generating corresponding responses.
[0005] Currently, large language models are widely used in the field of converting natural language text into database queries. In related technologies, the mainstream approach typically inputs schema information from the database (including table creation statements and primary / foreign keys) as contextual information into the large model, establishing a connection between the large model and database knowledge. Then, a few-shot learning method is used to guide the large model in generating SQL (Structured Query Language) statements. However, because database fields are usually numerous and the relationships between fields and between fields and values are complex, the accuracy of SQL statements generated using large language models suffers.
[0006] There is currently no effective solution to the above problems. Summary of the Invention
[0007] According to one aspect of the embodiments of this application, a database query method is provided, comprising: determining a knowledge graph corresponding to a database to be queried, wherein the knowledge graph is used to represent the logical structure and relationships of data in the database to be queried; determining a similarity score between a user question text and each graph node in the knowledge graph, and determining a target node in each graph node of the knowledge graph based on the similarity score, wherein the similarity score is used to represent the degree of association between the graph node and the user question text; generating database schema information corresponding to the database to be queried based on the target node, and using a large language model, generating a structured query language statement corresponding to the user question text based on the database schema information, wherein the structured query language statement is used to query data in the database to be queried.
[0008] Optionally, determining the knowledge graph corresponding to the database to be queried includes: generating the knowledge graph corresponding to the database to be queried by traversing the data tables in the database to be queried. The knowledge graph consists of multiple graph nodes and edges between graph nodes. Each graph node corresponds to an entity, including table entities and column entities. The attributes of the table entity include table name and table description. The attributes of the column entity include column name, column description, data type, example value, and value meaning. Each edge is used to represent the association between the two graphs it connects to. The association includes: table relationships, table-column containment relationships, and foreign key relationships.
[0009] Optionally, after determining the knowledge graph corresponding to the database to be queried, the method further includes: obtaining a sample dataset corresponding to the database to be queried, wherein the sample dataset contains multiple historical structured query language statements and natural language question texts corresponding to the historical structured query language statements; using a large language model, based on the sample dataset, determining target tuples and target triples, wherein the target tuples are used to represent the correspondence between standard fields in the historical structured query language statements and descriptive information in the natural language question texts, with each standard field corresponding to a column entity, and the target triples are used to represent the dependency relationship between two standard fields; and adding the information corresponding to the target tuples and target triples to the knowledge graph.
[0010] Optionally, obtaining the sample dataset corresponding to the database to be queried includes: obtaining the structured query language statements used for data querying from the historical interaction data of the database to be queried, thereby obtaining the historical structured query language statements, wherein the data query includes: multi-table query; using a large language model to generate natural language question text corresponding to the historical structured query language statements, and verifying and correcting the natural language question text generated by the large language model; and constructing the sample dataset based on the historical structured query language statements and the corrected natural language question text.
[0011] Optionally, determining target nodes among the various graph nodes of the knowledge graph based on similarity scores includes: using a target model to convert the user's question text into a first vector representation and converting the graph node into a second vector representation, wherein the second vector representation is determined by the attributes corresponding to the graph node, the target model is trained based on multiple training sample pairs, and the training sample pairs are determined by standard fields extracted from the sample dataset and the graph nodes corresponding to the standard fields in the knowledge graph; determining the similarity score between the first vector representation and the second vector representation, and identifying graph nodes with similarity scores greater than a preset similarity threshold as target nodes.
[0012] Optionally, using a large language model and based on database schema information, generating a structured query language statement corresponding to the user's question text includes: reconstructing the database schema information corresponding to the database to be queried based on the structural information of the target node and the corresponding dependencies in the knowledge graph, wherein the database schema information is used to limit the scope of the generated structured query language statement; using a large language model, determining knowledge based on the reconstructed database schema information, the user's question text, and instruction information, wherein the knowledge is used to represent the association between the user's question text and the database schema information, and the instruction information is used to instruct the large language model to complete the task; and using a large language model, generating a structured query language statement corresponding to the user's question text based on the reconstructed database schema information, knowledge, and the user's question text.
[0013] Optionally, the method further includes: identifying target historical user query texts in the database to be queried that have a semantic similarity greater than a preset threshold with the user question text; obtaining the decoding process data of the structured query language statement corresponding to the target historical user query text; and using a large language model, generating the structured query language statement corresponding to the user question text based on the reconstructed database schema information, knowledge, user question text, target historical user query text, and decoding process data.
[0014] According to another aspect of the embodiments of this application, a database query apparatus is also provided, comprising: a knowledge graph construction module, configured to determine a knowledge graph corresponding to a database to be queried, wherein the knowledge graph is used to represent the logical structure and relationships of data in the database to be queried; a knowledge selection module, configured to determine the similarity score between a user's question text and each graph node in the knowledge graph, and to determine a target node among each graph node of the knowledge graph based on the similarity score, wherein the similarity score is used to represent the degree of association between the graph node and the user's question text; and a large model decision module, configured to generate database schema information corresponding to the database to be queried based on the target node, and to generate a structured query language statement corresponding to the user's question text using a large language model based on the database schema information, wherein the structured query language statement is used to query data in the database to be queried.
[0015] According to another aspect of the embodiments of this application, an electronic device is also provided, including: a memory and a processor, the processor being configured to run a program stored in the memory, wherein the program executes a database query method during runtime.
[0016] According to another aspect of the embodiments of this application, a non-volatile storage medium is also provided, the non-volatile storage medium including a stored computer program, wherein the device where the non-volatile storage medium is located executes a database query method by running the computer program.
[0017] According to another aspect of the embodiments of this application, a computer program product is also provided, including a computer program that, when executed by a processor, implements the steps of a database query method.
[0018] Details of one or more embodiments of this application are set forth in the following drawings and description. Other features, objects, and advantages of this application will become apparent from the specification, drawings, and claims. Attached Figure Description
[0019] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:
[0020] Figure 1 is a hardware structure block diagram of a computer terminal (or electronic device) for implementing a database query method according to an embodiment of this application;
[0021] Figure 2 is a schematic diagram of a database query method according to an embodiment of this application;
[0022] Figure 3 is a schematic diagram of a method for optimizing database queries based on scalable knowledge graph enhancement according to an embodiment of this application;
[0023] Figure 4 is a schematic diagram of a knowledge graph construction process according to an embodiment of this application;
[0024] Figure 5 is a schematic diagram of a field retrieval model structure provided according to an embodiment of this application;
[0025] Figure 6 is a schematic diagram of a knowledge generation and large model decision execution process according to an embodiment of this application;
[0026] Figure 7 is a schematic diagram of a database query device provided according to an embodiment of this application. Detailed Implementation
[0027] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of the present application.
[0028] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0029] To facilitate a better understanding of the embodiments of this application by those skilled in the art, some technical terms or nouns involved in the embodiments of this application are explained as follows:
[0030] NLP (Natural Language Processing) is a subfield of artificial intelligence (AI) that aims to enable computers to understand, process, and generate human language.
[0031] LLM (Large Language Model): Commonly known as a large model or large-scale language model, it is an artificial intelligence model designed to understand and generate human language. It is typically trained on massive amounts of text data and can perform a wide range of tasks, including text summarization, translation, sentiment analysis, and more, containing billions of parameters.
[0032] Pipeline: A commonly used technique for organizing multiple tasks in a specific order to form a processing flow. Each task receives input data sequentially, generates an output, and then passes the output to the next task as its input.
[0033] A knowledge graph is a technique in the field of artificial intelligence used to represent semantic network structures that depict relationships between entities. It is a semantic graph containing a large number of entities (such as people, places, things, etc.) and the relationships between them. The goal of a knowledge graph is to capture and organize structured information about the world, enabling computers to better understand language and perform reasoning. It is commonly used in applications such as natural language understanding, question-answering systems, and information retrieval, helping computers to more accurately understand semantics and extract and organize knowledge from text.
[0034] Text-to-SQL (Natural Language Text to Structured Database Query) is a technique in the field of natural language processing used to convert natural language questions or instructions into Structured Query Language (SQL). The goal of this technology is to enable computers to understand database query requirements described by humans in natural language and translate them into SQL query statements that can be directly executed on the database. This typically requires a combination of knowledge of language models, syntax analysis, semantic parsing, and database schemas. Applications of text-to-SQL technology include automated question answering systems, intelligent improvements to database query interfaces, and semantic search.
[0035] Schema: In the database field, a schema is a logical concept used to organize objects in a database. Objects in a schema typically include tables, indexes, data types, sequences, stored procedures, primary keys, and foreign keys.
[0036] In related technologies, current mainstream methods typically input schema information from the database (including table creation statements and primary and foreign keys) as contextual information into a large model to establish a connection between the large model and database knowledge. Then, a few-sample learning approach is used to guide the large model in generating SQL statements. However, because databases usually contain a large number of fields, and the relationships between fields and between fields and values are complex, there is a problem of poor accuracy in SQL statements generated using large language models.
[0037] To address the aforementioned issues, this application provides a solution whereby a knowledge graph of the database can be constructed. This graph represents information such as tables, fields, and primary / foreign key relationships within the database, making the information more intuitive and easier to understand. The knowledge graph not only displays relationships between fields but also captures the connections between fields and specific values, thus providing richer and more accurate contextual information for the large model. In practical applications, the knowledge graph is combined with the large model. First, the database schema information is preprocessed using the knowledge graph to extract key structured information. Then, this information is provided as additional input to the large model, helping it better understand the database structure and the relationships between fields. During SQL generation, the large model can utilize the information in the knowledge graph for reasoning and verification, thereby improving the accuracy and reliability of the generated SQL.
[0038] Furthermore, knowledge graphs can be used to enhance the learning process of large models. By adding more semantic information and rules to knowledge graphs, large models can be guided to better understand the intent of natural language queries and generate SQL queries that better meet user needs. This combination not only improves the performance of large models but also makes them more efficient and accurate in handling complex database queries.
[0039] Knowledge graphs play a crucial supporting role in converting natural language text into database queries. By combining knowledge graphs with large-scale models and integrating them with graph retrieval techniques, the problems of numerous fields and complex relationships can be effectively solved, improving the accuracy and reliability of SQL generation and providing users with a better query experience. The following is a detailed description of the solution proposed in this application.
[0040] According to an embodiment of this application, a method embodiment for database query is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0041] The method embodiments provided in this application can be executed in a mobile terminal, computer terminal, or similar computing device. Figure 1 shows a hardware structure block diagram of a computer terminal (or electronic device) for implementing a database query method. As shown in Figure 1, the computer terminal 10 (or electronic device) may include one or more processors 102 (shown as 102a, 102b, ..., 102n in the figure) (processor 102 may include, but is not limited to, a microprocessor MCU or a programmable logic device FPGA, etc.), a memory 104 for storing data, and a transmission device 106 for communication functions. In addition, it may also include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of a BUS bus), a network interface, a power supply, and / or a camera. Those skilled in the art will understand that the structure shown in Figure 1 is only illustrative and does not limit the structure of the above-described electronic device. For example, the computer terminal 10 may also include more or fewer components than shown in Figure 1, or have a different configuration than shown in Figure 1.
[0042] It should be noted that the aforementioned one or more processors 102 and / or other data processing circuits are generally referred to herein as "data processing circuits". These data processing circuits may be embodied, in whole or in part, in software, hardware, firmware, or any other combination thereof. Furthermore, the data processing circuits may be a single, independent processing module, or may be integrated, in whole or in part, into any other element within the computer terminal 10 (or electronic device). As involved in the embodiments of this application, the data processing circuits serve as a processor control mechanism (e.g., selection of a variable resistor termination path connected to an interface).
[0043] The memory 104 can be used to store software programs and modules of application software, such as the program instructions / data storage device corresponding to the database query method in this embodiment. The processor 102 executes various functional applications and data processing by running the software programs and modules stored in the memory 104, thereby realizing the above-mentioned database query method. The memory 104 may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to the computer terminal 10 via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0044] The transmission device 106 is used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by the communication provider of the computer terminal 10. In one example, the transmission device 106 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 may be a Radio Frequency (RF) module, used for wireless communication with the Internet.
[0045] The display may be, for example, a touchscreen liquid crystal display (LCD) that allows the user to interact with the user interface of the computer terminal 10 (or electronic device).
[0046] Under the above operating environment, this application embodiment provides a database query method. Figure 2 is a schematic diagram of the flow of a database query method provided according to an embodiment of this application. As shown in Figure 2, the method includes the following steps:
[0047] Step S202: Determine the knowledge graph corresponding to the database to be queried, wherein the knowledge graph is used to represent the logical structure and relationships of the data in the database to be queried;
[0048] Step S204: Determine the similarity score between the user's question text and each graph node in the knowledge graph, and determine the target node in each graph node of the knowledge graph based on the similarity score. The similarity score is used to characterize the degree of association between the graph node and the user's question text.
[0049] Step S206: Based on the target node, generate the database schema information corresponding to the database to be queried, and use a large language model to generate a structured query language statement corresponding to the user's question text based on the database schema information. The structured query language statement is used to query data in the database to be queried.
[0050] Through the above steps, by structurally representing the stored data content and defined database schema in the database as a knowledge graph, the correlation between data is automatically established. Then, using large model technology, hidden domain knowledge is automatically mined and updated based on the system's interaction history and added to the knowledge graph. This constructs the correlation between user natural language queries and database knowledge. Finally, the knowledge in the knowledge graph is input into the large language model in the form of context, realizing the goal of converting the user's natural language into a general database query statement. This solves the technical problem of poor accuracy in generating database query statements using large language models due to the large amount of field information and complex relationships in the database.
[0051] The database query method in steps S202 to S206 of the embodiments of this application will be further described below.
[0052] This application embodiment combines knowledge graph and large model technology to construct a general knowledge understanding optimization method for text-to-SQL generation. It converts schema information in the database into a knowledge graph and redefines a general database schema that is well-adapted to large model input, applicable to any relational database and highly scalable. Through graph retrieval and dense retrieval techniques, fields and tables highly relevant to the user query can be obtained from the knowledge graph, thereby filtering out irrelevant information that interferes with SQL generation. This significantly reduces the length of the large model input context, effectively saving resources and improving the accuracy and reliability of SQL generation. Figure 3 is a schematic diagram of the method flow for database query optimization based on scalable knowledge graph enhancement according to this application embodiment. As shown in Figure 3, firstly, the knowledge graph is constructed and completed in conjunction with the database schema; secondly, a dense retrieval model is used for knowledge selection; and finally, a large model is used to complete the generation of knowledge and SQL. A detailed description follows.
[0053] First, the knowledge graph is constructed, that is, the knowledge graph corresponding to the database to be queried is determined. In this embodiment, the data source for the knowledge graph construction is the original schema of the database and the stored data details. By constructing the knowledge graph, the relationship between different knowledge in the database can be established, which helps to reconstruct and utilize the knowledge in subsequent stages. The specific steps are as follows.
[0054] In some embodiments of this application, determining the knowledge graph corresponding to the database to be queried includes the following steps: generating the knowledge graph corresponding to the database to be queried by traversing the data tables in the database to be queried. The knowledge graph consists of multiple graph nodes and edges between graph nodes. Each graph node corresponds to an entity, including table entities and column entities. The attributes of the table entity include table name and table description. The attributes of the column entity include column name, column description, data type, example value, and value meaning. Each edge is used to represent the association relationship between the two graphs it connects to. The association relationship includes: inter-table relationship, inclusion relationship between table and column, and foreign key relationship.
[0055] Specifically, a knowledge graph can be constructed based on table structure and column information. In this embodiment, the constructed knowledge graph mainly consists of two key entity types: table entities and column entities. The former uses "Table Name" and "Table Description" as table entity attributes, while the latter uses "Column Name," "Column Description," "Data Type," "Example Value," and "Value Explanation" as column entity attributes.
[0056] Furthermore, the relationships between entities include, but are not limited to, relationships between tables, containment relationships between tables and columns, and foreign key relationships. Metadata (i.e., the entity types and relationship categories mentioned above) can be extracted using different database operations to obtain structured database information for constructing a knowledge graph.
[0057] Meanwhile, to effectively store and query the constructed knowledge graph, this embodiment employs graph database technology (e.g., Neo4j can be used as the graph database implementation tool) to convert the extracted metadata into nodes and edges of the graph database. Table entities and column entities are stored as nodes, with node attributes corresponding to table and column attributes. Relationships between tables, containment relationships between tables and columns, and foreign key relationships are stored as edges, with edge types and attributes reflecting the specific content of the relationships. Graph databases can naturally represent entities and their relationships, are highly efficient in handling complex queries, and facilitate convenient storage, updating, and querying of graph data.
[0058] This application embodiment constructs a knowledge graph to structurally represent the table knowledge, field knowledge, and dependency relationships in the database. In subsequent solutions, key knowledge obtained from the knowledge base based on user questions can be reorganized into a general database pattern to assist in SQL generation.
[0059] In real-world applications, due to varying scenarios, manually constructed databases don't always provide complete information describing the database schema. This can result in a sparse knowledge graph built through the aforementioned steps, and some functional dependencies between fields (such as formula calculations and numerical dependencies) may not be provided. However, when people interact with the database, the relationship between user questions and SQL queries typically requires high-quality association of these dependencies. Therefore, this embodiment can also use a large model to assist manual completion of the database dynamically, with the specific steps as follows.
[0060] In some embodiments of this application, after determining the knowledge graph corresponding to the database to be queried, the method further includes the following steps: obtaining a sample dataset corresponding to the database to be queried, wherein the sample dataset contains multiple historical structured query language statements and natural language question texts corresponding to the historical structured query language statements; using a large language model, based on the sample dataset, determining target tuples and target triples, wherein the target tuple is used to represent the correspondence between standard fields in the historical structured query language statements and descriptive information in the natural language question text, each standard field corresponds to a column entity, and the target triple is used to represent the dependency relationship between two standard fields; adding the information corresponding to the target tuples and target triples to the knowledge graph.
[0061] Specifically, the construction process of the above sample dataset is described below, with the specific steps as follows.
[0062] In some embodiments of this application, obtaining the sample dataset corresponding to the database to be queried includes the following steps: obtaining the structured query language statements used for data querying from the historical interaction data of the database to be queried, thereby obtaining the historical structured query language statements, wherein the data query includes: multi-table query; using a large language model to generate natural language question text corresponding to the historical structured query language statements, and verifying and correcting the natural language question text generated by the large language model; and constructing the sample dataset based on the historical structured query language statements and the corrected natural language question text.
[0063] Specifically, 10,000 SQL statements involving multi-table queries and responsible queries (i.e., the aforementioned historical structured query language statements) can be filtered from the historical interaction data of the database to be queried using rule functions. Then, the historical SQL statements and database schema information are used as context input to a large language model (e.g., GPT4), which is then guided to output the user's natural language question (i.e., the aforementioned natural language question text) that the historical SQL statement may correspond to. The output of the large language model will correspond one-to-one with the input historical SQL statement. Afterward, professional database operators can manually verify the output pre-labeled data (i.e., the natural language question text corresponding to the historical structured query language statements), revise the natural language expression, and finally use the revised natural language text and SQL statement as the initial information for completing the data knowledge graph (i.e., the aforementioned sample dataset).
[0064] After obtaining the sample dataset, a large language model can be used to determine the target bigrams and target triples based on the dataset. Specifically, the simulated user question (i.e., the natural language question text mentioned above), historical SQL statements, and the original database schema are input into the large model. The model then extracts the attribute descriptions of the fields: the standard fields in the historical SQL statements are mapped to the natural language question text to form the bigram attribute = {column name, desc}, where desc represents the descriptive information about the field corresponding to column name extracted from the natural language question text; and the model can also extract the triple relationship relationship = {column name1, dependency, column name2}, which defines the specific dependency between two fields with a dependency relationship. Finally, column name and relationship will be used as completion information to update the knowledge graph and the large model. Through knowledge graph completion, more field dependencies are established, enriching the attribute information of the fields, which helps in subsequent semantic matching using retrieval models and provides richer knowledge.
[0065] This application adopts a paradigm combining knowledge graphs and large models, reconstructing isolated data content and structural information in the database into a graph storage format, which can effectively mine dependencies in patterns. Using large model technology, the knowledge graph can be automatically completed based on the system's interaction history, automatically adapting to changes in database patterns and reducing the need for manual intervention. Through the self-learning capability of the large model, the system can continuously optimize and update the knowledge graph, maintaining efficient support for the latest data and patterns.
[0066] The following example illustrates the process of constructing and completing the knowledge graph.
[0067] As shown in Figure 4, in this embodiment, the database structure and its data details are all stored in files ending with ".sqlite". First, the program iterates through all databases and their sub-table names in the database file. Then, using standard SQLite database operation statements, the relevant field names and their associated attribute values are extracted to form a candidate set. Finally, Neo4j is used to transform it into a knowledge graph structure.
[0068] After constructing and completing the knowledge graph, the knowledge based on the general knowledge base can be easily managed and maintained. However, the database information closely related to the user's question has not been identified. Therefore, in this embodiment, a new general database pattern can be constructed by recalling knowledge closely related to the user's question text from a large number of knowledge graph nodes through a dense retrieval model. That is, the target node is determined in each graph node of the knowledge graph. The specific steps are as follows.
[0069] In some embodiments of this application, determining target nodes among various graph nodes of a knowledge graph based on similarity scores includes the following steps: using a target model to convert user question text into a first vector representation and graph nodes into second vector representations, wherein the second vector representation is determined by the attributes corresponding to the graph nodes, the target model is trained based on multiple training sample pairs, and the training sample pairs are determined by standard fields extracted from the sample dataset and graph nodes corresponding to the standard fields in the knowledge graph; determining the similarity score between the first vector representation and the second vector representation, and identifying graph nodes with similarity scores greater than a preset similarity threshold as target nodes.
[0070] Specifically, the first step is to construct the knowledge retrieval training data: the 10,000 user questions and SQL statement question-answer pairs collected in the early stage (i.e., the data in the above sample dataset) can be used as the initial data. The tables and field names (i.e., the above standard fields) involved in the historical SQL statements are extracted through the rule function. Then, the corresponding node n (containing node information and attribute values: field name, field description, value example, value description, association information, etc.) is queried from the database or knowledge graph. Then, the user question q and the extracted nodes are used to form multiple training sample pairs {(q, n1), (q, n2)...} for training.
[0071] Next, the knowledge retrieval model (i.e., the target model mentioned above) can be constructed. Specifically, a pre-trained language model (BERT) can be selected as the underlying semantic encoder to represent the user question text and graph nodes. Then, the MaxSim post-interaction method based on maximum similarity is used to calculate the similarity score between the user question q and the graph node c. The degree of association between the table and the node is determined by the similarity score. The specific calculation formula is shown below: O q =Normalize(CNN(BERT) Q (q)) O c =Normalize(CNN(BERT) C (c))
[0072] Among them, O q and O cIt is a vector representation encoded by a dual-tower model, corresponding to the first vector representation and the second vector representation mentioned above, respectively. The score represents the similarity score between the user's question text and the current graph node. By setting a similarity threshold, the target node can be filtered.
[0073] Then, new database schema information can be redefined based on the selected knowledge graph nodes (i.e. target nodes) and provided as context for SQL generation in the large model.
[0074] The embodiments of this application employ dense retrieval technology, which can efficiently find fields and tables related to user queries on large-scale complex knowledge graphs. By calculating the similarity in the vector space, irrelevant information can be quickly filtered out, thereby improving the accuracy and efficiency of SQL generation.
[0075] The following example illustrates the process of selecting the above knowledge.
[0076] As shown in Figure 5, in this embodiment, a dual-tower model can be used to retrieve the field most relevant to the current problem from the knowledge graph, and then the graph tool is used to determine the table to which the current field belongs. The knowledge retrieval engine is trained based on ColBERT. First, based on all field names in the database table, entity links are created in the knowledge graph, transforming the corresponding representation of the entities into: {n=n name ||ndescription||c example Finally, the knowledge representations of the user query and the field entities are input into the field retrieval engine to calculate the similarity. By setting a score threshold μ, field entities with a similarity score greater than μ with the user query will be retained as disambiguated entities; otherwise, they will be discarded.
[0077] After selecting target nodes with high relevance to the user's question text from the knowledge graph, a large-scale model decision can be made. This decision-making process begins by retrieving relevant fields, tables, and dependencies from the knowledge graph to construct a new database schema and generate knowledge associations. This information is then input into the large-scale model as context. A thought chain model is used to automatically break down the SQL generation process into multiple sub-problems. These sub-problems, from simple to complex, gradually generate the final SQL. Specifically, based on the target nodes, the database schema information corresponding to the database to be queried is generated. Then, a large language model is used to generate a structured query language statement corresponding to the user's question text based on the database schema information. The specific steps are as follows.
[0078] In some embodiments of this application, generating a structured query language statement corresponding to a user question text using a large language model and based on database schema information includes the following steps: reconstructing the database schema information corresponding to the database to be queried based on the structural information of the target node and the corresponding dependencies in the knowledge graph, wherein the database schema information is used to limit the scope of the generated structured query language statement; determining knowledge using a large language model based on the reconstructed database schema information, the user question text, and instruction information, wherein the knowledge is used to characterize the association between the user question text and the database schema information, and the instruction information is used to instruct the large language model to complete the task; and generating a structured query language statement corresponding to the user question text using a large language model based on the reconstructed database schema information, knowledge, and the user question text.
[0079] In some embodiments of this application, the method further includes: determining a target historical user query text in the historical user query text of the database to be queried that has a semantic similarity greater than a preset threshold with the user question text; obtaining the decoding process data of the structured query language statement corresponding to the target historical user query text; and using a large language model, generating the structured query language statement corresponding to the user question text based on the reconstructed database schema information, knowledge, user question text, target historical user query text, and decoding process data.
[0080] Specifically, a knowledge generation prompt can be designed to guide the large model to output a knowledge description between the current user question and the new database schema. In this embodiment, the prompt for the large model decision consists of five parts, including: instruction information, database schema information, knowledge, small sample (i.e., the decoding process data of the structured query language statement corresponding to the above target historical user query text) and user query (i.e., the above user question text).
[0081] The instruction information clarifies the tasks and precautions that the large model needs to complete. The database schema information uses a knowledge graph to filter out high-value tables and fields, representing them in a structured form to limit the scope of SQL generation. The knowledge section establishes a connection between user queries (user question text) and the database (database schema information), providing explanations of fields to help the large model understand their meaning. The small sample section uses a small sample filtering module to filter out multiple historical user queries and their SQL decoding processes that are semantically similar to the current user query. Specifically, it identifies target historical user query texts in the database to be queried that have a semantic similarity greater than a preset threshold with the user question text, and obtains the decoding process data of the structured query language statement corresponding to the target historical user query text. The user query (user question text) section clarifies the user's current query question.
[0082] In this embodiment, the large model module is flexible and versatile, without restrictions on its construction form or structure. If corresponding machine resources are available, training samples can be generated using the input prompt and correct SQL output of the large model for fine-tuning. Alternatively, the user's prompt can be directly input into the large model's online service or through open-source large model inference. Through learning from database schemas, knowledge, and few-shot examples, the large model divides user queries into multiple sub-questions and generates the complete SQL query in the final sub-step.
[0083] The following example illustrates the process of knowledge generation and large-scale model decision-making.
[0084] Taking the user question "Please provide the names of schools that meet the following conditions: the number of students eligible for free meals in K-12 exceeding 0.1 and with a score exceeding 1500" as an example. As shown in Figure 6, in this embodiment, knowledge selection can first be used to reorganize the fields recalled in the knowledge graph and their related tables and dependencies in the graph into a "schema based on knowledge graph reconstruction"; then, the user's question and the reconstructed schema are used as input information, and instruction information is concatenated as prompt words to input into the large model.
[0085] The large model understands and executes the above instructions, outputting knowledge that associates the user's problem with the schema, thus generating knowledge clues. Finally, the reconstructed schema, knowledge, and user problem are used together as context, prompting the large model: "Please combine the provided information to understand the knowledge in the data, divide the user's problem into multiple sub-problems, solve the sub-problems sequentially, and finally generate SQL." Finally, through the output of the large model, the SQL query "SELECT T2.`School Name`FROM satscores AS T1 INNER JOIN frpm AS T2 ON T1.cds=T2.CDSCode WHERE CAST(T2.`Free Meal Count(K-12)`AS REAL) / T2.`Enrollment(K-12)`>0.1AND T1.NumGE1500>0" is parsed.
[0086] Furthermore, after generating the SQL statement corresponding to the user's question text, the generated SQL statement and the corresponding user question text can be stored as historical information. Database professionals will verify the correctness of the SQL. If the SQL matches the user's question and executes correctly, it will be stored in the historical dependency database. Then, the <question, SQL> structure is periodically traversed to extract knowledge from the large model, thereby updating the knowledge graph.
[0087] This application proposes an SQL data retrieval optimization scheme that combines knowledge graphs, dense retrieval, and large model technologies. Addressing the challenge of acquiring data knowledge in intelligent database retrieval scenarios, which makes it difficult to provide effective knowledge understanding for large models and thus leads to inaccurate SQL generation, this application provides a pluggable and easy-to-implement intelligent data retrieval SQL generation solution, which has been practically deployed in intelligent data retrieval production scenarios.
[0088] By constructing a database-based knowledge graph, the structural and value information of the database is used as edges, nodes, and attribute values in the knowledge graph. Through the graph structure, relationships between various knowledge are cleverly established, and knowledge is easy to maintain and select. By constructing the knowledge graph, the characteristics of table data can be learned for different business scenarios, helping the model understand table information. Furthermore, useful information can be extracted from historical dialogues in the database to further improve the adaptability to business scenarios.
[0089] Based on dense retrieval technology, fields and user questions in the knowledge graph are represented as high-dimensional vectors. By using the similarity between vectors, tables and fields highly related to user questions can be retrieved from the semantic space perspective. This enables rapid retrieval and disambiguation of graph knowledge. Before generating SQL, information on potentially involved table fields is extracted from the knowledge graph, and potential knowledge content is retrieved through the association between different nodes in the graph. This effectively obtains fields and table content related to user questions, thereby greatly reducing the interference of redundant fields on subsequent model decisions and improving the accuracy of SQL generation.
[0090] By leveraging the powerful semantic understanding capabilities of large-scale models, the correlation between data patterns and user queries can be automatically extracted based on database interaction history, greatly enriching the completeness of the knowledge graph and providing more knowledge sources for SQL generation. The generation method based on large-scale models can intelligently generate SQL queries based on user input. Due to the prompt information provided by the large-scale models, background knowledge can be integrated into the model's learning and generation process, reducing information loss caused by indirect knowledge transfer. The generative process can dynamically and intelligently generate query statements based on user input.
[0091] In addition, the proposed solution is based on a general database data retrieval process, which allows for flexible selection of whether to add knowledge expansion modules according to different application scenarios and database settings, resulting in high scalability.
[0092] According to an embodiment of this application, a database query device is also provided. Figure 7 is a schematic diagram of the structure of a database query device provided according to an embodiment of this application. As shown in Figure 7, the device includes:
[0093] The knowledge graph construction module 70 is used to determine the knowledge graph corresponding to the database to be queried, wherein the knowledge graph is used to represent the logical structure and relationship of the data in the database to be queried;
[0094] The knowledge selection module 72 is used to determine the similarity score between the user's question text and each graph node in the knowledge graph, and to determine the target node in each graph node of the knowledge graph based on the similarity score. The similarity score is used to characterize the degree of association between the graph node and the user's question text.
[0095] The large model decision module 74 is used to generate database schema information corresponding to the database to be queried based on the target node, and to generate a structured query language statement corresponding to the user's question text based on the database schema information using a large language model. The structured query language statement is used to query data in the database to be queried.
[0096] Optionally, determining the knowledge graph corresponding to the database to be queried includes: generating the knowledge graph corresponding to the database to be queried by traversing the data tables in the database to be queried. The knowledge graph consists of multiple graph nodes and edges between graph nodes. Each graph node corresponds to an entity, including table entities and column entities. The attributes of the table entity include table name and table description. The attributes of the column entity include column name, column description, data type, example value, and value meaning. Each edge is used to represent the association between the two graphs it connects to. The association includes: table relationships, table-column containment relationships, and foreign key relationships.
[0097] Optionally, after determining the knowledge graph corresponding to the database to be queried, the knowledge graph construction module 70 is further configured to: obtain a sample dataset corresponding to the database to be queried, wherein the sample dataset contains multiple historical structured query language statements and natural language question texts corresponding to the historical structured query language statements; using a large language model, based on the sample dataset, determine target tuples and target triples, wherein the target tuples are used to represent the correspondence between standard fields in the historical structured query language statements and descriptive information in the natural language question texts, with each standard field corresponding to a column entity, and the target triples are used to represent the dependency relationship between two standard fields; and add the information corresponding to the target tuples and target triples to the knowledge graph.
[0098] Optionally, obtaining the sample dataset corresponding to the database to be queried includes: obtaining the structured query language statements used for data querying from the historical interaction data of the database to be queried, thereby obtaining the historical structured query language statements, wherein the data query includes: multi-table query; using a large language model to generate natural language question text corresponding to the historical structured query language statements, and verifying and correcting the natural language question text generated by the large language model; and constructing the sample dataset based on the historical structured query language statements and the corrected natural language question text.
[0099] Optionally, determining target nodes among the various graph nodes of the knowledge graph based on similarity scores includes: using a target model to convert the user's question text into a first vector representation and converting the graph node into a second vector representation, wherein the second vector representation is determined by the attributes corresponding to the graph node, the target model is trained based on multiple training sample pairs, and the training sample pairs are determined by standard fields extracted from the sample dataset and the graph nodes corresponding to the standard fields in the knowledge graph; determining the similarity score between the first vector representation and the second vector representation, and identifying graph nodes with similarity scores greater than a preset similarity threshold as target nodes.
[0100] Optionally, using a large language model and based on database schema information, generating a structured query language statement corresponding to the user's question text includes: reconstructing the database schema information corresponding to the database to be queried based on the structural information of the target node and the corresponding dependencies in the knowledge graph, wherein the database schema information is used to limit the scope of the generated structured query language statement; using a large language model, determining knowledge based on the reconstructed database schema information, the user's question text, and instruction information, wherein the knowledge is used to represent the association between the user's question text and the database schema information, and the instruction information is used to instruct the large language model to complete the task; and using a large language model, generating a structured query language statement corresponding to the user's question text based on the reconstructed database schema information, knowledge, and the user's question text.
[0101] Optionally, the large model decision module 74 is also used to: determine the target historical user query text in the historical user query text of the database to be queried, whose semantic similarity with the user question text is greater than a preset threshold; obtain the decoding process data of the structured query language statement corresponding to the target historical user query text; and use the large language model to generate the structured query language statement corresponding to the user question text based on the reconstructed database schema information, knowledge, user question text, target historical user query text, and decoding process data.
[0102] It should be noted that each module in the above-mentioned database query device can be a program module (for example, a set of program instructions that implement a certain function) or a hardware module. For the latter, it can be manifested in the following forms, but is not limited to them: each of the above modules is manifested as a processor, or the functions of each of the above modules are implemented by a processor.
[0103] It should be noted that the database query device provided in this embodiment can be used to execute the database query method shown in Figure 2. Therefore, the relevant explanations and descriptions of the above database query method also apply to the embodiments of this application, and will not be repeated here.
[0104] This application embodiment also provides a non-volatile storage medium, which includes a stored computer program. The device containing the non-volatile storage medium executes the following database query method by running the computer program: determining a knowledge graph corresponding to the database to be queried, wherein the knowledge graph is used to represent the logical structure and relationships of the data in the database to be queried; determining the similarity score between the user's question text and each graph node in the knowledge graph, and determining a target node among the graph nodes based on the similarity score, wherein the similarity score is used to represent the degree of association between the graph node and the user's question text; generating database schema information corresponding to the database to be queried based on the target node, and using a large language model, generating a structured query language statement corresponding to the user's question text based on the database schema information, wherein the structured query language statement is used to query data in the database to be queried.
[0105] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the database query method described in various embodiments of this application: determining a knowledge graph corresponding to the database to be queried, wherein the knowledge graph is used to represent the logical structure and relationships of the data in the database to be queried; determining the similarity score between the user's question text and each graph node in the knowledge graph, and determining a target node in each graph node of the knowledge graph based on the similarity score, wherein the similarity score is used to represent the degree of association between the graph node and the user's question text; generating database schema information corresponding to the database to be queried based on the target node, and using a large language model, generating a structured query language statement corresponding to the user's question text based on the database schema information, wherein the structured query language statement is used to query data in the database to be queried.
[0106] The sequence numbers of the embodiments in this application are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0107] In the above embodiments of this application, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0108] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units can be a logical functional division, and in actual implementation, there may be other division methods. For instance, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual coupling, direct coupling, or communication connection may be through some interfaces; the indirect coupling or communication connection between units or modules may be electrical or other forms.
[0109] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0110] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0111] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard drive, magnetic disk, or optical disk.
[0112] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0113] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.
Claims
1. A database query method, comprising: determining a knowledge graph corresponding to a database to be queried, wherein the knowledge graph is used to represent the logical structure and the association relationship of the data in the database to be queried; determining a similarity score between a user question text and each graph node in the knowledge graph, and determining a target node among the graph nodes in the knowledge graph according to the similarity score, wherein the similarity score represents the degree of association between the graph node and the user question text; generating database schema information corresponding to the database to be queried according to the target node, and generating a structured query language statement corresponding to the user question text according to the database schema information by using a large language model, wherein the structured query language statement is used to query data in the database to be queried.
2. The database query method of claim 1, wherein determining a knowledge graph corresponding to a database to be queried comprises: generating the knowledge graph corresponding to the database to be queried by traversing the data tables in the database to be queried, wherein the knowledge graph is composed of a plurality of graph nodes and edges between the graph nodes, each graph node corresponds to an entity, and the entity includes table entities and column entities, the attributes of the table entities include table names and table descriptions, and the attributes of the column entities include column names, column descriptions, data types, example values, and value meanings, each edge represents the association relationship between two graph nodes connected by the edge, and the association relationship includes table relationship, inclusion relationship between table and column, and foreign key relationship.
3. The database query method of claim 2, wherein after determining the knowledge graph corresponding to the database to be queried, the method further comprises: obtaining a sample data set corresponding to the database to be queried, wherein the sample data set includes a plurality of historical structured query language statements and natural language question texts corresponding to the historical structured query language statements; determining target binary tuples and target triplets according to the sample data set by using a large language model, wherein the target binary tuples represent the corresponding relationship between standard fields in the historical structured query language statements and description information in the natural language question texts, each standard field corresponds to a column entity, and the target triplets represent the dependency relationship between two standard fields; adding information corresponding to the target binary tuples and the target triplets to the knowledge graph.
4. The database query method of claim 3, wherein obtaining a sample data set corresponding to the database to be queried comprises: obtaining a structured query language statement for data query in historical interaction data of the database to be queried to obtain the historical structured query language statement, wherein the data query includes multi-table query; generating the natural language question text corresponding to the historical structured query language statement by using a large language model, and verifying and correcting the natural language question text generated by the large language model. construct the sample dataset according to the historical structured query language sentence and the revised natural language question text.
5. The database query method of claim 4, wherein determining a target node among the graph nodes of the knowledge graph according to the similarity score comprises: converting the user question text into a first vector representation and converting the graph nodes into second vector representations using a target model, wherein the second vector representations are determined by attributes corresponding to the graph nodes, and the target model is trained according to a plurality of training sample pairs, the training sample pairs being determined by the standard fields extracted from the sample dataset and the graph nodes of the knowledge graph corresponding to the standard fields; determining the similarity score between the first vector representation and the second vector representation, and determining the graph node with a similarity score greater than a preset similarity threshold as the target node.
6. The database query method of claim 1, wherein generating a structured query language sentence corresponding to the user question text according to the database schema information using a large language model comprises: reconstructing the database schema information corresponding to the database to be queried according to the structural information and corresponding dependency relationships of the target node in the knowledge graph, wherein the database schema information is used to limit the range of structured query language sentence generation; determining knowledge according to the reconstructed database schema information, the user question text, and instruction information using a large language model, wherein the knowledge is used to represent the association between the user question text and the database schema information, and the instruction information is used to indicate the task to be completed by the large language model; generating the structured query language sentence corresponding to the user question text according to the reconstructed database schema information, the knowledge, and the user question text using a large language model.
7. The database query method of claim 6, wherein the method further comprises: determining a target historical user query text with a semantic similarity greater than a preset threshold between the user question text in the historical user query text of the database to be queried; obtaining decoding process data of the structured query language sentence corresponding to the target historical user query text; generating the structured query language sentence corresponding to the user question text according to the reconstructed database schema information, the knowledge, the user question text, the target historical user query text, and the decoding process data using a large language model.
8. A database query apparatus, comprising: a knowledge graph construction module configured to determine a knowledge graph corresponding to a database to be queried, wherein the knowledge graph is used to represent the logical structure and association of data in the database to be queried. The knowledge selection module is configured to determine a similarity score between the user question text and each graph node in the knowledge graph, and determine a target node from the graph nodes in the knowledge graph according to the similarity score, wherein the similarity score represents a degree of association between the graph node and the user question text. The large model decision module is configured to generate database schema information corresponding to the to-be-queried database according to the target node, and generate a structured query language statement corresponding to the user question text according to the database schema information by using a large language model, wherein the structured query language statement is used to query data in the to-be-queried database.
9. An electronic device comprising: The memory and the processor are configured to run a program stored in the memory, and the program is configured to implement the database query method in any one of claims 1 to 7 when running.
10. A non-transitory storage medium including a stored computer program, wherein, The device in which the non-volatile storage medium is located is configured to implement the database query method in any one of claims 1 to 7 by running the computer program.
11. A computer program product comprising a computer program, wherein the computer program is configured to implement the steps of the database query method in any one of claims 1 to 7 when executed by a processor.