A method for constructing SQL agent in power field based on KMDI chain

By introducing the KMDI chain method into the Text-to-SQL technology in the power field, organizing a variety of domain knowledge bases and using knowledge matching and distillation processes, the problems of insufficient domain knowledge and SQL generation errors in the power field data query are solved, and SQL generation is achieved that is more accurate and in line with actual needs.

CN119166662BActive Publication Date: 2025-05-16YANTAI HAIYI SOFTWARE
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411666549.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-21
Publication Date
2025-05-16
Estimated Expiration
2044-11-21

AI Technical Summary

Technical Problem

The existing Text-to-SQL technology has problems such as weak domain knowledge ability, complex SQL content, high database complexity, and mismatch of query problem descriptions with database content in data query in the power field, which makes it difficult for the model to understand domain terms and generate incorrect SQL.

Method used

A method for constructing SQL agents in the power field based on KMDI chain is proposed. By organizing and constructing a variety of domain knowledge bases, including SQL Q&A pairs, database table structure, inter-table relationships and coding mapping relationships, we use the chain process of knowledge matching, key knowledge distillation and injection to guide the large language model to generate SQL that meets actual needs.

Benefits of technology

It significantly enhances the model's expertise acquisition ability in specific business scenarios, improves the accuracy of complex SQL generation, and ensures the correctness of SQL statements and the compliance of database logic.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119166662B_ABST
    Figure CN119166662B_ABST
Patent Text Reader

Abstract

The present invention belongs to the technical field of data query in the electric power field, and specifically relates to a method for constructing an SQL agent in the electric power field based on a KMDI chain. The method constructs a variety of domain knowledge bases based on professional knowledge such as data structure, data encoding, and data relationship in the electric power field, and constructs an SQL agent by designing a chain process consisting of three links: knowledge matching and decision-making, key knowledge distillation, and key knowledge injection, so as to realize the data encoding conversion of electric power professional terms in natural language questions with the support of a large language model, and accurately generate data query SQL statements that are adapted to the needs, so that business personnel in the electric power field can directly access and operate data through natural language.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of data query in the electric power field, and specifically relates to a method for constructing an SQL intelligent body in the electric power field based on a KMDI (Knowledge Matching-Distilling-Injection) chain. Background Art

[0002] After years of informatization construction, the power sector has built an informatization system covering all businesses, which is the key support for promoting process automation and digital transformation in the power sector. However, all kinds of data in the power sector are stored in structured databases. Only specially trained technicians who are familiar with data architecture have the ability to access data, and business personnel cannot directly access data.

[0003] In order to facilitate business personnel to directly access business data, the field of natural language processing began to study Text-to-SQL technology. Researchers initially used deep learning technology (such as Seq2Seq model, etc.) combined with attention mechanism, and realized the function of directly converting natural language into SQL queries through large-scale corpus training. However, due to the limitation of model parameters, when faced with complex problems or problems unrelated to the training corpus, the above technology is difficult to draw inferences and accurately generate SQL.

[0004] With the development of big language models and their applications, the combination of big language models and Text-to-SQL has shown good prospects. However, the existing Text-to-SQL technology applications have obvious shortcomings in data query in the power field, facing the following problems:

[0005] (1) The general large language model has weak domain knowledge capabilities: Although the general large language model performs well in reasoning in general fields, its professional knowledge in specific fields such as power marketing is relatively weak. It lacks a deep understanding of specific terminology, business processes, and industry rules in the field, which makes it difficult for the model to understand the expression of domain terminology and cannot accurately generate SQL queries that meet actual needs.

[0006] (2) The SQL content corresponding to the business scenario is complex: In complex business scenarios such as power marketing, SQL queries usually involve advanced operations such as multi-table joins, subqueries, aggregate functions, and grouping, which place higher demands on the model's semantic understanding and reasoning capabilities. When dealing with such complex tasks, large language models are prone to misunderstanding or reasoning errors, resulting in SQL generation results that do not meet expectations.

[0007] (3) The complexity of business databases in the power sector is high: The data query requirements of the power industry usually involve large enterprise-level databases. Due to the confusion of table names and field names caused by similar terms, large language models are prone to errors when processing this information, and it is difficult to accurately locate the library tables and fields required for SQL generation. In addition, the implementation of databases related to power sector business is relatively complex. Because there are many database tables and fields involved, the schema information input into the large language model during the Text-to-SQL task is lengthy, which can easily cause communication bottlenecks, inaccurate understanding, and distraction, and may even exceed the limit on the number of input tokens that the large language model can handle.

[0008] (4) The query problem description cannot match the database content: When generating SQL, the general large language model often directly uses part of the query problem content as the query content or condition of the SQL. However, in the business database of the power industry, the domain feature data is mostly stored in the form of coded items, which does not directly correspond to the business terms used by users when querying. This causes the large model to be unable to understand the essential needs of the domain and unable to match the business feature terms with the feature term coding items in the database in the generated SQL, resulting in incorrect SQL and failure to obtain the required data. Summary of the invention

[0009] In order to overcome the problems in the prior art, the present invention proposes a method for constructing SQL intelligent entities in the power field based on the KMDI chain.

[0010] The technical solution of the present invention to solve the above technical problems is as follows:

[0011] The present invention provides a method for constructing a domain SQL agent based on a KMDI chain, comprising the following steps:

[0012] Step 100: knowledge organization in the electric power data field and knowledge base construction, the knowledge base includes a SQL question-answer pair knowledge base, a database table structure knowledge base, an inter-table relationship knowledge base, and a coding mapping relationship knowledge base;

[0013] Step 200: Knowledge matching and decision-making: Using retrieval enhancement generation technology, based on user data query questions, retrieve similar question cases from the SQL question-answer pair knowledge base, design a prompt template for similar question-answer pair level judgment, use the understanding of the large language model to judge the similarity level of similar question-answer pairs, and make decisions on whether there are similar cases based on the similarity level to enter different key knowledge distillation routes;

[0014] Step 300: Key knowledge distillation: For data query problems with similar question-answer pairs, a difference entity extraction prompt template is designed, and the difference entities extracted by the large language model and similar case information are used to retrieve and obtain relevant knowledge; for data query problems without similar cases, a question entity extraction prompt template is designed to extract the key entities in the data query problem, and the database table information most relevant to the query problem is obtained based on a hybrid retrieval and positioning method of local sensitive hashing and vector similarity;

[0015] Step 400: key knowledge injection: based on the domain knowledge results obtained by key knowledge distillation, design a prompt template for generating SQL in the power field and a key knowledge formatting method, so that the large language model can fully utilize and understand the meaning of knowledge; inject the formatted key knowledge into the prompt template for generating SQL in the power field, and obtain a complete prompt with the power data domain knowledge required for querying questions, which is used to guide the large language model to generate SQL statements that meet the query requirements and conform to the actual logic of the power field business database;

[0016] Step 500: Construction of SQL agent in the power field: Take the work chain consisting of knowledge matching and decision-making, key knowledge distillation, and key knowledge injection as the thinking process of the agent, combine the ideas of agent-environment interaction, memory and feedback, design SQL execution actions and SQL verification mechanisms, and build SQL agent in the power field. Through the agent, the functions of SQL statement generation, SQL statement execution and result verification feedback are realized.

[0017] Furthermore, the step 100 also includes: collecting and organizing data query SQL question and answer pairs, database table structures, database table association relationships and data coding mapping relationships in the power field business to form serial number-question knowledge documents, serial number-question and answer pair knowledge documents, database table structure knowledge documents, inter-table relationship knowledge documents and data coding mapping relationship knowledge documents, and segmenting the serial number-question knowledge documents, database table structure knowledge documents, inter-table relationship knowledge documents and data coding knowledge mapping relationship documents, and performing vector embedding processing on the segmented text block data to convert it into a machine-readable form.

[0018] Furthermore, the step 200 specifically includes:

[0019] Step 210: embed the query question into a vector to obtain a query question vector;

[0020] Step 220: Calculate the similarity between the query question vector and each text block vector in the SQL question-answer pair knowledge base to obtain a similarity score; and return the content in the most similar text block as a retrieval matching result according to the similarity score;

[0021] Step 230: Design a case knowledge decision prompt template to enable the large language model to generate the required output through context prompts: for each SQL question-answer pair, determine its similarity level and output relevant information;

[0022] Define the case knowledge decision prompt template as a tuple , including the following parts:

[0023] ;

[0024] Among them, G represents the defined task role goal, that is, to give the large language model a clear role and explain the task goal of the role; ST represents the standard definition of similarity judgment, and a total of four similarity levels are designed, among which level 1 has the smallest similarity and level 4 has the largest similarity, representing a complete match; CS represents the set of candidate similar SQL question-answer pairs {cases}; T represents the target database type {db_type} for generating SQL; W represents important reminders, including important tips and constraints when completing tasks; O represents the output format of the task;

[0025] Step 240: Make a decision on whether there are similar cases based on the similarity level results, extract the required information, and enter different key knowledge distillation routes.

[0026] Furthermore, in step 240, if there are cases with a similarity level greater than or equal to 3, the process switches to the SQL knowledge distillation route with cases; if the maximum similarity level is less than 3, the process switches to the knowledge distillation route without cases.

[0027] Furthermore, in step 300, for data query problems with similar question-answer pairs, a difference entity extraction prompt template is designed, and the difference entities extracted by the large language model and similar case information are used to retrieve and obtain relevant knowledge, specifically including:

[0028] Step 310: Obtain schema information of each relevant table from the database table structure knowledge base to obtain a table schema knowledge set;

[0029] Step 320: Design a different entity extraction prompt template, combine it with the large language model, determine the parts of the similar cases that are different from the input query question, and extract each different entity name and corresponding information;

