Graph query statement generation method, data query method and related devices

By identifying entity relationships and obtaining local graph structure data during the graph query statement generation process, combined with Gremlin syntax and field validation, the problem of inaccurate generation of large language models is solved, and higher-quality graph query statement generation is achieved.

CN120653804APending Publication Date: 2025-09-16GUANGZHOU SHIYUAN ELECTRONICS CO LTD +1

Patent Information

Application Number
CN202410301445.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-03-15
Publication Date
2025-09-16

AI Technical Summary

Technical Problem

When large language models generate graph query statements, due to input length limitations and the complexity of graph structure data, entity relationship mapping is inaccurate, and the output graph query statements are not accurate enough.

Method used

By identifying entity relationships in natural language questions, retrieving target vertices in the graph, and obtaining local graph structure data within a specified degree to generate prompt statements, the large language model is input to generate graph query statements, and the statements are optimized through Gremlin syntax verification and field verification.

Benefits of technology

It improves the accuracy of graph query statements, solves the token length limitation problem of large language models, reduces misleading fields, enhances the correct mapping of entity relationships, and optimizes the generation process of complex queries.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120653804A_ABST
    Figure CN120653804A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a graph query statement generation method, a data query method and related devices, and belongs to the technical field of natural language processing. The graph query statement generation method comprises the following steps: identifying an entity relationship in a natural language question, and retrieving a target vertex matched with the entity relationship in a graph; wherein the graph is pre-generated based on the graph structure data; acquiring local graph structure data within a specified degree from the target vertex, and generating a first prompt statement based on the local graph structure data; and inputting the first prompt statement into the large language model to generate a first graph query statement. According to the method, the accuracy of converting the natural language into the graph query language by a large model can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of natural language processing technology, and in particular to a graph query statement generation method, a data query method, and related devices. Background Art

[0002] With the continuous development of Internet technology, the amount of data generated by various industries is also expanding, and the relationships between data are becoming more complex and changeable. In order to effectively access, organize and manage large amounts of data resources, database technology has emerged.

[0003] A graph database is a database that stores and queries data based on a graph structure. When executing various query tasks, users' natural language questions can be converted into graph query statements and executed in the database to obtain query results. Natural language questions and prompts can be input into a large language model, which then generates graph query statements.

[0004] Large language models typically have input length limitations. When the prompt input is too long, it becomes difficult to correctly map the entity relationships mentioned in the natural language question to the prompt, resulting in inaccurate graph query output from the large language model. Summary of the Invention

[0005] The present application provides a graph query statement generation method, a data query method, and related devices, which can solve the technical problem of inaccurate graph query statements output by large language models. To achieve the above objectives, the present application provides the following technical solutions:

[0006] In a first aspect, an embodiment of the present application provides a method for generating a graph query statement, the method comprising:

[0007] Identifying entity relationships in natural language questions and retrieving target vertices matching the entity relationships in a graph; wherein the graph is pre-generated based on graph structure data;

[0008] Acquire local graph structure data within a specified degree from the target vertex, and generate a first prompt statement based on the local graph structure data;

[0009] The first prompt sentence is input into a large language model to generate a first graph query sentence.

[0010] In a second aspect, an embodiment of the present application provides a device for generating a graph query statement, the device comprising:

[0011] An entity relationship recognition module is used to identify entity relationships in natural language questions and retrieve target vertices matching the entity relationships in the graph; wherein the graph is pre-generated based on graph structure data;

[0012] a prompt statement generating module, configured to obtain local graph structure data within a specified degree from the target vertex, and generate a first prompt statement based on the local graph structure data;

[0013] A graph query statement generating module is configured to input the first prompt statement into a large language model to generate a first graph query statement.

[0014] In a third aspect, an embodiment of the present application provides a data query method, the method comprising:

[0015] Obtain the graph query statement described in the first aspect;

[0016] Execute the graph query statement in the graph database and obtain the data query results.

[0017] In a fourth aspect, an embodiment of the present application provides a data query device, the device comprising:

[0018] A graph query statement acquisition module, configured to acquire the graph query statement described in the first aspect;

[0019] The graph query statement execution module is used to execute the graph query statement in the graph database and obtain data query results.

[0020] In a fifth aspect, an embodiment of the present application provides a computer-readable storage medium, which stores a plurality of instructions, and the instructions are suitable for being loaded by a processor and executing the method described in the first aspect.

[0021] In an embodiment of the present application, the graph structure data can be stored as a graph in advance. When a user initiates a natural language question, the entity relationship existing in the natural language question initiated by the user can be identified, and the vertex that best matches the identified entity relationship can be retrieved in the graph. Afterwards, all graph structure data within a specified degree (for example, 1 degree) from the vertex is obtained, that is, part of the graph structure data is obtained as a prompt statement. In this way, compared to using the entire graph structure data as a prompt statement, the length and complexity of the prompt statement can be significantly reduced, which not only overcomes the input length limit of the large model, but also reduces the fields that may cause misunderstandings, and provides the large model with higher quality input data, thereby improving the accuracy of converting natural language questions into graph query statements.

