Graph Database Query Method and Device
By introducing a semantic layer and a controlled execution layer between the large model and the graph database, the problems of uncontrollability and poor readability of results caused by the large model directly generating query statements for the underlying graph database are solved, realizing the expressive power of natural language queries and the accuracy and controllability of graph database access.
Patent Information
- Application Number
- CN202611594186.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-10-15
- Publication Date
- 2026-11-10
AI Technical Summary
In existing technologies, directly generating underlying graph database query statements from large models leads to problems such as uncontrollable query behavior, easy errors in understanding label and attribute fields, lack of unified execution constraints, and poor readability of returned results.
By introducing a semantic layer and a controlled execution layer, the system obtains natural language query requests from the user, uses a large model agent to understand the intent and convert it into structured semantic query parameters, generates controlled graph database query statements on the server side, and processes the results semantically before returning them to the large model agent.
It achieves controllability of large model query behavior and accuracy of results, improves the accuracy of graph database access and controllability of enterprise system execution, and ensures the readability and stability of returned results.
Smart Images

Figure CN123068698A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of graph database query technology, and in particular to a graph database query method and apparatus. Background Technology
[0002] With the development of large language models and intelligent agent frameworks, intelligent systems are gradually gaining the ability to invoke external tools, access business systems, and retrieve structured data. In question-answering or analysis scenarios involving graph databases, existing technologies typically include the following solutions: The first approach involves developers pre-writing several fixed graph data query templates or interfaces, with the large model selecting one to call based on the user's question. While this approach is controllable, it lacks expressive power. When faced with multi-step problems requiring locating candidate objects, examining hierarchical relationships, pre- and post-relationships, or associating with external data, it necessitates numerous interface combinations, causing the number of interfaces to balloon as the business scenario expands.
[0003] The second approach involves a large model directly generating raw graph data query statements based on the user's natural language question, the graph database schema description, and a few examples, before submitting them to the graph database for execution. In this approach, the query statement's expressive space is too large, making it difficult to stably constrain query type, traversal depth, returned fields, pagination count, and extended content. This can easily lead to query failures, excessively large results, context bloat, or inconsistent returned structures. Furthermore, the underlying graph database typically contains numerous business tags, compatibility tags, relation edges, and historical fields. The same business meaning may correspond to multiple underlying fields or tags, and the large model's direct understanding of the underlying schema can easily result in errors in tag names, relation names, attribute names, or directional understanding.
[0004] The third approach involves the application system encapsulating independent query interfaces for each type of business problem, with the large model only responsible for selecting the interface and filling in a few parameters. This approach lacks flexibility, requiring the development of a separate interface for each new type of business problem, making it difficult to adapt to the complex and ever-changing enterprise graph query needs.
[0005] Furthermore, existing solutions typically return low-level attributes from the graph database directly to the large model or user. For example, fields such as responsible person, planned working hours, actual working hours, and planned start time may use internal English abbreviations or historical naming conventions at the low level. Attributes of related data objects may also come from business forms, object schemas, or external business systems. Direct exposure reduces readability and increases the risk of model misunderstanding. In enterprise-level graphs, queries often need to be executed in conjunction with contexts such as tenant, user, business domain, and task scope. If existing solutions lack a unified, controlled execution layer, they are prone to problems such as cross-scope erroneous queries, unclear permission boundaries, or unreproducible results.
[0006] Therefore, how to provide a graph database query method for large-scale intelligent agents that balances the expressive power of natural language queries, the accuracy of graph database access, and the controllability of enterprise system execution has become an urgent problem to be solved. Summary of the Invention
[0007] This invention provides a graph database query method and apparatus to address the shortcomings of existing technologies where large models directly generate underlying graph database query statements, leading to uncontrollable query behavior, errors in understanding tags and attribute fields, lack of unified execution constraints, and poor readability of returned results. Instead of directly generating arbitrary underlying graph data query statements, the invention generates constrained, structured semantic query requests from large models. Simultaneously, the server uniformly handles semantic verification, graph data query statement generation, execution control, and semantic return of results, thus balancing the expressive power of natural language queries, the accuracy of graph database access, and the controllability of enterprise system execution.
[0008] This invention provides a graph database query method, comprising: Obtain the user's natural language query request; The natural language query request is understood by a large model agent. Based on the preset semantic mapping rules, the natural language query request is converted into structured semantic query parameters by calling the agent tool. The structured semantic query parameters are used to represent the query intent and query constraints. The semantic mapping rules are used to establish the correspondence between semantic objects, semantic relations and semantic fields and node labels, relation edges and attribute fields in the graph database. Based on the structured semantic query parameters, a graph database query statement is generated and executed through a controlled execution method to obtain the original query results. The controlled execution method includes limiting the query scope and query behavior according to the query constraints. Based on the semantic mapping rules, the original query results are converted from the physical graph mode of the graph database into semantic query results for the large model agent, and the semantic query results are returned to the large model agent so that the large model agent can generate a response result corresponding to the natural language query request based on the semantic query results and return it to the user terminal.
[0009] In one possible implementation, the method further includes: Parse the natural language query request to identify the node types, relation edges, and attribute fields involved in the query intent; Based on the semantic mapping rules, the node type is converted into a semantic object, the relation edge is converted into a semantic relation, and the attribute field is converted into a semantic field; Based on the transformed semantic objects, semantic relationships, and semantic fields, the structured semantic query parameters are generated according to the tool parameter schema, wherein the tool parameter schema is constructed based on the semantic mapping rules and is used to constrain the fields and value range of the structured semantic query parameters; The structured semantic query parameters include: Query type, node range, anchor node identifier, relationship direction, filter conditions, sorting and pagination parameters, traversal depth, return mode, and extended items.
[0010] In one possible implementation, the method further includes: The structured semantic query parameters are validated. If the required parameters corresponding to the query type are missing, or if the filter fields in the filtering conditions do not belong to the preset semantic field set, a semantic error message is returned and the graph database query statement generation stage is not entered. The required parameters corresponding to the query type include the query type and the return mode.
[0011] In one possible implementation, the method further includes: Based on the query type in the structured semantic query parameters, the structured semantic query parameters are routed to the corresponding execution branch, wherein the execution branch includes a node retrieval branch, a relation traversal branch, a top-level object retrieval branch, or a context retrieval branch; In the execution branch, based on the preset query template and the semantic mapping rules, the structured semantic query parameters are converted into graph database query statements, and the available fields, available operators, traversal depth limit, pagination limit and return mode are restricted according to preset rules to control query behavior; The graph database query statement is executed in the execution branch to obtain the original query result.
[0012] In one possible implementation, the method further includes: Map the underlying attribute fields in the original query results to unified semantic fields, and map the underlying relation edges to semantic relations; Based on the return pattern in the structured semantic query parameters, the mapped result is shaped and output to obtain the semantic query result, which is then returned to the large model agent. This enables the large model agent to generate a response result corresponding to the natural language query request based on the semantic query result and return it to the user.
[0013] In one possible implementation, the method further includes: Based on the extended items in the structured semantic query parameters, the external data source is invoked to obtain the associated data; The underlying attributes of the associated data are converted into semantic fields, and the return size is controlled according to preset pruning rules; The associated data after converting the underlying attributes into semantic fields is integrated into the semantic query result.
[0014] In one possible implementation, the method further includes: When a query fails, a semantic error message is generated and returned to the large model agent, so that the large model agent can regenerate or adjust the structured semantic query parameters or provide feedback on the reason to the user.
[0015] In one possible implementation, the method further includes: Establish a correspondence between semantic objects and node types or node labels in the graph database; Establish the correspondence between semantic relations and relation edges and relation directions in the graph database; Establish the correspondence between semantic fields and attribute fields in the graph database; The semantic mapping rules are used to convert the structured semantic query parameters into physical query conditions of the graph database, and to convert the original query results into semantic query results for the large model agent.
[0016] The present invention also provides a graph database query device, comprising the following modules: The acquisition module is used to acquire natural language query requests from the user's client. The conversion module is used to understand the intent of the natural language query request through a large model agent. Based on the preset semantic mapping rules, the natural language query request is converted into structured semantic query parameters through the agent tool call. The structured semantic query parameters are used to represent the query intent and query constraints. The semantic mapping rules are used to establish the correspondence between semantic objects, semantic relations and semantic fields and node labels, relation edges and attribute fields in the graph database. The query module is used to generate and execute graph database query statements based on the structured semantic query parameters in a controlled manner to obtain the original query results. The controlled execution method includes limiting the query scope and query behavior according to the query constraints. The conversion module is further configured to convert the original query result from the physical graph mode of the graph database into a semantic query result for the large model agent based on the semantic mapping rules, and return the semantic query result to the large model agent so that the large model agent can generate a response result corresponding to the natural language query request based on the semantic query result and return it to the user terminal.
[0017] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the graph database query method as described above.
[0018] 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 the graph database query method as described above.
[0019] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the graph database query method as described above.
[0020] The graph database query method and apparatus provided by this invention acquires a natural language query request from a user; performs intent understanding on the natural language query request through a large model agent; and, based on preset semantic mapping rules, converts the natural language query request into structured semantic query parameters through agent tool invocation. The structured semantic query parameters characterize the query intent and query constraints, and the semantic mapping rules establish the correspondence between semantic objects, semantic relationships, and semantic fields and node labels, relation edges, and attribute fields in the graph database. Based on the structured semantic query parameters, a graph database query statement is generated and executed in a controlled execution manner to obtain the original query result. The controlled execution manner includes limiting the query scope and query behavior according to the query constraints. Based on the semantic mapping rules, the original query result is converted from the physical graph mode of the graph database into a semantic query result oriented towards the large model agent, and the semantic query result is returned to the large model agent, so that the large model agent generates a response result corresponding to the natural language query request based on the semantic query result and returns it to the user. Compared to existing technologies where large models directly generate underlying graph database query statements, resulting in uncontrollable query behavior, errors in understanding label and attribute fields, lack of unified execution constraints, and poor readability of returned results, this solution avoids generating arbitrary underlying graph data query statements directly from the large model. Instead, it generates constrained structured semantic query requests. Simultaneously, the server uniformly handles semantic validation, graph data query statement generation, execution control, and semantic return of results, thus balancing the expressive power of natural language queries, the accuracy of graph database access, and the controllability of enterprise system execution. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0022] Figure 1 This is one of the flowcharts illustrating the graph database query method provided by the present invention.
[0023] Figure 2 This is the second flowchart illustrating the graph database query method provided by the present invention.
[0024] Figure 3 This is a schematic diagram of the graph database query device provided by the present invention.
[0025] Figure 4 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0026] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0027] To facilitate understanding of the embodiments of the present invention, further explanations and descriptions will be provided below with reference to the accompanying drawings and specific embodiments. These embodiments do not constitute a limitation on the embodiments of the present invention.
[0028] Figure 1 This is one of the flowcharts illustrating the graph database query method provided by the present invention, such as... Figure 1 As shown, the method includes the following: S11. Obtain the natural language query request from the user.
[0029] The core idea of this invention is to set up a semantic layer and a controlled execution layer between the large model and the graph database, so that the large model only generates structured semantic query parameters that conform to the preset schema. The server then generates Cypher query statements that match the business graph based on these parameters and executes them. Finally, the results are converted into a stable semantic result structure.
[0030] Specifically, a large model agent deployed on the server receives natural language questions input by the user, such as querying all ongoing subtasks in a project and their responsible persons.
[0031] S12. The natural language query request is understood by a large model agent. Based on the preset semantic mapping rules, the natural language query request is converted into structured semantic query parameters by calling the agent tool.
[0032] Based on pre-defined semantic mapping rules, a large-scale model agent performs intent understanding on natural language query requests and converts these requests into structured semantic query parameters through agent tool calls. The semantic mapping rules establish the correspondence between semantic objects, semantic relationships, and semantic fields and node labels, relation edges, and attribute fields in the graph database. The structured semantic query parameters represent the query intent and constraints. Through this conversion, the large model does not need to directly understand the underlying graph database schema; instead, it generates structured semantic query parameters representing the query intent and constraints based on the tool parameter schema. These parameters include query type, node range, anchor node identifier, relation direction, filtering conditions, sorting and pagination parameters, traversal depth, return mode, and extended items.
[0033] S13. Based on the structured semantic query parameters, generate and execute a graph database query statement in a controlled execution manner to obtain the original query results.
[0034] Based on structured semantic query parameters, graph database query statements are generated and executed through controlled execution to obtain the original query results. Controlled execution includes limiting the query scope and behavior according to query constraints. For example, structured semantic query parameters are routed to the corresponding execution branch based on the query type. Query statements are generated based on preset query templates and semantic mapping rules. During generation and execution, restrictions are placed on available fields, available operators, maximum traversal depth, maximum pagination, and return mode to prevent large models from directly concatenating arbitrary query fragments.
[0035] S14. Based on the semantic mapping rules, the original query result is converted from the physical graph mode of the graph database into a semantic query result for the large model agent, and the semantic query result is returned to the large model agent so that the large model agent generates a response result corresponding to the natural language query request based on the semantic query result and returns it to the user terminal.
[0036] Based on the aforementioned semantic mapping rules, the original query results are transformed from the physical graph schema of the graph database into semantic query results for large-scale model agents. Specifically, the underlying attribute fields in the original query results are mapped to unified semantic fields, and the underlying relation edges are mapped to semantic relations. The mapped results are then shaped and output according to the return pattern in the structured semantic query parameters to obtain the semantic query results. These semantic query results are returned to the large-scale model agent, enabling it to generate a response corresponding to the natural language query request and return it to the user. Furthermore, based on the extended items in the structured semantic query parameters, external data sources can be invoked to obtain related data. The underlying attributes of this related data are converted into semantic fields, and the returned data is controlled according to preset pruning rules before being integrated into the semantic query results.
[0037] The graph database query method provided by this invention obtains a natural language query request from a user; a large model agent performs intent understanding on the natural language query request, and based on preset semantic mapping rules, converts the natural language query request into structured semantic query parameters through agent tool invocation. The structured semantic query parameters characterize the query intent and query constraints, and the semantic mapping rules establish the correspondence between semantic objects, semantic relationships, and semantic fields and node labels, relationship edges, and attribute fields in the graph database. Based on the structured semantic query parameters, a graph database query statement is generated and executed in a controlled execution manner to obtain the original query result. The controlled execution manner includes limiting the query scope and query behavior according to the query constraints. Based on the semantic mapping rules, the original query result is converted from the physical graph mode of the graph database into a semantic query result oriented towards the large model agent, and the semantic query result is returned to the large model agent, so that the large model agent generates a response result corresponding to the natural language query request based on the semantic query result and returns it to the user. Compared to existing technologies that directly generate underlying graph database query statements from large models, resulting in uncontrollable query behavior, errors in understanding label and attribute fields, lack of unified execution constraints, and poor readability of returned results, this method introduces a semantic layer and a controlled execution layer between the large model and the graph database. This allows the large model to generate only structured semantic query parameters that conform to a preset schema. The server then generates and executes controlled query statements that match the business graph based on these parameters. Finally, the results are converted into a stable semantic result structure and returned, thus balancing the expressive power of natural language queries, the accuracy of graph database access, and the controllability of enterprise system execution.
[0038] Figure 2 This is the second flowchart illustrating the graph database query method provided by the present invention, as shown below. Figure 2As shown, this method operates in a system environment that includes large model / agent applications, semantic query tools, graph query services, data object schema services, and graph database interfaces. It should be noted that the following task graph is merely an example for illustrative purposes, and this invention can also be applied to other graph database scenarios that include nodes, relationships, attributes, and business objects.
[0039] Specifically, the following steps are included: Step 1: Define the semantic graph. The system first performs semantic analysis on the business graph, extracting query concepts that can be stably understood by the large model. For task-type graphs, nodes are uniformly abstracted as task objects, underlying labels from different business domains are grouped into task types or business domain scopes, hierarchical edges are uniformly abstracted as parent-child relationships, execution order edges or dependency edges are uniformly abstracted as pre- and post-relationships, and external materials, forms, or data objects associated with tasks are treated as expandable related information. Simultaneously, the system establishes an attribute field mapping table, mapping underlying fields to semantic fields. For example, the underlying responsible person field is mapped to the responsible person identifier, and the underlying actual working hours field is mapped to actual working hours. For attributes with the same meaning but different names, the system uniformly maps them to the same semantic field; for fields with historical names, abbreviated names, or those unsuitable for direct exposure to the large model, the system replaces them with business-understandable names.
[0040] Step two, generating a semantic query request: After receiving the user's question, the agent determines whether a graph database query is needed based on system prompts, tool descriptions, and tool parameter schema. When the question involves node retrieval, object relationships, upstream and downstream links, hierarchical statistics, local context, or association with external data, the agent invokes the semantic graph database query tool and generates a structured semantic query request. Structured semantic query requests include: (1) query type, used to indicate node retrieval, relationship retrieval, top-level object retrieval or context retrieval; (2) node range, used to limit business domain, node category or task type; (3) anchor node identifier, used to expand relationship query around a known node; (4) relationship direction, used to indicate parent node, child node, predecessor node, successor node or other related nodes; (5) filtering conditions, used to filter according to semantic fields, operators and values; (6) sorting and pagination parameters, used to control the return order and return quantity; (7) traversal depth, used to control the number of relationship expansion layers; (8) return mode, used to control the return graph structure, row list, field set, count result, hierarchical statistics or relationship bucketing; (9) extension items, used to return working hours, input objects, output objects or other related information as needed.
[0041] Step 3: Perform tool input and context verification: After receiving the structured semantic query parameters from the agent, the semantic graph query tool performs rigorous conversion and verification of these parameters. If any required parameters corresponding to the query type are missing, such as query type, return mode, anchor identifier, or relationship direction, or if the filter fields in the filtering conditions do not belong to the preset semantic field set, the system directly returns a semantic error message without proceeding to the underlying query statement generation stage. When submitting the graph query service, the tool layer also carries the current user's login information, tenant information, or business context information. The graph query service can further limit the data scope based on this context to ensure that the same semantic query request falls within the corresponding execution scope in different user or tenant environments.
[0042] Step four: Controlled query statement generation and execution based on query type: The graph query service selects the corresponding controlled execution branch based on the query type in the structured semantic query request, and generates the query statement on the server side according to preset graph semantic rules. Execution branches cover types such as node retrieval, relation traversal, root node or top-level object retrieval, local context retrieval, and statistical analysis. For node retrieval, node matching, filtering, sorting, and pagination queries are generated; for relation queries, path traversal queries around anchor nodes for parent-child or pre- and post-relationships are generated; for top-level node queries, range queries without parent nodes or root nodes are generated; for context queries, one-hop adjacency queries for anchor nodes are generated. Different branches share unified field mapping, relation semantics, filtering conditions, sorting and pagination, and traversal depth control rules. After generating the query statement, the system restricts available fields, available operators, traversal depth, pagination limit, return mode, and extended content according to preset rules, and can combine user login information, tenant information, business domain tags, or other contextual information to limit the query scope. The query statement is then executed in the execution branch to obtain the original query results.
[0043] Step 5: Control the return mode: After the underlying graph query is executed, the system does not directly return the original graph database results. Instead, it reshapes the results according to the return mode in the structured semantic query request. Return modes include graph structure, row-based lists, specified fields, quantity statistics, hierarchical statistics, or relationship grouping, to adapt to different needs such as structure display, detailed viewing, summary generation, statistical analysis, and context compression. The system also controls the scale of the returned content by combining field selection, pagination limits, extension item switches, and pruning rules, ensuring that the final response maintains graph semantics while avoiding exposing too many underlying implementation details to large models or users.
[0044] Step Six: Semantic Extension of External Information and Related Data: In practical graph applications, some information related to the query object may not be directly used as graph nodes or graph relationships in modeling, but is stored in external business systems, data objects, forms, files, logs, indicator services, or other structured data sources. This embodiment of the invention allows setting extension items in the structured semantic query request, whereby the caller declares the types of external information to be supplemented, the return scope, and pruning rules as needed. After completing the basic graph query, the graph query service calls the corresponding external data source based on the hit node identifier, business type, or context information, and converts the external data into unified semantic fields or semantic objects before incorporating it into the query response. The system does not expand all external data by default, but controls the return scale through explicit switches, quantity limits, field selection, and content truncation. For example, in a task graph scenario, task-related input materials, output forms, work logs, approval records, or business indicators can all be accessed as external extended information. The system can query related objects based on task identifiers, and then combine object attribute definitions, field comments, or business metadata to convert underlying field names, internal codes, or original attributes into field expressions that the business can understand, and trim excessively long content according to preset trimming rules.
[0045] Step 7: Result Return and Error Handling: The graph query service ultimately encapsulates nodes, relationships, row data, statistical results, input / output objects, and summary information into a unified semantic response. If the query fails or the parameters are invalid, the system returns an understandable error message, enabling the agent to adjust the query request or explain the reason to the user.
[0046] The static structural relationships of this invention are as follows: (1) Large model / agent application: responsible for understanding the user's natural language questions and generating structured semantic query requests based on the tool schema.
[0047] (2) Semantic query tool: As a tool interface between large model / agent application and graph query service, it is responsible for exposing tool name, tool description and parameter schema, receiving structured parameters generated by large model / agent, and transferring them to the server query service.
[0048] (3) Graph Query Service: responsible for semantic request validation, query type branch routing, query statement template selection, filter condition assembly, sorting and pagination assembly, relation traversal control, extended object query and semantic processing of results.
[0049] (4) Semantic mapping rule base: used to describe the mapping relationship between the underlying graph database labels, relation edges, attribute fields and business semantics.
[0050] (5) External data semantic mapping service: used to query the object type, object description, field comments or business metadata of related data objects, and map the underlying attributes of external data to fields that can be understood by the business.
[0051] (6) Graph database or graph gateway: responsible for executing controlled query statements generated by the graph query service and returning node, relation or record results.
[0052] The differences between this invention and the prior art are as follows: (1) Compared with the scheme of directly generating Cypher from large model, this invention introduces structured semantic query request as an intermediate layer. The large model only expresses the query intent, and the underlying query statement is generated by the server according to controlled rules.
[0053] (2) Compared with fixed template query interfaces, this invention does not expose interfaces separately for each type of problem. Instead, it forms a unified semantic protocol through query type, relationship direction, filtering conditions, return mode and extension items, which covers multiple types of graph query scenarios while maintaining controllability.
[0054] (3) Compared with the scheme that directly returns the original results of the graph database, the present invention performs semantic mapping on node fields, relation types and input / output object attributes, making the returned results stable, readable and suitable for large models to continue reasoning.
[0055] (4) Compared with solutions that rely solely on prompt words to constrain model behavior, this invention applies constraints on multiple levels, including tool schema, server-side validation, query statement branch generation, pagination depth limit, and result pruning, thereby forming a controlled execution loop that can be implemented in engineering.
[0056] (5) Compared with query tools that are only oriented to a single graph structure, the semantic abstraction method of the present invention can be extended to other graph databases that include node categories, semantic relationships, attribute mappings and extended objects, and has good versatility.
[0057] Through the above implementation methods, this invention transforms raw query statements directly generated from natural language into structured semantic requests generated from natural language, and then generates controlled query statements on the server side, reducing the probability of large models misusing labels, fields, relationship directions, and return structures. By limiting query types, relationship directions, filtering fields, operators, return modes, traversal depths, and pagination limits, it avoids the uncontrollable execution risks brought about by arbitrary query statements. By using a unified semantic query protocol to cover multiple scenarios such as node retrieval, relationship traversal, top-level object retrieval, context viewing, statistics, and field selection, the number of tools is reduced while the expressive power is stronger. Through field normalization, relation semanticization, and data object attribute translation, the returned results no longer depend on underlying English fields, internal labels, or original edge names, making them more suitable for large models to continue reasoning and for business users to read. Through return modes, field selection, extension item switches, pagination, and pruning rules, the system can return graph structures, lists, fields, quantities, or statistical results as needed, avoiding the input of large-scale graph data into the model context all at once. By combining user, tenant, and business domain contexts for query scope control, it is suitable for enterprise graph scenarios with multiple business domains, multiple labels, multiple levels, and multiple relationship types.
[0058] The graph database query device provided by the present invention is described below. The graph database query device described below and the graph database query method described above can be referred to in correspondence.
[0059] Figure 3 This is a schematic diagram of the structure of the graph database query device provided by the present invention, specifically including: The acquisition module 301 is used to acquire natural language query requests from the user's client. For detailed explanations, please refer to the relevant descriptions in the above method embodiments; they will not be repeated here.
[0060] The conversion module 302 is used to understand the intent of the natural language query request through a large model agent. Based on preset semantic mapping rules, it converts the natural language query request into structured semantic query parameters through agent tool invocation. The structured semantic query parameters represent the query intent and query constraints, and the semantic mapping rules establish the correspondence between semantic objects, semantic relationships, and semantic fields and node labels, relationship edges, and attribute fields in the graph database. For detailed explanations, please refer to the relevant descriptions in the above method embodiments; they will not be repeated here.
[0061] The query module 303 is used to generate and execute a graph database query statement based on the structured semantic query parameters in a controlled execution manner to obtain the original query results. The controlled execution manner includes limiting the query scope and query behavior according to the query constraints. For detailed explanations, please refer to the relevant descriptions in the above method embodiments; they will not be repeated here.
[0062] The conversion module 302 is further configured to convert the original query result from the physical graph mode of the graph database into a semantic query result for the large model agent based on the semantic mapping rules, and return the semantic query result to the large model agent so that the large model agent can generate a response result corresponding to the natural language query request based on the semantic query result and return it to the user terminal. For detailed explanation, please refer to the relevant descriptions in the above method embodiments, which will not be repeated here.
[0063] Figure 4 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 4 As shown, the electronic device may include: a processor 410, a communications interface 420, a memory 430, and a communications bus 440, wherein the processor 410, the communications interface 420, and the memory 430 communicate with each other through the communications bus 440. The processor 410 can call logical instructions in the memory 430 to execute a graph database query method. This method includes: acquiring a natural language query request from a user; understanding the intent of the natural language query request through a large model agent; converting the natural language query request into structured semantic query parameters based on preset semantic mapping rules and through agent tool invocation, wherein the structured semantic query parameters characterize the query intent and query constraints, and the semantic mapping rules establish the correspondence between semantic objects, semantic relationships, and semantic fields and node labels, relationship edges, and attribute fields in the graph database; generating and executing a graph database query statement based on the structured semantic query parameters in a controlled execution manner to obtain the original query result, wherein the controlled execution manner includes limiting the query scope and query behavior according to the query constraints; converting the original query result from the physical graph mode of the graph database into a semantic query result oriented towards the large model agent based on the semantic mapping rules, and returning the semantic query result to the large model agent so that the large model agent generates a response result corresponding to the natural language query request based on the semantic query result and returns it to the user.
[0064] Furthermore, the logical instructions in the aforementioned memory 430 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0065] On the other hand, the present invention also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the graph database query method provided by the above methods. This method includes: obtaining a natural language query request from a user; understanding the intent of the natural language query request through a large model agent; and converting the natural language query request into structured semantic query parameters based on preset semantic mapping rules through agent tool invocation. The structured semantic query parameters are used to characterize the query intent and query constraints, and the semantic mapping rules are used to establish semantic objects. The semantic relationships and semantic fields are mapped to the node labels, relation edges, and attribute fields in the graph database. Based on the structured semantic query parameters, a graph database query statement is generated and executed in a controlled manner to obtain the original query result. The controlled execution method includes limiting the query scope and query behavior according to the query constraints. Based on the semantic mapping rules, the original query result is converted from the physical graph mode of the graph database into a semantic query result for the large model agent. The semantic query result is then returned to the large model agent so that the large model agent can generate a response result corresponding to the natural language query request based on the semantic query result and return it to the user.
[0066] Furthermore, the present invention also provides a non-transitory computer-readable storage medium storing a computer program thereon. When executed by a processor, this computer program implements the graph database query method provided by the aforementioned methods. This method includes: obtaining a natural language query request from a user; understanding the intent of the natural language query request through a large model agent; and converting the natural language query request into structured semantic query parameters based on preset semantic mapping rules through agent tool invocation. The structured semantic query parameters are used to characterize the query intent and query constraints, and the semantic mapping rules are used to establish semantic objects, semantic relationships, and semantic fields in the graph database. The correspondence between node labels, relation edges, and attribute fields is defined. Based on the structured semantic query parameters, a graph database query statement is generated and executed in a controlled execution manner to obtain the original query result. The controlled execution manner includes limiting the query scope and query behavior according to the query constraints. Based on the semantic mapping rules, the original query result is converted from the physical graph mode of the graph database into a semantic query result for the large model agent, and the semantic query result is returned to the large model agent so that the large model agent can generate a response result corresponding to the natural language query request based on the semantic query result and return it to the user.
[0067] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0068] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0069] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A graph database query method, characterized in that, include: Obtain the user's natural language query request; The natural language query request is understood by a large model agent. Based on the preset semantic mapping rules, the natural language query request is converted into structured semantic query parameters by calling the agent tool. The structured semantic query parameters are used to represent the query intent and query constraints. The semantic mapping rules are used to establish the correspondence between semantic objects, semantic relations and semantic fields and node labels, relation edges and attribute fields in the graph database. Based on the structured semantic query parameters, a graph database query statement is generated and executed through a controlled execution method to obtain the original query results. The controlled execution method includes limiting the query scope and query behavior according to the query constraints. Based on the semantic mapping rules, the original query results are converted from the physical graph mode of the graph database into semantic query results for the large model agent, and the semantic query results are returned to the large model agent so that the large model agent can generate a response result corresponding to the natural language query request based on the semantic query results and return it to the user terminal.
2. The method according to claim 1, characterized in that, The method, based on preset semantic mapping rules, converts the natural language query request into structured semantic query parameters through intelligent agent tool invocation, including: Parse the natural language query request to identify the node types, relation edges, and attribute fields involved in the query intent; Based on the semantic mapping rules, the node type is converted into a semantic object, the relation edge is converted into a semantic relation, and the attribute field is converted into a semantic field; Based on the transformed semantic objects, semantic relationships, and semantic fields, the structured semantic query parameters are generated according to the tool parameter schema, wherein the tool parameter schema is constructed based on the semantic mapping rules and is used to constrain the fields and value range of the structured semantic query parameters; The structured semantic query parameters include: Query type, node range, anchor node identifier, relationship direction, filter conditions, sorting and pagination parameters, traversal depth, return mode, and extended items.
3. The method according to claim 1 or 2, characterized in that, After generating the structured semantic query parameters, the process also includes: The structured semantic query parameters are validated. If the required parameters corresponding to the query type are missing, or if the filter fields in the filtering conditions do not belong to the preset semantic field set, a semantic error message is returned and the graph database query statement generation stage is not entered. The required parameters corresponding to the query type include the query type and the return mode.
4. The method according to claim 1, characterized in that, Based on the structured semantic query parameters, a graph database query statement is generated and executed in a controlled execution manner to obtain the original query results, including: Based on the query type in the structured semantic query parameters, the structured semantic query parameters are routed to the corresponding execution branch, wherein the execution branch includes a node retrieval branch, a relation traversal branch, a top-level object retrieval branch, or a context retrieval branch; In the execution branch, based on the preset query template and the semantic mapping rules, the structured semantic query parameters are converted into graph database query statements, and the available fields, available operators, traversal depth limit, pagination limit and return mode are restricted according to preset rules to control query behavior; The graph database query statement is executed in the execution branch to obtain the original query result.
5. The method according to claim 1, characterized in that, The process of converting the original query result from the physical graph schema of the graph database into a semantic query result for the large model agent based on the semantic mapping rules, and returning the semantic query result to the large model agent, so that the large model agent can generate a response result corresponding to the natural language query request based on the semantic query result and return it to the user, includes: Map the underlying attribute fields in the original query results to unified semantic fields, and map the underlying relation edges to semantic relations; Based on the return pattern in the structured semantic query parameters, the mapped result is shaped and output to obtain the semantic query result, which is then returned to the large model agent. This enables the large model agent to generate a response result corresponding to the natural language query request based on the semantic query result and return it to the user.
6. The method according to claim 5, characterized in that, Before returning the semantic query result to the large model agent, the method further includes: Based on the extended items in the structured semantic query parameters, the external data source is invoked to obtain the associated data; The underlying attributes of the associated data are converted into semantic fields, and the return size is controlled according to preset pruning rules; The associated data after converting the underlying attributes into semantic fields is integrated into the semantic query result.
7. The method according to claim 1, characterized in that, The method further includes: When a query fails, a semantic error message is generated and returned to the large model agent, so that the large model agent can regenerate or adjust the structured semantic query parameters or provide feedback on the reason to the user.
8. The method according to claim 1, characterized in that, The preset semantic mapping rules include: Establish a correspondence between semantic objects and node types or node labels in the graph database; Establish the correspondence between semantic relations and relation edges and relation directions in the graph database; Establish the correspondence between semantic fields and attribute fields in the graph database; The semantic mapping rules are used to convert the structured semantic query parameters into physical query conditions of the graph database, and to convert the original query results into semantic query results for the large model agent.
9. A graph database query device, characterized in that, include: The acquisition module is used to acquire natural language query requests from the user's client. The conversion module is used to understand the intent of the natural language query request through a large model agent. Based on the preset semantic mapping rules, the natural language query request is converted into structured semantic query parameters through the agent tool call. The structured semantic query parameters are used to represent the query intent and query constraints. The semantic mapping rules are used to establish the correspondence between semantic objects, semantic relations and semantic fields and node labels, relation edges and attribute fields in the graph database. The query module is used to generate and execute graph database query statements based on the structured semantic query parameters in a controlled manner to obtain the original query results. The controlled execution method includes limiting the query scope and query behavior according to the query constraints. The conversion module is further configured to convert the original query result from the physical graph mode of the graph database into a semantic query result for the large model agent based on the semantic mapping rules, and return the semantic query result to the large model agent so that the large model agent can generate a response result corresponding to the natural language query request based on the semantic query result and return it to the user terminal.
10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that, When the processor executes the computer program, it implements the graph database query method as described in any one of claims 1 to 8.
11. 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, it implements the graph database query method as described in any one of claims 1 to 8.
12. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the graph database query method as described in any one of claims 1 to 8.
