Database knowledge body construction method and structured query language generation method
By building a relational database knowledge body and integrating data table metadata and historical database table query data, the problem of insufficient data information accuracy and comprehensiveness in TEXT2SQL generation is solved, and higher quality and accurate SQL generation is achieved.
Patent Information
- Application Number
- CN202411441782.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-15
- Publication Date
- 2025-02-28
AI Technical Summary
When the prior art uses a large language model to generate TEXT2SQL, the accuracy and comprehensiveness of the data information are insufficient, resulting in unsatisfactory generation results.
A method for constructing relational database knowledge bodies and structured query language generation method are proposed. By integrating data table metadata and historical database table query data, a knowledge network containing multi-layer node data is built to improve the TEXT2SQL generation capability of large language models.
By providing refined low-redundancy, accurate, comprehensive and rich dependency data, the TEXT2SQL generation quality and accuracy of large language models are significantly improved.
Smart Images

Figure CN119537486A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of neural network models, and in particular to a method for constructing a database knowledge body and a method for generating a structured query language. Background Art
[0002] TEXT2SQL (Text to SQL, converting natural language text into structured query language SQL) technology based on large language models has brought possibilities for automation and intelligence of data analysis, and has a wide range of application scenarios. At present, high-performance large language models (LLM) have strong SQL logic understanding capabilities (such as GPT-4), but the generation results of TEXT2SQL based on LLM are not ideal. The main reason is that in the TEXT2SQL process, the data information provided for LLM to generate SQL cannot guarantee sufficient accuracy and comprehensiveness within the smallest possible context length, and cannot support LLM to complete the SQL generation task excellently.
[0003] Generally, when using LLM to generate SQL, the conventional method is to use ordinary database knowledge graphs (knowledge bases) for data retrieval, or directly search for fields and tables in the original database based on the similarity principle, or search for similar scenarios from the historical records of manual SQL queries and provide corresponding SQL codes as a reference for LLM to generate SQL.
[0004] Regarding the method of using conventional database knowledge graphs (knowledge bases), since conventional database knowledge graphs (knowledge bases) are usually mainly generated by database metadata and their usage scenarios are not specifically for TEXT2SQL, the information contained therein is not sufficient to support TEXT2SQL tasks.
[0005] Regarding the method of directly searching for fields and tables in the original database based on the similarity principle, since this type of retrieval cannot be very accurate, when sufficient information needs to be recalled, the retrieval structure will contain a lot of redundant and irrelevant information, which will cause the LLM context length to be occupied too much. At the same time, the generated results will be inaccurate and unstable due to the inclusion of irrelevant information.
[0006] Regarding the method of searching for similar scenarios from the historical records of manual SQL queries and providing corresponding SQL codes, since historical similar scenarios can never be completely consistent with actual retrieval requirements, this method can only provide partial data and SQL references and cannot guarantee high accuracy of generation. Summary of the invention
[0007] The present application aims to solve one of the technical problems in the related art at least to some extent.
[0008] To this end, the first purpose of the present application is to propose a method for constructing a relational database knowledge body to provide a relational database knowledge body for improving the TEXT2SQL generation capability of a large language model.
[0009] The second purpose of this application is to propose a structured query language generation method based on a relational database knowledge body, which effectively improves the generation quality of the large language model TEXT2SQL.
[0010] The third objective of the present application is to provide a device for constructing a relational database knowledge body.
[0011] The fourth objective of the present application is to propose a structured query language generation device based on a relational database knowledge body.
[0012] A fifth objective of the present application is to provide an electronic device.
[0013] A sixth object of the present application is to provide a computer-readable storage medium.
[0014] A seventh object of the present application is to provide a computer program product.
[0015] To achieve the above-mentioned purpose, the first aspect of the present application proposes a method for constructing a relational database knowledge body, wherein the relational database knowledge body includes a plurality of knowledge body units, and the plurality of knowledge body units are interconnected through associated fields to form a knowledge network; each of the knowledge body units corresponds to a data table, and the knowledge body unit includes multi-layer node data, and the multi-layer node data includes table information, field information and field vector, field enumeration value information and field enumeration value vector representation of the data table; the construction method includes the following steps:
[0016] Based on the data table metadata, obtaining first data table information;
[0017] Inferring and extracting the text-code pairing data set through a large language model to obtain second data table information; wherein the text-code pairing data set includes a plurality of pairing data consisting of question texts and corresponding SQL codes obtained from a database query history;
[0018] Merging the first data table information and the second data table information to obtain data table fusion information;
[0019] The data table fusion information is matched with the multi-layer node data of the knowledge body unit to construct a relational database knowledge body.
[0020] To achieve the above-mentioned purpose, the second aspect of the present application proposes a method for generating a structured query language based on a relational database knowledge body, characterized in that the relational database knowledge body is a relational database knowledge body constructed by the method for constructing a relational database knowledge body described in any one of the first aspects; the method for generating a structured query language comprises the following steps:
[0021] Get user questions;
[0022] Retrieving the relational database knowledge body based on the user question to obtain third data table information;
[0023] Merging the user question with the third data table information to obtain data table merge information;
[0024] The data table merging information is added to the prompt words of the large language model, and the prompt words are input into the large language model to generate a structured query language.
[0025] To achieve the above-mentioned purpose, the third aspect of the present application proposes a device for constructing a relational database knowledge body, wherein the relational database knowledge body includes a plurality of knowledge body units, and the plurality of knowledge body units are interconnected through associated fields to form a knowledge network; each of the knowledge body units corresponds to a data table, and the knowledge body unit includes multi-layer node data, and the multi-layer node data includes table information, field information and field vector, field enumeration value information and field enumeration value vector representation of the data table; the device includes:
[0026] An information acquisition module, used for acquiring first data table information based on data table metadata;
[0027] An information extraction module, used to infer and extract the text-code pairing data set through a large language model to obtain second data table information; wherein the text-code pairing data set includes a plurality of pairing data consisting of question texts and corresponding SQL codes obtained from the database query history;
[0028] An information fusion module, used for fusing the first data table information and the second data table information to obtain data table fusion information;
[0029] The knowledge body construction module is used to match the data table fusion information with the multi-layer node data of the knowledge body unit to construct a relational database knowledge body.
[0030] To achieve the above-mentioned purpose, the fourth aspect of the present application proposes a structured query language generation device based on a relational database knowledge body, characterized in that the relational database knowledge body is a relational database knowledge body constructed by the relational database knowledge body construction method described in any one of the first aspects; the structured query language generation device includes:
[0031] Question text acquisition module, used to obtain user questions;
[0032] A knowledge retrieval module, used to retrieve the relational database knowledge body based on the user question to obtain third data table information;
[0033] An information merging module, used for merging the user question with the third data table information to obtain data table merged information;
[0034] The language generation module is used to add the data table merging information to the prompt words of the large language model, and input the prompt words into the large language model to generate a structured query language.
[0035] To achieve the above-mentioned purpose, the fifth aspect embodiment of the present application proposes an electronic device, comprising: a processor, and a memory communicatively connected to the processor; the memory stores computer-executable instructions; the processor executes the computer-executable instructions stored in the memory to implement the method described in the first aspect or the second aspect.
[0036] To achieve the above-mentioned purpose, the sixth aspect embodiment of the present application proposes a computer-readable storage medium, in which computer execution instructions are stored. When the computer execution instructions are executed by a processor, they are used to implement the method described in the first aspect or the second aspect.
[0037] To achieve the above-mentioned purpose, the seventh aspect of the present application proposes a computer program product, including a computer program, which implements the method described in the first aspect or the second aspect when executed by a processor.
[0038] The database knowledge body construction method and structured query language generation method provided in the present application, on the basis of conventional data table metadata, perform knowledge extraction on the accumulated historical database table query data, and perform data fusion based on the database knowledge body structure proposed in the present application, so that the database knowledge body not only has the basic metadata information of the conventional database knowledge graph or knowledge base, but also integrates the accurate and extensive knowledge and experience in historical manual queries, performs necessary supplements and quality improvements on the database table metadata, and obtains a relational database knowledge body for improving the TEXT2SQL generation capability of the large language model. After obtaining the user question, the relational database knowledge body is retrieved based on the user question to obtain the highly accurate and comprehensive data information required for LLM to generate SQL, providing refined, low-redundant, accurate, comprehensive and rich dependency data for the TEXT2SQL task of the large language model, improving the TEXT2SQL generation capability of the large language model, and improving the quality of TEXT2SQL generation.
[0039] Additional aspects and advantages of the present application will be given in part in the description below, and in part will become apparent from the description below, or will be learned through the practice of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] The above and / or additional aspects and advantages of the present application will become apparent and easily understood from the following description of the embodiments in conjunction with the accompanying drawings, in which:
[0041] Figure 1 A flowchart of a method for constructing a relational database knowledge body provided in an embodiment of the present application;
[0042] Figure 2 A schematic diagram of the structure of the relational database knowledge body provided in the embodiment of the present application.
[0043] Figure 3 An example diagram of a method for constructing a relational database knowledge body provided in an embodiment of the present application;
[0044] Figure 4 A flow chart of a method for generating a structured query language based on a relational database knowledge body provided in an embodiment of the present application;
[0045] Figure 5 An example diagram of a method for generating a structured query language based on a relational database knowledge body provided in an embodiment of the present application;
[0046] Figure 6 An example diagram of a database knowledge body retrieval process provided by an embodiment of the present application;
[0047] Figure 7An example diagram of a process of expanding the search structure of a database knowledge body to obtain other relevant information provided by an embodiment of the present application;
[0048] Figure 8 A diagram showing the minimum set data table and field selection principle of the minimum full coverage table set principle provided in the embodiment of the present application;
[0049] Fig. 9 A schematic diagram of the structure of a device for constructing a relational database knowledge body provided in an embodiment of the present application;
[0050] Fig.10 A schematic diagram of the structure of a structured query language generation device based on a relational database knowledge body provided in an embodiment of the present application;
[0051] Fig.11 A block diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0052] Embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present application, and should not be construed as limiting the present application.
[0053] The following describes the database knowledge body construction method and structured query language generation method, device and equipment of the embodiments of the present application with reference to the accompanying drawings.
[0054] Figure 1 A flowchart of a method for constructing a relational database knowledge body provided in an embodiment of the present application.
[0055] It should be noted that the executor of the method for constructing a relational database knowledge body in the embodiment of the present application is the device for constructing a relational database knowledge body in the embodiment of the present application. The device for constructing a relational database knowledge body can be configured in an electronic device so that the electronic device can execute the function of constructing a relational database knowledge body.
[0056] It should be noted that the embodiment of the present application proposes a relational database knowledge body for improving the TEXT2SQL generation capability of a large language model. The relational database knowledge body includes multiple knowledge body units, and the multiple knowledge body units are interconnected through associated fields to form a knowledge network; each knowledge body unit corresponds to a data table, and the knowledge body unit includes multi-layer node data, and the multi-layer node data includes table information, field information and field vectors, field enumeration value information and field enumeration value vector representation of the data table. The relational database knowledge body contains accurate and comprehensive data information required for text2sql reasoning of a large language model. In conjunction with the retrieval mechanism proposed later in this application, it can provide relatively comprehensive, accurate and low-redundancy prompt information for text2sql reasoning of a large language model, thereby improving the text2sql reasoning capability.
[0057] In some embodiments, the knowledge body unit includes seven layers of node data, the first layer of node data includes the data table name and the business meaning of the data table name, the second layer of node data includes the historical usage frequency of the data table, the third layer of node data includes the field names included in the data table and the business meaning of each field name, the fourth layer of node data includes the field index of each field, the fifth layer of node data includes the field vector of each field, the sixth layer of node data includes the field data type, field enumeration value and meaning list, field business description, field processing code snippet and field historical usage frequency, and the seventh layer of node data includes a field enumeration value vector representation list.
[0058] For example, Figure 2 , a knowledge unit of the relational database knowledge body corresponds to a data table. Each knowledge unit presents a tree structure in form. From the root to the top, it is divided into 7 layers, namely L1-data table (table) basic layer (where the node is the table name and the business meaning of the table), L2-data table (table) attribute layer (where the node is the historical usage frequency of the table), L3-field layer (where the node is the field name contained in the table and its business meaning), L4-field index layer (where the node is the index of the field), L5-field vector layer (where the node is the vector representation of the field), L6-field attribute layer (including 4 types of nodes: field data type, field enumeration value and meaning list, field business description, field processing code snippet, field historical usage frequency), L7-field enumeration value vector layer (where the internal node is the vector representation list of each enumeration value of the field (when the enumeration value is a meaningful string type such as 'Hebei Province') or its meaning (when the enumeration value is a numeric type or a meaningless string type such as 'None' 'Other', etc.)). Figure 2 The knowledge body units corresponding to the data table 1 and the data table 2 are shown respectively. The two knowledge body units are connected through the associated fields of the data table 1 and the data table 2 to form a knowledge body network.
[0059] The following is an explanation of the role of each layer node: L1, L2, L3, L6 nodes contain data table information as information carriers; L4 is a field unique index identifier used to distinguish fields; L5 and L7 are search entries, semantic matching retrieval is performed through vector similarity, and its vector can be generated through the embedding model. Among them, it is necessary to specially explain the content and role of the field code snippet in the L6 layer, which includes common processing code snippets for the field, such as distinct operation, cast data type conversion, data format conversion, etc. Its role is to provide field processing reference context for the reasoning process of text2sql and improve the detailed reasoning ability of text2sql.
[0060] In the database knowledge body, field code snippets are introduced as a knowledge node in the knowledge body. These code snippets contain necessary processing methods for the corresponding fields, such as cast data type conversion, data format conversion, distinct operation, etc. They provide necessary code detail references for LLM to generate SQL, so as to solve the problem that LLM cannot correctly process necessary field processing when generating SQL.
[0061] like Figure 1 As shown, the method for constructing the relational database knowledge body includes the following steps:
[0062] Step 101: Obtain first data table information based on data table metadata.
[0063] As an implementation method, first data table information is obtained based on data table metadata; including: based on the data table metadata, obtaining the data table name and the business meaning of the data table name, the field name, the field description, the field data type, the field enumeration value and the meaning of the field enumeration value, the table-related foreign key and the data table business description; quantifying the field enumeration value to obtain the field enumeration value vector; and forming the field enumeration value and the corresponding field enumeration value vector into a first data pair; forming the field name, the field description, and the data table business description into a second data pair, the second data pair being in the form of: field name-concat, wherein concat is represented as: data table business description, field description (that is, the second data pair is in the form of: field name-concat(data table business description, field description)).
[0064] Step 102, inferring and extracting the text-code pairing data set through a large language model to obtain second data table information; wherein the text-code pairing data set includes a plurality of pairing data consisting of question texts and corresponding SQL codes obtained from the database query history.
[0065] As an implementation method, a text-code pairing data set is inferred and extracted through a large language model to obtain second data table information; including: for each paired data in the text-code pairing data set, field meaning is inferred through the question text to obtain field meaning inference text; field names in the SQL code are extracted, and the extracted field names are matched with the field meaning inference text to form a third data pair; targeted processing of the field in the SQL code is identified and stored as a field processing code snippet of the field to form a fourth data pair; and related fields between different data tables in the SQL code are extracted.
[0066] Therefore, the database knowledge body of the embodiment of the present application not only has the basic metadata information of a conventional database knowledge graph or knowledge base, but also incorporates the precise and extensive knowledge and experience in historical manual queries, and makes necessary supplements and quality improvements to the database table metadata, such as more accurate and comprehensive field semantic information, table associations, frequently used data tables and fields, specific enumeration value usage scenarios, field processing methods, etc.
[0067] Step 103: merge the first data table information and the second data table information to obtain data table merge information.
[0068] As an implementation method, the first data table information and the second data table information are fused to obtain data table fusion information; including: merging the data pairs with the same field name of the second data pair and the third data pair to obtain a unique data pair of the field name; the unique data pair of the field name is in the form of: field name-description text list; calculating the average vector of the description text list in the unique data pair of the field name, using the average vector as the field vector of the field name and forming a fifth data pair, the fifth data pair is in the form of: field name-field vector; index encoding each field name.
[0069] Step 104, matching the data table fusion information with the multi-layer node data of the knowledge body unit to construct a relational database knowledge body.
[0070] That is to say, after obtaining the data of each layer of the knowledge body unit corresponding to each data table, the relational database knowledge body is constructed.
[0071] In order to clearly illustrate this embodiment, a specific example is now used for illustration. Figure 3 As shown, the method for constructing the knowledge body unit and knowledge network of the relational database knowledge body includes the following contents:
[0072] (1) Obtain the data table name and meaning, field name, field description, field data type, field enumeration value and meaning, and table-related foreign keys from the data table metadata;
[0073] (2) Obtain field enumeration values from the data table metadata and vectorize them using a suitable embedding model to construct (field enumeration value-field enumeration value vector) data pairs;
[0074] (3) Obtain the field name, field description, and data table business description from the data table metadata and form a (field name-concat (data table business description, field description)) data pair;
[0075] (4) From the reliable historical (question text-SQL code) paired data sets, traverse each paired data set and use LLM to perform the following tasks:
[0076] ① Infer the meaning of the field through the question text;
[0077] ②Extract the field name in the SQL code and match it with the field meaning inference result in ① to form a (field name-field meaning inference text) data pair;
[0078] ③ Identify the targeted processing of the field in the SQL code and store it as a code snippet of the field to form a (field name-field processing code snippet) data pair;
[0079] ④Extract the related fields between different tables in the SQL code.
[0080] (5) Merge the data pairs with the same field name (field name - concat (data table business description, field description)) in (3) and (field name - field meaning inference text) in (4)-② to form a unique data pair of (field name - description text list);
[0081] (6) vectorizing each element text in the description text list in the unique data pair of the field name in (5), and calculating the average vector of the corresponding description text list for each field name to obtain the vector representation of the field, forming a (field name-field vector) data pair;
[0082] (7) Count the usage frequency of each table and field from a reliable historical (question text-SQL code) paired data set;
[0083] (8) Index and encode each field, and based on the database knowledge body unit and network structure, combine the information obtained from steps (1), (2), (4)-③, (4)-④, and (6) to construct the database knowledge body and complete the construction.
[0084] It can be seen that the database knowledge body contains comprehensive information: in addition to the conventional database table metadata, it also contains reliable historical database table query queries and sql codes (historical (question text-SQL code) paired data sets). Among them, the database table metadata can provide L1 table name, L3 field name, field data type in L6, field enumeration value and meaning, field business description information, and some foreign key fields (associated fields) between different tables; reliable historical database table query queries and sql codes can provide field meaning reasoning from the user's perspective (implemented with LLM, the purpose is to align the field information in the user query query with the field information in the knowledge body), the number of historical usage of the data table in L2, the field processing code snippet and the number of historical field usage in L6, and more abundant information such as associated fields between different tables.
[0085] Through deep knowledge extraction and fusion of relational database data table metadata information and reliable historical database table query texts (such as "What is the sales volume this month?") and corresponding SQL codes accumulated in applications, combined with the characteristics of the large language model TEXT2SQL generation process, a relational database knowledge body was established. The application method of this knowledge body was also proposed later, and the accurate, comprehensive and concise information required in the large language model TEXT2SQL process was obtained to help LLM generate high-quality SQL.
[0086] The method for constructing the relational database knowledge body of this embodiment, based on the metadata of conventional data tables, extracts knowledge from the accumulated historical database table query data, and performs data fusion based on the database knowledge body structure proposed in this application, so that the database knowledge body not only has the basic metadata information of the conventional database knowledge graph or knowledge base, but also integrates the accurate and extensive knowledge and experience in historical manual queries, makes necessary supplements and improves the quality of the database table metadata, and obtains the relational database knowledge body used to enhance the TEXT2SQL generation capability of the large language model. In addition, accurate knowledge extraction of historical data can be performed automatically with the help of the powerful understanding ability of the large language model, which improves execution efficiency while minimizing the human resource consumption caused by manual participation.
[0087] The structure and construction method of the relational database knowledge body are described in detail above through the embodiments. The application method of the relational database knowledge body is described below.
[0088] Figure 4 A flow chart of a method for generating a structured query language based on a relational database knowledge body provided in an embodiment of the present application. The method for generating a structured query language based on a relational database knowledge body comprises the following steps:
[0089] Step 201, obtaining user questions.
[0090] Get the question text information entered by the user, for example, "Please calculate the sales of self-operated products in Beijing in 2023", to generate an SQL statement based on the user's question.
[0091] Step 202: Retrieve the relational database knowledge body based on the user question to obtain third data table information.
[0092] As an implementation method, the relational database knowledge body is retrieved based on the user question to obtain the third data table information; including the following contents:
[0093] Perform field reasoning and enumeration value extraction on user questions through a large language model to obtain at least one field to be searched and at least one enumeration value to be searched;
[0094] Performing vectorization processing on at least one to-be-searched field and at least one to-be-searched enumeration value respectively, to obtain a field vector representation of each to-be-searched field in the at least one to-be-searched field and an enumeration value vector representation of each to-be-searched enumeration value in the at least one to-be-searched enumeration value;
[0095] Based on the field vector representation of each to-be-searched field and the enumeration value vector representation of each to-be-searched enumeration value, the relevant fields of the relational database knowledge body are searched to obtain a first search result; the first search result includes a number of candidate fields corresponding to each to-be-searched field and related information thereof, the most matching enumeration value corresponding to each to-be-searched enumeration value, and a number of candidate fields corresponding to the most matching enumeration value and related information thereof;
[0096] Filter the first search result to obtain unique candidate fields for each field to be searched and related information thereof, and unique candidate fields for each enumeration value to be searched and related information thereof;
[0097] Based on the unique candidate field of each field to be searched, query the knowledge unit corresponding to the unique candidate field to obtain the supplementary information of each field to be searched;
[0098] Based on the unique candidate field of each enumeration value to be retrieved, query the knowledge body unit corresponding to the unique candidate field to obtain the supplementary information of each enumeration value to be retrieved;
[0099] The unique candidate fields of each to-be-searched field and the related information and supplementary information thereof, and the unique candidate fields of each to-be-searched enumeration value and the related information and supplementary information thereof are formatted to obtain the third data table information.
[0100] Further, based on the field vector representation of each to-be-searched field and the enumeration value vector representation of each to-be-searched enumeration value, the relevant fields of the relational database knowledge body are searched to obtain a first search result; including the following contents:
[0101] Based on the field vector representation of each field to be searched, the field vector of the relational database knowledge body is searched to obtain N preliminary similarity vectors similar to the field vector representation of each field to be searched;
[0102] The comprehensive score fi of each of the N preliminary similarity vectors is obtained by the first formula; wherein the first formula is expressed as follows:
[0103] fi=si*0.7+ui*0.3
[0104] Among them, si is the cosine similarity of the primary similarity vector normalized by the maximum and minimum values, and ui is the historical usage frequency of the primary similarity vector normalized by the maximum and minimum values;
[0105] Based on the comprehensive score of each of the N preliminary similarity vectors, several candidate fields and related information of each field to be searched are obtained;
[0106] Based on the enumeration value vector representation of each to-be-retrieved enumeration value, the field enumeration value vector representation of the relational database knowledge body is retrieved to obtain the target enumeration value vector representation that is most similar to the enumeration value vector representation of each to-be-retrieved enumeration value;
[0107] Obtain the target enumeration value name corresponding to the target enumeration value vector representation, and based on the target enumeration value name, obtain several candidate fields of each enumeration value to be retrieved and related information thereof;
[0108] A plurality of candidate fields of each to-be-searched field and related information thereof and a plurality of candidate fields of each to-be-searched enumeration value and related information thereof are deduplicated to obtain a first search result.
[0109] The embodiment of the present application uses a semantic vector as the retrieval entry node of the database knowledge body. On the one hand, the semantic vector comes from the actual search intent semantics in the historical manual query records and the business description text of the database metadata, which is consistent with the language habits and wording of the actual input search query text, fits the usage scenario, and makes the retrieval more accurate; on the other hand, the use of semantic retrieval can achieve accurate fuzzy retrieval, especially in the enumeration value retrieval scenario (such as the enumeration value specified in the query is 'Inner Mongolia Autonomous Region', and the enumeration value stored in the actual database is "Inner Mongolia"). In addition, the database knowledge body provides dual entries for field retrieval and enumeration value retrieval, making the retrieval results more comprehensive and accurate. At the same time, a two-stage retrieval of the database knowledge body is implemented. First, a rough check of vector similarity matching is performed, and after obtaining a number of candidate data, a secondary fine check is performed through comprehensive scoring to make the retrieval results more accurate.
[0110] Step 203: merge the user question and the third data table information to obtain data table merge information.
[0111] It can be understood that the user question is merged with the relevant information retrieved from the database knowledge body to obtain merged information.
[0112] Step 204: Add the data table merging information to the prompt words of the large language model, and input the prompt words into the large language model to generate a structured query language.
[0113] In order to clearly illustrate the application process of the relational database knowledge body of this embodiment, a specific example is now used for illustration. Figure 5 As shown, the structured query language generation method includes the following contents:
[0114] (1) After the user asks a question, LLM is used to perform field reasoning on the question and extract the given enumeration values, generating a list of fields to be searched (including multiple fields to be searched) and a list of enumeration values to be searched (including at least one enumeration value to be searched); Example 1: The user query is "Please calculate the sales of self-operated goods in Beijing in 2023". After LLM reasoning, the field information required to construct the SQL includes {sales date, region, sales type, sales amount}, and the given enumeration values include {Beijing, self-operated}.
[0115] (2) Each to-be-searched field and to-be-searched enumeration value obtained in (1) is vectorized using an embedding model;
[0116] (3) For each vector of the to-be-searched field and the to-be-searched enumeration value obtained in (2), the relevant fields are searched in the database knowledge body to obtain a number of candidate fields and their data table sets corresponding to each to-be-searched field, as well as the best matching enumeration value corresponding to each to-be-searched enumeration value and a number of candidate fields and their data table sets corresponding to the best matching enumeration value. For example: for the example to-be-searched field in (1), the field candidate set obtained after searching the database knowledge body is as follows:
[0117]
[0118]
[0119] (4) For the candidate set of fields and the candidate set of tables obtained in (3), the candidate data tables and fields are filtered based on the principle of the minimum full coverage table set for all fields to be searched, and the unique candidate field and data table for each field to be searched is obtained. For example, for the example candidate set in (3), the unique candidate field and data table for the field to be searched are obtained after filtering as follows:
[0120] {Best matching enumeration value: 'self-operated', field name: sale_type, field index: 30001, table name: t_1}, ]
[0122] }
[0123] (5) For the unique candidate field obtained in (4), the retrieved field and table association information are identified and supplemented in the database knowledge body through the field index, and the unique candidate field name, field meaning, field attribute, field enumeration value (and meaning), field processing code fragment, table name, and table association relationship of each to-be-retrieved field and to-be-retrieved enumeration value are obtained;
[0124] (6) The information retrieved in (5) is structured and organized to facilitate the understanding of the large model when generating SQL statements later. For example, the example in (4) may be supplemented and organized through (5) into the following form:
[0125] {
[0126] The required field information is as follows:
[0127] 1. Field name: soldate, field meaning: date, field data type: string, field-related operation code reference: [SUBSTRING(saledate,1,4)as year,SUBSTRING(saledate,1,4)='2021'], data table: t_1, field associated with data table t_3: company_id.
[0128] 2. Field name: city, field meaning: sales region, field data type: string, field filter enumeration value: 'Beijing City', data table: t_3, field associated with data table t_1: company_id.
[0129] 3. Field name: sale_type, field meaning: sales type, field data type: string, field filter enumeration value: 'self-operated', data table: t_1, field associated with data table t_3: company_id.
[0130] 4. Field name: sale, field meaning: sales amount, field data type: double, data table: t_1, field associated with data table t_3: company_id.
[0131] }
[0132] (7) The information sorted in (6) is combined with the user query as part of the prompt for the final input into the large model to generate the SQL code. At this point, the knowledge body information retrieval based on the database is completed.
[0133] The above-mentioned part (3): the database knowledge body retrieval process of searching the database knowledge body for the vectors of the fields to be retrieved and the enumeration values to be retrieved obtained in (2) for the relevant fields is described as follows:
[0134] The database knowledge body retrieval process is the process of searching for the most relevant content in the database knowledge body for the fields to be searched or the enumeration values to be searched. The basic principle is to use the vector representation of the fields to be searched or the enumeration values to be searched, and to search for the fields or enumeration values with the best semantic match in the database knowledge body according to the vector approximation principle, and then obtain other richer information about the relevant fields or enumeration values according to the data knowledge body. The database knowledge body retrieval process consists of the following steps: Figure 6 As shown, specifically:
[0135] (1) For an input list of field vectors to be searched, traverse each element in the list and execute steps (2) to (3);
[0136] (2) For each specific field vector to be searched, select the most similar Top-N candidate similar vectors according to the vector similarity principle, and obtain their field names and field indexes. The number of Top-N is determined by the following rule (Top-N truncation rule):
[0137] If [v1,v2,...,vn] is a set of candidate similar vectors, and its cosine similarity with the vector to be retrieved is [s1,s2,...,sn] (where s1>s2>...>sn), then the Top-N set is [s1,...,sm] (m<=n), where m satisfies the requirements s1-sm<=0.2 and s1-s(m+1)>0.2.
[0138] (3) Calculate the comprehensive score of each field in the result of (2), and obtain the top 5 field names and indexes, and field historical usage frequency information. The comprehensive score calculation rules are as follows:
[0139] Let the candidate similarity vector set be [v1, v2, ..., vm], where the cosine similarity of the i-th element after min-max normalization is si, and the historical usage frequency after min-max normalization is ui, then the comprehensive score of the similarity vector is fi = si*0.7+ui*0.3.
[0140] The min-max normalization formula is: (current value - minimum value of the set) / (maximum value of the set - minimum value of the set).
[0141] (4) For an input enumeration value vector list to be retrieved, traverse each element therein and execute steps (5) to (6);
[0142] (5) For each specific enumeration value vector to be retrieved, select the most similar Top-1 candidate similarity vector according to the vector similarity principle, and obtain its enumeration value name;
[0143] (6) Obtain relevant candidate fields according to the enumeration value name, and select the top 5 candidate field names, field indexes, and field historical usage frequencies from high to low according to historical usage frequencies;
[0144] (7) De-duplicate the search results of the to-be-searched field list and the to-be-searched enumeration value list (giving priority to the results of the to-be-searched enumeration value), and obtain the table name of the relevant field and the historical usage frequency information of the table. Return the corresponding search results.
[0145] Furthermore, in the process of retrieving the knowledge body in the database, directly related fields (or enumeration values) are found according to the vector similarity principle, and then the process of obtaining other related information is expanded as follows: Figure 7 As shown, the following steps are included:
[0146] (1) Retrieve and match the target vector based on the vector cosine similarity;
[0147] (2) Along the knowledge unit, the target field or enumeration value is obtained from the target vector;
[0148] (3) Acquire the target data table from the target field along the knowledge unit;
[0149] (4) Connect along the knowledge body network and obtain the associated fields of the target data table;
[0150] (5) Along the knowledge unit, obtain the relevant attributes of each target field.
[0151] The following is an explanation of the candidate data tables and field screening based on the above-mentioned principle of the minimum full coverage table set of the fields to be retrieved:
[0152] Because in the above search process, the Top-N candidate results are retained to varying degrees for each search item, so the candidate set of fields and tables after direct search contains a lot of redundant information with repeated functions. In order to make the output more concise and accurate, the "minimum full coverage table set principle for the fields to be searched" is used to select the fields and table sets with the simplest relationship between data tables and the highest actual usage frequency.
[0153] The principle of the minimum set of tables that fully cover the fields to be searched requires that the selected data table set should be able to cover all the fields to be searched and contain the smallest number of data tables. If there are multiple data table sets that meet this principle, first sum the highest historical usage frequencies of each field to be searched in each set, then select the largest set of the summed results as the optimal set, and in this optimal set, retain the field with the highest historical usage frequency for each field to be searched as the optimal field. The principle of selecting the minimum set of data tables and fields that meets the principle of the minimum set of tables that fully cover the fields to be searched is as follows: Figure 8 shown.
[0154] Therefore, after the user raises a query, before the query is sent to LLM for SQL generation, relevant information is first retrieved from the database knowledge body based on the user query to obtain the data table, data field, field attribute, field enumeration value, field processing code snippet and table-related fields required for SQL generation, so as to provide complete and accurate basic data for LLM to generate SQL and input it into LLM for SQL generation.
[0155] It should be noted that after the relevant information retrieved from the database knowledge body based on the user query is merged with the user query, it can also be combined with other techniques to improve the accuracy of LLM reasoning and input into LLM for SQL generation.
[0156] The structured query language generation method based on relational database knowledge body in the embodiment of the present application, after obtaining user questions, retrieves the relational database knowledge body based on the user questions, obtains the highly accurate and comprehensive data information required for LLM to generate SQL, and provides refined, low-redundant, accurate, comprehensive and rich dependency data for the TEXT2SQL task of the large language model, thereby improving the TEXT2SQL generation capability of the large language model and improving the TEXT2SQL generation quality.
[0157] In order to implement the above-mentioned relational database knowledge body construction method embodiment, the present application also proposes a relational database knowledge body construction device. Fig. 9 A schematic diagram of a structure of a relational database knowledge body construction device provided in an embodiment of the present application. The relational database knowledge body includes multiple knowledge body units, which are interconnected through associated fields to form a knowledge network; each knowledge body unit corresponds to a data table, and the knowledge body unit includes multi-layer node data, and the multi-layer node data includes table information, field information and field vectors, field enumeration value information and field enumeration value vector representation of the data table, such as Fig. 9 As shown, the apparatus for constructing a relational database knowledge body may include: an information acquisition module 301 , an information extraction module 302 , an information fusion module 303 , and a knowledge body construction module 304 .
[0158] The information acquisition module 301 is used to acquire first data table information based on data table metadata;
[0159] The information extraction module 302 is used to infer and extract the text-code pairing data set through the large language model to obtain the second data table information; wherein the text-code pairing data set includes a plurality of pairing data consisting of question texts and corresponding SQL codes obtained from the database query history;
[0160] An information fusion module 303 is used to fuse the first data table information and the second data table information to obtain data table fusion information;
[0161] The knowledge body construction module 304 is used to match the data table fusion information with the multi-layer node data of the knowledge body unit to construct a relational database knowledge body.
[0162] Furthermore, in a possible implementation of an embodiment of the present application, the knowledge body unit includes seven layers of node data, the first layer of node data includes the data table name and the business meaning of the data table name, the second layer of node data includes the historical usage frequency of the data table, the third layer of node data includes the field names included in the data table and the business meaning of each field name, the fourth layer of node data includes the field index of each field, the fifth layer of node data includes the field vector of each field, the sixth layer of node data includes the field data type, field enumeration value and meaning list, field business description, field processing code snippet and field historical usage frequency, and the seventh layer of node data includes a field enumeration value vector representation list.
[0163] Furthermore, in a possible implementation of the embodiment of the present application, the information acquisition module 301 is specifically used to:
[0164] Based on the data table metadata, obtain the data table name and its business meaning, field name, field description, field data type, field enumeration value and its meaning, table-related foreign keys, and data table business description;
[0165] Quantifying the field enumeration value to obtain a field enumeration value vector; and forming a first data pair with the field enumeration value and the corresponding field enumeration value vector;
[0166] The field name, field description, and data table business description are combined into a second data pair, and the second data pair is in the form of: field name-concat, where concat represents: data table business description, field description.
[0167] Furthermore, in a possible implementation of the embodiment of the present application, the information extraction module 302 is specifically configured to:
[0168] For each paired data in the text-code paired data set, the field meaning is inferred through the question text to obtain the field meaning inference text;
[0169] Extracting the field names in the SQL code, and matching the extracted field names with the field meaning inference text to form a third data pair;
[0170] Identify the targeted processing performed on the field in the SQL code, and store it as a field processing code snippet of the field to form a fourth data pair;
[0171] Extract the related fields between different data tables in SQL code.
[0172] Furthermore, in a possible implementation of the embodiment of the present application, the information fusion module 303 is specifically configured to:
[0173] The data pairs with the same field name of the second data pair and the third data pair are merged to obtain a unique data pair of the field name; the unique data pair of the field name is in the form of: field name-description text list;
[0174] Calculate the average vector of the description text list in the unique data pair of the field name, use the average vector as the field vector of the field name and form a fifth data pair, the form of the fifth data pair is: field name-field vector;
[0175] Index each field name.
[0176] It should be noted that the above explanation of the embodiment of the method for constructing a relational database knowledge body is also applicable to the apparatus for constructing a relational database knowledge body of this embodiment, and will not be repeated here.
[0177] In order to implement the above-mentioned structured query language generation method embodiment based on relational database knowledge body, the present application also proposes a structured query language generation device based on relational database knowledge body. Fig.10 The schematic diagram of the structure of a structured query language generation device based on a relational database knowledge body provided in an embodiment of the present application. The relational database knowledge body is the relational database knowledge body provided in any of the above method embodiments, such as Fig.10 As shown, the structured query language generation device based on relational database knowledge body may include: a question text acquisition module 401, a knowledge retrieval module 402, an information merging module 403 and a language generation module 404.
[0178] The question text acquisition module 401 is used to acquire user questions;
[0179] The knowledge retrieval module 402 is used to retrieve the relational database knowledge body based on the user question to obtain the third data table information;
[0180] An information merging module 403 is used to merge the user question with the third data table information to obtain data table merged information;
[0181] The language generation module 404 is used to add the data table merging information to the prompt words of the large language model, and input the prompt words into the large language model to generate a structured query language.
[0182] Furthermore, in a possible implementation of the embodiment of the present application, the knowledge retrieval module 402 is specifically configured to:
[0183] Perform field reasoning and enumeration value extraction on user questions through a large language model to obtain at least one field to be searched and at least one enumeration value to be searched;
[0184] Performing vectorization processing on at least one to-be-searched field and at least one to-be-searched enumeration value respectively, to obtain a field vector representation of each to-be-searched field in the at least one to-be-searched field and an enumeration value vector representation of each to-be-searched enumeration value in the at least one to-be-searched enumeration value;
[0185] Based on the field vector representation of each to-be-searched field and the enumeration value vector representation of each to-be-searched enumeration value, the relevant fields of the relational database knowledge body are searched to obtain a first search result; the first search result includes a number of candidate fields corresponding to each to-be-searched field and related information thereof, the most matching enumeration value corresponding to each to-be-searched enumeration value, and a number of candidate fields corresponding to the most matching enumeration value and related information thereof;
[0186] Filter the first search result to obtain unique candidate fields for each field to be searched and related information thereof, and unique candidate fields for each enumeration value to be searched and related information thereof;
[0187] Based on the unique candidate field of each field to be searched, query the knowledge unit corresponding to the unique candidate field to obtain the supplementary information of each field to be searched;
[0188] Based on the unique candidate field of each enumeration value to be retrieved, query the knowledge body unit corresponding to the unique candidate field to obtain the supplementary information of each enumeration value to be retrieved;
[0189] The unique candidate fields of each to-be-searched field and the related information and supplementary information thereof, and the unique candidate fields of each to-be-searched enumeration value and the related information and supplementary information thereof are formatted to obtain the third data table information.
[0190] Further, in a possible implementation of the embodiment of the present application, the knowledge retrieval module 402 retrieves the relevant fields of the relational database knowledge body based on the field vector representation of each field to be retrieved and the enumeration value vector representation of each enumeration value to be retrieved, and obtains the first search result; specifically, it is used to:
[0191] Based on the field vector representation of each field to be searched, the field vector of the relational database knowledge body is searched to obtain N preliminary similarity vectors similar to the field vector representation of each field to be searched;
[0192] The comprehensive score fi of each of the N preliminary similarity vectors is obtained by the first formula; wherein the first formula is expressed as follows:
[0193] fi=si*0.7+ui*0.3
[0194] Among them, si is the cosine similarity of the primary similarity vector normalized by the maximum and minimum values, and ui is the historical usage frequency of the primary similarity vector normalized by the maximum and minimum values;
[0195] Based on the comprehensive score of each of the N preliminary similarity vectors, several candidate fields and related information of each field to be searched are obtained;
[0196] Based on the enumeration value vector representation of each to-be-retrieved enumeration value, the field enumeration value vector representation of the relational database knowledge body is retrieved to obtain the target enumeration value vector representation that is most similar to the enumeration value vector representation of each to-be-retrieved enumeration value;
[0197] Obtain the target enumeration value name corresponding to the target enumeration value vector representation, and based on the target enumeration value name, obtain several candidate fields of each enumeration value to be retrieved and related information thereof;
[0198] A plurality of candidate fields of each to-be-searched field and related information thereof and a plurality of candidate fields of each to-be-searched enumeration value and related information thereof are deduplicated to obtain a first search result.
[0199] It should be noted that the above explanation of the embodiment of the structured query language generation method based on a relational database knowledge body is also applicable to the structured query language generation device based on a relational database knowledge body of this embodiment, and will not be repeated here.
[0200] In order to implement the above embodiment, the present application also proposes an electronic device. Fig.11 , Fig.11 Schematic diagram of the structure of the electronic device provided in the embodiment of the present application. Fig.11 As shown, the electronic device 1100 includes: a processor 1101, and a memory 1102 communicatively connected to the processor 1101; the memory 1102 stores computer-executable instructions; the processor 1101 executes the computer-executable instructions stored in the memory to implement the method provided in the aforementioned embodiment.
[0201] In order to implement the above embodiments, the present application also proposes a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, they are used to implement the methods provided by the above embodiments.
[0202] In order to implement the above embodiments, the present application also proposes a computer program product, including a computer program, which implements the methods provided by the above embodiments when executed by a processor.
[0203] The collection, storage, use, processing, transmission, provision and disclosure of user personal information involved in this application are in compliance with relevant laws and regulations and do not violate public order and good morals.
[0204] It should be noted that personal information from users should be collected for legitimate and reasonable purposes and should not be shared or sold outside of these legitimate uses. In addition, such collection / sharing should be carried out after receiving the user's informed consent, including but not limited to notifying the user to read the user agreement / user notice and sign the agreement / authorization including authorization of relevant user information before the user uses the function. In addition, any necessary steps should be taken to protect and safeguard access to such personal information data and ensure that others who have access to personal information data comply with its privacy policy and procedures.
[0205] The present application is expected to provide an implementation scheme for users to selectively block the use or access of personal information data. That is, the present disclosure is expected to provide hardware and / or software to prevent or block access to such personal information data. Once the personal information data is no longer needed, the risk can be minimized by limiting data collection and deleting the data. In addition, when applicable, such personal information is de-identified to protect the privacy of the user.
[0206] In the description of the aforementioned embodiments, the description with reference to the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present application. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine the different embodiments or examples described in this specification and the features of the different embodiments or examples, without contradiction.
[0207] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In the description of this application, the meaning of "plurality" is at least two, such as two, three, etc., unless otherwise clearly and specifically defined.
[0208] Any process or method description in a flowchart or otherwise described herein may be understood to represent a module, fragment or portion of code comprising one or more executable instructions for implementing the steps of a custom logical function or process, and the scope of the preferred embodiments of the present application includes alternative implementations in which functions may not be performed in the order shown or discussed, including performing functions in a substantially simultaneous manner or in the reverse order depending on the functions involved, which should be understood by technicians in the technical field to which the embodiments of the present application belong.
[0209] The logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as an ordered list of executable instructions for implementing logical functions, and can be embodied in any computer-readable medium for use by an instruction execution system, device or apparatus (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or apparatus and execute the instructions), or in combination with these instruction execution systems, devices or apparatuses. For the purpose of this specification, "computer-readable medium" can be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, device or apparatus, or in combination with these instruction execution systems, devices or apparatuses. More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection with one or more wires (electronic device), a portable computer disk box (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and programmable read-only memory (EPROM or flash memory), a fiber optic device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium and then editing, interpreting or processing in other suitable ways if necessary, and then stored in a computer memory.
[0210] It should be understood that the various parts of the present application can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, multiple steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.
[0211] A person skilled in the art may understand that all or part of the steps in the method for implementing the above-mentioned embodiment may be completed by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, which, when executed, includes one or a combination of the steps of the method embodiment.
[0212] In addition, each functional unit in each embodiment of the present application may be integrated into a processing module, or each unit may exist physically separately, or two or more units may be integrated into one module. The above-mentioned integrated module may be implemented in the form of hardware or in the form of a software functional module. If the integrated module is implemented in the form of a software functional module and sold or used as an independent product, it may also be stored in a computer-readable storage medium.
[0213] The storage medium mentioned above may be a read-only memory, a magnetic disk or an optical disk, etc. Although the embodiments of the present application have been shown and described above, it can be understood that the above embodiments are exemplary and cannot be understood as limiting the present application. A person of ordinary skill in the art may change, modify, replace and modify the above embodiments within the scope of the present application.
Claims
1. A method for constructing a relational database knowledge body, characterized in that: The relational database knowledge body includes a plurality of knowledge body units, which are interconnected through associated fields to form a knowledge network; each of the knowledge body units corresponds to a data table, and the knowledge body unit includes multi-layer node data, and the multi-layer node data includes table information, field information and field vector, field enumeration value information and field enumeration value vector representation of the data table; the construction method includes the following steps: Based on the data table metadata, obtaining first data table information; Inferring and extracting the text-code pairing data set through a large language model to obtain second data table information; wherein the text-code pairing data set includes a plurality of pairing data consisting of question texts and corresponding SQL codes obtained from a database query history; Merging the first data table information and the second data table information to obtain data table fusion information; The data table fusion information is matched with the multi-layer node data of the knowledge body unit to construct a relational database knowledge body.
2. The method according to claim 1, characterized in that The knowledge body unit includes seven layers of node data, the first layer of node data includes the data table name and the business meaning of the data table name, the second layer of node data includes the historical usage frequency of the data table, the third layer of node data includes the field names included in the data table and the business meaning of each field name, the fourth layer of node data includes the field index of each field, the fifth layer of node data includes the field vector of each field, the sixth layer of node data includes the field data type, field enumeration value and meaning list, field business description, field processing code snippet and field historical usage frequency, and the seventh layer of node data includes a field enumeration value vector representation list.
3. The method according to claim 1, characterized in that The step of obtaining first data table information based on data table metadata includes: Based on the data table metadata, obtain the data table name and its business meaning, field name, field description, field data type, field enumeration value and its meaning, table-related foreign keys, and data table business description; Quantizing the field enumeration value to obtain a field enumeration value vector; and forming a first data pair with the field enumeration value and the corresponding field enumeration value vector; The field name, field description, and data table business description are combined into a second data pair, the second data pair being in the form of: field name-concat, wherein concat represents: data table business description, field description.
4. The method according to claim 3, characterized in that The method of inferring and extracting the text-code pairing data set through the large language model to obtain the second data table information includes: For each paired data in the text-code paired data set, field meaning inference is performed through the question text to obtain field meaning inference text; Extracting field names in the SQL code, and matching the extracted field names with the field meaning inference text to form a third data pair; Identify the targeted processing performed on the field in the SQL code, and store it as a field processing code snippet of the field to form a fourth data pair; Extract the related fields between different data tables in SQL code.
5. The method according to claim 4, characterized in that The fusing the first data table information and the second data table information to obtain data table fusion information comprises: Merge the data pairs with the same field name of the second data pair and the third data pair to obtain a unique data pair with the field name; the unique data pair with the field name is in the form of: field name-description text list; Calculate the average vector of the description text list in the unique data pair of the field name, use the average vector as the field vector of the field name and form a fifth data pair, the fifth data pair is in the form of: field name-field vector; Index each field name.
6. A method for generating structured query language based on relational database knowledge body, characterized in that: The relational database knowledge body is a relational database knowledge body constructed by the relational database knowledge body construction method according to any one of claims 1 to 5; the structured query language generation method comprises the following steps: Get user questions; Retrieving the relational database knowledge body based on the user question to obtain third data table information; Merging the user question with the third data table information to obtain data table merge information; The data table merging information is added to the prompt words of the large language model, and the prompt words are input into the large language model to generate a structured query language.
7. The method according to claim 6, characterized in that The step of retrieving the relational database knowledge body based on the user question to obtain third data table information comprises: Performing field reasoning and enumeration value extraction on the user question through a large language model to obtain at least one field to be searched and at least one enumeration value to be searched; Performing vectorization processing on the at least one to-be-searched field and the at least one to-be-searched enumeration value respectively, to obtain a field vector representation of each to-be-searched field in the at least one to-be-searched field and an enumeration value vector representation of each to-be-searched enumeration value in the at least one to-be-searched enumeration value; Based on the field vector representation of each field to be searched and the enumeration value vector representation of each enumeration value to be searched, the relevant fields of the relational database knowledge body are searched to obtain a first search result; the first search result includes a number of candidate fields corresponding to each field to be searched and related information thereof, the most matching enumeration value corresponding to each enumeration value to be searched, and a number of candidate fields corresponding to the most matching enumeration value and related information thereof; Filter the first search results to obtain unique candidate fields for each field to be searched and related information thereof, and unique candidate fields for each enumeration value to be searched and related information thereof; Based on the unique candidate field of each field to be searched, query the knowledge unit corresponding to the unique candidate field to obtain the supplementary information of each field to be searched; Based on the unique candidate field of each enumeration value to be retrieved, query the knowledge body unit corresponding to the unique candidate field to obtain the supplementary information of each enumeration value to be retrieved; The unique candidate fields of the fields to be searched and their related information and supplementary information, and the unique candidate fields of the enumeration values to be searched and their related information and supplementary information are formatted to obtain third data table information.
8. The method according to claim 7, characterized in that The step of retrieving the relevant fields of the relational database knowledge body based on the field vector representation of each field to be retrieved and the enumeration value vector representation of each enumeration value to be retrieved to obtain a first search result comprises: Based on the field vector representation of each field to be searched, the field vector of the relational database knowledge body is searched to obtain N preliminary similarity vectors similar to the field vector representation of each field to be searched; The comprehensive score fi of each of the N preliminary similarity vectors is obtained by the first formula; wherein the first formula is expressed as follows: fi=si*0.7+ui*0.3 Among them, si is the cosine similarity of the primary similarity vector normalized by the maximum and minimum values, and ui is the historical usage frequency of the primary similarity vector normalized by the maximum and minimum values; Based on the comprehensive score of each of the N preliminary similarity vectors, obtaining a number of candidate fields and related information of each of the fields to be searched; Based on the enumeration value vector representations of the enumeration values to be retrieved, the field enumeration value vector representations of the relational database knowledge body are retrieved to obtain target enumeration value vector representations that are most similar to the enumeration value vector representations of the enumeration values to be retrieved; Obtaining a target enumeration value name corresponding to the target enumeration value vector representation, and based on the target enumeration value name, obtaining a number of candidate fields of each to-be-retrieved enumeration value and related information thereof; Deduplication processing is performed on the candidate fields of each to-be-searched field and related information thereof and the candidate fields of each to-be-searched enumeration value and related information thereof to obtain a first search result.
9. A device for constructing a relational database knowledge body, characterized in that: The relational database knowledge body includes a plurality of knowledge body units, which are interconnected through associated fields to form a knowledge network; each of the knowledge body units corresponds to a data table, and the knowledge body unit includes multi-layer node data, and the multi-layer node data includes table information, field information and field vector, field enumeration value information and field enumeration value vector representation of the data table; the device includes: An information acquisition module, used for acquiring first data table information based on data table metadata; An information extraction module, used to infer and extract the text-code pairing data set through a large language model to obtain second data table information; wherein the text-code pairing data set includes a plurality of pairing data consisting of question texts and corresponding SQL codes obtained from the database query history; An information fusion module, used for fusing the first data table information and the second data table information to obtain data table fusion information; The knowledge body construction module is used to match the data table fusion information with the multi-layer node data of the knowledge body unit to construct a relational database knowledge body.
10. A structured query language generation device based on a relational database knowledge body, characterized in that: The relational database knowledge body is a relational database knowledge body constructed by the relational database knowledge body construction method according to any one of claims 1 to 5; the structured query language generation device comprises: Question text acquisition module, used to obtain user questions; A knowledge retrieval module, used to retrieve the relational database knowledge body based on the user question to obtain third data table information; An information merging module, used for merging the user question with the third data table information to obtain data table merged information; The language generation module is used to add the data table merging information to the prompt words of the large language model, and input the prompt words into the large language model to generate a structured query language.
Citation Information
Cited By
Construction method of table knowledge base, query method of data table and related devices
CN120409643A
Retrieval enhancement mode linking method and device based on multi-round iteration
CN120523827A
Retrieval enhancement mode linking method and device based on multiple iterations
CN120523827B
Digital main line automatic modeling method and system based on large language model
CN120523828A
Metadata and metadata relationship automatic extraction method, system and product
CN121144577A