Query statement generation method and device of knowledge graph and electronic equipment

By combining natural language questions with question templates to generate large language model questions, and using large language models to generate initial statements, and finally generating knowledge graph query statements, the problems of low generation efficiency and limited applicability in the existing technology are solved, and more efficient query statement generation is achieved.

CN119557328AActive Publication Date: 2025-03-04TSINGHUA UNIVERSITY

Patent Information

Application Number
CN202411539171.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-31
Publication Date
2025-03-04
Estimated Expiration
2044-10-31

AI Technical Summary

Technical Problem

In the prior art, the applicability of the knowledge graph query statement generation method is limited and requires the execution of complex training processes, making it difficult to effectively analyze users' natural language questions and improve generation efficiency.

Method used

By obtaining natural language questions and combining them with pre-set question templates to generate large language model questions, ask questions to the large language model to obtain the initial statement, and finally generate query statements for the target query language based on the initial statement.

Benefits of technology

It improves the generation efficiency of knowledge graph query statements, can more effectively parse users' natural language questions, enhances the generation applicability of query statements, and avoids complex training processes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119557328A_ABST
    Figure CN119557328A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of knowledge graphs, and provides a knowledge graph query statement generation method and device and electronic equipment, and the knowledge graph query statement generation method comprises the steps: obtaining a natural language question for a knowledge graph; generating a big language model question according to the natural language question and a preset question template, wherein the question template is used for guiding a big language model to convert the natural language question into a statement of a target query language; asking a question to the big language model according to the big language model question to obtain an initial statement of a target query language output by the big language model; and generating a query statement of the target query language based on the initial statement. According to the method, the generation efficiency of the knowledge graph query statement is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of knowledge graph technology, and in particular to a method, device and electronic device for generating query statements for a knowledge graph. Background Art

[0002] With the rapid development of information technology, knowledge graphs have become an important tool for storing and organizing structured knowledge. Knowledge graphs effectively represent and store a large amount of triple information through a graphical data structure consisting of entities and relationships. In many fields, such as search engines, recommendation systems, and natural language processing, knowledge graphs provide powerful data support and knowledge reasoning capabilities.

[0003] As one of the important ways of human-computer interaction, the research and application of knowledge graph question answering system has received extensive attention. The system analyzes the user's natural language questions and generates corresponding query languages, such as SPARQL or RDF query language query statements, to retrieve answers from the knowledge graph.

[0004] Existing knowledge graph question answering methods are mainly divided into rule-based template methods and deep learning-based methods. The rule-based template method extracts key information from the question and generates query statements in the target query language by finding the corresponding template according to the format of the question. However, due to the diversity of human questioning methods, it is difficult to cover all possible questioning scenarios, resulting in limited applicability. The deep learning-based method relies on a large amount of knowledge graph data and trains machine learning models to predict query statements in the target query language. However, the performance of this method is highly dependent on the quantity and quality of training data, and the training process is relatively complex. Summary of the invention

[0005] The present invention provides a method, device and electronic device for generating query statements of a knowledge graph, which are used to solve the problems in the prior art that the applicability of query statement generation methods is limited and a complex training process needs to be performed. The method can effectively parse the user's natural language questions and improve the efficiency of query statement generation.

[0006] The present invention provides a method for generating query statements for a knowledge graph, comprising: obtaining a natural language question for a knowledge graph; generating a large language model question according to the natural language question and a preset question template, wherein the question template is used to guide the large language model to convert the natural language question into a statement in a target query language; asking the large language model a question according to the large language model question to obtain an initial statement in the target query language output by the large language model; and generating a query statement in the target query language based on the initial statement.

[0007] According to a method for generating query sentences for a knowledge graph provided by the present invention, the question template includes a question head for asking questions to a large language model and a query example sentence in the target query language, the query example sentence in the target query language includes a general example sentence and a targeted example sentence, and the targeted example sentence is determined by the following steps: selecting a natural language example sentence similar to the natural language question sentence from a preset natural language example sentence set based on the large language model; and determining the selected natural language example sentence and the example sentence of the target query language corresponding to the selected natural language example sentence as the targeted example sentence of the natural language question sentence.

