Cypher query statement generation optimization method, device and system based on large language model
By combining large language models for intent recognition and relational link screening, and generating and optimizing Cypher query statements, the problem of natural language conversion to Cypher query statements is solved, and efficient and intelligent graph database query operations are achieved.
Patent Information
- Application Number
- CN202411964878.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-04-11
AI Technical Summary
The prior art is difficult to directly convert natural language into effective Cypher query statements, and it is difficult to optimize query efficiency, resulting in technical barriers for non-technical users to interact with graph databases.
By combining the large language model to identify the original problems of users, filter out the relationship link that best meets the user's intentions, and design effective prompt words to guide the large language model to generate accurate and clear format Cypher query statements, and optimize the generated statements.
It enables non-technical users to easily generate effective Cypher query statements, lowering technical thresholds, improving user experience, and improving the generation accuracy and query efficiency of query statements.
Smart Images

Figure CN119807232A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of graph database query optimization, and in particular to a method, device and system for optimizing Cypher query statement generation based on a large language model. Background Art
[0002] In traditional graph database queries, users usually need to have a certain technical background and professional knowledge to write effective Cypher query statements. These Cypher query statements not only need to accurately specify the data fields, but also consider the efficiency and optimization effect of the query. However, for non-technical users, this query method of interacting with the graph database has obvious technical barriers. In many application scenarios, users prefer to be able to directly describe their problems in natural language, so that they can interact with the graph database to obtain data in the graph database without having to learn complex query languages to interact with the graph database. However, it is difficult for existing technologies to directly convert natural language into effective Cypher query statements and to optimize query efficiency. For example, the generation of graph database query language Cypher statements based on a general large language model is not accurate enough and has a high error rate. If the model is fine-tuned to improve accuracy, the quality requirements for the training data set are high, and the accuracy of Cypher statement generation outside the training data set is still not high.
[0003] In view of this, how to overcome the defects of the existing technology, how to directly convert natural language into effective Cypher query statements, and how to optimize query efficiency have become important technical issues that need to be urgently solved in the industry. Summary of the invention
[0004] In view of the defects or improvement needs in the prior art: it is difficult to directly convert natural language into effective Cypher query statements, and it is difficult to optimize query efficiency. The present invention provides a method, device and system for optimizing the generation of Cypher query statements based on a large language model. By combining the large language model to perform intent recognition on the original question raised by the user, the user's query intention can be understood more accurately, and the possible relationship links are preliminarily screened, and then the large language model is used for semantic analysis to screen out multiple relationship links that best meet the user's intention. In combination with the above information, effective prompt words (Prompt) are designed to guide the large language model to generate accurate, clearly formatted, easy to understand and use Cypher query statements, and the large language model is used to optimize the generated Cypher query statements, further improving the performance of the query statements, so that even non-technical users can easily generate effective Cypher query statements, and can interact with the graph database using a lower technical threshold, greatly improving the user experience. In summary, the present invention, by combining the advanced technology of large language models, not only improves the generation accuracy and query efficiency of Cypher query statements, but also enhances the friendliness, adaptability and intelligence of user interaction with graph databases, providing a more efficient and intelligent solution for graph database query operations.
[0005] The present invention adopts the following technical solution:
[0006] In a first aspect, the present invention provides a method for optimizing Cypher query statement generation based on a large language model, comprising:
[0007] Combined with the large language model, the original question raised by the user is used to identify the intent;
[0008] Based on the results of intent recognition, the large language model is used to extract key entity types, and the possible relationship links are preliminarily screened out through the key entity types combined with all the relationship links calculated based on the entity type and relationship type information in the graph database schema;
[0009] Using a large language model, we perform semantic analysis on the initially selected relationship links, and select N relationship links that best match the results of intent recognition from multiple possible relationship links.
[0010] Based on the N relationship links and the results of intent recognition, a large language model is used to generate corresponding Cypher query statements, and the large language model is used to optimize the generated Cypher query statements.
[0011] In some embodiments, the step of identifying the intent of the original question raised by the user in combination with the large language model specifically includes:
[0012] Define an intent classification system, including querying entities with specific attribute values, querying relationships between entities, and querying one or more query intent categories in a list of entities that meet specific conditions;
[0013] Designing a first prompt word, wherein the first prompt word is used to enable the large language model to identify possible query intent categories according to the user's question and rewrite the question that best matches the query intent;
[0014] The user's original question and the first prompt word are provided as input to the large language model, and the output of the large language model is post-processed to obtain the result after intent recognition.
[0015] In some embodiments, the method of extracting key entity types using a large language model based on the result of intent recognition, and preliminarily screening possible relationship links by combining the key entity types with all relationship links calculated based on the entity type and relationship type information in the graph database schema, specifically includes:
[0016] Construct a second prompt word, where the second prompt word is used to enable the large language model to identify the key entity type in the user's question based on the result of intent recognition and the entity type in the graph database schema;
[0017] Build a complete relationship link based on the entity types and relationship types in the graph database schema;
[0018] The complete relationship links are traversed in a loop. During the traversal process, all key entity types must appear in the relationship links to meet the screening conditions, until M relationship links are screened out.
[0019] In some embodiments, using a large language model to perform semantic analysis on the initially screened relationship links, and screening out N relationship links that best match the results of intent recognition from multiple possible relationship links specifically includes:
[0020] Constructing a third prompt word, wherein the third prompt word is used to enable the large language model to filter out N relationship links that best match the question semantics from M relationship links according to the question semantics; wherein M is greater than or equal to N;
[0021] The N selected relationship links are sorted according to semantic relevance, so that the relationship link with the most relevant relevance score to the user's intention is placed at the front.
[0022] In some embodiments, the generating of corresponding Cypher query statements using a large language model according to the N relationship links and the result of intent recognition specifically includes:
[0023] Use the large language model to understand the results of intent recognition again, analyze the N relationship links provided, and understand the entity type information and relationship type information in each link;
[0024] A fourth prompt word is constructed, where the fourth prompt word is used to enable the large language model to generate a Cypher query statement according to the original question raised by the user, the result after intent recognition, the graph database Schema, and the N relationship links.
[0025] In some embodiments, when the generated Cypher query statements are optimized using the large language model, the optimization content includes one or more of error checking and correction, query rewriting, pattern matching optimization, and query merging.
[0026] In some embodiments, the optimization of the generated Cypher query statement using the large language model specifically includes:
[0027] A fifth prompt word is constructed, where the fifth prompt word is used to enable the large language model to judge and optimize the generated Cypher query statement in sequence according to error checking and correction, query rewriting, pattern matching optimization, and query merging, and the large language model is limited to output only the optimized Cypher query statement.
[0028] In a second aspect, the present invention further provides a Cypher query statement generation and optimization device based on a large language model, the device comprising:
[0029] At least one processor; and a memory in communication with the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the processor to execute the Cypher query statement generation optimization method based on a large language model described in the first aspect.
[0030] In a third aspect, the present invention further provides a Cypher query statement generation optimization system based on a large language model, which applies the Cypher query statement generation optimization method based on a large language model as described in the first aspect, and the system includes an intention recognition module, a relationship link preliminary screening module, a relationship link secondary screening module, and a query statement generation optimization module, wherein:
[0031] The intention recognition module is used to: perform intention recognition on the original question raised by the user in combination with the large language model;
[0032] The relationship link preliminary screening module is used to: extract key entity types using a large language model based on the result of intent recognition, and preliminarily screen out possible relationship links by combining the key entity types with all relationship links calculated based on the entity type and relationship type information in the graph database schema;
[0033] The relationship link re-screening module is used to: use a large language model to perform semantic analysis on the relationship links initially screened, and screen out N relationship links that best match the results after intent recognition from multiple possible relationship links;
[0034] The query statement generation optimization module is used to: generate corresponding Cypher query statements using a large language model according to N relationship links and the results after intent recognition, and optimize the generated Cypher query statements using the large language model.
[0035] In a fourth aspect, the present invention further provides a non-volatile computer storage medium, wherein the computer storage medium stores computer executable instructions, which are executed by one or more processors to complete the Cypher query statement generation optimization method based on a large language model described in the first aspect.
[0036] Compared with the prior art, the present invention provides a method, device and system for optimizing the generation of Cypher query statements based on a large language model, which has the beneficial effects of: by combining the large language model to perform intent recognition on the original question raised by the user, the user's query intention can be understood more accurately, and by preliminarily screening possible relationship links, the large language model is used for semantic analysis to screen out multiple relationship links that best meet the user's intention, and by designing effective prompt words (Prompt) in combination with the above information to guide the large language model to generate accurate, clearly formatted, easy to understand and use Cypher query statements, and using the large language model to optimize the generated Cypher query statements, further improving the performance of the query statements, so that even non-technical users can easily generate effective Cypher query statements, and can interact with the graph database using a lower technical threshold, greatly improving the user experience.
[0037] In summary, the present invention, by combining the advanced technology of large language models, not only improves the generation accuracy and query efficiency of Cypher query statements, but also enhances the friendliness, adaptability and intelligence of user interaction with graph databases, providing a more efficient and intelligent solution for graph database query operations. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required to be used in the embodiments of the present invention. Obviously, the drawings described below are only some embodiments of the present invention, and for ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0039] Figure 1 A flowchart of a method for optimizing generation of Cypher query statements based on a large language model provided in Example 1 of the present invention;
[0040] Figure 2 An expanded flow chart of step 100 provided in Example 1 of the present invention;
[0041] Figure 3 An expanded flow chart of step 200 provided in Embodiment 1 of the present invention;
[0042] Figure 4 An expanded flow chart of step 300 provided in Embodiment 1 of the present invention;
[0043] Figure 5 An expanded flow chart of step 400 provided in Embodiment 1 of the present invention;
[0044] Figure 6 A schematic diagram of a module of a Cypher query statement generation optimization system based on a large language model provided in Example 1 of the present invention;
[0045] Figure 7 This is a schematic diagram showing an example of the graph database Schema structure provided in Example 2 of the present invention;
[0046] Figure 8 A schematic diagram of the structure of a Cypher query statement generation optimization device based on a large language model provided in Example 3 of the present invention. DETAILED DESCRIPTION
[0047] The present invention is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but do not limit the present invention in any form. It should be noted that, for those of ordinary skill in the art, without departing from the concept of the present invention, several variations and improvements can also be made. These all belong to the protection scope of the present invention. It should be noted that, if there is no conflict, the various features in the embodiments of the present invention can be combined with each other, all within the protection scope of this application. In addition, although the functional module division may be performed in the device schematic diagram, and the logical order may be shown in the flow chart, in some cases, it may be different from the module division in the device, or the steps shown or described in the order in the flow chart may be performed.
[0048] Unless otherwise defined, all technical and scientific terms used in this specification have the same meaning as those commonly understood by those skilled in the art of the present invention. The terms used in this specification and in the specification of the present invention are only for the purpose of describing specific embodiments and are not intended to limit the present invention. In addition, the technical features involved in each embodiment of the present invention described below can be combined with each other as long as they do not conflict with each other.
[0049] Unless the context requires otherwise, throughout the specification and claims, the term "including" is to be interpreted as open inclusion, that is, "including, but not limited to". In the description of the specification, the terms "one embodiment", "some embodiments", "exemplary embodiments", "examples", "specific examples" or "some examples" and the like are intended to indicate that specific features, structures, materials or characteristics associated with the embodiment or example are included in at least one embodiment or example of the present disclosure. The schematic representation of the above terms does not necessarily refer to the same embodiment or example. In addition, the specific features, structures, materials or characteristics may be included in any one or more embodiments or examples in any appropriate manner, that is, although they may be carried in the embodiments or examples of the above terms due to reasons such as the order and position of appearance, it is not limited to that they can be carried in combination by one embodiment or example.
[0050] For non-technical users, traditional query methods have obvious barriers. In many application scenarios, users prefer to be able to directly describe their problems in natural language to interact with the graph database without having to learn complex query languages. Therefore, an embodiment of the present invention provides a Cypher query statement generation optimization method based on a large language model to solve the above difficulties. Among them, the processing of intent recognition is one of the core difficulties of the technical solution of the embodiment of the present invention. Unlike traditional imperative interactions, intent recognition requires the system to be able to deeply understand the user's natural language input, and accurately infer the true intention behind it, as well as handle the ambiguity and fuzziness of the language. How to efficiently extract key information from the user's input statements, and how to accurately identify the user's intentions in a variety of contexts, is one of the key issues that need to be solved.
[0051] On the other hand, the embodiment of the present invention also extracts key entity types from the results after intent recognition, and preliminarily screens out possible relationship links through the key entity types combined with the relationship links in the graph database schema, and then uses the capabilities of the large language model to perform semantic analysis on the preliminarily screened relationship links, and screens out N relationship links that best meet the user's intent from multiple possible relationship links.
[0052] On the other hand, the embodiment of the present invention also uses a large language model to generate corresponding accurate and efficient Cypher query statements based on the N relationship links and the results of intent recognition, and optimizes them to ensure the execution efficiency of the query.
[0053] When using a large language model directly to generate Cypher statements, the error rate of the generated Cypher statements may be high due to the large language model's insufficient understanding of the graph database structure and query logic. However, by carefully designing the prompt engineering and providing the large language model with some sample questions and corresponding Cypher statements, the model can learn the correspondence between specific types of questions and Cypher patterns, and can apply them to similar queries. In addition, by providing the relationship links in the generated Cypher statements, the accuracy of the generated Cypher statements can be greatly improved.
[0054] Therefore, the embodiments of the present invention aim to overcome the deficiencies of the prior art and propose an innovative method, which, with the help of a large language model, enables non-technical users to easily generate effective Cypher query statements through natural language, and interact with the graph database with a relatively low technical threshold, greatly improving the user experience. The embodiments of the present invention also demonstrate great potential and value in various application scenarios such as intelligent dialogue systems, AI question-and-answer functions, knowledge graph-driven question-and-answer, and automated Cypher queries, making data query and analysis more intuitive and efficient.
[0055] In order to make the purpose, technical scheme and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not intended to limit the present application. The present invention is described in detail below with reference to the accompanying drawings and embodiments.
[0056] Embodiment 1:
[0057] like Figure 1 As shown, an embodiment of the present invention provides a Cypher query statement generation optimization method based on a large language model, and the method includes the following steps.
[0058] Step 100: Use a large language model to identify the intent of the original question raised by the user.
[0059] Step 200: Based on the results of intent recognition, the large language model is used to extract key entity types, and all relationship links calculated based on the key entity types and entity type information in the graph database schema are used to preliminarily screen possible relationship links. Among them, the relationship link is: the connection and interaction between nodes (entities), which defines how entities are related to each other and the direction of these relationships.
[0060] Step 300: Using a large language model, semantic analysis is performed on the initially screened relationship links, and N relationship links that best match the results after intent recognition are screened out from multiple possible relationship links. For example, the top 3 relationship links that best match the results after intent recognition are screened out from multiple possible relationship links.
[0061] Step 400: Based on the N relationship links and the results after intent recognition, a large language model is used to generate corresponding Cypher query statements, and the generated Cypher query statements are optimized using the large language model. When the generated Cypher query statements are optimized using the large language model, the optimization content includes one or more of error checking and correction, query rewriting, pattern matching optimization, and query merging. For example, based on the top 3 relationship links and the results after intent recognition, a large language model is used to generate corresponding accurate and efficient Cypher query statements; the generated Cypher query statements are optimized using the large language model for error checking and correction, query rewriting, pattern matching optimization, query merging, etc.
[0062] Through the above method, non-technical users can easily generate valid Cypher query statements through natural language and interact with the graph database with a low technical threshold, which greatly improves the user experience.
[0063] The concept of graph database schema mainly refers to the structured definition of data in the graph database, which describes the nodes (Node), edges (Edge) and the relationships and properties between them in the graph database. The details are as follows:
[0064] Node / Entity(Node):
[0065] A node is the basic unit in a graph database and represents an entity or object.
[0066] Each node has a label / type (Label) which is used to classify and distinguish different types of nodes.
[0067] A node can contain multiple properties, which describe the characteristics of the node.
[0068] Edge:
[0069] Edges represent the relationship between nodes and are directed.
[0070] Edges also have labels / types to distinguish between different types of relationships.
[0071] Edges can also contain attributes, describing characteristics of the relationship.
[0072] The edge has a start node and an end node to indicate the information of the edge connecting the two nodes and the connection direction.
[0073] Property:
[0074] Attributes are key-value pairs that store additional information about nodes and edges.
[0075] Attributes can be of any data type, such as string, number, Boolean, date, time, geographic coordinates (Geo), etc.
[0076] like Figure 2 As shown, in some embodiments, in step 100, the process of identifying the intent of the original question raised by the user in combination with the large language model specifically includes the following steps.
[0077] Step 101: Define an intent classification system, including querying entities with specific attribute values, querying relationships between entities, and querying one or more query intent categories in a list of entities that meet specific conditions.
[0078] Step 102: Design a first prompt word, where the first prompt word is used to enable the large language model to identify possible query intent categories based on the user's question and rewrite the question that best meets the query intent.
[0079] Step 103: Provide the user's original question and the first prompt word as input to the large language model, and post-process the output of the large language model to obtain the result after intent recognition.
[0080] In the above steps, based on business needs and possible user query types, an intent classification system is defined, an effective prompt is designed to guide the large language model to understand the task it needs to perform, and the user's original question, together with the designed prompt, is provided as input to the large language model. This input usually needs to be formatted to ensure that the model can correctly understand the context of the task, and the model's output is post-processed to ensure the accuracy and usability of the results.
[0081] The significance of the above steps: Through intent recognition, the core intent in the user's natural language is identified to reduce the noise in the subsequent generated Cypher statements and make subsequent task execution more accurate. The core of intent recognition is achieved by designing prompts corresponding to intent classifications.
[0082] like Figure 3As shown, in some embodiments, in step 200, the key entity types are extracted using a large language model based on the result of intent recognition, and the possible relationship links are preliminarily screened out by combining the key entity types with all the relationship links calculated based on the entity type and relationship type information in the graph database Schema, which specifically includes the following steps.
[0083] Step 201: construct a second prompt word, where the second prompt word is used to enable the large language model to identify the key entity type in the user's question based on the result of intent recognition and the entity type in the graph database Schema.
[0084] In the above steps, the key entity types to be extracted are clarified according to the entity types in the graph database schema, ensuring that the model understands the entity type information to be extracted, constructing effective prompts to guide the large language model to identify and extract the required entity type information, and inputting the results of intent recognition together with the designed prompts into the large language model. This input needs to be clearly formatted so that the large language model can accurately understand the task, post-process the output of the model to ensure that the extracted entity type information conforms to the expected format, and perform necessary cleaning and verification.
[0085] The significance of the above steps is: extract key entity types, use a large language model, build an effective prompt to identify and extract key entities in the results of intent recognition, and identify the start, end, and middle entity node information.
[0086] Step 202: Construct a complete relationship link based on the entity type and relationship type in the graph database schema. It should be noted that all relationship links are not directly reflected in the graph database schema, but are calculated through the entity type and relationship type information provided in the graph database schema. Specifically, the method of calculating all relationship links includes: initializing an empty list of all relationship links to store relationship links that meet the conditions. Construct a node label list based on the node labels in the node information provided in the graph database schema. Generate a permutation list with the number of elements greater than or equal to 2 for the node list. Traverse the permutation list to obtain each element in the permutation list, where each element is a list, the first element in the list is the starting node, the last element is the ending node, and the middle elements are intermediate nodes, and they are arranged in order; then combine the starting node and the ending node in the edge information in the graph database schema to determine whether it is possible to reach the ending node from the starting node in the permutation element and the middle must pass through the intermediate nodes in the list in order. If it can be reached, it is added to the list of all relationship links until the loop ends, and all relationship links can be obtained.
[0087] Step 203: loop through the complete relationship links, during which all key entity types must appear in the relationship links to meet the screening conditions, until M relationship links are screened out. For example, 5 relationship links are screened out.
[0088] In the above steps, a complete relationship link is constructed based on the entity type information and relationship type information in the graph database schema, and the complete relationship link is traversed in a loop. During the traversal process, all key entity types must appear in the link to meet the screening conditions, until 5 relationship links are screened out.
[0089] The significance of the above steps is: preliminarily screening the relationship links and obtaining 5 relationship links that meet the start, end, and intermediate nodes. At this time, the 5 relationship links obtained are accurate relationship links.
[0090] like Figure 4 As shown, in some embodiments, in step 300, the use of a large language model to perform semantic analysis on the initially screened relationship links, and screening out N relationship links that best match the results after intent recognition from multiple possible relationship links specifically includes the following steps.
[0091] Step 301: construct a third prompt word, wherein the third prompt word is used to enable the large language model to filter out N relationship links that best match the question semantics from M relationship links according to the question semantics; wherein M is greater than or equal to N.
[0092] Step 302: sort the N selected relationship links according to semantic relevance, so that the relationship link with the most relevant relevance score to the user's intention is placed at the front.
[0093] In the above steps, an effective prompt is constructed to guide the large language model. The candidate relationship links are sorted according to the relevance score between the relationship link and the user intention, combined with the user intention and the 5 selected relationship links. The result of intent recognition is input into the large language model together with the designed prompt. The input needs to be clearly formatted so that the large language model can accurately understand the sorting task. The output of the model is post-processed to ensure that the top 3 most suitable relationship links selected are correct and complete relationship links.
[0094] The significance of the above steps is: filter the top 3 relationship links, sort the relevance scores according to the above 5 relationship links, and then combine the semantic reasoning of the large language model to obtain the top 3 most relevant relationship links.
[0095] like Figure 5As shown, in some embodiments, in step 400, the process of using a large language model to generate corresponding accurate and efficient Cypher query statements based on N relationship links and the results of intent recognition, and using the large language model to optimize the generated Cypher query statements specifically includes the following steps.
[0096] Step 401: Use the large language model to understand the result of intent recognition again, analyze the N provided relationship links, and understand the entity type information and relationship type information in each link.
[0097] Step 402: Construct a fourth prompt word, where the fourth prompt word is used to enable the large language model to generate a Cypher query statement according to the original question raised by the user, the result after intent recognition, the graph database Schema, and N relationship links.
[0098] In the above steps, the large language model is used to understand the results of intent recognition again, and the three relationship links provided are analyzed to understand the entity type information and relationship type information in each link, as well as how they are associated with the user's question intent, and to construct an effective prompt to guide the large language model to generate Cypher query statements. This prompt should include the user's original question, the result of user question intent recognition, the graph database schema, the top three relationship links, and a few-shot prompt for generating Cypher query statements (this step may require dynamic acquisition of few-shot). The designed prompt is input into the large language model to generate Cypher query statements.
[0099] Step 403: construct a fifth prompt word, wherein the fifth prompt word is used to enable the large language model to judge and optimize the generated Cypher query statement in accordance with error checking and correction, query rewriting, pattern matching optimization, and query merging in sequence, and limit the large language model to only output the optimized Cypher query statement.
[0100] In the above steps, the large language model is used to perform grammatical and semantic analysis on Cypher query statements, identify potential errors and inconsistencies, and correct them; the large language model is used to reduce unnecessary node and relationship traversals in Cypher query statements, and the pattern matching in Cypher query statements is further optimized by merging similar patterns or using more efficient matching strategies; for multiple related queries, they are merged into one query with the help of the large language model to reduce the number of database accesses.
[0101] The significance of the above steps is: to optimize Cypher query statements and to perform error checking and optimization on various aspects of the generated Cypher statements through a large language model.
[0102] The embodiment of the present invention can more accurately understand the user's query intention by combining a large language model to perform intent recognition on the original question raised by the user, and can screen out the top 3 relationship links that best meet the user's intention by preliminarily screening possible relationship links and then using the large language model for semantic analysis. In combination with the above information, effective prompts are designed to guide the large language model to generate accurate, clearly formatted, easy-to-understand and easy-to-use Cypher query statements, and the large language model is used to perform error checking and correction, query rewriting, pattern matching optimization, query merging and other optimization operations on the generated Cypher query statements, thereby further improving the performance of the query statements. By formatting input and post-processing output, the friendliness of user interaction is ensured, so that even non-technical users can easily generate effective Cypher query statements, and can interact with the graph database with a low technical threshold, greatly improving the user experience.
[0103] In summary, the present invention, by combining the advanced technology of large language models, not only improves the generation accuracy and query efficiency of Cypher query statements, but also enhances the friendliness, adaptability and intelligence of user interaction with graph databases, providing a more efficient and intelligent solution for graph database query operations.
[0104] Based on the above method, this embodiment also provides a Cypher query statement generation optimization system based on a large language model. Figure 6 As shown, the system includes an intention recognition module, a relationship link preliminary screening module, a relationship link re-screening module and a query statement generation optimization module, wherein: the intention recognition module is used to: perform intention recognition on the original question raised by the user in combination with a large language model; the relationship link preliminary screening module is used to: extract key entity types based on the result after intention recognition using a large language model, and preliminarily screen out possible relationship links through all relationship links obtained by combining key entity types with entity types and relationship type information in the graph database Schema to participate in the calculation; the relationship link re-screening module is used to: use a large language model to perform semantic analysis on the relationship links preliminarily screened out, and screen out N relationship links that best meet the results after intention recognition from multiple possible relationship links; the query statement generation optimization module is used to: generate corresponding Cypher query statements based on N relationship links and the results after intention recognition using a large language model, and optimize the generated Cypher query statements using a large language model. The specific implementation process of each module can refer to the above method steps, which will not be repeated here.
[0105] Embodiment 2:
[0106] Based on the Cypher query statement generation optimization method based on a large language model provided in the above-mentioned embodiment 1, embodiment 2 of the present invention provides a specific example for illustration.
[0107] Examples of the above scheme are as follows:
[0108] Step 1: Intent recognition.
[0109] The questions asked by users are all based on the content in the graph database, so you need to have a detailed understanding of the graph database schema first, such as Figure 7 As shown, the Figure 7 Describes the interrelationships between the infrastructure and related services of a smart city. It covers the urban drainage system (including pumps and sewers), water accumulation in city streets, the impact of heavy rain on buildings and farms, the ownership and use of buildings and farms, and the interaction between people and companies, buildings, and flights. Figure 7 It provides a comprehensive perspective for smart cities to monitor and manage key infrastructure and services in the city, as well as the interactions between them. The detailed structure of its graph database schema is described as follows:
[0110] Entity types and their properties:
[0111] Entity type: water pump, attribute information: name, availability, maximum discharge capacity;
[0112] Entity type: sewer pipe, attribute information: name, current capacity, warning capacity;
[0113] Entity type: street, attribute information: name, surface water depth, surface water accumulation;
[0114] Entity type: Heavy rain, attribute information: name, level, precipitation area
[0115] Entity type: Farm, attribute information: name, address, coordinates;
[0116] Entity type: building, attribute information: name, type, address;
[0117] Entity type: person, attribute information: name, gender, age;
[0118] Entity type: company, attribute information: name, address;
[0119] Entity type: flight, attribute information: flight number, departure point, destination;
[0120] Relationship types and their properties:
[0121] Relationship type: Installation, Attribute information: None, Start entity type: Street, End entity type: Sewer pipe;
[0122] Relationship type: supporting, attribute information: none, starting entity type: sewer pipe, ending entity type: water pump;
[0123] Relation type: precipitation, attribute information: none, start entity type: rainstorm, end entity type: street;
[0124] Relation type: flooding, attribute information: none, starting entity type: rainstorm, ending entity type: farm;
[0125] Relationship type: Affects, attribute information: None, starting entity type: Heavy rain, ending entity type: Building;
[0126] Relationship type: Farmer, Attribute information: None, Starting entity type: Person, Ending entity type: Farm;
[0127] Relationship type: medical consultation, attribute information: none, starting entity type: person, ending entity type: building;
[0128] Relationship type: Electricity service, Attribute information: None, Starting entity type: Building, Ending entity type: Person;
[0129] Relationship type: office space, attribute information: none, starting entity type: company, ending entity type: building;
[0130] Relationship type: employment, attribute information: none, starting entity type: person, ending entity type: company;
[0131] Relationship type: service, attribute information: none, starting entity type: building, ending entity type: flight;
[0132] Relationship type: booking, attribute information: none, starting entity type: person, ending entity type: flight;
[0133] Combined with the above graph database schema, we first define an intent classification system. The specific intent classification system is: querying entities with specific attribute values, querying the relationship between entities, and querying the list of entities that meet specific conditions.
[0134] Design the first prompt word (Prompt), the specific prompt word is:
[0135]
[0136] The design of this prompt word clearly informs the large language model of its tasks and goals, and provides a clear framework, allowing the large language model to fully understand the context information to guide the model to focus on identifying the user's query intent and rephrasing the question, rather than completing other tasks. By listing possible query intent categories, the model can better understand and classify the user's questions, so as to rephrase the questions that best meet the query intent.
[0137] For example: The user inputs the question: Please check whose flights will be affected by the heavy rain in Shuangliu District?
[0138] After step 1, the rewritten question is: query the relationship between the entities of Shuangliu area heavy rain, flights, and people.
[0139] Step 2: Preliminary screening of relationship links.
[0140] Based on the question that best matches the query intent obtained in step 1 and combined with the entity type in the graph database schema, the second prompt is constructed. The specific prompt is:
[0141]
[0142]
[0143] The design of this prompt word enables the large language model to accurately understand the semantic information in the user's question, and to limit the result range in combination with the provided entity type information. The output result is concise and clear, only contains the identified entity types, and is separated by # for easy subsequent processing and analysis.
[0144] For example, after step 1, the rewritten question is: Query the relationship between the entities Shuangliu District Heavy Rain, Flight, and Person. Fill it in the question placeholder of the second prompt word (Prompt) above, and the result output by the large language model is: Heavy Rain#Flight#Person.
[0145] After identifying the key entity types, it is necessary to preliminarily screen out possible relationship links by combining the key entity types with all relationship links calculated based on the entity type and relationship type information in the graph database schema. In this step, it is necessary to build a complete relationship link based on the entity type and relationship type in the graph database schema (the starting entity and the ending entity in the relationship type), and then loop through the complete relationship link. During the traversal process, all key entity types must appear in the link to meet the screening conditions, until 5 relationship links are screened out, as shown below:
[0146]
[0147] Through the above steps, 5 relationship links that meet the conditions can be obtained.
[0148] For example, after screening, the five relationship links that meet the conditions are:
[0149] (Heavy rain)-[Affected]->(Building)-[Service]->(Flight)<-[Ticket booking]-(People)
[0150] (Heavy rain)-[Flooding]->(Farm)<-[Farmer]-(Person)-[Ticket booking]->(Flight)
[0151] (Heavy rain)-[Affected]->(Building)<-[See a doctor]-(People)-[Booking]->(Flight)
[0152] (Heavy rain)-[Affected]->(Buildings)-[Electricity services]->(People)-[Ticket booking]->(Flights)
[0153] (Heavy rain)-[Affected]->(Building)<-[Office]->(Company)<-[Employment]-(Person)-[Ticket booking]->(Flight)
[0154] Step 3: Filter the top three relationship links that best match the intent.
[0155] After obtaining the five relationship links that meet the conditions in step 2, the third prompt word (Prompt) is constructed. The specific prompt word is:
[0156]
[0157]
[0158] This prompt word can accurately extract semantics from the user's question, and combined with the provided relationship link information, filter out the three links that best meet the user's needs. This prompt word allows the large language model to follow strict screening steps, ensuring that only information in the context is used without any irrelevant play. The screening results will be sorted according to the relevance score of the relationship link and the user's intention, ensuring that the most relevant information is ranked first. The final output is concise and clear, only contains the required relationship links, no additional explanatory information is required, and the output format is standardized, easy to understand and use.
[0159] For example, after filtering, the top 3 relationship links are:
[0160] (Heavy rain)-[Affected]->(Building)-[Service]->(Flight)<-[Ticket booking]-(People)
[0161] (Heavy rain)-[Affected]->(Building)<-[See a doctor]-(People)-[Booking]->(Flight)
[0162] (Heavy rain)-[Affected]->(Buildings)-[Electricity services]->(People)-[Ticket booking]->(Flights)
[0163] Step 4: Generate a Cypher query statement.
[0164] After obtaining the top 3 relationship links according to step 3, construct the fourth prompt word (Prompt), the specific prompt word is:
[0165]
[0166]
[0167]
[0168] This prompt word enables the large language model to fully understand and optimize the syntax of the Cypher language, improve query performance, and enable the large language model to write Cypher query statements in a specific style to ensure the accuracy of the query path. It also ensures that the large language model directly writes the query path based on the provided relationship link information without performing additional relationship reasoning. By using entity type information and relationship type information to limit the accuracy of the Cypher query statement results generated by the large language model, and isolating Cypher query statement keywords with spaces, the generated Cypher is more accurate. By adding few-shot examples in placeholder mode, the accuracy of the generated Cypher query statements can be more guaranteed and more flexible.
[0169] For example, the top 3 relationship links obtained in step 3 and the information required in the prompt word are sent to the large language model, and the Cypher query statement generated is:
[0170] MATCH p = (Rainstorm: Heavy rain {precipitation area: 'Shuangliu District'})-[: Affected buildings]->(Building: Building)-[: Service]->(Flight: Flight)<-[: Ticket booking]-(Person: Person) RETURN p.
[0171] Step 5: Optimize the Cypher query statement.
[0172] According to the Cypher query statement obtained in step 4, the fifth prompt word (Prompt) is constructed. The specific prompt word is:
[0173]
[0174]
[0175] This prompt first gives the large language model a relatively high role positioning, enabling the large language model to exert its maximum capabilities under the corresponding role identity. By allowing the large language model to judge and optimize successively through various means such as error checking and correction, query rewriting, pattern matching optimization, and query merging, and restricting the large language model to only output the optimized Cypher query statement for convenient subsequent use.
[0176] For example: When the generated Cypher query statement is:
[0177] MATCH(Person: person)WHERE Person.name IN ['Alice', 'Bob', 'Charlie', 'David'] RETURN Person;
[0178] The Cypher query statement optimized through step 5 is:
[0179] MATCH(Person: person)WHERE Person.name = 'Alice' OR Person.name = 'Bob' OR Person.name = 'Charlie' OR Person.name = 'David' RETURN Person;
[0180] This is because in some cases, using multiple OR conditions is more efficient than using the IN clause, especially when the list is not very large. This is because the IN clause is internally converted into multiple OR conditions, but writing out these conditions manually can avoid the additional conversion steps.
[0181] For example: When the generated Cypher query statement is:
[0182] MATCH(Person: person{name: 'Alice'})MATCH(Person)-[:visited]->(Build: building) RETURN Build;
[0183] The Cypher query statement optimized through step 5 is:
[0184] MATCH(Person: person{name: 'Alice'})OPTIONAL MATCH(Person)-[:visited]->(Build: building) RETURN Build;
[0185] This is because using OPTIONAL MATCH can complete the work of two queries in one query, reducing the complexity of the query and can handle the situation where there is no visited building without returning an error.
[0186] For example, when the generated Cypher query statement is:
[0187] MATCH (Person: person)-[r: visit]->(Build: building) RETURN Person, r, Build;
[0188] The Cypher query statement optimized through Step 5 is:
[0189] MATCH (Person: person)-[r: visit]->(Build: building) WITH Person, COLLECT(Build) AS builds RETURN Person, builds;
[0190] This is because using the WITH clause and the COLLECT aggregation function can reduce the amount of data returned, only returning each person and the list of buildings they visited, rather than individual records for each building.
[0191] Generally speaking, the embodiments of the present invention provide an optimization method for generating Cypher query statements based on a large language model, aiming to achieve the purpose that non-technical users can easily generate effective Cypher query statements. The algorithm is constructed through the following steps: First, the large language model is combined to identify the intention of the original question proposed by the user. Subsequently, according to the result of the intention recognition, the large language model is used to extract the key entity types, and all the relationship links calculated by combining the entity types and relationship type information in the graph database Schema through the key entity types are used to preliminarily screen out the possible relationship links. Further, the large language model is used to perform semantic analysis on the preliminarily screened relationship links, and the top 3 relationship links that best meet the user's intention are screened out from multiple possible relationship links. In addition, according to the top 3 relationship links and the result of the intention recognition, the large language model is used to generate the corresponding accurate and efficient Cypher query statement and optimize it to ensure the execution efficiency of the query.
[0192] In summary, the embodiments of the present invention overcome the problems of low query efficiency and poor user-friendliness in the traditional solutions by combining the advanced technology of the large language model, and provide a user with an efficient and intelligent method for generating Cypher query statements for a graph database.
[0193] Embodiment 3:
[0194] Based on the optimization method for generating Cypher query statements based on a large language model provided in the above Embodiment 1, the present invention also provides a device for optimizing the generation of Cypher query statements based on a large language model that can be used to implement the above method and system, as Figure 8, which is a schematic diagram of the device architecture of an embodiment of the present invention. The Cypher query statement generation and optimization device based on a large language model of this embodiment includes one or more processors 21 and a memory 22. Among them, Figure 8 A processor 21 is taken as an example.
[0195] The processor 21 and the memory 22 may be connected via a bus or other means. Figure 8 The example of connecting through bus is taken in the following.
[0196] The memory 22, as a non-volatile computer-readable storage medium, can be used to store non-volatile software programs, non-volatile computer executable programs and modules, such as the Cypher query statement generation optimization method based on a large language model in Example 1. The processor 21 executes various functional applications and data processing of the Cypher query statement generation optimization device based on a large language model by running the non-volatile software programs, instructions and modules stored in the memory 22, that is, implements the Cypher query statement generation optimization method based on a large language model in Example 1.
[0197] The memory 22 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some embodiments, the memory 22 may optionally include a memory remotely arranged relative to the processor 21, and these remote memories may be connected to the processor 21 via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0198] The program instructions / modules are stored in the memory 22. When executed by one or more processors 21, the Cypher query statement generation optimization method based on the large language model in the above embodiment 1 is executed. For example, the above described Figure 1-Figure 5 The steps shown.
[0199] The above-mentioned product can execute the method provided in the embodiment of the present application, and has the functional modules and beneficial effects corresponding to the execution method. For technical details not fully described in this embodiment, please refer to the method provided in the embodiment of the present application.
[0200] It should be noted that the device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0201] Through the description of the above implementation methods, ordinary technicians in this field can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, it can also be implemented by hardware. Ordinary technicians in this field can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing related hardware through a computer program, and the program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc.
[0202] 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 them. Under the concept of the present invention, the technical features in the above embodiments or different embodiments may also be combined, the steps may be implemented in any order, and there are many other changes in different aspects of the present invention as described above, which are not provided in detail for the sake of simplicity. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A Cypher query statement generation optimization method based on a large language model, characterized in that: include: Combined with the large language model, the original question raised by the user is used to identify the intent; Based on the results of intent recognition, the large language model is used to extract key entity types, and the possible relationship links are preliminarily screened out through the key entity types combined with all the relationship links calculated based on the entity type and relationship type information in the graph database schema; Using a large language model, we perform semantic analysis on the initially selected relationship links, and select N relationship links that best match the results of intent recognition from multiple possible relationship links. Based on the N relationship links and the results of intent recognition, a large language model is used to generate corresponding Cypher query statements, and the large language model is used to optimize the generated Cypher query statements.
2. The method for optimizing the generation of Cypher query statements based on a large language model according to claim 1, characterized in that: The method of combining the large language model to identify the intent of the original question raised by the user specifically includes: Define an intent classification system, including querying entities with specific attribute values, querying relationships between entities, and querying one or more query intent categories in a list of entities that meet specific conditions; Designing a first prompt word, wherein the first prompt word is used to enable the large language model to identify possible query intent categories according to the user's question and rewrite the question that best matches the query intent; The user's original question and the first prompt word are provided as input to the large language model, and the output of the large language model is post-processed to obtain the result after intent recognition.
3. The method for optimizing the generation of Cypher query statements based on a large language model according to claim 1, characterized in that: According to the result of intent recognition, the key entity types are extracted using a large language model, and the possible relationship links are initially screened out by combining the key entity types with all the relationship links calculated based on the entity type and relationship type information in the graph database schema. Specifically, the following are included: Construct a second prompt word, where the second prompt word is used to enable the large language model to identify the key entity type in the user's question based on the result of intent recognition and the entity type in the graph database schema; Build a complete relationship link based on the entity types and relationship types in the graph database schema; The complete relationship links are traversed in a loop. During the traversal process, all key entity types must appear in the relationship links to meet the screening conditions, until M relationship links are screened out.
4. The method for optimizing the generation of Cypher query statements based on a large language model according to claim 3, characterized in that: The use of a large language model to perform semantic analysis on the initially screened relationship links, and to screen out N relationship links that best match the results of intent recognition from multiple possible relationship links, specifically includes: Constructing a third prompt word, wherein the third prompt word is used to enable the large language model to filter out N relationship links that best match the question semantics from M relationship links according to the question semantics; wherein M is greater than or equal to N; The N selected relationship links are sorted according to semantic relevance, so that the relationship link with the most relevant relevance score to the user's intention is placed at the front.
5. The method for optimizing Cypher query statement generation based on a large language model according to claim 1, characterized in that: The method of using a large language model to generate corresponding Cypher query statements based on the N relationship links and the results of intent recognition specifically includes: Use the large language model to understand the results of intent recognition again, analyze the N relationship links provided, and understand the entity type information and relationship type information in each link; A fourth prompt word is constructed, where the fourth prompt word is used to enable the large language model to generate a Cypher query statement according to the original question raised by the user, the result after intent recognition, the graph database Schema, and the N relationship links.
6. The method for optimizing the generation of Cypher query statements based on a large language model according to claim 1, characterized in that: When the generated Cypher query statement is optimized using the large language model, the optimization content includes one or more of error checking and correction, query rewriting, pattern matching optimization, and query merging.
7. The method for optimizing the generation of Cypher query statements based on a large language model according to claim 6, characterized in that: The optimization of the generated Cypher query statement by using the large language model specifically includes: A fifth prompt word is constructed, where the fifth prompt word is used to enable the large language model to judge and optimize the generated Cypher query statement in sequence according to error checking and correction, query rewriting, pattern matching optimization, and query merging, and the large language model is limited to output only the optimized Cypher query statement.
8. A Cypher query statement generation and optimization device based on a large language model, characterized in that: The device includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the processor to execute the Cypher query statement generation optimization method based on a large language model described in any one of claims 1-7.
9. A Cypher query statement generation optimization system based on a large language model, applying the Cypher query statement generation optimization method based on a large language model as described in any one of claims 1 to 7, characterized in that: The system includes an intention recognition module, a relationship link preliminary screening module, a relationship link secondary screening module and a query statement generation optimization module, wherein: The intention recognition module is used to: perform intention recognition on the original question raised by the user in combination with the large language model; The relationship link preliminary screening module is used to: extract key entity types using a large language model based on the result of intent recognition, and preliminarily screen out possible relationship links by combining the key entity types with all relationship links calculated based on the entity type and relationship type information in the graph database schema; The relationship link re-screening module is used to: use a large language model to perform semantic analysis on the relationship links initially screened, and screen out N relationship links that best match the results after intent recognition from multiple possible relationship links; The query statement generation optimization module is used to: generate corresponding Cypher query statements using a large language model according to N relationship links and the results after intent recognition, and optimize the generated Cypher query statements using the large language model.
10. A non-volatile computer storage medium, characterized in that: The computer storage medium stores computer executable instructions, which are executed by one or more processors to complete the Cypher query statement generation optimization method based on a large language model as described in any one of claims 1-7.
Citation Information
Cited By
Test case self-correction Text-to-SQL method and system and medium
CN120256320A
Atmospheric pollution process analysis method, electronic equipment and storage medium
CN120821747A
Database natural language query autoregression intention classification method and system
CN120995178A
Mode linking method and device, electronic equipment and storage medium
CN121560899A
Intelligent power distribution network data retrieval method based on double-helix question and answer system
CN122364380A