Cypher generation method and system based on directional control mode
Through the Schema design and model fine-tuning of the directional control mode, the existing Text2Cypher technology has been solved in terms of accuracy and efficiency, and the efficient and accurate natural language to Cypher query conversion in the Neo4j graph database is realized.
Patent Information
- Application Number
- CN202510620706.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-14
- Publication Date
- 2025-08-29
AI Technical Summary
The existing Text2Cypher technology is inaccurate when dealing with complex semantics and cross-domain texts, generates incorrect Cypher queries, and is inefficient, making it difficult to adapt to text conversion requirements in different fields.
Using directional control mode, through data preprocessing, schema design, prompt word design and model fine-tuning, Cypher statements that conform to Neo4j query syntax are generated to ensure that the generated Cypher query statements are consistent with the content of the graph database, and dynamically generate the minimum query space to improve efficiency.
It realizes the accuracy and efficiency of Cypher queries, adapts to text conversion requirements in different fields, and ensures that the generated Cypher statements can accurately match the graph database data, taking into account performance and query requirements.
Smart Images

Figure CN120561352A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the fields of natural language processing and graph database query, and in particular to a Cypher generation method and system based on a directional control mode. Background Art
[0002] In the application of knowledge graphs and graph databases, Cypher, a declarative language for querying and manipulating graph databases, uses graph structures to describe entity relationships, providing an effective way to express complex knowledge and promoting the widespread use of graph databases. However, the traditional Cypher query language is highly specialized and difficult for ordinary users to master, which greatly limits the popularity and widespread application of graph databases. Therefore, the need to convert natural language into Cypher query statements is becoming increasingly urgent. Text2Cypher technology emerged amid the rapid development of knowledge graphs and graph databases, aiming to meet the demand for efficient graph data queries. Recent advances in Large Language Models (LLMs) have played a key role in driving the development of Text2Cypher. LLMs, with their powerful language understanding and generation capabilities, are well-suited to processing complex natural language. Furthermore, through hint engineering, they can quickly and accurately convert natural language into Cypher queries, significantly improving conversion efficiency and accuracy, making it easy for even non-experts to perform natural language to database query conversion. Currently, there are many technical solutions for implementing Text2Cypher. For example, the existing patent 1, "text2cypher system, method, device, processor and medium for implementing financial question and answer based on heuristic rules", uses the heuristic rule engine module to parse phrases and words, combines the Cypher query generator to generate Match units, Where units and Return units, and combines them with the results of the text parsing module to form a complete Cypher query statement; Patent 2, "A personalized TCM consultation system based on a large TCM model and multiple knowledge bases", extracts relevant TCM entities and relationships from the question based on the "large TCM model", and converts them into Cypher query statements for the graph database; Patent 3, "A personalized TCM consultation system based on a large TCM model and multiple knowledge bases", extracts relevant TCM entities and relationships from the question based on the "large TCM model", and converts them into Cypher query statements for the graph database; Patent 4, "A personalized TCM consultation system based on a large TCM model and multiple knowledge bases", extracts relevant TCM entities and relationships from the question based on the "large TCM model", and converts them into Cypher query statements for the graph database; Patent 5, "A personalized TCM consultation system based on a large TCM model and multiple knowledge bases", extracts relevant TCM entities and relationships from the question based on the "large TCM model", and converts them into Cypher query statements for the graph database; Patent 6, "A personalized TCM consultation system based on a large TCM model and multiple knowledge bases", extracts relevant TCM entities and relationships from the question based on the "large TCM model", and converts them into Cypher query statements for the graph database; Patent 7, "A personalized TCM consultation system based on a large TCM model and multiple knowledge bases", extracts relevant TCM entities and relationships from the question based on the "large TCM model", and converts them into Cypher query statements for the graph database; Patent 8, "A personalized TCM consultation system based on a large TCM model and multiple knowledge bases", extracts relevant TCM entities and relationships from the question based on the "large TCM model", and converts them into Cypher query statements for the graph database Patent 4, "An improved NL2Cypher generation method and system based on a generative pre-trained model," uses the LoRA method to fine-tune the generative pre-trained model, concatenates the prompt word template with the query question set to obtain the final prompt word, inputs the final prompt word into the fine-tuned generative pre-trained model, outputs several Cypher query statement results, and obtains the Cypher query statement with the highest score after scoring. Shortcomings of the prior art and objects of the present invention Existing technologies have numerous shortcomings. Patent 1 employs a traditional Text2Cypher method that lacks accuracy. When processing complex semantics and ambiguous expressions, it often generates erroneous or inaccurate Cypher queries, resulting in an inability to retrieve correct data from the graph database. Furthermore, the method's fixed templates make it less flexible and scalable. Patent 2's method can only be used in the field of Traditional Chinese Medicine diagnosis. Because professional texts in different fields have unique terminology, language styles, and semantic rules, this natural language-to-Cypher technology lacks sufficient flexibility and adaptability, making it difficult to effectively process cross-domain texts. Patent 3's method is expensive to construct and works well for small-scale text processing. However, when faced with large-scale text data, the process of constructing an abstract syntax tree is extremely time-consuming and resource-intensive, resulting in inefficient Cypher query statements. Patent 4 utilizes a fine-tuned generative model and prompt word technology to generate Cypher statements, but lacks effective Schemas control, resulting in the output of multiple, uncertain Cypher query statements with relatively low accuracy and efficiency. In view of the shortcomings of the above-mentioned prior art, the present invention provides a Cypher generation method and system based on a directional control mode, aiming to improve the accuracy and efficiency of converting natural language to Cypher query statements, enhance the versatility of the method, and enable it to adapt to the text conversion needs of different fields.
[0003] Therefore, those skilled in the art provide a Cypher generation method and system based on a directional control mode to solve the problems raised in the above background technology. Summary of the Invention
[0004] To solve the above technical problems, the present invention provides a Cypher generation method based on a directional control mode, comprising the following steps: Step 1: Data preprocessing steps: Obtain text containing Neo4j graph database data and user input data, use the Jieba word segmentation tool to segment the user input data, and then clean the segmented data by removing stop words and special symbols, performing uppercase and lowercase conversion, and removing meaningless words; Step 2: Directional control mode design steps: Schema design: Based on the word segmentation of the user input content, the target graph database is searched for multiple parent-child nodes, relationships, and attribute information contained in the word segmentation, and the minimum query space is generated as the schema content. The schema content contains node information, relationship information, and feature information of nodes and relationships in the target graph database; Prompt word design: Design prompt words to instruct the large language model to generate Cypher query statements that can answer user questions based on the given schema. The node names and relationship names that appear in the generated Cypher statements must be included in the schema content. The Cypher return value should include all node labels that appear in the match. Use the entity name in the user's question as the node name to perform a bidirectional query on the parent and child nodes of the node. The direction of the relationship between the nodes in the match must be included in the schema content. Model fine-tuning: Based on the schema and prompt words, fine-tune some parameters of the large language model; Generate Cypher: After the above steps, generate Cypher statements that conform to Neo4j query syntax and user requirements; Step 3: Graph database query step: Query the Neo4j graph database based on the generated Cypher statement.
[0005] Preferably, in the data preprocessing step, the removal of stop words is to remove the words in the text that match the stop word list; the removal of special symbols is to use regular expressions or simple character matching methods to remove special symbols, punctuation marks and numbers in the text; the case conversion is to convert all texts into lowercase form; the removal of meaningless words is to remove spaces and garbled characters in the text through manual screening or automatic program screening.
[0006] Preferably, in the Schema design, the number of the multiple parent-child nodes, relationships, and attribute information is 5.
[0007] Preferably, in the model fine-tuning step, taking the large language model deepseek-v3671B as an example, the stream parameter is set to False and the temperature parameter is set to 0.7.
[0008] A Cypher generation system based on a directional control mode, including Data preprocessing module: used to obtain text containing Neo4j graph database data and user input data, use the Jieba word segmentation tool to segment the user input data, and then clean the segmented data by removing stop words and special symbols, performing uppercase and lowercase conversion, and removing meaningless words; Directional control mode design module: Schema design unit: used to query the target graph database for multiple parent-child nodes, relationships, and attribute information contained in the word segmentation of the user input content, and generate a minimum query space as the schema content. The schema content includes node information, relationship information, and feature information of nodes and relationships in the target graph database; Prompt word design unit: This unit is used to design prompt words. These prompt words are used to instruct the large language model to generate Cypher query statements that can answer user questions based on the given schema. The node names and relationship names that appear in the generated Cypher query statements must be included in the schema content. The Cypher return value should include all node labels that appear in the match. The parent and child nodes of the node are bidirectionally searched using the entity name in the user's question as the node name. The direction of the relationship between the nodes in the match must be included in the schema content. Model fine-tuning unit: used to fine-tune some parameters of the large language model based on the schema and prompt words; Generate Cypher unit: After the above steps, it is used to generate Cypher statements that conform to Neo4j query syntax and user requirements; Graph database query module: used to query the Neo4j graph database based on the generated Cypher statement.
[0009] Preferably, in the data preprocessing module, the removal of stop words is to remove the words in the text that are consistent with the stop word list; the removal of special symbols is to use regular expressions or simple character matching methods to remove special symbols, punctuation marks and numbers in the text; the case conversion is to convert all texts into lowercase form; the removal of meaningless words is to remove spaces and garbled characters in the text through manual screening or automatic program screening.
[0010] Preferably, in the Schema design unit, the number of the multiple parent-child nodes, relationships, and attribute information is 5.
[0011] Preferably, in the model fine-tuning unit, taking the large language model deepseek-v3671B as an example, the stream parameter is set to False and the temperature parameter is set to 0.7.
[0012] Technical effects and advantages of the present invention: The design of the directional control mode effectively solves the uncertainty problem of Cypher generated by LLM, realizes the directional generation of Cypher, and ensures that the generated Cypher query statements can accurately match the data in the graph database and meet the user's query needs.
[0013] By dynamically generating a schema based on user input, while ensuring the accuracy of generated Cyphers, the data scale is reasonably controlled to avoid performance degradation caused by processing too much data. This improves the efficiency of Cypher generation and overall system performance, and implements a dynamic minimum query space method that can both meet query requirements and take performance into consideration. The present invention comprehensively utilizes a variety of advanced optimization methods, including but not limited to lexical and syntactic analysis, denoising, and normalization conversion of text data. These methods effectively enhance the system's ability to process various types of complex text data. Whether it is rigorously structured academic documents or diverse online texts, the system can respond accurately and its adaptability is significantly enhanced. The solid data preprocessing work lays a solid foundation for the subsequent design of directional control modes and the accurate generation of Cypher statements, ensuring the efficient and stable operation of the entire process. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] Figure 1 This is a Cypher query result diagram of a Cypher generation method and system based on a directional control mode provided in an embodiment of the present application; Figure 2 This is a directional control mode flow chart of a Cypher generation method and system based on a directional control mode provided in an embodiment of the present application. DETAILED DESCRIPTION
[0015] The present invention will be further described in detail below in conjunction with the accompanying drawings and specific embodiments. The embodiments of the present invention are given for purposes of illustration and description, and are not exhaustive or limit the present invention to the disclosed form. Many modifications and variations are obvious to those of ordinary skill in the art. The embodiments are selected and described to better illustrate the principles and practical applications of the present invention, and to enable those of ordinary skill in the art to understand the present invention and design various embodiments with various modifications suitable for specific purposes. Embodiment
[0016] Please refer to Figures 1-2 , in this embodiment, a Cypher generation method based on a directional control mode is provided, including three parts: data preprocessing, directional control mode design, and graph database query.
[0017] I. Data preprocessing 1. Data preparation The present invention is applicable to all types of texts, not limited to Chinese and English. The text data involved includes two parts: One part is the Neo4j graph database, which stores the test text data involved in the present invention; The other part is user input data. The user inputs key hint information by asking questions and obtains the desired answers by querying the Neo4j graph database.
[0018] 2. Text tokenization The present invention uses the jieba tokenization tool to tokenize the user input questions. Jieba tokenization has extremely high flexibility and supports exact mode tokenization (suitable for text analysis, which can cut the sentence most precisely), full mode (scans all words that can form words in the sentence, with high speed but unable to solve ambiguity), and search engine mode (based on the exact mode, further splitting long words to improve the recall rate, suitable for search engine tokenization scenarios). 3. Data cleaning After completing the tokenization operation of the text data, data cleaning is a crucial step; data cleaning can effectively remove meaningless words, reduce interference information, and significantly improve the quality of the text data. The specific cleaning methods are as follows: Removing stop words: Stop words are words that frequently appear in the text but basically do not contain substantial information, such as "de", "shi", "zai", etc. By comparing with the stop word list one by one, these words are removed from the text. Removing special symbols: Using regular expressions or simple character matching methods, special symbols, punctuation marks, and numbers in the text are removed to make the data more concise. Case conversion: Convert all text to lowercase to facilitate subsequent processing and analysis, and avoid analysis errors caused by case differences. Remove meaningless words: Remove meaningless content such as spaces and garbled characters that may appear in the text through manual screening or automatic program screening. 2. Directional control mode design Directed control mode design is the core part of this invention and is the basis for ensuring that LLM generates appropriate Cyphers according to user intentions. This part includes four sections: Schema design, prompt word design, model fine-tuning, and Cypher generation. 1. Schema design Currently, when natural language text is converted into Cypher using LLM and prompt word technology, the conversion lacks direction and cannot be used in conjunction with the Neo4j graph database for data query. Schema design can control the content of the Cypher query statements generated by LLM to keep them consistent with the content in the Neo4j graph database. The key elements of Schema design include: Content restrictions: The schema content must include node information in the target graph database (including entity nodes and concept nodes required for Cypher queries), relationship information (defining the connections between entity nodes and between entity nodes and concept nodes), and feature information of nodes and relationships. Size Limit: In theory, the more information a schema contains, the more compliant the generated Cypher will be. However, different large models can process different numbers of tokens at a time. Furthermore, the larger the number of input tokens, the slower the processing speed. This can seriously impact the user experience, especially when processing large amounts of text data. Therefore, it is necessary to find an appropriate balance between the schema's content information and the token size.
[0019] Taking the large model deepseek-v3671B as an example, the maximum number of input tokens it can process is 57,344. In order to take into account the overall time of generating Cypher, the number of tokens is controlled within 57,344.
[0020] Based on the above limitations, the Schema of the present invention dynamically generates a minimum query space based on the user's input, rather than the information of the entire target graph data; the specific approach is to use the word segmentation of the user input content as the starting point, and query the target graph database for multiple (5 in the test data) parent-child nodes, relationships, and attribute information contained above and below the word segmentation, and use the query results as the Schema content.
[0021] 2. Prompt word design The prompt design directly affects how LLM parses intent from user input and generates corresponding Cypher query statements. The prompts involved in this invention must take into account three aspects: integration with the schema, integration with user questions, and control of the final output content. Based on the requirements for generating Cypher, the prompt content is as follows: ①. You are a GraphDB Cypher expert. Please generate Cypher queries that can answer user questions based on the given Schema. ② The node names and relationship names that appear in the generated Cypher statements must be included in the Schema content; ③. The returned Cypher query statement does not need to be explained; ④、The return value of Cypher should include all node labels that appear in MATCH; ⑤. Use the entity name in the user's question as the node name to perform a bidirectional query on the parent and child nodes of the node; ⑥. The direction of the relationship between nodes in MATCH must be included in the Schema content. 3. Fine-tuning of large models Based on the above schema and prompt words, some parameters of LLM are fine-tuned. Taking the large model deepseek-v3671B as an example, the adjustment results are as follows: 4. Generate Cypher After completing the above steps, a Cypher statement that complies with Neo4j query syntax and user requirements is generated.
[0022] 3. Graph Database Query Based on the Cypher results generated in the directional control pattern design section, query the target graph database and compare the query results with the target results.
[0023] A Cypher generation system based on a directional control mode, including Data preprocessing module: used to obtain text containing Neo4j graph database data and user input data, use the Jieba word segmentation tool to segment the user input data, and then clean the segmented data by removing stop words and special symbols, performing uppercase and lowercase conversion, and removing meaningless words; Directional control mode design module: Schema design unit: used to query the target graph database for multiple parent-child nodes, relationships, and attribute information contained in the word segmentation of the user input content, and generate a minimum query space as the schema content. The schema content includes node information, relationship information, and feature information of nodes and relationships in the target graph database; Prompt word design unit: This unit is used to design prompt words. These prompt words are used to instruct the large language model to generate Cypher query statements that can answer user questions based on the given schema. The node names and relationship names that appear in the generated Cypher query statements must be included in the schema content. The Cypher return value should include all node labels that appear in the match. The parent and child nodes of the node are bidirectionally searched using the entity name in the user's question as the node name. The direction of the relationship between the nodes in the match must be included in the schema content. Model fine-tuning unit: used to fine-tune some parameters of the large language model based on the schema and prompt words; Generate Cypher unit: After the above steps, it is used to generate Cypher statements that conform to Neo4j query syntax and user requirements; Graph database query module: used to query the Neo4j graph database based on the generated Cypher statement.
[0024] Preferably, in the data preprocessing module, the removal of stop words is to remove the words in the text that are consistent with the stop word list; the removal of special symbols is to use regular expressions or simple character matching methods to remove special symbols, punctuation marks and numbers in the text; the case conversion is to convert all texts into lowercase form; the removal of meaningless words is to remove spaces and garbled characters in the text through manual screening or automatic program screening.
[0025] Preferably, in the Schema design unit, the number of the multiple parent-child nodes, relationships, and attribute information is 5. Example
[0026] In this embodiment, a Cypher generation method based on a directional control mode is provided, including: This method mainly includes three parts: data preprocessing, directional control mode design and graph database query.
[0027] 1. Data Preprocessing 1. Data preparation The present invention is applicable to all types of text, not limited to Chinese and English. The text data involved includes two parts: One part is a Neo4j graph database, which stores the test text data involved in the present invention; an example is as follows: “The characteristics of Miss Austen’s humour are so subtle and delicate that they are, perhaps, at all times easier to apprehend than to express, and at any particular time likely to be differently apprehended by different persons.” Another part is the user input data. The user inputs key hint information by asking questions and obtains the desired answers by querying the Neo4j graph database.
[0028] The examples are as follows: “How about Miss Austen’s humour characteristics?” 2. Text Word Segmentation The present invention uses the jieba word segmentation tool to segment the questions input by the user. Jieba word segmentation has extremely high flexibility and supports accurate mode word segmentation (suitable for text analysis, which can cut the sentence most accurately), full mode (scanning out all the words that can form words in the sentence, with high speed but unable to solve ambiguity), and search engine mode (based on the accurate mode, further segmenting long words to improve the recall rate, suitable for the word segmentation scenario of search engines).
[0029] The examples are as follows: Segment the question input by the user, and the word segmentation result is: [‘How’,’’,’about’,’’,’Miss’,’’,’Austen’,’’,”’”,’s’,’humour’,’’,’characteristics’,’’,’?’] 3. Data Cleaning After completing the word segmentation operation of the text data, data cleaning is a crucial step; data cleaning can effectively remove meaningless words, reduce interference information, and significantly improve the quality of text data. The specific cleaning methods are as follows: Remove stop words: Stop words are words that frequently appear in the text but basically do not contain substantial information, such as “的” “是” “在” etc. By comparing with the stop word list one by one, these words are removed from the text. Remove special symbols: Use regular expressions or simple character matching methods to remove special symbols, punctuation marks, and numbers in the text to make the data more concise. Case conversion: Convert all text to lowercase to facilitate subsequent processing and analysis, and avoid analysis errors caused by case differences. Remove meaningless words: Remove meaningless content such as spaces and garbled characters that may appear in the text through manual screening or automatic program screening.
[0030] Here is an example: The word segmentation results after cleaning are: ['How','about','Miss','Austen','humour','characteristics'] 2. Directional control mode design Directed control mode design is the core part of this invention and is the basis for ensuring that LLM generates appropriate Cyphers according to user intentions. This part includes four sections: Schema design, prompt word design, model fine-tuning, and Cypher generation. 1. Schema design Currently, when natural language text is converted into Cypher using LLM and prompt word technology, the conversion lacks direction and cannot be used in conjunction with the Neo4j graph database for data query. Schema design can control the content of the Cypher query statements generated by LLM to keep them consistent with the content in the Neo4j graph database. The key elements of Schema design include: Content restrictions: The schema content must include node information in the target graph database (including entity nodes and concept nodes required for Cypher queries), relationship information (defining the connections between entity nodes and between entity nodes and concept nodes), and feature information of nodes and relationships. Size Limit: In theory, the more information a schema contains, the more compliant the generated Cypher will be. However, different large models can process different numbers of tokens at a time. Furthermore, the larger the number of input tokens, the slower the processing speed. This can seriously impact the user experience, especially when processing large amounts of text data. Therefore, it is necessary to find an appropriate balance between the schema's content information and the token size.
[0031] Taking the large model deepseek-v3671B as an example, the maximum number of input tokens it can process is 57,344. In order to take into account the overall time of generating Cypher, the number of tokens is controlled within 57,344.
[0032] Based on the above limitations, the Schema of the present invention dynamically generates a minimum query space based on the user's input, rather than the information of the entire target graph data; the specific approach is to use the word segmentation of the user input content as the starting point, and query the target graph database for multiple (5 in the test data) parent-child nodes, relationships, and attribute information contained above and below the word segmentation, and use the query results as the Schema content.
[0033] For the above user input question example, part of the final schema is as follows: 1. Node Information Example (_1892:delicate{indexId:12,ner:'O',tag:'JJ',word:'delicate'}) (_2281:satire{indexId:85,ner:'O',tag:'NN',word:'satire'}) (_1899:Austen{indexId:5,ner:'PERSON',tag:'NNP',word:'Austen'}) (_1900:they{indexId:14,ner:'O',tag:'PRP',word:'they'}) (_1906:characteristic{indexId:2,ner:'O',tag:'NNS',word:'characteristics'}) (_2167:Austen{indexId:29,ner:'PERSON',tag:'NNP',word:'Austen'}) 2. Relationship Information Example (_1891)-[:_sid_{}]->(_1886) (_2301)-[:nmod{longName:'nmod_preposition',shortName:'nmod',specific:'such_as'}]->(_2281) (_2269)-[:fixed{longName:'multi-wordexpression',shortName:'fixed'}]->(_2331) (_1931)-[:nsubj_xsubj{longName:'controllingnominalsubject',shortName:'nsubj:xsubj'}]->(_1938) (_1900)-[:_same{}]->(_1906) (_2084)-[:det{longName:'determiner',shortName:'det'}]->(_2107) 2. Prompt word design The prompt design directly affects how LLM parses intent from user input and generates corresponding Cypher query statements. The prompts involved in this invention must take into account three aspects: integration with the schema, integration with user questions, and control of the final output content. Based on the requirements for generating Cypher, the prompt content is as follows: ①. You are a GraphDB Cypher expert. Please generate Cypher queries that can answer user questions based on the given Schema. ② The node names and relationship names that appear in the generated Cypher statements must be included in the Schema content; ③. The returned Cypher query statement does not need to be explained; ④、The return value of Cypher should include all node labels that appear in MATCH; ⑤. Use the entity name in the user's question as the node name to perform a bidirectional query on the parent and child nodes of the node; ⑥. The direction of the relationship between nodes in MATCH must be included in the Schema content. 3. Fine-tuning of large models Based on the above schema and prompt words, some parameters of LLM are fine-tuned. Taking the large model deepseek-v3671B as an example, the adjustment results are as follows: 4. Generate Cypher After completing the above steps, a Cypher statement that complies with Neo4j query syntax and user requirements is generated.
[0034] Here is an example: For the above user input question example, the final Cypher statement is as follows: MATCH(austen:Austen)-[:compound]->(miss:Miss), (humour:humour)-[:nmod_poss]->(austen), (characteristic:characteristic)<-[r]->(a) RETURNmiss,austen,humour,characteristic,a 3. Graph Database Query Based on the Cypher results generated in the directional control pattern design section, query the target graph database and compare the query results with the target results.
[0035] Here is an example: Based on the generated Cypher results, query the target graph database. By comparing the query results ( Figure 1 ) and the target results, the keywords "Miss", "Austen", "characteristics", "humor", "subtle", and "delicate" can all be accurately matched.
[0036] The drawings in this specification Figure 1 The results of a query in a Neo4j graph database based on the generated Cypher statement are shown. This graph visually demonstrates that the query results contain key information that matches the target result, such as nodes and relationships, verifying the effectiveness and accuracy of the Cypher statement generated by the method.
[0037] Figure 2 The flow of the directional control mode of the present invention is presented in detail. It starts with schema design to determine the content scope of the generated cypher; then, prompt words are designed to guide the LLM to generate cyphers in a specific direction; then, the large model is fine-tuned to optimize the generation effect; finally, the cypher statements that meet the requirements are generated. The flowchart clearly shows the logical relationship and execution order between the various links of the directional control mode.
[0038] Obviously, the embodiments described are only some of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field and related fields without making creative efforts should fall within the scope of protection of the present invention. Structures, devices, and operating methods not specifically described and explained in the present invention shall be implemented in accordance with conventional means in the field unless otherwise specified or limited.
Claims
1. A Cypher generation method based on a directional control mode, characterized in that: The following steps are involved: Step 1: Data preprocessing steps: Obtain text containing Neo4j graph database data and user input data, use the Jieba word segmentation tool to segment the user input data, and then clean the segmented data by removing stop words and special symbols, performing uppercase and lowercase conversion, and removing meaningless words; Step 2: Directional control mode design steps: Schema design: Based on the word segmentation of the user input content, the target graph database is searched for multiple parent-child nodes, relationships, and attribute information contained in the word segmentation, and the minimum query space is generated as the schema content. The schema content contains node information, relationship information, and feature information of nodes and relationships in the target graph database; Prompt word design: Design prompt words to instruct the large language model to generate Cypher query statements that can answer user questions based on the given schema. The node names and relationship names that appear in the generated Cypher statements must be included in the schema content. The Cypher return value should include all node labels that appear in the match. Use the entity name in the user's question as the node name to perform a bidirectional query on the parent and child nodes of the node. The direction of the relationship between the nodes in the match must be included in the schema content. Model fine-tuning: Based on the schema and prompt words, fine-tune some parameters of the large language model; Generate Cypher: After the above steps, generate Cypher statements that conform to Neo4j query syntax and user requirements; Step 3: Graph database query step: Query the Neo4j graph database based on the generated Cypher statement.
2. A Cypher generation method based on a directional control mode according to claim 1, characterized in that: In the data preprocessing step, the removal of stop words is to remove the words in the text that match the stop word list; the removal of special symbols is to use regular expressions or simple character matching methods to remove special symbols, punctuation marks and numbers in the text; the case conversion is to convert all texts into lowercase form; the removal of meaningless words is to remove spaces and garbled characters in the text through manual screening or automatic program screening.
3. The Cypher generation method based on the directional control mode according to claim 1, characterized in that: In the Schema design, the number of the multiple parent-child nodes, relationships, and attribute information is 5.
4. The Cypher generation method based on the directional control mode according to claim 1, characterized in that: In the model fine-tuning step, taking the large language model deepseek-v3671B as an example, the stream parameter is set to False and the temperature parameter is set to 0.
7.
5. A Cypher generation system based on a directional control mode, characterized in that: include Data preprocessing module: used to obtain text containing Neo4j graph database data and user input data, use the Jieba word segmentation tool to segment the user input data, and then clean the segmented data by removing stop words and special symbols, performing uppercase and lowercase conversion, and removing meaningless words; Directional control mode design module: Schema design unit: used to query the target graph database for multiple parent-child nodes, relationships, and attribute information contained in the word segmentation of the user input content, and generate a minimum query space as the schema content. The schema content includes node information, relationship information, and feature information of nodes and relationships in the target graph database; Prompt word design unit: This unit is used to design prompt words. These prompt words are used to instruct the large language model to generate Cypher query statements that can answer user questions based on the given schema. The node names and relationship names that appear in the generated Cypher query statements must be included in the schema content. The Cypher return value should include all node labels that appear in the match. The parent and child nodes of the node are bidirectionally searched using the entity name in the user's question as the node name. The direction of the relationship between the nodes in the match must be included in the schema content. Model fine-tuning unit: used to fine-tune some parameters of the large language model based on the schema and prompt words; Generate Cypher unit: After the above steps, it is used to generate Cypher statements that conform to Neo4j query syntax and user requirements; Graph database query module: used to query the Neo4j graph database based on the generated Cypher statement.
6. A Cypher generation system based on a directional control mode according to claim 5, characterized in that: In the data preprocessing module, the removal of stop words is to remove the words in the text that match the stop word list; the removal of special symbols is to use regular expressions or simple character matching methods to remove special symbols, punctuation marks and numbers in the text; the case conversion is to convert all texts into lowercase form; the removal of meaningless words is to remove spaces and garbled characters in the text through manual screening or automatic program screening.
7. A Cypher generation system based on a directional control mode according to claim 5, characterized in that: In the Schema design unit, the number of the plurality of parent-child nodes, relationships, and attribute information is 5.
8. The Cypher generation system based on the directional control mode according to claim 5, characterized in that: In the model fine-tuning unit, taking the large language model deepseek-v3671B as an example, the stream parameter is set to False and the temperature parameter is set to 0.7.