[0008] According to a method for generating query statements for a knowledge graph provided by the present invention, the query statement corresponding to the natural language question sentence is generated based on the initial statement, comprising: verifying whether the grammar of the initial statement is correct; if the grammar of the initial statement is verified to be incorrect, regenerating the initial statement based on a large language model; if the grammar of the initial statement is verified to be correct, performing a predefined statement correction operation on the initial statement to obtain the query statement.

[0009] According to a method for generating query statements for a knowledge graph provided by the present invention, the knowledge graph is established based on an encyclopedia website, and the query statement is obtained by performing a predefined statement correction operation on the initial statement, including: using a regular expression to determine a core entity in the initial query statement; performing entity linking on the core entity based on a pre-established entity linking tool of the encyclopedia website to obtain a formal name of the core entity; and replacing the core entity in the initial statement with the formal name.

[0010] According to a method for generating query statements for a knowledge graph provided by the present invention, after replacing the core entity in the initial statement with the formal name, performing a predefined statement correction operation on the initial statement to obtain the query statement also includes: determining whether there is a first type of triple in the initial statement, the first type of triple including a first head entity, a first relationship and a first tail entity, the first head entity and the first relationship are known, and the first tail entity is unknown; if there is a first type of triple in the initial statement, obtaining the attribute associated with the first head entity based on a pre-updated entity and relationship list of the knowledge graph; selecting a first attribute matching the first relationship from the attributes associated with the first head entity based on a large language model; replacing the first relationship in the initial query statement with the first attribute; and replacing the first tail entity in the initial query statement with the attribute value of the first attribute.

[0011] According to a method for generating query statements for a knowledge graph provided by the present invention, the first tail entity in the initial query statement is replaced with the attribute value corresponding to the first attribute, including: judging whether the first tail entity is a jump node for transition in a multi-hop problem; if the first tail entity is a jump node for transition in a multi-hop problem, judging whether the attribute value corresponding to the first attribute is multiple entities; if the attribute value corresponding to the first attribute is multiple entities, obtaining a subsequent relationship of the jump node; obtaining an alternative relationship list matching the subsequent relationship from a pre-updated list of entities and relationships of the knowledge graph based on similarity; determining multiple relationship items close to the subsequent relationship from the alternative relationship list based on a large language model; and replacing the subsequent relationship in the initial query statement with the multiple relationship items.

[0012] According to a method for generating query statements for a knowledge graph provided by the present invention, after replacing the core entity in the initial statement with the formal name, performing a predefined statement correction operation on the initial statement to obtain the query statement also includes: determining whether there is a second type of triple in the initial statement, the second type of triple including a second head entity, a second relationship, and a second tail entity, the second tail entity and the second relationship are known, and the second head entity is unknown; if there is a second type of triple in the initial statement, querying the attribute list corresponding to the attribute value matching the second tail entity in the knowledge graph based on the pre-updated entity and relationship list of the knowledge graph; selecting at least one second attribute matching the second relationship from the attribute list based on a large language model; and replacing the second relationship in the initial query statement with the at least one second attribute.

[0013] The present invention also provides a query statement generation device for a knowledge graph, comprising: an acquisition module, configured to acquire a natural language question for a knowledge graph; a first generation module, configured to generate a large language model question based on the natural language question and a pre-set question template, wherein the question template is used to guide the large language model to convert the natural language question into a statement in a target query language; a questioning module, configured to ask a question to the large language model based on the large language model question, and obtain an initial statement in the target query language output by the large language model; and a second generation module, configured to generate a query statement in the target query language based on the initial statement.

[0014] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, a method for generating query statements for the knowledge graph as described in any one of the above-mentioned methods is implemented.

[0015] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a method for generating a query statement for a knowledge graph as described in any one of the above.

[0016] The present invention also provides a computer program product, including a computer program, which, when executed by a processor, implements a query statement generation method for a knowledge graph as described in any one of the above.

[0017] The knowledge graph query statement generation method, device and electronic device provided by the present invention generate a large language model question according to a natural language question and a pre-set question template, and then ask the large language model questions according to the large language model question to obtain an initial sentence of the target query language output by the large language model, and finally generate a query sentence of the target query language based on the initial sentence, thereby improving the generation efficiency of the knowledge graph query sentence. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are 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.

