Power field Text2SQL data analysis method based on LLM and RAG
By building a structured data set and a multi-dimensional text knowledge base in the power field, fine-tuning LLM and combining the RAG search module, the problem of hallucination phenomena in Text2SQL data analysis in the power field is solved, and the high-accurate data analysis effect is achieved.
Patent Information
- Application Number
- CN202411711070.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-03-18
- Estimated Expiration
- 2044-11-27
AI Technical Summary
In the prior art, the search augmented generation technology (RAG) lacks a single vectorized model in specific fields and the type and dimensions of knowledge bases, resulting in noise and redundancy in the search results, which causes large language models to experience a large number of hallucinations in the analysis of Text2SQL data in the power field.
The Text2SQL data analysis method in the power field based on LLM and RAG is adopted. By constructing a structured data set in the power field, a multi-dimensional text knowledge base and a RAG search module, the LLM is fine-tuned to generate accurate SQL statements, and data analysis is performed through the Agent data analysis module.
Through a multi-dimensional knowledge base and mixed search algorithm, the accuracy of the search results is improved, the hallucination phenomenon when LLM generates SQL statements is reduced, the effect of Text2SQL data parsing is ensured, and the model training cost is reduced.
Smart Images

Figure CN119646170A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of large language model and data processing technology, and in particular to a Text2SQL data parsing method in the electric power field based on LLM and RAG. Background Art
[0002] The existing Text2SQL data parsing in the power sector mainly relies on manual query of relational databases. Operators need to have high professional knowledge, and the operation process is complicated, time-consuming, and prone to errors. With the rapid development of large language models, especially in natural language processing and intelligent data analysis, new possibilities are provided for intelligent Text2SQL data parsing. However, there is currently a lack of intelligent Text2SQL data parsing methods suitable for the power sector.
[0003] There are several difficulties in applying large language models to Text2SQL data parsing in the power sector. First, the large language model (LLM) needs to be able to understand the professional terms and domain knowledge in the power sector; second, the database table data involved in the power sector is usually private data within the enterprise, and the data security needs to be guaranteed and can be updated at any time; finally, in order to be effectively used in actual work scenarios, the SQL query statements generated by the large language model must be accurate enough to minimize the generation of hallucinations.
[0004] Retrieval-augmented generation (RAG) is a technique that uses relevant retrieval technology to retrieve text blocks related to the question from an external knowledge base to further enhance the generation capability of large models and reduce the illusion of large models.
[0005] So how to use retrieval enhancement generation technology to realize Text2SQL data parsing in the power field has become a technical problem that needs to be solved urgently. Summary of the invention
[0006] The purpose of the present invention is to solve the defects of RAG technology in the prior art, that is, the retrieval results contain noise and redundancy, and a large number of hallucination phenomena still occur in its large model due to the lack of a vectorized model for a specific field and the single type and dimension of the knowledge base. A Text2SQL data parsing method in the power field based on LLM and RAG is provided to solve the above problems.
[0007] In order to achieve the above object, the technical solution of the present invention is as follows:
[0008] A Text2SQL data parsing method in the power field based on LLM and RAG includes the following steps:
[0009] Construct a data set for the Text2SQL data parsing method in the power sector: construct a structured data set in the power sector, manually perform few-shot annotations on the structured data set in the power sector, construct a question-answer pair data set of the "question-SQL statement" type, and construct a "question-thinking chain" data set based on the question-answer pair data set of the "question-SQL statement" type based on LLM;
[0010] Construct a multi-dimensional text knowledge base for Text2SQL data parsing methods in the power sector;
[0011] Fine-tuning the LLM base of the Text2SQL data parsing method in the power sector: Using the "question-thinking chain" data set, it is organized into json format data. Based on the Qwen-72B large model base, the LoRA method is used to perform only one epoch fine-tuning to obtain the Qwen-72B-SQL-Cot large language model, so that its LLM learns a specific way of thinking to generate accurate SQL statements;
[0012] Constructing the RAG retrieval module of the Text2SQL data parsing method in the power sector;
[0013] Construct an Agent data parsing module for the Text2SQL data parsing method in the power field and perform data parsing processing.
[0014] The data set for constructing the Text2SQL data parsing method in the power field includes the following steps:
[0015] The real structured data in the power field is divided according to different power scenarios. Based on the structured data in the power field and the description information of the database tables and fields extracted from the structured data, a structured data set in the power field is jointly constructed.
[0016] Based on the structured data set in the power field, a small number of questions and corresponding SQL statements are annotated for each table, and a question-answer pair data set of the "question-SQL statement" type is constructed;
[0017] Based on the question-answer pair dataset of the "question-SQL statement" type, the tables and fields in the SQL corresponding to the question are taken out and organized into different prompts together with the question; then, with the help of open source LLM, the thinking chain or logical reasoning process from natural language to SQL statement Text2SQL is generated to construct a "question-thinking chain" dataset.
[0018] The multi-dimensional text knowledge base of the Text2SQL data parsing method in the power field is constructed, comprising the following steps:
[0019] Firstly, the database table name and field name in the SQL statement are extracted from the annotated question-answer pair data set of the "question-SQL statement" type; then, the Comment description information of the database is queried in the structured data set of the power field according to the database table name and field name; finally, the question is used as the index of knowledge base retrieval, and the description information Comment is used as the retrieval content, which is in one-to-one correspondence with the question index to form a text knowledge base Q-Comment;
[0020] Based on the structured data set in the power field, each database table is regarded as a knowledge base data. Each knowledge base data stores the table name, table description information Comment, field name, field type, and field description information Comment; each knowledge base data is both an index and a search content, and a massive database description information text knowledge base A-Comment is constructed;
[0021] Based on the "question-thinking chain" dataset, the questions in the "question-SQL statement" question-answer pair dataset are used as knowledge base indexes, and the thinking chain process corresponding to the questions is used as the retrieval content to construct a text knowledge base Q-Cot of questions and the thinking chain information corresponding to the questions.
[0022] The RAG retrieval module for constructing the Text2SQL data parsing architecture in the power field includes the following steps:
[0023] Build a RAG multi-dimensional vector knowledge base based on the three-dimensional text knowledge base of Q-Comment, A-Comment, and Q-Cot. Use the text2vec-large-chinese vector model to convert each index in each knowledge base into a vector, and finally build a three-dimensional vector knowledge base
[0024] Q-Comment-Embedding, A-Comment-Embedding and Q-Cot-Embedding;
[0025] Constructing a hybrid retrieval algorithm for RAG multi-dimensional and multi-category knowledge base:
[0026] Construct a keyword retrieval algorithm BM25, as shown in formula (1), to extract keywords from the user input question, calculate the relevance score between the keywords and each document in the multi-dimensional text knowledge base, and recall the top-k documents with the highest scores;
[0027] Construct the cosine similarity of the vector retrieval algorithm, as shown in formula (2), convert the user question into a vector, and calculate the k-nearest neighbor vector blocks with the highest similarity to the question vector in the multi-dimensional vector knowledge base;
[0028] Construct the reciprocal ranking fusion algorithm RRF, as shown in formula (4), to re-rank the scores of the text and vector of the retrieval results;
[0029]
[0030] in,
[0031]
[0032]
[0033] In formula (1), Score(D,Q) represents the matching score between question Q and knowledge base document D. First, query Q is segmented to obtain keyword q i ; Then, calculate the keyword q i The inverse document frequency IDF is as shown in formula (2); then calculate the keyword q i The frequency of occurrence f(q i , D); where k1 and b are hyperparameters, adjusted according to documents and keywords; |D| is the length of document D, and avgdl is the average length of all documents in the knowledge base;
[0034] In formula (2), N represents the total number of documents, n(q i ) means it contains the keyword q i The total number of documents,
[0035] In formula (3), A·B represents the dot product of two vectors, ‖A‖ and ‖B‖ represent the lengths of the vectors.
[0036] In formula (4), D represents the knowledge base document set, R represents the set ranked by score, k is a constant greater than 1, r(d) is the score ranking of document d in a certain retrieval algorithm, and the final score is the sum of the constant plus the reciprocal of the ranking of the document in the result set;
[0037] Building a RAG retrieval module
[0038] Retrieve the candidate information required for LLM:
[0039] First, the BM25 algorithm is used to extract keywords from user questions, and the similarity between keywords and the index of the A-Comment text knowledge base is calculated. The five database tables most relevant to the current user question and their annotation information are retrieved. Then, the user question is converted into a vector, and the cosine similarity cos(θ) between the vector and the vector index in the A-Comment-Embedding knowledge base is calculated. The five database tables most relevant to the question and the corresponding annotation information are also retrieved. Finally, all search contents are scored, and the RRF algorithm is used to re-rank the 10 candidate information, and the final top five database table annotation information is selected to provide a candidate database table set for LLM to answer the current question.
[0040] Examples of references needed to retrieve an LLM:
[0041] First, BM25 and cosine similarity algorithm cos(θ) are used respectively to search the index created by the marked question in the Q-Comment and Q-Comment-Embedding knowledge bases according to the user's current question, and the top 5 similar historical questions and their corresponding database table annotation information are selected from each type of knowledge base; finally, the search calculation score is calculated, and the RRF algorithm is used to re-rank the 10 similar historical questions, and the top 5 historical questions with the most similar semantics and keywords are selected; as an LLM reference example, the historical question is to learn how to select tables and fields related to the question from a massive database, so that for the retrieved candidate information, LLM selects the most appropriate single table or multiple tables and fields for the current question;
[0042] An example of the reasoning needed to retrieve the LLM:
[0043] First, based on the keyword retrieval algorithm BM25, the current user question is searched in the Q-Cot knowledge base to obtain the top 5 historical questions and their corresponding thinking chains; then, based on the vector retrieval algorithm cosine similarity cos(θ), the historical question vector index is searched in the Q-Cot-Embedding knowledge base, and the top 5 data are also selected; finally, the RRF algorithm is used to re-rank and finally select the top 5 similar historical questions and their corresponding thinking chains; as an LLM reference reasoning example, learn how to generate accurate SQL statements step by step for historical questions with the help of the field annotation information of the provided database table;
[0044] Build the RAG prompt template SQL-Prompt:
[0045] Setting up SQL-Prompt includes four steps: the first step is to set the large model as the SQL statement expert role, so that LLM only generates SQL statements and does not generate any irrelevant information; the second step is to require the SQL statements generated by LLM to be based only on the candidate database table set provided by the RAG retrieval module; the third step is to require LLM to generate SQL statements based on the historical problem reference examples provided by the RAG retrieval module, that is, to find the tables and fields that the SQL statements really need from the candidate database table set; the fourth step is to require LLM to generate SQL statements based on the thinking chain reference examples provided by the RAG retrieval module, that is, to generate each part of the SQL statement step by step according to the final table and field information selected based on the historical problem reference; finally, the constructed RAG prompt template SQL-Prompt is used as the final output result of the RAG retrieval module.
[0046] The Agent data parsing module of the Text2SQL data parsing architecture in the power field and the data parsing process include the following steps:
[0047] Construct SQL statement self-check module: When the Qwen-72B-SQL-Cot large language model generates SQL statements according to the SQL-Prompt prompt output by the RAG retrieval module, first, the SQL statements are sent to the SQL syntax error capture module to self-check whether there are syntax errors in the SQL statements; then, after passing the syntax self-check module, they are sent to the table name and field name detection module to extract the table names and field names involved in the SQL statements and search for them in the power field database to prevent the large model from hallucinating and making up a table name or field name that does not exist in the database; finally, they are sent to the user self-check module. When there is an error in the SQL syntax or the table name or field name is incorrect, the large model is prompted to regenerate;
[0048] When the SQL statement passes the self-check module, an Agent workflow is built to automatically execute the SQL statement;
[0049] Get the results of SQL statements in the power field, organize the data results into Markdown format, and input them into the Qwen-72B-SQL-Cot large model according to different power scenarios and different prompts to analyze the data queried by SQL to obtain multi-dimensional data analysis results such as data trends, response problems, and table visualization.
[0050] Beneficial Effects
[0051] The present invention discloses a Text2SQL data parsing method in the electric power field based on LLM and RAG. Compared with the prior art, the RAG-based technology enables a large language model to obtain relevant information required for the Text2SQL data parsing in the electric power field through retrieval. At the same time, the domain data stored in the form of a knowledge base ensures the privacy of the data and can be updated in a timely manner. The LLM-based Text2SQL data parsing method with prompt engineering as the main method also greatly reduces the model training cost. The mixed retrieval parameter setting makes the retrieval result have higher accuracy, ensuring the effect of Text2SQL data parsing. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] Figure 1 is a method sequence diagram of the present invention;
[0053] Figure 2 is a flow chart of the method of the present invention;
[0054] Figure 3 This is an example diagram of the Q-Comment knowledge base of the present invention;
[0055] Figure 4 This is an example diagram of the A-Comment knowledge base of the present invention;
[0056] Figure 5 This is an example diagram of the Q-Cot knowledge base of the present invention;
[0057] Figure 6 The exemplary diagrams are shown in part to illustrate the achievements of the present invention. DETAILED DESCRIPTION
[0058] In order to have a further understanding and recognition of the structural features and the effects achieved by the present invention, a preferred embodiment and accompanying drawings are used for detailed description as follows:
[0059] like Figure 1 and Figure 2 As shown, the Text2SQL data parsing method in the power field based on LLM and RAG of the present invention includes the following steps:
[0060] The first step is to build a data set for the Text2SQL data parsing architecture in the power sector: construct a structured data set in the power sector, manually perform a few-shot annotation based on the structured data set in the power sector, build a question-answer pair data set of the "question-SQL statement" type, and build a "question-thinking chain" data set based on the question-answer pair data set of the "question-SQL statement" type based on LLM. This provides a data foundation for the subsequent knowledge base construction and a reference example for LLM generation.
[0061] (1) The real structured data in the power field is divided according to different power scenarios. Based on the structured data in the power field and the descriptive information of database tables and fields extracted from the structured data, a structured data set in the power field is jointly constructed.
[0062] (2) Based on the structured data set in the power field, a small number of questions and the corresponding SQL statements are annotated for each table, and a question-answer pair data set of the "question-SQL statement" type is constructed.
[0063] (3) Based on the question-answer pair dataset of the “question-SQL statement” type, the tables and fields in the SQL corresponding to the question are extracted and organized into different prompts together with the question; then, with the help of open source LLM, the thinking chain or logical reasoning process from natural language to SQL statement generation is generated to construct a “question-thinking chain” dataset.
[0064] The second step is to build a multi-dimensional text knowledge base of the Text2SQL data parsing architecture in the power field. The multi-dimensional text knowledge base can not only improve the accuracy of text retrieval, but more importantly, enhance the generation capability of LLM from multiple dimensions.
[0065] (1) Based on the annotated “question-SQL statement” type question-answer pair dataset, the database table name and field name in the SQL statement are extracted. Based on the database table name and field name, the Comment description information of the database is queried in the structured dataset of the power field. The question is used as the index for knowledge base retrieval, and the description information Comment and the question are matched one by one to form the knowledge base Q-Comment, such as Figure 3 shown.
[0066] (2) Based on the structured data set in the power field, each database table is used as a knowledge base data and as a search index. Each data entry stores the table name, table description information Comment, field name, field type, and field description information Comment. A massive database description information text knowledge base A-Comment is constructed, such as Figure 4 shown.
[0067] (3) Based on the “question-thinking chain” dataset, the questions in the “question-SQL statement” question-answer pair dataset are used as knowledge base indexes to construct a knowledge base Q-Cot of questions and their corresponding thinking chain information, such as Figure 5 shown.
[0068] The third step is to fine-tune the LLM base of the Text2SQL data parsing architecture in the power field: use the "problem-thinking chain" data set, organize it into json format data, and use the LoRA method to perform only 1 epoch fine-tuning based on the Qwen-72B large model base to obtain the Qwen-72B-SQL-Cot large language model, so that its LLM learns a specific way of thinking to generate accurate SQL statements.
[0069] The fourth step is to build the RAG retrieval module of the Text2SQL data parsing architecture in the power field. First, a multi-dimensional vector type knowledge base is built to provide vector semantic retrieval technology. At the same time, combined with the above multi-dimensional text type knowledge base, multiple dimensional knowledge base information is obtained, which greatly improves the generation ability of the LLM model. Searching in multiple types of knowledge bases and using the RRF algorithm to sort the two search results not only achieves a mixed search effect, but also further improves the search accuracy and reduces the LLM illusion phenomenon.
[0070] (1) Construct a RAG multi-dimensional vector knowledge base based on the three dimensional text knowledge bases of Q-Comment, A-Comment, and Q-Cot. Use the text2vec-large-chinese vector model to convert each index in each knowledge base into a vector. Finally, construct three types of vector knowledge bases: Q-Comment-Embedding, A-Comment-Embedding, and Q-Cot-Embedding.
[0071] (2) Constructing a hybrid retrieval algorithm for RAG multi-dimensional and multi-category knowledge base:
[0072] Construct a keyword retrieval algorithm BM25, as shown in formula (1), to extract keywords from the user input question, calculate the relevance score between the keywords and each document in the multi-dimensional text knowledge base, and recall the top-k documents with the highest scores;
[0073] Construct the cosine similarity of the vector retrieval algorithm, as shown in formula (2), convert the user question into a vector, and calculate the k-nearest neighbor vector blocks with the highest similarity to the question vector in the multi-dimensional vector knowledge base;
[0074] Construct the reciprocal ranking fusion algorithm RRF, as shown in formula (4), to re-rank the scores of the text and vector of the retrieval results;
[0075]
[0076] in,
[0077]
[0078]
[0079] In formula (1), Score(D,Q) represents the matching score between question Q and knowledge base document D. First, query Q is segmented to obtain keyword q i ; Then, calculate the keyword q i The inverse document frequency IDF is as shown in formula (2); then calculate the keyword q i The frequency of occurrence f(q i , D); where k1 and b are hyperparameters, adjusted according to documents and keywords; |D| is the length of document D, and avgdl is the average length of all documents in the knowledge base;
[0080] In formula (2), N represents the total number of documents, n(q i ) means it contains the keyword q i The total number of documents,
[0081] In formula (3), A·B represents the dot product of two vectors, ‖A‖ and ‖B‖ represent the lengths of the vectors.
[0082] In formula (4), D represents the knowledge base document set, R represents the set ranked by score, k is a constant greater than 1, r(d) is the score ranking of document d in a certain retrieval algorithm, and the final score is the sum of the constant plus the inverse of the ranking of the document in the result set.
[0083] (3) Constructing RAG retrieval module
[0084] A1) Retrieve the candidate information required for LLM:
[0085] First, the BM25 algorithm is used to extract keywords from user questions, and the similarity between keywords and the index of the A-Comment text knowledge base is calculated to retrieve the five database tables most relevant to the current user question and their annotation information. Then, the user question is converted into a vector, and the cosine similarity between the vector and the vector index in the A-Comment-Embedding knowledge base is calculated. The five database tables most relevant to the question and the corresponding annotation information are also retrieved. Finally, all search contents are scored, and the RRF algorithm is used to re-rank the 10 candidate information, and the final top five database table annotation information is selected to provide a candidate database table set for LLM to answer the current question.
[0086] A2) Examples of references required to search for LLM:
[0087] First, we use the BM25 and cosine similarity algorithms to search the indexes created by the marked questions in the Q-Comment and Q-Comment-Embedding knowledge bases according to the user's current question, and select the top 5 similar historical questions and their corresponding database table annotation information from each type of knowledge base. Finally, we calculate the search scores and use the RRF algorithm to re-rank the 10 similar historical questions, and select the top 5 historical questions that are most similar in terms of semantics and keywords. As a reference example for LLM, we learn how to select tables and fields related to the question from a massive database in historical questions, so that LLM selects the most appropriate single table or multiple tables and fields for the retrieved candidate information for the current question.
[0088] A3) Example of reasoning required to retrieve LLM:
[0089] First, based on the keyword retrieval algorithm BM25, the current user question is searched in the Q-Cot knowledge base to obtain the top 5 historical questions and their corresponding thinking chains; then, based on the vector retrieval algorithm cosine similarity, the historical question vector index is searched in the Q-Cot-Embedding knowledge base, and the top 5 data are also selected; finally, the RRF algorithm is used to re-sort and finally select the top 5 similar historical questions and their corresponding thinking chains; as an LLM reference reasoning example, learn how historical questions use the field annotation information of the provided database table to generate accurate SQL statements step by step.
[0090] (4) Build the RAG prompt template SQL-Prompt:
[0091] Setting SQL-Prompt consists of four steps. The first step is to set the large model as an SQL statement expert role, so that LLM only generates SQL statements and does not generate any irrelevant information; the second step is to require the SQL statements generated by LLM to be based only on the candidate database table set provided by the RAG retrieval module; the third step is to require LLM to generate SQL statements based on the historical problem reference examples provided by the RAG retrieval module, that is, to find the tables and fields that the SQL statements really need from the candidate database table set; the fourth step is to require LLM to generate SQL statements based on the thinking chain reference examples provided by the RAG retrieval module, that is, to generate each part of the SQL statement step by step according to the final table and field information selected based on the historical problem reference; finally, the constructed RAG prompt template SQL-Prompt is used as the final output result of the RAG retrieval module.
[0092] The fifth step is to build the Agent data parsing module of the Text2SQL data parsing architecture in the power field and perform data parsing processing.
[0093] (1) Constructing an SQL statement self-checking module: When the Qwen-72B-SQL-Cot large language model generates an SQL statement according to the SQL-Prompt prompt output by the RAG retrieval module, first, the SQL statement is sent to the SQL syntax error capture module to self-check whether the SQL statement has any syntax errors; then, after passing the syntax self-checking module, it is sent to the table name and field name detection module to extract the table name and field nouns involved in the SQL statement and search for their existence in the power field database to prevent the large model from hallucinating and making up a table name or field name that does not exist in the database; finally, it is sent to the user self-checking module. When an SQL syntax error occurs or the table name or field name is incorrect, the large model is prompted to regenerate.
[0094] (2) When the SQL statement passes the self-check module, an Agent workflow is built to automatically execute the SQL statement.
[0095] (3) Obtain the query results of SQL statements in the power field, organize the data results into Markdown format, and input them into the Qwen-72B-SQL-Cot large model according to different power scenarios and different prompts to analyze the SQL query data and obtain multi-dimensional data analysis results such as data trends, response problems, and table visualization.
[0096] like Figure 6 As shown in the figure, when a user raises a question related to the power field, the big model uses the thinking chain method to analyze step by step based on the information retrieved from the multi-dimensional and multi-class knowledge base to obtain the final SQL statement result. It also provides users with the operation of executing and modifying SQL statements.
[0097] The above shows and describes the basic principles, main features and advantages of the present invention. Those skilled in the art should understand that the present invention is not limited to the above embodiments. The above embodiments and descriptions only describe the principles of the present invention. The present invention may be subject to various changes and improvements without departing from the spirit and scope of the present invention. These changes and improvements fall within the scope of the present invention. The scope of protection claimed by the present invention is defined by the attached claims and their equivalents.
Claims
1. A Text2SQL data parsing method in the power field based on LLM and RAG, characterized in that: The following steps are involved: 11) Construct a data set for the Text2SQL data parsing method in the power sector: construct a structured data set in the power sector, manually perform few-shot annotations on the structured data set in the power sector, construct a question-answer pair data set of the "question-SQL statement" type, and construct a "question-thinking chain" data set based on the question-answer pair data set of the "question-SQL statement" type based on LLM; 12) Construct a multi-dimensional text knowledge base for Text2SQL data parsing methods in the power sector; 13) Fine-tuning the LLM base of the Text2SQL data parsing method in the power sector: Using the "problem-thinking chain" data set, it is organized into json format data. Based on the Qwen-72B large model base, the LoRA method is used to perform only one epoch fine-tuning to obtain the Qwen-72B-SQL-Cot large language model, so that its LLM learns a specific way of thinking to generate accurate SQL statements; 14) Construct the RAG retrieval module of the Text2SQL data parsing method in the power field; 15) Construct an Agent data parsing module for the Text2SQL data parsing method in the power field and perform data parsing processing.
2. According to claim 1, a Text2SQL data parsing method in the power field based on LLM and RAG is characterized in that: The data set for constructing the Text2SQL data parsing method in the power field includes the following steps: 21) Divide the real structured data in the power field according to different power scenarios, and build a structured data set in the power field based on the structured data in the power field and the description information of the database tables and fields extracted from the structured data; 22) Based on the structured data set in the power field, a small number of questions and corresponding SQL statements are annotated for each table to construct a question-answer pair data set of the "question-SQL statement" type; 23) Based on the question-answer pair dataset of the "question-SQL statement" type, the tables and fields in the SQL corresponding to the question are taken out and organized into different prompts together with the question; then, with the help of open source LLM, the thinking chain or logical reasoning process from natural language to SQL statement Text2SQL is generated to construct a "question-thinking chain" dataset.
3. According to claim 1, a Text2SQL data parsing method in the power field based on LLM and RAG is characterized in that: The multi-dimensional text knowledge base of the Text2SQL data parsing method in the power field is constructed, comprising the following steps: 31) First, extract the database table name and field name in the SQL statement according to the annotated "question-SQL statement" type question-answer pair data set; then, query the database Comment description information in the structured data set of the power field according to the database table name and field name; finally, take the question as the index of knowledge base retrieval, take the description information Comment as the retrieval content, and correspond it one-to-one with the question index to form a text knowledge base Q-Comment; 32) Based on the structured data set in the power field, each database table is taken as a knowledge base data. Each knowledge base data stores the table name, table description information Comment, field name, field type, and field description information Comment; each knowledge base data is both an index and a search content, and a massive database description information text knowledge base A-Comment is constructed; 33) Based on the "question-thinking chain" dataset, the questions in the "question-SQL statement" question-answer pair dataset are used as knowledge base indexes, and the thinking chain process corresponding to the questions is used as the retrieval content to construct a text knowledge base Q-Cot of questions and the thinking chain information corresponding to the questions.
4. According to claim 1, a Text2SQL data parsing method in the electric power field based on LLM and RAG is characterized in that: The RAG retrieval module for constructing the Text2SQL data parsing architecture in the power field includes the following steps: 41) Build a multi-dimensional vector knowledge base of RAG. Based on the three-dimensional text knowledge bases of Q-Comment, A-Comment, and Q-Cot, use the text2vec-large-chinese vector model to convert each index in each knowledge base into a vector, and finally build three-dimensional vector knowledge bases of Q-Comment-Embedding, A-Comment-Embedding, and Q-Cot-Embedding; 42) Constructing a hybrid retrieval algorithm for RAG multi-dimensional and multi-category knowledge base: Construct a keyword retrieval algorithm BM25, as shown in formula (1), to extract keywords from the user input question, calculate the relevance score between the keywords and each document in the multi-dimensional text knowledge base, and recall the top-k documents with the highest scores; Construct the cosine similarity of the vector retrieval algorithm, as shown in formula (2), convert the user question into a vector, and calculate the k-nearest neighbor vector blocks with the highest similarity to the question vector in the multi-dimensional vector knowledge base; Construct the reciprocal ranking fusion algorithm RRF, as shown in formula (4), to re-rank the scores of the text and vector of the retrieval results; in, In formula (1), Score(D,Q) represents the matching score between question Q and knowledge base document D. First, query Q is segmented to obtain keyword q i ; Then, calculate the keyword q i The inverse document frequency IDF is as shown in formula (2); then calculate the keyword q i The frequency of occurrence f(q i , D); where k1 and b are hyperparameters, adjusted according to documents and keywords; |D| is the length of document D, and avgdl is the average length of all documents in the knowledge base; Formula (2), N represents the total number of documents, n(q i ) means it contains the keyword q i The total number of documents, In formula (3), A·B represents the dot product of two vectors, ‖A‖ and ‖B‖ represent the lengths of the vectors. In formula (4), D represents the knowledge base document set, R represents the set ranked by score, k is a constant greater than 1, r(d) is the score ranking of document d in a certain retrieval algorithm, and the final score is the sum of the constant plus the reciprocal of the ranking of the document in the result set; 43) Construct RAG retrieval module 431) Retrieve the candidate information required for LLM: First, the BM25 algorithm is used to extract keywords from user questions, and the similarity between keywords and the index of the A-Comment text knowledge base is calculated. The five database tables most relevant to the current user question and their annotation information are retrieved. Then, the user question is converted into a vector, and the cosine similarity cos(θ) between the vector and the vector index in the A-Comment-Embedding knowledge base is calculated. The five database tables most relevant to the question and the corresponding annotation information are also retrieved. Finally, all search contents are scored, and the RRF algorithm is used to re-rank the 10 candidate information, and the final top five database table annotation information is selected to provide a candidate database table set for LLM to answer the current question. 432)Example of references required to retrieve LLM: First, BM25 and cosine similarity algorithm cos(θ) are used respectively to search the index created by the marked question in the Q-Comment and Q-Comment-Embedding knowledge bases according to the user's current question, and the top 5 similar historical questions and their corresponding database table annotation information are selected from each type of knowledge base; finally, the search calculation score is calculated, and the RRF algorithm is used to re-rank the 10 similar historical questions, and the top 5 historical questions with the most similar semantics and keywords are selected; as an LLM reference example, the historical question is to learn how to select tables and fields related to the question from a massive database, so that for the retrieved candidate information, LLM selects the most appropriate single table or multiple tables and fields for the current question; 433) Example of reasoning required to retrieve LLM: First, based on the keyword retrieval algorithm BM25, the current user question is searched in the Q-Cot knowledge base to obtain the top 5 historical questions and their corresponding thinking chains; then, based on the vector retrieval algorithm cosine similarity cos(θ), the historical question vector index is searched in the Q-Cot-Embedding knowledge base, and the top 5 data are also selected; finally, the RRF algorithm is used to re-rank and finally select the top 5 similar historical questions and their corresponding thinking chains; as an LLM reference reasoning example, learn how to generate accurate SQL statements step by step for historical questions with the help of the field annotation information of the provided database table; 44) Build the RAG prompt template SQL-Prompt: Setting up SQL-Prompt includes four steps: the first step is to set the large model as the SQL statement expert role, so that LLM only generates SQL statements and does not generate any irrelevant information; the second step is to require the SQL statements generated by LLM to be based only on the candidate database table set provided by the RAG retrieval module; the third step is to require LLM to generate SQL statements based on the historical problem reference examples provided by the RAG retrieval module, that is, to find the tables and fields that the SQL statements really need from the candidate database table set; the fourth step is to require LLM to generate SQL statements based on the thinking chain reference examples provided by the RAG retrieval module, that is, to generate each part of the SQL statement step by step according to the final table and field information selected based on the historical problem reference; finally, the constructed RAG prompt template SQL-Prompt is used as the final output result of the RAG retrieval module.
5. According to claim 1, a Text2SQL data parsing method in the electric power field based on LLM and RAG is characterized in that: The Agent data parsing module of the Text2SQL data parsing architecture in the power field and the data parsing process include the following steps: 51) Construct SQL statement self-check module: When the Qwen-72B-SQL-Cot large language model generates SQL statements according to the SQL-Prompt prompt output by the RAG retrieval module, first, the SQL statements are sent to the SQL syntax error capture module to self-check whether there are syntax errors in the SQL statements; then, after passing the syntax self-check module, they are sent to the table name and field name detection module to extract the table names and field names involved in the SQL statements, and search for them in the power field database to prevent the large model from hallucinating and making up a table name or field name that does not exist in the database; finally, they are sent to the user self-check module. When there is an error in the SQL syntax or the table name or field name is incorrect, the large model is prompted to regenerate; 52) When the SQL statement passes the self-check module, build an Agent workflow to automatically execute the SQL statement; 53) Obtain the query results of SQL statements in the power field, organize the data results into Markdown format, and input them into the Qwen-72B-SQL-Cot large model according to different power scenarios and different prompts, analyze the SQL query data, and obtain multi-dimensional data analysis results such as data trends, response problems, and table visualization.
Citation Information
Patent Citations
Electric power defect image detection method based on image-text question-answer multi-modal model
CN117763107A
Database query statement generation method and device, equipment and storage medium
CN118227655A
Large language model generation type knowledge question-answering method in vertical field of power auditing
CN118245586A
Text2SQL semantic parsing method for domain large language model
CN118377796A
Multi-round text-to-SQL (Structured Query Language) method and system based on editing chain, storage medium and electronic equipment
CN118394777A
Cited By
Large model retrieval enhancement generation method oriented to policies in electricity price field
CN119940512A
RAG-based electric power communication management file assisted compilation method and system
CN120234405A
Intelligent bidding document qualification information extraction method based on knowledge graph and mixed retrieval
CN120407728A
RAG enhancement method for coarse and fine granularity fusion segmentation and semantic connection
CN120409472A
Emergency distortion information identification method and system
CN120744342A