[0030] Among them, the difference entity extraction prompt template is defined as a tuple , including the following parts:

[0031] ;

[0032] Among them, G represents the defined task role goal, that is, to give the large language model a clear role and explain the task goal of the role; I p It represents an important reference step for extracting differential entities; O represents the output format of the task; Q represents the input query question {question}; CK represents the set of similar SQL question-answer pairs {cases};

[0033] Step 330: Search the entities with differences in the encoding mapping relationship knowledge base, sort the search results, and obtain the encoding information of the entities.

[0034] Furthermore, the step 330 specifically includes:

[0035] For the difference entity, different coding search conditions are constructed according to whether there is a corresponding column name. If there is a corresponding column name, the column-entity coding knowledge form is used as the search condition; if the corresponding column name is empty, the difference entity name is directly used as the search condition;

[0036] The encoded search condition is embedded in text and searched to obtain the most relevant predecessor of the difference entity. k If there is a corresponding column name search, take k =3 as the search result; if the corresponding column name does not exist, take k =5 as the search result to get more candidates;

[0037] The encoding retrieval results are sorted using a pre-trained re-ranking model to retrieve the relevant encoding mappings for all differential entities.

[0038] Furthermore, in step 300, for data query problems without similar cases, a question entity extraction prompt template is designed to extract key entities in the data query problem, and based on a hybrid retrieval and positioning method of local sensitive hashing and vector similarity, the database table information most relevant to the query problem is obtained, specifically including:

[0039] Step 350: Design a question entity extraction prompt template to extract key entities in the data query question.

[0040] Define the question entity extraction prompt template as a tuple , including the following parts:

[0041] ;

[0042] Among them, G represents the defined task role goal, that is, to give the large language model a clear role and explain the task goal of the role; W represents important reminders, including important tips and constraints when completing the task; O represents the output format of the task; Q represents the input query question {question};

[0043] Through the question entity extraction prompt template, the large language model extracts each key entity in the question and determines whether it is a query condition;

[0044] Step 360: Use the keyword retrieval and table location method of local sensitive hashing to screen and exclude redundant information and quickly lock the relevant data table, and obtain the schema of the relevant table;

[0045] Step 370: Use the question embedding vector to retrieve relevant knowledge in the database table structure knowledge base and the inter-table relationship knowledge base, and perform encoding mapping relationship knowledge retrieval on the entity names that meet the conditions to obtain the relevant table schema, inter-table relationship and encoding mapping relationship set.

[0046] Furthermore, it also includes: filtering the code mapping relationship search results:

[0047] For each code mapping relationship retrieval result, extract the corresponding code column name according to the organization form, expressed as:

[0048] ;

[0049] Among them, cut( r ik ) method means to split the content according to the separator; [0] means to take the first split result; r ik Indicates the first k Search results, Rc ik express r ik The corresponding encoding column name;

[0050] The relationships corresponding to the column names that do not belong to the extracted table schema are excluded, and the knowledge of the encoding mapping relationships that meet the conditions is retained to ensure that the encoding mapping relationships that are finally retained match the fields in the table structure.

[0051] Furthermore, the step 400 includes:

[0052] Step 410: Design and format the key knowledge representation form, including formatted similar cases, table schema, all coding mapping relationships and time information;

[0053] Step 420: Designing SQL to generate prompt template;

[0054] Define the SQL generation prompt template as P G , including the following parts:

[0055] ;

[0056] Among them, G represents the defined task role goal, that is, to set a clear role for the large language model and explain the task goal of the role; W represents important reminders, including important tips and constraints when completing the task; O represents the output format of the task; CK represents the set of similar SQL question-answer pairs {cases}, which provides domain business knowledge reference in the form of similar cases; TK is the target database type. When the database type in the similar case is inconsistent with the target database type, it prompts to pay attention to the conversion of the database type; SK represents the schema knowledge of the relevant table, which is used to provide the necessary table structure information for generating SQL; RK represents the relationship knowledge between the relevant tables, which provides the primary and foreign key information when multiple tables are associated; EK represents the knowledge of the relevant encoding mapping relationship, which is used to provide entity-encoding correspondence information, so that the large language model can convert the entities expressed in natural language into corresponding encodings when generating SQL; DT represents the current time information to cope with the problem of real-time query needs;

[0057] Step 430: Inject the formatted similar cases, table schema, code mapping relationship, time information and inter-table relationship knowledge into the SQL generation prompt template to obtain a complete SQL generation prompt to meet the professional knowledge requirements in the power field.

[0058] Furthermore, the step 500 includes:

[0059] Step 510: constructing an SQL execution action tool for parsing SQL statements from the results generated by the large language model and executing the SQL statements by connecting to a corresponding database;

[0060] Step 520: Construct verification method V G , contains the following parts:

[0061] ;

[0062] Among them, FV represents result format verification. After the large language model generates content, it verifies whether the result meets the format requirements in the prompt. If not, there is no need to perform SQL execution, but the reason for the verification failure is fed back to the SQL agent in the power field. TV represents code mapping relationship verification. Regular expressions are used to extract the where condition items in the SQL statement, and the condition columns and condition values ​​in the condition items are extracted at the same time, and compared with the input code mapping relationship. If the condition column and the corresponding condition value are inconsistent with a certain code mapping relationship, it is judged that the verification fails, and the specific condition item that failed is returned. IV represents execution result verification. The purpose is to verify the execution result of SQL and confirm that the generated SQL can be executed normally and return data. If the execution fails, the returned failure result is fed back to the SQL agent in the power field.

[0063] Step 530: constructing SQL agent in the power field;

[0064] Combining the knowledge matching and decision-making process of step 200, the key knowledge distillation process of step 300, the key knowledge injection process of step 400, the SQL execution action / tool ​​T defined in step 510, and the verification method constructed in step 520 V G , and a large language model to jointly build SQL agents in the power field:

[0065] ;

[0066] in, G A represents the SQL agent in the power field; Q represents the input question, i.e., the task goal; MP, DP, IP They respectively represent the stage processes to which step 200, step 300, and step 400 belong, and M represents a large language model.

[0067] Compared with the prior art, the present invention has the following technical effects:

[0068] (1) In order to solve the problem of weak domain knowledge capability of general large language models, the present invention introduces a multi-type domain knowledge retrieval and distillation module before the SQL agent generates content. This module carefully organizes and constructs various types of knowledge bases, including SQL question-answer pairs, database table structures, relationships between tables, and encoding mapping relationships in power data queries. Knowledge is organized and stored in a specific form to improve the accuracy of knowledge retrieval and matching. Knowledge distillation filters and distills the extracted domain knowledge, removes redundant information, and retains only the most relevant content to ensure that the model can efficiently utilize this knowledge. This method significantly enhances the model's ability to acquire professional knowledge in specific business scenarios, constructs prompts based on various types of power domain knowledge, and effectively solves the problem of insufficient domain knowledge when general large language models generate SQL.

[0069] (2) In view of the problem of complex SQL content corresponding to business scenarios, the present invention designs a knowledge matching and decision-making method for similar cases based on the SQL question-answer knowledge base, and obtains the complex relationship knowledge of the table according to the different situations of whether there are similar cases or not. When there are similar cases, the necessary knowledge related to generating SQL is obtained by analyzing the consistency and difference between the query question and similar cases and the information in the SQL of similar cases, and prompting the large language model to imitate the SQL in similar cases for generation, which effectively reduces the difficulty of complex query tasks; when there are no similar cases, the association relationship information of the tables is retrieved through the constructed inter-table relationship knowledge base and input into the large language model, so that the large language model can also understand the multi-table association relationship when there are no similar cases, thereby improving the accuracy of complex SQL generation.

[0070] (3) In response to the high complexity of the business database in the power sector, the present invention is based on the construction of multiple domain knowledge bases and case knowledge decision results, combined with two knowledge distillation routes, and adopts different methods to retrieve and locate the required library tables and field information. When there are similar cases, the table name information can be directly extracted from the similar cases to obtain the required schema efficiently and accurately; when there are no similar cases, a hybrid retrieval and positioning method based on LSH and vector similarity is proposed, which can quickly locate the relevant library table fields through query questions and extracted key entities of the questions. Through the two routes, it is ensured that only the schema information of the relevant tables is finally input into the large language model, which greatly reduces the redundant content of the input.

[0071] (4) To address the problem that the query question description cannot match the database content, the present invention proposes a method for converting power business entities and codes based on the constructed coding item knowledge base. The information of the entity to be retrieved is obtained through difference entity extraction or problem entity extraction. The knowledge of candidate coding items is obtained more accurately by using two different methods: vector similarity retrieval and LSH positioning. Then, by designing an SQL generation prompt, the coding mapping relationship knowledge is input into the large language model, so that the large language model has coding conversion knowledge to directly generate SQL statements that conform to the database coding. BRIEF DESCRIPTION OF THE DRAWINGS

[0072] 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.

[0073] Figure 1 This is a schematic diagram of the method for constructing SQL intelligent entities in the power field based on the KMDI chain proposed in the present invention.

[0074] Figure 2 Four knowledge base text content display diagrams constructed for the present invention.

[0075] Figure 3 This is the knowledge matching and decision-making results in Experiment 1.

[0076] Figure 4 The specific results of differential entity extraction in Experiment 1.

[0077] Figure 5 These are the specific results of retrieval and filtering of the encoding mapping relationship in Experiment 1.

[0078] Figure 6 This is the SQL generation prompt and specific results of SQL generation after filling in Experiment 1.

[0079] Figure 7 This is a graph showing the results after the SQL generated in Experiment 1 is executed.

[0080] Figure 8 This is a comparison chart of the effects of the general method of Experiment 1 and the method of the present invention in generating SQL with cases.

[0081] Fig. 9 This is the knowledge matching and decision-making results in Experiment 2.