[0019] Figure 1 It is one of the flow charts of the query statement generation method of the knowledge graph provided by the present invention.

[0020] Figure 2 This is the second flow chart of the query statement generation method of the knowledge graph provided by the present invention.

[0021] Figure 3 It is a structural schematic diagram of the query statement generating device of the knowledge graph provided by the present invention.

[0022] Figure 4 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0023] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0024] Combine the following Figure 1-Figure 4Describe the query statement generation method, device and electronic device of the knowledge graph of the present invention.

[0025] Figure 1 This is one of the flow charts of the method for generating a query statement of a knowledge graph provided by an embodiment of the present invention. Figure 1 As shown, the method includes: Step 101: Obtain natural language questions for the knowledge graph.

[0026] In this embodiment, a natural language question input by a user may be obtained, and the natural language question may indicate the information that the user wants to obtain from the knowledge graph.

[0027] Step 102: Generate a large language model question based on the natural language question and a preset question template.

[0028] In this embodiment, the question template is used to guide the large language model to convert natural language questions into sentences in the target query language. Large language models (LLMs) refer to natural language processing models with a large number of parameters that are trained through deep learning and machine learning technologies. Large language models may include BERT (Bidirectional Encoder Representations from Transformers), GPT (Generative Pre-trained Transformer) series, etc.

[0029] Question templates can cover various query scenarios and contain necessary placeholders for replacing with natural language questions or position information indicating natural language questions so as to be combined with natural language questions. For example, a question template can be a question header and a place to fill in the natural language question. Optionally, the question template can also include some auxiliary information, such as some information that helps to improve the accuracy of the generated sentence, such as requirements or examples. Taking the query language as SparQL as an example, the question header in the question template can include "Write a sparql query based on the following question to query the virtuoso testData database. The database stores entries and attributes from the preset encyclopedia website. The answer only contains sparql statements, and English is in lowercase, and no explanation is required." Virtuoso is a high-performance RDF (Resource Description Framework) four-color database management system that supports the storage, retrieval and management of RDF data.

[0030] Step 103: Ask the large language model a question according to the large language model question sentence, and obtain an initial sentence of the target query language output by the large language model.

[0031] In this embodiment, the large language model can output an initial sentence of the target query language based on its own processing logic.

[0032] Step 104: Generate a query statement in the target query language based on the initial statement.

[0033] In this embodiment, generating a query statement in the target query language based on the initial statement may include one or more of the following operations: statement verification, entity replacement, and relationship replacement. It may also include modification based on other pre-set rules, which may be specifically set according to the actual situation of generating the initial statement.

[0034] The knowledge graph query statement generation method provided by the present invention generates a large language model question according to a natural language question and a pre-set question template, and then asks the large language model according to the large language model question to obtain an initial sentence of the target query language output by the large language model, and finally generates a query sentence of the target query language based on the initial sentence. In the knowledge graph question and answer, no additional model is trained, and only a general large model is used with code and basic tools, which improves the generation efficiency of the knowledge graph query statement.

[0035] In some optional implementations, the question template includes a question head for asking questions to the large language model and a query example in the target query language. The query example in the target query language includes a general example and a targeted example. The targeted example is determined by the following steps: selecting a natural language example similar to the natural language question from a preset natural language example set based on the large language model; determining the selected natural language example and the example in the target query language corresponding to the selected natural language example as the targeted example of the natural language question. In this implementation, the query example in the target query language helps the large language model to generate the initial question more accurately, and the targeted example enables the large language model to understand the natural language question more accurately and generate the initial sentence corresponding to the natural language question.

[0036] As an example, the beginning of the pre-set general question can be "Write a sparql query based on the following question to the testData database of virtuoso. The database stores entries and attributes from Baidu. The answer only contains sparql statements. English is in lowercase and no explanation is required." Then the obtained natural language question can be added to the question header, and finally the reference example sentence can be added. It should be noted that the positions of the above parts can be set according to actual needs.

[0037] In some optional implementations, a query statement corresponding to a natural language question is generated based on an initial statement, including: verifying whether the grammar of the initial statement is correct; if the grammar of the initial statement is incorrect, regenerating the initial statement based on a large language model; if the grammar of the initial statement is correct, performing a predefined sentence correction operation on the initial statement to obtain a query statement.

