Large language model Cypher generation method and system fusing graph mode selection and reinforcement learning
By integrating graph pattern selection and reinforcement learning into a large language model Cypher generation method, the problem of steep Cypher learning curves is solved, user-friendly graph database queries are achieved, and data analysis efficiency and accuracy are improved.
Patent Information
- Application Number
- CN202511267564.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-05
- Publication Date
- 2025-12-16
AI Technical Summary
The existing graph database query language Cypher has a steep learning curve, and its complex syntax rules limit ordinary users' ability to perform in-depth queries and analyses of graph data, thus restricting the widespread adoption and application of graph database technology.
A Cypher generation method for large language models, which integrates graph pattern selection and reinforcement learning, is proposed. By constructing training datasets from multiple graph databases, reinforcement learning is performed to obtain natural language queries and select relevant subgraph patterns. After verification, Cypher queries are generated. A graph pattern selection module is introduced and integrated into the reinforcement learning training framework to improve the model's performance in Cypher generation tasks.
By effectively aligning large language models and graph patterns, Cypher's generation accuracy and robustness are improved, the difficulty of generating complex queries is reduced, and the efficiency of users' operations on graph databases and data-driven decision-making is enhanced.
Smart Images

Figure CN121146071A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application belongs to the technical field of graph database and large model, and relates to a large language model Cypher generation method and system fusing graph pattern selection and reinforcement learning. BACKGROUND
[0002] In the current era of unprecedented information explosion, the network structure between data is becoming increasingly complex, which makes the long-standing data processing paradigm appear to be inadequate in terms of efficient analysis and management. With the continuous evolution of information technology, the volume and complexity of data have increased simultaneously, posing a severe challenge to existing data management and analysis systems. Those well-known traditional methods, such as relational databases and their supporting analysis tools, often reveal their limitations when faced with such data with deep correlations and dynamic evolution characteristics.
[0003] The graph data structure exhibits unparalleled value in many frontier fields due to its ability to intuitively depict complex interaction relationships between entities. The graph model, with its excellent expressiveness and high flexibility, can represent and process complex entity networks in a very natural way. A typical example is that the vast amount of information on the current Internet is organized in various knowledge graphs, which not only record a large amount of entity information, but more importantly, reveal the intricate connections between them. In social media analysis, graph-based analysis methods can reveal deep patterns of user interaction and community formation, providing powerful tools for businesses and researchers to understand user behavior. Similarly, in the fields of bioinformatics, financial risk control, and intelligent transportation, the high correlation and dynamics of data are their core characteristics, and the graph data model can precisely capture and effectively utilize these characteristics.
[0004] All these application developments collectively confirm a core point: in response to the challenges brought about by today's highly interconnected data ecosystem, the graph-based methodology plays a crucial role. The deep integration of the Internet of Things and the Internet makes the correlation between data increasingly close, forming a vast network of information. The graph methodology is a powerful tool for managing this complexity, as it can help us uncover deeper insights and provide more efficient data governance solutions. However, despite the theoretical advantages of graph data models and graph databases, how to fully convert their potential into productivity in practical applications remains a daunting task.
[0005] In order to efficiently operate graph data, the industry has developed specialized graph database technology, which provides a powerful solution for storing and querying complex and highly correlated information. Graph databases can quickly process and analyze large-scale graphs through their flexible model and high-performance query engine. However, the learning curve of the mainstream query language Cypher is quite steep, and the complex syntax rules set a high technical barrier for users who want to convert natural language instructions into database operations, especially for those without programming background. Although Cypher is powerful, its rigorous and complex query structure undoubtedly limits the popularity and application of graph database technology in a wider range.
[0006] Therefore, there is an urgent need for an innovative technical path, namely the development of a generation method that can automatically convert human natural language queries into standard Cypher query code. Such a method will become a bridge between ordinary users and powerful graph databases, supporting more efficient and intuitive data exploration. The core challenge is that the model not only needs to have a deep natural language understanding ability, but also must be able to accurately map the user's intent to the query logic of the graph database. Once achieved, users can easily perform deep queries and analysis on graph data without mastering the cumbersome syntax of Cypher, thereby truly releasing the potential of graph databases and greatly improving the efficiency and breadth of data-driven decision-making. SUMMARY
[0007] The present application provides a large language model Cypher generation method and system that combines graph pattern selection and reinforcement learning to solve the technical problems mentioned above, specifically using the following technical solutions:
[0008] In a first aspect, a large language model Cypher generation method that combines graph pattern selection and reinforcement learning includes the following steps:
[0009] A training data set containing multiple graph databases is constructed, each graph database type corresponds to a graph pattern composed of node type, relationship type and attribute constraint information; the training data set includes natural language queries, corresponding subgraph patterns and Cypher queries, the subgraph pattern is the smallest subset selected from the complete graph pattern data that is most relevant to the natural language query;
[0010] The large language model is trained with reinforcement learning that combines graph pattern selection;
[0011] Obtain the user input natural language query and the corresponding graph pattern data, instruct the trained large language model to select the subgraph pattern related to the user input natural language query from the complete graph pattern data, and perform rule verification on the selected subgraph pattern to determine the final selected graph pattern;
[0012] With the selected graph schema and the user input natural language query as the prompt, the corresponding Cypher query is generated by the large language model after training.
[0013] Further, the process of reinforcement learning training of the large language model combined with the selection of the graph schema includes:
[0014] The two-stage inference process of the large language model is defined: the first stage takes the natural language query and the simplified graph schema data as input, instructs the large language model to select a subgraph schema from the simplified graph schema data, and if the selected subgraph schema fails the rule verification, it falls back to the simplified graph schema to obtain the final selected graph schema; the second stage takes the natural language query and the selected graph schema as the prompt to guide the large language model to generate a Cypher query;
[0015] According to the defined two-stage inference process, the large language model is trained by reinforcement learning.
[0016] Further, the reinforcement learning process is represented as:
[0017]
[0018] Among them, represents the loss function, represents the training data set, represents the input natural language query and simplified graph schema, represents the Cypher query label, represents the Cypher query generated based on the new strategy i-th sampling, represents the number of samplings, represents the old strategy, represents the new strategy, is a hyperparameter, is the KL divergence, represents the reference model, represents the expectation, represents the number of tokens generated by the large language model, represents the new strategy probability, represents the old strategy probability, represents the advantage of the i-th generated Cypher query, represents the clip function, represents the hyperparameter, represents the mask of the i-th sampling t-th token, represents the reward corresponding to the i-th code, which includes the sum of the format reward, the graph schema selection reward, and the Cypher execution reward.
[0019] Furthermore, the simplified graph pattern in the first stage is obtained by deleting the attribute constraint information from the complete graph pattern data, and the graph pattern in the prompt words in the second stage restores the attribute constraint information.
[0020] Furthermore, rule validation refers to verifying whether the node type and relation type of the selected subgraph pattern are a subset of the simplified graph pattern.
[0021] Furthermore, the three reward mechanisms are represented as follows:
[0022]
[0023]
[0024]
[0025] in, For format rewards, Select rewards for map mode. Execute rewards for Cypher.
[0026] Furthermore, various graph databases include movie recommendation data, football data, art data, and biological data.
[0027] Secondly, this invention proposes a Cypher generation system for large language models that integrates graph pattern selection and reinforcement learning, to implement the aforementioned Cypher generation method for large language models.
[0028] The beneficial effects of this invention are:
[0029] The Cypher generation method and system for large language models that integrates graph pattern selection and reinforcement learning provided by this invention effectively aligns the large language model and graph patterns by training the large language model with reinforcement learning data from multiple graph databases to better adapt it to the Cypher generation task.
[0030] This invention introduces a graph pattern selection module and integrates the graph pattern selection process into the reinforcement learning training framework. It enables the selection of relevant subgraph patterns from the complete graph pattern based on the user's question, and verifies the subgraph patterns. Finally, it generates a Cypher based on the subgraph patterns and the question, thereby improving the model's performance in the Cypher generation task. Attached Figure Description
[0031] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0032] Figure 1 This is a schematic diagram of the Cypher generation method and system for large language models based on fusion graph pattern selection and reinforcement learning proposed in this application.
[0033] Figure 2 This is a schematic diagram of the reinforcement learning training process for selecting the fusion graph pattern in this application. Detailed Implementation
[0034] Embodiments of the present invention are described in detail below. Examples of these embodiments are illustrated in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and intended to explain the present invention, and should not be construed as limiting the present invention.
[0035] It is understood that the accompanying drawings are merely illustrative of the invention and are not necessarily drawn to scale. Some of the block diagrams shown in the drawings are functional entities and do not necessarily correspond to physically or logically independent entities. These functional entities can be implemented in software, in one or more hardware modules or integrated circuits, or in different network and / or processor devices and / or microcontroller devices.
[0036] It is understood that the flowchart shown in the attached diagram is merely an illustrative example and does not necessarily include all steps. For example, some steps may be broken down, while others may be combined or partially combined; therefore, the actual execution order may change depending on the specific circumstances.
[0037] like Figure 1 The diagram illustrates a Cypher generation method and system for a large language model that integrates graph pattern selection and reinforcement learning, comprising the following steps: S1: Constructing a dataset containing training data from multiple graph databases. S2: Training the large language model using reinforcement learning that integrates graph pattern selection. S3: Obtaining the user-input question and graph database patterns. S4: Selecting relevant sub-graph patterns from the complete graph patterns based on the user question and validating them. S5: The large language model generates corresponding Cypher queries using the selected patterns and the given question.
[0038] The core idea of this invention is to enhance the ability of a large language model to generate Cyphers by training a large model through reinforcement learning. Through the above steps, the large language model Cypher generation method and system of this application, which integrates graph pattern selection and reinforcement learning, enables the large language model to better adapt to the Cypher generation task through reinforcement learning training on various graph data, effectively aligning the large language model with the graph patterns. The method is described in detail below.
[0039] For step S1: Constructing a dataset, which contains training data from various graph databases.
[0040] First, datasets with different graph patterns were acquired, including movie recommendation data, football data, art data, and biological data, each with a different graph pattern. Each dataset contained information on node types, relationship types, and attribute constraints under different graph patterns. The resulting datasets with multiple graph patterns were used for supervised fine-tuning of a large language model.
[0041] It should be noted that: movie recommendation data describes the interaction relationship between users, movies, and actors; football data describes the relationship between players, clubs, and awards; art data describes the relationship between figures, paintings, sculptures, and genres; and biological data describes the relationship between taxonomic units, taxonomic levels, habitats, and conservation status.
[0042] Each graph schema dataset contains complete node types (representing entity categories), relationship types (defining the connection methods between nodes), and attribute constraints (limiting the attribute fields of nodes / relationships).
[0043] The data node types for movie recommendations include user nodes, movie nodes, and actor nodes. Relationship types include user-rated movie relationships, actor-starring movie relationships, and movie-directed-by-a-director relationships. Attribute constraints include, for example, user nodes must have a user ID and user name; movie nodes must have a movie ID and movie name and release year; actor nodes must have an actor ID and name and nationality attributes; user-rated movie relationships have a rating attribute; actor-starring movie relationships have a character name attribute; and movie-directed-by-a-director relationships have a character name attribute.
[0044] Football data descriptions use players, clubs, positions, awards, stadiums, and leagues as core node types, covering players, clubs, and their interactions. Node types include: player nodes, club nodes, position nodes, etc. Relationship types include:
[0045] Players are assigned to clubs, play positions, and receive awards. Attribute constraints include player nodes, which must have a player ID, as well as name, date of birth, place of birth, gender, height, and weight. Club nodes must have a club ID, as well as attributes such as the year the club was founded, its country of origin, current head coach, and owner.
[0046] Art data descriptions use figures, paintings, sculptures, genres, art movements, and museums as core node types, covering artworks, artists, and their interactions. Node types include: figure nodes, painting nodes, sculpture nodes, etc. Relationship types include: paintings created by figures, sculptures created by figures, paintings belonging to art movements, sculptures belonging to art movements, paintings belonging to genres, sculptures belonging to genres, paintings exhibited in museums, sculptures exhibited in museums, etc. Attribute constraints include, for example, figure nodes must have a figure ID, as well as birthplace, gender, birth date, and death date; painting and sculpture nodes must have an artwork ID, as well as country of creation and year of creation; art movement nodes include start and end year attributes.
[0047] Biological data descriptions use taxonomic units, taxonomic ranks, habitats, and conservation status as core node types, covering species and their ecological relationships. Node types include: taxonomic unit nodes, taxonomic rank nodes, and habitat nodes. Relationship types include: taxonomic units have taxonomic ranks, taxonomic units have parent taxonomic units, taxonomic units inhabit habitats, taxonomic units feed on other taxonomic units, and taxonomic units have conservation status. Attribute constraints include, for example, taxonomic unit nodes must have a taxonomic unit name, as well as diurnal activity cycles, average gestation periods, and maximum lifespans. Taxonomic rank nodes, habitat nodes, and conservation status nodes serve as supplementary attribute nodes for taxonomic and ecological information.
[0048] In the embodiments of this application, the constructed dataset of multiple graph patterns includes Cypher generated data and graph pattern selection data. The graph pattern selection data includes natural language queries and the subgraph patterns required by those queries. The Cypher generated data includes natural language queries and the corresponding Cypher queries.
[0049] In this step, dataset construction is the core of the method, and its quality and diversity directly impact the overall performance of the large language model. By aggregating graph database pattern information from different business domains, the model's adaptability and stability in diverse application environments can be improved. The process of cleaning and preprocessing the raw data aims to remove interfering information and contradictory records, thereby improving data purity and further enhancing the model's training effect and predictive performance.
[0050] For step S2: Perform reinforcement learning training on the large language model that incorporates graph pattern selection.
[0051] Inspired by recent advances in reinforcement learning to improve model reasoning and generalization capabilities, this method reimagines the traditional text-to-Cypher task.
[0052] Unlike traditional supervised fine-tuning methods, the core innovation of this invention lies in seamlessly integrating graph pattern selection into the Cypher generation process, treating it as a key inference step. Within this framework, the model no longer passively learns independent data pairs, but rather actively learns how to reason and select within complex full-graph patterns based on the user's natural language queries through reinforcement learning.
[0053] During training, the graph pattern is first simplified by removing attribute constraints from the complete graph pattern, retaining only node type and relation type information. Using a natural language query and the simplified graph pattern as input, the large model is instructed to first select a subgraph pattern from the input graph pattern before generating a Cypher query. During subgraph pattern selection, it's necessary to determine if the selected subgraph pattern is correct and can pass rule verification. Correctness refers to whether the node and relation types of the selected subgraph pattern are consistent with the standard subgraph patterns in the training set; if they are, it's correct, otherwise it's incorrect. Rule verification refers to whether the node and relation types of the selected subgraph pattern are a subset of the node and relation types in the simplified graph pattern; subgraph patterns that cannot pass rule verification are marked. For cases that cannot pass rule verification, the selected subgraph pattern is replaced with the simplified graph pattern and attribute constraint information is added before being used in the next Cypher query generation. In other cases, attribute constraint information is directly added to the selected subgraph pattern (whether correct or incorrect) before being used in the next Cypher query generation.
[0054] The reinforcement learning process is represented as:
[0055]
[0056] in, Represents the loss function. Represents the training dataset. The natural language query and simplified graph pattern represent the input. Represents Cypher query tags. This represents the Cypher query code based on the i-th sample using the new strategy. Represents the number of samples. Representing the old strategy, Represents a new strategy. It's a hyperparameter. It is the KL divergence. Representative reference model, Represents expectations, This represents the number of lexical units generated by the large model. Represents the probability of the new strategy. Represents the probability of the old strategy. This represents the advantage corresponding to the i-th Cypher query code. Represents the clip function. Represents hyperparameters, This represents the mask for the t-th word sampled in the i-th sampling. This represents the reward corresponding to the i-th code. In this embodiment, the reward includes the sum of three parts: format reward, graph pattern selection reward, and Cypher execution reward. Specifically, as shown... Figure 2 As shown, the graph pattern selection mechanism is dynamically guided by the input natural language query and simplified graph pattern data, and its selection results recursively influence and shape the subsequent reasoning process. This means that the model first infers and determines the minimum and most relevant subgraph pattern needed to answer the user's question, and then generates the final Cypher query based on the content of this precisely selected subgraph pattern. This process breaks down the complex generation task into a coherent step of "reasoning and selection first, then encoding and generation," greatly reducing the difficulty of directly generating complex queries.
[0057] This invention designs three reward mechanisms for training reinforcement learning: format reward, graph pattern selection reward, and Cypher execution reward. The three reward mechanisms are represented as follows:
[0058]
[0059]
[0060]
[0061] in, For format rewards, Select rewards for map mode. Execute rewards for Cypher.
[0062] This reinforcement learning paradigm, which internalizes pattern selection into the reasoning process, offers significant advantages. It forces the model to understand more deeply the structural and attribute constraints of graph patterns, such as node types and relationship types, rather than simply memorizing patterns from the training data. By focusing on directly related subgraph patterns, the model effectively avoids interference from irrelevant information, significantly improving the accuracy and robustness of generated queries. Through learning a large number of natural language queries and corresponding subgraph patterns and Cypher query pairs, the model gradually masters the mapping relationship from natural language to Cypher. With a large number of training samples, the model can better understand the semantics of user queries and transform them into Cypher queries that conform to graph database patterns. This helps improve the model's understanding of user questions, thus generating more accurate queries. The model learns Cypher query generation methods under different graph patterns, enabling it to better adapt to various graph database patterns and improve the generality and adaptability of generated queries.
[0063] For step S3: Obtain the natural language query and graph pattern input by the user.
[0064] Obtaining user input questions and graph patterns is the starting point for the entire system's interaction with the user. User questions may contain various natural language expressions, while graph patterns define the structure and relationships of the data. Accurately obtaining and understanding this information is a prerequisite for generating correct Cypher queries.
[0065] For step S4: Select relevant subgraph patterns from the complete graph pattern using natural language queries and verify them.
[0066] Before generating a Cypher query, the system performs a crucial graph pattern filtering step. At this stage, it doesn't need to know the attribute constraints of the graph pattern; it only infers based on node and relationship types. This includes selecting relevant subgraph patterns from a simplified graph pattern based on the user's question and performing rule validation. Rule validation determines whether the selected subgraph pattern is a subset of the node and relationship types in the simplified graph pattern. If not, it reverts to the simplified graph pattern and the selected subgraph pattern is no longer used.
[0067] There are two main reasons for selecting the mapping pattern:
[0068] Improving accuracy and avoiding information overload: Directly inputting the entire graph schema into a large language model (LLM) results in information redundancy. This is like giving the model a thick dictionary but only asking it to find one word. Too many irrelevant details distract the model, making it difficult to identify truly relevant information. By filtering, the model's focus can be concentrated on the schema components most relevant to the user's question, thus more accurately mapping natural language queries to specific nodes and relational edges in the graph schema. This not only improves the accuracy of Cypher generation but also effectively alleviates the ambiguity of natural language queries themselves, making the generated queries closer to the user's intent.
[0069] Improving computational efficiency: Many advanced code generation methods, such as multiple generation, self-reflection, or pre-planning, require repeated calls to large language models. If the complete pattern is used as input each time, the excessively long context leads to significant computational overhead. Graph pattern filtering can effectively shorten the length of the input context, thereby significantly improving computational efficiency when calling the model multiple times.
[0070] While large language models excel at handling complex language tasks, they don't inherently "understand" the constraints of specific graph patterns. This can lead to the model developing an "illusion"—that it picks subgraph patterns that deviate from the original pattern. For example, it might assign a non-existent node or edge attribute, or generate incorrect edge directions. Even small deviations can severely impact the generation of subsequent Cypher queries.
[0071] Therefore, to ensure the accuracy of the selected pattern, the method of this invention adds a verification step. If the selected pattern fails to meet the constraints of the original graph pattern, the system discards the problematic sub-pattern and uses the complete graph pattern instead. This verification step is crucial; it ensures that the pattern used by the model to generate Cypher queries is accurate and reliable, thereby avoiding the generation of invalid queries inconsistent with the graph pattern.
[0072] For step S5: The large language model generates the corresponding Cypher query using the selected pattern and the given question.
[0073] After graph pattern selection, relevant subgraph patterns, supplemented with relation type information, are identified and included in the prompts. For patterns that do not meet the criteria, simplified graph patterns are added with relation type information and then identified and included in the prompts. Specifically, patterns irrelevant to the query are removed from the subgraph patterns, thus simplifying the prompts. With the selected patterns, the prompts are greatly simplified, and the number of words in the prompts can be significantly reduced. Finally, the prompts, composed of the user's natural language query and the subgraph patterns, will be used by the large language model to generate Cypher queries.
[0074] Generating Cypher queries is the ultimate goal of this methodology. Through the preceding steps, the model has obtained subgraph patterns relevant to the user's problem, and these patterns have been validated to ensure consistency with the original graph patterns. Cypher queries generated based on this foundation are more likely to be accurate and effective.
[0075] Based on the same inventive concept, this application also proposes a Cypher generation system for large language models that integrates graph pattern selection and reinforcement learning, comprising:
[0076] A dataset building module is used to build a training dataset, which includes training data from various graph databases;
[0077] The reinforcement learning module is used to train large language models using reinforcement learning.
[0078] The acquisition module acquires the natural language query input by the user and the corresponding graph database schema;
[0079] The subgraph pattern selection and verification module is used to select subgraph patterns related to the natural language query input by the user from the complete graph pattern, verify the subgraph patterns, and determine the final selected graph pattern.
[0080] The Cypher generation module uses the selected graph pattern and the user-input natural language query as prompt words to generate corresponding Cypher queries through a large language model trained by reinforcement learning.
[0081] For the system embodiments, since they basically correspond to the method embodiments, relevant details can be found in the descriptions of the method embodiments; the implementation methods of the remaining modules will not be repeated here. The system embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of the present invention according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0082] The system embodiments of the present invention can be applied to any device with data processing capabilities, such as a computer or other similar device. The system embodiments can be implemented in software, hardware, or a combination of both. Taking software implementation as an example, as a logical device, it is formed by the processor of any data processing device loading the corresponding computer program instructions from non-volatile memory into memory for execution.
[0083] To demonstrate the effectiveness of the present invention, this embodiment uses data from different graph databases for reinforcement learning training and tests it on multiple datasets to show the versatility of the proposed method.
[0084] This invention utilizes CypherBench to evaluate the conversion effect from natural language to Cypher queries. Specifically,
[0085] CypherBench includes seven different attribute graphs covering areas such as fictional characters, companies, flight accidents, geography, movies, NBA, and politics. To ensure fair comparison, the evaluation criteria from previous benchmark tests are used. This embodiment primarily measures model performance using the following metrics:
[0086] Execution accuracy: Evaluates whether the results of the predicted query, after being executed in the database, are consistent with the results produced by the actual query.
[0087] Subgraph Jaccard Similarity: A subgraph is defined as a subgraph that quantifies the similarity of queries by calculating the Jaccard similarity between the subgraphs of the generated query and the actual query, by executing the MATCH clause and combining the graph parts detected by RETURN *.
[0088] Executable percentage: Evaluates whether the generated Cypher query can be successfully executed in the graph database without compiler errors.
[0089] It was compared with a range of benchmark methods, which can be divided into two categories:
[0090] Common methods include Qwen-2.5-Coder-7B, Llama3.1-8B, Gemma-2-9B, GPT-4o-mini, Gemini-1.5-Pro, and GPT-4o.
[0091] Text-to-Cypher methods include R^3 NLGQL and Text2Cypher-Gemma-3-27B.
[0092] The method of this invention is obtained by training on the large model Text2Cypher-Gemma-3-27B.
[0093]
[0094] Table 1 lists the performance of the proposed method and baseline methods on CypherBench. It can be seen that the method of this invention exhibits state-of-the-art performance in key aspects of Cypher query generation, achieving an execution accuracy of 69.59%. This result significantly outperforms other models, especially the robust GPT-4o model (60.18%), by 9.41 percentage points.
[0095] Furthermore, the method of this invention sets a new benchmark in generating syntactically correct queries, achieving an executable percentage of 99.28%. This near-perfect ability to generate executable queries ranks first among all evaluated models, surpassing GPT-4o, which fully demonstrates the high reliability and robustness of the method of this invention.
[0096] The foregoing has shown and described the basic principles, main features, and advantages of the present invention. Those skilled in the art should understand that the above embodiments do not limit the present invention in any way, and all technical solutions obtained by equivalent substitution or equivalent transformation fall within the protection scope of the present invention.
Claims
1. A Cypher generation method for large language models that integrates graph pattern selection and reinforcement learning, characterized in that, Includes the following steps: Construct a training dataset containing multiple graph databases, each graph database type corresponding to a graph pattern, which consists of node type, relation type, and attribute constraint information; The training dataset includes natural language queries, corresponding subgraph patterns, and Cypher queries. The subgraph patterns are the smallest subsets selected from the complete graph pattern data that are most relevant to the natural language queries. Large language models were trained using reinforcement learning that incorporates graph pattern selection. The system obtains the natural language query input by the user and the corresponding graph pattern data, instructs the trained large language model to select subgraph patterns related to the natural language query input by the user from the complete graph pattern data, performs rule verification on the selected subgraph patterns, and determines the final selected graph pattern. Using the selected graph pattern and the user's input natural language query as prompt words, the trained large language model generates the corresponding Cypher query.
2. The Cypher generation method for large language models based on fusion graph pattern selection and reinforcement learning as described in claim 1, characterized in that, The process of training a large language model using reinforcement learning that incorporates graph pattern selection includes: The two-stage reasoning process of the large language model is defined as follows: The first stage takes natural language queries and simplified graph pattern data as input, instructing the large language model to select subgraph patterns from the simplified graph pattern data. If the rule verification of the selected subgraph pattern fails, it falls back to the simplified graph pattern to obtain the final selected graph pattern; The second stage uses natural language queries and the selected graph pattern as prompt words to guide the large language model to generate Cypher queries. The large language model is trained using reinforcement learning based on the defined two-stage reasoning process.
3. The Cypher generation method for large language models based on fusion graph pattern selection and reinforcement learning according to claim 2, characterized in that, The reinforcement learning process is represented as: ; in, Represents the loss function. Represents the training dataset. The natural language query and simplified graph pattern represent the input. Represents Cypher query tags. This represents the Cypher query generated based on the i-th sampling of the new strategy. Represents the number of samples. Representing the old strategy, Represents a new strategy. It's a hyperparameter. It is the KL divergence. Representative reference model, Represents expectations, This represents the number of lexical units generated by the large language model. Represents the probability of the new strategy. Represents the probability of the old strategy. This represents the advantage corresponding to the i-th generated Cypher query. Represents the clip function. Represents hyperparameters, This represents the mask for the t-th word sampled in the i-th sampling. This represents the reward corresponding to the i-th code, which includes the sum of three parts: format reward, graph pattern selection reward, and Cypher execution reward.
4. The Cypher generation method for large language models based on fusion graph pattern selection and reinforcement learning according to claim 3, characterized in that, The simplified graph pattern in the first stage is obtained by removing the attribute constraint information from the complete graph pattern data, while the graph pattern in the prompt words in the second stage restores the attribute constraint information.
5. The Cypher generation method for large language models based on fusion graph pattern selection and reinforcement learning according to claim 3, characterized in that, Rule validation refers to verifying whether the node type and relation type of the selected subgraph pattern are a subset of the simplified graph pattern.
6. The Cypher generation method for large language models based on fusion graph pattern selection and reinforcement learning according to claim 3, characterized in that, The three reward mechanisms are represented as follows: ; ; ; in, For format rewards, Select rewards for map mode. Execute rewards for Cypher.
7. The Cypher generation method for large language models based on fusion graph pattern selection and reinforcement learning according to claim 1, characterized in that, Multiple graph databases include movie recommendation data, football data, art data, and biological data.
8. A Cypher generation system for large language models that integrates graph pattern selection and reinforcement learning, used to implement the Cypher generation method for large language models as described in claim 1, characterized in that, The system includes: A dataset building module is used to build a training dataset, which includes training data from various graph databases; The reinforcement learning module is used to train large language models using reinforcement learning. The acquisition module acquires the natural language query input by the user and the corresponding graph database schema; The subgraph pattern selection and verification module is used to select subgraph patterns related to the natural language query input by the user from the complete graph pattern, verify the subgraph patterns, and determine the final selected graph pattern. The Cypher generation module uses the selected graph pattern and the user-input natural language query as prompt words to generate corresponding Cypher queries through a trained large language model.
Citation Information
Cited By
Large language model graph query language generation method and system based on reinforcement learning
CN121681720A