[0082] Fig.10 The results of question entity extraction and LSH retrieval positioning in Experiment 2.

[0083] Fig.11 This is a comparison chart of the effects of the general method of Experiment 2 and the method of the present invention on case-free SQL generation.

[0084] Fig.12 This is a graph showing the results after the SQL generated in Experiment 2 is executed.

[0085] Fig.13 The specific content of the case knowledge decision prompt template is shown.

[0086] Fig.14 The specific content of the difference entity extraction prompt template is shown.

[0087] Fig.15 Shows the specific content of the question entity extraction prompt template.

[0088] Fig.16 Shows the specific content of the SQL generation prompt template. DETAILED DESCRIPTION

[0089] 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 in the present invention have the same meaning as those commonly understood by technicians in the technical field of the present invention.

[0090] In order to overcome the above challenges, the present invention proposes a method for constructing SQL agents in the power field based on the KMDI (Knowledge Matching-Distilling-Injection) chain for data query scenarios in the power field. The method constructs a variety of domain knowledge bases based on professional knowledge such as data structure, data encoding and data relationship in the power field, and realizes the intelligent conversion of natural language to SQL in the power field and constructs SQL agents by designing a chain process composed of links such as knowledge matching and decision-making (Knowledge Matching), key knowledge distillation (Knowledge Distilling) and key knowledge injection (Knowledge Injection), so that business personnel in the power field can directly access and operate data through natural language under the support of LLM (Large Language Model). Based on the results of knowledge matching and decision-making, two routes of knowledge distillation methods are designed and used, case knowledge distillation and case-free knowledge distillation, to ensure the adequacy of knowledge distillation in different problem scenarios; in the knowledge injection link, a proprietary prompt is designed and combined with the knowledge after distillation in the power field to carry out knowledge injection, realize the encoding conversion of professional terms in natural language questions, and accurately generate data query SQL statements that are compatible with data access requirements.

[0091] In this embodiment, refer to Figure 1 , a method for constructing a domain SQL agent based on a KMDI chain, comprising the following steps:

[0092] Step 100: knowledge organization in the electric power data field and knowledge base construction, the knowledge base includes a SQL question-answer pair knowledge base, a database table structure knowledge base, an inter-table relationship knowledge base, and a coding mapping relationship knowledge base;

[0093] Step 200: Knowledge matching and decision-making: Using the retrieval enhancement generation technology, based on the user data query question, retrieve similar question cases from the SQL question-answer pair knowledge base, design a prompt template for similar question-answer pair level judgment, use the understanding of the large language model to judge the similarity level of similar question-answer pairs, and make a decision on whether there are similar cases based on the similarity level to enter different key knowledge distillation routes;

[0094] Step 300: Key knowledge distillation: For data query problems with similar question-answer pairs, a difference entity extraction prompt template is designed, and the difference entities extracted by the large language model and similar case information are used to retrieve and obtain relevant knowledge; for data query problems without similar cases, a question entity extraction prompt template is designed to extract the key entities in the data query problem, and the database table information most relevant to the query problem is obtained based on a hybrid retrieval and positioning method of local sensitive hashing and vector similarity;

[0095] Step 400: key knowledge injection: based on the domain knowledge results obtained by key knowledge distillation, design a prompt template for generating SQL in the power field and a key knowledge formatting method, so that the large language model can fully utilize and understand the meaning of knowledge; inject the formatted key knowledge into the prompt template for generating SQL in the power field, and obtain a complete prompt with the power data domain knowledge required for querying questions, which is used to guide the large language model to generate SQL statements that meet the query requirements and conform to the actual logic of the power field business database;

[0096] Step 500: Construction of SQL agent in the power field: Take the work chain consisting of knowledge matching and decision-making, key knowledge distillation, and key knowledge injection as the thinking process of the agent, combine the ideas of agent-environment interaction, memory and feedback, design SQL execution actions and SQL verification mechanisms, and build SQL agent in the power field. Through the agent, the functions of SQL statement generation, SQL statement execution and result verification feedback are realized.

[0097] The following is a detailed explanation of each of the above steps:

[0098] Step 100: Electricity data domain knowledge organization and knowledge base construction. The database table structure corresponding to the electric power business, the relationship between tables, the coding standard of the electric power domain data, and the common data query SQL question and answer pairs in the field are taken as domain knowledge; different knowledge organization forms are designed for the relevant knowledge and the corresponding domain knowledge bases are constructed respectively, and the knowledge is stored in a vector database or ES (Elasticsearch).

[0099] To accurately generate data query SQL based on the natural language of business personnel in the power field, professional domain knowledge and domain data management knowledge are required. In order to generate complex SQL statements that conform to business logic, it is first necessary to create a relevant domain knowledge base to help the large language model fully understand the power data query needs. The present invention fully analyzes the characteristics of power data query problems in natural language discourse and domain data design patterns, and sorts out the relevant knowledge in four aspects, including power data query SQL question and answer pairs, database table structure, table relationships and data encoding mapping relationships, and then designs different knowledge organization forms and constructs knowledge bases for different knowledge contents; in order to improve the convenience and accuracy of the large language model's retrieval of relevant knowledge, the knowledge base is vectorized and finally saved in a vector database or ES, so that the intelligent agent can more accurately retrieve and use the corresponding knowledge and finally generate SQL. The specific steps are as follows:

[0100] Step 110: Collect, organize and store power domain knowledge.

[0101] As an example, this step may include the following sub-steps:

[0102] Step 111: Collect, organize and store SQL question-answer pairs for power field data query to form a sequence number-question knowledge document and a sequence number-question-answer pair knowledge document.

[0103] Extract the query requirements (business natural language expression) of existing scenarios in the existing power data query business and the corresponding data query SQL to form a domain SQL question-answer pair, so that similar cases can be obtained as references in the future to improve the accuracy of the generated results.

[0104] Design a knowledge organization method for SQL question-answer pairs, splitting the question-answer pairs into two types of knowledge organization structures. One is the JSON-like knowledge structure corresponding to "serial number-question". In this structure, the question is separated from the SQL so that the similarity calculation during subsequent retrieval will not be interfered by the SQL content. Its specific form is as follows:

[0105] [{"PID": "1", "Q": "xxxxxx"}, {"PID": "2", "Q": "xxxxxx"}, ...];

[0106] Among them, PID is the key name of the serial number of the question, and Q is the key name of the specific content of the question.

[0107] The second is the JSON knowledge structure corresponding to the "serial number-question-answer pair". The question serial number in the "serial number-question" structure is used as the primary key. The corresponding SQL question-answer pair can be directly found according to the serial number. For example, the specific form of the structure of question serial number 1 is as follows:

[0108] {

[0109] "1":

[0110] {"Question": "xxxxxx", "SQL": "xxxxxx", "Database_Type": "xxxxxx"},

[0111] / / Other SQL question and answer pairs

[0112] }

[0113] Among them, Question is the key of the specific content of the question; SQL is the key of the corresponding SQL specific content; Database_Type is the database type corresponding to the SQL, so that SQL conversion of different databases can be adapted later.

[0114] For the "serial number-question" knowledge, it is stored in txt text format, and a special separator symbol is inserted between every two question structures for subsequent processing, such as {"PID": "1", "Q": "xxxxxx"} / @ / {"PID": "2", "Q": "xxxxxx"} / @ / …; for the "serial number-question-answer pair" knowledge, it is stored in json file, which can be directly read, loaded and used.

[0115] Step 112: Collect, organize and store database table structures to form database table structure knowledge documents.

[0116] Extract the structural knowledge of power business related tables from the database, including table name, table comment, column name, column comment, etc., so that the data structure information can be obtained and SQL can be generated later. Design a knowledge organization method similar to json for the table structure, which is clearer and more concise than the structure of database DDL (Data Definition Languages) statements. For each table, it is organized in the following form:

[0117] {

[0118] "table 1 ":

[0119] "table 1 (column_name 1 (comment 1 ), column_name 2 (comment 2 ), ...), table 1 _comment",

[0120] / / Library table structure information of other tables

[0121] }

[0122] Among them, table 1 is the name of the table, which serves as the primary key in the structure; table 1 _comment represents the comment of the table; column_name and comment are the name of each column and the comment information of the column respectively.

[0123] All library table structure knowledge is stored in json file format and txt text format respectively, and a special separator is inserted between each library table structure stored in txt text format.

[0124] Step 113: Collect, organize and store database table associations to form a knowledge document on the relationship between tables.

[0125] Extract the association relationship knowledge of power business related database tables from the database, including table name, table comment, foreign key and the table corresponding to the foreign key, and design a knowledge organization method similar to JSON format to correspond each table to its associated information one by one. For each database table with an associated relationship, organize it in the following form: [

[0127] {

[0128] "TableName": "table 1 ",

[0129] "Description": "xxxxxxx",

[0130] "ForeignKeys": {

[0131] "fk 1 ": "table m .column l ",

[0132] "fk 2 ": "table n .column k ",

[0133] / / Other related field information

[0134] }

[0135] },

[0136] / / Table association information of other tables ]

[0138] Among them, TableName is the key name of the table name, such as table1 ; Description is the key name of the table annotation; the content in ForeignKeys is the association relationship information corresponding to the table, such as fk1 is one of the foreign keys of the table, and m Column l Columns are associated.

[0139] The associative relationship knowledge of all library tables is stored in txt text format, and special separators are inserted between each relationship structure.

[0140] Step 114: Collect, organize and store data coding mapping relationships to form data coding knowledge documents.

[0141] Extract the required information from the table in the database that specifically stores the coding correspondence, including the coding item name, coding value, and the column name corresponding to the coding, and organize the information in a specific form so that it can be effectively used in subsequent retrieval and generation. The specific form is as follows:

[0142] Column---Coding_name: Coding_value;

[0143] Among them, Column represents the column name corresponding to the code, Coding_name represents the name of the coding item, and Coding_value represents the coding value. For example, in the "User Category (YHLB)" column, the coding value corresponding to the "Resident Household" coding name is "000001", and its coding knowledge organization form is "YHLB---Resident Household: 000001;".

[0144] All encoding mapping relationship knowledge is stored in txt text format, and special separators are inserted between each encoding mapping relationship.

[0145] Step 120: segment the serial number-question knowledge document, the library table structure knowledge document, the table relationship knowledge document and the data encoding mapping relationship knowledge document; perform vector embedding processing on the segmented text block data to convert it into a machine-readable form.

[0146] In order to ensure that the large language model can efficiently and accurately utilize relevant knowledge, while avoiding the interference of contextual restrictions and redundant knowledge on the results, the special separator used in the previous knowledge base construction is used to segment the sequence number-question knowledge documents, library table structure knowledge documents, table relationship knowledge documents, and data encoding mapping relationship knowledge documents stored in steps 111 to 114. In this way, continuous documents will be converted into a series of shorter and more targeted fragments, so that each fragment contains only the required precise information.

[0147] The segmented text block data is vectorized and converted into a machine-readable form. The value of the embedded vector can also be used to calculate similarity. Text vectorization first requires the text to be segmented. T , the word segmentation result can be expressed as , where each element Represents a word; secondly, the word segmentation results are matched according to the dictionary of the embedding model, and the results are converted into the corresponding token form in the dictionary, which is expressed as ,in, Represents the start of the input sequence, Used to indicate the end of the entire sequence; the converted sequence is input into the pre-trained model for embedding calculation. For example, using the Sentence-BERT embedding model, it can be expressed as:

[0148] ;

[0149] in, The result is a vector embedding. In this way, each text block can be represented as a multi-dimensional vector, which can be used for text retrieval.

[0150] Step 130: Store the vectorized serial number-question knowledge document, library-table structure knowledge document, inter-table relationship knowledge document and data encoding mapping relationship knowledge document in different vector libraries respectively, to form four knowledge bases in vector form, including the serial number-question knowledge base (i.e., SQL question-answer pair knowledge base), library-table structure knowledge base, inter-table relationship knowledge base and data encoding knowledge base.

[0151] The knowledge base in vector form optimizes the data storage structure of high-dimensional vectors, making the storage and retrieval of large-scale vector data more efficient. Therefore, the vectorized sequence-question knowledge documents, library table structure knowledge documents, inter-table relationship knowledge documents and data encoding knowledge documents are stored in different vector knowledge bases respectively.

[0152] Step 200: Knowledge matching and decision-making: Using retrieval enhancement generation technology, retrieve similar question cases from the SQL question-answer pair knowledge base based on user data query questions, design a prompt template for similar question-answer pair level judgment, use the understanding of the large language model to judge the similarity level of similar question-answer pairs, and design a decision-making method for similar cases to improve the reliability of knowledge matching so that more accurate decisions can be made on whether there are similar cases or not based on the similarity level results, and enter different key knowledge distillation routes.

[0153] In order to make fuller use of the existing SQL question-answer pair knowledge to assist in the generation of power field data query SQL and avoid interference of irrelevant question-answer pairs with the generated results, the present invention specifically proposes a knowledge matching and decision-making method based on SQL question-answer pair similarity measurement, combining RAG (Retrieval Augmented Generation) technology and a large language model to retrieve cases similar to power data query questions expressed in natural language from the SQL question-answer pair knowledge base, quantify their similarity and make decisions on whether there are similar cases based on the similarity level. The specific steps are as follows:

[0154] Step 210: embed the query question into a vector to obtain a query question vector.

[0155] The query question raised in natural language is converted into a multi-dimensional vector form in the same way as step 120, so that the question can be matched with the knowledge, which is expressed as:

[0156] ;

[0157] in, is the problem vector, Tokens corresponding to the query question.

[0158] Step 220: Calculate the similarity between the query question vector and each text block vector in the sequence-question knowledge base to obtain a similarity score; and return the content in the most similar text block as a retrieval matching result based on the similarity score.

[0159] The similarity between the query question vector and each text block vector in the sequence number-question vector library is calculated by cosine similarity to obtain a similarity score, where the cosine similarity calculation formula is:

[0160] ;

[0161] in, Indicates i The embedding vector of a text block, is the similarity score, Represents the magnitude of a vector.

[0162] According to the similarity score, several vectors most relevant to the query question are retrieved, and the content in the corresponding text block is returned as the search matching result, that is, the "serial number-question" text. The result D is expressed as:

[0163] ;

[0164] in, k is the number of matches set. dFor each successfully matched text block, d k For the k The content of the text block that matches successfully. k The default is 5.

[0165] Step 230: Design a prompt word template for judging the level of similar question and answer pairs, use the understanding ability of the large language model to judge the similarity level of similar question and answer pairs, and output relevant information.

[0166] Since the retrieved k The question text is only the most similar calculation result in the knowledge base, which does not necessarily mean that it is relevant to the query question. Therefore, the large language model is used to further judge the similarity of the retrieval results to obtain the most similar SQL question-answer pairs.

[0167] The large language model takes a sequence input, i.e. a series of tokens. , calculate the probability distribution of the next word based on the input sequence and the output of each step P , and gradually generate a sequence , which can be expressed as:

[0168] ;

[0169] in, Indicates i The output sequence before the word; Indicates that in a given X Under the conditions Y The conditional probability distribution of Y The length of Y The number of words in the sequence; Indicates that in a given X and before i− 1 Sequences generated by words Under the conditions i The probability distribution of words.

[0170] Based on this, a case knowledge decision prompt template is designed to enable the large language model to generate the required output through context prompts: for each SQL question-answer pair, its similarity level is judged and relevant information is output. The case knowledge decision prompt template is defined as a tuple , contains the following parts:

[0171] ;

[0172] Among them, G represents the defined task role goal, that is, to give the large language model a clear role and explain the task goal of the role; ST represents the standard definition of similarity judgment, and a total of four similarity levels are designed, among which level 1 has the smallest similarity and level 4 has the largest similarity, representing a complete match; CS represents the set of candidate similar SQL question-answer pairs ({cases}); T represents the target database type {db_type} for generating SQL; W represents important reminders, including important tips and constraints when completing tasks; O represents the output format of the task. See the case knowledge decision prompt template for details. Fig.13 .

[0173] Step 240: Make a decision on whether there are similar cases based on the similarity level results, extract the required information, and enter different key knowledge distillation routes.

[0174] As an example, this step may include the following steps:

[0175] Step 2401: Use the data retrieved in step 220 k The question number content is extracted according to the PID key, and then the SQL question-answer pair knowledge corresponding to each question number is obtained as a candidate similar case by reading the sequence number-question-answer pair json file saved in step 111, thereby forming a set of candidate similar SQL question-answer pair cases to be determined.

[0176] Step 2402: Fill the SQL question-answer pair set and the target database type into the case knowledge decision prompt template constructed in step 230, input the large language model and guide it to determine the specific similarity level between the user question and each candidate case.

[0177] Step 2403: Obtain and parse the output results of the large language model, including the similarity levels of all output cases, cases with similarity greater than or equal to 3, and their corresponding SQL table names. The process is represented as follows:

[0178] ;

[0179] ;

[0180] ;

[0181] in, S max Indicates the maximum similarity level in the output results; O i The output of the large language model i Candidate similar cases; O i ['sim_level'] meansO i Similarity level of Sc Indicates the case number with similarity greater than or equal to 3; O i ['chunk_id'] indicates the text block number corresponding to the candidate similar case; St Represents the extracted table name set; O i ['tables'] means O i The table names involved.

[0182] Step 2404: Determine whether to subsequently switch to different key knowledge distillation routes based on the similarity level of the case with the maximum similarity in the output results: if there is a case with a similarity level greater than or equal to 3, switch to the SQL knowledge distillation route with cases; if the maximum similarity level is less than 3, switch to the knowledge distillation route without cases.

[0183] The key knowledge distillation route is expressed as:

[0184] ;

[0185] Among them, R represents the key knowledge distillation route, l s Represents a route with cases, l n Represents a route with no cases.

[0186] Step 300: Key knowledge distillation: Based on the results of knowledge matching and decision-making, two strategic routes are designed to distill key knowledge: For data query problems with similar question-answer pairs, a difference entity extraction prompt template is designed, and the difference entities extracted by the large language model and similar case information are used to retrieve and obtain relevant knowledge; for data query problems without similar cases, a question entity extraction prompt template is designed to extract key entities in the data query problem, and based on the hybrid retrieval and positioning method of local sensitive hashing and vector similarity, the relevant library table structure and relevant encoding mapping relationship are more accurately obtained, thereby avoiding model hallucination problems and inefficiency problems caused by too much irrelevant table information and encoding information.

[0187] The general large language model usually lacks the ability to understand business logic when generating SQL in the power field, and needs to rely on information such as the table structure of the database for effective query generation. Due to different knowledge backgrounds in different situations with and without similar cases, the present invention proposes a two-route key knowledge distillation method, which designs knowledge distillation processes with and without cases respectively, so as to provide sufficient and accurate domain knowledge support for the model during the SQL generation process, thereby improving the accuracy and reliability of generated SQL statements. Among them, route one is steps 310 to 340, and route two is steps 350 to 380. The specific steps are as follows:

[0188] Route 1: SQL knowledge acquisition with cases: For data query problems with similar question-answer pairs, design a prompt template for extracting different entities, and use the different entities extracted by the large language model and similar case information to retrieve and acquire relevant knowledge. The specific steps include:

[0189] Step 310: Acquire the schema information of each relevant table from the database table structure knowledge base to obtain a table schema knowledge set.

