Knowledge graph query statement generation method and device, and electronic equipment
By using a large language model and question templates to generate query statements in a knowledge graph question-answering system, the problem of limited applicability of generation methods in existing technologies is solved, and efficient and accurate query statement generation is achieved.
Patent Information
- Application Number
- CN202411539171.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-31
- Publication Date
- 2025-10-21
- Estimated Expiration
- 2044-10-31
AI Technical Summary
The query statement generation method of the knowledge graph question answering system in the existing technology has limited applicability and requires a complex training process, making it difficult to effectively parse users' natural language questions.
By obtaining natural language questions, using pre-set question templates to generate large language model questions, and asking questions to the large language model, an initial sentence is generated. Then, grammar correction and entity replacement are performed based on the initial sentence to generate a query sentence in the target query language.
It improves the generation efficiency of knowledge graph query statements, simplifies the training process, and improves the accuracy and efficiency of query statement generation.
Smart Images

Figure CN119557328B_ABST
Abstract
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 large amounts of triples through a graphical data structure composed of entities and relationships. Knowledge graphs provide powerful data support and knowledge reasoning capabilities in numerous fields, such as search engines, recommendation systems, and natural language processing.
[0003] As a key method of human-computer interaction, knowledge graph question answering systems have garnered widespread attention in their research and application. These systems parse users' natural language questions and generate corresponding query languages, such as SPARQL or RDF, 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. Rule-based template methods extract key information from the question and find corresponding templates based on the question format to generate query statements in the target query language. However, due to the diversity of human questioning methods, it is difficult to cover all possible questioning scenarios, resulting in limited applicability. Deep learning-based methods rely on large amounts of knowledge graph data and train 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 based on a knowledge graph, which are used to solve the problems in the prior art of limited applicability of query statement generation methods and the need to perform complex training processes. It can effectively parse users' 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 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; asking the large language model a question based on 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 statements 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 query examples in the target query language. The query examples in the target query language include general examples and targeted examples. The targeted examples are determined by the following steps: selecting natural language examples similar to the natural language question from a pre-set set of natural language examples based on the large language model; and determining the selected natural language examples and the examples in the target query language corresponding to the selected natural language examples as targeted examples of the natural language question.
[0008] According to a method for generating query statements for a knowledge graph provided by the present invention, generating a query statement corresponding to the natural language question sentence based on the initial statement includes: 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 the 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 the 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 being known, and the first tail entity being 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 query statement generation method 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 the subsequent relationship of the jump node; obtaining an alternative relationship list matching the subsequent relationship from a pre-updated entity and relationship list 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: judging 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, 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 natural language questions 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 query statement generating method for a knowledge graph as described above 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 query statement generation method for a knowledge graph as described in any one of the above.
[0016] The present invention also provides a computer program product, comprising 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 based on natural language questions and pre-set question templates, and then ask questions to the large language model based on the large language model questions to obtain the initial sentence of the target query language output by the large language model. Finally, the query sentence of the target query language is generated based on the initial sentence, thereby improving the generation efficiency of knowledge graph query sentences. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. 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 any creative work.
[0019] Figure 1 This 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 diagram of the query statement generation device for 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] To make the objectives, technical solutions, and advantages of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the embodiments described are only some embodiments 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 making creative efforts shall fall within the scope of protection of the present invention.
[0024] The following combination 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 query statements for the knowledge graph provided by the embodiment of the present invention, such as Figure 1 As shown, the method includes:
[0026] Step 101: Obtain a natural language question for the knowledge graph.
[0027] 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.
[0028] Step 102: Generate a large language model question based on the natural language question and the preset question template.
[0029] In this embodiment, the question template is used to guide a large language model to convert natural language questions into statements in the target query language. Large language models (LLMs) are natural language processing models with a large number of parameters trained through deep learning and machine learning techniques. Large language models can include models such as the BERT (Bidirectional Encoder Representations from Transformers) and the GPT (Generative Pre-trained Transformer) series.
[0030] Question templates can cover a variety of query scenarios and contain necessary placeholders for natural language questions or information indicating the location of natural language questions so that they can be combined with natural language questions. For example, a question template can be a question header and a placeholder for the natural language question. Optionally, the question template can also include some auxiliary information, such as requirements or examples, to help improve the accuracy of the generated statement. Taking the query language 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 a preset encyclopedia website. The answer only contains SparQL statements, English is 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.
[0031] Step 103: Ask the large language model a question based on the large language model question sentence, and obtain an initial sentence in the target query language output by the large language model.
[0032] In this embodiment, the large language model can output an initial sentence of the target query language based on its own processing logic.
[0033] Step 104: Generate a query statement in the target query language based on the initial statement.
[0034] 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 validation, entity replacement, and relationship replacement. Modifications based on other pre-set rules may also be performed, which may be specifically configured based on the actual situation of generating the initial statement.
[0035] The knowledge graph query statement generation method provided by the present invention generates a large language model question based on natural language questions and pre-set question templates, and then asks questions to the large language model based on the large language model questions to obtain the initial sentence of the target query language output by the large language model. Finally, the query sentence of the target query language is generated based on the initial sentence. In the knowledge graph question answering, no additional model is trained. Only a general large model is used in combination with code and basic tools, which improves the generation efficiency of knowledge graph query sentences.
[0036] In some optional implementations, the question template includes a question header for asking the large language model and a query example in the target query language. The query example in the target query language includes general examples and targeted examples. The targeted examples are determined by the following steps: selecting a natural language example similar to the natural language question from a pre-set set of natural language examples based on the large language model; and determining the selected natural language example and the target query language example corresponding to the selected natural language example as the targeted example for the natural language question. In this implementation, the query example in the target query language helps the large language model more accurately generate the initial question, and the targeted example enables the large language model to more accurately understand the natural language question and generate the initial sentence corresponding to the natural language question.
[0037] For example, a pre-set general question might begin with "Based on the following question, write a SPARQL query to query the Virtuoso testData database. The database stores entries and attributes from Baidu. The answer only contains SPARQL statements, and English is in lowercase. No explanation is required." The obtained natural language question can then be added to the question header, and finally a reference example sentence can be added. It should be noted that the positions of the above parts can be set according to actual needs.
[0038] In some optional implementations, generating a query statement corresponding to a natural language question based on an initial statement includes: 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 a query statement.
[0039] In this implementation, the correctness of the initial statement can be verified using query language validation tools or pre-established rules. For example, if the query language is SparQL, the correctness of the initial SparQL statement can be verified using SPARQLWrapper, a Python SparQL toolkit. This implementation verifies the initial statement output by the large language model, ensuring the accuracy of the generated initial statement results and format.
[0040] In some optional implementations, a knowledge graph is established based on an encyclopedia website, and a predefined statement correction operation is performed on an initial statement to obtain a query statement, including: using a regular expression to determine the core entity in the initial query statement; performing entity linking on the core entity based on an entity linking tool of a pre-established encyclopedia website to obtain the official name of the core entity; and replacing the core entity in the initial statement with the official name.
[0041] Specifically, the search and analysis capabilities of tools like ES (Elasticsearch) can be leveraged to resolve ambiguous entity recognition issues within the knowledge graph. Furthermore, the knowledge graph can be queried in advance using query languages like SparQL to obtain the latest entity and relationship lists. These lists can then be stored in a dict structure (a key-value dictionary) based on the first and last characters and the corresponding entities. This cached entity and relationship list can then be updated at program startup to ensure normal use and high accuracy even when knowledge graph triples undergo significant changes.
[0042] 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 entity linking tools 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 by similarity alone. Therefore, the feature that searching for abbreviations on encyclopedia websites will jump to the entry with 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 due to the colloquialization of entities and aliases to a certain extent.
[0043] In some alternative implementations, after replacing the core entity in the initial statement with the formal name, performing the 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 the 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.
[0044] As an example, in the triple "<instance / Name A><property / graduating institution>?instance1", the head entity Name A is known, the property or relationship graduating institution 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 / graduating institution>" is School A, then the tail entity "?instance1" can be replaced with "<instance / School A>".
[0045] In some alternative implementations, 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, then 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.
[0046] 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.
[0047] In this implementation method, 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.
[0048] In some optional implementation methods, after replacing the core entity in the initial statement with its formal name, performing a predefined statement correction operation on the initial statement to obtain a query statement further includes: determining whether there is a triple of the second type in the initial statement. The triple of the second type 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 triple of the second type in the initial statement, query 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; select at least one second property that matches the second relationship from the property list based on a large language model; replace 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.
[0049] 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 triple of the second type. 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 a large language model; replacing the second relationship in the initial query statement with at least one second property.
[0050] In addition, when looking for a 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 a large language model to obtain an approximate option list based on the alternative options, and then perform a reverse verification method in the knowledge graph. [[ID=X]] [[ID=Y]]
[0051] See Figure 2 , Figure 2 which is the second flowchart of the method for generating a query statement of the knowledge graph provided by the present invention. [[ID=Z]] 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 Figure 2 and details this process with examples. First, the user inputs a natural language question. For example, the question sentence is "What is the latest version of the game with character A as the protagonist?", and then the question sentence can be filled into the question sentence template. The question sentence template includes a question sentence head and a reference statement. The following is an example of a reference sparql statement:
[0052] What is the weight of person A? Sparql is: select distinct * from <testdata>where {<instance / Person A><property / weight>?s}
[0053] Who is Person B's daughter? The SPARQL is: select distinct * from <testdata>where {?s<property / daughter><instance / Person B>}
[0054] Who is the daughter of Person C? The SPARQL is: select distinct * from <testdata>where {<instance / Person C><property / daughter>?s}
[0055] 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}
[0056] 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:
[0057] 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}
[0058] 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}
[0059] 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.
[0060] 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, and then the following sparql 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".
[0061] For multi-hop queries, the system can narrow the scope of the list of alternative relationships. 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 "the latest version". By taking advantage of the fact that synonyms often have at least a part of the same beginning and end, a list of alternative relationships is obtained from the attribute list of the knowledge graph: "<latest version>, <shortest path distance>, <latest chapter>...", and then a large language model can be used to obtain the 10 options closest to "latest version" from the list. The reference question of the large language model is as follows: "Please select the 10 attributes that are 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>,<Latest Update>,<Latest Collection>,<Latest Edition>,<Latest Work>,<Latest News>,<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 triple relationship in A is the latest version and the triple relationship in B is the latest version. In this case, querying one relationship will result in omissions.
[0062] After these steps are completed, the system generates the final SPARQL query statement.
[0063] 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.
[0064] The query statement generation device for the knowledge graph provided by the present invention is described below. The query statement generation device for the knowledge graph described below and the query statement generation method for the knowledge graph described above can be referenced to each other.
[0065] Figure 3 A schematic diagram of the structure of the query statement generation device for the knowledge graph provided in the 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, wherein 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; a questioning module 303, configured to ask questions to the large language model according to the large language model question, and obtain an initial statement in the target query language output by the large language model; a second generation module 304, configured to generate a query statement in the target query language based on the initial statement.
[0066] In some optional implementations, the question template includes a question head for asking questions to the large language model and query examples in the target query language. The query examples in the target query language include general examples and targeted examples. The targeted examples are determined by the following steps: based on the large language model, natural language examples similar to the natural language question are selected from a pre-set set of natural language examples; the selected natural language examples and the examples in the target query language corresponding to the selected natural language examples are determined as targeted examples of the natural language question.
[0067] In some optional implementations, the second generation module 304 is further configured to: verify whether the grammar of the initial statement is correct; if the grammar of the initial statement is verified to be incorrect, regenerate the initial statement based on the large language model; if the grammar of the initial statement is verified to be correct, perform a predefined statement correction operation on the initial statement to obtain a query statement.
[0068] 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; and replace the core entity in the initial statement with the formal name.
[0069] 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 the first attribute that matches 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.
[0070] 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; obtain an alternative relationship list that matches the subsequent relationship from the entity and relationship list of the pre-updated knowledge graph based on similarity; determine multiple relationship items 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.
[0071] 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.
[0072] The knowledge graph query statement generation device provided by the present invention generates a large language model question based on natural language questions and pre-set question templates, and then asks questions to the large language model based on the large language model questions to obtain the initial sentence of the target query language output by the large language model. Finally, the query sentence of the target query language is generated based on the initial sentence, thereby improving the generation efficiency of the knowledge graph query sentence.
[0073] Figure 4 An example of a physical structure diagram of an electronic device is shown below. Figure 4 As shown, the electronic device may include: a processor 410, a communications interface 420, a memory 430, and a communications bus 440, wherein the processor 410, the communications interface 420, and the memory 430 communicate with each other via the communications bus 440. The processor 410 may call logic instructions in the memory 430 to execute a method for generating query statements for a knowledge graph, 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 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 based on 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.
[0074] Furthermore, the logic instructions in the aforementioned memory 430 can be implemented as software functional modules and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the portion that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product, stored in a storage medium, includes instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, a mobile hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0075] On the other hand, the present invention also provides a computer program product, which includes a computer program, which 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 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.
[0076] 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 the 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 the 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.
[0077] The device embodiments described above are merely illustrative, wherein the modules described as separate components may or may not be physically separate, and the components shown as modules may or may not be physical modules, i.e., they may be located in one place or distributed across multiple network modules. Some or all of the modules may be selected based on actual needs to achieve the objectives of the present embodiment. Those skilled in the art can understand and implement the present invention without inventive effort.
[0078] Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented using software plus a necessary general-purpose hardware platform, or of course, hardware. Based on this understanding, the essence of the above technical solution, or the portion that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, or an optical disk, and includes a number of instructions for causing a computer device (such as a personal computer, server, or network device) to execute the methods described in each embodiment or certain portions of the embodiments.
[0079] 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 various embodiments of the present invention.< / testdata> < / testdata> < / testdata> < / testdata> < / testdata> < / testdata> < / testdata> < / testdata> < / testdata>
Claims
1. A method for generating query statements based on a knowledge graph, characterized in that: include: Obtain natural language questions for the knowledge graph; Generate a large language model question sentence based on 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 the 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; Generating a query statement in the target query language based on the initial statement includes: Verifying the correctness of the initial statement; 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, including: Determining the core entity in the initial query statement using a regular expression; Performing entity linking on the core entity based on an entity linking tool of a pre-established encyclopedia website to obtain a formal name of the core entity; replacing the core entity in the initial sentence with the formal name; Determining whether a first type of triple exists 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 the initial sentence contains a triple of the first type, then obtaining attributes associated with the first head entity based on a pre-updated list of entities and relationships in the knowledge graph; selecting, based on a large language model, a first attribute matching the first relationship from attributes associated with the first head entity; Replacing the first relation in the initial query statement with the first attribute; Replacing the first tail entity in the initial query statement with the attribute value of 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; Obtaining a list of candidate relations that match the subsequent relation from a pre-updated list of entities and relations in the knowledge graph based on similarity; determining, from the candidate relationship list based on a large language model, a plurality of relationship items close to the subsequent relationship; The subsequent relationship in the initial query statement is replaced by the multiple relationship items.
2. The method for generating query statements for 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 the targeted example sentence of the natural language question sentence.
3. The method for generating query statements for a knowledge graph according to claim 1, wherein: The method further comprises: If the initial sentence is verified to be wrong, the initial sentence is regenerated based on the large language model.
4. The method for generating query statements for a knowledge graph according to claim 3, wherein: The knowledge graph is established based on an encyclopedia website.
5. A knowledge graph query statement generation device, characterized in that: include: An acquisition module, configured to acquire natural language questions for the knowledge graph; A first generating module is configured to generate 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 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, including: Verifying the correctness of the initial statement; 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, including: Determining the core entity in the initial query statement using a regular expression; Performing entity linking on the core entity based on an entity linking tool of a pre-established encyclopedia website to obtain a formal name of the core entity; replacing the core entity in the initial sentence with the formal name; Determining whether a first type of triple exists 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 the initial sentence contains a triple of the first type, then obtaining attributes associated with the first head entity based on a pre-updated list of entities and relationships in the knowledge graph; selecting, based on a large language model, a first attribute matching the first relationship from attributes associated with the first head entity; Replacing the first relation in the initial query statement with the first attribute; Replacing the first tail entity in the initial query statement with the attribute value of 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; Obtaining a list of candidate relations that match the subsequent relation from a pre-updated list of entities and relations in the knowledge graph based on similarity; determining, from the candidate relationship list based on a large language model, a plurality of relationship items close to the subsequent relationship; The subsequent relationship in the initial query statement is replaced by the multiple relationship items.
6. 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 according to any one of claims 1 to 4 is implemented.
7. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, it implements the query statement generation method of the knowledge graph as described in any one of claims 1 to 4.
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