[0022] In addition, the embodiment of the present application can also perform secondary optimization on the graph query statements output by the large language model. On the one hand, after the graph query statement is generated, Gremlin syntax verification and field verification are added. By checking and correcting the syntax and fields of the generated Gremlin statement, it is possible to optimize the inference errors and hallucination problems and improve the accuracy of the graph query statement generated by the large language model. On the other hand, after the graph database executes the graph query statement, data is extracted from the failed queries that were repaired by manual intervention to build a knowledge base for complex queries, which is used to generate optimized prompt statements for complex queries, and the optimized prompt statements are input into the large language model to generate optimized graph query statements. In this way, the accuracy of the generation of complex graph query statements can be improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0024] Figure 1 A flowchart of a method for generating a graph query language provided in an embodiment of the present application;

[0025] Figure 2 A schematic diagram of a specific process of a graph query language generation method provided in an embodiment of the present application;

[0026] Figure 3 A schematic diagram of a graph structure provided in an embodiment of the present application;

[0027] Figure 4 A flowchart of another method for generating a graph query language provided in an embodiment of the present application;

[0028] Figure 5 This is a schematic diagram of the structure of a graph query language generation device provided in an embodiment of the present application;

[0029] Figure 6 It is a structural diagram of a data query device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0030] With the continuous development of Internet technology, the scale of data in various industries is also expanding, and the relationships between data are becoming more complex and changeable. In order to effectively access, organize and manage large amounts of data resources, database technology came into being.

[0031] A graph database is a database that stores and queries data based on a graph structure (Schema). The key concepts of a graph database are nodes and edges, where nodes represent entities, such as names of people, places, and organizations, and edges represent relationships between entities. By connecting nodes (or vertices) together through edges, a natural entity relationship network can be constructed, allowing for fast graph query operations. Graph databases usually perform query tasks based on a graph query language. When users use natural language to express query and operation requirements for graph data, the natural language needs to be converted into a graph query language supported by the graph database. Since the graph query language has more syntax and a certain learning cost, automatically converting natural language into a graph query language can effectively lower the threshold for using a graph database.

[0032] A large language model (LLM) is a model based on machine learning and natural language processing technologies. It is trained on large amounts of text data to learn the ability to understand and generate human language. It can understand and generate natural language text and possesses certain logical thinking and reasoning abilities. Therefore, LLMs are widely used in tasks such as text summarization, translation, and sentiment analysis.

[0033] In one embodiment of the present application, see Figure 1 , users can input natural language questions through user-end devices, such as personal computers, mobile phones, tablets and other devices. The user-end device can send the questions input by the user to the server for processing. After the server obtains the natural language questions from the user device, it can generate corresponding graph query statements through the large language model, input the graph query statements into the graph database for execution, and return the query results to the user device. Among them, the user's natural language questions can be combined with customized prompt statements (Prompt), such as prompt statements generated based on the entire graph structure, and input them into the large language model together to guide the large language model to generate graph query statements that can be executed in the graph database.

[0034] It is worth mentioning that running a large language model usually requires a large amount of computing and storage resources. Therefore, the graph query statement generation method and data query method for the user's natural language questions can be executed by a server with rich computing and storage resources. With the continuous optimization of large language models and the continuous improvement of the performance of user-end devices, more and more user-end devices are able to support the operation of large language models. Thus, in some embodiments, the user-end device may not send the natural language questions to the server, but may generate graph query statements and data queries locally on the user-end device. Therefore, the execution subject of the graph query statement generation method and data query method provided in each embodiment of the present application may be a server or a user-end device, and the present application does not impose any restrictions on this.

[0035] A token is the smallest processing unit of a large language model and can usually be counted based on characters, words, or Chinese characters. To improve the efficiency and effectiveness of model training, large language models usually have a token length limit, that is, a limit on the length of the input prompt statement. Using a graph structure as a prompt statement will increase the number of input tokens. When the input prompt statement is too long, in order to meet the token length limit, the excess part can be deleted or compressed, or the prompt statement can be input in segments, etc. In this way, it is difficult to correctly map the entity relationship mentioned in the natural language question to the input prompt statement, and the model may be confused or misunderstood about the information, which in turn leads to inaccurate graph query statements output by the large language model. In addition, the excessive number of schema fields will also amplify the problem of inaccurate mapping between entities and graph structures in natural language questions.

[0036] It is worth mentioning that Figure 1 The illustrated implementation method was obtained by the inventors of the present application during the process of developing a graph query statement generation method. It is not a method known to the public at home and abroad before the proposal of the present application.