[0190] According to the similarity level condition defined in step 230, when the similarity between the current task and the case is greater than or equal to 3, the table used by the SQL corresponding to the task is highly consistent with the similar case, and only some query conditions are inconsistent. Therefore, the schema information of each table is obtained from the json format file of the library table structure knowledge stored in step 112 using the relevant table name set extracted in step 240, and the table schema knowledge set is expressed as:

[0191] ;

[0192] in, Indicates i A knowledge set of related tables; K sc Represents the knowledge file of the library table structure. St i Indicates i The name of the related table. sc The schema of the table.

[0193] Step 320: Design a difference entity extraction prompt template, combine it with the large language model, determine the parts of similar cases that are different from the input query question, and extract each different entity name and corresponding information to make better use of the knowledge contained in similar cases and provide retrieval conditions for subsequent encoding conversion.

[0194] As an example, this step may include the following sub-steps:

[0195] Step 3201: Design a difference entity extraction prompt template to guide the large language model to complete the difference entity extraction task.

[0196] Define the difference entity extraction prompt template as a tuple , contains the following parts:

[0197] ;

[0198] in, I p It represents an important reference step for extracting differential entities. Q represents the input query question ({question}), CK represents the set of similar SQL question-answer pairs ({cases}); the meanings of G and O are the same as those of the case knowledge decision prompt in step 230. For details of the differential entity extraction prompt template, see Fig.14 , "{}" in the template is the parameter to be input.

[0199] Step 3202: Based on the result of step 240, SQL question-answer pair cases with a similarity level greater than or equal to 3 and the input query question are filled into the difference entity extraction prompt template and input into the large language model, and the difference entity extraction prompt is used to enable the large language model to output relevant results.

[0200] Step 3203: Obtain entity information sequences in the output results of the large language model, parse the output results, and extract each entity name and corresponding information with differences in the results. The process is represented as follows:

[0201] ;

[0202] ;

[0203] in, Dn represents the extracted set of entity names with differences; Oe i The output of the large language model i Entity related information, extract the entity name according to the 'entity' primary key; Dc Indicates the possible corresponding column name set of the difference entity in the similar case SQL, extracted according to the 'column' primary key. Oe i If ['column'] is empty, the result is an empty string.

[0204] Step 330: Search the difference entity names in the encoding mapping relationship knowledge base, sort the search results, and obtain the encoding information of the entity.

[0205] Since there is a lack of sufficient information about differential entities in similar cases, in order to enable the large language model to have encoding mapping relationship knowledge when generating SQL and convert entity names into corresponding encoding values, it is necessary to retrieve the corresponding encoding information in the encoding mapping relationship knowledge base based on the differential entity names.

[0206] For the difference entity Dn i , construct different encoding search conditions according to whether there is a corresponding column name;

[0207] If the corresponding column name exists Dc i , organize it into a similar coded knowledge form as in step 114 as the retrieval condition; if the corresponding column name is empty, directly use the entity name as the retrieval condition, that is:

[0208] ;

[0209] in, CondE i Indicates i The coded search condition for an entity is "{ }" means to fill in the parameter " " corresponding content.

[0210] Will CondE i Embed the text and retrieve the most relevant predecessors for the entity k The embedding method and retrieval method of the coding mapping relationship knowledge are the same as those of step 210 and step 220. If there is a corresponding column name search, the retrieval condition is more accurate. k =3 as the search result; if the corresponding column name does not exist, take k =5 as the search result to get more candidates.

[0211] The encoding retrieval results are sorted using a pre-trained re-ranking model. The re-ranking model is a model used to finely sort the retrieval results. It can rearrange the retrieved candidate results according to the relevance between the query and the candidate items. Through the re-ranking model, the encodings most relevant to the entity are placed in the front row to enhance the attention effect of the large language model, which is expressed as:

[0212] ;

[0213] ;

[0214] in, R i For the i The coded search results of entities, R i' For the i The result of the encoded retrieval results of entities after being processed by the re-ranking model; rerank() Represents a reranking model. The present invention adopts the bge-reranker model for reranking.

[0215] The relevant encoding mapping relationship of all difference entities is retrieved through the above method, denoted as C= { r i1 , r i2 …, r ik , …, r mk},in, r ik Representing different entities Dn i After reordering k Search results.

[0216] Step 340: Filter the search results.

[0217] The previous k Each code mapping relationship result may contain irrelevant items, so the search results need to be filtered. For each code mapping relationship search result, extract its corresponding code column name according to the organization form, expressed as:

[0218] ;

[0219] Among them, cut( r ik ) method means to split the content according to the separator. Here, "---" is used to split the content to correspond to its organizational form. [0] means to take the first split result. Rc ik express r ik The corresponding encoding column name.

[0220] After extracting the column names in all the code mapping relationships, exclude the mapping relationships that do not belong to the column names in the table schema extracted in step 310, retain the code mapping relationship knowledge that meets the conditions, and ensure that the code mapping relationship finally retained matches the field in the table structure, thereby improving the relevance of the search results. It is expressed as:

[0221] ;

[0222] in, c i is the encoding mapping relationship set C i Item, T(c i )for c i The encoding column name, Sc The table schema set obtained in step 310, The filtered result.

[0223] Route 2: SQL knowledge acquisition without cases: For data query problems without similar cases, a problem entity extraction prompt template is designed to extract key entities in the data query problem, and based on the hybrid retrieval and positioning method of locality-sensitive hashing (LSH) and vector similarity, the relevant library table structure and related encoding mapping relationship are more accurately obtained. The specific steps include:

[0224] Step 350: Design a question entity extraction prompt template to extract key entities in the data query question.

[0225] In the absence of similar cases for reference, we can only extract entity information from the input query question. Therefore, we design a question entity extraction prompt template to guide the large language model to extract key words and phrases in the question, and enhance the means of subsequent knowledge acquisition through full analysis of the question.

[0226] Define the question entity extraction prompt template as a tuple , contains the following parts:

[0227] ;

[0228] Among them, the meanings of G, W, and O are the same as those in step 230 case knowledge decision prompt; Q The meaning is the same as the difference entity extraction prompt in step 310 in route one.

[0229] See the prompt template for question entity extraction for details. Fig.15 , "{}" in the template is the parameter to be input.

[0230] The input query question is filled into the question entity extraction prompt template and input into the large language model. Through the question entity extraction prompt template, the large language model extracts each key entity in the question and determines whether it is a query condition. After obtaining the output result of the model, the result is parsed to obtain each entity name and condition identifier in the result, that is, whether the entity is a query condition, which is expressed as:

[0231] ;

[0232] ;

[0233] in, En Represents the set of extracted entity names, Ec Identify the set of conditions, Represents the entity extraction results output in output format O.

[0234] Step 360: Use the keyword retrieval and table location method of local sensitive hashing to screen and exclude redundant information, quickly lock the relevant data table, and obtain the schema of the relevant table.

[0235] Aiming at the problem that it is difficult to quickly and accurately locate tables related to user query needs in vector databases based on the RAG method in large-scale databases in the power industry, a keyword retrieval and table location method based on local sensitive hashing (LSH) is proposed. It can efficiently filter and exclude redundant information and quickly lock relevant data tables based on specific data information in the database table. As an efficient approximate nearest neighbor search technology, LSH quickly identifies the database entries that best match specific keywords.

[0236] As an example, this step may include the following sub-steps:

[0237] Step 361: Create a database LSH index;

[0238] a. Table field screening:

[0239] ;

[0240] in, T Represents a collection of tables in a database. F(T) express T The field collection of Type(f) Representation field f The data type of the . F filtered A symbol that indicates a set of fields that have been initially screened, indicating that T In the , all fields are of character type ( VARCHAR, CHAR )and Int This set is made up of fields that exclude non-character and non- Int Type of field (such as DECIMAL, DOUBLE, DATE etc.) to obtain;

[0241] b. Exclude specific field names:

[0242] ;

[0243] ;

[0244] in, Name(f) Representation field fThe name of the F final The symbol for further filtering of the set of fields is in F filtered On the basis of , we further exclude those fields whose names end with specific strings ("bh", "mc", "dz", "jc", "lj", "dh", "hm", "bs", "zh", "id"). These fields usually represent numbers, names, addresses, abbreviations, paths, telephone numbers, numbers, logos, account numbers, IDs, etc., and are not suitable for uniqueness analysis or LSH index construction.

[0245] c. Uniqueness analysis:

[0246] ;

[0247] ;

[0248] in, Count(f) Representation field f The number of unique values ​​of TotalCount(T) Representation field f Table T The total amount of data, Thred Represents the threshold, set to 0.25. F unique It means calculating the ratio of the number of unique values ​​of each field to the total amount of data in the table where the field is located. If the ratio exceeds the threshold of 0.25, the field is excluded.

[0249] d. Build a dictionary of unique field values;

[0250] After completing steps a, b, and c, count the unique values ​​of each field and organize the results into a structured dictionary format:

[0251] ;

[0252] in, UniqueValues(f) Representation field f A set of unique values. D T The dictionary format is as follows:

[0253] {

[0254] "table_name": {

[0255] "column_name1": [v 1 , v 2 , v 3 , ...],

[0256] "column_name2": [v1 , v 2 , v 3, ...],

[0257] / / Other columns and field value key-value pairs

[0258] },

[0259] / / Other table and column key-value pairs

[0260] }

[0261] Among them, table_name represents the name of the table in the database, which is the key of the dictionary; the corresponding value is a nested dictionary, column_name is the field name, which is the key of the inner dictionary, and its value is a list containing all the unique values ​​of the field, that is, v. After the dictionary is built, the dictionary is serialized as pkl File format for persistent storage and subsequent tasks.

[0262] e. Code value restoration;

