SQL statement generation method, electronic equipment and storage medium
By combining historical query results and large language models and using domain-specific data fine-tuning models, the adaptability and accuracy problems of NL2SQL when applied in specific fields are solved, and more efficient SQL statement generation is achieved.
Patent Information
- Application Number
- CN202411805257.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-09
- Publication Date
- 2025-04-04
AI Technical Summary
When NL2SQL technology is applied in specific fields, it is difficult to understand the user's query intention and adapt to the database structure, resulting in poor accuracy of generated SQL statements.
By obtaining the historical query results with the closest semantics as supplementary model input, SQL statements are generated by combining large language models and fine-tuning models for specific domain data. This method uses dimensionality reduction and dimensionality increase matrices for fine-tuning to enhance the model's adaptability in specific domains.
It improves the adaptability and accuracy of NL2SQL to generate SQL statements in specific fields, and can better understand user query intentions and adapt to specific database structures.
Smart Images

Figure CN119759935A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence, and in particular to a SQL statement generation method, electronic device and storage medium. Background Art
[0002] The core function of NL2SQL (natural language to sql) technology is to convert the user's natural language instructions into structured query language (SQL) statements that can be executed on structured databases. The emergence of this technology allows non-technical users to effectively use data and significantly reduces the complexity of database operations, bringing new development vitality to multiple fields such as business and medicine.
[0003] However, despite the significant progress made by NL2SQL technology, its adaptability is still insufficient when working in specific fields. The reason is that specific fields and databases have unique professional terms and table structures, which makes it difficult for NL2SQL to understand the query intentions of users in the field and to adapt to the specific database structure, resulting in poor accuracy of the generated SQL statements. Therefore, how to improve the adaptability of NL2SQL when working in specific fields has become an urgent problem to be solved in the current widespread application of NL2SQL technology. Summary of the invention
[0004] The present application provides a method for generating SQL statements, which can improve the adaptability of NL2SQL in generating SQL statements in specific domains by obtaining the historical query results with the closest semantics to supplement the model input and using specific domain data to fine-tune the model in conjunction with a large language model.
[0005] In a first aspect, the present application provides a method for generating an SQL statement, the method comprising: obtaining prompt information, the prompt information comprising a target query content input by a user, and a historical query result with the closest semantics to the target query content. Inputting the prompt information into an SQL statement generation model to obtain an SQL statement corresponding to the query content, the SQL statement generation model comprising: a large language model and a target fine-tuning model, the target fine-tuning model comprising a dimensionality reduction matrix and a dimensionality increase matrix, the matrix parameters of the dimensionality reduction matrix and the dimensionality increase matrix are obtained based on training of a data set; the data set is in the same technical field as the target query content.
[0006] The present application provides a method for generating SQL statements, which can train a fine-tuning model using a data set that is the same as the technical field to which the user input content belongs, so that the fine-tuning model has a certain memory ability for the knowledge in the specific technical field. By forming an SQL statement generation model with a fine-tuning model and a large language model, the SQL statement generation model can have a certain adaptability when operating in a specific technical field, so as to better complete the task requirements in a specific field. In addition, the method uses prompt information composed of user input query content and historical query results, so that the prompt information can provide additional contextual information or examples for the input of the SQL statement generation model, thereby improving the accuracy of generating SQL statements.
[0007] In a possible implementation, the prompt information is input into a SQL statement generation model to obtain an SQL statement corresponding to the query content, including: converting the prompt information into a vector representation. The vector representation is input into a large language model and a target fine-tuning model respectively to obtain a first output result and a second output result. The first output result and the second output result are weighted summed to obtain an SQL statement.
[0008] In another possible implementation, the prompt information includes: obtaining a target query content input by a user, determining a historical query result that is semantically closest to the target query content from a historical query library, the historical query library being used to indicate a correspondence between the historical query content and the SQL statement, and constructing the prompt information based on the target query content and the historical query result.
[0009] Another possible implementation method is to determine the historical query results that are closest to the semantics of the query content from the historical query library, including: determining as the first query result the historical query content in the historical query library whose similarity with the target query content is greater than a first threshold. Determine as the second query result the historical query content in the historical query library whose importance with the target query content is greater than a second threshold. Sort the first query result and the second query result based on the retrieval ranking model to obtain the closest historical query result.
[0010] In another possible implementation, the importance is determined based on the inverse document frequency (IDF) of the target query content and the query item weight: IDF is used to indicate the uniqueness of the target query content in the historical query library. The query item weight is used to indicate the frequency and position characteristics of the target query content in the historical query content.
[0011] In another possible implementation, after obtaining the target query content input by the user, the method further includes: inputting the target query content into a classification model to obtain a classification result, the classification model is used to understand and correct the target query content, and the classification result indicates any of the following: clear intent, irrelevant business, and unclear intent.
[0012] Determining the historical query results with the closest semantics to the target query content from the historical query library includes: when the classification result indication is clear, determining the historical query results with the closest semantics to the target query content from the historical query library.
[0013] In another possible implementation, when the classification result indicates that the business is irrelevant or the intention is unclear, feedback information is output, and the feedback information is used to prompt the user to modify the input target query content.
[0014] In a second aspect, the present application provides a device for generating SQL statements, which includes: an acquisition module and a processing module.
[0015] An acquisition module is used to obtain prompt information;
[0016] The processing module is used to input the prompt information into the SQL statement generation model to obtain the SQL statement corresponding to the query content.
[0017] In a possible implementation, the processing module is specifically used to convert the prompt information into a vector representation, input the vector representation into the large language model and the target fine-tuning model respectively to obtain a first output result and a second output result, and perform a weighted summation of the first output result and the second output result to obtain a SQL statement.
[0018] In another possible implementation, the acquisition module is specifically used to acquire the target query content input by the user. Determine the historical query result with the closest semantics to the target query content from the historical query library; the historical query library is used to indicate the correspondence between the historical query content and the SQL statement. Construct prompt information based on the target query content and the historical query results.
[0019] In another possible implementation, the acquisition module is specifically used to determine, in the historical query library, the historical query content whose similarity with the target query content is greater than a first threshold as the first query result. Determine, in the historical query library, the historical query content whose importance with the target query content is greater than a second threshold as the second query result. Sort the first query result and the second query result based on the retrieval ranking model to obtain the closest historical query result.
[0020] In another possible implementation, the importance of the historical query content and the target query content is determined based on the inverse document frequency (IDF) of the target query content and the query item weight: IDF is used to indicate the uniqueness of the target query content in the historical query library. The query item weight is used to indicate the frequency and position characteristics of the target query content in the historical query content.
[0021] In another possible implementation, the acquisition module is further used to input the target query content into a classification model to obtain a classification result; the classification model is used to understand and correct the target query content; the classification result indicates any of the following: clear intent, irrelevant business, unclear intent.
[0022] Determining the historical query results with the closest semantics to the target query content from the historical query library includes: when the classification result indication is clear, determining the historical query results with the closest semantics to the target query content from the historical query library.
[0023] In another possible implementation, the acquisition module is further configured to output feedback information when the classification result indicates that the business is irrelevant or the intention is unclear. The feedback information is used to prompt the user to modify the input target query content.
[0024] In a third aspect, the present application provides an electronic device, comprising: a processor and a memory; the memory stores instructions executable by the processor; when the processor is configured to execute the instructions, the electronic device implements the method of the first aspect above.
[0025] In a fourth aspect, the present application provides a computer-readable storage medium, which includes: computer software instructions; when the computer software instructions are executed in an electronic device, the electronic device implements the method of the first aspect above.
[0026] In a fifth aspect, the present application provides a computer program product. When the computer program product is run on a computer, it enables the computer to execute the steps of the related method described in the first aspect above to implement the method of the first aspect above.
[0027] The beneficial effects of the second to fifth aspects mentioned above refer to the corresponding description of the first aspect and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0028] Figure 1 A flowchart of a method for generating SQL statements provided in this application;
[0029] Figure 2 A flowchart of a method for obtaining prompt information provided by this application;
[0030] Figure 3 A flowchart of a method for obtaining historical query results that are closest in semantics to the target query content provided by this application;
[0031] Figure 4 A schematic diagram of the SQL statement generation process provided for this application;
[0032] Figure 5An architectural reference diagram of a SQL statement generation model provided in this application;
[0033] Figure 6 A flowchart of another SQL statement generation method provided by the present application;
[0034] Figure 7 A flowchart of a method for generating SQL statements provided in this application;
[0035] Figure 8 A schematic diagram of the composition of a SQL statement generation device provided in this application;
[0036] Fig. 9 A schematic diagram of the composition of an electronic device provided in this application. DETAILED DESCRIPTION
[0037] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0038] It should be noted that, in the embodiments of the present application, words such as "exemplarily" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplarily" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplarily" or "for example" is intended to present related concepts in a specific way.
[0039] In order to facilitate the clear description of the technical solutions of the embodiments of the present application, in the embodiments of the present application, the words "first", "second", etc. are used to distinguish the same items or similar items with basically the same functions and effects. Those skilled in the art can understand that the words "first", "second", etc. are not limiting the quantity and execution order.
[0040] In order to better understand the technical solution of this application, the terms involved are explained:
[0041] In the information age, data is growing rapidly. In order to effectively manage and utilize this data, people store it in structured and semi-structured databases. At the same time, to effectively utilize the data in these databases, users need to master SQL statements, which allow users to retrieve, update and manage data from databases in a precise manner. However, for non-technical users, understanding the database structure, mastering SQL syntax, and writing efficient SQL queries is a difficult task, which requires not only non-technical users to have a certain programming foundation, but also to have a deep understanding of the database system.
[0042] It is in this context that NL2SQL technology came into being, which aims to convert users' natural language queries into SQL statements that can be executed on relational databases. Users only need to enter natural language, and NL2SQL technology can convert it into corresponding SQL statements. The emergence of this technology reduces the difficulty of using databases, allowing non-technical personnel to easily use databases, improve query efficiency, and provide strong support for the development of business, medicine and other fields.
[0043] At present, NL2SQL technology has shown diversified characteristics in the implementation of the solution providers. In some open source projects such as Nl2Sql, the conversion from natural language input to SQL query output is realized by adopting the .NET development framework. At the same time, the research on NL2SQL technology is also deepening, including natural language analysis, SQL query construction and result processing, and has made certain progress.
[0044] However, despite the significant development and breakthroughs made by NL2SQL technology in recent years, its performance in specific fields and specific database applications still faces severe challenges. This dilemma is mainly due to the significant differences between fields and databases. The professional terminology and database table structure of each specific field are relatively unique, which hinders the NL2SQL system from accurately understanding the professional terminology in the input query and generating appropriate SQL queries from databases with specific architectures. Therefore, it is urgent to improve the adaptability of NL2SQL when operating in specific fields, so that it can generate SQL statements that are more in line with the technical characteristics of the specific field when operating in a specific field.
[0045] Based on the above problems, the present application provides a SQL statement generation method, an electronic device and a storage medium. The SQL statement generation method can use the same data set as the technical field to which the user input content belongs to train a fine-tuning model, so that the fine-tuning model has a certain memory ability for the knowledge in the specific technical field. By forming an SQL statement generation model with a fine-tuning model and a large language model, the SQL statement generation model can have a certain adaptability when operating in a specific technical field, so as to better complete the task requirements in a specific field. In addition, the method uses prompt information composed of user input query content and historical query results, so that the prompt information can provide additional contextual information or examples for the input of the SQL statement generation model, thereby improving the accuracy of generating SQL statements.
[0046] The SQL statement generation method provided in the present application can be applied to a server. The server can be a server cluster composed of multiple servers, or a single server, or a computer, or a processor or processing chip in a server or computer. The embodiments of the present application do not limit the specific device form of the server.
[0047] In order to better understand the solution of this application, the existing NL2SQL technology is now explained, which is specifically divided into demand analysis and data preparation, model design and implementation, model training and optimization, system deployment and testing, and continuous iteration and improvement, among which:
[0048] Requirements analysis and dataset preparation. Clarify the requirements of the NL2SQL system, the structure and fields of the target database. Collect and prepare annotated datasets of natural language queries and corresponding SQL statements.
[0049] Model design and implementation. Including text preprocessing, semantic parsing and SQL generation. Text preprocessing preprocesses natural language queries, including word segmentation, part-of-speech tagging, named entity recognition and other methods, so that the query can be better understood and processed later; semantic parsing converts the preprocessed natural language query into an intermediate representation (such as an abstract syntax tree, semantic graph, etc.). This is the key point of NL2SQL, which needs to accurately capture the semantic information of the query.
[0050] Model training and optimization. It is divided into model training and model optimization. Model training is to train the NL2SQL model using a labeled data set. During the training process, strategies such as cross-validation and early stopping can be used to prevent overfitting. Model optimization is to adjust model parameters and optimize algorithms based on the performance of the model on the validation set to improve the performance of the model.
[0051] System deployment and testing. It is divided into system deployment and system testing. System deployment is to deploy the trained NL2SQL model to the actual application scenario and integrate it with the database; system testing is to test the deployed system, including functional testing, performance testing, and user experience testing, to ensure that the system can operate normally and meet user needs.
[0052] Continuous iteration and improvement. As the system is used, new natural language queries and corresponding SQL statements are continuously collected, annotated and added to the data set, and the model is trained and optimized using the new data set to improve the accuracy and generalization ability of the model.
[0053] Figure 1 A schematic diagram of a SQL statement generation method provided by this application. Figure 1 As shown, a SQL statement generation method provided by the present application can be implemented by the above server, and specifically includes the following steps:
[0054] S101. Obtain prompt information, where the prompt information includes target query content input by the user and historical query results that are semantically closest to the target query content.
[0055] For example, the prompt information may also be called prompt, which includes: the closest historical query content and the corresponding generated SQL statement, the target query content input by the user, and the database table structure. The database table structure includes the table, column name, and the Chinese translation of the column name.
[0056] It should be understood that the prompt information includes the target query content entered by the user and the historical query results that are semantically closest to the target query content. By adding historical query results, the content richness of the prompt information can be improved, additional contextual information or examples can be provided for the input of the large language model, and the accuracy of the SQL statement generated by the large language model can be improved.
[0057] In some embodiments, prompt information is obtained, such as Figure 2 As shown, the specific steps include:
[0058] S1011. Obtain target query content input by the user.
[0059] In some embodiments, the target query content input by the user can be obtained from an HTTP (hyper text transfer protocol, HTTP) request submitted by the front end.
[0060] S1012: Determine, from the historical query library, the historical query result that is closest in semantics to the target query content.
[0061] The historical query library is used to indicate the correspondence between historical query content and SQL statements.
[0062] In some embodiments, the system collects and stores the user's historical query content query and query result sql to form a historical query query-sql data pair, and stores it in a historical query library in JSON format.
[0063] For example, the specific implementation of S1012 can be found in Figure 3 The corresponding description.
[0064] S1013. Construct prompt information based on the target query content and historical query results.
[0065] For example, the prompt information content may refer to the following structure:
[0066] {
[0067] instruction: "You are an SQL writing expert. Please write the corresponding SQL according to the user's query.",
[0068] Input: "The closest historical query content: What is the GDP and population of a city that has served as a host city more than once? The generated SQL is as follows: SELECT t1.gdp,t1.Regional_Population FROM city as T1 JOINhosting_city as T2 ON T1.city_id=T2.host_city GROUP BY t2.Host_City HAVINGcount(*)>1 The target query content entered by the user: What is the GDP and population of a city that has served as a host city more than 3 times? Please generate the corresponding SQL"
[0069] desc: "Table 1 name: countly_public_safely, Table 1 field Chinese and English analysis crime_rate: crime rate, countly_id: province id, case_burden: population burden Table 2 name: city, Table 2 field Chinese and English analysis city: city name, city_id: city id, gdp: gdp, regional_population: regional population, country_id: province id"
[0070] };
[0071] In some embodiments, the historical query results closest to the semantics of the query content are determined from the historical query library, such as Figure 3 As shown, the specific steps include:
[0072] S101a: Determine in the historical query library that the historical query content and the target query content have a similarity greater than a first threshold as the first query result.
[0073] It should be understood that the similarity between the historical query content and the target query content refers to the historical query in the historical query library that is most similar in structure to the target query content.
[0074] S101b: Determine in the historical query library that the importance of the historical query content to the target query content is greater than a second threshold as the second query result.
[0075] It should be understood that the importance of historical query content and target query content refers to finding the historical query most relevant to the keyword in the historical query library by dividing the target query content into keywords.
[0076] In some embodiments, the importance of historical query content and target query content is determined based on the inverse document frequency IDF of the target query content and the query item weight: IDF is used to indicate the uniqueness of the target query content in the historical query library, and the query item weight is used to indicate the frequency and position characteristics of the target query content in the historical query content.
[0077] Exemplarily, the importance of the historical query content and the target query content can be obtained by the bm25 algorithm. The specific algorithm formula is:
[0078]
[0079] Scoring formula: Given a target query content q and a historical query library record d, their relevance score score(q,d) is calculated by a specific function f(q i ,d) calculated. Among them:
[0080] Inverse Document Frequency (IDF): For each query term q in the target query content i (Also called keyword) IDF measures the rarity of the query term in the historical query library. The higher the IDF value, the more unique the query term is, and the more helpful it is in distinguishing different historical query library records.
[0081] Query item weight: f(q i ,d) is the query item q i The weight in the historical query library record d, which may be based on the frequency, position or other characteristics of the query term in the historical query library record.
[0082] Record length normalization: The is a normalization factor that adjusts the impact of the length of the historical query database records on the score.
[0083] b is a tuning parameter;
[0084] |d| is the length of the history query database record;
[0085] avgdl is a predefined threshold for the length of the history query library records.
[0086] Score calculation: The final score is the product of the IDF value of the query term and the weight of the query term in the historical query library record, multiplied by the above normalization factor. This score reflects the importance of the query term in the historical query library record.
[0087] S101c. Sort the first query result and the second query result based on the retrieval ranking model to obtain the closest historical query result.
[0088] For example, the retrieval ranking model can use the bge-reranker (BAAI general embedding reranker, bge-reranker) model to sort historical similar query results. Bge-reranker can accurately re-rank the retrieval results and improve the relevance and accuracy of the retrieval results. The specific usage process can be found in the relevant technical documents, which will not be described in detail here.
[0089] It should be noted that the present application realizes the most semantically similar historical query through a multi-way recall method, i.e., recalling similar historical query results (corresponding to the first query result in the above step S101a) based on the faiss (facebook AI similarity search, faiss) vector library recall and the similarity hash (simhash) algorithm, and recalling important historical query results (corresponding to the second query result in the above step S101b) based on the best matching 25 (bestmatching 25, bm25) algorithm.
[0090] It should be understood that the historical query method of retrieving the most similar semantics realizes the retrieval enhancement of the input information of the large language model. This method can retrieve information related to the target query content from a large amount of data, thereby providing a reliable information source for the input of the large language model, making the generated SQL more accurate.
[0091] S102: Input the prompt information into the SQL statement generation model to obtain the SQL statement corresponding to the query content.
[0092] Among them, the SQL statement generation model includes: a large language model and a target fine-tuning model.
[0093] The target fine-tuning model includes a dimension-upgrading matrix and a dimension-reducing matrix. Matrix parameters of the dimension-reducing matrix and the dimension-upgrading matrix are obtained based on training of a data set, and the data set is in the same technical field as the target query content.
[0094] It should be understood that by adding the historical query results with the closest semantics to the prompt information, the information richness of the model input can be improved, and relevant examples or knowledge can be provided for the SQL generation model. In addition, a fine-tuning model is trained using a data set in the same field. By combining a large language model with the fine-tuning model, the generated results can be closer to the technical features of a specific field, thereby improving the adaptability and accuracy of NL2SQL in a specific field.
[0095] It should be noted that the target fine-tuning model is obtained by training with LoRA using a dataset in a specific field. In the SQL generation model, the target fine-tuning model is placed on the bypass of the large language model parameter matrix, and the large language model can be fine-tuned by modifying some parameters to improve the adaptability of the large language model to a specific field.
[0096] Specifically, the principle of the LoRA fine-tuning method. The specific steps are as follows:
[0097] 1. Add a bypass next to the large language model to simulate the parameter update through low-rank decomposition (first reduce the dimension and then increase the dimension);
[0098] 2. Initialize the reduced-dimensional matrix A with a random Gaussian distribution and the increased-dimensional matrix B with a zero matrix;
[0099] 3. During training, the large language model remains unchanged, and only matrix A and matrix B are trained;
[0100] 4. After training is completed, multiply the A matrix and the B matrix, and then add the product to the large language model parameters as the fine-tuned model parameters.
[0101] Among them, the dimension reduction matrix A is used in LoRA to map the input parameters from high-dimensional space to low-dimensional space. The low-dimensional space retains the key information and features of the original data and removes redundancy or noise. The dimension of the dimension reduction matrix is usually small, which helps to reduce computational complexity and storage requirements.
[0102] The dimension-raising matrix B is the opposite of the dimension-reducing matrix A. It is responsible for mapping the data or parameters in the low-dimensional space back to the original high-dimensional space. In this process, LoRA is able to fine-tune the model without changing the input and output dimensions of the large language model. The dimension-raising matrix and the dimension-reducing matrix are matched to ensure that they can be multiplied and restored to the dimensions of the original parameters.
[0103] LoRA achieves low-rank adjustment of pre-trained model parameters by introducing dimensionality reduction matrices and dimensionality increase matrices, thereby significantly reducing the number of parameters that need to be trained while maintaining model performance, and improving the efficiency and flexibility of model adjustment.
[0104] For example, suppose a large language model has a weight matrix of 100 rows and 100 columns, which needs to store 10,000 numbers. LoRA may decompose it into a 1000x2 dimension reduction matrix A and a 2x100 dimension increase matrix B. By multiplying matrix A and matrix B, we can approximately restore the weight matrix of the pre-trained model. And by fine-tuning the parameters of the low-dimensional matrix B, we can fine-tune the training model.
[0105] In some embodiments, the prompt information is input into the SQL statement generation model to obtain the SQL statement corresponding to the query content, such as Figure 4 As shown, the specific steps include:
[0106] S1021, converting the prompt information into a vector representation;
[0107] S1022, inputting the vector representation into the large language model and the target fine-tuning model respectively to obtain a first output result and a second output result;
[0108] S1023. Perform weighted summation on the first output result and the second output result to obtain an SQL statement.
[0109] For example, Figure 5 As shown in the figure, the architecture reference diagram of the SQL statement generation model is as follows: Figure 5 As shown, 501 is a dimension-increasing matrix, 502 is a dimension-reducing matrix, 501 and 502 constitute a target fine-tuning model, and the target fine-tuning model and the large language model constitute an SQL generation model. When prompt information is input, it is input into the large language model and the target fine-tuning model respectively, and then the outputs of the two are weighted summed to generate SQL statements.
[0110] Exemplarily, as the prompt information exemplified in the aforementioned step S1013, the SQL generation model generates the SQL statement according to the prompt information as follows:
[0111] SELECT t1.gdp,t1.Regional_Population
[0112] FROM city AS T1 JOIN hosting_city AS T2
[0113] ON T1.city_id=T2.host_city
[0114] GROUP BY t2.Host_City HAVING count(*)>1
[0115] In some embodiments, before step S101, Figure 6 As shown, step S103 may also be included.
[0116] S103: Input the target query content into the classification model to obtain a classification result.
[0117] Specifically, when the classification result shows that the intention is clear, the execution continues; otherwise, feedback information is prompted.
[0118] In some embodiments, after obtaining the target query content input by the user, the method further includes: inputting the target query content into a classification model to obtain a classification result. The classification model is used to understand and correct the target query content, and the classification result indicates any of the following: clear intent, irrelevant business, and unclear intent.
[0119] When the classification result indicates a clear diagram, continue to execute the above S101-S102.
[0120] When the classification result indicates that the business is irrelevant or the intention is unclear, feedback information is prompted, and the feedback information is used to prompt the user to modify the input target query content.
[0121] Specifically, when it is determined that the target query content input by the user is irrelevant to the business, the user will be prompted with the irrelevant part of the target query content. When it is determined that the target query content input by the user is unclear in intent, the user will be prompted with the part of the target query content that cannot be understood in intent.
[0122] For example, if the business domain of NL2SQL is the medical field, and the user inputs "query the GDP of X city last year", since GDP belongs to the economic field and has no relevance to the medical field, the target input content is judged to be irrelevant to the business.
[0123] For example, if the user inputs "query sales data", this paragraph lacks a specific time range and product type, resulting in the system being unable to determine the specific sales data that the user wants to see. Therefore, the target input content is judged to be unclear in intent.
[0124] For example, the classification model is trained by a text convolutional neural network (TextCNN) algorithm. The classification model can understand the semantics of the target query content input by the user and automatically correct the user's expression according to the recognition result, so that users with non-technical backgrounds can also use it conveniently.
[0125] like Figure 7The flowchart of a method for generating SQL statements provided by the present application is shown in FIG. Figure 7 As shown, where:
[0126] Correct the target query content. When the query content entered by the user is obtained, the classification model is used to correct the error. If the query content is judged to be of a clear intent type, the next task is performed. Otherwise, the user is prompted to modify the query content.
[0127] Retrieve the historical query with the closest semantics according to the target query content. First, retrieve similar historical queries based on the vector library Faiss recall and Simhash recall; second, use the BM25 algorithm keyword recall to retrieve important similar history. Merge the two sets of query results.
[0128] Sort the search results by the most semantically similar ones. This module uses the bge-reranler model to re-sort the historical query results according to the principle of the most semantically similar ones, and selects one or more historical query results with the most semantically similar ones.
[0129] Build prompt information based on the sorting results. Build prompt confidence based on the historical query results with the closest semantics. Prompt information may include: target query content, the closest historical query results, and the corresponding SQL statement and table structure description.
[0130] Use a domain-specific dataset to train a fine-tuned model. The domain-specific dataset and the target query content need to be in the same technical field. Use the dataset to train a fine-tuned model through LoRA fine-tuning.
[0131] Combine the fine-tuning model and the large language model into a SQL generation model. Merge the determined fine-tuning model into the large language model to form a SQL generation model.
[0132] The SQL generation model generates SQL statements based on the prompt information. Finally, the prompt information is input into the SQL generation model to generate SQL statements corresponding to the target content.
[0133] This application provides a method for generating SQL statements. First, the historical query results that are closest to the semantics of the target query content input by the user are searched in the historical query library. Prompt information is constructed by combining the historical query results with the target query content to achieve retrieval enhancement, provide additional context information for the input of the large language model, and make the generated SQL more accurate.
[0134] In addition, the present application can train a fine-tuning model through a data set in a specific field. By cooperating with the fine-tuning model and the large language model, the SQL statements generated by the large language model can be more in line with the technical characteristics of the specific field, thereby improving the adaptability of the large language model when operating in a specific field.
[0135] Finally, through the classification model, this application can understand and correct the target query content. By preprocessing the target query content, it can promptly discover errors in the input query content and provide feedback to the user to modify the content, saving the user's time in modifying and debugging queries and improving query efficiency.
[0136] For example, Figure 8 A schematic diagram of the composition of a SQL statement generation device provided in this application. Figure 8 As shown, the SQL statement generating device includes: an acquisition module 801 and a processing module 802.
[0137] The acquisition module 801 is used to acquire prompt information;
[0138] The processing module 802 is used to input the prompt information into the SQL statement generation model to obtain the SQL statement corresponding to the query content.
[0139] In a possible implementation, the processing module is specifically used to convert the prompt information into a vector representation, input the vector representation into the large language model and the target fine-tuning model respectively to obtain a first output result and a second output result, and perform a weighted summation of the first output result and the second output result to obtain a SQL statement.
[0140] In another possible implementation, the acquisition module is specifically used to acquire the target query content input by the user, determine the historical query results that are closest in semantics to the target query content from the historical query library, and the historical query library is used to indicate the correspondence between the historical query content and the SQL statement, and construct prompt information based on the target query content and the historical query results.
[0141] In another possible implementation, the acquisition module is specifically used to determine, in the historical query library, the historical query content whose similarity with the target query content is greater than a first threshold as the first query result. Determine, in the historical query library, the historical query content whose importance with the target query content is greater than a second threshold as the second query result. Sort the first query result and the second query result based on the retrieval ranking model to obtain the closest historical query result.
[0142] In another possible implementation, the importance of the historical query content and the target query content is determined based on the inverse document frequency (IDF) of the target query content and the query item weight: IDF is used to indicate the uniqueness of the target query content in the historical query library. The query item weight is used to indicate the frequency and position characteristics of the target query content in the historical query content.
[0143] In another possible implementation, the acquisition module is further used to input the target query content into a classification model to obtain a classification result. The classification model is used to understand and correct the target query content, and the classification result indicates any of the following: clear intent, irrelevant business, unclear intent.
[0144] Determining the historical query results with the closest semantics to the target query content from the historical query library includes: when the classification result indication is clear, determining the historical query results with the closest semantics to the target query content from the historical query library.
[0145] In another possible implementation, the acquisition module is further configured to output feedback information when the classification result indicates that the business is irrelevant or the intention is unclear. The feedback information is used to prompt the user to modify the input target query content.
[0146] An embodiment of the present application also provides an electronic device, which may be the SQL statement generating device in the above method embodiment. Fig. 9 The following is a schematic diagram of the composition of an electronic device provided in this application. Fig. 9 As shown, the electronic device may include: a processor 901 and a memory 902; the memory 902 stores instructions executable by the processor 901; when the processor 901 is configured to execute the instructions, the server implements the method described in the aforementioned method embodiment.
[0147] In an exemplary embodiment, the present application also provides a computer-readable storage medium on which computer program instructions are stored; when the computer program instructions are executed by a computer, the computer implements the method described in the aforementioned embodiment. The computer-readable storage medium may be a non-temporary computer-readable storage medium, for example, a non-temporary computer-readable storage medium may be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, and an optical data storage device.
[0148] In an exemplary embodiment, the embodiment of the present application further provides a computer program product. When the computer program product is run on a computer, the computer is caused to execute the above-mentioned related method steps to implement the SQL statement generation method in the above-mentioned embodiment.
[0149] The above are only specific implementations of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A method for generating a structured query language SQL statement, characterized in that: The method comprises: Acquire prompt information; the prompt information includes the target query content input by the user and the historical query results that are semantically closest to the target query content; The prompt information is input into an SQL statement generation model to obtain an SQL statement corresponding to the query content; the SQL statement generation model includes: a large language model and a target fine-tuning model; the target fine-tuning model includes a dimensionality reduction matrix and a dimensionality increase matrix, and the matrix parameters of the dimensionality reduction matrix and the dimensionality increase matrix are obtained based on data set training; the data set is in the same technical field as the target query content.
2. The method according to claim 1, characterized in that The step of inputting the prompt information into a SQL statement generation model to obtain an SQL statement corresponding to the query content includes: Convert the prompt information into a vector representation; Inputting the vector representation into the large language model and the target fine-tuning model respectively to obtain a first output result and a second output result; The first output result and the second output result are weightedly summed to obtain the SQL statement.
3. The method according to claim 1, characterized in that The obtaining of prompt information includes: Acquire the target query content input by the user; Determine the historical query result that is closest in semantics to the target query content from the historical query library; the historical query library is used to indicate the correspondence between the historical query content and the SQL statement; The prompt information is constructed based on the target query content and the historical query results.
4. The method according to claim 3, characterized in that: Determining the historical query result closest in semantics to the query content from the historical query library includes: Determine in the historical query library, as a first query result, a historical query content whose similarity with the target query content is greater than a first threshold; Determine in the historical query library that the importance of the historical query content to the target query content is greater than a second threshold as a second query result; The first query result and the second query result are sorted based on a retrieval sorting model to obtain the closest historical query result.
5. The method according to claim 4, characterized in that The importance is determined according to the inverse document frequency (IDF) of the target query content and the query item weight; The IDF is used to indicate the uniqueness of the target query content in the historical query library; The query item weight is used to represent the frequency and position characteristics of the target query content appearing in the historical query content.
6. The method according to claim 3, characterized in that After obtaining the target query content input by the user, the method further includes: Inputting the target query content into a classification model to obtain a classification result; the classification model is used to understand and correct the target query content; the classification result indicates any of the following: clear intent, irrelevant business, unclear intent; Determining the historical query result closest in semantics to the target query content from the historical query library includes: When the classification result indicates a clear intent, a historical query result having the closest semantics to the target query content is determined from the historical query library.
7. The method according to claim 6, characterized in that The method further comprises: When the classification result indicates that the business is irrelevant or the intention is unclear, feedback information is output; the feedback information is used to prompt the user to modify the input target query content.
8. A SQL statement generating device, characterized in that: The device comprises: an acquisition module and a processing module; An acquisition module is used to obtain prompt information; The processing module is used to input the prompt information into the SQL statement generation model to obtain the SQL statement corresponding to the query content.
9. An electronic device, characterized in that: The electronic device comprises: a processor and a memory; The memory stores instructions executable by the processor; When the processor is configured to execute the instructions, the electronic device implements the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes: computer software instructions; When the computer software instructions are executed in an electronic device, the electronic device implements the method according to any one of claims 1 to 7.
Citation Information
Cited By
Natural language intelligent analysis and data query instruction generation method based on large model
CN121188079A