[0037] Based on this, an embodiment of the present application provides a method for generating a graph query statement, which stores graph structure data as a graph, determines the core information in the natural language question based on the graph, and uses the core information to generate an optimized prompt statement for input into the large language model. In this way, the length of the prompt statement is shortened, and the problem of the token length limit of the large language model can be solved. At the same time, the entire graph structure is no longer input into the large language model as a prompt statement, and the fields in the complete graph structure data that may mislead the large language model are also reduced, so that the entity relationship in the natural language question can be correctly mapped to the optimized prompt statement. Furthermore, after the optimized prompt statement is input into the large language model, a more accurate graph query statement can be output.

[0038] It is worth mentioning that the graph query language used in the graph query statement of this application can be any graph query language supported by the graph database, such as Cypher language, Gremlin language, etc., and this application does not impose any restrictions on this. Among them, Gremlin language is a path-oriented graph database query language, which can concisely represent complex graph traversals and multi-step operations. It is a very popular query language in the field of graph databases. However, since the Gremlin language has more syntax and a certain learning cost, automatically converting natural language into Gremlin can effectively lower the threshold for using graph databases. The embodiment of this application will be explained in detail using the Gremlin query language as an example.

[0039] In order to make the objectives, technical solutions and advantages of this application clearer, the embodiments of this application will be further described in detail below with reference to the accompanying drawings.

[0040] It should be clear that the embodiments described are only part of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.

[0041] When the following description refers to the accompanying drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present application. Instead, they are merely examples of devices and methods consistent with certain aspects of the present application, as detailed in the appended claims.

[0042] In the description of this application, it should be understood that the terms "first", "second", "third", etc. are only used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence, nor can they be understood as indicating or implying relative importance. For those of ordinary skill in the art, the specific meanings of the above terms in this application can be understood according to the specific circumstances. In addition, in the description of this application, unless otherwise specified, "multiple" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships can exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the previous and subsequent associated objects are in an "or" relationship.

[0043] In one embodiment, see Figure 2, the graph query language generation method provided in the embodiment of the present application no longer inputs the entire graph structure data as a prompt to the large language model, but inputs the local graph structure data that is most relevant to the user's natural language question to the large language model. In this way, the length of the prompt is shortened, and the problem of the token length limit of the large language model can be solved. At the same time, it also reduces the fields that may be misleading, and retains the important information corresponding to the natural language question in the prompt. Furthermore, after the above-mentioned local graph structure data is input as a prompt to the large language model, a more accurate graph query statement can be output. Specifically, it may include the following steps:

[0044] S202, identifying entity relationships in natural language questions, and retrieving target vertices matching the entity relationships in the graph.

[0045] In practice, the graph can be pre-generated based on graph structure data. Natural language questions can be natural language text input by the user through a user terminal device, natural language text converted from user input, or natural language text recommended by the server based on the user's historical input or preferences, etc. This application does not impose any restrictions on this.

[0046] In one embodiment, the graph structure can be converted into a graph format for storage in advance. For example, a graph structure data can be stored as Figure 3 The graph shown in the figure. In this way, the relationship and attributes between entities can be intuitively expressed through the graph, and complex semantic relationships can be handled. Among them, the graph structure data includes:

[0047] Node properties:[name:student,properties:['birthday'];name:school,properties:['city'];name:class,properties:['grade'];name:topic,properties:['name'];name:knowledge point,properties:['name'];name:textbook version,properties:[];name:chapter,properties:[];]

[0048] Edge properties:[name:do the question,name:belongs to]

[0049] Relationships: [Students --> Questions --> Questions, Questions --> Knowledge Points, Classes --> Knowledge Points

[0050] School, Student-->Belongs to-->Class, Question-->Belongs to-->Textbook Version, Knowledge Point-->Belongs to-->Chapter, Chapter-->Belongs to-->Textbook Version]

[0051] Among them, Node properties represent the vertex properties in the graph structure, that is, entity properties; Edge properties represent the properties of the edges in the graph structure, that is, relationship properties; Relationships represent the relationships between entities.

[0052] It is worth mentioning that graph data can be a large data set, including a large number of vertices and edges. The specific data content and data volume vary depending on the specific business scenario and business needs, and this application does not impose any restrictions on this.

[0053] During implementation, after receiving a natural language question from a user, the server can use a large language model to identify entity relationships (referred to as entity relationships to be queried) from the natural language question. For example, the entity recognition function of the large language model is used to identify entities in the natural language question. Based on the identified entities, the relationship extraction function of the large language model is used to extract the relationships between entities from the natural language question. The graph is then searched for points (referred to as target vertices) that match or have the highest similarity to the entity relationships identified from the natural language question.

[0054] In one embodiment, a target vertex that matches the entity relationship to be queried can be retrieved in the graph by keyword matching. For example, the entity relationship to be queried can be used as a keyword, and the degree of matching between the keyword and each entity and entity relationship in the graph can be calculated to determine the portion of the graph that matches the keyword (which can be called a subgraph). The center point of this subgraph is determined as the target vertex that matches the entity relationship to be queried.