[0263] Since most of the field values ​​in the power business database are stored as coded values, it is necessary to store the dictionary D T The field value v of is restored to the corresponding coding item name so that it can match the problem entity name. Using the coding mapping relationship organized in step 114, it is organized into the following query format of the coding reverse mapping relationship:

[0264] {

[0265] Column1{Coding_value1:Coding_name1, Coding_value2:Coding_name2,…},

[0266] / / Other field encoding value mapping

[0267] }

[0268] The meanings of Column, Coding_value and Coding_name are the same as those in step 114.

[0269] Using the above sorted inverse mapping relationship, D T All field values ​​v under each field name (column_name) are restored to the coding item name (Coding_name), recorded as D T ’ .

[0270] f. Create LSH index:

[0271] ;

[0272] in, lsh Indicates based on D T ’ Initialized LSH data structure or object for subsequent similarity query operations; MinHashes Indicates based on D T ’ A set of signatures calculated to quickly estimate the similarity between data points; function make_lsh Responsible for creating LSH indexes, including the parameters involved signature_size, n_gram, threshold They are used to specify the length of the LSH signature (set to 100), n-gram The size of the segments (set to 2) and the threshold for hash matching (set to 0.3).

[0273] After the LSH index is built, it will be serialized and pkl The index is saved to a file in Python Pickle format. In this way, the index can be efficiently loaded and used for subsequent processing and analysis tasks, thereby achieving fast retrieval and data matching.

[0274] Step 362: keyword search and table positioning;

[0275] a. Keyword search:

[0276] Get the entity name set En extracted from the user query requirement in step 350 and the lsh model created in step 361. For each entity e, retrieve the most similar field value from the lsh model:

[0277] ;

[0278] in, Represents the field matching function of entity e in the lsh model, and M represents a table-field-field value mapping set.

[0279] b. Table location and schema acquisition:

[0280] Based on the filtered set M, count the number of relevant fields and field values ​​retrieved under each table, and sort the total number of table fields and field values ​​in descending order; obtain the top-k tables in the set and retain them as the tables most relevant to user needs to form a set of relevant table names Ts. Based on Ts, use the same method as step 310 to obtain the schema of each table in the set.

[0281] Step 370: Use the vector similarity retrieval method to embed the question vector in the database table structure knowledge base and the inter-table relationship knowledge base to retrieve relevant knowledge, and perform encoding mapping relationship knowledge retrieval on the entity names that meet the conditions to obtain the relevant table schema, inter-table relationship and encoding mapping relationship set.

[0282] Use the question embedding vector from step 210 , respectively search for knowledge related to the query question in the database table structure knowledge base and the table relationship knowledge base, the search method is the same as step 220, and the result is expressed as:

[0283] ;

[0284] ;

[0285] in, Sc' and Tr are the table schema and inter-table relationship knowledge obtained by vector retrieval respectively. m and n represent returning the m and n most similar results.

[0286] For the entity name set obtained in step 350 En If the condition is marked as 'true', the entity name is used as the search condition to perform code mapping relationship knowledge search. The search method is the same as step 330. The relevant code mapping relationship set C' of all searched entities is obtained, which is expressed as:

[0287] ;

[0288] ;

[0289] Among them, En' is the set of search items that meet the conditions, en i and ec i are the i-th item of En and Ec respectively, eni' is the i-th item of En', R( ) indicates vector retrieval.

[0290] Step 380: Filtering encoded knowledge;

[0291] The same method as step 340 in route 1 is used to filter the encoded knowledge.

[0292] Step 400: key knowledge injection: Based on the domain knowledge results obtained by distilling the key knowledge, design the prompt template for generating SQL in the power field and the key knowledge formatting method, so that the large language model can fully utilize and understand the meaning of the knowledge; inject the formatted key knowledge into the prompt template to obtain a complete prompt with the power data domain knowledge required for the query problem, which is used to guide the large language model to generate SQL statements that meet the query requirements and conform to the actual power field business database logic.

[0293] In order to enable the large language model to fully understand and use the power domain knowledge to generate SQL statements that meet the query requirements, a key knowledge injection method is designed. By formatting the distilled key domain knowledge, the knowledge structure and meaning are clarified; a power domain SQL generation prompt template is designed for knowledge injection, and the knowledge is used to effectively guide the large language model to generate SQL. The specific steps are as follows:

[0294] Step 410: Design a formatted key knowledge representation form, including formatted similar cases, table schema, all coding mapping relationships and time information.

[0295] The partial knowledge acquired in step 300 is further formatted and normalized and then input into the large language model so that the large language model can more easily understand the information therein.

[0296] Step 411: Design the formatted representation of the schema knowledge of the table, clearly distinguish the structure and content of each table, and clearly define the boundaries of different tables.

[0297] The standardized table schema knowledge representation is designed as follows:

[0298] "- TABLE_i : xxx \n … -TABLE_k : xxx \n … ”;

[0299] Among them, TABLE_i represents the i The name of the table, xxx " is the schema content of the table. All table schema knowledge expressions are formatted in this form.

[0300] Step 412: Design a formatted form of the coding mapping relationship knowledge to match each entity with its related coding relationship one by one to avoid confusion between the entity and the coding content.

[0301] The standardized encoding mapping relationship knowledge representation is designed as follows:

[0302] “#ENTITY_i :xxx \n Encoding : xxx \n\n …

[0303] #ENTITY_k :xxx \n Encoding : xxx \n\n … ”

[0304] Among them, ENTITY represents the name part of the entity, Encoding represents the relevant encoding part of the entity, xxx " is the content corresponding to each part. For example, the content of the relevant coding part is a filtered retrieval result set of a certain entity coding mapping relationship. In this form, the knowledge representation of the coding mapping relationship between all entities and their corresponding entities is formatted.

[0305] Step 413: Design a formatted form of case knowledge for similar questions and answers, clearly distinguish the boundaries of each case, and add similar cause content to better assist the large language model in understanding the case content.

[0306] The standardized similar question-answer case knowledge representation is designed as follows:

[0307] “ Case i : Similar content: xxx \n Similar reason: xxx - Case i End … ”

[0308] Among them, “Similar content: xxx "Represents the content part of similar cases," xxx " is the content in the form of similar question-answer pairs organized according to step 111; "Similar reason: xxx "Represents similar reasons part," xxx " is the thinking process of the large language model output in step 240.

[0309] Step 414: Design the format of the current time information, which is designed to be a universal time representation format of "XXXX-XX-XX" (year-month-day).

[0310] Step 420: Designing SQL to generate prompt template;

[0311] Define the SQL generation prompt template as P G , contains the following parts:

[0312] ;

[0313] Among them, the meanings of G, W, and O are the same as the case knowledge decision prompt in step 230; the meaning of CK is the same as the difference entity extraction prompt in step 320, which provides domain business knowledge reference in the form of similar cases; TK is the target database type. When the database type in the similar case is inconsistent with the target database type, it prompts to pay attention to the conversion of the database type; SK represents the schema knowledge of the relevant tables, which is used to provide the necessary table structure information for generating SQL; RK represents the relationship knowledge between the relevant tables, which provides the primary and foreign key information when multiple tables are associated; EK represents the relevant encoding mapping relationship knowledge, which is used to provide entity-encoding correspondence information, so that the large language model can convert the entities expressed in natural language into corresponding encodings when generating SQL; DT represents the current time information to cope with real-time query needs, such as query problems containing "this month" and "this year".

[0314] SQL generation prompt template, see Fig.16 , "{}" in the template is the parameter to be input.

[0315] Step 430: Inject the formatted similar cases, table schema, code mapping relationship, time information and inter-table relationship knowledge into the SQL generation prompt template to obtain a complete SQL generation prompt to meet the professional knowledge requirements in the power field.

[0316] The similar cases, table schema, all encoding mapping relationships and time information formatted in step 410 are respectively injected into the corresponding parameters of the SQL generation prompt template, that is, "{ schma}","{ encodings}"and"{ date}", if there is no similar case, fill it with blank; for the relationship knowledge between tables, directly fill it into the prompt template in the original form of the search results, if there is no similar case, fill it with blank. The complete SQL generation prompt obtained after knowledge injection has all the power field expertise required to generate query problem SQL.

[0317] Step 500: Construction of SQL agent in the power field: Take the work chain consisting of knowledge matching and decision-making, key knowledge distillation, and key knowledge injection as the thinking process of the agent, combine the ideas of agent-environment interaction, memory and feedback, design SQL execution actions and SQL verification mechanisms, and build SQL agent in the power field. Through the agent, the functions of SQL statement generation, SQL statement execution and result verification feedback are realized.

[0318] Use the KMDI work chain formed from step 200 to step 400 as the thinking process, design agent tools and agent verification methods, and jointly build the power field SQL agent, so that the agent can realize the functions of domain SQL generation, SQL query execution and verification based on the input query questions, so that the final generated SQL results are more reliable. The specific steps are as follows:

[0319] Step 510: Build SQL execution action / tool;

[0320] Building SQL execution actions for SQL agents in the power sector T , which is responsible for parsing SQL statements from the results generated by the large language model and executing them by connecting to the corresponding database. The main functions of this action / tool ​​are as follows:

[0321] (1) Database connection: Action T Through the preset database connection module, connect to the corresponding database, and the configuration information of the corresponding database is provided by the user. Since the business database types of different tasks may be different, connection modules for multiple databases are preset, such as MYSQL, Oracle, etc., and the type parameter of the connected database is automatically passed to the agent to fill in the SQL generation prompt template of step 420 ({ db_type}).

[0322] (2) SQL parsing: corresponding to the prompt output structure and action T Extract and parse valid SQL statements from text generated by a large language model.

[0323] (3) SQL execution: The parsed SQL statement is sent to the database through the database connection for execution.

