A method and device for generating SQL statements
By obtaining and integrating semantic association information of historical SQL Q&A pairs, the problem of insufficient background information quality in existing Text2SQL technology is solved, and high-quality SQL statements are generated.
Patent Information
- Application Number
- CN202510402766.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-01
- Publication Date
- 2025-06-24
- Estimated Expiration
- 2045-04-01
AI Technical Summary
Due to the poor background information quality of the existing Text2SQL technology, the generated SQL statements are of poor quality.
By obtaining semantic association information of historical SQL Q&A pairs, integrating historical SQL Q&A pairs and business data related to target query, filtering and extending background data, and generating high-quality SQL statements.
Improve the quality of generated SQL statements, and enhance the understanding and generation accuracy of the model by providing high-quality background data.
Smart Images

Figure CN119917528B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a method and device for generating SQL statements. Background Art
[0002] Structured Query Language (SQL) is a language used for database query and programming. It can be used to efficiently access, update and manage database systems. As a key technology for natural language interaction with databases, Text2SQL technology aims to automatically convert natural language questions into corresponding SQL statements through natural language understanding and database information parsing, thereby achieving convenient database operations.
[0003] The current Text2SQL technology focuses on the optimization of prompt words in large language models, and introduces relevant background information into the prompt words to assist in generating SQL statements. However, due to the poor quality of the background information, the quality of the SQL statements is poor. Summary of the invention
[0004] In view of the above problems, the object of the present invention is to provide a method and device for generating SQL statements, which can mine high-quality background information to improve the quality of generated SQL statements.
[0005] In order to solve the above technical problems, the present invention provides the following technical solutions:
[0006] In one aspect, the present invention provides a method for generating a SQL statement, comprising:
[0007] For each historical SQL question-answer pair, obtain semantic association information between the historical query and the historical SQL statement, wherein the semantic association information includes a plurality of sub-semantic association information and a historical business type corresponding to the historical query, and the semantic association information is obtained by analyzing the historical SQL question-answer pair using business data;
[0008] Integrate the first historical SQL question-answer pair and the first business data related to the target query, and infer a preliminary SQL statement corresponding to the target query, a target business type of the target query, and inference analysis data;
[0009] Using the target business type and the inference analysis data, filter the first business data and the semantic association information of all historical SQL question-answer pairs to obtain the second business data and the first semantic association information;
[0010] Using the target query to expand the first business data, and using the preliminary SQL statement to expand the first historical SQL question-answer pair, to obtain expanded business data and expanded SQL question-answer pairs;
[0011] Integrate the first semantic association information, the second service data, the extended service data, the extended SQL Q&A pairs, the first historical SQL Q&A pairs, and the preliminary SQL statement to generate a target SQL statement corresponding to the target query.
[0012] On the other hand, the present invention also provides an SQL statement generation device, including:
[0013] An acquisition module, configured to, for each historical SQL Q&A pair, acquire the semantic association information between the historical query and the historical SQL statement, where the semantic association information includes multiple sub-semantic association information and the historical service type corresponding to the historical query, and the semantic association information is obtained by analyzing the historical SQL Q&A pairs using service data;
[0014] A preliminary generation module, configured to integrate the first historical SQL Q&A pairs related to the target query and the first service data, and infer the preliminary SQL statement corresponding to the target query, the target service type of the target query, and the inference analysis data;
[0015] A filtering module, configured to filter the first service data and the semantic association information of all historical SQL Q&A pairs with the target service type and the inference analysis data to obtain the second service data and the first semantic association information;
[0016] An extension module, configured to use the target query to extend the first service data, and use the preliminary SQL statement to extend the first historical SQL Q&A pairs to obtain extended service data and extended SQL Q&A pairs;
[0017] A target generation module, configured to integrate the first semantic association information, the second service data, the extended service data, the extended SQL Q&A pairs, the first historical SQL Q&A pairs, and the preliminary SQL statement to generate a target SQL statement corresponding to the target query.
[0018] On the other hand, the present invention also provides an electronic device, including a processor and a memory, where the memory stores multiple instructions; the processor loads the instructions from the memory to execute the steps in any one of the SQL statement generation methods provided by the present invention.
[0019] On the other hand, the present invention also provides a computer-readable storage medium, where the computer-readable storage medium stores multiple instructions, and the instructions are suitable for being loaded by a processor to execute the steps in any one of the SQL statement generation methods provided by the present invention.
[0020] On the other hand, the present invention also provides a computer program product, including a computer program / instructions, and when the computer program / instructions are executed by a processor, the steps in any of the SQL statement generation methods provided by the present invention are implemented.
[0021] The beneficial effects brought by the technical solution provided by the present invention at least include:
[0022] In the embodiment of the present invention, semantic association information between historical queries and historical SQL statements established based on business data can be obtained to enhance the understanding ability of the model. Using the first historical SQL Q&A pairs related to the target query and the first business data, a preliminary SQL statement, a target business type, and inference analysis data are initially analyzed; the target business type and inference analysis data obtained by inference are used to filter the first business data and the semantic association information to obtain more matching background data; in order to enrich the background data, deep mining is enabled for the target business type and the preliminary SQL statement, and finally, the final target SQL statement is generated by combining the mined data, the filtered data, and the preliminary SQL statement, which can provide high-quality background data for the model, and thus can effectively improve the quality of the generated SQL statement. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following will briefly introduce the drawings required for the description of the embodiments. Obviously, the following drawings are only some embodiments of the present invention, and those skilled in the art can obtain other drawings without creative efforts based on these drawings.
[0024] Figure 1 is a schematic diagram of an application scenario of the SQL statement generation method provided by the embodiment of the present invention;
[0025] Figure 2 is a schematic flowchart of the SQL statement generation method provided by the embodiment of the present invention;
[0026] Figure 3 is a schematic diagram of generating sub-semantic association information provided by the embodiment of the present invention;
[0027] Figure 4 is a schematic diagram of obtaining a preliminary SQL statement provided by the embodiment of the present invention;
[0028] Figure 5 is a schematic diagram of filtering, expanding, and generating candidate SQLs provided by the embodiment of the present invention;
[0029] Figure 6 is a schematic diagram of the structure of the SQL statement generation device provided by the embodiment of the present invention;
[0030] Figure 7 It is a schematic structural diagram of an electronic device provided by an embodiment of the present invention. Specific embodiments
[0031] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative efforts belong to the scope of protection of the present invention.
[0032] The present invention provides a method and device for generating SQL statements, which can improve the quality of background data and thus improve the quality of SQL statements.
[0033] It can be understood that in the specific embodiments of the present invention, data related to users, such as data related to query statements input by users, etc., need to obtain user permission or consent, and the collection, use, and processing of relevant data need to comply with relevant laws, regulations, and standards of relevant countries and regions.
[0034] Refer to Figure 1 , which shows a schematic diagram of an application scenario of the SQL statement generation method. Among them, the application scenario may include a terminal 101 and a server 102. Data exchange can be performed between the terminal 101 and the server 102 through a network, and an application program related to question and answer can be installed on the terminal 101. Among them, the terminal 101 can be a mobile phone, a tablet computer, a smart Bluetooth device, a computer, a large screen, etc., a robot, etc.; the server 102 can be a single server or a server cluster composed of multiple servers.
[0035] The user can send a target query to the server 102 through the terminal 101. The server 102 can obtain the semantic association information between the historical query and the historical SQL statement for each historical SQL Q&A pair. Among them, the semantic association information includes multiple sub-semantic association information and the historical business type corresponding to the historical query. The semantic association information is obtained by analyzing the historical SQL Q&A pair using business data; fuse the first historical SQL Q&A pair related to the target query and the first business data, and infer the preliminary SQL statement corresponding to the target query, the target business type of the target query, and the inference analysis data; filter the first business data and the semantic association information of all historical SQL Q&A pairs with the target business type and the inference analysis data to obtain the second business data and the first semantic association information; use the target query to expand the first business data, and use the preliminary SQL statement to expand the first historical SQL Q&A pair to obtain the expanded business data and the expanded SQL Q&A pair; fuse the first semantic association information, the second business data, the expanded business data, the expanded SQL Q&A pair, the first historical SQL Q&A pair, and the preliminary SQL statement to generate the target SQL statement corresponding to the target query.
[0036] Then the server 102 can send the target SQL statement to the terminal 101 so that the terminal 101 can display the target SQL statement to the user.
[0037] In this embodiment, a method for generating an SQL statement is provided, as Figure 2 shown. The specific process of this SQL statement generation method can be as follows:
[0038] S110. For each historical SQL Q&A pair, obtain the semantic association information between the historical query and the historical SQL statement.
[0039] The historical SQL Q&A pair refers to the SQL Q&A pair generated within the historical time period. The historical SQL Q&A pair may include the historical query, the historical SQL statement corresponding to the historical query, and the historical business type corresponding to the historical query. Among them, the historical time period refers to the time period before the current time. For example, one week before the current time, one month before the current time, etc. The specific duration can be set according to actual needs and will not be specifically limited here.
[0040] Among them, the historical SQL Q&A pair can be in a specific business scenario, and the specific business scenario can also be consistent with the scenario of the target query for generating SQL according to actual needs. For example, if the target query is a user query in the e-commerce business, the historical SQL Q&A pair is also in the e-commerce business.
[0041] The historical business type is obtained by classifying historical queries by business. For example, in the e-commerce business, it can include repair types, sales types, etc. By strongly associating the historical business type with the historical query and the corresponding historical SQL statement, historical SQL question-answer pairs can be obtained.
[0042] SQL statements are statements oriented to databases, while user queries are statements based on natural language. In order to query corresponding data from the database to accurately answer user queries, the user query can be converted into an SQL statement according to the semantics of the user query. That is, there is a semantic association between the historical SQL statement and the historical query. For each historical SQL question-answer pair, semantic association information between the historical query and the historical SQL statement therein can be obtained. The semantic association information can include multiple sub-semantic association information, as well as the historical business type corresponding to the historical query.
[0043] Among them, the semantic association information of each historical SQL question-answer pair can be pre-analyzed and stored at a specified location, and can be directly obtained from the specified location when needed. The same word may have different meanings in different business scenarios. In order to ensure the accuracy of the obtained semantic association information, the business data in a specific business scenario can be used to analyze the semantic association between the two. That is, the historical SQL question-answer pairs and business data in the embodiments of the present invention are all data in the same business scenario.
[0044] As an implementation manner, the semantic association information between the historical query and the historical SQL statement can be obtained through the following steps: for each historical SQL question-answer pair, obtain the historical database information corresponding to the historical query in the historical SQL question-answer pair; use the similarity between the historical query and the business data to determine candidate business data from the business data and associate it with the historical business type corresponding to the historical query to obtain the business data to be used; use the similarity between the historical query and other historical queries to determine candidate SQL question-answer pairs in the historical SQL question-answer pair; combine the historical database information, the business data to be used, and the candidate SQL question-answer pairs to guide the third inference model to think about the relevance between the historical query and the historical SQL statement to obtain semantic association information; divide the semantic association information into multiple sub-semantic association information, and associate the sub-semantic association information with the historical business type.
[0045] See Figure 3, which shows a schematic diagram of generating sub-semantic association information. In the embodiments of the present invention, the historical SQL Q&A pairs include historical queries, historical SQL statements, and historical business types. The business data may include data in specific business scenarios, such as business nouns, business noun definitions, synonyms of business nouns, business calculation logics, and various types of text materials generated in specific business scenarios. Optionally, business nouns, business noun definitions, synonyms of business nouns, business calculation logics, etc. can be extracted from the business data, preprocessed, and stored in a noun vector database for subsequent use. Similarly, for various types of text materials in the business data, the text materials can be sliced and stored in a semantic vector database for subsequent use.
[0046] For each historical SQL Q&A pair, the relevance between the historical query and the historical SQL statement can be analyzed through a large language model, and the corresponding semantic association information can be summarized. Among them, a large language model with strong reasoning performance can be used. As an implementation method, semantic association information can be obtained by setting prompt words and then guiding the large language model to analyze using the prompt words.
[0047] It can be understood that in the process of converting a historical query into a historical SQL statement, the database information corresponding to the database related to the historical query needs to be used. The database information may include data table information, foreign keys, etc. in the database, which is also called schema. For the convenience of description, the database information here is denoted as historical database information. The expression format of the historical database information can adopt mainstream design ideas, for example:
[0048] "
DB name
[0049]
table name
[0050] (col name, col type, whether it is the primary key, sample: [col sample value sampling 1, col sample value sampling 1, col sample value sampling 1])
[0051] (col name, col type, whether it is the primary key, sample: [col sample value sampling 1, col sample value sampling 1, col sample value sampling 1])
[0052] (col name, col type, whether it is the primary key, sample: [col sample value sampling 1, col sample value sampling 1, col sample value sampling 1])
[0053]
table2 name
[0055]
Foreign key
[0056] table1.col1=table2.col2
[0057] table1.[col2 + col3 + col4] = table2.[col1 + col2 + col3]
[0058] ...”
[0059] To obtain semantic association information more accurately, for each historical SQL question - answer pair, partial data can be determined from business data and other historical SQL question - answer pairs to provide relevant background data for reasoning and analyzing semantic association information. Optionally, candidate business data can be determined by using the similarity between historical queries and business data.
[0060] As mentioned above, business data can be classified and stored in a noun vector database and a semantic vector database. Similarity matching can be performed on the historical query in the noun vector database, and the first preset number of contents ranked in descending order of similarity can be selected; similarly, similarity matching is performed on the historical query in the semantic vector database, and the second preset number of contents ranked in descending order of similarity can be selected.
[0061] By combining the first preset number of contents and the second preset number of contents, candidate business data can be obtained. Since each historical SQL question - answer pair contains the historical task type corresponding to the historical query, for the candidate business data, the historical task type of the corresponding historical query can be associated with each candidate business data, and then the business data to be used can be obtained.
[0062] Other historical queries refer to the historical queries in other historical SQL question - answer pairs, and other historical SQL question - answer pairs refer to the historical SQL question - answer pairs except the current historical SQL question - answer pair. For example, there are historical SQL question - answer pairs QA1, QA2, and QA3. When analyzing the semantic association information corresponding to QA1, QA2 and QA3 are other historical SQL question - answer pairs; when analyzing the semantic association information corresponding to QA2, QA1 and QA3 are other historical SQL question - answer pairs.
[0063] Perform similarity matching between the current historical query and the historical queries in other historical SQL question - answer pairs, and select the third preset number of historical SQL question - answer pairs in descending order of similarity as candidate SQL question - answer pairs. Among them, the specific values of the first preset number, the second preset number, and the third preset number in the embodiments of the present invention can be set to be the same or different, and can be specifically set according to actual needs.
[0064] Combining historical database information, business data to be used, and candidate SQL Q&A pairs, a third prompt can be constructed to guide the third inference model to analyze semantic association information using the third prompt. The third prompt can be obtained by filling in the content of the third template, which is a prompt template for analyzing semantic association information and may include table creation information slots, business data slots, question slots, and SQL statement slots. For example, the third template can be:
[0065] "
Task
[0066] Carefully analyze
Question
Table Creation Information
Business Data
SQL
Question
[0067]
Table Creation Information
[0068] {schema_1}
[0069]
Business Data
[0070] {knowledge_1}
[0071]
Question
[0072] {question_1}
[0073]
SQL
[0074] {sql_1}”
[0075] Among them, {schema_1} can be used to fill in the historical database information corresponding to the historical query; {knowledge_1} can be used to fill in the business data to be used and the candidate SQL Q&A pairs; {question_1} can be used to fill in the historical query; {sql_1} can be used to fill in the historical SQL statement corresponding to {question_1}. After the data filling is completed, the third prompt can be obtained. Input the third prompt into the third inference model, and the third inference model can analyze the historical query and combine the historical database information, business data to be used, and candidate historical SQL Q&A pairs to analyze the relevance between the historical query and the historical SQL statement, and output semantic association information.
[0076] The semantic association information output by the third reasoning model is segmented according to a format, and it can be segmented into multiple sub-semantic association information, and then the sub-semantic association information can be associated with the historical task type corresponding to the historical query. After processing all historical SQL Q&A pairs in the above manner, they are stored in a specified vector database for subsequent use. Through the above method, a specified vector database for cross-database semantic association between historical SQL Q&A pairs and business data can be constructed, which can provide highly relevant knowledge for subsequent SQL statement generation, improve the understanding ability of the model, and then improve the generation accuracy of SQL statements.
[0077] S120. Integrate the first historical SQL Q&A pair related to the target query and the first business data, and infer the preliminary SQL statement corresponding to the target query, the target business type of the target query, and the inference analysis data.
[0078] The target query is the text for which an SQL statement needs to be generated currently. The target query can be actively input by the user or passed in by other programs. The first historical SQL Q&A pair refers to the historical SQL Q&A pair similar to the target query, which can be specifically obtained through the similarity between the target query and the historical query in each historical SQL Q&A pair. The first business data refers to the business data similar to the target query, which can be specifically obtained through the similarity between the target query and each business data. It should be noted that the similarities involved in the embodiments of the present invention can all be conventional cosine similarities.
[0079] Combined with the first historical SQL Q&A pair and the first business data, the target query can be analyzed by a large language model to analyze the preliminary SQL statement of the target query and the target business type. Among them, the large language model has its corresponding thinking process, and the inference analysis data refers to the thinking process data of the large language model to obtain the preliminary SQL statement and the target business type.
[0080] Reference can be made to Figure 4 , which shows a schematic diagram of obtaining the preliminary SQL statement. Optionally, when inferring the preliminary SQL statement corresponding to the target query, the target business type, and the inference analysis data, it can be determined according to the similarity between the target query and each business data to determine the first business data from the business data; according to the similarity between the target query and the historical query in each historical SQL Q&A pair, determine the first historical SQL Q&A pair from the historical SQL Q&A pairs; fill the first business data, the first historical SQL Q&A pair, and the target database information into the first template to generate the first prompt; use the first prompt to guide the first reasoning model to analyze and infer the target query to generate the preliminary SQL statement and the target business data of the target query; obtain the process data generated by the first reasoning model during the analysis and inference as the inference analysis data.
[0081] As can be seen from the description in the foregoing embodiments, business data can be classified and stored in a noun vector database and a semantic vector database. Encode the target query into a target vector, calculate the similarity between the target vector and each data in the noun vector database, and then sort them in descending order of similarity, and extract the first fourth preset number of data with the highest rankings. Similarly, calculate the similarity between the target vector and each data in the semantic vector database, and then extract the first fifth preset number of data with the highest rankings after sorting in descending order of similarity. Combine the first fourth preset number of data and the first fifth preset number of data in a text splicing manner, and use a preset splicing symbol during splicing to obtain the first business data. Among them, the first fourth preset number and the first fifth preset number can be set according to actual needs and are not specifically limited here, and the splicing symbol can also be set according to actual needs. In the embodiments of the present invention, the splicing symbol can be "\n".
[0082] When determining the first historical SQL Q&A pair, the target query can be encoded into a target vector, and the historical queries in all historical SQL Q&A pairs are encoded into historical query vectors in the same way, calculate the similarity between the target vector and each historical query vector, and screen the first sixth preset number of historical SQL Q&A pairs in descending order of similarity as the first historical SQL Q&A pair.
[0083] The first template refers to a prompt word template for generating a preliminary SQL statement for the target query, and the first template can be set in advance according to actual needs. In the embodiments of the present invention, the first template can be:
[0084] "
Task
[0085] You are a {dialect} data analysis assistant who can combine
Business Materials
Similar Cases
Question
[0086] You need to label the user's question with tags selected from the tag list, which may include multiple tags. Output in the format ```tag\n\n1、...```.
[0087]
Table Creation Information
[0088] {schema_2}
[0089]
Business Materials
[0090] {knowledge_2}
[0091]
Similar Cases
[0092] {cases_2}
[0093]
Problem
[0094] {question_2}”
[0095] Among them, the first template may include multiple slots to be filled. {dialect} is a database type slot, which is a system-level configuration variable and can define the database type. For example, MySQL, Postgre, etc. {schema_2} is a slot for the target database information corresponding to the target query and can be filled with the target database information corresponding to the target query; {knowledge_2} is a slot for business data and can be filled with the first business data; {cases_2} can be filled with the first historical SQL Q&A pairs; {question_2} can be used to fill the target query.
[0096] Fill the content into the slots of the first template correspondingly to generate the first prompt. Input the first prompt into the first inference model. The first inference model can analyze and infer the preliminary SQL statement corresponding to the target query and the target business type according to the requirements in the first prompt. Among them, the first inference model can be a language model with strong inference ability, and the output content can be divided into two parts. One is the internal inference and analysis content of the model, that is, the process data generated by the first inference model during the analysis and inference, which can be used as inference analysis data; the other is the formal reply to the question, that is, the output preliminary SQL statement and the target business type. Combining similar historical SQL Q&A pairs and similar business data can provide more background data for the first inference model to improve the inference accuracy of the preliminary SQL statement and the target business type.
[0097] S130. Filter the semantic association information of the first business data and all historical SQL Q&A pairs with the target business type and the inference analysis data to obtain the second business data and the first semantic association information.
[0098] The semantic association information between the first business data and the historical SQL Q&A pairs are all background data related to the target query. To further improve the quality of the background data, after obtaining the target business type corresponding to the target query, the background data can be filtered and screened based on the target business type to obtain the second business data and the first semantic association information that both match the target query and the target business type.
[0099] Optionally, when obtaining the second service data and the first semantic association information, it may be to obtain the data service type corresponding to the first service data; filter out the first service data whose data service type does not include the target service type from the first service data to obtain the second service data; determine the semantic association information with the same historical service type as the target service type as the intermediate semantic association information; determine the first semantic association information based on the similarity between the intermediate semantic association information and the inference analysis data.
[0100] Among them, reference can be made to Figure 5 , which shows a schematic diagram of filtering, expanding, and generating candidate SQL. When obtaining the semantic association information of historical SQL question-answer pairs, the service data to be used can be determined from the service data by using historical queries. The service data to be used is the data associated with the historical service type. After traversing all historical SQL question-answer pairs, for one service data, there may be multiple corresponding historical service types, or there may be no corresponding historical service type. Based on the above process of determining the service data to be used, the corresponding historical service type can be marked for the corresponding service data.
[0101] When obtaining the data service type of the first service data, the historical service type marked by the first service data can be directly used as its data service type. Filter out the first service data whose data service type does not include the target service type from the first service data to obtain the second service data. In other words, the second service data is the first service data whose data service type includes the target service type.
[0102] In the semantic association information of each historical SQL question-answer pair, there are multiple sub-semantic association information and corresponding historical service types. The semantic association information whose historical service type does not include the target service type can be filtered out, and the remaining is the intermediate semantic association information. In other words, the intermediate semantic association information is the semantic association information whose historical service type includes the target service type.
[0103] Then, the first semantic association information can be determined by using the similarity between the inference analysis data and the intermediate semantic association information. In the embodiments of the present invention, filtering all semantic association information by using the target service type first can effectively reduce the data processing amount of subsequent similarity calculation.
[0104] In some embodiments, when determining the first semantic association information based on the similarity between the intermediate semantic association information and the inference analysis data, the inference analysis data may be segmented to obtain multiple sub-inference data; for each sub-inference data, calculate the similarity between each sub-semantic association information in the intermediate semantic association information and the sub-inference data; for each sub-semantic association information, use the similarities between all sub-inference data and the sub-semantic association information to calculate the target similarity corresponding to the sub-semantic association information; and determine the first semantic association information from the sub-semantic association information based on the target similarity.
[0105] For the obtained inference analysis data, the inference analysis data may be segmented. For example, it may be segmented by the line break character "\n" to obtain multiple sub-inference data. For each sub-inference data, calculate the similarity between the sub-inference data and each sub-semantic association information in each intermediate semantic association information.
[0106] For each sub-semantic association information, there is a similarity between the sub-semantic association information and each sub-inference data, that is, one sub-semantic association information corresponds to multiple similarities, and each similarity corresponds to a sub-inference data one by one. Using the multiple similarities corresponding to the sub-semantic association information, the target similarity corresponding to the sub-semantic association information can be calculated. Optionally, the multiple similarities may be directly summed as the target similarity.
[0107] Sort the sub-semantic association information in descending order of the target similarity, and select the first sixth preset number of sub-semantic association information with higher rankings as the first semantic association information. Among them, the sixth preset number can be set according to actual needs. Use the target business type to filter the redundant information in the first task data and the intermediate semantic association information to obtain highly associated second business data and the first semantic association information.
[0108] S140. Use the target query to expand the first business data, and use the preliminary SQL statement to expand the first historical SQL Q&A pair to obtain expanded business data and an expanded SQL Q&A pair.
[0109] Both the first business data and the first historical SQL Q&A pair are background data related to the target query. In order to provide richer background data for the model during the process of generating SQL, the first business data can be expanded based on the target query to obtain expanded business data; and the first historical SQL Q&A pair can be expanded using the preliminary SQL statement to obtain an expanded SQL Q&A pair.
[0110] As an implementation manner, when extending the first service data and the first historical SQL Q&A pairs, the third service data may be determined from the service data based on the similarity between the target query and each piece of the service data, and the number of the third service data is greater than the number of the first service data; the third service data different from the first service data is used as the extended service data; the preliminary SQL statement is randomly masked to obtain the SQL statement to be used; the second historical SQL Q&A pairs are determined based on the similarity between the SQL statement to be used and each historical SQL Q&A pair, and the number of the second historical SQL Q&A pairs is greater than the number of the first historical SQL Q&A pairs; the second historical SQL Q&A pairs different from the first historical SQL Q&A pairs are used as the extended SQL Q&A pairs.
[0111] Encode the target query into a target vector, and determine the third service data from the service data in the same way as determining the first service data. The difference is that when determining the first service data, the fourth preset number of data is extracted from the noun vector database, and the fifth preset number of data is extracted from the semantic vector database, while when determining the third service data, the seventh preset number of data is extracted from the noun vector database, and the eighth preset number of data is extracted from the semantic vector database. Among them, the seventh preset number is greater than the fourth preset number, and the eighth preset number is greater than the fifth preset number. Specifically, it can be set according to actual needs. For example, the seventh preset number is about 6 times the fourth preset number, and the eighth preset number is about 6 times the fifth preset number.
[0112] The number of the third service data determined in the above manner is also significantly greater than the number of the first service data. The third service data different from the first service data is used as the extended service data.
[0113] For the preliminary SQL statement, the preliminary SQL statement can be randomly masked. Among them, the masking process can be performed with words as the smallest granularity to obtain the SQL statement to be used. Then, the second historical SQL Q&A pairs can be determined based on the similarity between the SQL statement to be used and each historical SQL Q&A pair.
[0114] Optionally, the SQL statement to be used can be encoded into a vector to be used, and the historical SQL statements in each historical SQL Q&A pair can be encoded into historical SQL vectors in the same way. Calculate the similarity between the vector to be used and each historical SQL vector, and sort the historical SQL Q&A pairs corresponding to the historical SQL vectors in descending order of similarity, so as to screen out the first nine preset numbers of historical SQL Q&A pairs with higher rankings as the second historical SQL Q&A pairs. Among them, the ninth preset number is greater than the sixth preset number, and can be specifically set according to actual needs. In the embodiment of the present invention, the ninth preset number is about 6 times the sixth preset number.
[0115] It should be noted that the first historical SQL Q&A pair is determined based on the similarity between the target query and the historical query, while the second historical SQL Q&A pair is determined based on the similarity between the preliminary SQL statement and the historical SQL statement. There may be duplicate content in the first historical SQL Q&A pair and the second historical SQL Q&A pair. Therefore, the second historical SQL Q&A pair different from the first historical SQL Q&A pair can be used as the extended SQL Q&A pair.
[0116] The target query is used to expand the available business data, and the historical SQL Q&A pairs are expanded using the inferred preliminary SQL statement, which can provide more comprehensive and effective background data for the subsequent generation of the target SQL, thereby improving the accuracy of the target SQL.
[0117] S150. Integrate the first semantic association information, the second business data, the extended business data, the extended SQL Q&A pair, the first historical SQL Q&A pair, and the preliminary SQL statement to generate a target SQL statement corresponding to the target query.
[0118] After integrating the first semantic association information, the second business data, the extended business data, the extended SQL Q&A pair, the first historical SQL Q&A pair, and the preliminary SQL statement determined above, a target SQL statement corresponding to the target query can be generated. As an implementation manner, the above data can be directly combined with the database information of the target query and input into the prompt template to obtain the corresponding prompt, and then the corresponding prompt is input into the large language model for inference, so that the large language model outputs the target SQL statement corresponding to the target query.
[0119] As another implementation, in order to generate the target SQL statement more accurately, it is possible to generate multiple intermediate business data based on the first semantic association information, the second business data, and the extended business data; generate multiple intermediate SQL Q&A pairs based on the first historical SQL Q&A pair and the extended SQL Q&A pair; combine the intermediate SQL Q&A pairs and the intermediate business data to obtain multiple intermediate data; for each intermediate data, generate a candidate SQL statement based on the intermediate data, the target query, and the preliminary SQL statement; and determine the target SQL statement based on the execution data of all the candidate SQL statements and the preliminary SQL statement.
[0120] The first semantic association information, the second business data, and the extended business data are all related to business data. They can be regarded as a set of data. By fusing these three types of data, multiple intermediate business data can be generated. The first historical SQL Q&A pair and the extended SQL Q&A pair are both related to historical SQL Q&A pairs. They can be regarded as a set of data. By fusing these two types of data, multiple intermediate SQL Q&As can be generated.
[0121] Optionally, when generating multiple intermediate business data, it is possible to shuffle the extended business data to obtain the processed extended business data; divide the processed extended business data into multiple sub-extended business data; for each sub-extended business data, combine the sub-extended business data, the first semantic association information, and the second business data to obtain the intermediate business data.
[0122] Based on the content of determining the extended business data described above, the extended business data consists of multiple third business data, and the third business data is sorted according to the similarity to the target query. Shuffle the extended business data randomly to obtain the processed extended business data. Then, the extended business data can be divided into multiple sub-extended business data. The number of sub-extended business data can be set according to actual needs, and it is ensured that the data volume in each sub-extended business data is basically the same. For example, divide the extended business data equally into 5 sub-extended business data. Then, combine each sub-extended business data with the first semantic association information and the second business data to obtain the corresponding multiple intermediate business data.
[0123] Optionally, when generating multiple intermediate SQL Q&A pairs, it is possible to shuffle the extended SQL Q&A pairs to obtain the processed extended SQL Q&A pairs; divide the processed extended SQL Q&A pairs into multiple sub-extended SQL Q&A pairs; for each sub-extended SQL Q&A pair, combine the sub-extended SQL Q&A pair and the first historical SQL Q&A pair to obtain the intermediate SQL Q&A pair.
[0124] Based on the content of determining the extended SQL Q&A pairs mentioned above, all the extended SQL Q&A pairs are historical SQL Q&A pairs, and the extended SQL Q&A pairs are sorted according to the similarity with the preliminary SQL statements. Randomly shuffle the extended SQL Q&A pairs to obtain the processed extended SQL Q&A pairs. Then, the extended SQL Q&A pairs can be divided into multiple sub-extended SQL Q&A pairs. Among them, the number of sub-extended SQL Q&A pairs can be the same as the number of sub-extended business data, and it is ensured that the data volume in each sub-extended SQL Q&A pair is basically the same. For example, divide the extended SQL Q&A pairs equally into 5 sub-extended SQL Q&A pairs. Then, combine each sub-extended SQL Q&A pair with the first historical SQL Q&A pair to obtain corresponding multiple intermediate SQL Q&A pairs.
[0125] Combine the intermediate business data and the intermediate SQL Q&A pairs to obtain multiple intermediate data. Among them, it can be to combine one intermediate business data and one intermediate SQL Q&A pair, and the number of intermediate business data is the same as the number of intermediate SQL Q&A pairs. Optionally, the sub-extended business data and the sub-extended SQL Q&A pairs can be numbered according to the same coding rule, and use the number of the sub-extended business data as the number of the corresponding intermediate business data, and use the number of the sub-extended SQL Q&A pair as the number of the corresponding intermediate SQL Q&A. Combine the intermediate business data and the intermediate SQL Q&A pairs with the same number to obtain multiple intermediate data. Among them, the number of multiple intermediate data is the same as the number of intermediate business data and intermediate SQL Q&A pairs.
[0126] For each intermediate data, use the intermediate data, the target query, and the preliminary SQL statement to generate candidate SQL statements, and the number of candidate SQL statements is the same as the number of intermediate data.
[0127] As an implementation method, the intermediate data, the target query, and the preliminary SQL statement can be filled into the second template to obtain the second prompt word; input the second prompt word into the second inference model so that the second inference model outputs candidate SQL statements. Among them, the second template is a pre-set prompt word template for generating candidate SQL statements, and this second template can be as follows:
[0128] "
Task
[0129] You are a {dialect} data analysis assistant who can analyze and reason in combination with
Business Materials
Similar Cases
Question
Preliminary SQL
[0130]
Table Creation Information
[0131] {schema_2}
[0132]
Business Data
[0133] {knowledge_filtered}
[0134] {Q_rc_rag}
[0135] {K_i}
[0136]
Similar Cases
[0137] {cases_2}
[0138] {C_i}
[0139]
Problems
[0140] {question_2}
[0141]
Initial SQL
[0142] {pred_sql}”
[0143] Among them,
Business Data
Similar Cases
[0144] After filling the content, the corresponding second prompt word can be generated. It can be understood that one intermediate data can correspond to generating one second prompt word. Then, based on multiple second prompt words, the second inference model is called in parallel, and the second prompt word is input into the second inference model. What the second inference model outputs is the candidate SQL statement. Thus, multiple candidate SQL statements can be obtained, and through the parallel call method, the inference speed can be effectively improved.
[0145] Execute the candidate SQL statements and the preliminary SQL statements, and obtain their corresponding execution data, which may include execution time, execution status, and execution results. Count the execution data with a successful execution status, and use the SQL with the largest number of overlapping execution results and the shortest execution time as the target SQL. If there is no SQL statement that meets the above conditions, the SQL statement with a successful execution status and a non-empty execution result can be screened out as the first candidate SQL statement; if the first candidate SQL statement includes the preliminary SQL statement, the preliminary SQL statement is used as the target SQL statement; if the first candidate SQL statement does not include the preliminary SQL statement, one is randomly selected as the target SQL statement.
[0146] The SQL statement generation solution provided by the embodiment of the present invention can be applied in various scenarios where SQL statements need to be generated. For example, in intelligent question and answering, the solution provided by the embodiment of the present invention can assist the large language model to convert the target query into a high-quality SQL statement based on the target query input by the user, through the target database information and background data related to the target query, so as to improve the accuracy of the question and answer.
[0147] The method provided by the embodiment of the present invention can establish semantic association information between business data and historical SQL question-answer pairs, and use the first historical SQL question-answer pair related to the target query and the first business data to generate preliminary SQL, target business type and reasoning analysis data. The target business type and the analysis and reasoning data are used to filter out redundant information in the first historical question-answer pair and the semantic association information, and then the filtered data is expanded to deeply mine the data related to the target query to ensure the quality of the background data. The background data is then used to generate the final SQL statement, which can effectively improve the quality of the SQL statement.
[0148] In order to better implement the above method, an embodiment of the present invention further provides a SQL statement generation device, which can be integrated in an electronic device, and the electronic device can be a terminal, a server, etc. Among them, the terminal can be a mobile phone, a tablet computer, a smart Bluetooth device, a laptop, a personal computer, etc.; the server can be a single server or a server cluster composed of multiple servers.
[0149] For example, in this embodiment, the method of the embodiment of the present invention is described in detail by taking the SQL statement generation device being specifically integrated in the server as an example.
[0150] For example, Figure 6 As shown, the SQL statement generating device 200 may include an acquisition module 210 , a preliminary generating module 220 , a filtering module 230 , an expansion module 240 and a target generating module 250 .
[0151] An acquisition module 210, configured to obtain semantic association information between a historical query and a historical SQL statement for each historical SQL Q&A pair. The semantic association information includes a plurality of sub-semantic association information and the historical business type corresponding to the historical query. The semantic association information is obtained by analyzing the historical SQL Q&A pair using business data;
[0152] A preliminary generation module 220, configured to fuse a first historical SQL Q&A pair related to a target query and first business data, and infer a preliminary SQL statement corresponding to the target query, the target business type of the target query, and inference analysis data;
[0153] A filtering module 230, configured to filter the first business data and the semantic association information of all historical SQL Q&A pairs with the target business type and the inference analysis data, to obtain second business data and first semantic association information;
[0154] An expansion module 240, configured to expand the first business data using the target query, and expand the first historical SQL Q&A pair using the preliminary SQL statement, to obtain expanded business data and expanded SQL Q&A pairs;
[0155] A target generation module 250, configured to fuse the first semantic association information, the second business data, the expanded business data, the expanded SQL Q&A pairs, the first historical SQL Q&A pair, and the preliminary SQL statement, to generate a target SQL statement corresponding to the target query.
[0156] In some embodiments, the preliminary generation module 220 is specifically configured to:
[0157] Determine first business data from the business data according to the similarity between the target query and each business data;
[0158] Determine a first historical SQL Q&A pair from the historical SQL Q&A pairs according to the similarity between the target query and the historical queries in each historical SQL Q&A pair;
[0159] Fill the first business data, the first historical SQL Q&A pair, and the target database information into a first template to generate a first prompt;
[0160] Use the first prompt to guide a first inference model to analyze and infer the target query, to generate a preliminary SQL statement and the target business data of the target query;
[0161] Obtain the process data generated by the first inference model during the analysis and inference as the inference analysis data.
[0162] In some embodiments, the filtering module 230 is specifically configured to:
[0163] Obtain the data service type corresponding to the first service data;
[0164] Filter the first service data whose data service type does not include the target service type from the first service data to obtain second service data;
[0165] Determine the semantic association information with the same historical service type as the target service type as the intermediate semantic association information;
[0166] Determine the first semantic association information based on the similarity between the intermediate semantic association information and the inference analysis data.
[0167] In some embodiments, the filtering module 230 is specifically configured to:
[0168] Perform a segmentation process on the inference analysis data to obtain a plurality of sub-inference data;
[0169] For each of the sub-inference data, calculate the similarity between each sub-semantic association information in the intermediate semantic association information and the sub-inference data;
[0170] For each of the sub-semantic association information, calculate the target similarity corresponding to the sub-semantic association information by using the similarities between all the sub-inference data and the sub-semantic association information;
[0171] Determine the first semantic association information from the sub-semantic association information based on the target similarity.
[0172] In some embodiments, the extension module 240 is specifically configured to:
[0173] Based on the similarity between the target query and each of the service data, determine third service data from the service data, and the number of the third service data is greater than the number of the first service data;
[0174] Use the third service data different from the first service data as the extended service data;
[0175] Perform a random masking process on the preliminary SQL statement to obtain a SQL statement to be used;
[0176] Based on the similarity between the SQL statement to be used and each historical SQL Q&A pair, determine a second historical SQL Q&A pair, and the number of the second historical SQL Q&A pair is greater than the number of the first historical SQL Q&A pair;
[0177] Use the second historical SQL Q&A pair different from the first historical SQL Q&A pair as the extended SQL Q&A pair.
[0178] In some embodiments, the target generation module 250 is specifically configured to:
[0179] Generate a plurality of intermediate service data by using the first semantic association information, the second service data, and the extended service data;
[0180] Generate a plurality of intermediate SQL Q&A pairs by using the first historical SQL Q&A pairs and the extended SQL Q&A pairs;
[0181] Combine the intermediate SQL Q&A pairs and the intermediate service data to obtain a plurality of intermediate data;
[0182] For each piece of intermediate data, generate a candidate SQL statement based on the intermediate data, the target query, and the preliminary SQL statement;
[0183] Determine the target SQL statement based on the execution data of all the candidate SQL statements and the preliminary SQL statement.
[0184] In some embodiments, the target generation module 250 is specifically configured to:
[0185] Shuffle the extended service data to obtain processed extended service data;
[0186] Divide the processed extended service data into a plurality of sub-extended service data;
[0187] For each sub-extended service data, combine the sub-extended service data, the first semantic association information, and the second service data to obtain intermediate service data.
[0188] In some embodiments, the target generation module 250 is specifically configured to:
[0189] Shuffle the extended SQL Q&A pairs to obtain processed extended SQL Q&A pairs;
[0190] Divide the processed extended SQL Q&A pairs into a plurality of sub-extended SQL Q&A pairs;
[0191] For each sub-extended SQL Q&A pair, combine the sub-extended SQL Q&A pair and the first historical SQL Q&A pair to obtain an intermediate SQL Q&A pair.
[0192] In some embodiments, the acquisition module 210 is specifically configured to:
[0193] For each historical SQL Q&A pair, acquire the historical database information corresponding to the historical query in the historical SQL Q&A pair;
[0194] Determine candidate business data from the business data based on the similarity between the historical query and the business data, and associate it with the historical business type corresponding to the historical query to obtain business data to be used;
[0195] Determine candidate SQL Q&A pairs from the historical SQL Q&A pairs based on the similarity between the historical query and other historical queries;
[0196] Combining the historical database information, the business data to be used, and the candidate SQL Q&A pairs, guide the third inference model to think about the relevance between the historical query and the historical SQL statement to obtain semantic association information;
[0197] Slice the semantic association information into multiple sub-semantic association information, and associate each sub-semantic association information with the historical business type.
[0198] In specific implementation, each of the above modules can be implemented as an independent entity, or can be combined arbitrarily to be implemented as the same or several entities. For the specific implementation of each of the above modules, reference can be made to the method embodiments above, which will not be elaborated here.
[0199] As can be seen from the above, the SQL statement generation device in this embodiment can obtain the semantic association information between the historical query and the historical SQL statement established based on the business data to enhance the understanding ability of the model. Using the first historical SQL Q&A pairs related to the target query and the first business data, initially analyze the preliminary SQL statement, the target business type, and the inference analysis data; use the inferred target business type and inference analysis data to filter the first business data and the semantic association information to obtain more matching background data; in order to enrich the background data, enable the target business type and the preliminary SQL statement for in-depth mining, and finally generate the final target SQL statement by combining the mined data, the filtered data, and the preliminary SQL statement, which can provide high-quality background data for the model, and thus can effectively improve the quality of the generated SQL statement.
[0200] An embodiment of the present invention also provides an electronic device, which can be a device such as a terminal or a server. Among them, the terminal can be a mobile phone, a tablet computer, a smart Bluetooth device, a notebook computer, a personal computer, etc.; the server can be a single server or a server cluster composed of multiple servers, etc.
[0201] In some embodiments, the SQL statement generation device can also be integrated in multiple electronic devices. For example, the SQL statement generation device can be integrated in multiple servers, and the SQL statement generation method of the present invention is implemented by multiple servers.
[0202] In this embodiment, the electronic device in this embodiment will be described in detail by taking a server as an example. For example, as Figure 7 shown, which shows a schematic structural diagram of the electronic device involved in the embodiment of the present invention. Specifically:
[0203] The electronic device may include a processor 310 with one or more processing cores, a memory 320 with one or more computer-readable storage media, a power supply 330, an input module 340, a communication module 350, and other components. Those skilled in the art can understand that Figure 7 the structure of the electronic device shown in
[0204] does not constitute a limitation on the electronic device, and it may include more or fewer components than shown in the figure, or combine certain components, or have different component arrangements. Among them:
[0205] The processor 310 is the control center of the electronic device, connecting various parts of the entire electronic device through various interfaces and lines. By running or executing software programs and / or modules stored in the memory 320, and by calling data stored in the memory 320, it executes various functions of the electronic device and processes data. In some embodiments, the processor 310 may include one or more processing cores; in some embodiments, the processor 310 may integrate an application processor and a modem processor. Among them, the application processor mainly processes the operating system, user interface, application programs, etc., and the modem processor mainly processes wireless communication. It can be understood that the above modem processor may not be integrated into the processor 310.
[0206] The electronic device further includes a power supply 330 for powering each component. In some embodiments, the power supply 330 may be logically connected to the processor 310 through a power management system, so as to implement functions such as management of charging, discharging, and power consumption management through the power management system. The power supply 330 may further include any components such as one or more DC or AC power supplies, a recharge system, a power failure detection circuit, a power converter or inverter, a power status indicator, etc.
[0207] The electronic device may further include an input module 340, which may be used to receive input digital or character information, and generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function controls.
[0208] The electronic device may further include a communication module 350. In some embodiments, the communication module 350 may include a wireless module. The electronic device may perform short-range wireless transmission through the wireless module of the communication module 350, thereby providing users with wireless broadband Internet access. For example, the communication module 350 may be used to help users send and receive emails, browse web pages, and access streaming media, etc.
[0209] Although not shown, the electronic device may further include a display unit, etc., which will not be elaborated here. Specifically, in this embodiment, the processor 310 in the electronic device will load the executable files corresponding to the processes of one or more application programs into the memory 320 according to the following instructions, and the processor 310 will run the application programs stored in the memory 320, so as to implement the steps in the methods of the embodiments of the present invention.
[0210] For the specific implementation of the above operations, reference may be made to the previous embodiments, which will not be elaborated here.
[0211] As can be seen from the above, the electronic device provided by the embodiments of the present invention can obtain the semantic association information between the historical queries and historical SQL statements established based on business data to enhance the understanding ability of the model, and use the first historical SQL Q&A pairs related to the target query and the first business data to initially analyze the initial SQL statement, the target business type, and the inference analysis data; use the inferred target business type and inference analysis data to filter the first business data and the semantic association information to obtain more matching background data; in order to enrich the background data, enable the target business type and the initial SQL statement for in-depth mining, and finally generate the final target SQL statement by combining the mined data, the filtered data, and the initial SQL statement, which can provide high-quality background data for the model, and thus can effectively improve the quality of the generated SQL statements.
[0212] Those of ordinary skill in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructions or by controlling related hardware through instructions. These instructions can be stored in a computer-readable storage medium and loaded and executed by a processor.
[0213] For this reason, an embodiment of the present invention provides a computer-readable storage medium, in which multiple instructions are stored, and these instructions can be loaded by a processor to execute the steps in any one of the SQL statement generation methods provided by the embodiments of the present invention.
[0214] Among them, the storage medium may include: read-only memory (ROM, Read Only Memory), random access memory (RAM, Random Access Memory), magnetic disk or optical disc, etc.
[0215] According to one aspect of the present invention, there is provided a computer program product or a computer program. The computer program product or the computer program includes computer programs / instructions, and the computer programs / instructions are stored in a computer-readable storage medium. The processor of the electronic device reads the computer programs / instructions from the computer-readable storage medium, and the processor executes the computer programs / instructions, so that the electronic device executes the methods provided in the various optional implementation manners in terms of obtaining semantic association information or generating SQL statements provided in the above embodiments.
[0216] Since the instructions stored in the storage medium can execute the steps in any one of the SQL statement generation methods provided by the embodiments of the present invention, the beneficial effects that can be achieved by any one of the SQL statement generation methods provided by the embodiments of the present invention can be realized. For details, please refer to the previous embodiments and will not be elaborated here.
[0217] The above has introduced in detail a SQL statement generation method and device provided by the embodiments of the present invention. Specific examples are used in this article to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present invention.
Claims
1. A method for generating a SQL statement, characterized in that: The method comprises: For each historical SQL question-answer pair, obtain semantic association information between the historical query and the historical SQL statement, wherein the semantic association information includes a plurality of sub-semantic association information and a historical business type corresponding to the historical query, and the semantic association information is obtained by analyzing the historical SQL question-answer pair using business data; Integrate the first historical SQL question-answer pair and the first business data related to the target query, and infer a preliminary SQL statement corresponding to the target query, a target business type of the target query, and inference analysis data; Using the target business type and the inference analysis data, filter the first business data and the semantic association information of all historical SQL question-answer pairs to obtain the second business data and the first semantic association information; The first business data is expanded using the target query, and the first historical SQL question-answer pair is expanded using the preliminary SQL statement to obtain the expanded business data and the expanded SQL question-answer pair, including: based on the similarity between the target query and each of the business data, third business data is determined from the business data, and the amount of the third business data is greater than the amount of the first business data; the third business data different from the first business data is used as the expanded business data; the preliminary SQL statement is randomly masked to obtain the SQL statement to be used; based on the similarity between the SQL statement to be used and each historical SQL question-answer pair, a second historical SQL question-answer pair is determined, and the amount of the second historical SQL question-answer pair is greater than the amount of the first historical SQL question-answer pair; the second historical SQL question-answer pair different from the first historical SQL question-answer pair is used as the expanded SQL question-answer pair; The first semantic association information, the second business data, the extended business data, the extended SQL question-answer pair, the first historical SQL question-answer pair, and the preliminary SQL statement are integrated to generate a target SQL statement corresponding to the target query; Among them, for each historical SQL question and answer pair, obtaining the semantic association information between the historical query and the historical SQL statement includes: for each historical SQL question and answer pair, obtaining the historical database information corresponding to the historical query in the historical SQL question and answer pair; using the similarity between the historical query and the business data, determining the candidate business data from the business data and associating it with the historical business type corresponding to the historical query to obtain the business data to be used; using the similarity between the historical query and other historical queries, determining the candidate SQL question and answer pair from the historical SQL question and answer pair; combining the historical database information, the business data to be used and the candidate SQL question and answer pair, guiding the third reasoning model to think about the association between the historical query and the historical SQL statement to obtain semantic association information; dividing the semantic association information into multiple sub-semantic association information, and associating each of the sub-semantic association information with the historical business type.
2. The method according to claim 1, characterized in that The first historical SQL question-answer pair and the first business data related to the target query are integrated to infer the preliminary SQL statement corresponding to the target query, the target business type of the target query, and the inference analysis data, including: Determining first business data from the business data according to the similarity between the target query and each business data; Determine a first historical SQL question-answer pair from the historical SQL question-answer pairs according to the similarity between the target query and the historical query in each historical SQL question-answer pair; Filling the first business data, the first historical SQL question-answer pair, and target database information into a first template to generate a first prompt word; Using the first prompt word to guide the first reasoning model, analyzing and reasoning the target query to generate a preliminary SQL statement and target business data of the target query; The process data generated by the first reasoning model in the analysis and reasoning is obtained as the reasoning analysis data.
3. The method according to claim 1, characterized in that The step of filtering the first business data and the semantic association information of all historical SQL question-answer pairs based on the target business type and the inference analysis data to obtain the second business data and the first semantic association information includes: Obtaining a data service type corresponding to the first service data; From the first service data, filtering the first service data whose data service type does not include the target service type to obtain second service data; Determine the semantic association information that the historical service type is consistent with the target service type as the intermediate semantic association information; Based on the similarity between the intermediate semantic association information and the reasoning analysis data, first semantic association information is determined.
4. The method according to claim 3, characterized in that The determining the first semantic association information based on the similarity between the intermediate semantic association information and the reasoning analysis data includes: Segmenting the reasoning analysis data to obtain a plurality of sub-reasoning data; For each of the sub-inference data, calculating the similarity between each sub-semantic association information in the intermediate semantic association information and the sub-inference data; For each of the sub-semantic association information, using the similarities between all sub-inference data and the sub-semantic association information, calculate the target similarity corresponding to the sub-semantic association information; The first semantic association information is determined from the sub-semantic association information according to the target similarity.
5. The method according to claim 1, characterized in that The step of fusing the first semantic association information, the second business data, the extended business data, the extended SQL question-answer pair, the first historical SQL question-answer pair, and the preliminary SQL statement to generate a target SQL statement corresponding to the target query includes: Generate a plurality of intermediate business data using the first semantic association information, the second business data and the extended business data; Generate multiple intermediate SQL question-answer pairs using the first historical SQL question-answer pairs and the extended SQL question-answer pairs; Combining the intermediate SQL question-answer pair and the intermediate business data to obtain a plurality of intermediate data; For each intermediate data, generating a candidate SQL statement based on the intermediate data, the target query and the preliminary SQL statement; A target SQL statement is determined based on the execution data of all the candidate SQL statements and the preliminary SQL statement.
6. The method according to claim 5, characterized in that The generating a plurality of intermediate business data by using the first semantic association information, the second business data and the extended business data includes: Scrambling the extended service data to obtain processed extended service data; Dividing the processed extended service data into a plurality of sub-extended service data; For each sub-extended service data, the sub-extended service data, the first semantic association information and the second service data are combined to obtain intermediate service data.
7. The method according to claim 5, characterized in that The step of generating a plurality of intermediate SQL question-answer pairs using the first historical SQL question-answer pairs and the extended SQL question-answer pairs includes: The extended SQL question-answer pair is shuffled to obtain a processed extended SQL question-answer pair; Dividing the processed expanded SQL question-answer pair into a plurality of sub-expanded SQL question-answer pairs; For each sub-expanded SQL question-answer pair, the sub-expanded SQL question-answer pair and the first historical SQL question-answer pair are combined to obtain an intermediate SQL question-answer pair.
8. A device for generating SQL statements, the device being used to implement the method according to any one of claims 1 to 7, characterized in that: The device comprises: An acquisition module, for acquiring, for each historical SQL question-answer pair, semantic association information between a historical query and a historical SQL statement, wherein the semantic association information includes a plurality of sub-semantic association information and a historical business type corresponding to the historical query, and the semantic association information is obtained by analyzing the historical SQL question-answer pair using business data; A preliminary generation module, used to fuse the first historical SQL question-answer pair and the first business data related to the target query, and infer the preliminary SQL statement corresponding to the target query, the target business type of the target query, and the inference analysis data; A filtering module, used to filter the first business data and the semantic association information of all historical SQL question-answer pairs based on the target business type and the inference analysis data, to obtain the second business data and the first semantic association information; An expansion module, used to expand the first business data using the target query, and to expand the first historical SQL question-answer pair using the preliminary SQL statement, to obtain the expanded business data and the expanded SQL question-answer pair, including: based on the similarity between the target query and each of the business data, determining third business data from the business data, the amount of the third business data being greater than the amount of the first business data; using the third business data different from the first business data as the expanded business data; performing random masking processing on the preliminary SQL statement to obtain a SQL statement to be used; based on the similarity between the SQL statement to be used and each historical SQL question-answer pair, determining a second historical SQL question-answer pair, the amount of the second historical SQL question-answer pair being greater than the amount of the first historical SQL question-answer pair; using the second historical SQL question-answer pair different from the first historical SQL question-answer pair as the expanded SQL question-answer pair; A target generation module, used to fuse the first semantic association information, the second business data, the extended business data, the extended SQL question-answer pair, the first historical SQL question-answer pair, and the preliminary SQL statement to generate a target SQL statement corresponding to the target query; Among them, for each historical SQL question and answer pair, obtaining the semantic association information between the historical query and the historical SQL statement includes: for each historical SQL question and answer pair, obtaining the historical database information corresponding to the historical query in the historical SQL question and answer pair; using the similarity between the historical query and the business data, determining the candidate business data from the business data and associating it with the historical business type corresponding to the historical query to obtain the business data to be used; using the similarity between the historical query and other historical queries, determining the candidate SQL question and answer pair from the historical SQL question and answer pair; combining the historical database information, the business data to be used and the candidate SQL question and answer pair, guiding the third reasoning model to think about the association between the historical query and the historical SQL statement to obtain semantic association information; dividing the semantic association information into multiple sub-semantic association information, and associating each of the sub-semantic association information with the historical business type.
Citation Information
Patent Citations
Question and answer processing method and device, electronic equipment and storage medium
CN113553412A
Text-to-SQL conversion method and system based on large pre-training model
CN118503273A