[0038] In this implementation, the query language verification tool or pre-established rules can be used to verify whether the initial statement is correct. For example, if the query language is SparQL, the initial SparQL statement can be verified by SPARQLWrapper, which is a SparQL toolkit in Python. This implementation ensures the accuracy of the generated initial statement result and format by verifying the initial statement output by the large language model.

[0039] In some optional implementations, the knowledge graph is established based on an encyclopedia website, and a predefined sentence correction operation is performed on an initial sentence to obtain a query sentence, including: using regular expressions to determine the core entities in the initial query sentence; performing entity linking on the core entities based on an entity linking tool of a pre-established encyclopedia website to obtain the formal name of the core entity; and replacing the core entity in the initial sentence with the formal name.

[0040] Specifically, the search and analysis capabilities of tools such as ES (Elasticsearch) can be used to solve the ambiguity problem of entity recognition in the knowledge graph. In addition, the knowledge graph can be queried in advance through query languages ​​such as SparQL to obtain the latest entity and relationship list, and saved into a dict structure (key-value dictionary structure) according to the first and last characters and the corresponding entity, that is, the cached entity and relationship list is updated when the program starts, so as to ensure that it can be used normally and with a high accuracy rate when the knowledge graph triples undergo major changes.

[0041] As an example, ES can be used to compare the similarity between core instances and instances in the knowledge graph, and entity links based on encyclopedia websites can be used to obtain instances in the knowledge graph. The reason for using the entity linking tool is that many entities in common languages ​​are colloquial. For example, many colloquial abbreviations are generally saved as full names in the knowledge graph. In this case, it is difficult to obtain them only by similarity. Therefore, the feature that searching for abbreviations on encyclopedia websites will jump to the entry of the full name can be used to obtain the full name of the entity, which helps to locate the correct instance in the knowledge graph. The combination of ES similarity comparison and encyclopedia-based entity linking solves the problem of decreased accuracy in locating corresponding entities in the knowledge graph caused by colloquial entities and aliases to a certain extent.

[0042] In some alternative implementation manners, after replacing the core entity in the initial statement with the formal name, performing a predefined statement correction operation on the initial statement to obtain the query statement further includes: determining whether there is a triple of the first type in the initial statement, where the triple of the first type includes a first head entity, a first relationship, and a first tail entity, the first head entity and the first relationship are known, and the first tail entity is unknown; if there is a triple of the first type in the initial statement, obtaining the attributes associated with the first head entity based on the entity and relationship list of the pre-updated knowledge graph; selecting a first attribute that matches the first relationship from the attributes associated with the first head entity based on the large language model; replacing the first relationship in the initial query statement with the first attribute; and replacing the first tail entity in the initial query statement with the attribute value of the first attribute.

[0043] As an example, in the triple "<instance / Name A><property / graduation school>?instance1", the head entity Name A is known, the attribute or relationship graduation school is known, and the tail entity "?instance1" is unknown. This triple is a triple of the first type. If it is subsequently queried from the knowledge graph that the attribute value of "<instance / Name A><property / graduation school>" is School A, then the tail entity "?instance1" can be replaced with "<instance / School A>".

[0044] In some alternative implementation manners, replacing the first tail entity in the initial query statement with the attribute value corresponding to the first attribute includes: determining whether the first tail entity is a jump node used for transition in a multi-hop problem; if the first tail entity is a jump node used for transition in a multi-hop problem, determining whether the attribute value corresponding to the first attribute is multiple entities; if the attribute value corresponding to the first attribute is multiple entities, obtaining the subsequent relationship of the jump node; obtaining an alternative relationship list that matches the subsequent relationship from the entity and relationship list of the pre-updated knowledge graph based on similarity; determining multiple relationship items that are close to the subsequent relationship from the alternative relationship list based on the large language model; and replacing the subsequent relationship in the initial query statement with the multiple relationship items.

[0045] In addition, if the attribute value corresponding to the first attribute is a single entity, obtaining the subsequent relationship of the jump node; obtaining an alternative relationship list that matches the subsequent relationship from the entity and relationship list of the pre-updated knowledge graph based on similarity; determining a single relationship item that is close to the subsequent relationship from the alternative relationship list based on the large language model; and replacing the subsequent relationship in the initial query statement with the single relationship item.