[0055] In another embodiment, a target vertex with the highest similarity to an entity relationship can be retrieved in the graph through vector search. For example, the entities and relationships in the graph can be converted into vector form. By calculating the vector similarity between the entity relationship to be queried and each entity in the graph, the vertex with the highest similarity to the entity relationship to be queried can be found, and this vertex with the highest similarity can then be determined as the target vertex.

[0056] In implementation, a calculation method for the degree of matching / similarity may be selected according to actual needs, such as cosine similarity, Euclidean distance, and other algorithms, and this application does not impose any restrictions on this.

[0057] S204 : Acquire local graph structure data within a specified degree from the target vertex, and generate a first prompt sentence based on the local graph structure data.

[0058] In practice, after a target vertex matching the entity relationship being queried is retrieved based on the graph, local graph structure data within a specified degree from the target vertex can be obtained. Based on this local graph structure data, a prompt sentence for input into the large language model can be generated. For ease of description, the prompt sentence generated based on the local graph structure can be referred to as the first prompt sentence.

[0059] In one embodiment, a designated degree of a suitable value may be pre-set considering factors such as the size of the graph structure data and the type of the data query task. This application does not impose any restrictions on the size of the designated degree.

[0060] For example, Figure 3 In the embodiment shown, the specified degree is set to 1. After the target vertex is retrieved, all information within 1 degree from the target vertex can be returned, and the returned information is used as a prompt sentence input to the large language model. It is worth mentioning that Figure 3 The volume of graph structure data corresponding to the graph shown is relatively small, so the specified degree can be preset to a smaller value. In this way, it can be avoided that the data within the specified degree returned is the entire graph structure data, or contains unnecessary information due to the specified degree value being too large. This ensures that the embodiment of the present application can generate prompt statements based on an appropriate amount of local graph structure data, shortens the length of the Schema in the prompt statement, solves the problem of the token length limit of the large language model, and reduces the fields that may cause misleading, providing the large model with relatively high-quality input data, and guiding the large language model to output more accurate graph query statements.

[0061] In one embodiment, the natural language question initiated by the user is: "Which knowledge point did the question Zhang San did come from?" The entities involved include: Zhang San, question, knowledge point; the entity relationship involved is: a question Zhang San did came from a certain knowledge point. Figure 2 For the natural language question "Which knowledge point did the question Zhang San did come from?", the target vertex retrieved is "question". The local graph structure data within 1 degree of the target vertex is all the information between the vertex "student" and the vertex "question", and between the vertex "question" and the vertex "knowledge point", including:

[0062] Node properties:[name:student,properties:['birthday'];name:topic,properties:['name'];name:knowledge point,properties:['name'];]

[0063] Edge properties:[name:do the question,name:belongs to]

[0064] Relationships: [Student --> Exercise --> Topic, Topic --> Knowledge Point, Student --> Class, Topic --> Textbook Version, Knowledge Point --> Chapter]

[0065] S206: Input the first prompt statement into the large language model to generate a first graph query statement.

[0066] In practice, local graph structure data can be used as prompts and fed into a large language model to generate a graph query corresponding to the natural language question. For example, for the natural language question "Which knowledge point did the questions Zhang San did come from?", after feeding the local graph structure data into the large language model as prompts, the generated graph query can be:

[0067] gV().has('Person','name','Zhang San').out('did').in('belong').hasLabel('knowledge').values('name').

[0068] In this Gremlin statement, function g is the starting point of the graph query statement, indicating that the traversal starts from a global perspective. Function V() indicates that all vertices are selected. has() is a filter. For example, has('Person', 'name', 'Zhang San') indicates that vertices with the label "Person" and the "name" attribute value "Zhang San" are selected. out() indicates traversing the outgoing edges starting from the current vertex and obtaining the connection points of the outgoing edges. in() indicates traversing the incoming edges of the current vertex and obtaining the connection points of the incoming edges. hasLabel() indicates querying points whose label is a certain value. values() indicates obtaining the property values ​​of the selected vertex.

[0069] It should be noted that different graph query statements may include different graph query functions or syntaxes, and this application does not limit this. The specific syntax structure can be found in the official documentation or relevant guidelines of various graph query languages, and this application will not elaborate on it here.

[0070] In one embodiment, in order to ensure that the graph query statement meets expectations and improve the accuracy of the generated graph query statement, the graph query statement generated by the large language model can be further optimized. For ease of description, the graph query statement generated by the large language model based on the first prompt statement can be referred to as the first graph query statement, and the optimized graph query statement can be referred to as the second graph query statement. Accordingly, after step S206, the graph query statement generation method provided in the embodiment of the present application can also include: performing data verification on the first graph query statement to generate a second graph query statement.

[0071] In implementation, the first graph query statement and the second graph query statement are in a specified graph query language type, such as Gremlin. Of course, the first graph query statement and the second graph query statement may also use other graph query language types supported by the graph database, and this application does not impose any restrictions on this.

