Structured query language generation method and device and nonvolatile storage medium
Through GraphRAG technology, the knowledge graph and community structure are constructed, combined with the large language model (LLM), the problems of low retrieval accuracy and incomplete information in the existing technology are solved, and more accurate structured query language generation is realized, suitable for multi-table association and complex logical operations.
Patent Information
- Application Number
- CN202510346982.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-21
- Publication Date
- 2025-07-08
AI Technical Summary
When generating structured query languages, the search accuracy is low and the information is incomplete, resulting in the generated SQL statements being inaccurate and the inaccurate understanding of complex logical relationships and multi-table associations.
GraphRAG technology is adopted to build a knowledge graph and community structure, use graph databases to store and retrieve information, combine large language model (LLM) to accurately locate and integrate entities and their relationships, and generate structured query languages.
It improves retrieval accuracy and information integrity, and the generated SQL statements more accurately reflect user needs, can handle complex logical operations and multi-table association queries, improving data utilization efficiency and decision-making quality.
Smart Images

Figure CN120277094A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence, and more particularly, to a method, apparatus, and non-volatile storage medium for generating structured query languages. Background Art
[0002] Retrieval-augmented Generation (RAG) technology is a method that combines retrieval and generation model approaches, aiming to improve the performance of natural language processing systems, especially in text generation and question-and-answer scenarios. The core idea of RAG is to retrieve relevant information from a pre-built knowledge base or document collection before the generation model generates text, and then integrate the retrieved information into the context of the generation model to guide the generation of text, thereby enhancing the accuracy and information richness of the generated text.
[0003] However, traditional RAG technology mainly relies on the surface information of text and lacks the ability to understand deep logical relationships. When generating SQL query statements, the limitations of this technology are particularly obvious. It may not be able to accurately understand complex logical operations and multi-table association relationships, resulting in generated SQL statements that do not meet user requirements.
[0004] In response to the above problems, no effective solutions have been proposed yet. Summary of the Invention
[0005] Embodiments of this application provide a method, apparatus, and non-volatile storage medium for generating structured query languages, so as to at least solve the technical problem that the generated structured query language is inaccurate due to low retrieval accuracy and incomplete information in the prior art during retrieval.
[0006] According to one aspect of the embodiments of this application, a method for generating a structured query language is provided, including: receiving a data query instruction from a user, and extracting keywords in the data query instruction, where the keywords include at least one of the following: entity name, attribute name; determining relevant nodes in a knowledge graph according to the keywords, where nodes in the knowledge graph are used to record entity information in a database, edges in the knowledge graph are used to record relationships between entities, and relevant nodes are nodes whose recorded entity information has a similarity greater than a first preset threshold with the keywords; obtaining node information of relevant nodes and node information of associated nodes of relevant nodes, where associated nodes are nodes in the knowledge graph that have edge connections with relevant nodes, and node information includes the table name of the entity corresponding to the node and the field information of the table of the entity corresponding to the node; inputting the node information of relevant nodes, the node information of associated nodes, and the data query instruction into a structured query language generation model, and obtaining the structured query language output by the structured query language generation model.
[0007] Optionally, after extracting the keywords in the data query instruction, the method further includes: determining a target community in the knowledge graph, where the similarity between the community summary of the target community and the keywords is greater than a second preset threshold, and the community summary includes at least one of the following: summary information on the relationships between nodes in the community, summary information on the attribute information of each node, and the data range of each node in the community; inputting the community summary of the target community into a structured query language generation model.
[0008] Optionally, determining relevant nodes in the knowledge graph based on the keywords includes: vectorizing the keywords to obtain keyword vectors; determining the vector similarity between the keyword vectors and the corresponding vectors of the entity information of each node in the knowledge graph, and using the vector similarity as the similarity between the entity information of the relevant nodes and the keywords; taking the nodes with the similarity between the entity information and the keywords greater than the preset threshold as relevant nodes.
[0009] Optionally, after obtaining the structured query language output by the structured query language generation model, the method further includes: presenting relevant nodes, associated nodes, and the connection relationships between the relevant nodes and the associated nodes to the user; receiving first feedback information from the user on the connection relationships, where the first feedback information includes that the connection relationship is correct or incorrect; in the case where the first feedback information is that the connection relationship is incorrect, obtaining a relationship modification command input by the user; modifying the connection relationship with the incorrect connection relationship according to the relationship modification command.
[0010] Optionally, after obtaining the structured query language output by the structured query language generation model, the method further includes: presenting the structured query language to the user; obtaining second feedback information from the user, where the second feedback information includes user satisfaction information.
[0011] Optionally, in the case where the satisfaction information indicates a satisfactory result, the method further includes: running the structured query language; obtaining the query result corresponding to the structured query language; generating summary information on the data in the query result according to the query result and the data query instruction; generating an explanatory text according to the structured query language, where the explanatory text includes the query logic of the structured query language; presenting the query result, the summary information, and the explanatory text to the user.
[0012] Optionally, when the satisfaction information indicates an unsatisfactory result, the method further includes: obtaining a Structured Query Language (SQL) modification instruction input by the user; extracting semantic information from the SQL modification instruction; determining a modification prompt word according to the semantic information, where the modification prompt word includes a modification target and a modification method corresponding to the modification target, and the modification target includes at least one of the following: table name, field name, logical operator; inputting the modification prompt word into an SQL generation model, obtaining the SQL modified by the SQL generation model according to the modification prompt word, presenting the modified SQL to the user, and obtaining second feedback information of the user on the modified SQL until the second feedback information of the user on the modified SQL is that the user is satisfied.
[0013] Optionally, before receiving the data query instruction of the user, the method further includes: obtaining the structure information of the database, where the structure information includes at least one of the following: database table name, database table field name, field data type, and database table relationship; extracting the relationship between entities from the structure information; constructing a knowledge graph according to the relationship between entities.
[0014] According to another aspect of the embodiments of the present application, there is also provided an SQL generation device, including: a receiving module, configured to receive the data query instruction of the user and extract keywords in the data query instruction, where the keywords include at least one of the following: entity name, attribute name; a determining module, configured to determine relevant nodes in the knowledge graph according to the keywords, where the nodes in the knowledge graph are used to record entity information in the database, the edges in the knowledge graph are used to record the relationships between entities, and the relevant nodes are nodes whose similarity between the recorded entity information and the keywords is greater than a first preset threshold; an obtaining module, configured to obtain the node information of the relevant nodes and the node information of the associated nodes of the relevant nodes, where the associated nodes are nodes in the knowledge graph that are connected to the relevant nodes by edges, and the node information includes the table name of the entity corresponding to the node and the field information of the table of the entity corresponding to the node; a generating module, configured to input the node information of the relevant nodes, the node information of the associated nodes, and the data query instruction into an SQL generation model, and obtain the SQL output by the SQL generation model.
[0015] According to another aspect of the embodiments of the present application, there is also provided a non-volatile storage medium, in which a program is stored, and when the program runs, it controls the device where the non-volatile storage medium is located to execute the SQL generation method.
[0016] According to another aspect of the embodiments of the present application, there is also provided an electronic device, including: a memory and a processor, where the processor is configured to run the program stored in the memory, and when the program runs, it executes the SQL generation method.
[0017] According to another aspect of the embodiments of the present application, there is also provided a computer program product, including a computer program which, when executed by a processor, implements the structured query language generation method.
[0018] In the embodiments of the present application, a data query instruction of a user is received, and keywords in the data query instruction are extracted, where the keywords include at least one of the following: entity name, attribute name; relevant nodes are determined in a knowledge graph according to the keywords, where nodes in the knowledge graph are used to record entity information in a database, edges in the knowledge graph are used to record relationships between entities, and relevant nodes are nodes whose similarity between the recorded entity information and the keywords is greater than a first preset threshold; node information of relevant nodes and node information of associated nodes of relevant nodes are obtained, where associated nodes are nodes in the knowledge graph that have edge connections with relevant nodes, and the node information includes the table name of the entity corresponding to the node and the field information of the table of the entity corresponding to the node; the node information of relevant nodes, the node information of associated nodes, and the data query instruction are input into a structured query language generation model, and the structured query language output by the structured query language generation model is obtained. By retrieving based on the knowledge graph and community summary, the purpose of accurately positioning and integrating entity and its relationship information is achieved, thereby realizing the technical effect of improving retrieval accuracy and information integrity, and further solving the technical problem that the generated structured query language is inaccurate due to low retrieval accuracy and incomplete information in the prior art. Description of the Drawings
[0019] The drawings described herein are used to provide a further understanding of the present application, and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application, and do not constitute an improper limitation to the present application. In the drawings:
[0020] Figure 1 is a schematic diagram of the core link of a traditional RAG provided by the embodiments of the present application;
[0021] Figure 2 is a schematic diagram of the structure of a computer terminal provided by the embodiments of the present application;
[0022] Figure 3 is a schematic diagram of the process of a structured query language generation method provided by the embodiments of the present application;
[0023] Figure 4 is a schematic diagram of the working principle of GraphRAG provided by the embodiments of the present application;
[0024] Figure 5It is a schematic flow diagram of NL2SQL based on GraphRAG provided by an embodiment of the present application;
[0025] Figure 6 It is a schematic structural diagram of a structured query language generation device provided by an embodiment of the present invention. Detailed implementation manners
[0026] In order to enable those skilled in the art to better understand the solution of the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present application.
[0027] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above drawings are used to distinguish similar objects, and do not necessarily need to be used to describe a specific order or sequence. It should be understood that such used data can be interchanged under appropriate circumstances so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device including a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0028] In order to better understand the embodiments of the present application, the technical terms involved in the embodiments of the present application are explained as follows:
[0029] Retrieval-augmented Generation (RAG): When the model needs to generate text or answer questions, it will first retrieve relevant information from a large collection of documents, and then use this retrieved information to guide the generation of text, thereby improving the quality and accuracy of the prediction.
[0030] NL2SQL: (Natural Language to SQL), which means converting the user's natural language into executable SQL statements.
[0031] In the related art, the RAG technology usually introduces external knowledge during the generation process to enhance the output of the LLM (Large Language Model). Figure 1 Shows the core link of the traditional RAG, as Figure 1As shown in the figure, the core link of traditional RAG is divided into three stages:
[0032] 1. Indexing (vector embedding): Implement vector encoding of documents through the Embedding model service and write them into the vector database.
[0033] 2. Retrieval (similarity query): Implement vector encoding of queries through the Embedding model service and use similarity query (ANN) to achieve topK result search.
[0034] 3. Generation (document context): The result documents retrieved by the Retriver are submitted to the large model for processing together with the questions as context.
[0035] Traditional RAG hopes to enhance the context of the large model's question answering through the associated knowledge in the knowledge base to improve the quality of the generated content, but there are also many problems, such as truncating useful documents by TopK, losing context integration, failing to recognize useful knowledge, and insufficient accuracy.
[0036] Due to the limitations of existing RAG systems, their dependence on the representation of structured data and lack of context awareness may lead to scattered answers and inability to capture complex interdependencies. For example, when asking high-level and general questions such as "What is the case development in this area? Judgment of the subsequent development trend", traditional RAG may be at a loss. Because this is essentially a query-focused summarization (QFS) task rather than a clear retrieval task. Specifically, traditional RAG technology has the following deficiencies when generating SQL:
[0037] 1. Lack of understanding ability for complex structures:
[0038] For some NL2SQL tasks involving complex logical operations, such as nested queries and conditional judgments, traditional RAG may be difficult to accurately understand and process. This is because traditional RAG mainly relies on the surface information of the text and lacks the ability to understand deep logical relationships. For example, for a complex logical query like "Find the products with sales greater than the average sales and sort them in descending order of sales", traditional RAG may not be able to accurately analyze and generate the corresponding SQL statement.
[0039] 2. Limitations of retrieval results:
[0040] Incomplete information: When traditional RAG retrieves relevant information, due to imperfect retrieval strategies or data incompleteness, the retrieved information may be incomplete. This can affect subsequent SQL generation, making the generated SQL query unable to accurately reflect the user's needs. For example, if the database contains multiple tables and traditional RAG only retrieves a part of them, some important information may be missed, resulting in an incomplete SQL query.
[0041] Lack of precise retrieval ability: Traditional RAG mainly relies on text similarity during retrieval. For some cases that require exact matching, such as specific field values, table names, etc., it may not be able to accurately retrieve relevant information. This can lead to errors or inaccuracies in the generated SQL query. For example, if a user requests to query sales data for a specific date and traditional RAG cannot accurately retrieve the relevant information for that date, the generated SQL query will not be able to correctly obtain the required data.
[0042] 3. Over-reliance on training data:
[0043] High data quality requirements: The performance of traditional RAG depends to a large extent on the quality and quantity of training data. If there are problems such as noise, errors, or uneven data distribution in the training data, the performance of the model will be greatly affected. In the NL2SQL task, since the data structures and contents in the database vary, it is often difficult to obtain high-quality training data, which limits the application of traditional RAG in NL2SQL.
[0044] Difficulty in adapting to new domains: When applying traditional RAG to a new domain or database, due to the lack of learning of knowledge and data in the new domain, the model may require a large amount of new data for retraining to achieve better performance. This not only increases the training cost of the model but also limits the rapid deployment and application of the model.
[0045] To solve the above problems, relevant solutions are provided in the embodiments of the present application, which are described in detail below.
[0046] According to the embodiments of the present application, a method embodiment of a structured query language generation method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than here.
[0047] The method embodiments provided by the embodiments of the present application can be executed on a mobile terminal, a computer terminal, or a similar computing device. Figure 2A hardware block diagram of a computer terminal for implementing a structured query language generation method is shown. As Figure 2 shown, the computer terminal 20 may include one or more (shown as 202a, 202b, ……, 202n in the figure) processors 202 (the processors 202 may include, but are not limited to, processing devices such as a microprocessor MCU or a programmable logic device FPGA), a memory 204 for storing data, and a transmission device 206 for communication functions. In addition, it may further include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of the BUS bus), a network interface, a power supply, and / or a camera. Those of ordinary skill in the art can understand that Figure 2 the structure shown is only illustrative and does not limit the structure of the above electronic device. For example, the computer terminal 20 may further include more or fewer components than Figure 2 shown, or have a different configuration from Figure 2 shown.
[0048] It should be noted that the above one or more processors 202 and / or other data processing circuits are generally referred to as "data processing circuits" herein. The data processing circuit may be embodied in software, hardware, firmware, or any combination thereof, in whole or in part. In addition, the data processing circuit may be a single independent processing module, or be incorporated in whole or in part into any one of the other elements in the computer terminal 20. As involved in the embodiments of the present application, the data processing circuit is used for processor control (such as the selection of a variable resistor terminal path connected to an interface).
[0049] The memory 204 can be used to store software programs and modules of application software, such as the program instructions / data storage device corresponding to the structured query language generation method in the embodiments of the present application. The processor 202 executes various functional applications and data processing by running the software programs and modules stored in the memory 204, that is, implements the above-mentioned structured query language generation method. The memory 204 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memories, or other non-volatile solid-state memories. In some instances, the memory 204 may further include a memory remotely disposed relative to the processor 202, and these remote memories may be connected to the computer terminal 20 through a network. Examples of the above network include, but are not limited to, the Internet, an enterprise intranet, a local area network, a mobile communication network, and combinations thereof.
[0050] The transmission device 206 is used to receive or send data via a network. Specific examples of the above-mentioned network may include a wireless network provided by a communication provider of the computer terminal 20. In one example, the transmission device 206 includes a Network Interface Controller (NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 206 can be a Radio Frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0051] The display can be, for example, a touch-screen Liquid Crystal Display (LCD), which enables a user to interact with the user interface of the computer terminal 20.
[0052] Under the above operating environment, an embodiment of the present application provides a Structured Query Language generation method, as Figure 3 shown, the method includes the following steps:
[0053] Step S302, receive a data query instruction from a user, and extract keywords in the data query instruction, where the keywords include at least one of the following: entity name, attribute name.
[0054] As an optional implementation manner, before receiving the data query instruction from the user, the method further includes: obtaining structure information of a database, where the structure information includes at least one of the following: database table name, database table field name, field data type, and relationship between database tables; extracting the relationship between entities from the structure information; and constructing a knowledge graph based on the relationship between entities.
[0055] Optionally, the extraction of triples is implemented based on the LLM service. Entities are extracted from the table structure of the database (such as database table name, column name (i.e., field name), data value, business concept, etc.), the relationship between database tables (such as primary key-foreign key relationship, semantic association relationship, etc.), and the data dictionary, and entities are extracted in chunks from business documents and external knowledge bases in the industry. For the mined entities, duplicate removal is performed through LLM recognition, such as converting the case, removing entities with the same name with symbols, etc. A graph database (such as Neo4j) or a graph processing framework (such as NetworkX) is used to construct a knowledge graph, and the entities are stored and managed as nodes and the relationships as edges.
[0056] Among them, the graph database is the graph database used for storing and retrieving information in the GraphRAG technology. GraphRAG (Graph-based Retrieval-Augmented Generation) is a knowledge graph-based retrieval-augmented generation (RAG) application. Different from traditional vector-retrieval-based RAG applications, it allows for deeper, more detailed, and context-aware retrieval, thus helping to obtain higher-quality outputs. GraphRAG first constructs a graph structure to represent the relationships of knowledge or data. This graph can consist of nodes and edges. The nodes represent various entities, concepts, or text fragments (i.e., entity information), etc., and the edges represent the associations between them (i.e., the relationships between entities), such as semantic relationships, logical relationships, etc. Then, during the generation process, GraphRAG will use this graph for retrieval operations to retrieve information relevant to the current generation task from the graph. These retrieved information can be used as additional knowledge to enhance the generation results.
[0057] As an optional implementation, after constructing the knowledge graph, it further includes, according to factors such as the type of data (e.g., classification in cases, such as theft, fraud, civil disputes, etc.; similarity of occurrence regions, such as different neighborhoods in the same city or different cities in the same province; and time range, such as cases occurred within the time periods of the recent year, the recent five years, etc.), using community discovery algorithms (such as the Louvain algorithm, etc.) to partition the nodes in the constructed knowledge graph into communities. For example, cases of the same type and occurring in a similar region and time are partitioned into the same community. For each community, community summary information is generated. This can be achieved by aggregating and summarizing the attributes of the nodes in the graph within the community and the relevant text descriptions. For example, numerical information such as the number of cases in the community, the proportion of the main case types, the average processing time, etc. is statistically calculated, and key phrases and topics in the case details text are extracted as the text summary content. Text summary algorithms (such as the summary algorithm based on term frequency-inverse document frequency or deep learning summary models) can be used to generate the text summary.
[0058] Optionally, data cleaning is performed on the node data in the knowledge graph and the community summary information in the community. Data cleaning includes removing special characters, stop words, etc. Then, operations such as word segmentation and part-of-speech tagging are performed to convert the text into a format suitable for subsequent processing.
[0059] After data cleaning, a graph embedding algorithm (such as Graph Embedding technology, GraphSAGE is another graph embedding algorithm that can be used for inductive learning. It can effectively process dynamic graph data and generate embedding vectors by leveraging the feature information of nodes) is used to map the nodes and relationships (i.e., entity information) in the graph to a low-dimensional vector space for similarity calculation and retrieval in the vector space. At the same time, an embedding model is used to convert data texts, community summary texts, etc. into vector representations, enabling the semantic information of the texts to be integrated with the graph structure information.
[0060] As an optional implementation, the question is submitted to a large language model to achieve the understanding and analysis of the question, extract the keywords in the data query instruction, analyze and judge whether the question is a specific query (Who is Li's wife?) or an abstract question (What is the development of cases in this area?), obtain the question type judgment, the keywords in the question query, and possible entities, attributes, and relationships, etc. For simple specific queries, only knowledge graph retrieval is performed to save computing resources. For abstract questions that require complex queries, knowledge graph retrieval and community retrieval are performed to obtain detailed association information.
[0061] As an optional implementation, after extracting the keywords in the data query instruction, the method further includes: determining the target community in the knowledge graph, where the similarity between the community summary of the target community and the keywords is greater than a second preset threshold, and the community summary includes at least one of the following: summary information of the relationships between the nodes in the community, summary information of the node attribute information, and the data range of the nodes in the community; inputting the community summary of the target community into a structured query language generation model.
[0062] Optionally, the summary information of the relationships between the nodes in the community includes an overview of the main relationships between the nodes in the community, the summary information of the node attribute information includes the statistical summary of the key attributes and field values of the nodes, and the data range of the nodes in the community includes the time or numerical range of the node data.
[0063] Optionally, the retrieved communities are filtered according to the similarity between the community summary and the question vector (i.e., the vector corresponding to the keywords). For the filtered communities (i.e., target communities), information fusion techniques are adopted, such as integrating the statistical data of cases in the community with the text description information, and through methods such as weighted average and topic models (such as LDA), fusing information from different sources into a coherent overall information that can answer the question. If the question focuses on the occurrence of cases, information such as the number of cases and the type ratio (i.e., the summary information of node attribute information) is extracted from the community summary; for the subsequent trend analysis, the "subsequent time" relationship between the case nodes in the community (i.e., the summary information of the relationships between the nodes in the community) and the time-related information in the text description (i.e., the data range of each node in the community) are used to analyze the state changes of the cases at different time points. At the same time, the development paths of similar cases in the community can be referred to infer the trend.
[0064] By inputting the community summary into the Structured Query Language (SQL) generation model, it can assist the SQL generation model in analyzing the relationships between multiple entities involved in the community summary, and then determining the database tables to be joined in the SQL query and their connection methods. Using the semantic information in the community summary, the master-slave relationship, one-to-one or one-to-many relationships, etc. between entities are identified, thereby constructing a reasonable multi-table join logic. For example, if the community summary mentions that "a certain criminal suspect committed multiple similar criminal cases at multiple locations", it is determined that the suspect table, case table, and location table need to be joined, and the correct join conditions are set according to the semantic relationship, such as "suspect table.suspect ID = case table.suspect ID" and "case table.location ID = location table.location ID", to ensure that relevant data can be accurately obtained in the SQL query and avoid data omission or incorrect association caused by incorrect table joins.
[0065] During the multi-table association process, the join operation is optimized by considering the data range and constraint conditions in the community summary. For example, if the time range or data volume range is limited in the community summary, corresponding filtering conditions can be added during the multi-table join to filter out irrelevant data in advance, reduce the data transmission and processing volume, and improve the execution efficiency and accuracy of the SQL query. At the same time, according to the information in the community summary, the join algorithm is reasonably selected. For example, for the join of large data volume tables, the hash join or merge join algorithm is selected, and it is optimized and configured according to the characteristics of the database and the data distribution to ensure the accuracy and efficiency of the multi-table association. For example, when a user wants to query information about type B cases in area A within 10 years, and the retrieved community summary records that type B cases in area A only occurred within 5 years, after receiving the summary, the SQL generated by the SQL generation model sets the time constraint to within 5 years to improve the query efficiency.
[0066] Step S304: Determine relevant nodes in the knowledge graph according to the keywords. Among them, the nodes in the knowledge graph are used to record entity information in the database, the edges in the knowledge graph are used to record the relationships between entities, and the relevant nodes are the nodes whose similarity between the recorded entity information and the keywords is greater than the first preset threshold.
[0067] As an optional implementation manner, determining relevant nodes in the knowledge graph according to the keywords includes: vectorizing the keywords to obtain keyword vectors; determining the vector similarity between the keyword vectors and the corresponding vectors of the entity information of each node in the knowledge graph, and using the vector similarity as the similarity between the entity information of the relevant nodes and the keywords; taking the nodes whose similarity between the entity information and the keywords is greater than the preset threshold as the relevant nodes.
[0068] Optionally, determine relevant nodes according to the similarity between the nodes and the problem vector, and the corresponding vectors of the entity information of each node are determined by a graph embedding algorithm.
[0069] Optionally, use algorithms such as depth-first search (DFS) and breadth-first search (BFS) to retrieve nodes related to the problem (i.e., relevant nodes) in the graph. To improve the retrieval efficiency, some improved algorithms can be adopted, such as search algorithms combined with heuristic information.
[0070] Step S306: Obtain the node information of the relevant nodes and the node information of the associated nodes of the relevant nodes. Among them, the associated nodes are the nodes in the knowledge graph that have edge connections with the relevant nodes, and the node information includes the table name of the entity corresponding to the node and the field information of the table of the entity corresponding to the node.
[0071] Optionally, take the nodes that have edge connections with the relevant nodes as the associated nodes.
[0072] Optionally, input the community summaries of the communities where the relevant nodes and the associated nodes are located into a structured query language generation model.
[0073] Step S308: Input the node information of the relevant nodes, the node information of the associated nodes, and the data query instruction into a structured query language generation model, and obtain the structured query language output by the structured query language generation model.
[0074] As an optional implementation manner, after obtaining the structured query language output by the structured query language generation model, the method further includes: displaying the relevant nodes, the associated nodes, and the connection relationship between the relevant nodes and the associated nodes to the user; receiving the first feedback information about the connection relationship from the user, where the first feedback information includes that the connection relationship is correct or the connection relationship is incorrect; in the case where the first feedback information is that the connection relationship is incorrect, obtaining the relationship modification command input by the user; modifying the connection relationship with the incorrect connection relationship according to the relationship modification command.
[0075] Optionally, the knowledge graph generated by GraphRAG supports visualizing the process of text-to-SQL conversion, showing relevant nodes, associated nodes, and the connection relationships between relevant nodes and associated nodes to the user, enabling the user to clearly see how the model retrieves and reasons in the knowledge graph based on the information in the text and finally generates an SQL query statement. This is very helpful for users to understand the model's decision-making process, discover potential problems, and perform debugging and optimization. When an error occurs in the generated SQL query, it is easier to analyze the cause and location of the error based on the structure of the knowledge graph. If an entity is not correctly associated with other entities in the knowledge graph (i.e., the connection relationship is incorrect), or there is a deviation in the understanding of a relationship, it can be quickly discovered and corrected through the visual display of the knowledge graph. By accepting the relationship modification command feedback from the user, the connection relationship can be conveniently modified and the knowledge graph can be updated.
[0076] Specifically, a visual knowledge graph display interface is constructed. When an SQL query statement is generated, the knowledge graph area, community structure, and query path involved in the query are simultaneously displayed on the interface. The user can intuitively see how the SQL statement is generated step by step from a natural language query through the retrieval and analysis of the knowledge graph, including which communities are passed through, and which entities and relationships are involved in the screening and connection. For example, when querying the related cases of a criminal suspect, the interface clearly shows the query logic formed starting from the suspect node, passing through which case type communities, location communities, etc., enabling the user to deeply understand the basis and process of SQL generation.
[0077] Optionally, after obtaining the structured query language output by the structured query language generation model, it further includes using a large language model (LLM) to evaluate and verify the generated SQL. The LLM can evaluate the correctness, effectiveness, and performance of the SQL based on knowledge such as the database schema information and data distribution. For example, the LLM can determine whether the query conditions are reasonable, whether they will cause problems such as data redundancy or missing data. According to the evaluation results of the LLM, optimization suggestions for the SQL are generated. These suggestions can include adding indexes, adjusting query conditions, modifying connection methods, etc. The LLM can provide specific optimization solutions based on its understanding and experience of the database. Input the optimization solution into the structured language generation model to obtain the optimized structured query language, which helps users improve the execution efficiency and performance of the SQL.
[0078] Optionally, after obtaining the structured query language output by the structured query language generation model, the method further includes: presenting the structured query language to the user; obtaining the second feedback information from the user, where the second feedback information includes user satisfaction information.
[0079] Optionally, when the satisfaction information indicates a satisfactory result, the method further includes: running Structured Query Language; obtaining the query result corresponding to the Structured Query Language; generating summary information about the data in the query result based on the query result and the data query instruction; generating an explanatory text based on the Structured Query Language, where the explanatory text includes the query logic of the Structured Query Language; presenting the query result, the summary information, and the explanatory text to the user.
[0080] Optionally, the summary information about the data in the query result refers to the refinement and generalization of the queried data set, including but not limited to statistical metrics (such as mean, maximum, minimum, median, etc.), trend analysis (rising, falling, fluctuating, etc.), pattern recognition (periodicity, seasonality, etc.), anomaly detection (outliers, mutations, etc.), and data-based predictive conclusions. This summary information aims to convey the key features and insights of the data in a concise and clear manner, facilitating the user to quickly understand the meaning and value of the data, especially in scenarios of trend prediction and decision support.
[0081] Optionally, use the LLM to generate a detailed explanatory text for the generated SQL query statement and query result. For example, explain the meaning of each part of the SQL statement, why such a query logic is adopted, and the actual meaning represented by the query result. For example, for a complex multi-table join query, the LLM explains the role of each table in the case analysis, the case relationship reflected by the join condition, and how the data fields in the result correspond to specific case information (i.e., the query logic), enabling the user to comprehensively understand the logic and information value behind the query result while obtaining the query result.
[0082] Optionally, after obtaining the query results corresponding to the Structured Query Language, a summary natural language answer to the query results is generated according to the trained data analysis model. For example, for time series data in the field of political and legal public security, such as the change of crime rate over time, the case detection cycle, etc., time-related entities and relationships are specifically extracted during the construction of the knowledge graph, and a time series data model is constructed. Statistical analysis and machine learning algorithms are used to perform trend analysis on the time series data, such as using ARIMA model, LSTM neural network, etc. to predict future trend directions. When the natural language query involves trend prediction problems, such as "predict the growth trend of theft cases in a certain area in the next three months", the system generates an SQL query statement according to the time series data model and relevant community information, obtains historical data (i.e., query results), and calculates the trend results in combination with the prediction model, and then converts the results into natural language (i.e., summary information) description through the LLM and presents it to the user. Through time series analysis and trend modeling, forward-looking decision-making support can be provided for the field of political and legal public security. For example, predicting crime trends can allocate police resources in advance, formulate preventive measures, and effectively reduce crime risks. When dealing with social security incidents, it is possible to make preparations in advance, improve social stability and public sense of security.
[0083] Optionally, for summary questions, such as "summarize the common characteristics of a certain type of case", text summarization technology is used to extract summaries of relevant case text data in the community. The semantic clustering algorithm is used to group the cases according to similarity, and then the summaries of each group of cases are comprehensively analyzed and refined to find the key common information. The LLM generates a summary natural language answer based on this information and generates the corresponding SQL query statement when necessary to obtain more detailed data to support the integrity and accuracy of the summary. Text summarization and semantic clustering technologies help to gain in-depth insights into the laws and commonalities in political and legal public security operations. For example, summarizing the common characteristics of various cases can provide reference for formulating general investigation strategies, training programs, etc., improve the standardization and efficiency of overall work. At the same time, it also helps to discover potential problems and loopholes and make timely improvements and refinements.
[0084] Optionally, when the satisfaction information indicates an unsatisfactory result, the method further includes: obtaining a Structured Query Language modification instruction input by the user; extracting semantic information from the Structured Query Language modification instruction; determining a modification prompt word based on the semantic information, where the modification prompt word includes a modification target and a modification method corresponding to the modification target, and the modification target includes at least one of the following: table name, field name, logical operator; inputting the modification prompt word into a Structured Query Language generation model, obtaining the Structured Query Language modified by the Structured Query Language generation model according to the modification prompt word, presenting the modified Structured Query Language to the user, and obtaining second feedback information of the user on the modified Structured Query Language until the second feedback information of the user on the modified Structured Query Language is satisfactory to the user.
[0085] Optionally, present the optimized Structured Query Language to the user, and the user can provide feedback and make modifications according to actual needs. The system can, based on the user's feedback on user satisfaction information, combine the data information in GraphRAG and the learning ability of the LLM to perform iterative optimization and continuously improve the quality and accuracy of SQL.
[0086] Optionally, Figure 4 shows the working principle of GraphRAG, as Figure 4 shown, the retrieval-augmented generation in combination with the graph database includes the following workflow:
[0087] S401, Query:
[0088] The user inputs a query, which is the starting point for system processing. The query is sent to two modules simultaneously: the Vector Database and Graph Querying.
[0089] S402, Vector Database:
[0090] Embeddings: The query is converted into embedding vectors for similarity search in the vector database.
[0091] Retrieve: The vector database retrieves the documents or information most relevant to the query based on the embedding vectors.
[0092] These retrieval results are passed to the subsequent Prompt module.
[0093] S403, Graph Querying:
[0094] The query is sent to the Knowledge Graph for graph query. The Knowledge Graph is a structured data store that can quickly retrieve entities and relationships related to the query through a graph structure (nodes and edges). The results of the graph query are also passed to the Prompt module.
[0095] S404, Prompt construction:
[0096] This module is responsible for constructing a prompt to guide the language model (LLM) to generate an answer.
[0097] The prompt usually includes the following parts: Query: The user's original query. Knowledge Graph: The query results from the Knowledge Graph. Context: The retrieval results from the vector database. These pieces of information are integrated into a structured prompt and passed to the language model (LLM).
[0098] S405, Language Model (LLM):
[0099] The LLM generates the final answer (Response) based on the prompt. The LLM combines the query, Knowledge Graph information, and retrieval results to generate an accurate and contextually relevant answer.
[0100] S406. Response: The answer generated by the LLM is returned to the user, completing the entire process.
[0101] Figure 5 Shows a schematic diagram of the NL2SQL process based on GraphRAG, as Figure 5 shown. This process includes: a data preparation stage, a graph storage and vector storage stage, a retrieval stage, and a generation stage, where:
[0102] The data preparation stage includes a Loader, a Splitter, and a Graph Extractor. The loader is used to load data from different data sources (such as tables, documents, etc.). The splitter is used to split the loaded data into segments suitable for processing. Table data will be split into rows or columns, and text data will be split into sentences or paragraphs. The graph extractor is used to extract entities and relationships from the split data and construct a knowledge graph.
[0103] The graph storage and vector storage stage includes storing the constructed knowledge graph (the nodes and edges in the knowledge graph represent entities and the relationships between them), converting the entities and relationships in the knowledge graph into embedding vectors, which are used for similarity search in the vector database, and storing the embedding vectors for fast retrieval.
[0104] The retrieval stage involves the user entering a natural language question. The Embedding Model converts the user's question into an embedding vector (query vector). The Retriever uses the embedding vector to retrieve the most relevant embedding vectors in the vector database. The retrieval results include entities and relationships in the knowledge graph related to the question.
[0105] The generation stage includes extracting relevant community summary information (Relevantsummaries) related to the question from the knowledge graph. These summary information are used to construct a prompt. The synthesizer integrates the retrieved relevant information and the user's question into a prompt. The prompt usually includes the question, relevant entities, and relationships. The LLM (Large Language Model) generates an SQL query based on the prompt and context.
[0106] The NL2SQL support based on GraphRAG enables meticulous retrieval to obtain higher-quality outputs. Specifically, compared with traditional vector-retrieval-based NNL2SQ, the NL2SQL of this application has the following advantages:
[0107] 1. Improve the accuracy of conversion:
[0108] Better understand semantic relationships: In natural language text, the meaning of words and sentences often depends on the context and various potential semantic associations. The introduction of the knowledge graph can help the model more accurately understand the entities in the text and the relationships between them. For example, when the text mentions "father's age", the model can, through the relationship between "father" and "child" in the knowledge graph, accurately understand that the age information related to a specific person is to be queried, and thus generate the correct SQL query statement to obtain the corresponding data.
[0109] Reduce ambiguity: Natural language has inherent ambiguity, and the same expression may have different meanings in different contexts. By constructing a knowledge graph, GraphRAG presents the information in the text in a structured way, which can effectively reduce ambiguity. For example, "the price of apples" can refer to either the selling price of the fruit apple or the price of Apple Inc.'s products. In the knowledge graph, its specific reference can be clarified based on the specific context and relevant entity information, and then an accurate SQL statement can be generated.
[0110] 2. Enhance the ability to handle complex questions:
[0111] Handling multi-table join queries: In practical applications, it is often necessary to involve join queries between multiple tables. Traditional methods may encounter difficulties when dealing with such complex problems. However, NL2SQL based on GraphRAG can utilize the descriptions of the relationships between different tables in the knowledge graph to better understand the multiple tables involved in the problem and the join conditions between them, thereby generating correct multi-table join query statements. For example, when the user asks "Analyze the future case development trend based on the current case and area distribution", the model needs to involve information from multiple tables such as the "case table", "key person table", and "grid division". GraphRAG can accurately understand the relationships between these tables through the knowledge graph and generate appropriate composite SQL queries.
[0112] Supporting complex logical operations: Some complex text queries may involve logical operations, such as "Find people who are over 30 years old, are key persons, and have no job". GraphRAG can accurately identify and analyze the logical relationships in the text with the help of the knowledge graph, convert them into corresponding SQL logical operators and conditional expressions, thereby supporting complex logical operations.
[0113] 3. Improving retrieval efficiency and performance:
[0114] Quickly locating relevant information: Traditional text-to-SQL conversion methods may need to traverse and analyze a large amount of text when retrieving relevant information, resulting in low efficiency. However, by constructing a knowledge graph, GraphRAG stores information in the form of a graph and can use graph indexing and traversal algorithms to quickly locate the information nodes and edges relevant to the problem, thereby improving the retrieval speed and efficiency. This is very important for handling large-scale databases and complex query requests.
[0115] Reducing unnecessary calculations: In the process of generating SQL queries, traditional methods may perform some unnecessary calculations and attempts due to inaccurate understanding of the text. Based on the accurate understanding of the knowledge graph, GraphRAG can conduct more precise analysis and screening of the problem before generating the SQL query, avoid unnecessary calculations, and improve the performance of the entire conversion process.
[0116] 4. Adapting to diverse application scenarios:
[0117] Supporting data in different domains: Data in different domains has different characteristics and structures. Traditional methods may require a large amount of customized development and adjustment for data in different domains. However, GraphRAG can adapt to the data characteristics of different domains by constructing a general knowledge graph framework. Only by filling and updating the knowledge graph according to specific domain knowledge can it achieve NL2SQL conversion for different domains, with stronger generality and adaptability.
[0118] Meeting personalized needs: In some personalized application scenarios, users may have specific query requirements and preferences. GraphRAG can continuously update and optimize the knowledge graph based on the user's historical query records and feedback, so as to better meet the user's personalized needs and provide more accurate SQL query results that conform to the user's expectations.
[0119] Through the above steps, the technical effects of improving the retrieval accuracy and information integrity can be achieved, and further solve the technical problem of inaccurate structured query language caused by low retrieval accuracy and incomplete information in the prior art during retrieval. Specifically, the method embodiments of the present application have the following advantages:
[0120] 1. Introduction of knowledge graph and community structure: The knowledge graph and community structure constructed by GraphRAG provide richer semantic information and structural information for NL2SQL. Traditional NL2SQL methods mainly rely on the surface information of the text and simple pattern matching, while GraphRAG can capture the internal connections and topic distributions between data, making the generated SQL more in line with the actual situation of the data and the user's intention. For example, for complex multi-table association queries, the relationship between tables can be better understood through the community structure, so as to generate more accurate join statements.
[0121] Precise retrieval and fast response: Through multi-level knowledge graph indexing and dynamic community detection, RAG can quickly and accurately locate the information related to the query, greatly reducing the retrieval time and consumption of computing resources. In the field of political and legal public security, in the face of a large amount of complex data, such as crime databases, personnel information databases, etc., it can quickly provide accurate information support for tasks such as case detection and public security management, improving work efficiency and response speed. For example, in an emergency case investigation, it can quickly find relevant clue communities from the huge knowledge graph, provide key information for the police, and shorten the case detection cycle.
[0122] Improvement of data utilization efficiency: Avoiding blind search enables the system to make more effective use of data resources, and the information within each community can be utilized targeted. It will not cause confusion or inaccuracy of retrieval results due to the interference of irrelevant data, improving the data value mining ability. For example, when analyzing the patterns of specific types of crimes, after accurately locating the relevant community, it is possible to deeply analyze the data within the community and discover the hidden crime patterns and trends, providing a strong basis for formulating targeted prevention and crackdown strategies.
[0123] 2. Improvement of the accuracy of SQL generation:
[0124] Combining the powerful capabilities of LLM: LLM has powerful capabilities in natural language understanding and generation, and can accurately transform natural language questions into SQL query statements. At the same time, LLM can also evaluate and optimize SQL, providing professional suggestions and improvement plans. Combining GraphRAG and LLM gives full play to the advantages of both, improving the accuracy, efficiency, and intelligence of NL2SQL.
[0125] Accurate data acquisition: Based on semantic-enhanced SQL template matching and LLM-assisted verification and correction, the generated SQL query statements can highly accurately obtain the required data from the database. In the work of political and legal public security, this means being able to obtain precise information related to cases, avoiding case analysis deviations caused by incorrect or incomplete data. For example, when investigating the social relations of a criminal suspect, accurate SQL queries can comprehensively obtain information such as their associated personnel and interaction records, providing reliable clue support for case detection.
[0126] Reducing misjudgments and improving decision-making quality: Accurate SQL generation helps reduce misjudgments caused by inaccurate data and improve the decision-making quality of political and legal public security personnel. Whether it is in decision-making links such as case determination, crime trend judgment, or resource allocation, analysis based on accurate data can formulate more scientific and reasonable strategies and plans. For example, when formulating a public security patrol plan, accurate crime data queries can help determine key patrol areas and times, improving the effectiveness of public security management.
[0127] 3. Enhancing interpretability:
[0128] By constructing a visual knowledge graph display interface and explaining the generation logic of SQL and the data summary of query results, user trust and transparency can be improved. The knowledge graph visualization and the explanatory text generated by LLM make the SQL generation process and results more transparent and interpretable. Political and legal public security personnel can clearly understand the source and basis of data queries, enhancing their trust in the system. This interpretability is particularly important when it comes to important case decisions or data sharing, improving the credibility and collaborative efficiency of work. At the same time, intuitive explanations help new recruits quickly understand the working principle of the system and the data query logic, facilitating knowledge inheritance and training. In the field of political and legal public security, with the continuous application of technology and personnel replacement, this interpretability can accelerate the growth of new recruits and improve the business level and data analysis ability of the entire team.
[0129] 4. Scalability and Adaptability: The method embodiments of the present application can be applied to various different types of databases and data structures, with strong scalability and adaptability. Whether it is a relational database, a graph database, or other types of databases, a knowledge graph can be constructed through GraphRAG and combined with an LLM for SQL generation and optimization. At the same time, for continuously changing data and user requirements, the method embodiments of the present application can maintain good performance and accuracy by continuously updating the knowledge graph and the learning of the LLM. The method embodiments of the present application are applicable to various fields that require analysis and summary by combining global information, such as the political and legal public security field.
[0130] An embodiment of the present application provides a structured query language generation device. Figure 6 is a schematic structural diagram of the device, as Figure 6 shown. The device includes: a receiving module 60, configured to receive a data query instruction from a user and extract keywords in the data query instruction, where the keywords include at least one of the following: entity name, attribute name; a determining module 62, configured to determine relevant nodes in the knowledge graph according to the keywords, where the nodes in the knowledge graph are used to record entity information in the database, the edges in the knowledge graph are used to record the relationships between entities, and the relevant nodes are nodes whose recorded entity information has a similarity greater than a first preset threshold with the keywords; an obtaining module 64, configured to obtain the node information of the relevant nodes and the node information of the associated nodes of the relevant nodes, where the associated nodes are nodes in the knowledge graph that have an edge connection with the relevant nodes, and the node information includes the table name of the entity corresponding to the node and the field information of the table of the entity corresponding to the node; a generating module 66, configured to input the node information of the relevant nodes, the node information of the associated nodes, and the data query instruction into a structured query language generation model, and obtain the structured query language output by the structured query language generation model.
[0131] In some embodiments of the present application, after extracting the keywords in the data query instruction, it further includes: determining a target community in the knowledge graph, where the community summary of the target community has a similarity greater than a second preset threshold with the keywords, and the community summary includes at least one of the following: summary information on the relationships between the nodes in the community, summary information on the attribute information of each node, and the data range of each node in the community; inputting the community summary of the target community into the structured query language generation model.
[0132] In some embodiments of the present application, determining relevant nodes in the knowledge graph according to the keywords includes: vectorizing the keywords to obtain keyword vectors; determining the vector similarity between the keyword vectors and the vector corresponding to the entity information of each node in the knowledge graph, and using the vector similarity as the similarity between the entity information of the relevant nodes and the keywords; using the nodes whose entity information has a similarity greater than the preset threshold with the keywords as the relevant nodes.
[0133] In some embodiments of the present application, after obtaining the structured query language output by the structured query language generation model, the following steps are further included: presenting relevant nodes, associated nodes, and the connection relationship between the relevant nodes and the associated nodes to the user; receiving the first feedback information of the user on the connection relationship, where the first feedback information includes that the connection relationship is correct or the connection relationship is incorrect; in the case where the first feedback information is that the connection relationship is incorrect, obtaining the relationship modification command input by the user; and modifying the connection relationship with incorrect connection relationship according to the relationship modification command.
[0134] In some embodiments of the present application, after obtaining the structured query language output by the structured query language generation model, the following steps are further included: presenting the structured query language to the user; and obtaining the second feedback information of the user, where the second feedback information includes user satisfaction information.
[0135] In some embodiments of the present application, in the case where the satisfaction information indicates a satisfactory result, the following steps are further included: running the structured query language; obtaining the query result corresponding to the structured query language; generating summary information on the data in the query result according to the query result and the data query instruction; generating an explanatory text according to the structured query language, where the explanatory text includes the query logic of the structured query language; and presenting the query result, the summary information, and the explanatory text to the user.
[0136] In some embodiments of the present application, in the case where the satisfaction information indicates an unsatisfactory result, the following steps are further included: obtaining the structured query language modification instruction input by the user; extracting the semantic information in the structured query language modification instruction; determining a modification prompt word according to the semantic information, where the modification prompt word includes a modification target and a modification method corresponding to the modification target, and the modification target includes at least one of the following: table name, field name, logical operator; inputting the modification prompt word into the structured query language generation model, obtaining the structured query language modified by the structured query language generation model according to the modification prompt word, presenting the modified structured query language to the user, and obtaining the second feedback information of the user on the modified structured query language until the second feedback information of the user on the modified structured query language is that the user is satisfied.
[0137] In some embodiments of the present application, before receiving the data query instruction of the user, the following steps are further included: obtaining the structure information of the database, where the structure information includes at least one of the following: database table name, database table field name, field data type, and relationship between database tables; extracting the relationship between entities from the structure information; and constructing a knowledge graph according to the relationship between entities.
[0138] It should be noted that each module in the above Structured Query Language generation device can be a program module (for example, a set of program instructions that implement a specific function), or a hardware module. For the latter, it can be presented in the following forms, but not limited to: the manifestation form of each of the above modules is a processor, or the functions of each of the above modules are implemented by a processor.
[0139] An embodiment of the present application provides a non-volatile storage medium in which a program is stored. When the program runs, it controls the device where the non-volatile storage medium is located to execute the following Structured Query Language generation method: receive a data query instruction from a user, extract keywords in the data query instruction, where the keywords include at least one of the following: entity name, attribute name; determine relevant nodes in the knowledge graph according to the keywords, where the nodes in the knowledge graph are used to record entity information in the database, the edges in the knowledge graph are used to record the relationships between entities, and the relevant nodes are nodes whose recorded entity information has a similarity greater than a first preset threshold with the keywords; obtain the node information of the relevant nodes and the node information of the associated nodes of the relevant nodes, where the associated nodes are nodes in the knowledge graph that have an edge connection with the relevant nodes, and the node information includes the table name of the entity corresponding to the node and the field information of the table of the entity corresponding to the node; input the node information of the relevant nodes, the node information of the associated nodes, and the data query instruction into a Structured Query Language generation model to obtain the Structured Query Language output by the Structured Query Language generation model.
[0140] An embodiment of the present application provides an electronic device, including: a memory and a processor, where the processor is used to run the program stored in the memory. When the program runs, it executes the Structured Query Language generation method: receive a data query instruction from a user, extract keywords in the data query instruction, where the keywords include at least one of the following: entity name, attribute name; determine relevant nodes in the knowledge graph according to the keywords, where the nodes in the knowledge graph are used to record entity information in the database, the edges in the knowledge graph are used to record the relationships between entities, and the relevant nodes are nodes whose recorded entity information has a similarity greater than a first preset threshold with the keywords; obtain the node information of the relevant nodes and the node information of the associated nodes of the relevant nodes, where the associated nodes are nodes in the knowledge graph that have an edge connection with the relevant nodes, and the node information includes the table name of the entity corresponding to the node and the field information of the table of the entity corresponding to the node; input the node information of the relevant nodes, the node information of the associated nodes, and the data query instruction into a Structured Query Language generation model to obtain the Structured Query Language output by the Structured Query Language generation model.
[0141] An embodiment of the present application provides a computer program product, including a computer program, which implements the following Structured Query Language (SQL) generation method when executed by a processor: receiving a data query instruction from a user, extracting keywords in the data query instruction, where the keywords include at least one of the following: entity name, attribute name; determining relevant nodes in a knowledge graph according to the keywords, where the nodes in the knowledge graph are used to record entity information in a database, the edges in the knowledge graph are used to record relationships between entities, and the relevant nodes are nodes whose similarity between the recorded entity information and the keywords is greater than a first preset threshold; obtaining the node information of the relevant nodes and the node information of the associated nodes of the relevant nodes, where the associated nodes are nodes in the knowledge graph that have an edge connection with the relevant nodes, and the node information includes the table name of the entity corresponding to the node and the field information of the table of the entity corresponding to the node; inputting the node information of the relevant nodes, the node information of the associated nodes, and the data query instruction into an SQL generation model, and obtaining the SQL output by the SQL generation model.
[0142] In the above embodiments of the present application, the descriptions of the various embodiments have their own emphases. For parts not detailed in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0143] In several embodiments provided by the present application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only illustrative. For example, the division of the units can be a logical function division. In actual implementation, there can be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection to each other can be through some interfaces. The indirect coupling or communication connection of units or modules can be in an electrical or other form.
[0144] The units described as separate components may or may not be physically separated. The components displayed as units may or may not be physical units, that is, they can be located in one place, or they can be distributed to multiple units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0145] In addition, the functional units in the various embodiments of the present application can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above integrated units can be implemented in the form of hardware or in the form of software functional units.
[0146] When the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the related technology, or all or part of this 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 for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in various embodiments of this application. The aforementioned storage medium includes: various media that can store program codes, such as USB flash drives, read-only memories (ROM, Read-Only Memory), random access memories (RAM, Random Access Memory), mobile hard disks, magnetic disks, or optical discs.
[0147] The above are only the preferred embodiments of this application. It should be noted that for those of ordinary skill in the art, without departing from the principle of this application, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of this application.
Claims
1. A method for generating a Structured Query Language, characterized in that, Including: Receiving a data query instruction of a user, and extracting keywords in the data query instruction, where the keywords include at least one of the following: entity name, attribute name; Determining relevant nodes in a knowledge graph according to the keywords, where nodes in the knowledge graph are used to record entity information in a database, edges in the knowledge graph are used to record relationships between the entities, and the relevant nodes are nodes whose similarity between the recorded entity information and the keywords is greater than a first preset threshold; Obtaining node information of the relevant nodes and node information of associated nodes of the relevant nodes, where the associated nodes are nodes in the knowledge graph that have edge connections with the relevant nodes, and the node information includes the table name of the entity corresponding to the node and field information of the table of the entity corresponding to the node; Inputting the node information of the relevant nodes, the node information of the associated nodes, and the data query instruction into a structured query language generation model, and obtaining the structured query language output by the structured query language generation model.
2. The method for generating a structured query language according to claim 1, wherein After extracting the keywords in the data query instruction, the method further includes: Determining a target community in the knowledge graph, where the similarity between the community summary of the target community and the keywords is greater than a second preset threshold, and the community summary includes at least one of the following: summary information on relationships between nodes in the community, summary information on attribute information of each node, data ranges of each node in the community; Inputting the community summary of the target community into the structured query language generation model.
3. The method for generating a structured query language according to claim 1, wherein Determining relevant nodes in the knowledge graph according to the keywords includes: Vectorizing the keywords to obtain keyword vectors; Determining the vector similarity between the keyword vectors and the vector corresponding to the entity information of each node in the knowledge graph, and using the vector similarity as the similarity between the entity information of the relevant nodes and the keywords; Taking nodes with a similarity between the entity information and the keywords greater than a preset threshold as relevant nodes.
4. The method for generating a structured query language according to claim 1, wherein After obtaining the structured query language output by the structured query language generation model, the method further includes: Displaying the relevant nodes, the associated nodes, and the connection relationship between the relevant nodes and the associated nodes to the user; Receiving first feedback information of the user on the connection relationship, where the first feedback information includes that the connection relationship is correct or the connection relationship is incorrect; When the first feedback information is that the connection relationship is incorrect, obtaining a relationship modification command input by the user; Modifying the connection relationship with a connection relationship error according to the relationship modification command.
5. The method for generating a structured query language according to claim 1, wherein After obtaining the structured query language output by the structured query language generation model, the method further includes: Displaying the structured query language to the user; Obtaining second feedback information of the user, where the second feedback information includes user satisfaction information.
6. The method for generating a structured query language according to claim 5, wherein When the satisfaction information indicates that the result is satisfactory, the method further includes: Running the structured query language; Obtaining a query result corresponding to the structured query language; Generate summary information about the data in the query result based on the query result and the data query instruction; Generate an explanatory text based on the structured query language, where the explanatory text includes the query logic of the structured query language; Display the query result, the summary information, and the explanatory text to the user.
7. The method for generating a structured query language according to claim 5, wherein When the satisfaction information indicates dissatisfaction, the method further includes: Obtain the structured query language modification instruction input by the user; Extract the semantic information in the structured query language modification instruction; Determine a modification prompt word based on the semantic information, where the modification prompt word includes a modification target and a modification method corresponding to the modification target, and the modification target includes at least one of the following: table name, field name, logical operator; Input the modification prompt word into the structured query language generation model, obtain the structured query language modified by the structured query language generation model according to the modification prompt word, display the modified structured query language to the user, and obtain the second feedback information of the user on the modified structured query language until the second feedback information of the user on the modified structured query language is satisfactory.
8. The method for generating a structured query language according to claim 1, wherein Before receiving the data query instruction of the user, the method further includes: Obtain the structure information of the database, where the structure information includes at least one of the following: database table name, database table field name, field data type, and database table relationship; Extract the relationship between entities from the structure information; Construct a knowledge graph based on the relationship between the entities.
9. A structured query language generation device, characterized in that Include: A receiving module, configured to receive a data query instruction of a user and extract keywords in the data query instruction, where the keywords include at least one of the following: entity name, attribute name; A determining module, configured to determine relevant nodes in the knowledge graph according to the keywords, where the nodes in the knowledge graph are used to record entity information in the database, the edges in the knowledge graph are used to record the relationships between the entities, and the relevant nodes are nodes whose recorded entity information has a similarity greater than a first preset threshold with the keywords; An obtaining module, configured to obtain the node information of the relevant nodes and the node information of the associated nodes of the relevant nodes, where the associated nodes are nodes in the knowledge graph that are connected to the relevant nodes by edges, and the node information includes the table name of the entity corresponding to the node and the field information of the table of the entity corresponding to the node; A generating module, configured to input the node information of the relevant nodes, the node information of the associated nodes, and the data query instruction into a structured query language generation model, and obtain the structured query language output by the structured query language generation model.
10. A non-volatile storage medium, characterized in that, A program is stored in the non-volatile storage medium, and when the program runs, it controls the device where the non-volatile storage medium is located to execute the structured query language generation method according to any one of claims 1 to 8.
11. An electronic device, characterized in that, Include: A memory and a processor, the processor being configured to run a program stored in the memory, wherein, when the program runs, it executes the structured query language generation method according to 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 structured query language generation method according to any one of claims 1 to 8.
Citation Information
Cited By
Table establishment method and device based on cooperation of large language model and speculation algorithm
CN120725022A
Table building method and device based on large language model and speculation algorithm cooperation
CN120725022B
Data processing method and device and data query method and device
CN120763260A
LLM-driven data query dependency retrieval method and device, equipment and medium
CN121327192A
Systems, methods, media and products for crop gene questions and answers
CN121722889A