[0046] In this implementation, different methods are used to handle the relationships of entities when generating query statements and the relationships of entities that are not determined during multi-hop queries. When dealing with multi-hop problems, multiple relationship items are used instead of a single relationship, ensuring that results are not easily missed when dealing with multi-hop problems.

[0047] In some alternative implementations, after replacing the core entity in the initial statement with the formal name, performing a predefined statement correction operation on the initial statement to obtain the query statement further includes: determining whether there is a second type of triple in the initial statement, where the second type of triple includes a second head entity, a second relationship, and a second tail entity, the second tail entity and the second relationship are known, and the second head entity is unknown; if there is a second type of triple in the initial statement, querying the property list corresponding to the property value that matches the second tail entity in the knowledge graph based on the entity and relationship list of the pre-updated knowledge graph; selecting at least one second property that matches the second relationship from the property list based on the large language model; and replacing the second relationship in the initial query statement with at least one second property. The second property can be one or more. In the case where there are multiple second properties, their form of existence can be a second property list.

[0048] As an example, in the triple "?s <property / occupation> <instance_final / teacher>", the tail entity "teacher" is known, the property or relationship "occupation" is known, and the head entity "?s" is unknown. This triple is a second type of triple. If subsequently querying the property list corresponding to the property value that matches "teacher" based on the knowledge graph, selecting at least one second property that matches "teacher" from the property list based on the large language model; and replacing the second relationship in the initial query statement with at least one second property.

[0049] In addition, when looking for the target relationship in the knowledge graph, it is also possible to narrow down the scope to obtain alternative options based on the first and last characters, then use the large language model to obtain an approximate option list based on the alternative options, and then perform reverse verification in the knowledge graph.

[0050] See Figure 2 , Figure 2 is the second flow diagram of the query statement generation method for the knowledge graph provided by the present invention. Figure 2 In Figure 2 taking the query language as SparQL as an example, the generation of the query statement is completed. The following combines What is the weight of person A? Sparql is: select distinct * from <testdata>where {<instance / person A><property / weight>?s}

[0051] Whose daughter is person B? The SPARQL is: select distinct * from <testdata>where {?s <property / daughter> <instance / Person B>}

[0052] Who is the daughter of Person C? The SPARQL is: select distinct * from <testdata>where {<instance / person C><property / daughter>?s}

[0053] What is the weight of person C's daughter? The SPARQL is: select distinct * from <testdata>where { <instance / Person C> <property / daughter> ?instance1. ?instance1 <property / weight> ?s}

[0054] Targeted examples are a list of potentially relevant examples selected from the reference list according to the current knowledge graph structure and requirements. For example: Who are the alumni of Person D? The SPARQL is: select distinct * from <testdata>where {<instance / Person D><property / graduation institution>?instance1. ?s<property / graduation institution>?instance1}

[0055] What is the nationality of the son of Person E? The SPARQL is: select distinct * from <testdata>where {<instance / Person E><property / son>?i+92rexnstance1.?instance1<property / nationality>?s}

[0056] Then, the large model generates an initial SPARQL query statement based on this question, such as "select distinct * from <testdata>where {<instance / Role A><property / starring>?instance1.?instance1<property / latest version>?s}”. Then, the system verifies the correctness of this initial SPARQL statement. If an error is found, the process returns to the step of generating the question for correction; if it is correct, the process continues.

[0057] In the basic tool part, the system can use regular expressions to determine that the starting part of SparQL is "<instance / Role A><property / starring>?instance1”. Based on <instance / and <property / , the core instance "Role A” and the relationship of the first hop "starring” are obtained. Then, the following sparql query is used to query the possible relationships that the instance "Role A” may use in the knowledge graph: "select distinct ?p from <testdata>WHERE {<Character A>?p ?o} limit20” gets the first-hop relationship candidate list: "type, Chinese name, other names, foreign name, gender, characteristics, appearances, height".

