NL2SQL problem enhancement method driven by multivariate knowledge link
Through the NL2SQL problem enhancement method driven by multi-knowledge links, the problem of insufficient natural language description in the power field is solved, the accuracy and reliability of SQL generation is improved, and the field specificity and database matching effect is achieved.
Patent Information
- Application Number
- CN202510237478.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-03
- Publication Date
- 2025-04-01
- Estimated Expiration
- 2045-03-03
AI Technical Summary
The existing NL2SQL technology cannot be effective and practical in the power field. The main reason is that the problem of natural language description is not enough to allow the big model to accurately understand the query intentions of business personnel, resulting in low accuracy of generated SQL.
A multi-knowledge link-driven NL2SQL problem enhancement method is proposed. By constructing the relationship knowledge of power data mode and business-mode mapping, combining the large language model to analyze the original problem structure, using mixed similarity search and multi-level mode linking methods, the relevant database tables and data patterns are obtained, and knowledge standardization and problem enhancement are carried out to enhance the understanding of the power field knowledge of the large language model.
It improves the accuracy and reliability of SQL generation in large-scale models, significantly reduces ambiguity and ambiguity, enhances domain specificity, and enables users' natural language data queries to better match power database tables.
Smart Images

Figure CN119739837A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of electric power natural language data question answering, and specifically relates to a NL2SQL (Natural Language to SQL, natural language converted to structured query language) question enhancement method driven by multiple knowledge links. Background Art
[0002] The current situation that business personnel in the power sector need to rely on the structured query language SQL written by system developers to access data has greatly limited the value of power data. Recently, the NL2SQL technology, which converts natural language into the structured query language SQL that the database can understand based on the big language model, has made great progress, but this technology is still not effective in the power sector. One important reason is that the questions described in natural language are not enough for the big model to accurately understand the query intentions of business personnel: First of all, the power business is highly professional, and general large language models cannot effectively understand the various terms in the power field. They cannot fully understand the data query requirements expressed in business terms and thus generate erroneous data query results.
[0003] Secondly, for business personnel who are not familiar with database architecture and SQL language, the information they express in natural language for data query needs is often insufficient, and the large language model cannot obtain enough information to effectively map query requirements with data entities, and thus cannot generate correct SQL.
[0004] Third, business personnel are accustomed to asking short data questions based on the business knowledge they have mastered. However, the large language model cannot understand the complex business knowledge hidden behind these questions through literal descriptions, and thus cannot correctly understand the query requirements, which ultimately leads to the accuracy of the generated SQL failing to meet the requirements.
[0005] Finally, the complexity of the power business and the diversity of the system lead to an extremely complex data structure. There are a large number of database tables and there is a high degree of similarity between table names and field names. This makes it very easy for large language models to confuse data entities when generating SQL, resulting in low accuracy of the generated SQL. Summary of the invention
[0006] In order to overcome the problems in the prior art, the present invention proposes a NL2SQL question enhancement method driven by multiple knowledge links.
[0007] The technical solution of the present invention to solve the above technical problems is as follows: The present invention provides a multi-knowledge link driven NL2SQL problem enhancement method, comprising the following steps: Step 100: constructing a power data model using database table information, and sorting out power domain knowledge, wherein the power domain knowledge includes business-model mapping relationship knowledge and data encoding relationship knowledge; Step 200: construct a question parsing prompt template, and use a large language model to analyze the structure of the original question and extract key entities from the original question; Step 300: Based on the sorted out power domain knowledge and the key entities extracted from the original question, the power domain knowledge is retrieved by using a hybrid similarity retrieval method; Step 400: Obtain the database table and data schema related to the original question through a multi-level schema linking method; Step 500: Based on the retrieved power field knowledge and the acquired data model, knowledge standardization is performed, and a question enhancement prompt template is designed, and the original question is reconstructed and enhanced using a large language model.
[0008] Furthermore, in step 100, constructing the power data model includes: The data definition language DDL of all power business-related database tables is extracted from the power database, where each DDL contains the creation information of the database table. The required database table information is extracted by parsing the DDL; the data example content is collected for each database table; the database table information and data examples extracted from the database are reorganized to build a data model.
[0009] Furthermore, in step 100, the business-mode mapping relationship knowledge includes: Organize business terms in the power field and form a correspondence between the business terms and data models, that is, a business-model mapping relationship; Extract business term features, including vectorized business term names and business term names n - gram feature; Store all business-mode mapping relationship knowledge in JSON files; store all vectorized business term name knowledge in vector databases; n - gram Feature knowledge is stored in the ES database.
[0010] Furthermore, in step 100, the data encoding relationship knowledge includes: Extract the required information from the table storing the coding correspondence in the database, including the coding name, coding value and corresponding column name; For each encoding relationship, extract its vector features and n - gramFeatures, and store the extracted features in the encoding relationship vector database and the encoding relationship ES database.
[0011] Furthermore, in step 200, a question resolution prompt template is constructed, and the question resolution prompt template is defined as a tuple ,include: ; in, G Indicates the defined task role goal, that is, to give the large language model a task-related role and clarify the task goal; I Indicates important reminders, including important tips and constraints when completing tasks; O Indicates the output format of the task; C Indicates the reference output case provided; Q Represents the original question of the input.
[0012] Furthermore, in step 300, the hybrid similarity retrieval method for retrieving power domain knowledge includes using a method of fusing vector similarity and BM25 similarity to jointly retrieve business-mode mapping relationship knowledge: Calculate the cosine similarity between the entity and the business term to retrieve the business term name with similar semantics to the entity in the vector database; Use BM25 similarity to search for business term names with the same keyword fragments in the ES database; According to the vector cosine similarity score, the most relevant business term name for the key entity is obtained in the vector database; according to the BM25 similarity score, the most relevant business term name is also obtained in the ES database; the two business term names are merged as the business term name knowledge set of the entity.
[0013] Furthermore, in step 300, the hybrid similarity retrieval method for retrieving power domain knowledge includes using a method of fusing vector similarity and BM25 similarity to jointly retrieve coding relationship knowledge: The cosine similarity and BM25 similarity methods are used to calculate the similarity of the content of each key entity in the encoding relationship vector library and the encoding relationship ES library, and obtain the encoding relationship knowledge set corresponding to all key entities.
[0014] Furthermore, in step 400, the database table and data schema related to the original question are obtained through a multi-level schema linking method, including: A fast linking method based on double hash value search links the extracted original question entities with the tables and fields in the database respectively to obtain the database tables and data models related to the original question.
[0015] Furthermore, in step 500, performing knowledge standardization includes: Based on the coding relationship knowledge retrieval results, the corresponding coding column names are extracted according to their organizational form, and the coding relationships corresponding to the column names that are not in the relevant data model are removed; and the final representation of the data model, data coding mapping relationship and business-model mapping relationship is standardized and designed.
[0016] Furthermore, in step 500, the question enhancement prompt template is defined as P G ,include: ; in, G Indicates the defined task role goal, that is, to give the large language model a task-related role and clarify the task goal; The formatted data mode content; EK For formatted coded relational knowledge content; T K It is the formatted business-pattern mapping relationship knowledge content; W represents important reminders, including important tips and constraints when completing tasks; O Indicates the output format of the task; Q Represents the original input question; Q A Parse the content for the obtained question.
[0017] Compared with the prior art, the present invention has the following technical effects: (1) The present invention aims to improve the accuracy of SQL generated by the large model through the following steps: first, by parsing the original question, the key entities and the original question structure information in the original question are obtained; then, the business knowledge related to the key entities is retrieved using the hybrid similarity retrieval technology; then, the relevant table information knowledge is obtained through the pattern linking method. On this basis, through knowledge standardization and question enhancement prompt design, the large language model's understanding of the knowledge in the power field is enhanced, and finally a question with clear expression and clear logic is generated, which effectively eliminates confusion and interference factors, better matches the power database structure, and thus improves the accuracy and reliability of SQL queries.
[0018] (2) The present invention proposes a method for constructing data models and business-model mapping relationships only for the power sector. The data model involves database table structure analysis, data example extraction, and model organization, ensuring the clarity and ease of use of database table knowledge; the business-model mapping relationship involves the organization of common businesses and explanations, the organization and storage of business, database tables, and fields, and other content. This content not only provides a detailed description of the power business, but also clarifies the relationship between the business and specific tables and fields, providing the necessary knowledge support for the positioning of tables in the power sector and problem enhancement.
[0019] (3) The present invention proposes a multi-level pattern linking method. On the one hand, it adopts a data pattern linking method based on double hash value search, combined with the key entity extraction results in the original question, and through a double search strategy of tables and fields, it can quickly and accurately match the user's natural language question with the most relevant table in the database. This method improves the accuracy of pattern linking and greatly reduces redundant content. On the other hand, based on the business-pattern mapping relationship knowledge obtained by retrieving the key entities of the original question, the association logic between the relevant business and the library table in the user's question is obtained, and a more accurate positioning result is provided in the form of associating business with the library table. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the technical solutions and advantages in the embodiments of the present invention or the prior art, the drawings required for use in the embodiments or the prior art descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0021] Figure 1 Enhancement method flow chart for the problem raised by the present invention; Figure 2 A schematic diagram of the data model and business knowledge content constructed for the present invention organization; Figure 3 The specific results of the semantic parsing step of the original question in the experiment; Figure 4 Provides specific results for the entity-based business knowledge linking steps in the experiment; Figure 5 The specific results of double hash value search and table filtering in the experiment; Figure 6 Provide specific results for the problem-solving steps in the experiment; Figure 7 This is a comparison chart of the effects of generating SQL results in the experiment; Figure 8 Shows the specific content of the problem resolution prompt template; Fig. 9Shows the specific content of the library table filtering prompt template; Fig.10 Shows the specific content of the question-enhanced prompt template. DETAILED DESCRIPTION
[0022] In order to further explain the technical means and effects taken by the present invention to achieve the predetermined invention purpose, the specific implementation methods, structures, features and effects of the technical solutions proposed by the present invention are described in detail below in conjunction with the accompanying drawings and preferred embodiments. The specific features, structures or characteristics in one or more embodiments may be combined in any suitable form. Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by technicians in the technical field of the present invention.
[0023] The present invention proposes a NL2SQL question enhancement method driven by multiple knowledge links, which is used to improve the accuracy of business personnel in the power field when using natural language to achieve reliable data access. The present invention obtains necessary knowledge through a series of key steps, including power data model construction and business knowledge combing, original problem semantic analysis, entity-based business knowledge linking, multi-level data model linking and other strategies, and optimizes and enhances the expression and structure of the natural language demands of business personnel to query data in combination with a large language model. The enhanced user data query demands enhance the domain specificity, significantly reduce fuzziness and ambiguity, and increase the data model mapping of the business background so that it can better match the power database table. In other words, the core creativity of the present invention lies in enhancing and reconstructing the natural language data query of business personnel through multiple knowledge links, converting the brief and vague query expressions of business personnel into a form that is more in line with business logic and data models, and eliminating confusion and interference factors, so that the large model can accurately understand the data query demands of business personnel, thereby improving the quality of generated SQL and ultimately obtaining data that meets the needs.
[0024] In one embodiment of the present invention, referring to Figure 1 , provides a multi-knowledge link driven NL2SQL question enhancement method, including the following steps: Step 100: constructing a power data model using database table information, and sorting out power domain knowledge, wherein the power domain knowledge includes business-model mapping relationship knowledge and data encoding relationship knowledge; Step 200: construct a question parsing prompt template, and use a large language model to analyze the structure of the original question to extract key entities from the original question; Step 300: Based on the sorted out power domain knowledge and the key entities extracted from the original question, the power domain knowledge is retrieved by using a hybrid similarity retrieval method; Step 400: Obtain the database table and data schema related to the original question through a multi-level schema linking method; Step 500: Based on the retrieved power field knowledge and the acquired data model, knowledge standardization is performed; and a question enhancement prompt template is designed, and the original question is reconstructed and enhanced using a large language model.
[0025] The following is a detailed explanation of each of the above steps: Step 100: construct an electric power data model and sort out electric power domain knowledge, wherein the electric power domain knowledge includes business-model mapping relationship knowledge and data encoding relationship knowledge.
[0026] This embodiment analyzes the characteristics of power data query in business term description and database design mode, uses database table information to build power data mode, and sorts out field knowledge such as business-mode mapping relationship and data encoding relationship. In addition, a unique knowledge organization form is designed according to different contents, and finally the knowledge is stored in a database or JSON (JavaScript Object Notation, a lightweight data exchange format) file to improve the retrievability of knowledge.
[0027] As an example, this step 100 may include the following sub-steps: Step 110: Construct a power data model.
[0028] The description of database table information is crucial for the large language model to understand the database structure and content. Therefore, this embodiment extracts the data definition language (DDL) of all power business-related database tables from the power database, where each DDL contains the creation information of the database table, and extracts the required database table information by parsing the DDL information, which includes table name, table comment, column name, column type, etc.
[0029] In addition, data sample content is collected for each database table. The data sample content provides more accurate and intuitive database context information for the large language model by displaying the actual value of the field. The first two sample values of each field in each table are obtained by executing the following SQL statement: SELECT DISTINCT col mn FROM t m WHERE col mn IS NOT NULL LIMIT 2 ; in, t m Indicates m The name of the table. colmn Indicates m Table n The name of the column.
[0030] The database table information and data examples extracted from the database are reorganized to construct a data model. Compared with DDL statements, it is more concise and structured, and can better express the hierarchical relationship and field meaning of the database table. It is stored in JSON format to facilitate subsequent acquisition of table information. The specific form is as follows: { "table 1 ": "table 1 (col 1 ([col 1 _ch_name], [type 1 ], [data 11 , data 12 ]), col 2 ([col 2 _ch_name], [type 2 ], [data 21 ,data 22 ]), / / Other field information), table 1 _ch_name, table 1 _comment", … / / Other table structure information } Among them, table 1 The name of the table in the database, usually an English abbreviation; table 1 _ch_name represents the Chinese name of the table; table 1 _comment represents the comment of the table; 'col 1 'Represents the name of the first field in the table in the database; col 1 _ch_name、type 1 With [data 11 , data 12 ] represent the Chinese name, type and two data examples of the field respectively.
[0031] Step 120: Construct business-model mapping relationship knowledge.
[0032] As an example, this step 120 may include the following sub-steps: Step 121: sort out the business terms in the electric power field, and form a correspondence between the business terms and the data patterns, that is, a business-pattern mapping relationship.
[0033] There are a large number of concise and refined proprietary business terms and industry languages in the power sector, and some of the terms are deeply associated with specific database tables and fields. Therefore, this embodiment sorts out the specific descriptions of commonly used power business terms, and forms a correspondence between the content involved and the database structure, including related tables, fields, and the relationship between multiple tables, and finally organizes them into a specific JSON format. By establishing a correspondence between "business terms → data models" and providing relevant explanations, the large language model is assisted in understanding key business knowledge and required database table knowledge.
[0034] The knowledge organization of business-mode mapping relationship is as follows: { "nt 1 ": { "description": "xxx", "tables": ["t 1 ", "t 2 ", …], "columns": ["c 1 ", "c 2 ", …], "relations":["t 1。 c 1 = t 2。 c 1 ", ......] }, ... / / Other business term information } Among them, nt indicates the name of the business term; description contains the explanation of the business term; tables, columns, and relations are the table names, column names, and inter-table relationships involved in the business term, respectively, which are stored in the form of lists. If there is no corresponding library table content, it is represented as an empty list.
[0035] Step 122: Extract business term features, the business term features including vectorized business term names and business term names. n - gram feature.
[0036] As an example, this step 122 may include the following sub-steps: Step 1221: Perform vector embedding processing on each business term name to form a vectorized business term name.
[0037] By using the embedding vector to calculate semantic similarity, similar business terms are closer in the vector space. This embodiment uses a pre-trained embedding model based on BERT (Bidirectional Encoder Representations from Transformers) to embed text, which can capture the deep semantic meaning. The process is expressed as follows: ; in, Indicates i Business term name; The vector embedding result for the business term; Embedding Represents the embedding model used. In this way, each mapping relationship can be represented as a multidimensional vector, which can be used for text similarity retrieval.
[0038] Step 1222: Extract each business term name n - gram feature.
[0039] n - gram By splitting the text into continuous n Character combinations are used to capture the phrase structure in the text for keyword extraction to match similar content. This embodiment uses 2- gram Extract features. For example, for the text "arrears is not 0", its features are extracted as "arrears, fees are not, not, is 0" and so on. The process is expressed as follows: ; in, It is the 2-gram feature of the business term name.
[0040] Step 123: store the business-mode mapping relationship knowledge, the vectorized business term name knowledge, and the 2-gram feature knowledge of the business term name.
[0041] Store all business-model mapping relationship knowledge in a JSON file.
[0042] All vectorized business term name knowledge is stored in the vector database, which has special optimization for high-dimensional vector storage structure to support efficient storage and retrieval of large-scale vector data.
[0043] The 2-gram feature knowledge of all extracted business term names is stored in the Elasticsearch (ES) database. The ES database supports efficient full-text search and can quickly find relevant content based on the complete text or partial features.
[0044] Step 130: Construct data encoding relationship knowledge.
[0045] As an example, this step 130 may include the following sub-steps: Step 131: Data encoding relational knowledge organization.
[0046] The design of the power database is highly standardized. Most business-type texts are usually stored in the database in coded form, so there are situations where the text description cannot correspond to the data structure.
[0047] Therefore, this embodiment extracts the required information from the table storing the coding correspondence in the database, including: (1) Code name: the specific business meaning of the code item; (2) Coded value: the value of the coded name stored in the database; (3) Corresponding column name: the name of the database column to which the code belongs; This information is organized in a specific format so that the knowledge can be effectively used later. The knowledge organization form of each encoding relationship is as follows: Column: name---value; Among them, Column represents the column name corresponding to the code, name represents the code item name, and value represents the code value. For example, in the "Measurement Method Code (JLFSDM)" column, the code value corresponding to the "High Supply and High Measurement" code name is "1", and its coding relationship is organized as "JLFSDM: High Supply and High Measurement---1;".
[0048] Step 132: Extract and store the encoding relationship knowledge features.
[0049] For each coding relationship, the method described in step 122 is used to extract its vector features and 2-gram features respectively, and the extracted features are stored in the coding relationship vector database and the coding relationship ES database.
[0050] Step 200: Construct a question parsing prompt template and use a large language model to analyze the structure of the original question to extract key entities from the original question.
[0051] Semantic analysis of the original question is a key step in question enhancement, which provides a basis for understanding the user's query intent. Therefore, we build a question parsing prompt and use a large language model to analyze the structure of the question. Through this process, we identify the key elements of the question, including key entities, query conditions, and query targets, and provide accurate information support for subsequent business knowledge linking and question enhancement. The specific steps are as follows: Step 210: Design a problem resolution prompt template.
[0052] By designing a reasonable question parsing prompt template, the large language model is guided to analyze the question and effectively extract key information from the question. The question parsing prompt template instructs the large language model to extract three aspects of content, namely key entities, query conditions and query targets. The query target is the specific information or data that the user wants to obtain through the query, and the query condition is the content used to limit the query scope in the question. Extracting query conditions and query targets helps to disassemble the structure of the question and better identify the query intent; extracting key entities helps to retrieve database knowledge and business knowledge related to the entity.
[0053] Define the question parsing prompt template as a tuple , contains the following parts: ; in, G Indicates the defined task role goal, that is, to give the large language model a task-related role and clarify the task goal; Indicates important reminders, including important tips and constraints when completing tasks; O Indicates the output format of the task; C Indicates the reference output case provided; Q Indicates the original question ({question}) to be input. "{}" indicates the parameters to be input. For details about the question resolution prompt template, see Figure 8 .
[0054] Step 220: Analyze the structure of the original question using a large language model, and extract key elements from the original question. The key information includes key entities, query conditions, and query targets.
[0055] Fill the original user input question into the question parsing prompt template constructed in step 210, input it into the large language model and obtain the output result of the model. Extract the key entity list from the JSON format result output by the large language model En , query target Q With query condition list Q , for subsequent use.
[0056] Step 300: Based on the sorted out power domain knowledge and the key entities extracted from the original question, a method of fusion of vector similarity and BM25 similarity is used to retrieve power domain knowledge, wherein the power domain knowledge includes business-mode mapping relationship knowledge and data encoding relationship knowledge.
[0057] Step 300: Based on the sorted out power domain knowledge and the key entities extracted from the original question, a hybrid similarity retrieval method is used to retrieve the power domain knowledge.
[0058] In order to enhance the model's ability to understand user questions and provide necessary information support for question enhancement, based on the key entities extracted from the original question, vector similarity and BM25 similarity are used to jointly retrieve domain-specific knowledge to improve the coverage, relevance and accuracy of knowledge retrieval. The retrieved content includes business-model mapping relationship knowledge and data encoding relationship knowledge, and the entities in the question are associated with the corresponding knowledge. The specific steps are as follows: Step 310: Extract key entity features, including vector features and 2-gram features.
[0059] The key entity list obtained according to step 220 En= { e 1 , e 2 , ...}, for each key entity e i , using the method described in step 122, extract its vector features and 2-gram features respectively, so that the key entity can be matched with the knowledge, expressed as: ; ; in, e i Indicates i key entities, For key entities e i The vector features of For key entities e i 2-gram features of .
[0060] Step 320: Use the method of fusing vector similarity and BM25 similarity to jointly retrieve business-mode mapping relationship knowledge.
[0061] In order to make the large language model have relevant business terminology knowledge when enhancing questions, and to obtain database table connections related to the question business, the vector database constructed in step 122 is retrieved from the ES library according to the entity name. This embodiment adopts a hybrid method retrieval strategy, which includes: Step 3201: Calculate the cosine similarity between the entity and the business term to retrieve the business term name with similar semantics to the entity in the vector database.
[0062] The cosine similarity calculation formula is: ; in, The vector cosine similarity score between the entity and the business term, Represents the magnitude of a vector.
[0063] Step 3202: Use the BM25 algorithm to search for business term names with the same keyword fragment in the ES database.
[0064] The BM25 (Best Matching 25) algorithm calculates the frequency of occurrence of keywords and their importance in domain documents to evaluate their relevance based on word frequency and inverse document frequency.
[0065] The BM25 calculation formula is: ; ; in, represents the BM25 similarity score; for The l Feature content; N is the total number of business term names in the ES database; Contains the query word The number of documents; express In Features T ei The frequency of occurrence in Representation characteristics Length; L avg represents the average length of all features; k 1 , k 3 , b They are the hyperparameters for adjusting the score size.
[0066] Step 3203: Obtain the vectors corresponding to the key entity in the vector database according to the vector cosine similarity score. e i The most relevant business term name; according to the BM25 similarity score, the most relevant business term name is also obtained in the ES database; the two business term names are merged as the business term name knowledge set of the entity.
[0067] In order to ensure that the business terms have a strong correlation with the entity, the results with a cosine similarity score less than 0.7 are further filtered; according to the BM25 similarity score, the most relevant business term name is also obtained in the ES database, and the merged business term name knowledge set of the entity is expressed as: ; in, Representing key entities e i The name of the first related business term in the , or an empty list if there are no related contents.
[0068] For each business term name obtained by searching nt , obtained from the JSON file storing the business-mode mapping relationship in step 122 nt The corresponding knowledge content, that is, the relevant business term explanations and the mapping relationship between them and the data model K t , expressed as: ; in, Representing key entities e i The first relevant business-model mapping relationship knowledge.
[0069] Step 330: Calculate the similarity between each key entity and the content in the encoding relationship vector library and the encoding relationship ES library to obtain the encoding relationship knowledge set corresponding to all key entities.
[0070] In order to enable the large language model to correspond the problem entity with the relevant coding value and coding column name, the cosine similarity and BM25 similarity calculation method described in step 320 are used to calculate the similarity between each key entity and the coding relationship vector library and the content in the ES library constructed in step 132, and obtain the coding relationship knowledge set corresponding to all key entities. K m , expressed as: ; in, Representing Entities e i The first relevant encoding relation knowledge of , if there is no relevant knowledge it is represented as an empty list.
[0071] Step 400: Using a multi-level schema linking method, obtain the database table and data schema related to the original question.
[0072] In order to match the user's original question with the specific tables and fields in the database, this embodiment adopts a multi-level data model linking method, which can efficiently, accurately and comprehensively obtain the data model related to the user's original question. The multi-level data model linking method, on the one hand, adopts a data model linking method based on double hash value search, combined with the key entity extraction results in the original question, through the dual search strategy of database tables and fields, it can quickly and accurately match the user's natural language question with the most relevant table in the database. This method improves the accuracy of model linking and greatly reduces redundant content; on the other hand, based on the business-model mapping relationship knowledge obtained by retrieving the key entities of the original question, the association logic between the relevant business and the library table in the user's question is obtained, and a more accurate positioning result is provided in the form of associating business with the library table. The specific steps of step 400 are as follows: Step 410: Table-level schema linking; LSH (Locality Sensitive Hashing) search is an efficient approximate nearest neighbor search technology suitable for big data, which can quickly complete similarity retrieval tasks. Based on this method, it can efficiently filter and eliminate redundant information in a large number of database tables and quickly lock related data tables.
[0073] Step 411: construct table-level LSH.
[0074] As an example, this step 411 may include the following sub-steps: Step 4111: Build a table name dictionary; Get the table name and Chinese name of each table in the database from the data definition language DDL and format it into a dictionary D T , the specific format is as follows: {“Table Name 1”: “Table Chinese Name 1”, / / Mapping of other table names to their Chinese names}; Step 4112: Create a table-level hash index; According to the dictionary D constructed in step 4111 T Create an LSH index. The specific formula is: ; in, lsh t Indicates based on D T Initialized LSH Data structure or object, used for subsequent table name similarity query operations; MinHashes t For storage D TA collection of minimal hash signatures of each data item and its metadata so that the original data item can be traced back and identified after a similarity query; function create _ lsh Responsible for building the LSH index and generating a mapping set between signatures and metadata; the parameters involved sig, n_gram, thred Respectively represent the signature length, n-gram The size of the segmentation and the threshold of hash similarity matching are set to 80, 2, and 0.3 respectively when creating table-level LSH.
[0075] Step 412: Similarity table name retrieval.
[0076] Based on the key entity list obtained in step 220 En= { e 1 , e 2 , ...} and the table level created in step 4112 lsh t For each key entity e i Perform the following search operation to find out the table names that are similar to the entity. The search process can be expressed as: ; in, Representing key entities e i In lsh t Matching functions in the model; M t Represents a set of table names similar to the input entity; M t The format is: {" e 1 ”: [“Table Chinese Name 1”, ...], “ e 2 ”: [“Table Chinese Name 1”,...], , / / Other entity and table name information}.
[0077] Step 413: Table name screening.
[0078] In order to improve the matching accuracy between key entities and table names, it is necessary to retrieve the similar table name set in step 412. M t Perform further screening. Calculate through SequenceMatcher M t This is achieved by measuring the text similarity between each key entity and its Chinese name in the similarity table. The specific calculation process is as follows: ; in, SimT Indicates the result of text similarity calculation; T j Indicates that from M t Obtained and key entities e i The corresponding table Chinese name list, and t j It is a single table Chinese name element in the list; SequenceMatcher It is a text similarity calculation method in the difflib library. It is used to calculate the similarity of two strings and output a matching ratio value between 0 and 1. The larger the value, the higher the matching degree. ratio Method representation gets e i and t j The similarity score of .
[0079] Step 414: Candidate table screening and mode linking.
[0080] Filter the results based on the table name in step 413 SimT , sort the element list corresponding to each key entity in descending order according to the similarity results, and select the top top-k The tables are reserved as the most relevant tables to user needs, forming a set of related table names T s .
[0081] Based on the power data model constructed in step 110, obtain T c The table information corresponding to each table name in the problem is used to complete the schema link between the problem entity and the table level.
[0082] Step 420: Field level schema linking.
[0083] As an example, this step 420 may include the following sub-steps: Step 421: Field-level LSH construction.
[0084] As an example, this step 421 may include the following sub-steps: Step 4211: filter the value fields of the library table; For some fields in the database table, if they are not suitable for indexing, they will be excluded to improve the efficiency of index building. The specific screening method is as follows: ; in, T Represents a collection of libraries and tables. F(T) Represents table T The field collection of Type(f) Indicator Fieldf The storage type. F filtered Represents a collection of valuable fields after preliminary screening. This collection only contains fields whose storage types are character type (VARCHAR and CHAR) and integer type (INT).
[0085] Step 4212: Filter out specific name fields; Perform secondary elimination of unnecessary or repeated fields in the database table to enhance the performance of the index. The specific screening method is as follows: ; ; in, Name(f) Representation field f The name of the F final The field set after secondary screening is further eliminated based on step 4211, with the fields ending with specific suffixes (such as "bh", "mc", "dz", "jc", "lj", "dh", "hm", "bs", "zh", "id"), most of which involve number, name, address, abbreviation, path, telephone, number, logo, account number, ID and other information.
[0086] Step 4213: Build table-field dictionary; Reformatting the results of step 4212 F final , build table-field dictionary D F , the dictionary format is: { Table Name 1: {“Field Name 1”: “Field 1 Chinese Name”, “Field Name 2”: “Field 2 Chinese Name”, / / Other field information}, Table Name 2: {“Field Name 1”: “Field 1 Chinese Name”, “Field Name 2”: “Field 2 Chinese Name”, / / Other field information}, / / Other table-field information } Step 4214: Create a field-level hash index; According to the constructed table name dictionary D F Create an LSH index: ; in, lsh f Indicates based on DF Initialized LSH Data structure or object, used for subsequent field name similarity query operations; MinHashes f For storage D F The minimum hash signature of each data item and its metadata set, so that the original data item can be traced and identified after similarity query; the function create_lsh is responsible for building the LSH index and generating a mapping set between signatures and metadata. The parameters involved are sig, n_gram, thred Respectively represent the signature length, n-gram The size of the segmentation and the threshold for hash similarity matching are set to 100, 2, and 0.05 respectively when creating field-level LSH.
[0087] Step 422: Similar field name search.
[0088] Based on the key entity list obtained in step 220 En= { e 1 , e 2 , ...} and the field level constructed in step 421 lsh f Model. For each key entity e i Perform the following search operation to find out the field names that are similar to the entity. The search process can be expressed as: ; in, Representing key entities e i In lsh f The matching function in the model, M f Represents a collection of field names similar to the input entity, M f The format is: { “e 1 ”: {“Table Name 1”: [“Field 1 Chinese Name”, “Field 2 Chinese Name”, ...], / / Other table information}, “e 2 ”: {“Table Name 1”: [“Field 1 Chinese Name”, “Field 2 Chinese Name”, ...], / / Other table information}, / / Other entity information } Step 423: Filter similar tables and field names.
[0089] Get the result of step 422 and convert the original dictionary M f The table name and field list corresponding to each entity element in the table are merged to form a new dictionary, in which each table name corresponds to a list containing all non-repeated fields. At the same time, the field names of the tables involved in the new dictionary are F final Get the corresponding field name and format it into a new dictionary D h , the specific format is: {"Table Name 1": {"Field Name 1": "Field 1 Chinese Name", "Field Name 2": "Field 2 Chinese Name", / / Other field information}, / / Other table information}; Step 4231: Design of library table filtering prompt template; Design a library table filtering prompt template, combine it with a large language model, and select the new dictionary based on the degree of match with the user's question. D h Filter the tables and fields involved in the question to get the most relevant and semantically consistent tables. Define the question enhancement prompt template as P F , contains the following parts: ; in, G, I, O, Q The meaning is the same as the question resolution prompt template in step 210; KP Represents the key thinking process prompts for filtering tables; TF Indicates the table and field information to be filtered. "{}" in the prompt indicates the parameters to be input. For details on the prompt template for table filtering, see Fig. 9 .
[0090] Step 4232: filtering library table information; Input questions and dictionaries from the user D h Fill in the database table filter prompt template constructed in step 4231, input the large language model and obtain the output of the model. By parsing the JSON format output of the model, extract the candidate table T to which similar fields belong h , for subsequent use.
[0091] Step 424: Candidate table schema linking.
[0092] Similarly, based on the power data model constructed in step 110, T h The table information corresponding to each table name in the problem is used to complete the schema link between the problem entity and the field level.
[0093] Step 430: Knowledge-based pattern linking.
[0094] For the business term relationship set obtained in step 320 K , because each piece of knowledge kt The business-mode mapping relationship knowledge has been organized according to step 121, so only the analysis kt The corresponding content in and obtain the required related table name can be expressed as: ; ; in, tn ( kt i ) represents knowledge kt i The 'tables' primary key contains the tables, T n for K The set of table names contained in all knowledge.
[0095] As described in step 414 and step 424, based on the table name set T n Get the corresponding table information.
[0096] Step 500: Based on the retrieved power field knowledge and the acquired data model, knowledge standardization is performed, and a question enhancement prompt template is designed, and the original question is reconstructed and enhanced using a large language model.
[0097] Based on the question analysis content, domain knowledge and data patterns obtained from steps 200 to 400, standardize the knowledge and enhance the questions. By designing a knowledge formatting method, filter out irrelevant or redundant knowledge content; then design a question enhancement prompt template, use the large language model to reconstruct and enhance the original question, so that it can better understand the true meaning of the user's question, and generate more accurate SQL queries on this basis. The specific steps are as follows: Step 510: Knowledge standardization: Filter out irrelevant or redundant knowledge content by designing a knowledge formatting method.
[0098] As an example, this step 510 may include the following sub-steps: Step 511: Filtering the data encoding mapping relationship.
[0099] Since the encoding relationship retrieved in step 330 may contain content completely irrelevant to the data pattern obtained in step 400, it is necessary to filter the search results of step 330 to retain only information related to the actual database structure to avoid introducing unnecessary information.
[0100] Specifically, for each retrieval result of the data encoding mapping relationship, the corresponding encoding column name is extracted according to its organizational form, and the encoding relationship corresponding to the column name that is not in the relevant data mode is removed, which is expressed as: ; in, is the filtered set of encoded relational knowledge, express The encoding column name, SC These are all data patterns obtained in step 414 , step 424 , and step 430 .
[0101] Step 512: Standardize the design of the final representation of knowledge such as data model, data encoding mapping relationship and business-model mapping relationship.
[0102] In order to make it easier for large language models to understand the provided knowledge, a standardized and clear format is designed for the final representation of various knowledge.
[0103] First, the data schema content obtained in step 400 is formatted to clearly distinguish the structure and content of each table and clearly define the boundaries of different tables. The standardized table schema knowledge representation design is as follows: "- TABLE_i : xxx \n … -TABLE_k : xxx \n … ” Among them, TABLE_i represents the i The name of the table, xxx " is the specific content of the data model corresponding to the table.
[0104] Secondly, format the knowledge form of the coding mapping relationship to ensure that each entity corresponds to its related coding relationship one by one to avoid confusion. The standardized coding relationship knowledge representation design is as follows: "#Entity Name i: xxx \n Related coding knowledge : xxx \n … # Entity name k: xxx \n Related coding knowledge : xxx \n … ” Among them, 'Entity Name i The content after ' is Middle i Entities ei , the content after 'Related Encoded Knowledge' represents the entity The corresponding coding knowledge part in the output will not be formatted if there is no relevant coding knowledge.
[0105] Finally, the business-model mapping relationship knowledge is formatted, and the key entities are also matched one by one with the knowledge. The standardized business-model mapping relationship knowledge representation design is as follows: "#Entity Name i: xxx \n Related business knowledge : xxx \n … # Entity name k: xxx \n Related business knowledge : xxx \n … ” Among them, 'Entity Name i The content after ' is K t Middle i Key Entities e i ; The content after 'Related business knowledge' represents the entity K t The corresponding business terminology knowledge part will not be formatted and output if there is no relevant knowledge.
[0106] Step 520: Design a question-enhanced Prompt template.
[0107] By designing a question enhancement prompt template, the large language model can be guided to reconstruct and optimize the original question. Since the knowledge and data patterns obtained in steps 300 and 400 may still contain redundant information, the question enhancement prompt template needs to fully express the acquired knowledge content and have a clear structure and necessary reminder content to ensure that the large language model can more accurately match the question with the relevant knowledge and data patterns. Define the question enhancement prompt template as P G , contains the following parts: ; in, G, O, Q The meaning is the same as step 210 Problem Analysis Prompt Template; SK The data mode content formatted in step 512; EK For formatted coded relational knowledge content; T K To format the business-model mapping relationship knowledge content; Q A The problem analysis content obtained in step 220; W represents an important reminder, including important tips and constraints when completing the task. Fig.10 .
[0108] Step 530: Use the large language model to reconstruct and enhance the original question, optimize its expression and match the database structure to better meet the query requirements of power field data.
[0109] Fill the required knowledge content into the question enhancement prompt template, guide the large language model to eliminate redundant information, understand the question structure and optimize the way the question is expressed, so that the enhanced question can be associated with the correct business knowledge and data model, and express the query requirements more accurately.
[0110] SQL queries are generated based on data patterns, domain knowledge, and enhanced questions interacting with large language models, reducing ambiguity or errors when generating SQL using original questions, allowing users to obtain more accurate results in data queries in the power field.
[0111] This example selects a data query problem related to the power field, uses the above method to enhance the problem, and uses the enhanced problem to generate SQL. The experimental results are as follows: Question: Query the information of electricity users under a certain power supply station who have monthly meter readings but no meter reading records for April 2022.
[0112] The example of constructing power data model and related knowledge is shown in Figure 2 .
[0113] The results of the analysis of step 200 are shown in Figure 3 ,It can be seen that, after being guided by the question parsing prompt, the large language model successfully extracts the key entities, query targets and query conditions of the original question.
[0114] The encoding relationship knowledge and business-mode mapping relationship knowledge obtained through the entity retrieval association in step 300 are shown in Figure 4 ,Through the entity retrieval in the question, the coding relationships related to entities such as 'power supply station', as well as the explanation and associated table and field information related to the 'monthly meter reading' business,are obtained.
[0115] Figure 5 The above is the search results of similar table hash values and similar field hash values in step 412 and step 422 of the present invention. It can be seen that based on the key entity, the table name related to the problem and the table containing the related table fields are obtained through double search respectively. Figure 5 The following is the result of filtering in step 430. The search results are filtered using a large language model and the two most relevant tables ("HS_KFYHXX" and "KH_YDKH") are selected.
[0116] Figure 6After the above knowledge is standardized in step 500, it is filled into the question enhancement prompt, and the question enhancement result output by the large language model. It can be seen that after the knowledge injection and prompt design, the large language model carefully analyzes the original question through step-by-step thinking and matches it with the relevant content of the database, and finally outputs the question statement that is closely related to the database content, such as the condition of "xx power supply station" is "the value of the `GDDWBM` field is `030000`", and "the information of electricity users" corresponds to "the detailed information of electricity customers (`KH_YDKH` table)".
[0117] Figure 7 This is a comparison of the SQL results generated by the present invention and other methods. Figure 7 The result of a is the SQL generated by the large language model using the domain knowledge organized by the present invention and the knowledge acquired in step 250 and the enhanced question input; Figure 7 b is the SQL generated by the large language model using only database knowledge and the original question input; Figure 7 The result of c is the SQL generated by the domain knowledge organized by the present invention and the knowledge acquired in step 250, but combined with the original question input into the large language model. It can be seen that Figure 7 The SQL result in b does not have relevant domain knowledge and lacks a method for locating database tables, which results in incomplete correct use of database tables and fields in SQL, and the inability to convert "xx power supply station" and "monthly meter reading" into correct business language. Figure 7 Since c contains the required domain knowledge, it can be converted into business language in SQL, such as "GDDWBM= '030000'", "CBZQ = '1'", but the simple statement of the user's question causes the generated SQL to be inconsistent with the actual logic and fail to query the required data. The method results based on the present invention can correctly understand the user's query intention and generate SQL that meets the requirements and is logically correct.
[0118] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.
Claims
1. A multi-knowledge link driven NL2SQL question enhancement method, characterized in that: The following steps are involved: Step 100: constructing a power data model using database table information, and sorting out power domain knowledge, wherein the power domain knowledge includes business-model mapping relationship knowledge and data encoding relationship knowledge; Step 200: construct a question parsing prompt template, and use a large language model to analyze the structure of the original question and extract key entities from the original question; Step 300: Based on the sorted out power domain knowledge and the key entities extracted from the original question, the power domain knowledge is retrieved by using a hybrid similarity retrieval method; Step 400: Obtain the database table and data schema related to the original question through a multi-level schema linking method; Step 500: Based on the retrieved power field knowledge and the acquired data model, knowledge standardization is performed, and a question enhancement prompt template is designed, and the original question is reconstructed and enhanced using a large language model.
2. The NL2SQL question enhancement method driven by multiple knowledge links according to claim 1, characterized in that: In step 100, constructing the power data model includes: The data definition language DDL of all power business-related database tables is extracted from the power database, where each DDL contains the creation information of the database table. The required database table information is extracted by parsing the DDL; the data example content is collected for each database table; the database table information and data examples extracted from the database are reorganized to build a data model.
3. The NL2SQL question enhancement method driven by multiple knowledge links according to claim 2 is characterized in that: In step 100, the business-mode mapping relationship knowledge includes: Organize business terms in the power field and form a correspondence between the business terms and data models, that is, a business-model mapping relationship; Extract business term features, including vectorized business term names and business term names n - gram feature; Store all business-mode mapping relationship knowledge in JSON files; store all vectorized business term name knowledge in vector databases; n - gram Feature knowledge is stored in the ES database.
4. The NL2SQL question enhancement method driven by multiple knowledge links according to claim 3 is characterized in that: In step 100, the data encoding relationship knowledge includes: Extract the required information from the table storing the coding correspondence in the database, including the coding name, coding value and corresponding column name; For each encoding relationship, extract its vector features and n - gram Features, and store the extracted features in the encoding relationship vector database and the encoding relationship ES database.
5. The NL2SQL question enhancement method driven by multiple knowledge links according to claim 1, characterized in that: In step 200, a question resolution prompt template is constructed, and the question resolution prompt template is defined as a tuple. ,include: ; in, G Indicates the defined task role goal, that is, to give the large language model a task-related role and clarify the task goal; I Indicates important reminders, including important tips and constraints when completing tasks; O Indicates the output format of the task; C Indicates the reference output case provided; Q Represents the original question of the input.
6. The NL2SQL question enhancement method driven by multiple knowledge links according to claim 4, characterized in that: In step 300, the hybrid similarity retrieval method for retrieving power domain knowledge includes using a method of fusing vector similarity and BM25 similarity to jointly retrieve business-mode mapping relationship knowledge: Calculate the cosine similarity between the entity and the business term to retrieve the business term name with similar semantics to the entity in the vector database; Use BM25 similarity to search for business term names with the same keyword fragments in the ES database; According to the vector cosine similarity score, the most relevant business term name for the key entity is obtained in the vector database; according to the BM25 similarity score, the most relevant business term name is also obtained in the ES database; the two business term names are merged as the business term name knowledge set of the entity.
7. The NL2SQL question enhancement method driven by multiple knowledge links according to claim 6, characterized in that: In step 300, the hybrid similarity retrieval method for retrieving power domain knowledge includes using a method of fusion of vector similarity and BM25 similarity to jointly retrieve the business-mode mapping relationship. Knowledge: The cosine similarity and BM25 similarity methods are used to calculate the similarity of the content of each key entity in the encoding relationship vector library and the encoding relationship ES library, and obtain the encoding relationship knowledge set corresponding to all key entities.
8. The NL2SQL question enhancement method driven by multiple knowledge links according to claim 6, characterized in that: In step 400, the database table and data schema related to the original question are obtained through a multi-level schema linking method, including: A fast linking method based on double hash value search links the extracted original question entities with the tables and fields in the database respectively to obtain the database tables and data models related to the original question.
9. The NL2SQL question enhancement method driven by multiple knowledge links according to claim 8, characterized in that: In step 500, performing knowledge standardization includes: Based on the coding relationship knowledge retrieval results, the corresponding coding column names are extracted according to their organizational form, and the coding relationships corresponding to the column names that are not in the relevant data model are removed; and the final representation of the data model, data coding mapping relationship and business-model mapping relationship is standardized and designed.
10. The NL2SQL question enhancement method driven by multiple knowledge links according to claim 1, characterized in that: In step 500, the question enhancement prompt template is defined as P G ,include: ; in, G Indicates the defined task role goal, that is, to give the large language model a task-related role and clarify the task goal; SK The formatted data mode content; EK For formatted coded relational knowledge content; T K It is the formatted business-pattern mapping relationship knowledge content; W represents important reminders, including important tips and constraints when completing tasks; O Indicates the output format of the task; Q Represents the original input question; Q A Parse the content for the obtained question.
Citation Information
Patent Citations
Distributed vector indexing method and system, plug-in and electronic equipment
CN111026922A
Full-stack retrieval method and system, engine and electronic equipment
CN111143400A
SQL (Structured Query Language) generation method and device based on background knowledge enhancement, equipment and medium
CN117312372A
Large language model reasoning method and system based on multi-level knowledge retrieval enhancement
CN118036753A
Text2SQL semantic parsing method for domain large language model
CN118377796A
Cited By
Retrieval enhancement mode linking method and device based on multi-round iteration
CN120523827A
Hierarchical knowledge network construction and retrieval method for intelligent electric charge questions and answers
CN120744073A
A hierarchical knowledge network construction and retrieval method for electricity fee intelligent question and answer
CN120744073B
SQL statement generation method and device based on multi-stage and retrieval enhancement
CN121681580A