[0324] (4) Result return: After the database execution is completed, the SQL statement and execution results (such as query results or other information) are returned to the SQL agent for subsequent use.

[0325] Step 520: construct a verification method;

[0326] In view of the fact that the hallucination problem of large language models may lead to incorrect results generated by the agent, in order to ensure that SQL can be successfully executed and return correct data, this paper designs three verification methods based on the functions and thinking process of SQL execution actions / tools to confirm the results, thereby improving the success rate of the task. It includes the following parts:

[0327] ;

[0328] in, V GIndicates the verification method; FV indicates result format verification. After the large language model generates content, verify whether the result meets the format requirements in the prompt. If not, there is no need to perform SQL execution, but the reason for the verification failure is fed back to the power field SQL agent;

[0329] TV stands for code mapping relationship verification, which aims to verify the consistency between the code conversion results in the generated SQL and the code mapping relationship knowledge, to prevent the large language model from hallucinating when converting the code value and fabricating the code out of thin air or "putting the wrong hat on the wrong head", that is, using non-corresponding code columns and code value relationships. Use regular expressions to extract the where condition items in the SQL statement, and at the same time extract the condition columns and condition values ​​in the condition items, and compare them with the input code mapping relationship. If there is a situation where the condition column and the corresponding condition value are inconsistent with a certain code mapping relationship, it is judged as verification failure, and the specific condition item that failed is returned.

[0330] IV stands for execution result verification, which aims to verify the execution result of SQL and confirm that the generated SQL can be executed normally and return data. If the execution fails, the returned failure result will be fed back to the SQL agent in the power field.

[0331] Step 530: constructing SQL agent in the power field;

[0332] Combining the knowledge matching and decision-making process of step 200, the key knowledge distillation process of step 300, the key knowledge injection process of step 400, the SQL execution action / tool ​​T defined in step 510, and the verification method constructed in step 520 V G , and a large language model to jointly build SQL agents in the power field:

[0333] ;

[0334] in, G A represents the SQL agent in the power field; Q represents the input question, i.e., the task goal; MP, DP, IP They respectively represent the stage processes to which step 200, step 300, and step 400 belong, and M represents a large language model.

[0335] The steps to generate SQL based on the power field SQL agent are as follows:

[0336] First, after receiving the query question input by the user, the agent searches and determines whether there are similar cases in step 200;

[0337] Secondly, based on the decision results of similar cases, different routes are selected to obtain relevant knowledge and filter irrelevant information through step 300;

[0338] Again, based on the key knowledge obtained in step 300, the key knowledge is formatted and expressed in step 400 and injected into the SQL generation prompt;

[0339] Finally, use the complete SQL generation prompt to guide the large language model to complete the SQL generation task, use the SQL execution action / tool ​​to parse the model output results, obtain the SQL statement and execute the SQL in the configured database; at the same time, use the verification method built by 430 to verify in this process. If the verification fails, regenerate the SQL according to the prompt and the returned verification reason; if the verification passes, output the SQL statement and the execution result of the SQL.

[0340] Method effect display:

[0341] 1. Experiment 1:

[0342] Experiment 1 mainly demonstrates the SQL generation process and results of SQL agents in the power field with similar cases.

[0343] Question: In 2024, there is a basic electricity charge calculated according to the maximum demand, and the monthly average operating capacity of the voltage levels is 35KV, 110KV, and 220KV.

[0344] Similar case question: Query the average monthly operating capacity of transformers with basic electricity charges calculated according to transformer capacity for voltage levels of 10KV, 20KV, 35KV, 110KV, and 220KV in 2023.

[0345] Four examples of knowledge base construction content are shown in Figure 2 .

[0346] After the knowledge matching and decision results of step 200, see Figure 3 ,After the judgment of the large language model, the first retrieved text block is determined as a similar case.

[0347] The extraction result of the difference entity extraction in step 320 in route 1 is shown in Figure 4 The results of step 330 and step 340 are shown in Figure 5 ,Based on the extracted difference entities, the encoding mapping relationship knowledge base is used for retrieval, which can retrieve the encoding information of related entities and exclude irrelevant encoding information through filtering.

[0348] Figure 6 The left side shows the content filled into the SQL generation prompt after the key knowledge is distilled based on step 300. It can be seen that through knowledge acquisition and prompt design, the large language model is clearly provided with business knowledge in the power field such as similar cases, table schema, and encoding mapping relationships. Figure 6The right side shows the SQL generation result of the large language model. This SQL uses the SQL in similar cases as a template, modifies the difference content based on the difference between the query question and similar cases, and successfully converts the corresponding voltage level and basic electricity fee content into business coding language ("jldydjdm in ('12', '13', '10')", "jbdfjsfsdm = '2'").

[0349] Figure 7 Demonstrated execution through agent tools Figure 6 The query results after the generated SQL confirm that the method proposed in the present invention can accurately retrieve the target data. Figure 8 The SQL generation results of the general method and the method of the present invention are compared. Figure 8 The left side shows the SQL statement generated by the general method. The general method only uses the prompt words containing the database table structure as the input of the large language model; Figure 8 The right side is the SQL generated by the method of the present invention. By comparison, it can be clearly seen that since the general method fails to incorporate key domain knowledge, the large language model cannot correctly understand the problem, the generated SQL statement is too simplified, and there are problems such as incorrect table usage, inconsistency with actual business logic, and inability to correctly convert entities such as "35KV", "110KV", "220KV" and "maximum demand" into business coding languages ​​recognizable by the database, resulting in the inability to query the correct results; while the method based on the present invention can correctly understand the user's query intention, generate correct SQL and return data that meets the requirements.

[0350] (II) Experiment 2:

[0351] Experiment 2 mainly demonstrates the SQL generation process and results of SQL agents in the power field without similar cases.

[0352] Question: Query the electricity charges and consumption information of users with A-level credit in xx Bureau before and after verification this year.

[0353] The knowledge matching and decision results of step 200 are shown in Fig. 9 As can be seen from the figure, the large language model determines that the results retrieved are not similar cases, and takes route 2 to proceed to the subsequent steps.

[0354] The problem entity extraction and LSH retrieval positioning results of step 350 and step 360 in route 2 are shown in Fig.10 ,It can be seen that LSH retrieves the tables, fields and values ​​related to the entity based on the extracted question entity. At the same time, after statistically analyzing the search results, it can locate several tables with the most related fields and values. For example, the tables “kh_ydkh” and “fw_ykgzdjbxx” have the most statistical results and are most relevant to the question.

[0355] Fig.11 The comparison of the effects of the general method and the method of the present invention in case-free SQL generation is demonstrated. Fig.11 The left side is the SQL generated by the general method. Fig.11 The right side is the SQL generated by the method of the present invention. By comparison, it can be clearly seen that the SQL generated by the method of the present invention effectively identifies and locates the database table required for the problem, and accurately converts the xx bureau and "A-level credit user" entities in the problem into business coding languages ​​that can be understood by the database, namely "DQBM = '028500'" and "k.XYDJDM = '101'". At the same time, it is able to perform association operations on the two tables, and successfully generate conditions about "this year" using time information ("h.CJSJ>= '2024-01-01' and h.CJSJ<= '2024-10-10'"). Finally, it also correctly outputs the fields required for the problem. The execution results are shown in Fig.12 .

[0356] on the contrary Fig.11 There are obvious problems in the SQL statements generated by the general method: first, the table positioning is wrong, and the database table required for the problem is not correctly identified; second, the correct coding item conversion is not performed for the xx bureau and the "A-level credit user" entity; in addition, due to improper table selection, it is impossible to accurately find the output fields that meet the problem requirements, and the output is random. For example, in order to meet the problem requirements, "JFZDL", "SQJFZDL", "ZDF" and "SQDFNY" in the SQL statement are incorrectly named "power before verification", "power after verification", "electricity fee before verification" and "electricity fee after verification". In fact, the correct meanings of these fields should be "total billed power", "total billed power in the previous period", "total electricity fee" and "year and month of the previous electricity fee". These errors cause the SQL statements generated by the general method to completely fail to meet the query requirements.

[0357] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such 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 invention, and should all be included in the protection scope of the present invention.

Claims

1. A method for constructing a domain SQL agent based on a KMDI chain, characterized in that: The following steps are involved: Step 100: knowledge organization in the electric power data field and knowledge base construction, the knowledge base includes a SQL question-answer pair knowledge base, a database table structure knowledge base, an inter-table relationship knowledge base, and a coding mapping relationship knowledge base; Step 200: Knowledge matching and decision-making: Using retrieval enhancement generation technology, based on user data query questions, retrieve similar question cases from the SQL question-answer pair knowledge base, design a prompt template for similar question-answer pair level judgment, use the understanding of the large language model to judge the similarity level of similar question-answer pairs, and make decisions on whether there are similar cases based on the similarity level to enter different key knowledge distillation routes; Step 300: Key knowledge distillation: For data query problems with similar question-answer pairs, obtain the information of each related table from the database table structure knowledge base; design a difference entity extraction prompt template, use the large language model to extract the difference entities, and obtain relevant knowledge, specifically: search the difference entity names in the encoding mapping relationship knowledge base, sort the search results, and obtain the entity encoding information; For data query problems without similar cases, a question entity extraction prompt template is designed to extract the key entities in the data query problem, and the database table information most relevant to the query problem is obtained based on a hybrid retrieval and positioning method of local sensitive hashing and vector similarity; Among them, the vector similarity retrieval method is used to embed the question vector in the database table structure knowledge base and the inter-table relationship knowledge base to retrieve relevant knowledge, and the encoding mapping relationship knowledge retrieval is performed on the entity names that meet the conditions to obtain the relevant table schema, inter-table relationship and encoding mapping relationship set; Step 400: key knowledge injection: based on the domain knowledge results obtained by key knowledge distillation, design a prompt template for generating SQL in the power field and a key knowledge formatting method, so that the large language model can fully utilize and understand the meaning of knowledge; inject the formatted key knowledge into the prompt template for generating SQL in the power field, and obtain a complete prompt with the power data domain knowledge required for querying questions, which is used to guide the large language model to generate SQL statements that meet the query requirements and conform to the actual logic of the power field business database; Step 500: Construction of SQL agent in the power field: Take the work chain consisting of knowledge matching and decision-making, key knowledge distillation, and key knowledge injection as the thinking process of the agent, combine the ideas of agent-environment interaction, memory and feedback, design SQL execution actions and SQL verification mechanisms, and build SQL agent in the power field. Through the agent, the functions of SQL statement generation, SQL statement execution and result verification feedback are realized.