[0058] For multi-hop queries, the system can narrow the range of the candidate relationship list. Since the result of the first hop, instance1, may be an entity list, and the subsequent relationships may not be of the same kind, a relationship candidate list can be used. Through regular expressions, it can be obtained that the subsequent multi-hop relationship is "latest version". By using the rule that synonyms are mostly consistent at least in part at the beginning and the end, a list of candidate relationships is obtained from the attribute list of the knowledge graph: "<latest version>, <shortest path distance>, <latest chapter>...", and then the large language model can be used to obtain the 10 closest alternatives to "latest version" from the list. The reference question of the large language model is as follows: "Please select the 10 attributes closest to the meaning of 'latest version' from the following attribute list. Only the answer is needed, no explanation is required:..." After verifying that the result is a relationship in the knowledge graph, the result can be used to rewrite the current SparQL to obtain a new SparQL: "select distinct * from <testdata>where {<character A><appeared works>?instance1 .?instance1 ?propertyId1 ?s.filter(?propertyId1 in (<latest version>,<latest chapter>,<latest preview version>,<last updated>,<latest collection>,<latest version>,<latest work>,<latest developments>,<latest progress>,<latest version>,<latest product>))}” The reason for using different methods is to prevent the situation where character A appears in both works A and B, while the relationship of the triples in A is the latest version, and the relationship of the triples in B is the latest version. In this case, querying one relationship will lead to omissions.

[0059] After these steps are completed, the system generates the final SPARQL query statement.

[0060] Finally, use this final SPARQL query statement to query the knowledge graph to get the answer to the question, that is, the latest version of the game with character A as the protagonist.

[0061] The query statement generating device of the knowledge graph provided by the present invention is described below. The query statement generating device of the knowledge graph described below and the query statement generating method of the knowledge graph described above can be referenced to each other.

[0062] Figure 3 A schematic diagram of the structure of a query statement generating device for a knowledge graph provided in an embodiment of the present application, such as Figure 3 As shown, it specifically includes: an acquisition module 301, configured to acquire natural language questions for the knowledge graph; a first generation module 302, configured to generate a large language model question according to the natural language question and a preset question template, the question template is used to guide the large language model to convert the natural language question into a sentence of the target query language; a questioning module 303, configured to ask questions to the large language model according to the large language model question, and obtain an initial sentence of the target query language output by the large language model; a second generation module 304, configured to generate a query sentence of the target query language based on the initial sentence.

[0063] In some optional implementations, the question template includes a question head for asking questions to a large language model and query examples in a target query language, the query examples in the target query language include general examples and targeted examples, and the targeted examples are determined by the following steps: selecting natural language examples similar to natural language questions from a pre-set set of natural language examples based on the large language model; determining the selected natural language examples and examples of the target query language corresponding to the selected natural language examples as targeted examples of the natural language questions.

[0064] In some optional implementations, the second generation module 304 is further configured to: verify whether the grammar of the initial sentence is correct; if the grammar of the initial sentence is incorrect, regenerate the initial sentence based on the large language model; if the grammar of the initial sentence is correct, perform a predefined sentence correction operation on the initial sentence to obtain a query sentence.

[0065] In some optional implementations, the knowledge graph is established based on an encyclopedia website, and the second generation module 304 is further configured to: use regular expressions to determine the core entities in the initial query statement; perform entity linking on the core entities based on the entity linking tool of the pre-established encyclopedia website to obtain the formal name of the core entity; replace the core entity in the initial statement with the formal name.

[0066] In some optional implementations, the second generation module 304 is further configured to: determine whether there is a first type of triple in the initial statement, the first type of triple includes a first head entity, a first relationship and a first tail entity, the first head entity and the first relationship are known, and the first tail entity is unknown; if there is a first type of triple in the initial statement, obtain the attribute associated with the first head entity based on the entity and relationship list of the pre-updated knowledge graph; select a first attribute matching the first relationship from the attributes associated with the first head entity based on the large language model; replace the first relationship in the initial query statement with the first attribute; replace the first tail entity in the initial query statement with the attribute value of the first attribute.