[0072] In one embodiment, data validation may include syntax validation. Accordingly, performing data validation on the first graph query statement to generate the second graph query statement may specifically include: using a syntax parser to parse the first graph query statement; if the first graph query statement contains an erroneous function, adding the error location and a list of functions supported by the graph query language type to a first prompt statement to generate a second prompt statement; and inputting the second prompt statement into the large language model to generate the second graph query statement.

[0073] In implementation, since the large language model may have reasoning errors and hallucinations, it may fabricate one or more erroneous functions in the returned graph query statement, that is, use the wrong syntax. This may be caused by the prompt statement input to the large language model being inaccurate. Based on this, when the syntax of the first graph query statement generated by the large language model is more complex, the prompt statement input to the large language model can be optimized, and the optimized prompt statement can be used to guide the large language model to generate a more accurate graph query statement. For the sake of convenience of description, the prompt statement generated based on the local graph structure can be called the first prompt statement, and the further optimized prompt statement can be called the second prompt statement.

[0074] Specifically, a Gremlin parser can be used to parse the first graph query statement to determine whether it contains any erroneous functions. If so, the first prompt statement includes the location of the error and a list of functions supported by the Gremlin language, generating a second prompt statement. The optimized second prompt statement is then fed into the large language model to produce a more accurate second graph query statement. Furthermore, the optimized prompt statement and graph query statement can be used to fine-tune the large language model, enabling it to better adapt to real-world application scenarios and further improving its performance.

[0075] In one embodiment, data validation may include field validation. Accordingly, performing data validation on the first graph query statement to generate the second graph query statement may specifically include: identifying the fields referenced in the first graph query statement and determining whether each field exists in the graph structure data; if the target field does not exist in the graph structure data, obtaining a similar field in the graph structure data that matches the target field; and replacing the target field in the first graph query statement with the similar field to generate the second graph query statement.

[0076] In implementation, since large language models may have reasoning errors and hallucination problems, one or more non-existent erroneous fields may be fabricated in the returned graph query statement. Based on this, it is possible to compare whether the fields referenced in the graph query statement actually exist in the graph database. If the graph query statement references a non-existent field, it is possible to determine the inference errors and hallucination problems that occur in the graph query statement. At this time, similar fields that match each erroneous field (which can be called a target field) can be searched in the graph structure data to replace the erroneous field. For example, the real field with the highest similarity to the erroneous field can be found by calculating the similarity between the erroneous field and each field in the graph structure data. Then, after replacing the erroneous field in the first graph query statement with the matched similar field, a second graph query statement is obtained. In this way, a more accurate second graph query statement can be generated, and then when the second graph query statement is executed in the graph database, accurate query results can be obtained.

[0077] In one embodiment, data verification may include complex query verification. Accordingly, the steps of performing data verification on the first graph query statement and generating the second graph query statement may specifically include: determining whether the first graph query statement is a complex query statement based on the length of the first graph query statement; if so, performing vector representation on the first graph query statement to obtain a first graph query statement vector; retrieving similar knowledge items matching the first graph query statement vector from the complex query knowledge base, adding the similar knowledge items to the first prompt statement to generate a second prompt statement; and inputting the second prompt statement into the large language model to generate the second graph query statement.

[0078] In an implementation, the length of the first query statement may be the number of referenced functions in the first graph query statement. Accordingly, the step of determining whether the first graph query statement is a complex query statement based on the length of the first graph query statement may specifically include: determining whether the number of function references in the first graph query statement reaches a preset threshold; if so, determining that the first graph query statement is a complex query statement.

[0079] In implementation, a threshold value may be pre-set for the number of function references as a reference for determining whether the first query statement is a complex query statement. For example, if the number of function references in the first graph query statement is less than the pre-set threshold value, the first graph query statement may be determined to be non-complex; if the number of function references in the first graph query statement is not less than the pre-set threshold value, the first graph query statement may be determined to be complex.

[0080] In one embodiment, the complex query database used for the complex query verification process described above needs to be built in advance. Accordingly, before retrieving similar knowledge entries matching the first graph query statement vector from the complex query knowledge base, this embodiment further includes the following steps: when any graph query statement fails to execute in the graph database, obtaining a manually optimized graph query statement corresponding to the arbitrary graph query statement; and building a complex query knowledge base based on the natural language question, the arbitrary graph query statement, and the manually optimized graph query statement.

[0081] In implementation, for complex graph query statements, that is, statements with very long query logic or using a large number of functions (such as iteration, logical judgment, aggregation, branching, etc.), the failure rate of queries in the graph database will be relatively high. The reason for the failure may be that the execution result of the graph query statement is incorrect or the execution of the graph query statement fails. For failed queries, manual intervention can be made to adjust the graph query statement to obtain the correct graph query statement. In addition, data can be extracted from queries that have failed and have been repaired by manual intervention to construct a knowledge base for complex queries. For example, the "natural language question + the first graph query statement generated by the large language model + the manually adjusted graph query statement" corresponding to each failed query is stored as a knowledge entry in the complex query knowledge base. Furthermore, based on the constructed complex query knowledge base, the first graph query statement generated by the large language model can be used to perform complex query verification.