2. According to the method for constructing a domain SQL agent based on a KMDI chain according to claim 1, it is characterized in that: The step 100 includes: collecting and organizing data query SQL question-answer pairs, database table structures, database table associations and data coding mapping relationships in the power field business to form serial number-question knowledge documents, serial number-question-answer pair knowledge documents, database table structure knowledge documents, inter-table relationship knowledge documents and data coding mapping relationship knowledge documents, and segmenting the serial number-question knowledge documents, database table structure knowledge documents, inter-table relationship knowledge documents and data coding mapping relationship knowledge documents, and performing vector embedding processing on the segmented text block data to convert it into a machine-readable form.

3. According to the method for constructing a domain SQL agent based on a KMDI chain according to claim 2, it is characterized in that: The step 200 specifically includes: Step 210: embed the query question into a vector to obtain a query question vector; Step 220: Calculate the similarity between the query question vector and each text block vector in the SQL question-answer pair knowledge base to obtain a similarity score; and return the content in the most similar text block as a retrieval matching result according to the similarity score; Step 230: Design a case knowledge decision prompt template to enable the large language model to generate the required output through context prompts: for each SQL question-answer pair, determine its similarity level and output relevant information; Define the case knowledge decision prompt template as a tuple , including the following parts: ; Among them, G represents the defined task role goal, that is, to give the large language model a clear role and explain the task goal of the role; ST represents the standard definition of similarity judgment, and a total of four similarity levels are designed, among which level 1 has the smallest similarity and level 4 has the largest similarity, representing a complete match; CS represents the set of candidate similar SQL question-answer pairs {cases}; T represents the target database type {db_type} for generating SQL; W represents important reminders, including important tips and constraints when completing tasks; O represents the output format of the task; Step 240: Make a decision on whether there are similar cases based on the similarity level results, extract the required information, and enter different key knowledge distillation routes.

4. According to the method for constructing a domain SQL agent based on a KMDI chain according to claim 3, it is characterized in that: In step 240, if there are cases with a similarity level greater than or equal to 3, the process switches to a case-based SQL knowledge distillation route; if the maximum similarity level is less than 3, the process switches to a non-case-based knowledge distillation route.

5. According to the method for constructing a domain SQL agent based on a KMDI chain according to claim 1, it is characterized in that: In step 300, for data query problems with similar question-answer pairs, a difference entity extraction prompt template is designed, and the difference entities extracted by the large language model and similar case information are used to retrieve and obtain relevant knowledge, specifically including: Step 310: Obtain schema information of each relevant table from the database table structure knowledge base to obtain a table schema knowledge set; Step 320: Design a different entity extraction prompt template, combine it with the large language model, determine the parts of the similar cases that are different from the input query question, and extract each different entity name and corresponding information; Among them, the difference entity extraction prompt template is defined as a tuple , including the following parts: ; Among them, G represents the defined task role goal, that is, to give the large language model a clear role and explain the task goal of the role; I p It represents an important reference step for extracting differential entities; O represents the output format of the task; Q represents the input query question {question}; CK represents the set of similar SQL question-answer pairs {cases}; Step 330: Search the entity names with differences in the encoding mapping relationship knowledge base, sort the search results, and obtain the encoding information of the entity.

6. A method for constructing a domain SQL agent based on a KMDI chain according to claim 5, characterized in that: The step 330 specifically includes: For the difference entity, different coding search conditions are constructed according to whether there is a corresponding column name. If there is a corresponding column name, the column-entity coding knowledge form is used as the search condition; if the corresponding column name is empty, the difference entity name is directly used as the search condition; The encoded search condition is embedded in text and searched to obtain the most relevant predecessor of the difference entity. k If there is a corresponding column name search, take k =3 as the search result; if the corresponding column name does not exist, take k =5 as the search result to get more candidates; The encoding retrieval results are sorted using a pre-trained re-ranking model to retrieve the relevant encoding mappings for all differential entities.

7. According to the method for constructing a domain SQL agent based on a KMDI chain according to claim 1, it is characterized in that: In step 300, for data query problems without similar cases, a question entity extraction prompt template is designed to extract key entities in the data query problem, and the database table information most relevant to the query problem is obtained based on a hybrid retrieval and positioning method of local sensitive hashing and vector similarity, specifically including: Step 350: Design a question entity extraction prompt template to extract key entities in the data query question and define the question entity extraction prompt template as a tuple , including the following parts: ; Among them, G represents the defined task role goal, that is, to give the large language model a clear role and explain the task goal of the role; W represents important reminders, including important tips and constraints when completing the task; O represents the output format of the task; Q represents the input query question {question}; Through the question entity extraction prompt template, the large language model extracts each key entity in the question and determines whether it is a query condition; Step 360: Use the keyword retrieval and table location method of local sensitive hashing to screen and exclude redundant information and quickly lock the relevant data table, and obtain the schema of the relevant table; Step 370: Use the question embedding vector to retrieve relevant knowledge in the database table structure knowledge base and the inter-table relationship knowledge base, and perform encoding mapping relationship knowledge retrieval on the entity names that meet the conditions to obtain the relevant table schema, inter-table relationship and encoding mapping relationship set.

8. A method for constructing a domain SQL agent based on a KMDI chain according to claim 5 or 7, characterized in that: Also includes: Filter the code mapping relationship search results: For each code mapping relationship retrieval result, extract the corresponding code column name according to the organization form, expressed as: ; Among them, cut( r ik ) method means to split the content according to the separator; [0] means to take the first split result; r ik Indicates the first k Search results, Rc ik express r ik The corresponding encoding column name; The relationships corresponding to the column names that do not belong to the extracted table schema are excluded, and the knowledge of the encoding mapping relationships that meet the conditions is retained to ensure that the encoding mapping relationships that are finally retained match the fields in the table structure.

9. The method for constructing a domain SQL agent based on a KMDI chain according to claim 1, characterized in that: The step 400 includes: Step 410: Design and format the key knowledge representation form, including formatted similar cases, table schema, all coding mapping relationships and time information; Step 420: Designing SQL to generate prompt template; Define the SQL generation prompt template as P G , including the following parts: ; Among them, G represents the defined task role goal, that is, to set a clear role for the large language model and explain the task goal of the role; W represents important reminders, including important tips and constraints when completing the task; O represents the output format of the task; CK represents the set of similar SQL question-answer pairs {cases}, which provides domain business knowledge reference in the form of similar cases; TK is the target database type. When the database type in the similar case is inconsistent with the target database type, it prompts to pay attention to the conversion of the database type; SK represents the schema knowledge of the relevant table, which is used to provide the necessary table structure information for generating SQL; RK represents the relationship knowledge between the relevant tables, which provides the primary and foreign key information when multiple tables are associated; EK represents the knowledge of the relevant encoding mapping relationship, which is used to provide entity-encoding correspondence information, so that the large language model can convert the entities expressed in natural language into corresponding encodings when generating SQL; DT represents the current time information to cope with the problem of real-time query needs; Step 430: Inject the formatted similar cases, table schema, code mapping relationship, time information and inter-table relationship knowledge into the SQL generation prompt template to obtain a complete SQL generation prompt to meet the professional knowledge requirements in the power field.

10. A method for constructing a domain SQL agent based on a KMDI chain according to claim 1, characterized in that: The step 500 includes: Step 510: constructing an SQL execution action tool for parsing SQL statements from the results generated by the large language model and executing the SQL statements by connecting to a corresponding database; Step 520: Construct verification method V G , contains the following parts: ; Among them, FV represents result format verification. After the large language model generates content, it verifies whether the result meets the format requirements in the prompt. If not, there is no need to perform SQL execution, but the reason for the verification failure is fed back to the SQL agent in the power field. TV represents code mapping relationship verification. Regular expressions are used to extract the where condition items in the SQL statement, and the condition columns and condition values ​​in the condition items are extracted at the same time, and compared with the input code mapping relationship. If the condition column and the corresponding condition value are inconsistent with a certain code mapping relationship, it is judged that the verification fails, and the specific condition item that failed is returned. IV represents execution result verification. The purpose is to verify the execution result of SQL and confirm that the generated SQL can be executed normally and return data. If the execution fails, the returned failure result is fed back to the SQL agent in the power field. Step 530: constructing SQL agent in the power field; Combining the knowledge matching and decision-making process of step 200, the key knowledge distillation process of step 300, the key knowledge injection process of step 400, the SQL execution action / tool ​​T defined in step 510, and the verification method constructed in step 520 V G , and a large language model to jointly build SQL agents in the power field: ; in, G A represents the SQL agent in the power field; Q represents the input question, i.e., the task goal; MP, DP, IP They respectively represent the stage processes to which step 200, step 300, and step 400 belong, and M represents a large language model.

Citation Information

Patent Citations

  • Assisted knowledge discovery and publication system and method

    CA2595541A1

  • Method and device for predicting and enhancing query instruction of electric power customer service system

    CN118152428A