[0067] In some optional implementations, the second generation module 304 is further configured to: determine whether the first tail entity is a jump node for transition in a multi-hop problem; if the first tail entity is a jump node for transition in a multi-hop problem, determine whether the attribute value corresponding to the first attribute is multiple entities; if the attribute value corresponding to the first attribute is multiple entities, obtain the subsequent relationship of the jump node; based on similarity, obtain a list of alternative relationships that match the subsequent relationship from the entity and relationship list of the pre-updated knowledge graph; determine multiple relationship items that are close to the subsequent relationship from the alternative relationship list based on the large language model; and replace the subsequent relationship in the initial query statement with multiple relationship items.

[0068] In some optional implementations, the second generation module 304 is further configured to: determine whether there is a second type of triple in the initial statement, the second type of triple includes a second head entity, a second relationship and a second tail entity, the second tail entity and the second relationship are known, and the second head entity is unknown; if there is a second type of triple in the initial statement, query the attribute list corresponding to the attribute value matching the second tail entity in the knowledge graph based on the pre-updated entity and relationship list of the knowledge graph; select at least one second attribute matching the second relationship from the attribute list based on the large language model; replace the second relationship in the initial query statement with at least one second attribute.

[0069] The query statement generation device for the knowledge graph provided by the present invention generates a large language model question according to a natural language question and a pre-set question template, and then asks the large language model a question according to the large language model question to obtain an initial statement of the target query language output by the large language model, and finally generates a query statement of the target query language based on the initial statement, thereby improving the generation efficiency of the knowledge graph query statement.

[0070] Figure 4 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 4 As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute the query statement generation method of the knowledge graph, the method comprising: obtaining a natural language question for the knowledge graph; generating a large language model question according to the natural language question and a preset question template, the question template is used to guide the large language model to convert the natural language question into a sentence of the target query language; asking the large language model questions according to the large language model question to obtain an initial sentence of the target query language output by the large language model; generating a query sentence of the target query language based on the initial sentence.

[0071] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of software function modules and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0072] On the other hand, the present invention also provides a computer program product, which includes a computer program, and the computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the query statement generation method for the knowledge graph provided by the above methods, and the method includes: obtaining a natural language question for the knowledge graph; generating a large language model question based on the natural language question and a pre-set question template, and the question template is used to guide the large language model to convert the natural language question into a statement in the target query language; asking the large language model questions according to the large language model questions to obtain an initial statement of the target query language output by the large language model; and generating a query statement in the target query language based on the initial statement.

[0073] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a method for generating query statements for a knowledge graph provided by the above methods, the method comprising: obtaining a natural language question for the knowledge graph; generating a large language model question based on the natural language question and a pre-set question template, the question template being used to guide the large language model to convert the natural language question into a statement in a target query language; asking the large language model questions based on the large language model questions to obtain an initial statement in the target query language output by the large language model; and generating a query statement in the target query language based on the initial statement.

[0074] The device embodiments described above are merely illustrative, wherein the modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, i.e., they may be located in one place, or they may be distributed on multiple network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Those of ordinary skill in the art may understand and implement it without creative effort.

[0075] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0076] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.< / testdata> < / testdata> < / testdata> < / testdata> < / testdata> < / testdata> < / testdata> < / testdata> < / testdata>

Claims

1. A method for generating a query statement of a knowledge graph, characterized in that: include: Obtain natural language questions for the knowledge graph; Generate a large language model question sentence according to the natural language question sentence and a preset question sentence template, wherein the question sentence template is used to guide the large language model to convert the natural language question sentence into a sentence in a target query language; Asking the large language model a question according to the large language model question sentence, and obtaining an initial sentence of the target query language output by the large language model; A query statement in the target query language is generated based on the initial statement.

2. The method for generating a query statement of a knowledge graph according to claim 1, characterized in that: The question template includes a question header for asking a question to a large language model and a query example in the target query language. The query example in the target query language includes a general example and a targeted example. The targeted example is determined by the following steps: Selecting a natural language example sentence similar to the natural language question sentence from a preset natural language example sentence set based on a large language model; The selected natural language example sentence and the example sentence of the target query language corresponding to the selected natural language example sentence are determined as targeted example sentences of the natural language question sentence.

3. The method for generating a query statement of a knowledge graph according to claim 1, characterized in that: The step of generating a query sentence corresponding to the natural language question sentence based on the initial sentence includes: Verifying the correctness of the initial statement; If the initial sentence is verified to be wrong, re-generating the initial sentence based on the large language model; If the initial statement is verified to be correct, a predefined statement correction operation is performed on the initial statement to obtain the query statement.

