Power failure data query method and system based on large language model
Through word segmentation, training word vectorization model and calculation similarity, and combining with large language models to generate SQL statements, the problems of irregular naming and insufficient field adaptation in power failure data analysis are solved, and the accuracy and efficiency of power failure data analysis are improved. .
Patent Information
- Application Number
- CN202411222051.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-02
- Publication Date
- 2024-11-26
AI Technical Summary
The prior art has problems such as irregular naming, limited context length and insufficient adaptation of exclusive fields in power failure data analysis, resulting in a decrease in accuracy of large language models when understanding database tables in the power industry and generating SQL queries.
By collecting field information of the power industry database table, segmenting words and converting them into pinyin, training word vectorization models, calculating field information similarity, resorting database tables, and generating SQL statements based on large language models, thereby querying power failure data.
It improves the accuracy and efficiency of power failure data analysis, solves the problems of irregular naming and lack of industry customization, and enhances the multi-table correlation query capability.
Smart Images

Figure CN119025548A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of databases, and more specifically, to a method and system for querying power fault data based on a large language model. Background Art
[0002] In the power system, fault data analysis is a crucial task. It can help power companies predict and identify potential faults and improve the reliability and stability of the power grid. Traditional fault data analysis methods usually rely on expert experience and manual feature engineering, which limits the depth and breadth of the analysis. With the development of artificial intelligence technology, especially the rise of large language models, new possibilities are provided for power fault data analysis. Large language models have powerful feature extraction and pattern recognition capabilities. It can automatically learn complex features and rules from structured data, thereby improving the accuracy and efficiency of fault analysis.
[0003] With the rapid development of big data and artificial intelligence technology, research in the field of natural language processing (NLP) has made significant progress, especially the emergence of large language models (LLMs) based on the Transformer architecture, which enables machines to perform well in processing unstructured text data with contextual semantic associations. However, traditional database systems are mainly independent structured data. In the existing technology, most Text2SQL methods are mainly focused on general fields, and the models and methods used are not optimized for specific industries, especially not customized for the database characteristics of the power industry. Some methods mentioned in the literature, such as Seq2Seq-based models (See et al., 2017), the application of BERT models (Devlin et al., 2018) and the application of GPT models (Radford et al., 2019), although they have a certain accuracy in general fields, still face many problems in tasks such as understanding database tables, processing multi-table associations, and generating query statements (SQL) in specific industry fields (such as power, government affairs, etc.): 1. Irregular naming: In actual applications, databases in the power industry often have irregular naming problems. Table and field names may contain multiple naming methods such as English, Pinyin, and Pinyin initials, which makes it difficult for general word vector encoding models to understand these names, affecting the ability of large language models to understand the business meaning of database tables, resulting in a decrease in the accuracy of generated SQL statements.
[0004] 2. Context length limit. The database table information in a real production environment is usually very large, usually exceeding the length limit of the context understanding of the large language model. In addition, database tables are mostly created randomly and in an unordered manner (the correlation and relative position between tables are irrelevant), making it impossible for the large language model to fully understand all the related information and business meanings of the database table when generating SQL queries.
[0005] 3. Insufficient adaptation to specific fields. The corpus trained on the current open source large language model in the Text2SQL module is relatively complex. If it needs to be adapted to specific business scenarios, fine-tuning is required on this basis to improve the accuracy and efficiency of the model. Summary of the invention
[0006] The present invention aims at solving the technical problems existing in the prior art and provides a method and system for querying power fault data based on a large language model.
[0007] According to a first aspect of the present invention, a method for querying power fault data based on a large language model is provided, comprising: Collect historical database table field information of the power industry, segment the database table field information, and convert each segment into pinyin; summarize each processed segment, pinyin and pinyin first letter into a vocabulary table, and obtain the word vector of each segment in the vocabulary table; Train a word vectorization model based on the word segments and corresponding word vectors in the vocabulary; Read the table field information of all database tables for query, input each table field information into the trained word vectorization model, and obtain the word vector of each table field information; Based on the word vector of each table field information, calculate the similarity between the field information in different database tables, associate different database tables based on the similarity, reorder the order of all database tables for query according to the association degree, and place the associated database tables together; Input the name of the database table to be queried, field information and question combination into the LLM large language model to generate SQL statements for querying structured data; According to the SQL statement, the corresponding power fault data is queried from the database table for query.
[0008] According to a second aspect of the present invention, there is provided a power fault data query system based on a large language model, comprising: The first acquisition module is used to collect historical database table field information of the power industry, segment the database table field information, and convert each segment into pinyin; summarize each processed segment, pinyin and pinyin first letter into a vocabulary table, and obtain the word vector of each segment in the vocabulary table; The training module is used to train the word vectorization model based on the word segmentation and corresponding word vector in the vocabulary; The second acquisition module is used to read the table field information of all database tables for query, input each table field information into the trained word vectorization model, and obtain the word vector of each table field information; A calculation module is used to calculate the similarity between the field information in different database tables based on the word vector of each table field information, associate the different database tables based on the similarity, reorder the order of all database tables for query according to the association degree, and place the associated database tables together; The generation module is used to input the name of the database table to be queried, the field information and the question combination into the LLM large language model to generate the SQL statement for querying the structured data; The query module is used to query the corresponding power failure data from the database table for query according to the SQL statement.
[0009] The present invention provides a method and system for querying power fault data based on a large language model. By collecting database table field information with different naming rules, the table field information is segmented and vectorized; a word vectorization model is trained based on the segmentation and word vectorization; the read database table field information is vectorized based on the trained word vectorization model; the similarity of field information in different database tables is calculated, and the database tables are reordered based on the similarity; the name of the database table to be queried, the field information and the question combination are input into the LLM large language model to generate an SQL statement; according to the SQL statement, the corresponding power fault data is queried from the database table for query. The method of the present invention solves the problems of non-standardized naming, lack of industry customization and complicated training corpus existing in the prior art, and improves the accuracy and efficiency of structured power fault data analysis. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] Figure 1 A flow chart of a method for querying power fault data based on a large language model provided by the present invention; Figure 2 It is an overall flow chart of the power fault data query method based on the large language model; Figure 3 A structural schematic diagram of a power fault data query system based on a large language model provided by the present invention. DETAILED DESCRIPTION
[0011] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention. In addition, the technical features in the various embodiments or single embodiments provided by the present invention can be arbitrarily combined with each other to form a feasible technical solution. This combination is not subject to the constraints of the sequence of steps and / or the structural composition mode, but must be based on the ability of ordinary technicians in this field to achieve. When the combination of technical solutions is contradictory or cannot be achieved, it should be considered that such a combination of technical solutions does not exist and is not within the scope of protection required by the present invention.
[0012] Figure 1 A flow chart of a method for querying power fault data based on a large language model provided by the present invention is shown in FIG. Figure 1 and Figure 2 As shown, the method includes: Step 1: collect historical database table field information of the power industry, segment the database table field information, and convert each segment into pinyin; summarize each processed segment, pinyin and pinyin first letter into a vocabulary table, and obtain the word vector of each segment in the vocabulary table.
[0013] It is understandable that by collecting a large amount of database table field information in the power industry, including database table field information with different naming rules and different lengths. The collected database table field information is segmented, and the method of segmentation is mainly to extract entity words in the table field information as segmentations. Each segmentation is converted into pinyin, including the first letter of the pinyin. Then, the segmentation, pinyin, and first letter of the pinyin generated after processing the extracted field information are summarized into a vocabulary and deduplicated. Obtain the word vector of each segmentation in the vocabulary, that is, vectorize each segmentation.
[0014] Step 2: Train the word vectorization model based on the word segments and corresponding word vectors in the vocabulary.
[0015] It is understandable that a large number of word segments and word vectors of word segments are collected in the vocabulary in the above step 1, and the word vectorization model is trained based on the word segments and word vectors in the vocabulary. In the present invention, the Skip-gram method is used to train the Word2Vec model, and the vocabulary can be vectorized based on the trained Word2Vec model.
[0016] Among them, the word vectorization model is trained based on the word segmentation and corresponding word vector in the vocabulary, which previously includes: The entity words of the field information belonging to the same database table are taken as a word sequence to obtain the word sequence of each database table; If the length of the word sequence is greater than the maximum length that the word vectorization model can accept, the word sequence is divided so that the length of each word subsequence after the division is less than or equal to the maximum length that the word vectorization model can accept; otherwise, the word sequence is not divided.
[0017] It is understandable that before training the word vectorization model, the word segmentation in the vocabulary is processed to a certain extent. Specifically, the entity words of the field information belonging to the same database table are put together as a word sequence. If the length of the vocabulary of the word sequence is greater than the maximum length that the word vectorization model can accept, the word sequence needs to be divided so that the length of each word subsequence after the division is less than or equal to the maximum length that the word vectorization model can accept.
[0018] The training process of the word vectorization model is as follows: input each word sequence in the vocabulary into the word vectorization model, and output the predicted word vector of each word in the word sequence; compare the predicted word vector of each word with the actual word vector, and adjust the parameters of the word vectorization model according to the comparison result until the word vectorization model meets the objective function.
[0019] It can be understood that when training the word vectorization model, each word in each word sequence can be input into the word vectorization model, and the word vectorization model outputs the predicted word vector for each word. The difference between the predicted vector and the actual word vector is calculated, and the model parameters of the word vectorization model are adjusted according to the difference. Training is continuously performed until the trained word vectorization model meets the objective function.
[0020] Among them, the objective function is: ; ; Where T is the total number of words in the vocabulary, c is the maximum length that the word vectorization model can accept, t is the index of the word, and j is an integer. Indicates the word When it appears, the word The probability of appearing in the vocabulary, Words The vector representation of Words The vector representation of .
[0021] Through continuous training, when the word vectorization model meets the objective function, the trained word vectorization model is obtained.
[0022] Step 3: read the table field information of all database tables for query, input each table field information into the trained word vectorization model, and obtain the word vector of each table field information.
[0023] It is understandable that the table field information of all database tables for query is read, and the so-called all database tables for query are to query information in these database tables later. Specifically, write relevant PYTHON scripts to read database table field information (id, comments, data type, etc.) and output all data table field information in json format.
[0024] Entity words are extracted from the table field information of all database tables read for query, and each entity word is input into the trained word vectorization model to obtain the word vector of each entity word.
[0025] Since different fields in the database table have different importance, each word vector is weighted by TF-IDF to obtain the weighted word vector of each entity word; wherein, each word vector weighted by TF-IDF includes: ; in, Represents the word vector representation of the i-th word, express The weight of is the total number of words, is the weighted word vector.
[0026] Through this step, the word vector of the extracted database table field information is obtained based on the trained word vectorization model.
[0027] Step 4: Based on the word vector of each table field information, calculate the similarity between the field information in different database tables, associate different database tables based on the similarity, reorder the order of all database tables for query according to the association, and place the associated database tables together.
[0028] Among them, based on the word vector of each table field information, the similarity between the field information in different database tables is calculated, including: according to the word vector corresponding to each field information, the cosine similarity between the word vectors corresponding to two field information in different database tables is calculated, that is, the cosine value of the angle between the two vectors is calculated to measure their similarity.
[0029] Through the similarity of field information in different database tables, we can know whether the two database tables are related. A high similarity indicates that the two database tables are highly related, and a low similarity indicates that the two database tables are not related.
[0030] The different database tables are associated based on similarity, the order of all database tables for query is reordered according to the association degree, and the associated database tables are placed together, including: Collect query logs for database tables, parse the query logs, and extract key information from the query logs, the key information including query statements, database table names, field information, and association conditions; According to the key information in the query log and the similarity between the field information in different database tables, the relationship clusters between database tables are constructed.
[0031] Wherein, constructing a relationship cluster between database tables includes: According to the query statements in the query log, the association frequency between database tables and the similarity between field information in different database tables are counted to generate an association matrix of the database tables; Use K-means clustering algorithm to perform cluster analysis on the association matrix to form relational clusters of database tables, where the same relational cluster includes multiple related database tables; Adjust the arrangement order of all database tables and put database tables belonging to the same relationship cluster together.
[0032] It is understandable that the log function provided by the database system, such as the query log of MySQL or the log of PostgreSQL, is used to parse the log information to extract key information in the query log, such as query statements, table names, field names, association conditions, etc. The parsed data is stored in a database or file for subsequent analysis.
[0033] Based on the key information in the query log (including query statements, database table names, field names, and association conditions) and the similarity between field information in different database tables, a relationship cluster between database tables is constructed.
[0034] Specifically, the association frequency between query statement statistics tables in the query log is used to generate an association matrix; the K-means clustering algorithm is used to perform cluster analysis on the association matrix to form relationship clusters, and the same relationship cluster includes multiple related database tables.
[0035] The Large Language Model (LLM) uses positional encoding to understand the context and puts related database tables together, which can significantly improve the model's multi-table association query capabilities.
[0036] Based on the results of cluster analysis, write scripts to modify the physical storage order of database tables and put highly related tables together to optimize query performance. Re-evaluate index requirements based on the new table order, and you may need to add, delete, or modify indexes to adapt to the new table layout.
[0037] Step 5: Input the name of the database table to be queried, field information and question combination into the LLM large language model to generate an SQL statement for querying structured data.
[0038] It can be understood that the database table name, field information and question combination are input into the large language model LLM to generate the corresponding SQL statement.
[0039] Step 6: According to the SQL statement, query the corresponding power failure data from the database table for query.
[0040] It is understandable that according to the SQL statement generated by the large language model LLM, the corresponding power fault data is queried from different database tables, the power fault data queried from different database tables are merged to generate the final power fault data query result for subsequent reasoning and analysis.
[0041] See also Figure 3 , a power fault data query system based on a large language model provided by the present invention, comprising: The first acquisition module 301 is used to collect historical database table field information of the power industry, segment the database table field information, and convert each segment into pinyin; summarize each processed segment, pinyin and pinyin first letter into a vocabulary table, and obtain the word vector of each segment in the vocabulary table; A training module 302 is used to train a word vectorization model based on word segments and corresponding word vectors in a vocabulary; The second acquisition module 303 is used to read the table field information of all database tables for query, input each table field information into the trained word vectorization model, and obtain the word vector of each table field information; A calculation module 304 is used to calculate the similarity between the field information in different database tables based on the word vector of each table field information, associate the different database tables based on the similarity, reorder the order of all database tables for query according to the association, and place the associated database tables together; A generation module 305 is used to input the name of the database table to be queried, the field information and the question combination into the LLM large language model to generate an SQL statement for querying the structured data; The query module 306 is used to query the corresponding power fault data from the database table for query according to the SQL statement.
[0042] It can be understood that the power fault data query system based on a large language model provided by the present invention corresponds to the power fault data query method based on a large language model provided in the aforementioned embodiments. The relevant technical features of the power fault data query system based on a large language model can refer to the relevant technical features of the power fault data query method based on a large language model, which will not be repeated here.
[0043] The embodiment of the present invention provides a method and system for querying power fault data based on a large language model, which collects database table field information with different naming rules, performs word segmentation and vectorization on the table field information; trains a word vectorization model based on word segmentation and word vectors; vectorizes the read database table field information based on the trained word vectorization model; calculates the similarity of field information in different database tables, and reorders the database tables based on the similarity; inputs the name of the database table to be queried, the field information and the question combination into the LLM large language model to generate an SQL statement; and queries the corresponding power fault data from the database table for query according to the SQL statement. The method of the present invention solves the problems of non-standardized naming, lack of industry customization and complicated training corpus existing in the prior art, and improves the accuracy and efficiency of structured power fault data analysis.
[0044] It should be noted that in the above embodiments, the description of each embodiment has its own emphasis, and for parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0045] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0046] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded computer, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0047] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0048] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0049] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.
[0050] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A method for querying power fault data based on a large language model, characterized in that: include: Collect database table field information of the power industry, segment the table field information, and convert each segmented word into pinyin; Summarize each processed word, pinyin, and pinyin initials into a vocabulary, and obtain the word vector of each word in the vocabulary; Train a word vectorization model based on the word segments and corresponding word vectors in the vocabulary; Read the table field information of all database tables for query, input each table field information into the trained word vectorization model, and obtain the word vector of each table field information; Based on the word vector of each table field information, calculate the similarity between the field information in different database tables, associate different database tables based on the similarity, reorder the order of all database tables for query according to the association degree, and place the associated database tables together; Input the name of the database table to be queried, field information and question combination into the large language model LLM to generate SQL statements for querying structured data; According to the SQL statement, the corresponding power fault data is queried from the database table for query.
2. The power fault data query method based on a large language model according to claim 1 is characterized in that: Segmenting the database table field information includes: Extract entity words from database table field information.
3. The power fault data query method based on a large language model according to claim 1 is characterized in that: The word vectorization model is trained based on the word segmentation and the corresponding word vector in the vocabulary, which previously includes: The entity words of the field information belonging to the same database table are taken as a word sequence to obtain the word sequence of each database table; If the length of the word sequence is greater than the maximum length that the word vectorization model can accept, the word sequence is divided so that the length of each word subsequence after the division is less than or equal to the maximum length that the word vectorization model can accept; otherwise, the word sequence is not divided.
4. The power fault data query method based on a large language model according to claim 3 is characterized in that: The word vectorization model is trained based on the word segments in the vocabulary and the corresponding word vectors, including: Input each word sequence in the vocabulary into the word vectorization model and output the predicted word vector for each word in the word sequence; The predicted word vector of each word is compared with the actual word vector, and the parameters of the word vectorization model are adjusted according to the comparison result until the word vectorization model meets the objective function.
5. The power fault data query method based on a large language model according to claim 4 is characterized in that: The objective function is: ; ; Where T is the total number of words in the vocabulary, c is the maximum length that the word vectorization model can accept, t is the index of the word, and j is an integer. Indicates the word When it appears, the word The probability of appearing in the vocabulary, Words The vector representation of Words The vector representation of .
6. The power fault data query method based on a large language model according to claim 1 is characterized in that: The step of reading table field information of all database tables for query, inputting each table field information into the trained word vectorization model, and obtaining the word vector of each table field information includes: Extract entity words from the table field information of all database tables read for query, input each entity word into the trained word vectorization model, and obtain the word vector of each entity word; Each word vector is weighted by TF-IDF to obtain the weighted word vector of each entity word; wherein, each word vector is weighted by TF-IDF including: ; in, Represents the word vector representation of the i-th word, express The weight of is the total number of words, is the weighted word vector.
7. The power fault data query method based on a large language model according to claim 1 is characterized in that: The similarity between field information in different database tables is calculated based on the word vector of each table field information, including: According to the word vector corresponding to each field information, the cosine similarity between the word vectors corresponding to two field information in different database tables is calculated.
8. The power fault data query method based on a large language model according to claim 7 is characterized in that: The method of associating different database tables based on similarity, reordering the order of all database tables for query according to the degree of association, and placing the associated database tables together includes: Collect query logs for database tables, parse the query logs, and extract key information from the query logs, the key information including query statements, database table names, field information, and association conditions; According to the key information in the query log and the similarity between the field information in different database tables, a relationship cluster between database tables is constructed; Wherein, constructing the relationship cluster between database tables includes: According to the query statements in the query log, the association frequency between database tables and the similarity between field information in different database tables are counted to generate an association matrix of the database tables; Use K-means clustering algorithm to perform cluster analysis on the association matrix to form relational clusters of database tables, where the same relational cluster includes multiple related database tables; Adjust the arrangement order of all database tables and put database tables belonging to the same relationship cluster together.
9. The power fault data query method based on a large language model according to claim 1, characterized in that: According to the SQL statement, querying the corresponding power failure data from the database table for query includes: According to the SQL statement, the corresponding power failure data is queried from different database tables, and the power failure data queried from different database tables are merged to generate a final power failure data query result.
10. A power fault data query system based on a large language model, characterized in that: include: The first acquisition module is used to collect historical database table field information of the power industry, segment the database table field information, and convert each segment into pinyin; summarize each processed segment, pinyin and pinyin first letter into a vocabulary table, and obtain the word vector of each segment in the vocabulary table; The training module is used to train the word vectorization model based on the word segmentation and corresponding word vector in the vocabulary; The second acquisition module is used to read the table field information of all database tables for query, input each table field information into the trained word vectorization model, and obtain the word vector of each table field information; A calculation module is used to calculate the similarity between the field information in different database tables based on the word vector of each table field information, associate the different database tables based on the similarity, reorder the order of all database tables for query according to the association degree, and place the associated database tables together; The generation module is used to input the name of the database table to be queried, the field information and the question combination into the LLM large language model to generate the SQL statement for querying the structured data; The query module is used to query the corresponding power failure data from the database table for query according to the SQL statement.
