Natural language query method and system for relational database
By establishing sub-databases and schema screening, combining vector space and thinking chain technology, the difficulties of existing natural language to SQL technology in dealing with complex queries and dissolving semantic ambiguity are solved, and more accurate and efficient SQL generation is achieved.
Patent Information
- Application Number
- CN202510002847.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-02
- Publication Date
- 2025-05-02
- Estimated Expiration
- 2045-01-02
AI Technical Summary
Existing natural language conversion SQL technology has difficulties in dealing with complex queries and dissolving semantic ambiguity, especially for queries with unconventional expressions, which are difficult to accurately generate SQL statements that meet expectations.
By establishing sub-databases and schema screening, the difficulty of model reasoning is reduced; using vector space to find similar question-and-answer examples, and enhancing the model reasoning ability through thinking chains, and finally generating SQL statements.
It effectively reduces the probability of generating errors, enhances the model's ability to handle complex queries and resolve semantic ambiguity, and can more accurately generate SQL statements that meet expectations.
Smart Images

Figure CN119917519A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of database and natural language processing technology, and in particular to a natural language query method and system for a relational database. Background Art
[0002] Natural language query aims to convert natural language into structured SQL statements and is widely used in scenarios such as intelligent data analysis, voice assistants, and automated report generation. Its implementation process integrates multiple technical fields such as natural language processing and databases, and aims to enable users to query complex database information through simple language.
[0003] Natural language understanding technology: The core of natural language query is to deeply understand the natural language input by the user and extract the query intent. This process involves multiple technologies, including text preprocessing, entity recognition, etc. Through these technologies, the system can identify keywords, table names, field names, and various relationship information in natural language.
[0004] Semantic mapping and reasoning: The task of semantic mapping technology is to map abstract queries in natural language to the actual structure of the database. This process is not just about vocabulary matching, but also involves deep reasoning about the query intent. For example, the system needs to understand that "get the names of all employees older than 30" is not only a query about the "employee" table, but also needs to clarify the meaning of the fields "age" and "name" and generate appropriate SQL statements based on the structure of the database.
[0005] Although natural language to SQL technology has made significant progress, it still faces many challenges in practical applications. The processing of complex queries, the resolution of semantic ambiguity, and the adaptation of database structures are still difficult issues in research. In particular, for some queries with unconventional expressions, existing models often have difficulty in accurately generating SQL statements that meet expectations. Summary of the invention
[0006] The present invention provides a natural language query method and system for a relational database, the method comprising:
[0007] Step S1. Obtain external knowledge description and user's natural language questions;
[0008] Step S2. Extracting the table structure from the local connection database according to the user's selection and generating a sub-database based on the natural language question;
[0009] Step S3. Construct a screening prompt word template, input the sub-database model and the natural language question into the screening prompt word template to obtain a complete prompt word, input the complete prompt word into the large language model, obtain the table name and column name used for this natural language question, and generate a simplified database model according to the table name and column name;
[0010] Step S4. Construct a prediction prompt word template, put the natural language question into the prediction prompt word template, and obtain the predicted SQL keyword skeleton;
[0011] Step S5. convert the SQL keyword skeleton into a prediction vector, select a preset number of sample databases from the local connection database, extract the question-answer pairs in the sample database, convert the SQL statements of the question-answer pairs into sample SQL vectors and store them in the vector database;
[0012] Step S6. Calculate the similarity between the predicted vector and the example SQL vector in the vector database, obtain a preset number of similarity results and corresponding example SQL vectors, and take the question-answer pair corresponding to the SQL vector as an example;
[0013] Step S7. The simplified database model, natural language question, external knowledge description and the example input prompt word template are used to obtain a complete prompt word, and the prompt word is input into the large language model. The large language model uses the thinking chain to generate SQL statements to complete the natural language query.
[0014] Optionally, in step S2, extracting the table structure from the local connection database according to the user's selection and generating the content of the sub-database based on the natural language question specifically includes:
[0015] Obtain all schemas in a locally connected database, where the database schema is structural information of the database;
[0016] Filter tables and columns across all database schemas based on user selections;
[0017] A sub-database is generated based on the screening results.
[0018] Optionally, in step S3, the process of generating a simplified database schema according to the table name and the column name specifically includes:
[0019] Determine whether the table name and column name exist in the sub-database schema, and determine whether the nested relationship between the table and column conforms to the sub-database schema. If both are true, a simplified database schema is generated.
[0020] Optionally, in step S4, the predicted SQL keyword skeleton is a character string consisting of keywords and symbols in the SQL grammar except for custom content.
[0021] The present invention discloses a natural language query system for a relational database, the system comprising:
[0022] Data collection module, used to obtain external knowledge descriptions and users' natural language questions;
[0023] A sub-database generation module is used to extract the table column structure from the local connection database according to the user's selection and generate a sub-database based on natural language questions;
[0024] A simplified schema extraction module is used to construct a screening prompt word template, input the sub-database schema and the natural language question into the screening prompt word template to obtain a complete prompt word, input the complete prompt word into the large language model, obtain the table name and column name used in this natural language question, and generate a simplified database schema according to the table name and column name;
[0025] The skeleton prediction module is used to construct a prediction prompt word template, put the natural language question into the prediction prompt word template, and obtain the predicted SQL keyword skeleton;
[0026] A vector conversion module, used to convert the SQL keyword skeleton into a prediction vector, screen a preset number of sample databases from the local connection database, extract question-answer pairs from the sample database, convert the SQL statements of the question-answer pairs into sample SQL vectors, and store them in the vector database;
[0027] A similarity calculation module, used to calculate the similarity between the predicted vector and the example SQL vector in the vector database, obtain a preset number of similarity results and corresponding example SQL vectors, and take the question-answer pair corresponding to the SQL vector as an example;
[0028] The statement generation module is used to obtain complete prompt words from the simplified database model, natural language questions, external knowledge description and the example input prompt word template, input the prompt words into the large language model, and the large language model uses the thinking chain to generate SQL statements to complete the natural language query.
[0029] Optionally, the workflow of the sub-database generation module specifically includes:
[0030] Obtain all schemas in a locally connected database, where the database schema is structural information of the database;
[0031] Filter tables and columns of all database schemas based on user selections;
[0032] A sub-database is generated based on the screening results.
[0033] Optionally, in the simplified schema extraction module, the process of generating a simplified database schema according to the table name and the column name specifically includes:
[0034] Determine whether the table name and column name exist in the sub-database schema, and determine whether the nested relationship between the table and column conforms to the sub-database schema. If both are true, a simplified database schema is generated.
[0035] Optionally, in the skeleton prediction module, the predicted SQL keyword skeleton is a character string consisting of keywords and symbols in the SQL grammar except for custom content.
[0036] Compared with the prior art, the present invention has the following beneficial effects:
[0037] The present invention reduces the difficulty of model reasoning by establishing a sub-database and pattern screening, enhances the model reasoning ability by finding similar question-answer pair examples and thinking chain methods in vector space, and minimizes the probability of generation errors through pattern verification and result verification methods. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] In order to more clearly illustrate the technical solution of the present invention, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0039] Figure 1 This is a method step diagram of a natural language query method for a relational database according to an embodiment of the present invention. DETAILED DESCRIPTION
[0040] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0041] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0042] Embodiment 1
[0043] A natural language query method for relational databases, such as Figure 1 As shown, the method includes:
[0044] Step S1. Obtain external knowledge description and user's natural language questions.
[0045] The user enters the table name and column name that he wants to use later to preliminarily define the data range, enters the natural language question for subsequent generation of the corresponding SQL, and enters the external knowledge description to help the large model understand and reason. For example, the user enters the question: "Please list the name of each student and the average score of all their test scores." The selected table column is: student (student_name, test_score, student_id).
[0046] External knowledge is mainly used to help large models understand some professional knowledge, such as jargon or small-scale conventional rules. This type of knowledge is usually not within the training scope of general large models, so additional explanations are needed to help the model reason.
[0047] Step S2: extracting a table structure from a local connection database according to the natural language question and the external knowledge description, and generating a sub-database based on the natural language question.
[0048] First, obtain the entire schema of the locally connected database. The database schema usually refers to the structural information of the database, such as which tables exist in the database, the names of the columns in the tables, data types, foreign key relationships, etc. Then filter out the structures within the selected range in step 1 from the schema and organize them into a new sub-database.
[0049] The database schema can be represented in the form of a table creation statement, for example:
[0050]
[0051] It should be noted that different models have different understanding capabilities for different pattern representations. For example, the OpenAI model uses the table (column, column, ...) format during training, so this format is more effective, for example:
[0052] student(student_id,student_name,test_score)
[0053] Step S3. Construct a screening prompt word template, input the sub-database model and the natural language question into the screening prompt word template to obtain complete prompt words, input the complete prompt words into the large language model, obtain the table name and column name used for this natural language question, and generate a simplified database model based on the table name and column name.
[0054] Design a screening prompt word template, put the sub-database model obtained in step 2 and the natural language question input by the user obtained in step 1 into the prompt word template, submit the complete prompt word to the big model, and the big model determines which tables and columns are needed to generate the SQL for this question, and obtains the table name and column name returned by the model. For example, the table name returned here is: student, and the column name is: student_name, test_score.
[0055] After the model returns the result, verify whether the result is hallucinated. Specifically, verify whether the returned table name exists in the child database schema.
[0056] Whether the column names in the table exist in the sub-database schema, whether the nested relationship between the table and the column conforms to the sub-database schema. If the table creation statement format is used, whether the foreign key relationship and data type of the column conform to the sub-database schema.
[0057] If there is no illusion, a simplified database schema is generated. The simplified database schema uses the same format as the previous step. For example, the simplified database schema here is: student(student_name,test_score).
[0058] Step S4: construct a prediction prompt word template, put the natural language question into the prediction prompt word template, and obtain a predicted SQL keyword skeleton.
[0059] Design a prediction prompt word template, put the user's natural language question into the prompt word, and submit the complete prompt word to the big model. The big model predicts the SQL skeleton of this question. The SQL skeleton refers to a string consisting only of keywords and symbols in the SQL syntax, which does not contain customized elements such as table names and column names. This operation is designed to eliminate the interference of personalized data and only focus on the SQL syntax corresponding to the current question. For example, for the above example, the prediction here is SELECT#FROM#GROUP BY# (# represents null).
[0060] Step S5. Convert the SQL keyword skeleton into a prediction vector, select a preset number of sample databases from the local connection database, extract the question-answer pairs in the sample database, convert the SQL statements of the question-answer pairs into sample SQL vectors, and store them in the vector database.
[0061] The skeleton is converted into a vector using the native model of the vector database Chorma, and the three most similar examples are matched in the vector space.
[0062] Specifically, it is divided into the following steps: constructing a vector dataset, embedding a SQL skeleton, and retrieving similar examples.
[0063] Step 5-1: First, manually select the 10 sample databases with the most complex patterns in the Spider dataset, select representative question-answer pairs in each database, and use the native model of the vector database Chroma to convert SQL statements into sample SQL vectors, store them in the vector database, and form a vector dataset.
[0064] In step 5-2, the user's natural language question is put into the prompt word, and the large model predicts its corresponding SQL skeleton, and the skeleton is embedded into the vector space using the same method as step 5-1.
[0065] The generation process of the large language model is as follows:
[0066] Input representation: Assume that the input prompt word sequence is: X = {x 1 ,x 2 ,...,x n}, where x i Represents the i-th token in the input sequence. The input token is transformed into a vector representation through the embedding layer and positional encoding:
[0067] E i =Embed(x i )+PosEnc(i)
[0068] The representation matrix of the input sequence is: E = [E 1 ,E 2 ,...,E n ].
[0069] Self-Attention Mechanism: The weight of each token in the input sequence is calculated through the self-attention mechanism. The query (Q), key (K), and value (V) vectors are obtained by linear transformation:
[0070] Q=EW Q ,K=EW K ,V=EW V ;
[0071] in, W Q , W K , WV is a trainable parameter matrix. The attention weight is calculated by dot product attention:
[0072]
[0073] Among them, d kis the dimension of the key vector, used to scale the dot product. Multi-Head Attention: Multi-Head Attention captures information from different subspaces through parallel computation of multiple attention heads:
[0074] MultiHead(Q,K,V)=Concat(head 1 ,...,head h )W O
[0075] Each attention head is calculated as:
[0076] head i =Attention(Q i , K i , V i )
[0077] Among them, W 0 is the output transformation matrix.
[0078] Feed-Forward Network (FFN): The attention output of each token is processed through a feed-forward neural network:
[0079] FFN(x)=ReLU(xW 1 +b 1 )W 2 +b 2
[0080] Among them, W 1 ,W 2 ,b 1 ,b 2 is a trainable parameter.
[0081] Layer Normalization & Residual Connection:
[0082] Add residual connections and layer normalization to each layer:
[0083] Output=LayerNorm(x+SubLayer(x))
[0084] Decoding and Generation: For generation tasks, the decoder uses the previous output token as input and generates a probability distribution for the next token:
[0085] P(y t |y <t , X) = Softmax(h t W out +b out )
[0086] Among them, h t is the hidden state of the decoder at time step t, W out and b out are the output layer parameters.
[0087] Recursive generation process: The generated sequence is expanded in an autoregressive manner:
[0088] y t+1 =argmax((P(y t |y <t , X))
[0089] Until the end marker [EOS] is generated.
[0090] Step 5-3, return the top three examples in the vector data set that are similar to the predicted skeleton. As examples for subsequent generation. Here, the cosine similarity is used as the similarity judgment indicator.
[0091] In vector databases, cosine similarity is a common measurement method used to measure the directional similarity of two vectors in vector space. Its value is between -1 and 1, where:
[0092] 1: Indicates that the two vectors are exactly the same (they are in the same direction).
[0093] 0: Indicates that there is no similarity between the two vectors (the directions are orthogonal).
[0094] -1: Indicates that the directions of the two vectors are completely opposite.
[0095] The calculation formula of cosine similarity is as follows:
[0096]
[0097] in: Representation vector and vector dot product and is a vector and The model is:
[0098]
[0099] Step S6. Calculate the similarity between the predicted vector and the example SQL vector in the vector database, obtain a preset number of similarity results and corresponding example SQL vectors, and take the question-answer pair corresponding to the SQL vector as an example.
[0100] Put the simplified database model, natural language question, external knowledge description and three examples into the generated prompt word. Based on this embodiment, the simplified database model obtained in step 3, the user's natural language question obtained in step 1, the external knowledge description and the three examples obtained in step 5 are put into the prompt word template and submitted to the large model.
[0101] For example, here is a similar example:
[0102] 1.{"question":"Please give the total profit for each year","answer":"SELECT year,SUM(profit)AS profit FROM sales GROUP BY year;"},
[0103] 2.{"question":"Please give the total profit for each year and also provide a summary of the total profit for all years","answer":"SELECT year,SUM(profit)AS profit FROM sales GROUP BY year WITHROLLUP;"},
[0104] 3.{"question":"Please list the total profit of each year, each country, and each product","answer":"SELECT year,country,product,SUM(profit)AS profit FROM sales GROUP BY year,country,product;"}
[0105] Step S7. Input the simplified database model, natural language question, external knowledge description and example question and answer pair into the prompt word template to obtain a complete prompt word, input the prompt word into the large language model, and the large language model uses the thinking chain to generate SQL statements to complete the natural language query.
[0106] Give the generated prompt words to the big model, use the thinking chain in the prompt words to enhance the reasoning ability of the big model and generate the required results.
[0107] The use of thought chains in the prompts means that when a question or task is clearly given, the model is guided to come up with an answer through step-by-step reasoning and analysis. The use of thought chains can help the model handle complex problems more clearly and systematically, avoid skipping key steps, and thus generate more accurate and logical results.
[0108] The process of the thinking chain in this method is to first analyze the user problem, then extract pattern information, then make inferences, and finally draw conclusions.
[0109] The large model generates multiple results corresponding to user questions under higher randomness parameters and finds the best solution based on the consistency algorithm.
[0110] The randomness parameter is the model temperature (Temperature), and the choice of temperature should be obtained through repeated testing based on specific circumstances.
[0111] The temperature controls the randomness or determinism of the model's output, which directly affects the diversity and creativity of the generated content. When the temperature is higher, the generated text is more random and the model explores more possible outputs. This often leads to more creative or diverse results, but may also contain more uncertainty or out-of-context content.
[0112] At a suitable high temperature, the model generates multiple results, embeds the results into a vector space, and returns the SQL with the highest similarity to the rest of the results. For example, multiple results are generated here:
[0113] 1.SELECT student_name,AVG(test_score)FROM student GROUP BY student_name;
[0114] 2.SELECT student_name,test_score FROM student GROUP BY student_name;
[0115] 3.SELECT student_name,AVG(test_score)FROM student GROUP BY student_name ORDER BY student_name;
[0116] Select the result with the highest similarity, which is result 1.
[0117] The generated SQL is executed in the database to check the syntax correctness. If it is correct, the SQL is output. If it is wrong, the error prompt is submitted to the big model to guide the big model to modify the correct result and output the SQL.
[0118] If the execution fails, the error message is recorded, and the message and SQL statement are put into the correction prompt word to guide the large model to correct the SQL. If it fails after three times, it will directly return to the large model to regenerate. If it fails after three times, the generation failure message will be directly output. For example, the example here finally successfully generates SQL: SELECT student_name, AVG(test_score) FROM student GROUP BY student_name.
[0119] Embodiment 2
[0120] A natural language query system for a relational database, the system comprising:
[0121] The data collection module is used to obtain external knowledge descriptions and users' natural language questions.
[0122] The user enters the table name and column name that he wants to use later to preliminarily define the data range, enters the natural language question for subsequent generation of the corresponding SQL, and enters the external knowledge description to help the large model understand and reason. For example, the user enters the question: "Please list the name of each student and the average score of all their test scores." The selected table column is: student (student_name, test_score, student_id).
[0123] External knowledge is mainly used to help large models understand some professional knowledge, such as jargon or small-scale conventional rules. This type of knowledge is usually not within the training scope of general large models, so additional explanations are needed to help the model reason.
[0124] The sub-database generation module is used to extract the table column structure from the local connection database according to the natural language question and the external knowledge description, and generate a sub-database based on the natural language question.
[0125] First, obtain the entire schema of the locally connected database. The database schema usually refers to the structural information of the database, such as which tables exist in the database, the names of the columns in the tables, data types, foreign key relationships, etc. Then filter out the structures within the selected range in step 1 from the schema and organize them into a new sub-database.
[0126] In this embodiment, the database mode is represented in the form of a table creation statement, for example:
[0127] CREATE TABLE student(
[0128] student_id int primary key,
[0129] student_name text,
[0130]
[0131] It should be noted that different models have different understanding capabilities for different pattern representations. For example, the OpenAI model uses the table (column, column, ...) format during training, so this format is more effective, for example:
[0132] student(student_id,student_name,test_score).
[0133] The simplified mode extraction module is used to construct a screening prompt word template, input the sub-database mode and the natural language question into the screening prompt word template to obtain a complete prompt word, input the complete prompt word into the large language model, obtain the table name and column name used in this natural language question, and generate a simplified database mode according to the table name and column name. For example, the simplified mode here is: student (test_score, student_name).
[0134] Design a screening prompt word template, put the obtained sub-database model and the natural language question input by the user into the prompt word template, submit the complete prompt word to the big model, the big model determines which tables and columns are needed to generate the SQL for this question, and obtains the table name and column name returned by the model. For example, the table name returned here is: student, and the column name is: student_name, test_score.
[0135] After the model returns the result, verify whether the result is hallucinated. Specifically, verify whether the returned table name exists in the child database schema.
[0136] Whether the column names in the table exist in the sub-database schema, whether the nested relationship between the table and the column conforms to the sub-database schema. If the table creation statement format is used, whether the foreign key relationship and data type of the column conform to the sub-database schema.
[0137] If there is no illusion, a simplified database schema is generated. The simplified database schema uses the same format as the previous step. For example, the simplified database schema here is: student(student_name,test_score).
[0138] The skeleton prediction module is used to build a prediction prompt word template, put the natural language question into the prediction prompt word template, and obtain the predicted SQL keyword skeleton. For example, for the above example, the prediction here is SELECT#FROM#GROUP BY# (# represents empty).
[0139] After designing a prediction prompt word template, put the user's natural language question into the prompt word, and submit the complete prompt word to the big model, the big model predicts the SQL skeleton of this question. The SQL skeleton refers to a string consisting only of keywords and symbols in the SQL syntax, which does not contain customized elements such as table names and column names. This operation is designed to eliminate the interference of personalized data and only focus on the SQL syntax corresponding to the current question.
[0140] A vector conversion module is used to convert the SQL keyword skeleton into a prediction vector, screen out a preset number of example databases from the local connection database, extract question-answer pairs in the example database, convert the SQL statements of the question-answer pairs into example SQL vectors and store them in the vector database; the skeleton is converted into a vector through an embedding model, and the three most similar examples are matched in the vector space.
[0141] Specifically, it is divided into the following steps: constructing a vector dataset, embedding a SQL skeleton, and retrieving similar examples.
[0142] First, we manually screened out the 10 sample databases with the most complex patterns in the Spider dataset, selected representative question-answer pairs in each database, and used the native model of the vector database Chroma to convert SQL statements into sample SQL vectors and store them in the vector database to form a vector dataset.
[0143] The user's natural language question is put into the prompt word, and the large model predicts its corresponding SQL skeleton, and the skeleton is embedded into the vector space using the same method as before.
[0144] The generation process of the large language model is as follows:
[0145] Input representation: Assume that the input prompt word sequence is: X = {x 1 ,x 2 ,...,x n}, where x i Represents the i-th token in the input sequence. The input token is transformed into a vector representation through the embedding layer and positional encoding:
[0146] E i =Embed(x i )+PosEnc(i)
[0147] The representation matrix of the input sequence is: E = [E 1 ,E 2 ,...,E n ], i is the token number of the input sequence.
[0148] Self-Attention Mechanism: The weight of each token in the input sequence is calculated through the self-attention mechanism. The query (Q), key (K), and value (V) vectors are obtained by linear transformation:
[0149] Q=EW Q ,K=EW K ,V=EW V ;
[0150] Among them, W Q ,W K ,WV is a trainable parameter matrix. The attention weight is calculated by dot product attention:
[0151]
[0152] Among them, d k is the dimension of the key vector, used to scale the dot product. Multi-Head Attention: Multi-Head Attention captures information from different subspaces through parallel computation of multiple attention heads:
[0153] MultiHead(Q,K,V)=Concat(head 1 ,...,head h )W O
[0154] Each attention head is calculated as:
[0155] head i =Attention(Q i , K i , V i )
[0156] Among them, W 0 is the output transformation matrix.
[0157] Feed-Forward Network (FFN): The attention output of each token is processed through a feed-forward neural network:
[0158] FFN(x)=ReLU(xW 1 +b 1 )W 2 +b 2
[0159] Among them, W 1 ,W 2 ,b 1 ,b 2 is a trainable parameter.
[0160] Layer Normalization & Residual Connection:
[0161] Add residual connections and layer normalization to each layer:
[0162] Output=LayerNorm(x+SubLayer(x))
[0163] Decoding and Generation: For generation tasks, the decoder uses the previous output token as input and generates a probability distribution for the next token:
[0164] P(y t |y <t , X) = Softmax(h t W out +b out )
[0165] Among them, h t is the hidden state of the decoder at time step t, W out and b out are the output layer parameters.
[0166] Recursive generation process: The generated sequence is expanded in an autoregressive manner:
[0167] y t+1 =argmax((P(y t |y <t , X))
[0168] Until the end marker [EOS] is generated.
[0169] Return the top three examples in the vector data set that are similar to the predicted skeleton. Use them as examples for subsequent generation. Here, cosine similarity is used as the similarity judgment indicator.
[0170] In vector databases, cosine similarity is a common measurement method used to measure the directional similarity of two vectors in vector space. Its value is between -1 and 1, where:
[0171] 1: Indicates that the two vectors are exactly the same (they are in the same direction).
[0172] 0: Indicates that there is no similarity between the two vectors (the directions are orthogonal).
[0173] -1: Indicates that the directions of the two vectors are completely opposite.
[0174] The calculation formula of cosine similarity is as follows:
[0175]
[0176] in: Representation vector and vector dot product and is a vector and The model is:
[0177]
[0178] A similarity calculation module is used to calculate the similarity between the predicted vector and the example SQL vector in the vector database, obtain a preset number of similarity results and corresponding example SQL vectors, and take the question-answer pair corresponding to the SQL vector as an example; for example, the similar example here is:
[0179] 1.{"question":"Please give the total profit for each year","answer":"SELECT year,SUM(profit)AS profit FROM sales GROUP BY year;"},
[0180] 2.{"question":"Please give the total profit for each year and also provide a summary of the total profit for all years","answer":"SELECT year,SUM(profit)AS profit FROM sales GROUP BY year WITHROLLUP;"},
[0181] 3.{"question":"Please list the total profit of each year, each country, and each product","answer":"SELECT year,country,product,SUM(profit)AS profit FROM sales GROUP BY year,country,product;"}.
[0182] Put the simplified database model, natural language question, external knowledge description and three examples into the generated prompt word. Based on this embodiment, the simplified database model obtained in step 3, the user's natural language question obtained in step 1, the external knowledge description and the three examples obtained in step 5 are put into the prompt word template and submitted to the large model.
[0183] The statement generation module is used to input the simplified database model, natural language question, external knowledge description and prediction vector into the prompt word template to obtain a complete prompt word, and input the prompt word into the large language model. The large language model uses the thinking chain to generate SQL statements to complete the natural language query.
[0184] Give the generated prompt words to the big model, use the thinking chain in the prompt words to enhance the reasoning ability of the big model and generate the required results.
[0185] The use of thought chains in the prompts means that when a question or task is clearly given, the model is guided to come up with an answer through step-by-step reasoning and analysis. The use of thought chains can help the model handle complex problems more clearly and systematically, avoid skipping key steps, and thus generate more accurate and logical results.
[0186] The process of the thinking chain in this method is to first analyze the user problem, then extract pattern information, then make inferences, and finally draw conclusions.
[0187] The large model generates multiple results corresponding to user questions under higher randomness parameters and finds the best solution based on the consistency algorithm.
[0188] The randomness parameter is the model temperature (Temperature), and the choice of temperature should be obtained through repeated testing based on specific circumstances.
[0189] The temperature controls the randomness or determinism of the model's output, which directly affects the diversity and creativity of the generated content. When the temperature is higher, the generated text is more random and the model explores more possible outputs. This often leads to more creative or diverse results, but may also contain more uncertainty or out-of-context content.
[0190] At a suitable high temperature, the model generates multiple results, embeds the results into a vector space, and returns the SQL with the highest similarity to the rest of the results. For example, multiple results are generated here:
[0191] 1.SELECT student_name,AVG(test_score)FROM student GROUP BY student_name;
[0192] 2.SELECT student_name,test_score FROM student GROUP BY student_name;
[0193] 3.SELECT student_name,AVG(test_score)FROM student GROUP BY student_name ORDER BY student_name;
[0194] Select the result with the highest similarity, which is result 1.
[0195] The generated SQL is executed in the database to check the syntax correctness. If it is correct, the SQL is output. If it is wrong, the error prompt is submitted to the big model to guide the big model to modify the correct result and output the SQL.
[0196] If the execution fails, the error message is recorded, and the message and SQL statement are put into the correction prompt word to guide the large model to correct the SQL. If it fails after three repetitions, it will directly return to the large model for regeneration. If it fails after three regenerations, the generation failure message will be directly output.
[0197] For example, the example here finally successfully generates the SQL: SELECT student_name, AVG (test_score) FROM student GROUP BY student_name.
[0198] The embodiments described above are only descriptions of the preferred embodiments of the present invention and are not intended to limit the scope of the present invention. Without departing from the design spirit of the present invention, various modifications and improvements made to the technical solutions of the present invention by ordinary technicians in this field should all fall within the protection scope determined by the claims of the present invention.
Claims
1. A natural language query method for a relational database, characterized in that: The method comprises: Step S1. Obtain external knowledge description and user's natural language questions; Step S2. Extracting the table structure from the local connection database according to the user's selection and generating a sub-database based on the natural language question; Step S3. Construct a screening prompt word template, input the sub-database model and the natural language question into the screening prompt word template to obtain a complete prompt word, input the complete prompt word into the large language model, obtain the table name and column name used for this natural language question, and generate a simplified database model according to the table name and column name; Step S4. Construct a prediction prompt word template, put the natural language question into the prediction prompt word template, and obtain the predicted SQL keyword skeleton; Step S5. convert the SQL keyword skeleton into a prediction vector, select a preset number of sample databases from the local connection database, extract the question-answer pairs in the sample database, convert the SQL statements of the question-answer pairs into sample SQL vectors and store them in the vector database; Step S6. Calculate the similarity between the predicted vector and the example SQL vector in the vector database, obtain a preset number of similarity results and corresponding example SQL vectors, and take the question-answer pair corresponding to the SQL vector as an example; Step S7. The simplified database model, natural language question, external knowledge description and the example input prompt word template are used to obtain a complete prompt word, and the prompt word is input into the large language model. The large language model uses the thinking chain to generate SQL statements to complete the natural language query.
2. The natural language query method for a relational database according to claim 1, characterized in that: In step S2, extracting the table structure from the local connection database according to the user's selection and generating the sub-database based on the natural language question specifically includes: Obtain all schemas in a locally connected database, where the database schema is structural information of the database; Filter tables and columns across all database schemas based on user selections; A sub-database is generated based on the screening results.
3. The natural language query method for a relational database according to claim 2, characterized in that: In step S3, the process of generating a simplified database schema according to the table name and column name specifically includes: Determine whether the table name and column name exist in the sub-database schema, and determine whether the nested relationship between the table and column conforms to the sub-database schema. If both are true, a simplified database schema is generated.
4. The natural language query method for a relational database according to claim 1, characterized in that: In step S4, the predicted SQL keyword skeleton is a character string consisting of keywords and symbols in the SQL grammar except for the custom content.
5. A natural language query system for a relational database, the system being used to implement the natural language query method according to any one of claims 1 to 4, characterized in that the system include: Data collection module, used to obtain external knowledge descriptions and users' natural language questions; A sub-database generation module is used to extract the table column structure from the local connection database according to the user's selection and generate a sub-database based on natural language questions; A simplified schema extraction module is used to construct a screening prompt word template, input the sub-database schema and the natural language question into the screening prompt word template to obtain a complete prompt word, input the complete prompt word into the large language model, obtain the table name and column name used in this natural language question, and generate a simplified database schema according to the table name and column name; The skeleton prediction module is used to construct a prediction prompt word template, put the natural language question into the prediction prompt word template, and obtain the predicted SQL keyword skeleton; A vector conversion module, used to convert the SQL keyword skeleton into a prediction vector, screen a preset number of sample databases from the local connection database, extract question-answer pairs from the sample database, convert the SQL statements of the question-answer pairs into sample SQL vectors, and store them in the vector database; A similarity calculation module, used to calculate the similarity between the predicted vector and the example SQL vector in the vector database, obtain a preset number of similarity results and corresponding example SQL vectors, and take the question-answer pair corresponding to the SQL vector as an example; The statement generation module is used to obtain complete prompt words from the simplified database model, natural language questions, external knowledge description and the example input prompt word template, input the prompt words into the large language model, and the large language model uses the thinking chain to generate SQL statements to complete the natural language query.
6. The natural language query system for relational database according to claim 5, characterized in that: The workflow of the sub-database generation module specifically includes: Obtain all schemas in a locally connected database, where the database schema is structural information of the database; Filter tables and columns across all database schemas based on user selections; A sub-database is generated based on the screening results.
7. The natural language query system for relational database according to claim 6, characterized in that: In the simplified schema extraction module, the process of generating a simplified database schema according to the table name and the column name specifically includes: Determine whether the table name and column name exist in the sub-database schema, and determine whether the nested relationship between the table and column conforms to the sub-database schema. If both are true, a simplified database schema is generated.
8. The natural language query system for relational database according to claim 5, characterized in that: In the skeleton prediction module, the predicted SQL keyword skeleton is a character string consisting of keywords and symbols in the SQL grammar except for the custom content.
Citation Information
Patent Citations
Library query system, method and equipment based on information retrieval and large model and medium
CN118733611A
Text-to-structured query language statement generation method, system and equipment
CN118820285A
Text-to-SQL (Structured Query Language) method and device of mixed strategy, electronic equipment and storage medium
CN119166656A
Question response method and device, electronic equipment, storage medium and system
CN119202186A
Database query method and device, electronic equipment and nonvolatile storage medium
CN119226315A
Cited By
Database query method, device and equipment based on large model retrieval enhancement generation
CN120104762A
Large-scale language model enhanced table question and answer method based on thinking chain reasoning
CN120654835A
Large language model enhanced table question answering method based on chain-of-thought reasoning
CN120654835B
Question and answer method
CN121144470A
Method for question answering
CN121144470B