Method and device for converting natural language query into GQL statement
By constructing a knowledge base of GQL syntax, graph primitive information, and graph query examples, and combining it with large model technology, the accuracy and reliability issues of natural language to GQL conversion were solved, achieving efficient graph query conversion and accurate results, and lowering the barrier for users to learn GQL syntax.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- ALIPAY (HANGZHOU) INFORMATION TECH CO LTD
- Filing Date
- 2026-03-12
- Publication Date
- 2026-04-10
AI Technical Summary
Existing natural language to GQL conversion solutions are insufficient to meet the accuracy and reliability requirements of complex business scenarios, especially in complex query scenarios such as multi-hop queries and conditional filtering. Users find it difficult to write correct and efficient GQL statements, which affects the application efficiency of graph databases.
By constructing a GQL syntax knowledge base, a graph meta-information knowledge base, and a graph query sample knowledge base, and using large model technology, natural language queries are converted into GQL statements. First, relevant knowledge fragments are retrieved from the knowledge base, and then input into the large model to generate GQL statements, ensuring grammatical standardization and meta-information matching.
It significantly improves the accuracy of converting natural language queries into GQL statements, enhances the accuracy and efficiency of graph query results, and lowers the barrier to entry for users.
Smart Images

Figure CN121833767A_ABST
Abstract
Description
Technical Field
[0001] This specification relates to the field of graph query technology, and more particularly to a method and apparatus for converting natural language queries into GQL statements using large model technology, a computer-readable storage medium, and a computing device. Background Technology
[0002] As data correlation and complexity become increasingly prominent across industries, graph data models, due to their ability to intuitively and efficiently represent entities and the rich relationships between them, are widely used in many fields such as social network analysis, financial risk control, recommendation systems, and cybersecurity.
[0003] Graph queries are the core method for retrieving and manipulating data from graph databases. When executing queries on graph databases, users typically need to write query statements using a dedicated Graph Query Language (GQL). However, due to the inherent abstractness and complexity of GQL, users need to master its specific syntax rules, keywords, functions, and traversal semantics, resulting in a high learning curve and a certain barrier to entry. In real-world business scenarios, many business personnel or data analysts, despite a clear understanding of business requirements, struggle to independently and efficiently complete query tasks due to unfamiliarity with GQL syntax. This is especially true when facing complex query scenarios such as multi-hop queries and conditional filtering; writing correct and efficient GQL statements becomes a significant challenge, directly impacting the full realization of the potential and application efficiency of graph databases.
[0004] To lower the barrier to entry for graph query services, the industry has begun exploring ways to automatically convert user query inputs, expressed in natural languages (such as Chinese and English), into standardized GQL statements. This approach allows users to interact with graph databases using intuitive natural language without needing to delve into the details of GQL syntax.
[0005] However, existing natural language to GQL conversion schemes still have significant limitations in practical applications and are difficult to meet higher requirements for query accuracy, reliability and performance. Summary of the Invention
[0006] This specification describes a method and apparatus for converting natural language queries into GQL statements, which can solve the above-mentioned technical problems.
[0007] According to the first aspect, a method for converting natural language queries into GQL statements is provided. The method includes: receiving a graph query statement in natural language form; performing matching in a GQL syntax knowledge base based on the graph query statement to retrieve several syntax knowledge fragments; and performing matching in a graph primitive information knowledge base to retrieve several graph primitive knowledge fragments. Based on the graph query statement, the several syntax knowledge fragments, and the several graph primitive knowledge fragments, a first prompt word is formed. The first prompt word is input into a first model to obtain the GQL statement corresponding to the graph query statement.
[0008] In one embodiment, the method further includes: matching in a graph query sample knowledge base based on the graph query statement to retrieve several sample knowledge fragments; wherein the formation of the first prompt word is also based on the several sample knowledge fragments.
[0009] In one embodiment, the knowledge fragments in each knowledge base have a predetermined format, which includes a title, content, and category.
[0010] Furthermore, in one specific embodiment, the construction of the GQL syntax knowledge base includes: determining multiple syntax knowledge fragments based on the GQL language specification to form the GQL syntax knowledge base. In any syntax knowledge fragment, the content includes a standard GQL statement and its corresponding syntax annotation; the title describes the function of the standard GQL statement; and the category indicates that the current knowledge fragment originates from the GQL language specification.
[0011] In another specific embodiment, the construction of the graph element information knowledge base includes: determining multiple graph element knowledge fragments based on the meta-information of the first graph data to form the graph element information knowledge base. In any graph element knowledge fragment, the title includes the type name of the node / edge, the content includes the attribute definition of the node / edge, and the category indicates that the current knowledge fragment comes from the meta-information.
[0012] Furthermore, in one example, the method further includes: inserting a corresponding graph element knowledge fragment into the graph element information knowledge base according to the node / edge type added in the meta information; and / or deleting a corresponding graph element knowledge fragment from the graph element information knowledge base according to the node / edge type deleted from the meta information; and / or modifying a corresponding graph element knowledge fragment in the graph element information knowledge base according to the modification of the node / edge type in the meta information.
[0013] In another specific embodiment, the construction of the graph query example knowledge base includes: determining multiple example knowledge fragments based on graph query example data to form the graph query example knowledge base. In any example knowledge fragment, the title includes a graph query statement example in natural language form corresponding to the GQL statement example, the content includes the GQL statement example, and the category indicates that the current knowledge fragment comes from the graph query example data.
[0014] In another specific embodiment, matching is performed in the GQL syntax knowledge base based on the graph query statement, including: converting the graph query statement into a target query fragment with the predetermined format, wherein the title includes the graph query statement, the content includes the node / edge information to be queried extracted from the graph query statement, and the category is empty. Matching is then performed in the GQL syntax knowledge base based on the target query fragment.
[0015] In one embodiment, before recalling several primitive knowledge fragments, the method further includes: determining a first graph identifier corresponding to the first entry identifier that receives the graph query statement, based on a pre-established mapping relationship between entry identifiers and graph identifiers; and determining the primitive information knowledge base corresponding to the first graph identifier from multiple primitive information knowledge bases.
[0016] In one embodiment, recalling a plurality of grammatical knowledge fragments includes: calculating the similarity between the semantic vector of the graph query statement and the semantic vector of each grammatical knowledge fragment in the GQL grammatical knowledge base, and determining the plurality of grammatical knowledge fragments corresponding to a plurality of similarities that are located in a predetermined upper range.
[0017] The process of recalling several graph element knowledge fragments includes: calculating the similarity between the semantic vector of the graph query statement and the semantic vector of each graph element knowledge fragment in the graph element information knowledge base, and determining the graph element knowledge fragments corresponding to several similarities that are located in a predetermined first range.
[0018] According to a second aspect, an apparatus for converting natural language queries into GQL statements is provided. The apparatus includes: a natural language query receiving module configured to receive graph query statements in natural language form; a knowledge fragment retrieval module configured to, based on the graph query statement, perform matching in a GQL syntax knowledge base to retrieve several syntax knowledge fragments; and perform matching in a graph primitive information knowledge base to retrieve several graph primitive knowledge fragments; a prompt word construction module configured to, based on the graph query statement, the several syntax knowledge fragments, and the several graph primitive knowledge fragments, form a first prompt word; and a large model processing module configured to input the first prompt word into a first large model to obtain the GQL statement corresponding to the graph query statement.
[0019] According to a third aspect, a computer-readable storage medium is provided having a computer program stored thereon, which, when executed in a computer, causes the computer to perform the method provided in the first aspect.
[0020] According to a fourth aspect, a computing device is provided, including a memory and a processor, wherein the memory stores executable code, and the processor, when executing the executable code, implements the method provided in the first aspect.
[0021] In summary, by using the methods and apparatus disclosed in the embodiments of this specification, after receiving a graph query statement in natural language form, several knowledge fragments are first retrieved from knowledge bases such as GQL syntax and graph primitive information, and then the retrieved knowledge fragments and graph query statements are combined and input into a large model for the final GQL statement output. This can significantly improve the accuracy of converting natural language graph query statements into GQL query statements. Attached Figure Description
[0022] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the following description of the embodiments will be briefly introduced. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0023] Figure 1 This is a schematic diagram of the implementation architecture of the scheme for converting natural language queries into GQL statements disclosed in the embodiments of this specification; Figure 2 This is a schematic diagram of the method steps for converting natural language queries into GQL statements as disclosed in the embodiments of this specification; Figure 3 This is a schematic diagram of the apparatus structure for converting natural language queries into GQL statements, as disclosed in the embodiments of this specification. Detailed Implementation
[0024] The solution provided in this specification will now be described with reference to the accompanying drawings.
[0025] As mentioned earlier, to lower the barrier to entry for graph queries, the industry has proposed a technical approach of converting natural language queries into GQL statements. Early solutions mainly relied on rule-based or template-based static parsing and conversion, which had limited flexibility and generalization capabilities. With the rise of large language models (or simply large models) technology, the industry began to explore a new paradigm of directly inputting natural language queries into large models, which then generated GQL statements end-to-end. However, although large models excel in natural language understanding, the GQL statements they directly generate still suffer from insufficient accuracy in practical applications, making it difficult to meet the higher requirements for query reliability, accuracy, and execution efficiency in complex business scenarios.
[0026] The applicant's research found that the low accuracy of GQL statements generated by large models is mainly reflected in the following two aspects: First, at the semantic mapping level, the generated GQL statements often do not match the metadata of the target business graph data, and cannot correctly correspond to the node types, edge relationships, attribute definitions and constraints in the metadata, resulting in the query logic being disconnected from the underlying data structure, thus causing execution failure or result deviation; Second, at the syntax standardization level, the generated statements may not fully comply with the current GQL language standard, affecting the stable execution of queries.
[0027] Based on the above observations and analysis, the applicant proposed inputting graph data metadata (or graph metadata for short) and GQL language specifications simultaneously with the input of natural language into the large model. However, there is a size limit to the content submitted to the large model at one time.
[0028] Furthermore, the applicant proposed an improvement plan, see [link to relevant document]. Figure 1 :
[0029] In the preparation stage (which can be executed offline), the metadata of the target graph data and the GQL language specification are broken down into multiple knowledge fragments and assigned to their respective knowledge bases.
[0030] Subsequently, during the graph query service provision phase (usually executed online), Retrieval-Augmented Generation (RAG) technology is employed. Based on the user-input natural language graph query, a pre-built knowledge base is retrieved to locate relevant knowledge fragments. These retrieved knowledge fragments, along with the user-input graph query, are then input into a large model to generate a GQL statement that matches graph primitive information and conforms to grammatical rules. Furthermore, the generated GQL statement can be used to query target graph data to obtain the corresponding graph query results. This significantly improves the accuracy of the GQL statement, thereby enhancing the accuracy of the graph query results.
[0031] Next, we will elaborate on the specific implementation steps of the two phases in the above improvement plan.
[0032] Phase 1: Preparatory Phase
[0033] 1. Constructing a GQL syntax knowledge base based on the GQL language specification
[0034] It's important to understand that the GQL language specification is a graph query language specification that provides a complete and clear syntactic and semantic framework for manipulating graph data. This specification systematically defines the constituent elements of the GQL language, primarily including: 1) Query clause syntax: defining the structure and combination rules for key clauses such as MATCH, RETURN, WHERE, and ORDER BY. 2) Data manipulation syntax: standardizing the syntax for adding, deleting, and modifying graph structures using statements such as INSERT, UPDATE, DELETE, and MERGE. 3) Function and operator system: including the formal syntax for scalar functions, aggregate functions, predicates, and various operators. 4) Graph schema declaration syntax: defining the expression syntax used to describe vertices, edges, and their connections in a graph. Furthermore, the GQL language specification can also be ISOGQL, which is an international standard graph query language.
[0035] By performing the first processing on the GQL specification document, multiple syntactic knowledge fragments can be obtained, forming the aforementioned GQL syntactic knowledge base. It should be noted that the terms "first" in "first processing," and similar terms such as "second" and "third" elsewhere in the text, are used to distinguish similar items and do not have any ranking or other limiting function.
[0036] In one embodiment, the first processing can be implemented using a rule engine (static parsing). In another embodiment, the first processing can also be accomplished using a large model. For example, the GQL language specification and related task description can be input into a second large model to obtain multiple grammatical knowledge fragments as output. For example, the task description could be: Please break down the given GQL language specification into appropriately granular knowledge fragments, which will then be used to assist in converting natural language queries into GQL statements. Each knowledge fragment includes a grammatical element or a grammatical rule.
[0037] On the other hand, to improve the efficiency and accuracy of subsequent retrieval of the knowledge base, and thus enhance the accuracy and usability of the generated GQL statements, it is proposed to standardize the knowledge fragments in each knowledge base into a predetermined format. This predetermined format may include the following components: title, content, and category. The content refers to the specific knowledge information, the title is a brief description of the knowledge content, and the category indicates the data source of the knowledge content.
[0038] Specifically, in any grammar knowledge fragment, the title section can describe the function of the standard GQL statement, the content section can include the standard GQL statement and its corresponding syntax comments, descriptive information, etc., and the category section can indicate that the current knowledge fragment comes from the GQL language specification. See Table 1 below, which provides an example of a grammar knowledge fragment, where the content section includes "gql" (corresponding to the standard GQL statement), "gql_syntax" (corresponding to the syntax comment), and "describe" (corresponding to the descriptive information).
[0039] Table 1: Examples of Grammatical Knowledge Segments
[0040] title content category Scan points of a single type and return the point's attributes. { "gql": "MATCH (n:person) RETURN n.name", "gql_syntax": { "syntax_elements": { "node_pattern": "(alias:nodeType)","property_access": "alias.property"}, "syntax_content": "MATCH (node_variable:node_type) RETURN node_variable.property","match_clause": { "components": { "relationships": [], "nodes":[ { "alias": "u", "nodeType": "person","properties": {}} ]}, "pattern": "(n:person)"}}, "describe": "A GQL statement that matches nodes of the specified type and returns the value of a specific attribute of that node. It does not involve edges or relationships and only performs single-point queries."} GQL Language Specification
[0041] The above describes the composition and construction method of the GQL syntax knowledge base.
[0042] 2. Construct a primitive information knowledge base based on primitive information
[0043] Graph metadata describes the structure and system state of graph data. It does not contain specific business data (such as individual user nodes or transaction records), but rather describes how this data is organized, stored, and distributed. It should be noted that all user-related data mentioned in this article was legally collected and used after the user confirmed and authorized its use.
[0044] Graph primitive information mainly includes the graph schema, which describes the node types, edge types, and structural constraints of the graph. For example, node types include the node type name (e.g., person), attribute definitions (e.g., the person type has attributes such as name, age, and email), and descriptions of each attribute; edge types include the edge type name (e.g., PURCHASED), attribute definitions (e.g., the PURCHASED edge may have attributes such as timestamp and amount); structural constraints may include uniqueness constraints (e.g., Person.id is unique), and the data type of attributes (e.g., FLOAT, STRING), etc. It should be noted that the construction of the graph primitive information knowledge base can be based solely on the graph schema.
[0045] Graph element information can also include the distribution of graph replicas, graph statistics, etc.
[0046] By performing a second processing on the metadata of the first graph data, multiple graph primitive knowledge fragments can be obtained, forming the graph primitive information knowledge base. It should be noted that the first graph data can refer to any graph data or graph model; the second processing can be implemented through a rule engine (static parsing) or with the help of a large model.
[0047] In one particular implementation, multiple primitive knowledge fragments can also be standardized to have the aforementioned predetermined format, with components including a title, content, and classification.
[0048] Specifically, in any primitive knowledge fragment, the title portion may include the type name of the node / edge, the content portion may include the attribute definition and related descriptive information of the node / edge, and the category portion may indicate that the current knowledge fragment comes from primitive information. See Table 2 below, which provides an example of a primitive knowledge fragment. The title portion includes task_t (the type name of the corresponding node), the content portion includes "tableProperties" (the attribute definition of the corresponding node) and "describe" (the descriptive information), and the category is primitive information.
[0049] Table 2: Examples of Grammatical Knowledge Fragments
[0050] title content category task_t "tableType": "NODE","tableProperties":[{ "propertyName": "owner_id","propertyType": "STRING", "alias": "Responsible Person ID", "isPrimaryKey": false}, { "propertyName": "max_run_time_30d", "propertyType": "INT64","alias": "Longest Run Time in the Last 30 Days", "isPrimaryKey": false}],"alias": "Task","describe": "The table type is dot, the table type name is task_t, the table primary key is node_guid, and the table attribute is task_name","tableName": "task_t" Image element information
[0051] It should be noted that standardizing the primitive knowledge fragments into the aforementioned predetermined format can effectively improve the update efficiency of the primitive information knowledge base. Specifically, in one embodiment, a corresponding primitive knowledge fragment can be inserted into the primitive information knowledge base based on the node / edge type added to the primitive information. In another embodiment, a corresponding primitive knowledge fragment can be deleted from the primitive information knowledge base based on the node / edge type deleted from the primitive information. In yet another embodiment, a corresponding primitive knowledge fragment can be modified in the primitive information knowledge base based on the modification of the node / edge type in the primitive information.
[0052] The above uses any single graph data to illustrate the composition and construction method of its corresponding graph element information knowledge base. It should be understood that, assuming multiple graph data exist in a real-world business scenario, a separate graph element information knowledge base can be constructed for each graph data. For example, graph item A corresponds to graph element information knowledge base A, graph item B corresponds to graph element information knowledge base B, and so on.
[0053] 3. Construct a graph query sample knowledge base based on graph query sample data.
[0054] The graph query sample data includes various query examples, such as graph query statements in natural language and corresponding GQL statements. The graph query sample data can come from various sources, such as historical query logs for the first graph data, or query example data written by staff for the first graph data.
[0055] Specifically, a third processing step can be performed on the graph query sample data for the first graph data to obtain multiple sample knowledge fragments, forming a graph query sample knowledge base. Each sample knowledge fragment can include one or a series of query samples. It should be noted that the third processing can be implemented through a rule engine (static parsing) or with the help of a large model.
[0056] In one particular implementation, multiple sample knowledge fragments can also be standardized to have the aforementioned predetermined format, with components including a title, content, and classification.
[0057] Specifically, in any sample knowledge fragment, the title may include a sample graph query statement in natural language, the content may include a sample GQL statement corresponding to that graph query statement, and the category indicates that it comes from the graph query sample data. Table 3 below provides an example of a sample knowledge fragment.
[0058] Table 3: Examples of Sample Knowledge Fragments
[0059] title content category Find all tasks created by the person with the administrator ID 'user_001', and return the names of these tasks and their longest run time in the last 30 days. MATCH (t:task_t WHERE t.owner_id = 'user_001') RETURN t.task_name AS task_name, t.max_run_time_30d AS longest run time in the last 30 days; Example data for graph query
[0060] The above describes the composition and construction method of the graph query sample knowledge base.
[0061] It should be noted that the relative order of constructing different knowledge bases is not uniquely limited in the embodiments described in this specification. Furthermore, after the preparatory stage is completed, the online query stage can commence.
[0062] Phase Two: Provision of Graph Query Services
[0063] See Figure 2 The document illustrates the method and steps for converting natural language queries into GQL statements. It should be noted that the entity executing this method and the entity executing the aforementioned knowledge base construction can be any device, platform, server, or device cluster with computing and processing capabilities.
[0064] Figure 2 The following method steps are shown:
[0065] Step S210: Receive graph query statements in natural language form.
[0066] Generally, it can receive graph query statements entered by users in natural language through interactive interfaces (such as web front-end input boxes or mobile application dialog boxes). These statements are usually unstructured free text, with flexible expression formats, and do not require users to master professional graph query syntax.
[0067] For example, a graph query could be: Help me find all branch office contacts located in location B who have had transactions with customer A within the last three months.
[0068] From the above, we can receive natural language queries input by the user.
[0069] Step S220 involves matching the received graph query statement in the GQL syntax knowledge base to retrieve several syntax knowledge fragments; and matching in the graph primitive information knowledge base to retrieve several graph primitive knowledge fragments. It should be noted that "several" in this text refers to one or more.
[0070] Regarding the graph element information knowledge base on which the recall is based, in one implementation scenario, the business scenario involves multiple graph data. Accordingly, multiple graph element information knowledge bases are constructed in the preparatory stage. In this case, it is necessary to first locate the graph data that the graph query statement intends to query, and then locate the corresponding graph element information knowledge base.
[0071] Specifically, firstly, based on the pre-established mapping relationship between entry identifiers and graph identifiers, the first graph identifier corresponding to the first entry identifier that receives the graph query statement can be determined; then, the graph element information knowledge base corresponding to the first graph identifier can be determined from multiple graph element information knowledge bases.
[0072] For example, assuming a user enters a query from a financial portal, the query can be routed to the graph information knowledge base corresponding to the financial knowledge graph; or assuming a user enters a query from a medical portal, the query can be routed to the graph information knowledge base corresponding to the medical literature knowledge graph.
[0073] In another implementation scenario, the business scenario involves only one graph data. Accordingly, only one graph element information knowledge base is built in the preparation stage. In this case, it can be directly retrieved from this graph element information knowledge base.
[0074] On the other hand, in one embodiment, a graph query sample knowledge base is also constructed in the preparatory stage. Accordingly, this step may also include: matching in the graph query sample knowledge base based on the graph query statement to recall several sample knowledge fragments.
[0075] On the other hand, in one embodiment, when the knowledge fragments contained in each knowledge base are all structured into a predetermined format, the graph query statement can be converted into a target query fragment with a predetermined format before matching and recall, thereby further improving the recall accuracy.
[0076] Specifically, in the target query fragment, the title may include the current graph query statement, the content may include the node information, edge information, and query mode information (such as multi-hop query or variable-hop query) extracted from the graph query statement, and the category may be empty.
[0077] To determine the content (or query intent) in the target query segment, one specific embodiment can rely on a predefined domain rule base to match the graph query statement with a preset pattern template, extracting elements through keyword recognition and fixed sentence parsing. For example, when the template "[Entity A]'s [Relationship]'s [Entity B]" is matched, two nodes and one relationship can be directly extracted. In another specific embodiment, the extraction task is treated as a sequence labeling problem. By training a machine learning model (such as BiLSTM-CRF or a BERT-based model), the corresponding node / edge label (such as node A, edge B, pattern C) is predicted for each word in the graph query statement.
[0078] On the other hand, the matching and retrieval of graph query statements and knowledge fragments in the knowledge base in this step will be mainly explained using the GQL syntax knowledge base as an example. The matching and retrieval of knowledge fragments in other knowledge bases can be deduced in the same way, and will not be elaborated further.
[0079] In one embodiment, this can be achieved by calculating the similarity of semantic vectors.
[0080] First, determine the semantic vector of the graph query statement. For example, the graph query statement can be input into a trained semantic embedding model. It should be understood that the semantic embedding model can be an open-source model or can be trained independently.
[0081] Then, for each grammatical knowledge fragment in the GQL grammar knowledge base, the similarity between its corresponding semantic vector and the semantic vector of the graph query statement is calculated. It should be noted that the semantic vectors of each grammatical knowledge fragment can also be obtained by inputting a pre-trained semantic embedding model. Furthermore, these semantic vectors can be calculated in the preparatory stage so that they can be used directly in the online service.
[0082] Next, from all the calculated similarities, a number of similarities falling within a predetermined top range are determined, thus obtaining several corresponding grammatical knowledge fragments. It should be understood that the predetermined top range refers to the top subset of results that meet a preset ranking or percentage selection criterion in the result sequence sorted in descending order based on similarity. For example, the selection criterion could be set to the top 5 or the top 0.01%.
[0083] In another embodiment, the matching and retrieval of knowledge fragments in the knowledge base can also be achieved using a community clustering algorithm.
[0084] Based on the above, similar knowledge fragments can be retrieved from the GQL syntax knowledge base, the graph element information knowledge base (or, the graph query example knowledge base) according to the graph query statement.
[0085] Step S230: Based on the graph query statement, the plurality of grammatical knowledge fragments, and the plurality of graph element knowledge fragments, a first prompt word is formed. It can be understood that if several sample knowledge fragments are determined in the preceding steps, the formation of the first prompt word can also be based on these sample knowledge fragments.
[0086] In one embodiment, the graph query statement and the recalled knowledge fragments (such as several grammar knowledge fragments, several graph primitive knowledge fragments, and several sample knowledge fragments), as well as the task description of the GQL statement generation task, can be directly concatenated to obtain the first prompt word.
[0087] In another embodiment, the graph query statement and the recalled knowledge fragments can be filled into the prompt word template set for the GQL statement generation task to obtain the first prompt word.
[0088] For example, the prompt word template can be:
[0089] "The following is an introduction to the relevant ISOGQL syntax:"
[0090] [#Requires filling in grammar knowledge fragments#]
[0091] Below is the relevant diagram schema information:
[0092] [#Knowledge fragments to be filled in#]
[0093] Below are similar historical query examples:
[0094] [#Sample knowledge fragments need to be filled in#]
[0095] Based on the above information, please convert the current [#query statement to be filled in#] to GQL format?
[0096] From the above, we can obtain the first clue word.
[0097] Step S240: Input the first prompt word into the first major model to obtain the GQL statement corresponding to the graph query statement. It should be noted that the first major model and the second major model mentioned above can be the same or different.
[0098] For example, assuming the graph query is "find all students who have studied at University A", the GQL statement output by the first large model could be:
[0099] MATCH (p:Person)-[:STUDIED_AT]->(u:University {name: 'A University'})
[0100] RETURN p.name AS student_name
[0101] The above demonstrates how to convert graph query statements in natural language format into corresponding GQL statements. Furthermore, this GQL statement can be used to query the first graph data in the graph database to obtain the graph query results.
[0102] In summary, the method for converting natural language queries into GQL statements disclosed in the embodiments of this specification, after receiving a graph query statement in natural language form, first retrieves several knowledge fragments from knowledge bases such as GQL syntax and graph primitive information, and then combines the retrieved knowledge fragments with the graph query statement and inputs them into a large model for the final GQL statement output. This can significantly improve the accuracy of converting natural language graph query statements into GQL query statements.
[0103] Corresponding to the conversion method described above, embodiments of this specification also disclose corresponding conversion devices. See [link to documentation]. Figure 3 The conversion device 300 shown includes the following functional modules:
[0104] The natural language query receiving module 310 is configured to receive graph query statements in natural language form. The knowledge fragment retrieval module 320 is configured to, based on the graph query statement, perform matching in a GQL syntax knowledge base to retrieve several syntax knowledge fragments; and perform matching in a graph primitive information knowledge base to retrieve several graph primitive knowledge fragments. The prompt word construction module 330 is configured to, based on the graph query statement, the several syntax knowledge fragments, and the several graph primitive knowledge fragments, form a first prompt word. The large model processing module 340 is configured to input the first prompt word into a first large model to obtain the GQL statement corresponding to the graph query statement.
[0105] In one embodiment, the knowledge fragment retrieval module 320 is further configured to: perform matching in the graph query sample knowledge base based on the graph query statement, and retrieve several sample knowledge fragments; wherein, the formation of the first prompt word is also based on the several sample knowledge fragments.
[0106] In one embodiment, the knowledge fragments in each knowledge base have a predetermined format, which includes a title, content, and category.
[0107] Furthermore, in one specific embodiment, the construction of the GQL syntax knowledge base includes: determining multiple syntax knowledge fragments based on the GQL language specification to form the GQL syntax knowledge base. In any syntax knowledge fragment, the content includes a standard GQL statement and its corresponding syntax annotation; the title describes the function of the standard GQL statement; and the category indicates that the current knowledge fragment originates from the GQL language specification.
[0108] In one specific embodiment, the construction of the graph element information knowledge base includes: determining multiple graph element knowledge fragments based on the meta-information of the first graph data to form the graph element information knowledge base. Specifically, in any graph element knowledge fragment, the title includes the type name of the node / edge, the content includes the attribute definition of the node / edge, and the category indicates that the current knowledge fragment originates from the meta-information.
[0109] Furthermore, in one example, the conversion device 300 further includes a primitive knowledge base update module 350, configured to: insert corresponding primitive knowledge fragments into the primitive information knowledge base according to the node / edge types added in the primitive information; and / or, delete corresponding primitive knowledge fragments into the primitive information knowledge base according to the node / edge types deleted from the primitive information; and / or, modify corresponding primitive knowledge fragments into the primitive information knowledge base according to the modifications to the node / edge types in the primitive information.
[0110] In one specific embodiment, the construction of the graph query example knowledge base includes: determining multiple example knowledge fragments based on graph query example data to form the graph query example knowledge base. Specifically, in any example knowledge fragment, the title includes a graph query statement example in natural language form corresponding to the GQL statement example, the content includes the GQL statement example, and the category indicates that the current knowledge fragment comes from the graph query example data.
[0111] In a specific embodiment, the knowledge fragment retrieval module 320 is specifically configured to: convert the graph query statement into a target query fragment with the predetermined format, wherein the title includes the graph query statement, the content includes the node / edge information to be queried extracted from the graph query statement, and the category is empty; and perform matching in the GQL syntax knowledge base based on the target query fragment.
[0112] In one embodiment, the conversion device 300 further includes a primitive information knowledge base positioning module 360, configured to: determine the first graph identifier corresponding to the first entry identifier that receives the graph query statement based on a pre-established mapping relationship between entry identifiers and graph identifiers; and determine the primitive information knowledge base corresponding to the first graph identifier from multiple primitive information knowledge bases.
[0113] In one embodiment, the knowledge fragment retrieval module 320 is specifically configured to: calculate the similarity between the semantic vector of the graph query statement and the semantic vectors of each grammatical knowledge fragment in the GQL grammar knowledge base, and determine the grammar knowledge fragments corresponding to a certain number of similarities within a predetermined high range. Additionally, it calculates the similarity between the semantic vector of the graph query statement and the semantic vectors of each graph primitive knowledge fragment in the graph primitive information knowledge base, and determines the grammar knowledge fragments corresponding to a certain number of similarities within a predetermined high range.
[0114] It should be noted that for a description of the above-mentioned functional modules or functional units, please refer to the relevant description of the process method in the foregoing embodiments.
[0115] In this specification, the Large Language Model (LLM) may also be referred to simply as the Large Model. A Large Language Model is a natural language processing model based on deep learning techniques, typically with billions to hundreds of billions or even more parameters, possessing powerful language understanding and generation capabilities. Large Language Models can employ the Transformer architecture or its variants (such as GPT, BERT, etc.), which utilizes an attention mechanism to globally model sequential data, efficiently handling long-distance dependencies and thus performing exceptionally well in natural language tasks. Large Language Models learn the statistical features and semantic relationships of language through pre-training on large-scale corpora, giving them outstanding generalization capabilities. The core capabilities of Large Language Models include, but are not limited to: understanding contextual semantics, generating coherent and grammatically correct text, performing logical reasoning, and handling multi-task scenarios. Its usage typically includes two modes: direct inference and fine-tuning. In direct inference mode, the user guides the Large Language Model to generate specific outputs by designing prompts. Cue words can be task descriptions or instructions in text form, used to stimulate the semantic understanding and generation capabilities of large language models. In fine-tuning mode, large language models are further trained on small-scale datasets in specific domains to optimize their performance on specific tasks. The powerful generalization ability and flexibility of large language models make them an important tool in the field of artificial intelligence, providing efficient and accurate solutions for automated text generation and understanding.
[0116] In some embodiments, large language models can also understand and generate data from other modalities (such as visual and audio data). In this case, large language models can also be called multimodal large language models (MLLMs). MLLMs provide a richer and more natural interactive experience by integrating multiple types of input and output, such as text, images, and sound. The core advantage of MLLMs lies in their ability to process and understand information from different modalities and fuse this information to complete complex tasks. For example, MLLMs can analyze an image and generate descriptive text, or generate a corresponding image based on a text description. This cross-modal understanding and generation capability makes MLLMs widely applicable across multiple fields.
[0117] It should be noted that the key technologies of large language models can be found in the detailed description in the paper "A Survey of Large Language Models" (paper number: arXiv:2303.18223v16, published on March 11, 2025, public link: https: / / doi.org / 10.48550 / arXiv.2303.18223), and will not be repeated here.
[0118] According to another embodiment, a computer-readable storage medium is also provided, on which a computer program is stored, which, when executed in a computer, causes the computer to perform... Figure 2 The method described.
[0119] According to another embodiment, a computing device is also provided, including a memory and a processor, wherein the memory stores executable code, and the processor executes the executable code to implement... Figure 2 The method described.
[0120] Those skilled in the art will recognize that, in one or more of the examples above, the functions described in this invention can be implemented using hardware, software, firmware, or any combination thereof. When implemented in software, these functions can be stored in a computer-readable medium or transmitted as one or more instructions or code on a computer-readable medium.
[0121] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made on the basis of the technical solution of the present invention should be included within the scope of protection of the present invention.
Claims
1. A method for converting natural language queries into GQL statements, comprising: Accepts graph query statements in natural language format; Based on the graph query statement, a match is performed in the GQL syntax knowledge base to retrieve several syntax knowledge fragments. And by matching in the primitive information knowledge base, several primitive knowledge fragments are retrieved; Based on the graph query statement, the several grammatical knowledge fragments, and the several graph element knowledge fragments, a first prompt word is formed; Input the first prompt word into the first large model to obtain the GQL statement corresponding to the graph query statement.
2. The method according to claim 1, wherein, The method further includes: Based on the graph query statement, a matching is performed in the graph query sample knowledge base to retrieve several sample knowledge fragments; The formation of the first prompt word is also based on the aforementioned sample knowledge fragments.
3. The method according to claim 1 or 2, wherein the knowledge fragments in each knowledge base have a predetermined format, the predetermined format including title, content and category.
4. The method according to claim 3, wherein, The construction of the GQL syntax knowledge base includes: Based on the GQL language specification, multiple syntactic knowledge fragments are identified to form the GQL syntactic knowledge base; In any given grammatical knowledge fragment, the content includes a standard GQL statement and its corresponding grammatical annotation, the title describes the function of the standard GQL statement, and the category indicates that the current knowledge fragment comes from the GQL language specification.
5. The method according to claim 3, wherein, The construction of the primitive information knowledge base includes: Based on the metadata of the first graph data, multiple graph element knowledge fragments are determined to form the graph element information knowledge base; In any graph primitive knowledge fragment, the title includes the type name of the node / edge, the content includes the attribute definition of the node / edge, and the category indicates that the current knowledge fragment comes from the meta-information.
6. The method of claim 5, further comprising: Based on the node / edge type added in the metadata, insert the corresponding metadata knowledge fragment into the metadata knowledge base; And / or, Based on the type of node / edge deleted from the metadata, delete the corresponding metadata fragment from the metadata knowledge base; and / or, Based on the modification of the node / edge type in the metadata, the corresponding metadata knowledge fragment is modified in the metadata knowledge base.
7. The method according to claim 3, wherein, The construction of the graph query sample knowledge base includes: Based on the graph query sample data, multiple sample knowledge fragments are determined to form the graph query sample knowledge base; In any sample knowledge fragment, the title includes a graph query statement example in natural language form corresponding to the GQL statement example, the content includes the GQL statement example, and the category indicates that the current knowledge fragment comes from the graph query example data.
8. The method according to claim 3, wherein, Based on the graph query statement, matching is performed in the GQL syntax knowledge base, including: The graph query statement is converted into a target query fragment with the predetermined format, wherein the title includes the graph query statement, the content includes the node / edge information to be queried extracted from the graph query statement, and the category is empty; The target query fragment is matched against the GQL syntax knowledge base.
9. The method according to claim 1, wherein, Before recalling several primitive knowledge fragments, the method further includes: Based on the pre-established mapping relationship between entry identifiers and graph identifiers, the first graph identifier corresponding to the first entry identifier that receives the graph query statement is determined; The graphic element information knowledge base corresponding to the first graphic element identifier is determined from multiple graphic element information knowledge bases.
10. The method according to claim 1, wherein, Recall several grammar knowledge fragments, including: Calculate the similarity between the semantic vector of the graph query statement and the semantic vector of each grammatical knowledge segment in the GQL grammatical knowledge base, and determine the grammatical knowledge segments corresponding to a certain number of similarities that are located in a predetermined early range. This includes recalling several primitive knowledge fragments, including: Calculate the similarity between the semantic vector of the graph query statement and the semantic vector of each graph element knowledge fragment in the graph element information knowledge base, and determine the graph element knowledge fragments corresponding to a certain number of similarities that are located in a predetermined first range.
11. An apparatus for converting natural language queries into GQL statements, comprising: The natural language query receiving module is configured to receive graph query statements in natural language form. The knowledge fragment retrieval module is configured to match the graph query statement in the GQL syntax knowledge base and retrieve several syntax knowledge fragments. And by matching in the primitive information knowledge base, several primitive knowledge fragments are retrieved; The prompt word construction module is configured to form a first prompt word based on the graph query statement, the several grammatical knowledge fragments, and the several graph element knowledge fragments; The large model processing module is configured to input the first prompt word into the first large model to obtain the GQL statement corresponding to the graph query statement.
12. A computer-readable storage medium having a computer program stored thereon, wherein, When the computer program is executed in the computer, it causes the computer to perform the method according to any one of claims 1-10.
13. A computing device comprising a memory and a processor, wherein, The memory stores executable code, and when the processor executes the executable code, it implements the method of any one of claims 1-10.
Citation Information
Patent Citations
Knowledge question and answer method and device, electronic equipment and storage medium
CN119760070A
Graph query statement generation method, data query method and related devices
CN120653804A
Data analysis method and system based on retrieval enhancement
CN121029786A
Data query method and device, equipment, storage medium and product
CN121478813A
Mixed retrieval method and system for multi-dimensional heterogeneous knowledge recall enhancement
CN121636664A