[0082] It should be noted that due to space limitations, this application description does not enumerate all optional implementation methods. After reading this application description, those skilled in the art should be able to understand that as long as the technical features do not contradict each other, any combination of technical features can constitute an optional implementation method.

[0083] For example, in one embodiment, the relevant technical features of syntax checking are described, while in another embodiment, the relevant technical features of field checking are described. Since the above two technical features do not contradict each other, those skilled in the art, after reading this specification, should be able to understand that having both syntax checking and field checking is also an optional embodiment. In this way, graph query statements can be dually optimized, further improving the accuracy of the generated graph query statements.

[0084] In one embodiment, multiple data verification methods including syntax verification, field verification, and complex query verification can be combined to form an optional implementation method. Figure 4 The graph query statement generation method provided in this embodiment may include:

[0085] S402, obtaining a natural language question.

[0086] S404, identifying entity relationships.

[0087] S406: Generate a prompt statement.

[0088] S408: Generate a graph query statement using the large language model.

[0089] The processing of steps S402-S408 is similar to that of steps S202-S206. The implementation method thereof can be found in Figure 2 The relevant steps in the illustrated embodiment are not described in detail in this application.

[0090] S410: Perform data verification on the graph query statement.

[0091] In practice, large language models can be prone to inference errors and hallucinations, potentially creating incorrect functions or non-existent fields in returned queries. Such queries cannot be directly submitted to a graph database for execution. Therefore, after generating graph queries, Gremlin syntax and field validation modules can be added to verify and correct the syntax and fields of the generated Gremlin statements, improving their accuracy.

[0092] The syntax validation module uses the Gremlin parser to parse Gremlin statements. If syntax or function errors occur, prompts are displayed, specifically indicating the error location and a list of Gremlin-supported functions. This allows for secondary optimization using the large language model. The field validation module first identifies all query fields referenced in the Gremlin statement and determines whether each field exists in the graph schema. If not, the closest field is found based on similarity and replaced with a replacement.

[0093] Afterwards, the Gremlin statement can be further verified for complex queries. If the Gremlin statement is determined to be a complex query, the prompt statement can be optimized, thereby guiding the large language model to regenerate a more accurate Gremlin statement based on the optimized prompt statement.

[0094] S412, executing the graph query statement in the graph database.

[0095] During implementation, the optimized second graph query statement can be input into the graph database for execution to obtain query results.

[0096] S414, building a complex query knowledge base.

[0097] In practice, complex queries in graph databases have a high failure rate. Possible causes for failure include incorrect execution results or statement execution failure. For incorrect queries, manual intervention is used to adjust the Gremlin statements. Based on this, data can be extracted from failed queries that were manually repaired, including "natural language questions + graph query statements generated by the large model + manually adjusted corrected graph query statements" to build a knowledge base for complex queries.

[0098] After building the complex query knowledge base, a module for identifying complex queries can be added in step S410. If the Gremlin statement length or the number of function references reaches a preset threshold, the Gremlin statement can be identified as a complex query. The accuracy of such queries is relatively low, and optimizing complex queries can improve query accuracy.

[0099] Specifically, in step S410, the graph query statement can be subjected to complex query verification. For statements identified as complex queries, a vector representation is performed on the "natural language question + the first graph query statement generated by the large language model." The most similar statement is retrieved from the complex query knowledge base and added to the prompt statement. Furthermore, the large language model can optimize the complex query based on the knowledge base's optimization solution. Specifically, the optimized prompt statement is fed into the large language model to guide it in generating the correct graph query statement.

[0100] For example, the first graph query language corresponding to the natural language question "Which knowledge point did the math problems Zhang San have done come from?" is determined to be a complex query statement. Afterwards, the statement most similar to the complex query statement is retrieved in the complex query knowledge base to optimize the complex query. In this embodiment, the statement most similar to "Which knowledge point did the math problems Zhang San have done come from" and its graph query statement is: the knowledge entry corresponding to "the shortest path from knowledge point '8931' to knowledge point '9342'". After obtaining the most similar complex query optimization statement, the prompt statement finally generated for secondary optimization of the large language model can be as follows:

[0101] Please refer to the following Gremlin query examples for issues, unoptimized statements, and optimized statements to optimize your results:

[0102] Question: The shortest path from knowledge point '8931' to knowledge point '9342'

[0103] Unoptimized statement: gV().has('knowledge','name','8931')

[0104] .repeat(__.in('belong').simplePath())

[0105] .until(has('knowledge', 'name', '9342'))

[0106] .path().by('name').limit(1)

[0107] Optimized statement: g.V('8931')

[0108] .repeat(in('belong').simplePath())

[0109] .until(has(T.id, '9342'))

[0110] .path().limit(1).unfold()

[0111] Among them, the unoptimized statement represents the graph query statement generated by the large language model based on the first prompt statement, and the optimized statement represents the graph query statement optimized by humans.

