Data query statement generation method and device, storage medium and electronic equipment
By creating an initial knowledge graph, dividing communities, generating tags and updating knowledge graphs in a large model system, the problem of difficulty in accurately generating local database query statements in the prior art is solved, and efficient context information retention and query statement generation are achieved.
Patent Information
- Application Number
- CN202411366493.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-27
- Publication Date
- 2025-02-25
AI Technical Summary
The prior art is difficult to accurately generate local database query statements based on large model systems, especially when the database table information is huge and batch submission will lead to the loss of context information.
By creating the initial knowledge graph of the current database, using the clustering algorithm to divide multiple communities, generating community summary tags and entity overview tags, updating the knowledge graph, matching question-asked questions and community summary tags, and using a large language model to generate data query statements for the current database.
It realizes that the key information of the database is submitted to the large language model at one time without causing the loss of context information, and accurately generates local database query statements corresponding to the question asked.
Smart Images

Figure CN119513326A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a method and device for generating a data query statement, a storage medium, and an electronic device. Background Art
[0002] With the development of large language model technology, the accumulated local data is integrated with the analysis capabilities of the large language model, and the local data is used as external storage data to provide contextual analysis data for the large language model, so that the large language model can generate data output that meets local applications. However, due to the limitation of tokens, the basic unit of text processing and generation by the large language model, it is necessary to convert user questions into precise structured query language (SQL) query statements in order to retrieve the required information from the local database.
[0003] Since the amount of information in the database table is relatively large, it is impossible to submit all the information to the large language model at once, and submitting in batches will result in the loss of context information. Therefore, how to accurately generate accurate local database SQL query statements has become a technical problem that needs to be solved urgently. Summary of the invention
[0004] The embodiments of the present application provide a method and device for generating a data query statement, a storage medium, and an electronic device, so as to at least solve the technical problem in the related art that it is difficult to accurately generate a local database query statement based on a large model system.
[0005] According to one aspect of an embodiment of the present application, a method for generating a data query statement is provided, including: creating an initial knowledge graph corresponding to a current database, and dividing the knowledge graph into multiple communities using a preset clustering algorithm; generating community summary tags and entity overview tags corresponding to entities in the initial knowledge graph based on a preset large language model; the community summary tags are used to indicate the content that matches the community and the question; updating the initial knowledge graph based on the community summary tags and / or entity overview tags to obtain an updated knowledge graph; matching a current question with the community summary tags in the updated knowledge graph to obtain a target community summary tag related to the current question; based on the target community summary tag and triple information consisting of entities, attributes and relationships corresponding to the target community summary tag, obtaining a data query statement for the current database that matches the current question through the large language model.
[0006] According to another aspect of an embodiment of the present application, a device for generating a data query statement is also provided, including: a creation and division unit, used to create an initial knowledge graph corresponding to a current database, and divide the knowledge graph into multiple communities using a preset clustering algorithm; a generation unit, used to generate community summary tags and entity overview tags corresponding to entities in the initial knowledge graph; the community summary tags are used to indicate the content that matches the community with the question; an update unit, used to update the initial knowledge graph based on the community summary tags and / or entity overview tags to obtain an updated knowledge graph; a matching unit, used to match the current question with the community summary tags in the updated knowledge graph, and obtain a target community summary tag related to the current question; an acquisition unit, used to submit the triple information consisting of the target community summary tag and the entity, attribute and relationship corresponding to the target community summary tag to a preset large language model, and obtain a data query statement for the current database that matches the current question through the large language model.
[0007] According to another aspect of an embodiment of the present application, there is also provided an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the method for generating a data query statement through the computer program.
[0008] According to another aspect of the embodiments of the present application, a computer-readable storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the above-mentioned method for generating a data query statement when running.
[0009] In the embodiment of the present application, an initial knowledge graph corresponding to the current database is created, and the knowledge graph is divided into multiple communities using a preset clustering algorithm; based on a preset large language model, a community summary tag and an entity overview tag corresponding to an entity in the initial knowledge graph are generated; the community summary tag is used to indicate the content matching the community and the question; based on the community summary tag and / or the entity overview tag, the initial knowledge graph is updated to obtain an updated knowledge graph; the current question is matched with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question; based on the target community summary tag and the triple information consisting of entities, attributes and relationships corresponding to the target community summary tag, a method for obtaining a data query statement for the current database that matches the current question is obtained through the large language model; in the above method, the data tables and data files in the database are decomposed into smaller and easier to manage community modules, so that the key information of the database can be submitted to the large language model at one time without causing the loss of context information. And it can also accurately generate a query statement for the local database corresponding to the question. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0011] Figure 1 It is a schematic diagram of an application environment of an optional method for generating a data query statement according to an embodiment of the present application;
[0012] Figure 2 is a schematic diagram of an application environment of another optional method for generating a data query statement according to an embodiment of the present application;
[0013] Figure 3 It is a flowchart of an optional method for generating a data query statement according to an embodiment of the present application;
[0014] Figure 4 It is a flowchart of another method for generating a data query statement according to an embodiment of the present application;
[0015] Figure 5 It is a flowchart of another method for generating a data query statement according to an embodiment of the present application;
[0016] Figure 6 It is a flowchart of another method for generating a data query statement according to an embodiment of the present application;
[0017] Figure 7 It is a flowchart of another method for generating a data query statement according to an embodiment of the present application;
[0018] Figure 8 It is a structural schematic diagram of an optional data query statement generation device according to an embodiment of the present application;
[0019] Fig. 9 It is a schematic diagram of the structure of an optional electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0020] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only embodiments of a part of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0021] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0022] According to one aspect of an embodiment of the present application, a method for generating a data query statement is provided. Optionally, as an optional implementation, the method for generating a data query statement can be but is not limited to being applied to: Figure 1 In the application environment shown. The application environment includes: a terminal device 102 for human-computer interaction with a user, a network 104, and a server 106. A user 108 can perform human-computer interaction with the terminal device 102, and a client for generating data query statements is running in the terminal device 102. The above-mentioned terminal device 102 includes a human-computer interaction screen 1022, a processor 1024, and a memory 1026. The human-computer interaction screen 1022 is used to display the current question, and the data query statement for the current database that matches the current question; the processor 1024 is used to obtain the data query statement for the current database that matches the current question. The memory 1026 is used to store the current question, and the data query statement for the current database that matches the current question.
[0023] In addition, the server 106 includes a database 1062 and a processing engine 1064, wherein the database 1062 is used to store the current question and the data query statement for the current database that matches the current question. The processing engine 1064 is used to create an initial knowledge graph corresponding to the current database, and divide the knowledge graph into multiple communities using a preset clustering algorithm; based on a preset large language model, generate a community summary tag and an entity overview tag corresponding to an entity in the initial knowledge graph; the community summary tag is used to indicate the content that matches the community and the question; based on the community summary tag and / or the entity overview tag, update the initial knowledge graph to obtain an updated knowledge graph; match the current question with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question; based on the target community summary tag and the triple information consisting of entities, attributes and relationships corresponding to the target community summary tag, obtain a data query statement for the current database that matches the current question through the large language model; and send the above data query statement to the client of the above terminal device 102.
[0024] In one or more embodiments, the method for generating the data query statement described above in the present application can be applied to Figure 2 In the application environment shown. Figure 2 As shown, human-computer interaction can be performed between user 202 and user device 204. User device 204 includes memory 206 and processor 208. In this embodiment, user device 204 can refer to but not be limited to the operation performed by the above terminal device 102 to display the data query statement for the current database that matches the current question.
[0025] Optionally, the terminal device 102 and the user device 204 include but are not limited to mobile phones, tablet computers, laptop computers, PCs, vehicle-mounted electronic devices, wearable devices and other terminals, and the network 104 may include but is not limited to wireless networks or wired networks. Among them, the wireless network includes: WIFI and other networks that realize wireless communication. The wired network may include but is not limited to: wide area network, metropolitan area network, local area network. The server 106 may include but is not limited to any hardware device that can perform computing. The server may be a single server, or a server cluster composed of multiple servers, or a cloud server. The above is only an example, and no limitation is made to this in this embodiment.
[0026] As an optional implementation, Figure 3 As shown, the embodiment of the present application provides a method for generating a data query statement, comprising the following steps:
[0027] S302, creating an initial knowledge graph corresponding to the current database, and dividing the knowledge graph into multiple communities using a preset clustering algorithm.
[0028] S304, based on a preset large language model, generate a community summary tag and an entity overview tag corresponding to the entity in the initial knowledge graph; the community summary tag is used to indicate the content that matches the community with the question.
[0029] S306, updating the initial knowledge graph based on the community summary tag and / or entity overview tag to obtain an updated knowledge graph;
[0030] S308, matching the current question with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question;
[0031] S310, based on the target community summary tag and the triple information consisting of the entity, attribute and relationship corresponding to the target community summary tag, obtain a data query statement for the current database matching the current question through the large language model.
[0032] Specifically, in the embodiment of the present application, the current database includes various data tables, various fields, and the relationship between each table and field; based on the data tables, various fields, and the relationship between each table and field, an initial knowledge graph corresponding to the current database is created, and the clustering algorithm here can be, for example, a Leiden graph clustering algorithm, or other community discovery algorithms. A community is usually defined as a group of nodes, and a community can be a subgraph, including nodes and edges; the nodes in the same community are closely related, while the connections between communities are relatively sparse.
[0033] In one example, the initial knowledge graph TGraph is divided into communities, for example, using the Leiden graph clustering algorithm to divide the initial knowledge graph TGraph into communities to generate a community data set Sum = {s 1 ,s 2 ,s 3 ,…s i …,s n}; Among them, community data s i ={E i ,R i ,F i ,LB i}, E i is the entity set, R i is the attribute set, F i is a relation set, LB i A collection of tags.
[0034] In the embodiment of the present application, an initial knowledge graph corresponding to the current database is created, and the knowledge graph is divided into multiple communities using a preset clustering algorithm; based on a preset large language model, a community summary tag and an entity overview tag corresponding to an entity in the initial knowledge graph are generated; the community summary tag is used to indicate the content matching the community and the question; based on the community summary tag and / or the entity overview tag, the initial knowledge graph is updated to obtain an updated knowledge graph; the current question is matched with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question; based on the target community summary tag and the triple information consisting of entities, attributes and relationships corresponding to the target community summary tag, a method for obtaining a data query statement for the current database that matches the current question is obtained through the large language model; in the above method, the data tables and data files in the database are decomposed into smaller and easier to manage community modules, so that the key information of the database can be submitted to the large language model at one time without causing the loss of context information. And it can also accurately generate a query statement for the local database corresponding to the question.
[0035] In one or more embodiments, the creating an initial knowledge graph corresponding to the current database includes:
[0036] Obtaining a table set, a field set, and a relationship set corresponding to the current database;
[0037] Vectorizing the table set, field set and relationship set to obtain corresponding initial table vectors, initial field vectors and initial relationship vectors;
[0038] Merging vectors whose similarity is greater than or equal to a preset threshold among the initial table vector, the initial field vector, and the initial relationship vector to obtain corresponding table embedding vectors, field embedding vectors, and relationship embedding vectors;
[0039] Based on the table embedding vector, the field embedding vector and the relationship embedding vector, an initial knowledge graph corresponding to the current database is generated.
[0040] Specifically, the table set, field set and relationship set corresponding to the above-mentioned current database can be local database table schema information or data table schema file, and the schema structure data is generated into metadata shm_metadata corresponding to the above-mentioned initial knowledge graph through a preset scripting language. In one example, shm_metadata includes: table set T = {db_name, t_name, t_comment}, field set Col = {db_name, t_name, col_name, col_type, col_comment}, relationship set Rel = {r_t_name, r_col_name, l_t_name, 1_col_name, rel_name, rel_comment}. The scripting language here can be Python.
[0041] Secondly, the present application can map the text sequence corresponding to the metadata into a high-dimensional vector through the text data vectorization algorithm GPT (Generative Pre-trained Transformer) model, and vectorize the above-mentioned table set T, field set Col, and relationship set Rel to generate the corresponding initial table vector: table set T′, initial field vector: field set Col′, and initial relationship vector: relationship set Rel′, and obtain the vector data set VE_DATA_SET = [T′, Col′, Rel'].
[0042] Then, a tree-based nearest neighbor search algorithm is used to merge semantically similar vectors in the vector data set VE_DATA_SET into one vector through vector matching query to generate an alignment embedding vector (Alignment EmbeddingsVector) data set: table set T″, field set Col″, relationship set Rel″; that is, the alignment vector data set AGM_VE_DATA_SET = [T″, Col″, Rel″].
[0043] The above alignment vector data AGM_VE_DATA_SET is passed through the mapping function f(veparm, [e, r, f, lb]), where e is the entity mapping parameter, r is the attribute mapping parameter, f is the relationship mapping parameter, and lb is the entity label mapping parameter; the knowledge graph triple set corresponding to the current database is generated: the entity data set E = {e 1 , e 2 , e 3 ,...e i ..., e n}where e i =f(T″, e), attribute set R = {r 1 , r2 , r 3 ,...r i ..., r n}where r i =f(Col″, r), relation set F = {f 1 , f 2 , f 3 , ...f i ..., f n}where f i =f(T″,f)} and label set LB={lb 1 , lb 2 , lb 3 , ...lb i ..., lb n} where lb i =f(T″, lb), based on the above ternary set, the initial knowledge graph TGraph={E, R, F} corresponding to the current database is generated.
[0044] In one or more embodiments, generating entity summary labels corresponding to entities in the initial knowledge graph includes:
[0045] Obtain each entity in the initial knowledge graph, and take each of the entities as a center point to obtain a target subgraph whose entity depth is less than a preset depth; the target subgraph is a subgraph included in the initial knowledge graph;
[0046] The target subgraph is input into the large language model to obtain an entity summary label corresponding to the entity.
[0047] Specifically, the preset depth can be 1. In one example, based on the initial knowledge graph TGraph generated in the above embodiment, each entity in the entity data set E is taken as the center point to obtain a target subgraph e_tgraph with an entity depth of 1 = {e i (E), e i (R), e i (F), e i (LB)}, create entity summary prompt word prompt = {context: "e_tgraph", "query": "overall knowledge graph content"}, submit the prompt word to the big language model, submit e_tgraph and the prompt word to the big language model to generate the e_tgraph summary data label entity_sum_lb (entity summary label).
[0048] In one or more embodiments, updating the initial knowledge graph based on the community summary tag and / or entity overview tag to obtain an updated knowledge graph includes:
[0049] The acquired entity overview label corresponding to the entity is added to the entity label set corresponding to the initial knowledge graph to obtain the updated knowledge graph.
[0050] Specifically, in the embodiment of the present application, after the e_tgraph summary data tag entity_sum_lb (entity summary tag) is generated, entity_sum_lb is updated to the e_tgraph tag set LB={lb 1 , lb 2 , lb 3 ,entity_sum_lb,...,lb n}, and get the updated knowledge graph GAPTGraph.
[0051] In one or more embodiments, generating a community summary tag further includes:
[0052] Inputting community data sets corresponding to the plurality of communities and prompt word templates corresponding to community summaries into the large language model to generate a community summary matrix corresponding to each community;
[0053] Inputting community data sets corresponding to the multiple communities and prompt word templates corresponding to community questions into the large language model to generate a community question matrix corresponding to each community;
[0054] Based on the community summary matrix and the community question matrix, a community summary tag is generated. Specifically, in the embodiment of the present application, based on the generated community data set Sum={s 1 ,s 2 ,s 3 ,...s i ...,s n}, create community summary prompt word prompt = {s i , "Summarize and generate content for question-answer matching. Three summaries need to be generated"}, then s i Submit to large language model generation i Community Summary Matrix
[0055] Based on the generated community dataset Sum = {s 1 ,s 2 ,s 3 ,...s i ...,s n}, create community summary prompt word prompt = {s i , "Generate questions based on the submitted content, and generate three summaries"}, then s i Submit to large language model generation i Community Question Matrix Then, based on the community summary matrix comm_sum_lb i and community question matrix q i , generate community summary labels.
[0056] In one or more embodiments, generating a community summary tag based on the community summary matrix and the community question matrix includes:
[0057] Based on the community summary matrix and the community question matrix, obtaining a product matrix corresponding to the community question and the community summary;
[0058] Inputting the product matrix into the large language model to obtain the matching degree between the community question and the community summary;
[0059] Based on the community summary corresponding to the matching degree being greater than or equal to the preset threshold, the community summary label is obtained.
[0060] Specifically, in the embodiment of the present application, based on the community summary matrix and the community question matrix, the product matrix corresponding to the community question and the community summary is obtained. The product matrix SUM_Q i Input into the large language model, submit SUM_Q to the large language model to calculate the matching degree between the question and the summary, and score the matching degree from 1 to 10 to generate the summary score matrix V i =[comm_sum_lb_v 1 , comm_sum_lb_v 2 , comm_sum_lb_v 3 ], get the highest score summary sum_max_lb i =mxa(V i ), the community summary label sum_max_lb i Update to s i LB={lb 1 , lb 2 , lb 3 ,sum_max_lb,......,lb n}.
[0061] In one or more embodiments, matching the current question with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question includes:
[0062] Inputting the current question and the community summary tags in the updated knowledge graph into the large language model to obtain the matching degree between the current question and each community summary tag in the updated knowledge graph;
[0063] Based on the matching degree, a target community summary tag related to the current question is determined.
[0064] Specifically, in the embodiment of the present application, a community summary tag set F is obtained from the updated knowledge graph GAPTGraph through a Cypher graph query statement. LB = {sum_max_lb 1 , sum_max_lb 2 , sum_max_lb 3 ,......,lb n}, the current question query and F LB , assembled into prompt word prompt = {F LB , query}, submit the prompt word to the large language model, calculate the query and F LB Matching matrix QF LB = {qf 1 ,qf 2 ,qf 3 ,......,qf n}, based on the matching matrix, determine the target community summary tag related to the current question.
[0065] In one or more embodiments, determining a target community summary tag related to the current question text based on the matching degree includes:
[0066] Determine the average matching degree corresponding to each matching degree obtained;
[0067] The community summary tag corresponding to the matching degree greater than or equal to the average matching degree is determined as the target community summary tag related to the current question.
[0068] Specifically, in the embodiment of the present application, the current question query and the community summary tag set F are calculated. LB The matching degree is obtained by matching the score matrix QF LB = {qf 1 ,qf 2 ,qf 3 ,......,qf n}, the qf score range can be, for example, 1 to 10, and the target community summary label FQF related to the current question is obtained, where FQF = QF LB >agv(QF LB ).agv(QF LB ) is query and F LB The average matching degree of each summary label in .
[0069] In one or more embodiments, the acquiring a data query statement for the current database that matches the current question based on the target community summary tag and the triple information consisting of the entity, attribute, and relationship corresponding to the target community summary tag includes:
[0070] The target community summary label and entity, attribute and relationship triple information corresponding to the target community summary label are used as context prompt information;
[0071] The context information and the current question are submitted to a preset large language model to obtain a data query statement for the current database that matches the current question.
[0072] Specifically, in the implementation of this application, based on the above-mentioned target community summary label FQF, a subgraph set Sg with FQF summary labels is obtained from the updated knowledge graph GAPTGraph through a Cypher graph query statement. 1 , g 2 , g 3 ,... i ... n}where g i ={E i , R i , F i , L.B. i}. The current question query and the context subgraph Sg are combined into a prompt word prompt = [query, Sg] and submitted to the large language model. The large language model generates a data query statement corresponding to the current database based on prompt.
[0073] In addition, the application can also automatically detect whether the generated data query statement is available, for example, by connecting to the current database through the Java database connection jdbc, executing the generated data query statement, if the execution is successful, marking the end of generating the data query statement q_sql, if the execution is abnormal, regenerate the data query statement. If the new data query statement cannot be regenerated, it prompts to modify the query string query.
[0074] In an application embodiment, if Figure 4 As shown, the method for generating the above data query statement includes:
[0075] S402, creating a high-dimensional enhanced knowledge graph (GAPTGraph);
[0076] S404, generating a context subgraph (Sg);
[0077] S406: Generate a local data table query statement through a large language model based on the prompt words including the above context subgraph (Sg).
[0078] In an application embodiment, if Figure 5 As shown, the above step S402 includes the following steps:
[0079] S1. Get the local database table schema;
[0080] S2. Generate knowledge graph metadata shm_metadata through the script program.
[0081] S3. Generate a vector data set VE_DATA_SET from the metadata shm_metadata based on the vectorization algorithm GPT.
[0082] S4. The tree-based nearest neighbor search algorithm generates the alignment vector dataset AGM_VE_DATASET from Spotify.
[0083] S5. Construct table information knowledge graph TGraph = {E, R, F}.
[0084] S6. Generate entity summary data label entity_sum lb based on the large language model.
[0085] S7. Use Leiden graph clustering to divide TGraph into communities, and then generate community summary SUM based on the large language model to generate the community question matrix q.
[0086] S8. Score the community summary SUM to obtain the optimal summary LB.
[0087] S9. Update TGraph based on LB to obtain a high-dimensional enhanced graph GAPTGraph.
[0088] In an application embodiment, if Figure 6 As shown, the above step S404 includes the following steps:
[0089] S1. Obtain the community summary label set F from the high-dimensional enhanced graph GAPTGraph based on Cypher;
[0090] S2. Input the character question query and the community summary tag set F into the large language model to generate a community summary set FQF matching the query.
[0091] S3. Generate context subgraph Sg based on FQF.
[0092] In an application embodiment, if Figure 7 As shown, the above step S406 includes the following steps:
[0093] S1. Generate local data table query statement prompt based on character question query and context subgraph Sg;
[0094] S2. The large language model generates a local data table query statement q sql based on the prompt.
[0095] S3. Automatically detect whether the generated local data table query statement q_sql is available.
[0096] The following is a specific example to illustrate the method for generating the above data query statement:
[0097] 1. The local alarm database (current database) alarm_db contains data tables: vehicle alarm details table stat_vehicle_alarm, area data table sys_area_code, and vehicle file information table t_vehicle.
[0098] Generate graph metadata shm_metadata.
[0099] The stat_vehicle_alarm structure is as follows:
[0100] Serial number Field Name Field Name type Column Type 1 area_code Area Code str Foreign Keys 2 alarm_area Alarm area str 3 hphm License plate number str 4 alarm_time Alarm time time ……
[0101] The sys_area_code structure is as follows
[0102] Serial number Field Name Field Name type Column Type 1 code_id Region ID str Primary Key 2 code_name Region Name str 3 parent_code Parent area str ……
[0103] The t_vehicle structure is as follows
[0104] Serial number Field Name Field Name type Column Type 1 von Vehicle license plate str Primary Key 2 Vco Vehicle color str 3 Owner enterprise str ……
[0105] shm_metadata includes:
[0106] Table set T = [{alarm_db, stat_vehicle_alarm, vehicle alarm details table}, {alarm_db, sys_area_code, area data table}, {alarm_db, t_vehicle, vehicle file information table}],
[0107] Field collection Col = [
[0108] {alarm_db, stat_vehicle_alarm, area_code, str, area code},
[0109] {alarm_db, stat_vehicle_alarm, alarm_area, str, alarm area},
[0110] {alarm_db, stat_vehicle_alarm, hphm, str, license plate number}, ...,
[0111] {alarm_db, sys_area_code, code_id, str, area ID},
[0112] {alarm_db, t_vehicle, von, str, vehicle license plate},
[0113] {alarm_db, t_vehicle, vco, str, vehicle_color}]
[0114] Relation set Rel = [{stat_vehicle_alarm, hphm, t_vehicle, von, hphm, license plate number}, {stat_vehicle_alarm, area_code, sys_area_code, code_id, area_code, area code}]
[0115] 2. Generate a vector data set VE_DATA_SET = [T', Col', Rel'], the vector data set VE_DATA_SET includes:
[0116] T′=[{21001, 11101, 11102}, {21001, 11123, 11234}, {21001, 11345, 11346}],
[0117] Col′=[{21001, 11101, 32350, 32220, 41002, 32220}, {21001, 11101, 32370, 32330, 41002, 32330}, {21001, 11101, 32320, 32440, 410 02, 32440}, ..., {21001, 11123, 32210, 32550, 41002}, {21001, 11345, 32660, 32880, 41002}, {21001, 11345, 32770, 32990, 41002}],
[0118] Rel′=[{11101, 32320, 11345, 32660, 32320, 32440}, {11101, 32350, 11123, 32210, 32350, 32220}]
[0119] 3. Align the vector data sets. Use the tree-based nearest neighbor search algorithm to merge semantically similar vectors into one vector through vector matching query. The "vehicle license plate" and "license plate number" are externally represented as "license plate number"; "region ID" and "region code" are aligned as "region code".
[0120] Table set T = [{alarm_db, stat_vehicle_alarm, vehicle alarm details table}, {alarm_db, sys_area_code, area data table}, {alarm_db, t_vehicle, vehicle file information table}],
[0121] Field collection Col = [
[0122] {alarm_db, stat_vehicle_alarm, area_code, str, area code},
[0123] {alarm_db, stat_vehicle_alarm, alarm_area, str, alarm area},
[0124] {alarm_db, stat_vehicle_alarm, hphm, str, license plate number}, ...,
[0125] {alarm_db,sys_area_code,area_code,str,area code},
[0126] {alarm_db, t_vehicle, hphm, str, license plate number},
[0127] {alarm_db, t_vehicle, vco, str, vehicle_color}]
[0128] Relationship set Rel =
[0129] [{stat_vehicle_alarm, hphm, t_vehicle, hphm, hphm, license plate number}, {stat_vehicle_alarm, area_code, sys_area_code, area_code, area_code, area code}]
[0130] Generate an alignment vector data set AGM_VE_DATA_SET=[T″, Col″, Rel″].
[0131] 4. Construct the table information knowledge graph, and generate the knowledge graph triple set according to the alignment vector data AGM_VE_DATA_SET=[T″, Col″, Rel″] generated in step 3 through the mapping function f(veparm, [e, r, f, lb]).
[0132] E={stat_vehicle_alarm, sys_area_code, t_vehicle}
[0133] R={area_code,alarm_area,…,hphm,vco}
[0134] F=[{stat_vehicle_alarm, hphm, t_vehicle, hphm}, {stat_vehicle_alarm, area_code, sys_area_code, area_code}]
[0135] LB = [{alarm_db, stat_vehicle_alarm, vehicle alarm details table}, {alarm_db, sys_area_code, area data table}, {alarm_db, t_vehicle, vehicle file information table}]
[0136] Generate table knowledge graph TGraph = {E, R, F}:
[0137] create(a:alarm_db:stat_vehicle_alarm:vehicle alarm details table {coll:area_code, coll:alarm_area, coll:hphm,.....});
[0138] create(a:alarm_db:sys_area_code:area data table {coll:area_code,.....});
[0139] create(a:alarm_db:t_vehicle:vehicle file information table {coll:hphm,.....});
[0140] create(a:alarm_db:t_vehicle:vehicle file information table{coll:vco,.....});
[0141] CREATE(stat_vehicle_alarm)-[:area_code]->(sys_area_code)
[0142] CREATE(stat_vehicle_alarm)-[:hphm]->(t_vehicle).
[0143] 5. Generate entity data set E = {stat_vehicle_alarm, sys_area_code, t_vehicle} entity overview data label. Get the subgraph e_tgraph = MATCH (n: stat_vehicle_alarm) with a depth of 1 for stat_vehicle_alarm
[0144] OPTIONAL MATCH(n)-[r]-()
[0145] RETURN n, r.
[0146] Create entity summary prompt word prompt = {context: "e_tgraph", "query": "overall knowledge graph content"} and submit it to the large language model, generate the intermediate tag entity_sum_lb = {"data": "vehicle alarm details table, including license plate, alarm area and other information, and query with the vehicle file table through license plate number association"}, and update it to LB of stat_vehicle_alarm.
[0147] MATCH(n:stat_vehicle_alarm)
[0148] SET n:{"data":"Vehicle alarm details table, including license plate, alarm area and other information, and is queried by associating the license plate number with the vehicle file table"}
[0149] RETURN n;
[0150] Repeat step 5 to generate sys_area_code, t_vehicle summary entity_sum_lb.
[0151] 6. Divide TGraph into communities. Use the Leiden graph clustering algorithm to divide the table knowledge graph TGraph generated in step 5 into communities. Two communities Sum = {s 1 : Vehicle community, s 2 : Regional Community},
[0152] s 1 ={E{stat_vehicle_alarm,t-vehicle},
[0153] R{area code , alarm area , ..., hphm, vco},
[0154]
[0155] LB[{alarm_db, stat_vehicle_alarm, vehicle alarm details table},
[0156] {alarm_db, t_vehicle, vehicle profile information table}]}
[0157] s 2 ={{E{sys_area_code},
[0158] R{area_code},
[0159] F[{stat_vehicle_alarm, area_code, sys_area_code, area_code}],
[0160] LB = [{alarm_db, sys_area_code, area data table}]}}.
[0161] 7. Generate a community summary matrix based on the community dataset Sum = {s 1 : Vehicle community, s 2 : Regional community}, create community summary prompt word prompt = {s 1 , "summarize the content generated for question-answer matching, and generate three summaries"}, large language model generates s 1 Community Summary Matrix Repeat step 7 to generate s 2 Summarize.
[0162] 8. Generate a community summary matrix based on the community dataset Sum = {s 1 : Vehicle community, s 2 : Regional community}, create community summary prompt word prompt = {s 1 , "Generate questions based on the submitted content, and generate three summaries"}, large language model generates s 1 The community problem matrix q 1 = [vector data set VE_DATA_SET], repeat step 7 to generate s 2 question.
[0163] 9. Calculate the optimal community summary and update it to TGraph to generate a high-dimensional enhanced graph GAPTGraph. Based on the community Sum generated in step 6 = {s 1 : Vehicle community, s 2: Regional community}, generate the corresponding matrix of questions and community summaries through the community intermediate matrix and district question matrix generated in steps 7 and 8 Create scoring prompt word prompt = {SUM Q 1 , "Calculate the matching degree between the question and the summary, and score the matching degree from 1 to 10"}, generate the summary score matrix V 1 = [10, 8, 9], calculate the summary with the highest score, sum_max_lb 1 =mxa(V 1 ),
[0164] Update the highest-scoring "Summary 1" to LB 1 =
[0165] [{alarm_db, stat_vehicle_alarm, vehicle alarm details table, {vehicle community summary: summary 1}}, {alarm_db, t_vehicle, vehicle file information table, {regional community summary: summary 1}}]}, repeat step 9 for s 2 Compute the optimal community summary.
[0166] 10. Obtain the community summary set that matches the question according to the question, and obtain the community summary tag set in the knowledge graph GAPTGraph generated in step 9 through the Cypher graph query statement Cypher = MATCH (n: community summary) return n, F LB ={Vehicle community summary: summary 1, Regional community summary: summary 2}, F LB =n Substitute the characters in step 1 into query = question: Get the total number of alarms for the vehicle and the company to which the vehicle belongs. Calculate the summary that matches the question and give a score ranging from 1 to 10. Assemble the question and community summary prompt word prompt = {F LB , query}, submit the prompt word to the large language model to obtain QF LB ={10, 4}, FQF={10, 4}>agv({10, 4}), get the summary through the index, FQF={vehicle community summary}.
[0167] 11. Based on the regional summary FQF={vehicle community summary} obtained in step 10, obtain the subgraph set Sg=MATCH(n:vehicle community summary{p*})OPTIONALMATCH(n)-[r]-(f)RETURN n, r, p, f with the FQF summary label in GAPTGraph through the Cypher graph query statement.
[0168] 12. Combine the question query = "Question: Get the total number of vehicle alarms and the company to which the vehicle belongs. Use the knowledge graph as the morning and afternoon, and generate a local database SQL query statement according to the question." with the context subgraph Sg generated in step 11 to form the prompt word prompt = [query, Sg] and submit it to the large language model. The large language model generates a local data table SQL query statement q_sql based on the prompt word prompt.
[0169] 13. Execute the q_sql generated in step 12. If the execution is abnormal, repeat steps 10, 11, 12, and 13. If the query statement q_sql cannot be generated and executed after repeating the above three steps, you will be prompted to modify the character query.
[0170] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity, they are all described as a series of action combinations, but those skilled in the art should know that the present invention is not limited by the described action sequence, because according to the present invention, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present invention.
[0171] According to another aspect of the embodiment of the present application, a data query statement generation device for implementing the above-mentioned data query statement generation method is also provided. Figure 8 As shown, the device comprises:
[0172] A creation division unit 802 is used to create an initial knowledge graph corresponding to the current database, and divide the knowledge graph into multiple communities using a preset clustering algorithm;
[0173] A generating unit 804 is used to generate a community summary tag and an entity overview tag corresponding to the entity in the initial knowledge graph based on a preset large language model; the community summary tag is used to indicate the content matching the community and the question;
[0174] An updating unit 806, configured to update the initial knowledge graph based on the community summary tag and / or the entity overview tag to obtain an updated knowledge graph;
[0175] A matching unit 808, configured to match the current question with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question;
[0176] The acquisition unit 810 is used to submit the target community summary tag and the triple information consisting of the entity, attribute and relationship corresponding to the target community summary tag to a preset large language model, and obtain a data query statement for the current database that matches the current question through the large language model.
[0177] In the embodiment of the present application, an initial knowledge graph corresponding to the current database is created, and the knowledge graph is divided into multiple communities using a preset clustering algorithm; community summary tags and entity overview tags corresponding to entities in the initial knowledge graph are generated; the community summary tags are used to indicate the content that matches the community and the question; the initial knowledge graph is updated based on the community summary tags and / or entity overview tags to obtain an updated knowledge graph; the current question is matched with the community summary tags in the updated knowledge graph to obtain the target community summary tags related to the current question; based on the target community summary tags and the triple information consisting of entities, attributes and relationships corresponding to the target community summary tags, a method for obtaining a data query statement for the current database that matches the current question is obtained; in the above method, the data tables and data files in the database are decomposed into smaller and easier to manage community modules, so that the key information of the database can be submitted to the large language model at one time without causing the loss of context information. It can also accurately generate query statements for the local database corresponding to the question.
[0178] According to another aspect of the embodiment of the present application, an electronic device for implementing the above-mentioned method for generating a data query statement is also provided. The electronic device may be Fig. 9 The terminal device or server shown in the figure. This embodiment is described by taking the electronic device as a server as an example. Fig. 9 As shown, the electronic device includes a memory 902 and a processor 904. The memory 902 stores a computer program, and the processor 904 is configured to execute the steps in any of the above method embodiments through the computer program.
[0179] Optionally, in this embodiment, the electronic device may be located in at least one network device among a plurality of network devices of a computer network.
[0180] Optionally, in this embodiment, the processor may be configured to perform the following steps through a computer program:
[0181] S1, creating an initial knowledge graph corresponding to the current database, and dividing the knowledge graph into multiple communities using a preset clustering algorithm;
[0182] S2, based on a preset large language model, generating a community summary tag and an entity overview tag corresponding to the entity in the initial knowledge graph; the community summary tag is used to indicate the content matching the community and the question;
[0183] S3, updating the initial knowledge graph based on the community summary label and / or entity overview label to obtain an updated knowledge graph;
[0184] S4, matching the current question with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question;
[0185] S5, based on the target community summary tag and the triple information consisting of the entity, attribute and relationship corresponding to the target community summary tag, obtain a data query statement for the current database that matches the current question through the large language model.
[0186] Alternatively, a person skilled in the art may understand that: Fig. 9 The structure shown is for illustration only, and the electronic device may also be a smart phone (such as an Android phone, an iOS phone, etc.), a tablet computer, a PDA, a mobile Internet device (MID), a PAD, and other terminal devices. Fig. 9 The electronic device and the electronic equipment described above are not limited in structure. Fig. 9 More or fewer components (such as network interfaces, etc.) as shown in, or with Fig. 9 Different configurations shown.
[0187] Among them, the memory 902 can be used to store software programs and modules, such as the program instructions / modules corresponding to the method and device for generating data query statements in the embodiments of the present application. The processor 904 executes various functional applications and data processing by running the software programs and modules stored in the memory 902, that is, realizing the above-mentioned method for generating data query statements. The memory 902 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 902 may further include a memory remotely arranged relative to the processor 904, and these remote memories may be connected to the terminal via a network. Examples of the above-mentioned networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof. Among them, the memory 902 may be specifically, but not limited to, used to store resources corresponding to the target project, as well as question information input by the user and data query statements generated by the system. As an example, Fig. 9As shown, the memory 902 may include, but is not limited to, the creation and division unit 802, the generation unit 804, the update unit 806, the matching unit 808, and the acquisition unit 806 in the device for generating the data query statement. In addition, other module units in the device for generating the data query statement may also be included but are not limited to, which will not be described in detail in this example.
[0188] Optionally, the transmission device 906 is used to receive or send data via a network. Specific examples of the above-mentioned network may include wired networks and wireless networks. In one example, the transmission device 909 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices and routers via a network cable so as to communicate with the Internet or a local area network. In one example, the transmission device 906 is a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0189] In addition, the electronic device also includes: a display 908 for displaying resources corresponding to the target project, as well as question information input by the user and data query statements generated by the system; and a connection bus 910 for connecting various module components in the electronic device.
[0190] In other embodiments, the terminal device or server may be a node in a distributed system, wherein the distributed system may be a blockchain system, and the blockchain system may be a distributed system formed by connecting the multiple nodes through network communication. Among them, the nodes may form a peer-to-peer (P2P, Peer To Peer) network, and any form of computing device, such as a server, terminal and other electronic devices, may become a node in the blockchain system by joining the peer-to-peer network.
[0191] According to one aspect of the present application, a computer program product or computer program is provided, the computer program product or computer program includes computer instructions, the computer instructions are stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the above-mentioned method for generating a data query statement, wherein the computer program is configured to execute the steps of any one of the above-mentioned method embodiments when it is run.
[0192] Optionally, in this embodiment, the computer-readable storage medium may be configured to store a computer program for performing the following steps:
[0193] S1, creating an initial knowledge graph corresponding to the current database, and dividing the knowledge graph into multiple communities using a preset clustering algorithm;
[0194] S2, based on a preset large language model, generating a community summary tag and an entity overview tag corresponding to the entity in the initial knowledge graph; the community summary tag is used to indicate the content matching the community and the question;
[0195] S3, updating the initial knowledge graph based on the community summary label and / or entity overview label to obtain an updated knowledge graph;
[0196] S4, matching the current question with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question;
[0197] S5, based on the target community summary tag and the triple information consisting of the entity, attribute and relationship corresponding to the target community summary tag, obtain a data query statement for the current database that matches the current question through the large language model.
[0198] Optionally, in this embodiment, a person of ordinary skill in the art may understand that all or part of the steps in the various methods of the above embodiments may be completed by instructing hardware related to the terminal device through a program, and the program may be stored in a computer-readable storage medium, and the storage medium may include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk, etc.
[0199] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.
[0200] If the integrated units in the above embodiments are implemented in the form of software functional units and sold or used as independent products, they can be stored in the above computer-readable storage medium. Based on such understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling one or more computer devices (which can be personal computers, servers or network devices, etc.) to perform all or part of the steps of the methods of various embodiments of the present invention.
[0201] In the above embodiments of the present invention, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0202] In the several embodiments provided in the present application, it should be understood that the disclosed client can be implemented in other ways. Among them, the device embodiments described above are only schematic, for example, the division of units is only a logical function division, and there may be other division methods in actual implementation, for example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0203] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0204] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0205] The above are only preferred embodiments of the present invention. It should be pointed out that, for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A method for generating a data query statement, characterized in that: include: Creating an initial knowledge graph corresponding to the current database, and dividing the knowledge graph into multiple communities using a preset clustering algorithm; Based on a preset large language model, generate community summary tags and entity overview tags corresponding to entities in the initial knowledge graph; The community summary tag is used to indicate the content of the community that matches the question; Update the initial knowledge graph based on the community summary label and / or entity overview label to obtain an updated knowledge graph; Matching the current question with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question; Based on the target community summary tag and triple information consisting of entities, attributes and relationships corresponding to the target community summary tag, a data query statement for the current database matching the current question is obtained through the large language model.
2. The method according to claim 1, characterized in that The step of creating an initial knowledge graph corresponding to the current database includes: Obtaining a table set, a field set, and a relationship set corresponding to the current database; Vectorizing the table set, field set and relationship set to obtain corresponding initial table vectors, initial field vectors and initial relationship vectors; Merging vectors whose similarity is greater than or equal to a preset threshold among the initial table vector, the initial field vector, and the initial relationship vector to obtain corresponding table embedding vectors, field embedding vectors, and relationship embedding vectors; Based on the table embedding vector, the field embedding vector and the relationship embedding vector, an initial knowledge graph corresponding to the current database is generated.
3. The method according to claim 1 or 2, characterized in that: The generating of entity overview labels corresponding to entities in the initial knowledge graph includes: Obtain each entity in the initial knowledge graph, and take each of the entities as a center point to obtain a target subgraph whose entity depth is less than a preset depth; the target subgraph is a subgraph included in the initial knowledge graph; The target subgraph is input into the large language model to obtain an entity summary label corresponding to the entity.
4. The method according to claim 3, characterized in that The updating of the initial knowledge graph based on the community summary tag and / or entity overview tag to obtain an updated knowledge graph includes: The acquired entity overview label corresponding to the entity is added to the entity label set corresponding to the initial knowledge graph to obtain the updated knowledge graph.
5. The method according to claim 1 or 2, characterized in that: The generating of community summary labels includes: Inputting community data sets corresponding to the plurality of communities and prompt word templates corresponding to community summaries into the large language model to generate a community summary matrix corresponding to each community; Inputting community data sets corresponding to the multiple communities and prompt word templates corresponding to community questions into the large language model to generate a community question matrix corresponding to each community; Based on the community summary matrix and the community question matrix, a community summary label is generated.
6. The method according to claim 5, characterized in that The generating of community summary labels based on the community summary matrix and the community question matrix includes: Based on the community summary matrix and the community question matrix, obtaining a product matrix corresponding to the community question and the community summary; Inputting the product matrix into the large language model to obtain the matching degree between the community question and the community summary; Based on the community summary corresponding to the matching degree being greater than or equal to the preset threshold, the community summary label is obtained.
7. The method according to claim 6, characterized in that The step of matching the current question with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question includes: Inputting the current question and the community summary tags in the updated knowledge graph into the large language model to obtain the matching degree between the current question and each community summary tag in the updated knowledge graph; Based on the matching degree, a target community summary tag related to the current question is determined.
8. The method according to claim 7, characterized in that The determining, based on the matching degree, a target community summary tag related to the current question text includes: Determine the average matching degree corresponding to each matching degree obtained; The community summary tag corresponding to the matching degree greater than or equal to the average matching degree is determined as the target community summary tag related to the current question.
9. The method according to claim 7 or 8, characterized in that: The step of obtaining a data query statement for the current database that matches the current question based on the target community summary tag and the triple information consisting of the entity, attribute, and relationship corresponding to the target community summary tag includes: The target community summary label and entity, attribute and relationship triple information corresponding to the target community summary label are used as context prompt information; The context information and the current question are submitted to a preset large language model to obtain a data query statement for the current database that matches the current question.
10. A device for generating a data query statement, characterized in that: include: Creating a partitioning unit, used to create an initial knowledge graph corresponding to the current database, and using a preset clustering algorithm to divide the knowledge graph into multiple communities; A generating unit, configured to generate a community summary label and an entity overview label corresponding to an entity in the initial knowledge graph based on a preset large language model; The community summary tag is used to indicate the content of the community that matches the question; An updating unit, configured to update the initial knowledge graph based on the community summary tag and / or the entity overview tag to obtain an updated knowledge graph; A matching unit, used to match the current question with the community summary tag in the updated knowledge graph to obtain a target community summary tag related to the current question; An acquisition unit is used to submit the target community summary tag and the triple information consisting of the entity, attribute and relationship corresponding to the target community summary tag to a preset large language model, and obtain a data query statement for the current database that matches the current question through the large language model.
11. An electronic device comprising a memory and a processor, characterized in that: A computer program is stored in the memory, and the processor is configured to execute the method according to any one of claims 1 to 9 through the computer program.
12. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored program, wherein the program executes the method described in any one of claims 1 to 9 when executed.
Citation Information
Cited By
Entity extraction agent construction method and system based on large language model
CN120197686A