Graph database query method and device based on large language model and graph structure
Through the graph database query method based on large language models and graph structures, query statements are generated and executed in the graph database, which solves the problem of low query efficiency of traditional graph databases, improves ease of use and popularity, and realizes an efficient query process. .
Patent Information
- Application Number
- CN202410458999.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-17
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2044-04-17
AI Technical Summary
The traditional graph database query process requires professional query languages and familiarity with the database structure, resulting in low ease of use and popularity, and thus low query efficiency.
A graph database query method based on large language models and graph structures is used to generate query statements by obtaining the graph structure of the target graph database and user-input query questions, and execute them in the graph database to generate easy-to-understand query results.
It reduces users' requirements for professionalism, improves the ease of use and popularity of graph databases, and thereby improves query efficiency.
Smart Images

Figure CN118093632B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of graph database and artificial intelligence technology, and in particular to a graph database query method and device based on a large language model and a graph structure. Background Art
[0002] Graph database is a non-relational database based on graph theory. The data storage structure and data query method of graph database are based on graph theory. Graph database mainly stores nodes and relationships. At present, graph database has shown significant advantages in processing complex structured data, especially in relationship-intensive data environments.
[0003] However, in traditional graph database systems, the query process often requires a specific query language and users who are familiar with the database structure, which places high demands on the user's professionalism, resulting in low usability and popularity of graph databases, and in turn low query efficiency. Summary of the invention
[0004] The present invention provides a graph database query method and device based on a large language model and a graph structure, so as to solve the defect of low query efficiency of the graph database in the prior art and improve the versatility and query efficiency of the graph database.
[0005] The present invention provides a graph database query method based on a large language model and a graph structure, comprising:
[0006] Obtaining a target graph structure corresponding to a target graph database and a query question input by a user; the target graph structure includes target graph node attributes, target graph edge attributes, and target graph edge relationships;
[0007] Determine a prompt statement based on a prompt template, the target graph node attributes, the target graph edge attributes, and the target graph edge relationship;
[0008] Input the prompt statement and the query question into a large language model, and output a query statement corresponding to the target graph database;
[0009] The query statement is executed in the target graph database to determine the target query text.
[0010] According to the graph database query method based on a large language model and a graph structure provided by the present invention, the step of obtaining a target graph structure corresponding to the target graph database includes:
[0011] Obtain metadata in the target graph database; the metadata includes element labels, element types, target types, and attribute names corresponding to nodes or edges;
[0012] Based on the element type and the target type, filtering the target element from all metadata;
[0013] Based on the element tag and the attribute name corresponding to the target element, a target graph structure corresponding to the target graph database is determined.
[0014] According to the graph database query method based on a large language model and a graph structure provided by the present invention, the target element includes a first target node or a target edge;
[0015] The determining, based on the element tag and the attribute name corresponding to the target element, a target graph structure corresponding to the target graph database comprises:
[0016] Determining a target graph node attribute in the target graph structure based on the element tag and the attribute name corresponding to the first target node;
[0017] Determining a target graph edge attribute in the target graph structure based on the element tag and the attribute name corresponding to the target edge;
[0018] Based on the element tag and the attribute name corresponding to the target edge, and information of a second target node related to the target edge, a target graph edge relationship in the target graph structure is determined.
[0019] According to the graph database query method based on a large language model and a graph structure provided by the present invention, filtering the target element from all metadata based on the element type and the target type includes:
[0020] When the element type is a node and the target type is not a relationship, filtering from all metadata to obtain a first target node;
[0021] When the element type is a relationship and the target type is not a node or the target type is a relationship, a target edge is obtained by filtering from all metadata.
[0022] According to the graph database query method based on a large language model and a graph structure provided by the present invention, the prompt statement is determined based on the prompt template, the target graph node attribute, the target graph edge attribute and the target graph edge relationship, including:
[0023] Determine the target general prompt word in the prompt template; the target general prompt word includes a first general prompt word, a second general prompt word and a third general prompt word;
[0024] Update the first general prompt word based on the target graph node attribute; update the second general prompt word based on the target graph edge attribute; update the third general prompt word based on the target graph edge relationship;
[0025] The updated prompt template is determined as the prompt sentence.
[0026] According to the graph database query method based on a large language model and a graph structure provided by the present invention, executing the query statement in the target graph database to determine the target query text includes:
[0027] Execute the query statement in the target graph database to obtain a first query result;
[0028] The first query result and the query question are input into the large language model to determine the target query text.
[0029] According to the graph database query method based on a large language model and a graph structure provided by the present invention, the inputting the first query result and the query question into the large language model to determine the target query text includes:
[0030] Performing semantic analysis on the query question to determine a semantic analysis result corresponding to the query question;
[0031] When the semantic analysis result indicates that the query question includes a target output format, the first query result is format-converted based on the target output format to determine the target query text.
[0032] The present invention also provides a graph database query device based on a large language model and a graph structure, comprising:
[0033] An acquisition module is used to acquire a target graph structure corresponding to a target graph database and a query question input by a user; the target graph structure includes target graph node attributes, target graph edge attributes and target graph edge relationships;
[0034] A determination module, configured to determine a prompt statement based on a prompt template, the target graph node attributes, the target graph edge attributes, and the target graph edge relationship;
[0035] An output module, used to input the prompt statement and the query question into a large language model, and output a query statement corresponding to the target graph database;
[0036] The query module is used to execute the query statement in the target graph database and determine the target query text.
[0037] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, it implements any of the graph database query methods based on a large language model and a graph structure as described above.
[0038] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a graph database query method based on a large language model and a graph structure as described in any one of the above.
[0039] The graph database query method and device based on a large language model and a graph structure provided by the present invention, after obtaining the query question input by the user and the target graph structure corresponding to the target graph database, use the target graph node attributes, target graph edge attributes and target graph edge relationships in the target graph structure to update the prompt template to obtain a prompt statement, and input the prompt statement and the query question input by the user into the large language model, combine the complex structure processing capability of the graph database and the natural language processing capability of the large language model to generate a query statement, and after executing the query statement in the target graph database, generate a target query text that is easy for the user to understand, so that in the entire query process, the construction process of the query statement with higher professional requirements is executed by the large language model, and the execution process of the query statement is automatically executed, and the process involving user operations only includes inputting the query question and receiving the target query text, which greatly reduces the professional requirements for the user, improves the usability and popularity of the graph database, and thus improves the query efficiency of the graph database. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0041] Figure 1 This is one of the flow diagrams of a graph database query method based on a large language model and a graph structure provided by an embodiment of the present invention;
[0042] Figure 2 This is the second flow chart of the graph database query method based on a large language model and a graph structure provided by an embodiment of the present invention;
[0043] Figure 3 This is the third flow chart of the graph database query method based on a large language model and a graph structure provided by an embodiment of the present invention;
[0044] Figure 4 It is a structural schematic diagram of a graph database query device based on a large language model and a graph structure provided by an embodiment of the present invention;
[0045] Figure 5 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0046] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0047] In view of the low query efficiency of graph databases in the prior art, an embodiment of the present invention provides a graph database query method based on a large language model and a graph structure. Figure 1 This is one of the flow charts of the graph database query method based on a large language model and a graph structure provided by an embodiment of the present invention. Figure 1 As shown, the method includes:
[0048] Step 110: Obtain a target graph structure corresponding to a target graph database and a query question input by a user; the target graph structure includes target graph node attributes, target graph edge attributes, and target graph edge relationships.
[0049] It should be noted that the above-mentioned target graph database can be any one of the graph databases. Graph Database is a non-relational database (Not Only Structured Query Language, NoSQL) based on graph theory, that is, the data storage structure and data query method of the graph database are based on graph theory. In a graph database, the basic elements of a graph are nodes and edges. Nodes can be understood as entities in reality, and edges can be understood as the relationship between entities. Graph databases are designed for storing, querying, and analyzing highly interconnected data. Since nodes and edges have been stored in the graph database in advance, the graph database can quickly respond to complex related queries.
[0050] Optionally, the target graph database may include Neo4j, OrientDB, ArangoDB, and JanusGraph, among which Neo4j is a high-performance, object-oriented graph database that provides a very flexible Cypher query language. Both OrientDB and ArangoDB are multi-model databases that support graph, document, and key-value database models. JanusGraph is a distributed graph database that supports multiple storage backends, including Cassandra, HBase, and BerkeleyDB, among others, which is not limited in the embodiments of the present invention.
[0051] In addition, in response to the user's input operation, the query question input by the user can be obtained. For example, the user can enter the query question in an input box on the display interface of the electronic device, or the user can also enter the query question in an input box on the display interface of another device that is communicatively connected to the electronic device, and the other device can upload the query question to the electronic device, thereby obtaining the query question input by the user. The embodiment of the present invention does not limit the user's input operation.
[0052] It should be noted that in the target graph structure, the target graph node attributes include multiple attribute information related to the nodes, the target graph edge attributes include multiple attribute information related to the edges, and the target graph edge relationships are used to represent the connection relationship and directionality between nodes.
[0053] Further, the obtaining of the target graph structure corresponding to the target graph database includes:
[0054] Obtain metadata in the target graph database; the metadata includes element labels, element types, target types, and attribute names corresponding to nodes or edges;
[0055] Based on the element type and the target type, filtering the target element from all metadata;
[0056] Based on the element tag and the attribute name corresponding to the target element, a target graph structure corresponding to the target graph database is determined.
[0057] Specifically, the core structure of the target graph structure corresponding to the target graph database includes target graph node attributes, target graph edge attributes and target graph edge relationships. An embodiment of the present invention provides a general extraction method for the target graph structure, specifically: when obtaining the target graph node attributes, target graph edge attributes and target graph edge relationships of the target graph structure, first, by calling the plug-in command in the target graph database, the metadata of the instance in the target graph database can be obtained, and the element label, element type, target type and attribute name corresponding to each node or edge are returned. For example, taking Neo4j as the target database, the "CALL apoc.meta.data()" Cypher statement can be used to obtain the metadata corresponding to all nodes and edges in the Neo4j graph database. "CALL apoc.meta.data()" is a command in the APOC plug-in used in the Neo4j graph database. The metadata includes the number of nodes and edges, index information, and the status of the storage engine. Afterwards, when obtaining the target graph node properties and target graph edge properties, the "YIELD label, elementType, type, property" Cypher statement is used to limit the fields in the returned metadata, so that the returned metadata includes four fields: element label, element type, target type, and property name. Among them, label represents the element label, and the element label is the label name in the database. In Neo4j, the element label is used to classify the target element, elementType represents the element type, and the element type is used to indicate whether the returned node or relationship is a node or a relationship. Type represents the target type. For a relationship, the target type is used to indicate the type of the relationship. For a node, the target type is usually empty, and property represents the property name. When obtaining the target graph edge relationship, "YIELD label,elementType, type, property, other" can be used, so that the returned metadata includes five fields: element label, element type, target type, property name and other related information. Among them, other represents other related information, which is used to represent other information related to the target edge, for example, information corresponding to the second target node connected to the target edge.
[0058] Afterwards, different filtering conditions are formed according to the combination of element type and target type. Through the filtering conditions, the target element is filtered from all metadata to obtain the target element, which includes the first target node or target edge. After filtering to obtain the target element, the corresponding key-value pair is combined according to the element label and attribute name corresponding to the target element, and then the target graph structure corresponding to the target graph database is obtained. For example, when obtaining the target graph node attribute and the target graph edge attribute, the element label corresponding to the target element can be renamed, and all attribute names corresponding to the target element can be aggregated. After aggregation, all attribute names are renamed, and the key-value pairs corresponding to the element label and the key-value pairs corresponding to the attribute name are formed, and then the target graph node attribute and the target graph edge attribute are respectively formed. When obtaining the target graph edge relationship, after filtering to obtain the target element, the key-value pairs corresponding to the element label, the key-value pairs corresponding to the attribute name, and the key-value pairs corresponding to the information of the second target node related to the target edge are respectively constructed, and then the target graph edge relationship is constructed.
[0059] Further, filtering the target element from all metadata based on the element type and the target type includes:
[0060] When the element type is a node and the target type is not a relationship, filtering from all metadata to obtain a first target node;
[0061] When the element type is a relationship and the target type is not a node or the target type is a relationship, a target edge is obtained by filtering from all metadata.
[0062] Specifically, after obtaining the metadata, the element type and the target type are combined to construct a filtering condition, and the first target node or target edge is filtered from the metadata through the filtering condition. That is, when the element type is a node and the target type is used to ensure that the filtering result is not a relationship, the first target node can be accurately filtered, and when the element type is a relationship and the target type is used to ensure that the filtering result is not a node or the filtering result is a relationship, the target edge can be accurately filtered from all metadata.
[0063] For example, taking the target database as Neo4j, when obtaining the target graph node properties, the "WHEREelementType = "node" AND type<>"RELATIONSHIP"" statement can be used to select records with the element type (elementType) as node (node), and the accuracy of the above records can be ensured by the target type (type) not being a relationship (RELATIONSHIP). When obtaining the target graph edge properties, the "WHERE elementType = "relationship" ANDNOT type = "NODE"" statement can be used to select records with the element type (elementType) as relationship (relationship), and the accuracy of the above records can be ensured by the target type (type) not being a node (NODE). When obtaining the target graph edge relationship, the "WHERE elementType = "relationship" AND type = "RELATIONSHIP"" statement can be used to select records with the element type (elementType) as relationship (relationship), and the accuracy of the above records can be further ensured by the target type (type) being a relationship (relationship), which is used to indicate that the query focuses on the relationship types defined in the target graph database.
[0064] Further, determining the target graph structure corresponding to the target graph database based on the element tag and the attribute name corresponding to the target element includes:
[0065] Determining a target graph node attribute in the target graph structure based on the element tag and the attribute name corresponding to the first target node;
[0066] Determining a target graph edge attribute in the target graph structure based on the element tag and the attribute name corresponding to the target edge;
[0067] Based on the element tag and the attribute name corresponding to the target edge, and information of a second target node related to the target edge, a target graph edge relationship in the target graph structure is determined.
[0068] For example, taking the target database as Neo4j, when obtaining the target graph node properties, first convert and aggregate data through the "WITHlabel AS nodeTypes, collect(property) AS nodeProperties" statement, that is, rename the element label (label) of the first target node obtained by filtering to nodeTypes, collect all the property names of the first target node of each node type, and rename the property name collection to nodeProperties. After that, through the "RETURN {nodeLabels: nodeTypes, nodeProperties: nodeProperties} ASnodeData" statement, return a target graph node property containing two key-value pairs, the two keys are the node type (nodeLabels) and the property name collection (nodeProperties) of the node type, and rename the target graph node property to nodeData. When obtaining the target graph edge properties, first transform and aggregate the data through the "WITH label AS relationshipTypes, collect(property) AS relationshipProperties" statement, that is, rename the element label (label) of the filtered target edge to relationshipTypes, collect all the property names of each relationship type, and rename the collection of property names to relationshipProperties. After that, through the "RETURN{relationshipLabels: relationshipTypes, properties: relationshipProperties}AS relationshipData" statement, return a target graph edge property containing two key-value pairs, the two keys are the relationship type (relationshipLabels) and the property name collection (properties) of the relationship type, and rename the target graph edge property to relationshipData.When obtaining the target graph edge relationship, a target graph edge relationship containing three key-value pairs is returned through the "RETURN{relationshipType: label, propertyKey: property, targetInfo: other} ASrelationshipDetails" statement. The three keys are the relationship type (relationshipType), the property of the relationship (propertyKey), and other related information (targetInfo), and the target graph edge relationship is renamed relationshipDetails.
[0069] It should be noted that the above-mentioned general extraction method of the target graph structure does not depend on the graph structure. By extracting the target graph node attributes, target graph edge attributes and target graph edge relationships, the complete target graph structure can be determined, which can be extended to any graph database.
[0070] Step 120: Determine a prompt statement based on a prompt template, the target graph node attributes, the target graph edge attributes, and the target graph edge relationships.
[0071] Specifically, after obtaining the target graph structure including the target graph node attributes, the target graph edge attributes and the target graph edge relationships, the prompt engineering technology can be combined to further obtain the prompt template, and the target graph node attributes, the target graph edge attributes and the target graph edge relationships are used to update the prompt template to obtain the updated prompt statement, so as to ensure that the large language model can clearly understand the storage structure of the target graph database.
[0072] Furthermore, Figure 2 This is a second flow chart of a graph database query method based on a large language model and a graph structure provided by an embodiment of the present invention. Figure 2 As shown, the step of determining a prompt statement based on a prompt template, a target graph node attribute, a target graph edge attribute, and a target graph edge relationship includes:
[0073] Determine the target general prompt word in the prompt template; the target general prompt word includes a first general prompt word, a second general prompt word and a third general prompt word;
[0074] Update the first general prompt word based on the target graph node attribute; update the second general prompt word based on the target graph edge attribute; update the third general prompt word based on the target graph edge relationship;
[0075] The updated prompt template is determined as the prompt sentence.
[0076] Specifically, after obtaining the prompt template, the target general prompt words included in the prompt template are determined. The number of target general prompt words is the same as the number of core parts included in the target graph structure, that is, the target graph structure includes target graph node attributes, target graph edge attributes and target graph edge relationships. Then, the target general prompt words include the first general prompt word, the second general prompt word and the third general prompt word. The first general prompt word is associated with the target graph node attribute, the second general prompt word is associated with the target graph edge attribute, and the third general prompt word is associated with the target graph edge relationship. After that, the first general prompt word is replaced by the target graph node attribute, the second general prompt word is replaced by the target graph edge attribute, and the third general prompt word is replaced by the target graph edge relationship. Combined with other prompt texts in the prompt template, an updated prompt template, that is, a prompt sentence, can be obtained.
[0077] In this example, the target graph database is Neo4j, and the prompt text in the prompt template is "This is the schema representation of the Neo4j database:
[0078] Node properties are the following:{nodes_props};
[0079] Relationship properties are the following:{relations_props};
[0080] Relationship point from source to target nodes: {relations};
[0081] Make sure to respect relationship types and directions." is taken as an example, where the first general prompt word is "nodes_props", the second general prompt word is "relations_props", and the third general prompt word is "relations". The first general prompt word nodes_props is replaced by the target graph node attribute nodeData, the second general prompt word relations_props is replaced by the target graph edge attribute relationshipData, and the third general prompt word relations is replaced by the target graph edge relationship relationshipDetails. The prompt sentence is "This is the schema representation of the Neo4j database:
[0082] Node properties are the following: {nodeData};
[0083] Relationship properties are the following: {relationshipData};
[0084] Relationship point from source to target nodes: {relationshipDetails};
[0085] Make sure to respect relationship types and directions. "
[0086] Step 130: input the prompt statement and the query question into a large language model, and output a query statement corresponding to the target graph database.
[0087] Specifically, after the prompt statement is determined, the prompt statement and the query question input by the user are input into the large language model together. The large language model can understand the target graph structure of the target graph database based on the prompt statement, and perform semantic analysis on the query question input by the user, identify the entities and the relationships between entities in the query question, and convert the entities and the relationships between entities into query elements in the target graph structure, that is, map the entities to the nodes in the target graph structure, and map the relationships between entities to the relationships in the target graph structure; based on the mapping results, generate the query statement corresponding to the target graph database.
[0088] Optionally, the selection of the above-mentioned large language model is flexible and diverse, and can be any online large language model or private large language model, for example, Wenxin Yiyan or iFlytek Spark cognitive large model, etc. The embodiment of the present invention does not limit the type of large language model.
[0089] Step 140: Execute the query statement in the target graph database to determine the target query text.
[0090] Specifically, after determining the query statement corresponding to the query question, the query statement can be executed in the target graph database, the results related to the query question can be queried in the target graph database, and a target query text to be fed back to the user is generated, so that the user does not need to master professional graph database knowledge, but can easily obtain the content that needs to be queried in the graph database by simply asking questions in natural language.
[0091] Furthermore, Figure 3This is a flowchart of a graph database query method based on a large language model and a graph structure provided by an embodiment of the present invention. Figure 3 As shown, executing the query statement in the target graph database to determine the target query text includes:
[0092] Execute the query statement in the target graph database to obtain a first query result;
[0093] The first query result and the query question are input into the large language model to determine the target query text.
[0094] Specifically, after the large language model outputs the query statement, the query statement can be executed in the target graph database to obtain a first query result related to the query question. However, since the first query result of the target graph database is usually presented in a structured or graphical form, it is difficult for non-professional users or users without a graph database background to understand the first query result. Therefore, in order to facilitate user understanding or meet user needs, after determining the first query result, the first query result and the corresponding query question are input into the large language model together. The large language model can understand the user's intention again based on the query question, and then adjust the first query result according to the user's intention, convert it into a more natural and easier to understand target query text, and feed back the target query text to the user.
[0095] Furthermore, the step of inputting the first query result and the query question into the large language model to determine the target query text includes:
[0096] Performing semantic analysis on the query question to determine a semantic analysis result corresponding to the query question;
[0097] When the semantic analysis result indicates that the query question includes a target output format, the first query result is format-converted based on the target output format to determine the target query text.
[0098] Specifically, after the first query result and the query question are input into the large language model together, the large language model can perform semantic analysis on the query question to determine the semantic analysis result corresponding to the query question, that is, understand the user's corresponding intention. If the semantic analysis result includes the target output format indicated by the user, the large language model can adjust the first query result according to the target output format, convert it into a format corresponding to the target output format, and then determine the target query text that is easy for the user to understand, and then feed it back to the user. For example, taking the query question of "query the film and television dramas corresponding to director A and output them in Markdown format" as an example, after the query question is input into the large language model, based on the semantic analysis of the query question, it can be determined that the target output format indicated by the user is Markdown format, and the first query result is format converted according to the Markdown format to obtain the corresponding target query text.
[0099] In addition, if the user does not indicate a specific target output format in the query question, the large language model can adjust the first query result and convert it into a more natural and easier to understand target query text.
[0100] It should be noted that the graph database query method based on a large language model and a graph structure provided in an embodiment of the present invention can also be applied to other databases, for example, a relational database. In this case, it is only necessary to change the graph structure to a table structure, which has broad application prospects.
[0101] The graph database query method based on a large language model and a graph structure provided by an embodiment of the present invention, after obtaining the query question input by the user and the target graph structure corresponding to the target graph database, uses the target graph node attributes, target graph edge attributes and target graph edge relationships in the target graph structure to update the prompt template to obtain a prompt statement, and inputs the prompt statement and the query question input by the user into the large language model, combines the complex structure processing capability of the graph database and the natural language processing capability of the large language model to generate a query statement, and after executing the query statement in the target graph database, generates a target query text that is easy for the user to understand, so that in the entire query process, the construction process of the query statement with high professional requirements is executed by the large language model, and the execution process of the query statement is automatically executed, and the process involving user operations only includes inputting the query question and receiving the target query text, which greatly reduces the professional requirements for the user, improves the ease of use and popularity of the graph database, and thus improves the query efficiency of the graph database. In addition, it has greater flexibility and high scalability in selecting a large language model and a graph database.
[0102] The graph database query device based on a large language model and a graph structure provided by the present invention is described below. The graph database query device based on a large language model and a graph structure described below and the graph database query method based on a large language model and a graph structure described above can be referenced to each other.
[0103] The embodiment of the present invention also provides a graph database query device based on a large language model and a graph structure. Figure 4 is a schematic diagram of the structure of a graph database query device based on a large language model and a graph structure provided by an embodiment of the present invention. Figure 4 As shown, the graph database query device 400 based on a large language model and a graph structure includes: an acquisition module 410, a determination module 420, an output module 430 and a query module 440, wherein:
[0104] The acquisition module 410 is used to acquire the target graph structure corresponding to the target graph database and the query question input by the user; the target graph structure includes target graph node attributes, target graph edge attributes and target graph edge relationships;
[0105] A determination module 420, configured to determine a prompt statement based on a prompt template, the target graph node attributes, the target graph edge attributes, and the target graph edge relationship;
[0106] An output module 430 is used to input the prompt statement and the query question into a large language model, and output a query statement corresponding to the target graph database;
[0107] The query module 440 is used to execute the query statement in the target graph database to determine the target query text.
[0108] The graph database query device based on a large language model and a graph structure provided by an embodiment of the present invention, after obtaining the query question input by the user and the target graph structure corresponding to the target graph database, uses the target graph node attributes, target graph edge attributes and target graph edge relationships in the target graph structure to update the prompt template to obtain a prompt statement, and inputs the prompt statement and the query question input by the user into the large language model, combines the complex structure processing capability of the graph database and the natural language processing capability of the large language model to generate a query statement, and after executing the query statement in the target graph database, generates a target query text that is easy for the user to understand, so that in the entire query process, the construction process of the query statement with higher professional requirements is executed by the large language model, and the execution process of the query statement is automatically executed, and the process involving user operations only includes inputting the query question and receiving the target query text, which greatly reduces the professional requirements for the user, improves the usability and popularity of the graph database, and thus improves the query efficiency of the graph database.
[0109] Optionally, the acquisition module 410 is specifically configured to:
[0110] Obtain metadata in the target graph database; the metadata includes element labels, element types, target types, and attribute names corresponding to nodes or edges;
[0111] Based on the element type and the target type, filtering the target element from all metadata;
[0112] Based on the element tag and the attribute name corresponding to the target element, a target graph structure corresponding to the target graph database is determined.
[0113] Optionally, the target element includes a first target node or a target edge.
[0114] Optionally, the acquisition module 410 is specifically configured to:
[0115] Determining a target graph node attribute in the target graph structure based on the element tag and the attribute name corresponding to the first target node;
[0116] Determining a target graph edge attribute in the target graph structure based on the element tag and the attribute name corresponding to the target edge;
[0117] Based on the element tag and the attribute name corresponding to the target edge, and information of a second target node related to the target edge, a target graph edge relationship in the target graph structure is determined.
[0118] Optionally, the acquisition module 410 is specifically configured to:
[0119] When the element type is a node and the target type is not a relationship, filtering from all metadata to obtain a first target node;
[0120] When the element type is a relationship and the target type is not a node or the target type is a relationship, a target edge is obtained by filtering from all metadata.
[0121] Optionally, the determination module 420 is specifically configured to:
[0122] Determine the target general prompt word in the prompt template; the target general prompt word includes a first general prompt word, a second general prompt word and a third general prompt word;
[0123] Update the first general prompt word based on the target graph node attribute; update the second general prompt word based on the target graph edge attribute; update the third general prompt word based on the target graph edge relationship;
[0124] The updated prompt template is determined as the prompt sentence.
[0125] Optionally, the query module 440 is specifically configured to:
[0126] Execute the query statement in the target graph database to obtain a first query result;
[0127] The first query result and the query question are input into the large language model to determine the target query text.
[0128] Optionally, the query module 440 is specifically configured to:
[0129] Performing semantic analysis on the query question to determine a semantic analysis result corresponding to the query question;
[0130] When the semantic analysis result indicates that the query question includes a target output format, the first query result is format-converted based on the target output format to determine the target query text.
[0131] Figure 5 is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention, such as Figure 5 As shown, the electronic device may include: a processor 510, a communication interface 520, a memory 530 and a communication bus 540, wherein the processor 510, the communication interface 520 and the memory 530 communicate with each other through the communication bus 540. The processor 510 may call the logic instructions in the memory 530 to execute a graph database query method based on a large language model and a graph structure, the method comprising:
[0132] Obtaining a target graph structure corresponding to a target graph database and a query question input by a user; the target graph structure includes target graph node attributes, target graph edge attributes, and target graph edge relationships;
[0133] Determine a prompt statement based on a prompt template, the target graph node attributes, the target graph edge attributes, and the target graph edge relationship;
[0134] Input the prompt statement and the query question into a large language model, and output a query statement corresponding to the target graph database;
[0135] The query statement is executed in the target graph database to determine the target query text.
[0136] In addition, the logic instructions in the above-mentioned memory 530 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.
[0137] On the other hand, the present invention further provides a computer program product, the computer program product comprising a computer program, the computer program can be stored on a non-transitory computer-readable storage medium, and when the computer program is executed by a processor, the computer can execute the graph database query method based on a large language model and a graph structure provided by the above methods, the method comprising:
[0138] Obtaining a target graph structure corresponding to a target graph database and a query question input by a user; the target graph structure includes target graph node attributes, target graph edge attributes, and target graph edge relationships;
[0139] Determine a prompt statement based on a prompt template, the target graph node attributes, the target graph edge attributes, and the target graph edge relationship;
[0140] Input the prompt statement and the query question into a large language model, and output a query statement corresponding to the target graph database;
[0141] The query statement is executed in the target graph database to determine the target query text.
[0142] In another aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, which is implemented when the computer program is executed by a processor to execute the graph database query method based on a large language model and a graph structure provided by the above methods, the method comprising:
[0143] Obtaining a target graph structure corresponding to a target graph database and a query question input by a user; the target graph structure includes target graph node attributes, target graph edge attributes, and target graph edge relationships;
[0144] Determine a prompt statement based on a prompt template, the target graph node attributes, the target graph edge attributes, and the target graph edge relationship;
[0145] Input the prompt statement and the query question into a large language model, and output a query statement corresponding to the target graph database;
[0146] The query statement is executed in the target graph database to determine the target query text.
[0147] 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 they 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 scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0148] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0149] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A graph database query method based on a large language model and a graph structure, characterized in that: include: Obtaining a target graph structure corresponding to a target graph database and a query question input by a user; the target graph structure includes target graph node attributes, target graph edge attributes, and target graph edge relationships; Determine a prompt statement based on a prompt template, the target graph node attributes, the target graph edge attributes, and the target graph edge relationship; Input the prompt statement and the query question into a large language model, and output a query statement corresponding to the target graph database; Execute the query statement in the target graph database to determine the target query text; In the target graph structure, the target graph node attributes include multiple attribute information related to the nodes, the target graph edge attributes include multiple attribute information related to the edges, and the target graph edge relations are used to represent the connection relations and directionality between the nodes; The large language model is used to perform semantic analysis on the query question, identify the entities and the relationships between entities in the query question, and convert the entities and the relationships between entities into query elements in the target graph structure, determine the mapping results, and generate query statements corresponding to the target graph database based on the mapping results. The mapping results are used to characterize the mapping relationship between entities and nodes in the target graph structure, as well as the mapping relationship between relationships between entities and relationships in the target graph structure.
2. The graph database query method based on a large language model and a graph structure according to claim 1, characterized in that: The step of obtaining a target graph structure corresponding to the target graph database includes: Obtain metadata in the target graph database; the metadata includes element labels, element types, target types, and attribute names corresponding to nodes or edges; Based on the element type and the target type, filtering the target element from all metadata; Based on the element tag and the attribute name corresponding to the target element, a target graph structure corresponding to the target graph database is determined.
3. The graph database query method based on a large language model and a graph structure according to claim 2, characterized in that: The target element includes a first target node or a target edge; The determining, based on the element tag and the attribute name corresponding to the target element, a target graph structure corresponding to the target graph database includes: Determining a target graph node attribute in the target graph structure based on the element tag and the attribute name corresponding to the first target node; Determining a target graph edge attribute in the target graph structure based on the element tag and the attribute name corresponding to the target edge; Based on the element tag and the attribute name corresponding to the target edge, and information of a second target node related to the target edge, a target graph edge relationship in the target graph structure is determined.
4. The graph database query method based on a large language model and a graph structure according to claim 2, characterized in that: The filtering to obtain the target element from all metadata based on the element type and the target type includes: When the element type is a node and the target type is not a relationship, filtering from all metadata to obtain a first target node; When the element type is a relationship and the target type is not a node or the target type is a relationship, a target edge is obtained by filtering from all metadata.
5. The graph database query method based on a large language model and a graph structure according to any one of claims 1 to 4, characterized in that: The step of determining a prompt statement based on a prompt template, a target graph node attribute, a target graph edge attribute, and a target graph edge relationship includes: Determine the target general prompt word in the prompt template; the target general prompt word includes a first general prompt word, a second general prompt word and a third general prompt word; Update the first general prompt word based on the target graph node attribute; update the second general prompt word based on the target graph edge attribute; update the third general prompt word based on the target graph edge relationship; The updated prompt template is determined as the prompt sentence.
6. The graph database query method based on a large language model and a graph structure according to any one of claims 1 to 4, characterized in that: Executing the query statement in the target graph database to determine the target query text includes: Execute the query statement in the target graph database to obtain a first query result; The first query result and the query question are input into the large language model to determine the target query text.
7. The graph database query method based on a large language model and a graph structure according to claim 6, characterized in that: The step of inputting the first query result and the query question into the large language model to determine the target query text includes: Performing semantic analysis on the query question to determine a semantic analysis result corresponding to the query question; When the semantic analysis result indicates that the query question includes a target output format, the first query result is format-converted based on the target output format to determine the target query text.
8. A graph database query device based on a large language model and a graph structure, characterized in that: include: An acquisition module is used to acquire a target graph structure corresponding to a target graph database and a query question input by a user; the target graph structure includes target graph node attributes, target graph edge attributes and target graph edge relationships; A determination module, configured to determine a prompt statement based on a prompt template, the target graph node attributes, the target graph edge attributes, and the target graph edge relationship; An output module, used to input the prompt statement and the query question into a large language model, and output a query statement corresponding to the target graph database; A query module, used to execute the query statement in the target graph database to determine the target query text; In the target graph structure, the target graph node attributes include multiple attribute information related to the nodes, the target graph edge attributes include multiple attribute information related to the edges, and the target graph edge relations are used to represent the connection relations and directionality between the nodes; The large language model is used to perform semantic analysis on the query question, identify the entities and the relationships between entities in the query question, and convert the entities and the relationships between entities into query elements in the target graph structure, determine the mapping results, and generate query statements corresponding to the target graph database based on the mapping results. The mapping results are used to characterize the mapping relationship between entities and nodes in the target graph structure, as well as the mapping relationship between relationships between entities and relationships in the target graph structure.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the graph database query method based on a large language model and a graph structure is implemented as described in any one of claims 1 to 7.
10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the graph database query method based on a large language model and a graph structure as described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Method and device for constructing knowledge graph from PDF by using LLM based on graph database
CN117391192A