[0112] After obtaining the above prompt statements for complex queries, they can be input into the large language model to regenerate an accurate second graph query statement. For example, for the natural language question "Which knowledge points do the math problems done by Zhang San come from?", the second graph query statement generated in this embodiment can be:

[0113] g.V('Zhang San').out('did').in('belong')

[0114] .hasLabel('knowledge')

[0115] .values('name')

[0116] Compare with the first graph query statement generated by the large language model:

[0117] g.V().has('Person', 'name', 'Zhang San').out('did').in('belong').hasLabel('knowledge').values('name')

[0118] It is not difficult to find that the second graph query statement can accurately and concisely represent the query requirement expressed by the natural query question "Which knowledge points do the math problems done by Zhang San come from". Furthermore, the second graph query statement can accurately and quickly find the query result in the graph database.

[0119] Compared with the existing technology, the embodiment of the present application first adds the prerequisite task of entity relationship identification before generating the prompt statement. This reduces the length of the schema in the prompt and reduces the fields that may be misleading, providing relatively high-quality input data for the subsequent step of generating graph query statements through a large language model.

[0120] Secondly, after generating the graph query statement, Gremlin syntax verification and field verification are added. By verifying and correcting the syntax and fields of the generated Gremlin statement, the accuracy of the statement can be improved.

[0121] Next, after executing a graph query in the graph database, data is extracted from failed queries that required manual intervention and repair to build a knowledge base for complex queries. This is used to generate optimized prompts for these queries. This improves the accuracy of complex graph query generation. Finally, the grammatical logic and field information of the graph query are optimized. This reduces inference errors and hallucinations, improving the accuracy of the results generated by the large language model.

[0122] Based on the same technical concept, the present application embodiment also provides a device for generating a graph query statement, see Figure 4 , which may include:

[0123] An entity relationship recognition module is used to identify entity relationships in natural language question text and retrieve target vertices matching the entity relationships in the graph; wherein the graph is pre-generated based on graph structure data;

[0124] a prompt statement generating module, configured to obtain local graph structure data within a specified degree from the target vertex, and generate a first prompt statement based on the local graph structure data;

[0125] A graph query statement generating module is configured to input the first prompt statement into a large language model to generate a first graph query statement.

[0126] Optionally, the graph query statement generating device may further include:

[0127] A data verification module is used to perform data verification on the first graph query statement and generate a second graph query statement.

[0128] Optionally, the data verification includes syntax verification; the data verification module is specifically used to:

[0129] Using a grammar parser, parse the first graph query statement; the first graph query statement belongs to a specified graph query language type;

[0130] If the first graph query statement contains an erroneous function, then the error location and a list of functions supported by the graph query language type are added to the first prompt statement to obtain a second prompt statement;

[0131] The second prompt statement is input into the large language model to generate a second graph query statement.

[0132] Optionally, the data verification includes field verification; the data verification module is specifically used to:

[0133] Identifying fields referenced in the first graph query statement, and determining whether each of the fields is in the graph structure data;

[0134] If the target field is not in the graph structure data, obtaining a similar field in the graph structure data that matches the target field;

[0135] The target field in the first graph query statement is replaced with the similar field to generate a second graph query statement.

[0136] Optionally, the data verification includes complex query verification; the data verification module is specifically used to:

[0137] determining whether the first graph query statement is a complex query statement based on the length of the first graph query statement;

[0138] If yes, perform vector representation on the first graph query statement to obtain a first graph query statement vector;

[0139] Retrieving similar knowledge items matching the first graph query statement vector in a complex query knowledge base, and adding the similar knowledge items to the first prompt statement to generate a second prompt statement;

[0140] The second prompt statement is input into the large language model to generate the second graph query statement.

[0141] Optionally, the data verification module is further specifically configured to:

[0142] Determining whether the number of function references in the first graph query statement reaches a preset threshold;

[0143] If so, it is determined that the first graph query statement is a complex query statement.

[0144] Optionally, the data verification module is further used to:

[0145] When any graph query statement fails to execute in the graph database, obtaining a manually optimized graph query statement corresponding to the arbitrary graph query statement;

[0146] The complex query knowledge base is constructed based on the natural language question, the arbitrary graph query statement and the manually optimized graph query statement.

[0147] It should be noted that the device provided in the above embodiment only uses the division of the above functional modules as an example to illustrate when converting natural language questions into graph query statements. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the graph query statement generation device provided in the above embodiment and the graph query statement generation method embodiment are of the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.

[0148] Based on the same technical concept, the embodiment of the present application also provides a data query method, which may include:

[0149] Obtaining a first or second graph query statement generated by any implementation of the graph query statement generation method described above;

[0150] Execute the first or second graph query statement in the graph database and obtain data query results.

[0151] Based on the same technical concept, the present application also provides a data query device, see Figure 5 , which may include:

[0152] A graph query statement acquisition module, configured to acquire the first or second graph query statement generated by any implementation of the above graph query statement generation method;

[0153] The graph query statement execution module is used to execute the first or second graph query statement in the graph database and obtain data query results.

[0154] Based on the same technical concept, an embodiment of the present application also provides a computer-readable storage medium, which can store multiple instructions, which are suitable for being loaded by a processor and executing the method described in any of the above embodiments.

[0155] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus the necessary general hardware platform, and of course can also be implemented by hardware. Based on this understanding, the above technical solution, in essence, or the part that contributes to the existing technology, can be embodied in the form of a software product. The software product of the graph query statement generation method or data query method can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including storing a number of instructions for enabling a network device to execute the methods described in each embodiment or certain parts of the embodiment.

[0156] The above description is only a preferred embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present application should be included in the scope of protection of the present application.

Claims

1. A method for generating a graph query statement, characterized in that: The method comprises: Identifying entity relationships in natural language questions and retrieving target vertices matching the entity relationships in a graph; wherein the graph is pre-generated based on graph structure data; Acquire local graph structure data within a specified degree from the target vertex, and generate a first prompt statement based on the local graph structure data; The first prompt sentence is input into a large language model to generate a first graph query sentence.

2. The method according to claim 1, wherein After generating the first graph query statement, the method further includes: Perform data verification on the first graph query statement to generate a second graph query statement.

3. The method according to claim 2, wherein The data verification includes syntax verification; and performing data verification on the first graph query statement to generate the second graph query statement specifically includes: Using a grammar parser, parse the first graph query statement; If the first graph query statement contains an erroneous function, then the error location and a list of functions supported by the graph query language type are added to the first prompt statement to obtain a second prompt statement; The second prompt statement is input into the large language model to generate a second graph query statement.

4. The method according to claim 2, wherein The data verification includes field verification; and performing data verification on the first graph query statement to generate a second graph query statement specifically includes: Identifying the fields referenced in the first graph query statement, and determining whether each of the fields is in the graph structure data; If the target field is not in the graph structure data, obtaining a similar field in the graph structure data that matches the target field; The target field in the first graph query statement is replaced with the similar field to generate a second graph query statement.

5. The method according to claim 2, wherein The data verification includes complex query verification; performing data verification on the first graph query statement to generate a second graph query statement specifically includes: determining whether the first graph query statement is a complex query statement based on the length of the first graph query statement; If yes, perform vector representation on the first graph query statement to obtain a first graph query statement vector; Retrieving similar knowledge items matching the first graph query statement vector in a complex query knowledge base, and adding the similar knowledge items to the first prompt statement to generate a second prompt statement; The second prompt statement is input into the large language model to generate the second graph query statement.

6. The method according to claim 5, wherein The determining, based on the length of the first graph query statement, whether the first graph query statement is a complex query statement specifically includes: Determining whether the number of function references in the first graph query statement reaches a preset threshold; If so, it is determined that the first graph query statement is a complex query statement.

7. The method according to claim 5, wherein Before searching the complex query knowledge base for similar knowledge items matching the first graph query statement vector, the method further includes: When any graph query statement fails to execute in the graph database, obtaining a manually optimized graph query statement corresponding to the arbitrary graph query statement; The complex query knowledge base is constructed based on the natural language question, the arbitrary graph query statement and the manually optimized graph query statement.

8. The method according to claim 1, wherein The step of retrieving a target vertex matching the entity relationship in the graph specifically includes: By keyword matching or vector retrieval, the target vertex matching the entity relationship is retrieved in the graph.

9. A device for generating a graph query statement, characterized in that: include: An entity relationship recognition module is used to identify entity relationships in natural language questions and retrieve target vertices matching the entity relationships in the graph; wherein the graph is pre-generated based on graph structure data; a prompt statement generating module, configured to obtain local graph structure data within a specified degree from the target vertex, and generate a first prompt statement based on the local graph structure data; A graph query statement generating module is configured to input the first prompt statement into a large language model to generate a first graph query statement.

10. A data query method, characterized in that: The method comprises: Obtaining a graph query statement according to any one of claims 1 to 8; Execute the graph query statement in the graph database and obtain the data query results.

11. A data query device, characterized in that: The device comprises: A graph query statement acquisition module, configured to acquire a graph query statement according to any one of claims 1 to 8; The graph query statement execution module is used to execute the graph query statement in the graph database and obtain data query results.

12. A computer-readable storage medium, characterized in that The computer storage medium stores a plurality of instructions, and the instructions are suitable for being loaded by a processor and executing the method according to any one of claims 1 to 8.

Citation Information

Patent Citations

  • Knowledge graph query statement generation method and device, equipment and storage medium

    CN112818092A

  • Knowledge graph retrieval method and system fusing pre-training language model

    CN117555985A

  • Information query method and apparatus, storage medium, and smart terminal

    WO2021047169A1

Cited By

  • Method and device for converting natural language query into GQL statement

    CN121833767A