4. The method for generating a query statement of a knowledge graph according to claim 3, characterized in that: The knowledge graph is established based on an encyclopedia website, and the predefined statement correction operation is performed on the initial statement to obtain the query statement, including: Determine the core entity in the initial query statement using a regular expression; Perform entity linking on the core entity based on the pre-established entity linking tool of the encyclopedia website to obtain the formal name of the core entity; The core entity in the initial sentence is replaced with the formal name.

5. The method for generating a query statement of a knowledge graph according to claim 4, characterized in that: After replacing the core entity in the initial statement with the formal name, performing a predefined statement correction operation on the initial statement to obtain the query statement further includes: Determine whether there is a first type of triple in the initial sentence, where the first type of triple includes a first head entity, a first relationship, and a first tail entity, where the first head entity and the first relationship are known, and the first tail entity is unknown; If there is a triple of the first type in the initial sentence, acquiring attributes associated with the first head entity based on a pre-updated list of entities and relationships of the knowledge graph; Selecting a first attribute matching the first relationship from attributes associated with the first head entity based on a large language model; replacing the first relation in the initial query statement with the first attribute; The first tail entity in the initial query statement is replaced with the attribute value of the first attribute.

6. The method for generating a query statement of a knowledge graph according to claim 5, characterized in that: The step of replacing the first tail entity in the initial query statement with an attribute value corresponding to the first attribute includes: Determining whether the first tail entity is a jump node for transition in a multi-hop problem; If the first tail entity is a jump node for transition in a multi-hop problem, determining whether the attribute value corresponding to the first attribute is multiple entities; If the attribute value corresponding to the first attribute is multiple entities, obtaining the subsequent relationship of the jump node; Based on the similarity, a list of candidate relations matching the subsequent relation is obtained from a list of entities and relations of the knowledge graph that is updated in advance; Determining, from the candidate relationship list, a plurality of relationship items close to the subsequent relationship based on a large language model; The subsequent relationship in the initial query statement is replaced by the multiple relationship items.

7. The method for generating a query statement of a knowledge graph according to claim 4, characterized in that: After replacing the core entity in the initial statement with the formal name, performing a predefined statement correction operation on the initial statement to obtain the query statement further includes: Determine whether there is a second type of triple in the initial sentence, where the second type of triple includes a second head entity, a second relationship, and a second tail entity, where the second tail entity and the second relationship are known, and the second head entity is unknown; If there is a triple of the second type in the initial sentence, querying the attribute list corresponding to the attribute value matching the second tail entity in the knowledge graph based on the pre-updated entity and relationship list of the knowledge graph; Selecting at least one second attribute matching the second relationship from the attribute list based on a large language model; The second relation in the initial query statement is replaced by the at least one second attribute.

8. A query statement generation device for a knowledge graph, characterized in that: include: An acquisition module, configured to acquire a natural language question for the knowledge graph; A first generating module is configured to generate a large language model question sentence according to the natural language question sentence and a preset question sentence template, wherein the question sentence template is used to guide the large language model to convert the natural language question sentence into a sentence in a target query language; a questioning module configured to ask a question to the large language model according to the large language model question sentence, and obtain an initial sentence of the target query language output by the large language model; The second generating module is configured to generate a query statement in the target query language based on the initial statement.

9. An electronic device, characterized in that: It includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method for generating query statements for the knowledge graph as described in any one of claims 1 to 7 is implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method for generating query statements for the knowledge graph as described in any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • A knowledge graph embedding method based on a diverse graph attention mechanism

    CN109902183A

  • Knowledge graph intelligent question-answering method based on relationship prediction

    CN111782769A

  • Knowledge graph multi-hop reasoning method and device based on vector logic

    CN115269799A

  • Generating relationship links for making questions and answers

    CN115617963A

  • Intelligent customer service voice processing system and method based on knowledge graph

    CN115688879A

Cited By

  • Natural language and Cypher query language conversion method, system and terminal

    CN120541195A

  • Natural language and cypher query language conversion method, system and terminal

    CN120541195B

  • Regenerated material recovery method, device and equipment based on multi-modal large language model

    CN120804344A