Data question and answer method and system based on graph neural network and large language model
By introducing graph neural networks and large language models into the database Q&A system, the alignment relationship diagram of user problems and database patterns is constructed, and the problems of database schema information representation and SQL statement generation are solved, efficient data table filtering and intelligent SQL statement generation are realized, which significantly improves the intelligence level and user experience of the data Q&A system.
Patent Information
- Application Number
- CN202411635374.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-15
- Publication Date
- 2025-04-08
AI Technical Summary
When using a large language model to conduct database Q&A, we face problems such as unreasonable database schema information representation, difficulty in filtering data tables, complex and diverse SQL query statements corresponding to user problems, as well as unstable SQL statement generation, and difficult to automatically correct syntax and logical errors.
Using the data question-and-answer method based on graph neural network and large language model, the alignment relationship diagram of user problems and database patterns is constructed, and the graph neural network is used to obtain data set table entries related to user problems, and according to the complexity prediction of SQL query statements, the corresponding prompt information template is designed, and the large language model is guided to generate accurate SQL query statements, and comprehensive syntax and structure inspection are carried out.
It realizes efficient representation and mining of complex mode information, intelligently generates SQL statements, comprehensively checks SQL statements, significantly improving the intelligence level and user experience of the data question and answer system.
Smart Images

Figure CN119782456A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of database and natural language processing, and in particular to a data question-answering method and system based on graph neural network and large language model. Background Art
[0002] As the core carrier of information storage, databases gather massive and diverse data resources. Under the traditional technical architecture, users need to master the SQL language and understand the structural pattern information in the database to extract the required information from the database. With the continuous advancement of big language model technology, it has become possible to convert natural language descriptions into SQL language. However, when actually using big language models to complete data question-answering analysis tasks, there are the following problems:
[0003] It is not reasonable to use plain text sequences to represent database schema information. Databases are complex systems that contain rich structural information. They not only contain basic information such as data tables, column names, and their content attributes, but also involve advanced structures such as primary key information of data tables and foreign key relationships between tables. These advanced structural information is crucial for understanding and processing database queries, and these complex relationships are difficult to effectively express through simple sequence text.
[0004] Databases usually contain a large number of data tables, while the number of input tokens for large language models is limited. If the information of all tables in the database is used as input, the number of tokens may exceed the limit. In addition, too long input text will increase the computational burden of the large language model, which may cause slow operation or excessive resource consumption. At the same time, when the input text contains a lot of redundant or irrelevant information, it may interfere with the attention mechanism of the large language model, reduce the model's ability to capture key information, and thus affect the generation effect.
[0005] User questions are also diverse and complex, and the SQL query statements corresponding to user questions may involve different levels of complexity: from simple single-table queries (no joins or nesting), to queries that require multi-table joins in non-nested classes, to complex situations that require both multi-table joins and nested queries. These SQL statements of different complexities have significant differences in generation methods and logical processing, which places higher requirements on large language models.
[0006] The generation of large language models is somewhat unstable, and even if there are minor grammatical or structural flaws in the SQL query statement, the entire SQL statement cannot be executed smoothly, thus causing an error. Therefore, how to ensure that the generated SQL statement is accurate in terms of syntax and logic, and can automatically correct errors based on the error information returned by the database, so as to finally generate an accurate and executable SQL query statement, is a challenge that needs to be solved urgently.
[0007] In summary, when using large language models to answer database questions, we face many challenges, such as the representation of database schema information, the necessity of data table screening, the complexity and diversity of SQL query statements corresponding to user questions, and the inspection and verification of SQL query statements. Solving these problems will help significantly improve the intelligence level and user experience of data question answering systems. Summary of the invention
[0008] In view of the problems existing in the prior art, the purpose of the present invention is to provide a data question and answer method and system based on graph neural network and large language model, which can enhance the data question and answer capability.
[0009] To achieve the above objectives, the technical solution of the present invention includes the following contents.
[0010] A data question answering method based on a graph neural network and a large language model, the method comprising:
[0011] Build an alignment graph between user questions and database schemas, and obtain dataset table items related to user questions in the database based on graph neural networks;
[0012] Generate SQL query statements for the data set table items using a large language model;
[0013] Connect to the database and execute the SQL query statement;
[0014] Based on the user question and the query result of the SQL query statement, an answer to the user question is generated.
[0015] Furthermore, the step of constructing an alignment relationship diagram between user questions and database schemas includes:
[0016] Constructing a schema graph of a database, wherein the nodes of the schema graph include: table nodes and column nodes, and the edges of the schema graph are constructed based on primary keys and foreign keys of data tables in the database;
[0017] An alignment relationship diagram of user questions and database schemas is constructed based on the schema diagram.
[0018] Furthermore, the edges of the pattern graph are constructed based on the primary keys and foreign keys of the data tables in the database, including:
[0019] The primary key type edge is connected between the table node and the column node to identify a column in the data table as the primary key of the data table;
[0020] The foreign key type edge connects between column nodes to make it clear that a column in a data table is a foreign key to a column in another data table.
[0021] Furthermore, an alignment relationship diagram of user questions and database schemas is constructed based on the schema diagram, including:
[0022] Get the vocabulary in the user's question;
[0023] Calculate the matching relationship between the vocabulary and the table nodes and column nodes;
[0024] Generate an alignment relationship graph of user questions and database patterns; wherein the nodes in the alignment relationship graph include: vocabulary nodes, table nodes and column nodes, and the edges in the alignment relationship graph are constructed based on the edges of the pattern graph and the degree of matching relationship between the vocabulary and the table nodes and column nodes.
[0025] Furthermore, the SQL query statement for generating the data set table item by using the large language model includes:
[0026] Acquire data table item information of the data set table item related to the user question;
[0027] Predicting the complexity of the SQL query statement based on the user question and the data table item information; wherein the complexity of the SQL query statement includes: simple class, non-nested class and nested class;
[0028] According to the complexity of the SQL query statement, the corresponding SQL statement is retrieved to generate a prompt information template, and the user question and the data table item information are filled into the SQL statement to generate a prompt information template, and the SQL query statement is obtained based on the large language model.
[0029] Furthermore, when the complexity of the SQL query statement is simple, the content of the SQL statement generation prompt information template includes: role setting and task description, reserved user question and data table item information location, first generation step and output requirements; wherein the first generation step includes: understanding user needs, analyzing table structure and verifying query statements;
[0030] In the case where the complexity of the SQL query statement is a non-nested class, the content of the SQL statement generation prompt information template includes: role setting and task description, reserved user question and data table item information location, second generation step, first few-shot sample part and output requirements; wherein, the second generation step includes: understanding user needs, selecting the main data table, adding connection conditions, adding filtering conditions and sorting rules, and constructing SQL statements, and the few-shot sample part needs to be filled with several SQL statement cases involving nested subqueries related to connection, filtering conditions, and sorting;
[0031] When the complexity of the SQL query statement is a nested type, the content of the SQL statement generation prompt information template includes: role setting and task description, reserved user question and data table item information location, the third generation step, the second few-shot sample part and output requirements; wherein, the third generation step includes: clarifying requirements and understanding table structure, determining the main table and related tables of the query, writing subqueries to decompose complex problems, integrating subqueries into the main query and integrating to construct the main query, and the second few-shot sample part needs to be filled with several SQL statement cases involving nested subqueries.
[0032] Furthermore, the connecting to the database and executing the SQL query statement includes:
[0033] Fill the user question, data table item information and SQL query statement into the preliminary check and modification prompt information template, and perform syntax and structure check on the SQL query statement based on the large language model; wherein the content of the preliminary check and modification prompt information template includes: role setting and task description, reserved user question and data table item information and SQL query statement location, check steps and output requirements, and the check steps include: checking whether the SQL query statement syntax is correct, confirming whether the table name and column name exist, and verifying whether the query logic meets the user's intention;
[0034] Connect to the database and execute SQL query statements that pass syntax and structure checks;
[0035] In the case that the SQL query statement that has passed the syntax and structure check is not executable, the user question, data table item information, SQL query statement and error information are filled into the SQL error correction prompt information template, and after a new SQL query statement is obtained based on the large language model, the step of filling the user question, data table item information and SQL query statement into the preliminary check modification prompt information template is re-executed according to the new SQL query statement; wherein the content of the SQL error correction prompt information template includes: role setting and task description, reserved user question position, reserved data table item information position, reserved SQL query statement position, reserved error information position, modification steps and output requirements, and the modification steps include: analyzing and understanding error information, locating SQL statement problem position, and correcting syntax or logic errors;
[0036] If the SQL query statement that passes the syntax and structure check is executable, the SQL query statement is executed.
[0037] Furthermore, generating an answer to the user's question based on the user's question and the query result of the SQL query statement includes:
[0038] Organizing the query results of the SQL query statement and the user question into a format suitable for processing by a large language model;
[0039] The query result of the format-converted SQL query statement and the user question are filled into the question analysis prompt information template, and the answer to the user question is generated based on the large language model.
[0040] Furthermore, the content of the problem analysis prompt information template includes: role setting and task description, reserved user questions and query result locations, analysis steps and output requirements; wherein the analysis steps include:
[0041] Associate query results with user questions;
[0042] Provide explanations of query results to answer user questions;
[0043] Make sure the answers are accurate and meet the user's requirements.
[0044] A data question answering system based on a graph neural network and a large language model, the system comprising:
[0045] The question-related data table screening module is used to build an alignment relationship graph between user questions and database schemas, and obtain data set table items related to user questions in the database based on the graph neural network;
[0046] An SQL query statement generation module, used to generate SQL query statements for the data set table items using a large language model;
[0047] SQL query statement verification and execution module, used to connect to the database and execute the SQL query statement;
[0048] The question-answer analysis module is used to generate answers to user questions based on the user questions and the query results of the SQL query statements.
[0049] Compared with the prior art, the present invention has at least the following beneficial effects.
[0050] (1) Efficient representation and mining of complex pattern information: By representing the complex pattern information of the database in the form of a directed graph and using a graph neural network to mine the associations therein, the present invention can more effectively understand and utilize the structural information of the database, thereby more accurately screening out data tables related to user questions.
[0051] (2) Intelligent SQL statement generation: Based on user questions and relevant data table information, the present invention can predict the structural complexity of SQL query statements and generate prompt information for large language models according to different complexities, thereby intelligently guiding the large language model to generate accurate SQL query statements.
[0052] (3) Comprehensive SQL statement checking mechanism: The generated SQL query statements will be subject to syntax checking, logic verification, and running data testing to ensure that they are grammatically correct, logically reasonable, and can be correctly executed in the actual database. This comprehensive checking mechanism greatly improves the reliability and practicality of the query statements.
[0053] (4) Improved data question-and-answer capabilities: Compared with the existing technology, the present invention uses graph neural networks to screen data tables related to the problem and predict the structural complexity of the corresponding SQL query statements, which significantly improves the ability to conduct data question-and-answer based on large language models and provides relevant personnel with more convenient and efficient data question-and-answer and analysis tools.
[0054] In summary, the present invention can efficiently represent and mine complex pattern information, intelligently generate SQL statements, comprehensively check SQL statements, and enhance data question-answering capabilities. These innovations provide new ideas and methods for research and application in related fields. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] Figure 1 Block diagram of a data question answering system based on graph neural networks and large language models.
[0056] Figure 2 Flowchart of the data question answering method based on graph neural network and large language model.
[0057] Figure 3 Generates a prompt information template for SQL query statements.
[0058] Figure 4 Check and correct the prompt message template for SQL statements.
[0059] Figure 5 A template for question and answer analysis prompts. DETAILED DESCRIPTION
[0060] In order to make the purpose, technical solutions and advantages of the present invention more clear, the present invention is further described in detail below through specific implementations and in conjunction with the accompanying drawings.
[0061] The data question answering system based on graph neural network and large language model of the present invention is as follows: Figure 1As shown in the figure, it consists of the following core modules: question-related data table screening module, SQL query statement complexity prediction module, SQL query statement generation module, SQ query statement verification and execution module, and question-answer analysis module. First, a directed graph is used to represent the complex pattern information of the database. On the basis of the database pattern graph, the vocabulary vector nodes in the user question and the alignment relationship between the vocabulary vector and the database pattern are added. The data table information directly related to the user question is screened out through the graph neural network. Then, the complexity of the SQL query statement corresponding to the user question is predicted by combining the data table information related to the question and the specific question of the user. Based on the user question, the relevant data table information, and the predicted SQL query complexity, a prompt project is constructed to assist the large language model in generating SQL query statements. The generated SQL query statement is checked by the large language model and connected to the database for actual testing and correction. After the SQL statement is successfully executed, the relevant data can be retrieved from the database. The large language model will conduct in-depth analysis of these data and give answers based on the specific questions of the user.
[0062] Among them, the question-related data table screening module: uses a directed graph to represent the database schema information, and combines the natural language questions input by the user to build an alignment relationship graph between the user question and the database schema. Through the training and learning of the graph neural network, the data table set most relevant to the question is screened out.
[0063] The present invention defines the structural complexity of SQL query statements as simple, non-nested complex and nested complex. The SQL query statement structural complexity prediction module can predict the structural complexity of the SQL query statement corresponding to the user's question based on the question raised by the user and its related data table item information.
[0064] The present invention presets different prompt information templates according to the different complexity of the predicted SQL statement structure. The SQL query statement generation module can select the corresponding prompt information template according to the complexity of the SQL query statement corresponding to the user question predicted by the previous module, and improve the prompt information input to the large language model in combination with the data table item information involved in the user question, the user question and other contents, so as to guide it to generate accurate SQL query statements.
[0065] The SQL query statement verification and execution module uses a large language model to check the generated SQL query statements, including syntax checking, logic verification, and running tests and corrections, to ensure that the final generated SQL query statements are grammatically correct, logically reasonable, and can be correctly executed in the database. On the other hand, it connects to the database and executes the final generated SQL query statements to retrieve relevant data information that meets the query conditions from the database.
[0066] Question and answer analysis module: responsible for receiving the original data retrieved after SQL execution and the user's question information, forming prompt information, so that the large language model can comprehensively use this data, understand the user's intention, and generate accurate and comprehensive answers.
[0067] Figure 2 The flowchart of the data question answering method based on graph neural network and large language model includes the following steps 1 to
[0068] Step 1: Build an alignment graph between user questions and database schemas, and obtain dataset table items related to user questions in the database based on graph neural networks.
[0069] The present invention uses a directed graph to represent the schema information of the database, and on this basis adds an alignment relationship between user questions and database schema, and filters out data table item information related to user questions through a graph neural network.
[0070] Step 1.1: First, display the database schema information in the form of a graph. This database schema diagram not only contains the basic elements in the database, such as nodes to represent data tables and columns, but also covers structural information, which represents the database structure information by defining different types of edges, including primary key types and foreign key types. Specifically, the primary key type edge connects between the table node and the column node, clearly identifying a column as the primary key of a specific data table; while the foreign key type edge connects between column nodes, clearly indicating that a column is a foreign key of a column in another data table.
[0071] Step 1.2: Segment the natural language questions input by the user and convert them into word vectors. Based on the database schema graph, an alignment graph of user questions and database schema is constructed. The nodes of the alignment graph include not only the original tables and columns, but also the words in the user questions. The edge type includes not only the known schema relationships, but also the relationship between the question vocabulary and the schema. The edge type indicates the degree of matching between them.
[0072] Step 1.3: Use graph neural networks to encode and decode the final graph that contains database schema information and user question alignment information. The model can consider the relationship between natural language queries and database schemas at the encoding stage, thereby improving the understanding and processing capabilities of complex queries. Finally, use a classifier to classify the decoding results and filter out the data set table items that are most relevant to the user's question. If multiple tables are involved, the foreign key connection between tables must also be included, thereby providing strong support for subsequent data query and analysis.
[0073] Step 2: Generate SQL query statements for the data set table items using a large language model.
[0074] Step 2.1: Predict the SQL query statement structure complexity.
[0075] The present invention first merges the representation of the screened data table information and the representation of the user question to form a comprehensive feature vector. Then, a classification model (the model is trained by learning a large amount of training data with SQL query statement complexity annotations) is used to predict the complexity of the SQL query statement. The complexity can be divided into: ① simple class, simple class queries refer to those single-table queries that can be completed without table connection or nesting operations. This type of SQL query statement only involves a single data table and does not require complex connections or subqueries; ② non-nested class, non-nested class queries refer to queries that require table connection but do not require subqueries. This type of SQL query statement may involve operations such as connection, filtering conditions or sorting of multiple data tables, but does not yet contain nested subquery structures; ③ nested class, nested class queries are more complex, they not only require table connection, but may also contain subqueries and set operations. This type of SQL query statement may not only contain multi-table connections and complex filtering conditions, but may also involve one or more nested subqueries.
[0076] In order to train a model that can accurately predict the complexity of SQL statements, it is necessary to collect and organize relevant data sets, and process and annotate these data sets. Each piece of data is annotated with its corresponding SQL statement complexity to ensure that the model can fully learn the characteristics of queries of different complexities during the training process. After the model training is completed, the complexity of the SQL query statement corresponding to the question can be predicted by inputting the user question and the related data table information.
[0077] Step 2.2: Generate SQL query statement.
[0078] According to the different complexity of SQL query statement structure, three prompt information templates are preset, such as Figure 3 As shown in the figure, the prompt information template is selected based on the complexity of the predicted SQL query statement, and the template is improved by combining the table information involved in the user question and the user question. The improved prompt is input into the large language model to guide it to generate accurate SQL query statements.
[0079] Specifically, the SQL query statement generation process includes:
[0080] ① According to the complexity of the SQL query statement structure, three prompt information templates are preset to guide the construction of queries of different difficulty levels. (a) For basic simple queries, such queries often only involve a single data table, and simple prompt information can be constructed. Figure 3As shown in the basic simple class prompt information template, the template includes: role setting and task description, reserved user questions and data table item information location, generation steps, and output requirements. (b) For non-nested complex class queries, such queries may cover operations such as connecting, filtering conditions, or sorting multiple data tables. In the prompt information, you can set some few-shot SQL statement examples related to connection, filtering conditions, and sorting. Figure 3 As shown in the non-nested complex class prompt information template, the template includes: role settings and task descriptions, reserved user questions and data table item information locations, generation steps, few-shot sample parts, and output requirements. In the few-shot sample part, several SQL statement cases involving nested subqueries related to connections, filtering conditions, and sorting are required to be provided. The cases are used to show how to gradually build SQL query statements based on user questions and related data table item information according to the generation steps. These cases need to be adjusted according to specific data sets and business needs to ensure their relevance and practicality. (c) For nested complex class queries, such queries may not only contain multi-table connections and complex filtering conditions, but may also involve one or more nested subqueries. In the prompt information, some few-shot cases that decompose complex problems into subqueries can be constructed to better guide users to understand and build queries. As shown in the example Figure 3 The template includes: role setting and task description, reserved user questions and data table item information location, generation steps, few-shot sample part, and output requirements. In the few-shot sample part, several SQL statement cases involving nested subqueries need to be provided to show how to decompose and construct complex SQL query statements based on user questions and related data table item information according to the generation steps. These cases also need to be adjusted according to specific data sets and business needs to ensure their relevance and practicality.
[0081] ② Select a prompt information template based on the predicted complexity of the SQL query statement, fill in the prompt information template based on the user question and the data table information involved, and improve the prompt information.
[0082] ③ Input the improved prompt information into the large language model to guide the large language model to generate SQL query statements.
[0083] Step 3: Connect to the database and execute the SQL query statement.
[0084] In order to ensure that the generated SQL query statement conforms to the syntax and structure specifications and can be successfully executed in the actual database environment, the SQL query statement must first be checked and verified.
[0085] ①Grammar and structure check: Use Figure 4 The preliminary check modification prompt information template fills in the construction prompt information to guide the large language model to check the syntax and structure of the generated SQL statements, check whether there are logical or syntactic errors in the SQL statements, and modify them.
[0086] ② Executability verification: Connect to the database and execute SQL statements that have passed syntax and structure checks to verify their executability. When errors or exceptions are encountered, capture error information and use Figure 4 The SQL error correction prompt information template in the build prompt information is filled in, and the error information is fed back to the large language model, so that the large language model modifies the SQL statement according to the system error information to ensure that the output SQL statement is accurate and executable. The large language model corrects itself according to the error information, and repeatedly performs syntax and structure checks and executable verification until an error-free and executable SQL statement is generated.
[0087] Finally, securely connect to the database and execute the verified SQL query statement to extract the required relevant data.
[0088] Step 4: Generate an answer to the user's question based on the user's question and the query result of the SQL query statement.
[0089] The present invention constructs prompt information based on data query results and user questions, guides the large language model to understand the relationship between data and user questions; inputs the prompt information into the large language model to generate accurate answer analysis for user questions. Figure 5 shown.
[0090] Among them, user question and answer analysis includes:
[0091] ① Data and user question organization: collect and organize user questions and data retrieved from the database through SQL statements, and organize the data and user questions into a format suitable for processing by large language models.
[0092] ② Construct prompt information. Design prompt information based on the characteristics of the data and the needs of the user's questions. The prompt information should be able to guide the large language model to understand the relationship between the data and the user's questions.
[0093] ③ Big language model question answering: submit the sorted data, user questions, and constructed prompt information to the big language model. The big language model understands and analyzes the user's questions based on the received data, user questions, and prompt information. Generate and return answers to user questions.
[0094] In summary, the present invention constructs a data table screening model, which can accurately screen out data table item information associated with user questions from numerous data tables in a database.
[0095] The present invention constructs a complexity prediction model for SQL query statements corresponding to user questions, and can predict the structural complexity of SQL query statements corresponding to user questions based on user questions and associated data table item information.
[0096] The present invention designs corresponding prompt information templates for different complexities of SQL query statement structures. According to the templates, the user's specific questions and related data table item information are combined to construct appropriate prompt information content, which is provided to the large language model to generate SQL query statements.
[0097] The present invention checks whether the generated SQL statement has syntax errors and structural omissions, and connects to the database to verify whether the SQL statement can be executed. If an error occurs during the trial run, the SQL query statement can be modified in combination with the error information to ensure that the final output SQL statement is accurate and executable.
[0098] The present invention combines the data queried from the database with the user's original question to conduct in-depth question-answer analysis and output answers that both meet the user's needs and are supported by actual data.
[0099] The above is one of the embodiments of the present invention, and the present invention should not be limited to the contents disclosed in the embodiment and the drawings. Any equivalent or modification completed without departing from the spirit disclosed in the present invention shall fall within the scope of protection of the present invention.
Claims
1. A data question answering method based on graph neural network and large language model, characterized in that: The method comprises: Build an alignment graph between user questions and database schemas, and obtain dataset table items related to user questions in the database based on graph neural networks; Generate SQL query statements for the data set table items using a large language model; Connect to the database and execute the SQL query statement; Based on the user question and the query result of the SQL query statement, an answer to the user question is generated.
2. The method according to claim 1, characterized in that The step of constructing an alignment relationship diagram between user questions and database schemas includes: Constructing a schema graph of a database, wherein the nodes of the schema graph include: table nodes and column nodes, and the edges of the schema graph are constructed based on primary keys and foreign keys of data tables in the database; An alignment relationship diagram of user questions and database schemas is constructed based on the schema diagram.
3. The method according to claim 2, characterized in that The edges of the pattern graph are constructed based on the primary keys and foreign keys of the data tables in the database, including: The primary key type edge is connected between the table node and the column node to identify a column in the data table as the primary key of the data table; The foreign key type edge connects between column nodes to make it clear that a column in a data table is a foreign key to a column in another data table.
4. The method according to claim 2, characterized in that: An alignment relationship diagram of user questions and database schemas is constructed based on the schema diagram, including: Get the vocabulary in the user's question; Calculate the matching relationship between the vocabulary and the table nodes and column nodes; Generate an alignment relationship graph of user questions and database patterns; wherein the nodes in the alignment relationship graph include: vocabulary nodes, table nodes and column nodes, and the edges in the alignment relationship graph are constructed based on the edges of the pattern graph and the degree of matching relationship between the vocabulary and the table nodes and column nodes.
5. The method according to claim 1, characterized in that The SQL query statement for generating the data set table item by using the large language model includes: Acquire data table item information of the data set table item related to the user question; Predicting the complexity of the SQL query statement based on the user question and the data table item information; wherein the complexity of the SQL query statement includes: simple class, non-nested class and nested class; According to the complexity of the SQL query statement, the corresponding SQL statement is retrieved to generate a prompt information template, and the user question and the data table item information are filled into the SQL statement to generate a prompt information template, and the SQL query statement is obtained based on the large language model.
6. The method according to claim 5, characterized in that When the complexity of the SQL query statement is simple, the content of the SQL statement generation prompt information template includes: role setting and task description, reserved user question and data table item information location, first generation step and output requirements; wherein the first generation step includes: understanding user needs, analyzing table structure and verifying query statements; In the case where the complexity of the SQL query statement is a non-nested class, the content of the SQL statement generation prompt information template includes: role setting and task description, reserved user question and data table item information location, second generation step, first few-shot sample part and output requirements; wherein, the second generation step includes: understanding user needs, selecting the main data table, adding connection conditions, adding filtering conditions and sorting rules, and constructing SQL statements, and the few-shot sample part needs to be filled with several SQL statement cases involving nested subqueries related to connection, filtering conditions, and sorting; When the complexity of the SQL query statement is a nested type, the content of the SQL statement generation prompt information template includes: role setting and task description, reserved user question and data table item information location, the third generation step, the second few-shot sample part and output requirements; wherein, the third generation step includes: clarifying requirements and understanding table structure, determining the main table and related tables of the query, writing subqueries to decompose complex problems, integrating subqueries into the main query and integrating to construct the main query, and the second few-shot sample part needs to be filled with several SQL statement cases involving nested subqueries.
7. The method according to claim 5, characterized in that The connecting to the database and executing the SQL query statement includes: Fill the user question, data table item information and SQL query statement into the preliminary check and modification prompt information template, and perform syntax and structure check on the SQL query statement based on the large language model; wherein the content of the preliminary check and modification prompt information template includes: role setting and task description, reserved user question and data table item information and SQL query statement location, check steps and output requirements, and the check steps include: checking whether the SQL query statement syntax is correct, confirming whether the table name and column name exist, and verifying whether the query logic meets the user's intention; Connect to the database and execute SQL query statements that pass syntax and structure checks; In the case that the SQL query statement that has passed the syntax and structure check is not executable, the user question, data table item information, SQL query statement and error information are filled into the SQL error correction prompt information template, and after a new SQL query statement is obtained based on the large language model, the step of filling the user question, data table item information and SQL query statement into the preliminary check modification prompt information template is re-executed according to the new SQL query statement; wherein the content of the SQL error correction prompt information template includes: role setting and task description, reserved user question position, reserved data table item information position, reserved SQL query statement position, reserved error information position, modification steps and output requirements, and the modification steps include: analyzing and understanding error information, locating SQL statement problem position, and correcting syntax or logic errors; If the SQL query statement that passes the syntax and structure check is executable, the SQL query statement is executed.
8. The method according to claim 1, characterized in that The generating an answer to the user question based on the user question and the query result of the SQL query statement includes: Organizing the query results of the SQL query statement and the user question into a format suitable for processing by a large language model; The query result of the format-converted SQL query statement and the user question are filled into the question analysis prompt information template, and the answer to the user question is generated based on the large language model.
9. The method according to claim 8, characterized in that The content of the problem analysis prompt information template includes: role setting and task description, reserved user questions and query result locations, analysis steps and output requirements; wherein the analysis steps include: Associate query results with user questions; Provide explanations of query results to answer user questions; Make sure the answers are accurate and meet the user's requirements.
10. A data question answering system based on graph neural network and large language model, characterized in that: The system comprises: The question-related data table screening module is used to build an alignment relationship graph between user questions and database schemas, and obtain data set table items related to user questions in the database based on the graph neural network; An SQL query statement generation module, used to generate SQL query statements for the data set table items using a large language model; SQL query statement verification and execution module, used to connect to the database and execute the SQL query statement; The question-answer analysis module is used to generate answers to user questions based on the user questions and the query results of the SQL query statements.
Citation Information
Cited By
SQL statement generation method and device, electronic equipment and storage medium
CN120470021A