SQL (Structured Query Language) statement conversion method based on large language model, medium and equipment
Through the SQL statement conversion method based on the large language model, the target vector transformation model is constructed and SQL statements are generated, which solves the problem of insufficient conversion accuracy of SQL statements in the prior art, and improves the accuracy and reliability of the conversion results.
Patent Information
- Application Number
- CN202411969393.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-04-22
AI Technical Summary
The existing SQL statement conversion technology is difficult to apply to the flexibility and diversity of the user's natural language, resulting in poor accuracy of SQL statement conversion, increasing the risk of data errors or business process interference caused by the execution of incorrect SQL statements.
The SQL statement conversion method based on the large language model is adopted. By obtaining the initial statements and preset scenarios input by the user, the target vector transformation model is constructed, the target table is filtered based on the matching degree of statement vectors and description vectors, and the statement vectors and target tables are input into the large language model to generate SQL statements.
Improve the accuracy of SQL statement conversion results, reduce the error conversion situation caused by semantic understanding deviations, and reduce the risk of data errors or business process interference.
Smart Images

Figure CN119862201A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing, and in particular to a SQL statement conversion method, medium and device based on a large language model. Background Art
[0002] With the rapid development of information technology, the amount of data has exploded, and databases have become a key infrastructure for storing and managing massive amounts of data. Converting natural language into SQL statements is of great significance in fields such as data processing and database management. However, non-professional database users or ordinary business personnel are more accustomed to using natural language to express data query, operation and other needs. The traditional way of writing SQL statements requires familiarity with the database's grammatical structure, table structure and various functions, which makes ordinary users face a high technical threshold when interacting with the database, limiting the efficient use of data and the rapid development of business. Therefore, in order to achieve automatic conversion between natural language and SQL statements, improve the efficiency and convenience of data processing, reduce the technical difficulty of database use, and enable more non-professionals to easily interact with the database, thereby promoting the development of data-driven business, the conversion technology that converts natural language into SQL statements came into being.
[0003] At present, SQL statement conversion technology usually relies on a series of pre-defined natural language templates and corresponding SQL templates, and performs statement conversion by matching natural language templates and SQL templates. However, pre-defined templates are difficult to cover all possible natural language expressions and are difficult to adapt to the flexibility and diversity of user natural languages, resulting in poor conversion accuracy of SQL statements and increasing the risk of data errors or business process interference caused by the execution of erroneous SQL statements.
[0004] Therefore, how to improve the accuracy of SQL statement conversion results becomes an urgent problem to be solved. Summary of the invention
[0005] In view of the above technical problems, the technical solution adopted by the present invention is a SQL statement conversion method based on a large language model, and the SQL statement conversion method comprises the following steps:
[0006] S1, obtaining an initial sentence input by a target user, a preset scenario corresponding to the initial sentence, several preset tables, a preset description text and a preset scenario corresponding to each preset table, a preset large language model, and a target vector conversion model corresponding to each preset scenario.
[0007] S2, acquiring a description vector corresponding to each preset table according to a target vector conversion model corresponding to a preset scene corresponding to a preset description text corresponding to each preset table.
[0008] S3, converting the model according to the target vector corresponding to the preset scenario corresponding to the initial sentence, and obtaining the sentence vector corresponding to the initial sentence.
[0009] S4, obtaining the target matching degree between the initial sentence and each preset table according to the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence.
[0010] S5, according to the target matching degree between the initial statement and each preset table, the target table corresponding to the initial statement is screened out from all the preset tables.
[0011] S6: Input the sentence vector and the target table into the preset large language model to obtain the SQL statement corresponding to the initial sentence.
[0012] The present invention also provides a non-transitory computer-readable storage medium, in which at least one instruction or at least one program is stored, and the at least one instruction or at least one program is loaded and executed by a processor to implement the above-mentioned SQL statement conversion method based on a large language model.
[0013] The present invention also provides an electronic device, comprising a processor and the above-mentioned non-transitory computer-readable storage medium.
[0014] The present invention has at least the following beneficial effects: by constructing corresponding target vector conversion models for different preset scenarios, whether converting the description text of the preset table into a description vector or converting the initial sentence input by the user into a sentence vector, accurate mapping can be performed according to the semantic characteristics and data association logic in the specific scenario, so that the vector can more accurately carry the key information in the text, providing data support for subsequent table screening and SQL statement generation, reducing erroneous conversions caused by problems such as semantic understanding deviation, and improving the accuracy of SQL statement conversion results in various preset scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0016] Figure 1 A flowchart of a SQL statement conversion method based on a large language model provided in Embodiment 1 of the present invention;
[0017] Figure 2 A schematic diagram of the structure of a SQL statement conversion device based on a large language model provided in the second embodiment of the present invention. DETAILED DESCRIPTION
[0018] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0019] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It is understood that, where appropriate, the above-mentioned terms used to distinguish similar objects can be interchanged so that the present invention can also implement other embodiments other than the above-mentioned illustrated embodiments or described embodiments. In addition, the terms "including" and "having" and any variations are intended to cover non-exclusive inclusions. For example, a process, method, system, product or server that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0020] Embodiment 1
[0021] The first embodiment provides a SQL statement conversion method based on a large language model. The SQL statement conversion method based on a large language model includes the following steps: Figure 1 As shown:
[0022] S1, obtaining an initial sentence input by a target user, a preset scenario corresponding to the initial sentence, several preset tables, a preset description text and a preset scenario corresponding to each preset table, a preset large language model, and a target vector conversion model corresponding to each preset scenario.
[0023] The initial statement input by the user is a data operation requirement expressed by the user in natural language. The preset scenario is a way to classify different situations that may arise in user needs. The preset table is a data table structure that actually exists in the database and is related to user needs, and can be used as the object of SQL statement operation. The preset description text is used to explain in detail the structure, field meaning, data range and other information of each preset table, which helps to understand the specific composition of the table so as to accurately build the association between the SQL statement and the table.
[0024] For example, preset scenarios can be divided into sales business scenarios, financial business scenarios, human resources business scenarios, etc. according to business fields. They can also be divided into query scenarios, insert scenarios, update scenarios, etc. according to operation types. In an enterprise management database, preset tables may include employee information tables, sales data tables, department information tables, etc. The description text of the employee information table may introduce "This table contains fields such as employee number, name, department, and entry time. The employee number is the primary key and uniquely identifies each employee."
[0025] The preset large language model has powerful language understanding and generation capabilities, and is used to generate SQL statements that meet the requirements based on the input related vector information in the subsequent steps. The preset large language model can be some well-known large language models on the market, or it can be a model trained and customized by the enterprise itself.
[0026] The function of the vector conversion model is to convert natural language text into vector representation. Due to the different language characteristics, data relationships and other factors in different preset scenarios, a vector conversion model that is adapted to them is needed to more accurately measure the relationship between natural language text and vector representation, and provide a suitable vector data foundation for subsequent screening of target tables and generation of SQL statements.
[0027] In a specific implementation, S1 includes the following steps:
[0028] S101, obtaining an initial vector conversion model, a plurality of description text samples, a preset scene and description vector sample corresponding to each description text sample, and a preset sampling ratio corresponding to each preset scene.
[0029] S102, determining a preset sampling ratio as a target sampling ratio, determining each preset scene as an initial scene, and initializing the number of repetitions N=1.
[0030] S103, sampling from a plurality of description text samples according to a target sampling ratio corresponding to each initial scene, to obtain an Nth description text training sample set, an Nth description text verification sample set, and an Nth number of description text samples corresponding to each initial scene in the Nth description text training sample set.
[0031] S104, training the initial vector conversion model according to the Nth description text training sample set, the Nth description text verification sample set and the description vector sample corresponding to each description text sample, and obtaining the Nth vector conversion model and the Nth accuracy of each initial scene in the Nth vector conversion model.
[0032] S105 , obtaining an Nth degree of adaptation between each initial scene and the Nth vector conversion model according to the Nth number and the Nth accuracy rate corresponding to each initial scene.
[0033] S106, for any initial scene, determine the adaptation result corresponding to the current initial scene according to the comparison result of the Nth adaptation degree corresponding to the current initial scene and the preset degree threshold, wherein the adaptation result includes adaptation, non-matching and to be determined.
[0034] S107: If the adaptation result corresponding to the current initial scene is adaptation, the current initial scene is determined as the first scene, and the Nth vector conversion model is determined as the target vector conversion model corresponding to the current first scene.
[0035] S108: If the adaptation result corresponding to the current initial scene is not suitable, the current initial scene is determined as the second scene.
[0036] S109: If the adaptation result corresponding to the current initial scene is to be determined, the current initial scene is determined as the third scene.
[0037] S110, traverse all initial scenes, obtain all first scenes and the target vector conversion model corresponding to each first scene, all second scenes, all third scenes and the number of scenes corresponding to the third scenes.
[0038] S111, acquiring a target description text sample set corresponding to each second scene according to the preset scene and the description vector sample corresponding to each description text sample.
[0039] S113, training the initial vector conversion model according to the target description text sample set corresponding to each second scene and the description vector sample corresponding to each description text sample, to obtain the target vector conversion model corresponding to each second scene.
[0040] S114, if the number of scenes is greater than 0, update the preset sampling ratio corresponding to each third scene and determine the updated preset sampling ratio as the target sampling ratio, determine each third scene as the initial scene, update N=N+1, and repeat step S103 until the updated number of scenes is equal to 0, and obtain the target vector conversion model corresponding to each preset scene.
[0041] Among them, the initial vector conversion model is a general vector conversion model that is not optimized for specific scenarios. Subsequently, training and adjustment will be carried out based on the initial vector conversion model combined with specific data so that it can adapt to the text-to-vector conversion requirements in different preset scenarios.
[0042] The description vector sample is the correct vector representation of the corresponding description text sample, which is used to guide the model to learn how to accurately convert the description text into a vector. The description vector sample can be obtained by converting the description text sample based on a known vector conversion technology. Among them, those skilled in the art know that any vector conversion technology in the prior art falls within the protection scope of the present invention, and will not be repeated here. For example, the vector conversion technology can be Word2Vec technology.
[0043] During the training process of the initial vector conversion model, the description text samples in the Nth description text training sample set as training samples are input into the initial vector conversion model to assist the initial vector conversion model in learning how to convert the description text samples into vectors, and then the description text samples in the Nth description text verification sample set as verification samples are used for verification, and the difference between the vector output by the model and the corresponding description vector sample is compared. The accuracy of the model conversion in each initial scenario is measured by evaluation indicators such as cosine similarity, and finally the Nth vector conversion model and the Nth accuracy of each initial scenario in the Nth vector conversion model are obtained to reflect the accuracy of the current model in converting text into vectors in the corresponding initial scenario.
[0044] For any initial scene, the corresponding Nth degree of adaptation is compared with the preset degree threshold. If the Nth degree of adaptation is higher than the preset degree threshold, it means that the Nth vector conversion model performs well in the initial scene and the adaptation is ideal. If it is lower than the preset degree threshold, it is considered that the Nth vector conversion model is not very suitable for the initial scene. If it is just near the critical point of the preset degree threshold, it is determined to be in a pending state, and the vector conversion model adapted to the initial scene needs to be further updated and matched.
[0045] For the situation determined as the first scene, the target vector conversion model corresponding to each first scene is determined to complete the work of determining the appropriate model in some scenes.
[0046] For the cases determined to be the second scene, a target description text sample set suitable for retraining is collected and sorted out, and the initial vector conversion model corresponding to each second scene is trained separately to obtain the target vector conversion model corresponding to each second scene.
[0047] For the situation judged as the third scene, the vector conversion model is continuously optimized by updating the sampling ratio, repeating training and other operations until all preset scenes have clear model adaptation judgment results, and finally the target vector conversion model corresponding to each preset scene is obtained.
[0048] The above method of finding a suitable vector conversion model for different preset scenarios through multiple iterative training and evaluation aims to improve the accuracy and effectiveness of subsequent SQL statement conversion so that it can better adapt to diverse business scenarios and natural language input situations.
[0049] By acquiring basic data including the initial vector conversion model, descriptive text samples and their related preset scenarios and description vector samples, preset sampling ratios, etc., and through multiple rounds of iterative operations, each preset scenario and related sample data and vector conversion model are sampled, trained, evaluated and classified, and each initial scenario is divided into an adapted first scenario and its target vector conversion model is determined, an unsuitable second scenario is to be retrained, and a third scenario to be judged is to be further analyzed and optimized, until all preset scenarios obtain an adapted target vector conversion model, which lays a precise and adaptable vector conversion foundation for the subsequent natural language and SQL statement conversion based on the target vector conversion model, improves the depth and accuracy of the understanding of the initial statements corresponding to each scenario in the vector conversion process, thereby improving the accuracy of SQL statement generation and reducing the risk of data errors or business process interference caused by the execution of erroneous SQL statements.
[0050] In a specific implementation, the preset sampling ratio corresponding to each preset scene includes a training sample sampling ratio and a verification sample sampling ratio, and S103 includes the following steps:
[0051] S1031, for any initial scene, according to the sampling ratio of training samples corresponding to the current initial scene, a number of description text samples are sampled from all description text samples corresponding to the current initial scene as training samples, and the Nth number of description text samples corresponding to the current initial scene as training samples is obtained.
[0052] S1032 , according to the sampling ratio of verification samples corresponding to the current initial scene, sample a plurality of description text samples from all description text samples corresponding to the current initial scene as verification samples.
[0053] S1033, traverse all initial scenes, and obtain a number of description text samples as training samples and a number of description text samples as verification samples corresponding to each initial scene.
[0054] S1034: Determine a set of several description text samples corresponding to all initial scenes as training samples as the Nth description text training sample set.
[0055] S1035: Determine a set of several description text samples corresponding to all initial scenes as verification samples as the Nth description text verification sample set.
[0056] As mentioned above, from the descriptive text samples in each initial scenario, training samples and verification samples are reasonably divided according to the corresponding sampling ratios, and finally the overall training sample set and verification sample set are constructed, so as to provide the key data preparation and organizational basis for the subsequent training, verification and gradual optimization of the initial vector conversion model based on the training sample set and the verification sample set, thereby creating a suitable vector conversion model for different preset scenarios, and ensuring the scientificity and rationality of the entire model training process.
[0057] In a specific implementation, S105 includes the following steps:
[0058] S1051, normalize the Nth number corresponding to each initial scene to obtain the normalized Nth number corresponding to each initial scene.
[0059] S1052, input the normalized Nth number and Nth accuracy corresponding to each initial scene into the preset adaptation degree calculation model, and obtain the Nth adaptation degree of each initial scene and the Nth vector conversion model, wherein the Nth adaptation degree is inversely proportional to the corresponding normalized Nth number, and the Nth adaptation degree is directly proportional to the corresponding Nth accuracy.
[0060] Among them, there may be large differences in the number of descriptive text samples extracted as training samples from different initial scenes. This embodiment uses a normalization operation to uniformly convert the quantitative values of different magnitudes into a standard, comparable range, creating conditions for the subsequent accurate measurement of the degree of adaptation. The value range of the Nth number after normalization is [0, 1]. Those skilled in the art know that any normalization method in the prior art with a value range of [0, 1] falls within the protection scope of the present invention, and will not be repeated here. For example, the normalization method in this embodiment can be a Min-Max normalization method.
[0061] When the normalized Nth number corresponding to the initial scene is large, it means that there is relatively sufficient training data for the vector conversion model to learn the text conversion mode related to the initial scene. Correspondingly, the abundant data volume should help the vector conversion model to better capture the rules and improve the accuracy. However, when the Nth accuracy corresponding to the initial scene is low, it means that the vector conversion model cannot effectively extract features and understand semantics for the specific data features of the initial scene, so that the generated vector has a large deviation from the actual expected vector. Therefore, when the normalized Nth number corresponding to the initial scene is large and the Nth accuracy is low, it means that the structural design of the vector conversion model cannot fully adapt to the complexity and diversity of the initial scene data, and cannot effectively utilize the description text samples and corresponding description vector samples under the initial scene. Therefore, the Nth adaptation degree is inversely proportional to the corresponding normalized Nth number, and the Nth adaptation degree is proportional to the corresponding Nth accuracy.
[0062] Among them, the preset adaptation degree calculation model is a weighted sum model. The implementer can set the first weight corresponding to the normalized Nth number and the second weight corresponding to the Nth accuracy rate according to actual conditions, and obtain the Nth adaptation degree of each initial scene and the Nth vector conversion model by weighted summing the normalized Nth number and the Nth accuracy rate.
[0063] As described above, normalization is used to allow the number of training samples to participate in the evaluation on a fair and comparable basis. Then, based on the preset relationship between the degree of adaptation and the normalized Nth number and the Nth accuracy, the degree of adaptation is obtained using the adaptation degree calculation model. This provides a scientific and quantitative basis for the entire process to accurately judge the adaptation status of each initial scenario and the vector conversion model, which helps to improve the accuracy and reliability of constructing a vector conversion model that adapts to different preset scenarios, thereby improving the accuracy of subsequent SQL statement conversion.
[0064] In a specific implementation, the preset degree threshold includes a first degree threshold and a second degree threshold, and S106 includes the following steps:
[0065] S1061: If the Nth adaptation degree corresponding to the current initial scene is less than or equal to the first degree threshold, determine that the adaptation result corresponding to the current initial scene is unmatched.
[0066] S1062: If the Nth adaptation degree corresponding to the current initial scene is greater than or equal to the second degree threshold, it is determined that the adaptation result corresponding to the current initial scene is not adapted.
[0067] S1063: If the Nth adaptation degree corresponding to the current initial scene is less than the second degree threshold and greater than the first degree threshold, it is determined that the adaptation result corresponding to the current initial scene is to be determined.
[0068] Among them, if the Nth adaptation degree corresponding to the current initial scene is less than or equal to the pre-set first degree threshold, it means that the Nth vector conversion model in the initial scene has very poor sample utilization effect, scene semantic understanding accuracy, and the ability to generate expected vectors. In this case, it can be clearly determined that the adaptation result corresponding to the initial scene is unsuitable, that is, the vector conversion model trained together under multiple initial scenes is not suitable for the current initial scene. In this case, a target description text sample set suitable for retraining can be collected and sorted, and the initial vector conversion model corresponding to each second scene can be trained separately to obtain an adapted target vector conversion model.
[0069] If the Nth adaptation degree corresponding to the current initial scene is greater than or equal to the set second degree threshold, it means that the Nth vector conversion model has reached an ideal adaptation state in the current initial scene, and can effectively convert the initial sentence in the current initial scene into a suitable vector, and performs well in terms of accuracy, etc., then the current Nth vector conversion model can be used as the target vector conversion model for the current initial scene.
[0070] If the Nth adaptation degree corresponding to the current initial scene is less than the second degree threshold and greater than the first degree threshold, the adaptation result corresponding to the current initial scene is determined to be pending, and more rounds of analysis and processing are required to determine the adaptability of the current initial scene and the current vector conversion model.
[0071] The specific values of the first degree threshold and the second degree threshold can be set by the implementer according to actual conditions.
[0072] In the above, by setting the first degree threshold and the second degree threshold, the three adaptation results of unmatched, matched, and to be judged are clearly and reasonably divided, which provides a clear judgment standard and classification basis for the evaluation and screening of vector conversion models in different initial scenarios, and can classify and process many initial scenarios in an orderly manner, and adopt corresponding optimization strategies for different adaptation results, which helps to more accurately find a suitable target vector conversion model for each preset scenario, thereby ensuring the accuracy and effectiveness of subsequent SQL statement conversion work.
[0073] S2, acquiring a description vector corresponding to each preset table according to a target vector conversion model corresponding to a preset scene corresponding to a preset description text corresponding to each preset table.
[0074] According to the target vector conversion model corresponding to the preset scene corresponding to each preset table, the preset description text corresponding to each preset table is vector-converted to obtain the description vector corresponding to each preset table.
[0075] S3, converting the model according to the target vector corresponding to the preset scenario corresponding to the initial sentence, and obtaining the sentence vector corresponding to the initial sentence.
[0076] Among them, according to the target vector conversion model corresponding to the preset scene corresponding to the initial sentence, the initial sentence is vector converted to obtain the sentence vector corresponding to the initial sentence.
[0077] S4, obtaining the target matching degree between the initial sentence and each preset table according to the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence.
[0078] In a specific implementation, S4 includes the following steps:
[0079] S41, calculating the similarity between the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence.
[0080] S42, determining the similarity between the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence as the target matching degree between the initial sentence and each preset table.
[0081] Among them, the similarity between the description vector corresponding to each preset table and the statement vector corresponding to the initial statement can be used to measure the degree of association between the initial statement and each preset table, so as to obtain the target matching degree between the initial statement and each preset table, which serves as the basis for accurately screening out the target table that best matches the initial statement from many preset tables, thereby improving the accuracy and rationality of the generated SQL statement.
[0082] S5, according to the target matching degree between the initial statement and each preset table, the target table corresponding to the initial statement is screened out from all the preset tables.
[0083] In a specific implementation, S5 includes the following steps:
[0084] S51, comparing the target matching degree between the initial statement and each preset table to obtain the maximum target matching degree.
[0085] S52, determining the preset table corresponding to the maximum target matching degree as the target table corresponding to the initial sentence.
[0086] S6: Input the sentence vector and the target table into the preset large language model to obtain the SQL statement corresponding to the initial sentence.
[0087] Among them, after the preset large language model receives the input content, it generates an SQL statement corresponding to the initial statement based on the mapping relationship between natural language and SQL syntax and semantics learned through a lot of internal training, as well as the understanding of database operation logic.
[0088] In a specific implementation, S6 includes the following steps:
[0089] S61, obtaining a preset input template.
[0090] S62, input the sentence vector and the target table into a preset input template to obtain target input data.
[0091] S63, input the target input data into a preset large language model to obtain an SQL statement corresponding to the initial statement.
[0092] Among them, the preset input template is used to standardize the format and structure of the subsequent input data to the preset large language model. It can be designed in advance by the implementer according to the characteristics of the large language model and the requirements of the SQL statement generation task to ensure that the statement vector and the relevant information of the target table can be effectively integrated to guide the model to generate accurate SQL statements.
[0093] In the above, the initial sentence input by the target user, the preset scene corresponding to the initial sentence, several preset tables, the preset description text and preset scene corresponding to each preset table, the preset large language model and the target vector conversion model corresponding to each preset scene are obtained, and the description vector corresponding to each preset table is obtained according to the target vector conversion model corresponding to the preset scene corresponding to the preset description text corresponding to each preset table, and the sentence vector corresponding to the initial sentence is obtained according to the target vector conversion model corresponding to the preset scene corresponding to the initial sentence, and the target matching degree between the initial sentence and each preset table is obtained according to the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence, and the target matching degree between the initial sentence and each preset table is obtained according to the target matching degree between the initial sentence and each preset table. To a certain extent, the target table corresponding to the initial statement is screened out from all the preset tables, the statement vector and the target table are input into the preset large language model, and the SQL statement corresponding to the initial statement is obtained. By building corresponding target vector conversion models for different preset scenarios, whether converting the description text of the preset table into a description vector or converting the initial statement input by the user into a statement vector, accurate mapping can be performed according to the semantic characteristics and data association logic in a specific scenario, so that the vector can more accurately carry the key information in the text, providing data support for subsequent table screening and SQL statement generation, reducing the erroneous conversion caused by problems such as semantic understanding deviation, and improving the accuracy of SQL statement conversion results in each preset scenario.
[0094] Embodiment 2
[0095] The second embodiment provides a SQL statement conversion device based on a large language model. The SQL statement conversion device based on a large language model includes: Figure 2 As shown:
[0096] The data acquisition module 21 is used to obtain the initial sentence input by the target user, the preset scene corresponding to the initial sentence, several preset tables, the preset description text and preset scene corresponding to each preset table, the preset large language model and the target vector conversion model corresponding to each preset scene.
[0097] The description vector conversion module 22 is used to obtain the description vector corresponding to each preset table according to the target vector conversion model corresponding to the preset scene corresponding to the preset description text corresponding to each preset table.
[0098] The sentence vector conversion module 23 is used to obtain the sentence vector corresponding to the initial sentence according to the target vector conversion model corresponding to the preset scenario corresponding to the initial sentence.
[0099] The target matching degree acquisition module 24 is used to acquire the target matching degree between the initial sentence and each preset table according to the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence.
[0100] The target table screening module 25 is used to screen out the target table corresponding to the initial statement from all the preset tables according to the target matching degree between the initial statement and each preset table.
[0101] The SQL statement conversion module 26 is used to input the statement vector and the target table into the preset large language model to obtain the SQL statement corresponding to the initial statement.
[0102] In a specific implementation, the data acquisition module 21 includes:
[0103] The data acquisition submodule is used to obtain an initial vector conversion model, a number of description text samples, a preset scene and description vector sample corresponding to each description text sample, and a preset sampling ratio corresponding to each preset scene.
[0104] The data initialization submodule is used to determine the preset sampling ratio as the target sampling ratio, determine each preset scene as the initial scene, and initialize the number of repetitions N=1.
[0105] The first training data acquisition submodule is used to sample from a number of description text samples according to a target sampling ratio corresponding to each initial scene, to obtain the Nth description text training sample set, the Nth description text verification sample set, and the Nth number of description text samples corresponding to each initial scene in the Nth description text training sample set.
[0106] The first model training submodule is used to train the initial vector conversion model according to the Nth description text training sample set, the Nth description text verification sample set and the description vector sample corresponding to each description text sample, and obtain the Nth vector conversion model and the Nth accuracy of each initial scene in the Nth vector conversion model.
[0107] The adaptation degree acquisition submodule is used to obtain the Nth adaptation degree of each initial scene and the Nth vector conversion model according to the Nth number and the Nth accuracy rate corresponding to each initial scene.
[0108] The adaptation result acquisition submodule is used to determine the adaptation result corresponding to the current initial scene for any initial scene based on the comparison result of the Nth adaptation degree corresponding to the current initial scene with the preset degree threshold, wherein the adaptation result includes adaptation, non-adaptation and pending judgment.
[0109] The first scene determination submodule is used to determine the current initial scene as the first scene if the adaptation result corresponding to the current initial scene is adaptation, and determine the Nth vector conversion model as the target vector conversion model corresponding to the current first scene.
[0110] The second scene determination submodule is used to determine the current initial scene as the second scene if the adaptation result corresponding to the current initial scene is not suitable.
[0111] The third scene determination submodule is used to determine the current initial scene as the third scene if the adaptation result corresponding to the current initial scene is to be determined.
[0112] The initial scene traversal submodule is used to traverse all initial scenes, obtain all first scenes and the target vector conversion model corresponding to each first scene, all second scenes, all third scenes and the number of scenes corresponding to the third scenes.
[0113] The second training data acquisition submodule is used to acquire a target description text sample set corresponding to each second scene according to the preset scene and description vector sample corresponding to each description text sample.
[0114] The second model training submodule is used to train the initial vector conversion model according to the target description text sample set corresponding to each second scene and the description vector sample corresponding to each description text sample, so as to obtain the target vector conversion model corresponding to each second scene.
[0115] The data updating submodule is used to update the preset sampling ratio corresponding to each third scene if the number of scenes is greater than 0 and determine the updated preset sampling ratio as the target sampling ratio, determine each third scene as the initial scene, update N=N+1, and repeatedly execute the first training data acquisition submodule until the updated number of scenes is equal to 0, and obtain the target vector conversion model corresponding to each preset scene.
[0116] In a specific implementation, the preset sampling ratio corresponding to each preset scene includes a training sample sampling ratio and a verification sample sampling ratio, and the first training data acquisition submodule includes:
[0117] The training sample acquisition unit is used to sample a number of description text samples as training samples from all description text samples corresponding to the current initial scene according to the training sample sampling ratio corresponding to the current initial scene for any initial scene, and obtain the Nth number of description text samples corresponding to the current initial scene as training samples.
[0118] The verification sample acquisition unit is used to sample a number of description text samples as verification samples from all description text samples corresponding to the current initial scene according to the verification sample sampling ratio corresponding to the current initial scene.
[0119] The scene traversal unit is used to traverse all the initial scenes and obtain a number of description text samples corresponding to each initial scene as training samples and a number of description text samples as verification samples.
[0120] The training sample set determination unit is used to determine a set of several description text samples corresponding to all initial scenes as training samples as the Nth description text training sample set.
[0121] The verification sample set determination unit is used to determine a set of several description text samples corresponding to all initial scenes as verification samples as the Nth description text verification sample set.
[0122] In a specific implementation, the adaptation degree acquisition submodule includes:
[0123] The data normalization unit is used to normalize the Nth quantity corresponding to each initial scene, and obtain the normalized Nth quantity corresponding to each initial scene.
[0124] The adaptation degree acquisition unit is used to input the normalized Nth number and Nth accuracy corresponding to each initial scene into a preset adaptation degree calculation model, and obtain the Nth adaptation degree of each initial scene and the Nth vector conversion model, wherein the Nth adaptation degree is inversely proportional to the corresponding normalized Nth number, and the Nth adaptation degree is directly proportional to the corresponding Nth accuracy.
[0125] In a specific implementation, the preset degree threshold includes a first degree threshold and a second degree threshold, and the adaptation result acquisition submodule includes:
[0126] The first adaptation result obtaining unit is used to determine that the adaptation result corresponding to the current initial scene is not suitable if the Nth adaptation degree corresponding to the current initial scene is less than or equal to the first degree threshold.
[0127] The second adaptation result obtaining unit is configured to determine that the adaptation result corresponding to the current initial scene is not adapted if the Nth adaptation degree corresponding to the current initial scene is greater than or equal to a second degree threshold.
[0128] The third adaptation result obtaining unit is used to determine that the adaptation result corresponding to the current initial scene is to be determined if the Nth adaptation degree corresponding to the current initial scene is less than the second degree threshold and greater than the first degree threshold.
[0129] In a specific implementation, the target matching degree acquisition module 24 includes:
[0130] The similarity calculation submodule is used to calculate the similarity between the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence.
[0131] The target matching degree acquisition submodule is used to determine the similarity between the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence as the target matching degree between the initial sentence and each preset table.
[0132] In a specific implementation, the target table screening module 25 includes:
[0133] The matching degree comparison submodule is used to compare the target matching degree between the initial statement and each preset table to obtain the maximum target matching degree.
[0134] The target table screening submodule is used to determine the preset table corresponding to the maximum target matching degree as the target table corresponding to the initial statement.
[0135] In a specific implementation, the SQL statement conversion module 26 includes:
[0136] The preset input template acquisition submodule is used to obtain the preset input template.
[0137] The target input data acquisition submodule is used to input the sentence vector and the target table into the preset input template to obtain the target input data.
[0138] The SQL statement conversion submodule is used to input the target input data into the preset large language model to obtain the SQL statement corresponding to the initial statement.
[0139] It should be noted that the information interaction, execution process and other contents between the above modules are based on the same concept as the embodiment of the method of the present invention. Their specific functions and technical effects can be found in the method embodiment part and will not be repeated here.
[0140] Embodiment 3
[0141] Embodiment 3 of the present invention provides a non-transitory computer-readable storage medium, wherein at least one instruction or at least one program is stored in the non-transitory computer-readable storage medium, and the at least one instruction or at least one program is loaded and executed by a processor to implement the steps:
[0142] S1, obtaining an initial sentence input by a target user, a preset scenario corresponding to the initial sentence, several preset tables, a preset description text and a preset scenario corresponding to each preset table, a preset large language model, and a target vector conversion model corresponding to each preset scenario.
[0143] S2, acquiring a description vector corresponding to each preset table according to a target vector conversion model corresponding to a preset scene corresponding to a preset description text corresponding to each preset table.
[0144] S3, converting the model according to the target vector corresponding to the preset scenario corresponding to the initial sentence, and obtaining the sentence vector corresponding to the initial sentence.
[0145] S4, obtaining the target matching degree between the initial sentence and each preset table according to the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence.
[0146] S5, according to the target matching degree between the initial statement and each preset table, the target table corresponding to the initial statement is screened out from all the preset tables.
[0147] S6: Input the sentence vector and the target table into the preset large language model to obtain the SQL statement corresponding to the initial sentence.
[0148] Those of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be implemented by instructing related hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application may include non-volatile and volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronization link DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0149] Those skilled in the art can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.
[0150] Embodiment 4
[0151] A fourth embodiment of the present invention provides an electronic device, which includes a processor and the non-transitory computer-readable storage medium in the third embodiment of the present invention.
[0152] The above are only preferred embodiments of the present invention, and are not intended to limit the present invention in any form. Although the present invention has been disclosed as a preferred embodiment as above, it is not intended to limit the present invention. Any technician familiar with the profession can make some changes or modify the technical contents disclosed above into equivalent embodiments without departing from the scope of the technical solution of the present invention. However, any simple modification, equivalent change and modification made to the above embodiments based on the technical essence of the present invention without departing from the content of the technical solution of the present invention still fall within the scope of the technical solution of the present invention.
Claims
1. A SQL statement conversion method based on a large language model, characterized in that: The SQL statement conversion method comprises the following steps: S1, obtaining an initial sentence input by a target user, a preset scenario corresponding to the initial sentence, a plurality of preset tables, a preset description text and a preset scenario corresponding to each preset table, a preset large language model, and a target vector conversion model corresponding to each preset scenario; S2, acquiring a description vector corresponding to each preset table according to a target vector conversion model corresponding to a preset scene corresponding to a preset description text corresponding to each preset table; S3, acquiring a sentence vector corresponding to the initial sentence according to a target vector conversion model corresponding to a preset scenario corresponding to the initial sentence; S4, obtaining a target matching degree between the initial sentence and each preset table according to the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence; S5, filtering out a target table corresponding to the initial statement from all preset tables according to the target matching degree between the initial statement and each preset table; S6, inputting the sentence vector and the target table into the preset large language model, and obtaining an SQL statement corresponding to the initial sentence.
2. The SQL statement conversion method based on a large language model according to claim 1, characterized in that: S1 includes the following steps: S101, obtaining an initial vector conversion model, a plurality of description text samples, a preset scene and description vector sample corresponding to each description text sample, and a preset sampling ratio corresponding to each preset scene; S102, determining a preset sampling ratio as a target sampling ratio, determining each preset scene as an initial scene, and initializing the number of repetitions N=1; S103, sampling from the plurality of description text samples according to a target sampling ratio corresponding to each initial scene, to obtain an Nth description text training sample set, an Nth description text verification sample set, and an Nth number of description text samples corresponding to each initial scene in the Nth description text training sample set; S104, training the initial vector conversion model according to the Nth description text training sample set, the Nth description text verification sample set, and the description vector sample corresponding to each description text sample, and obtaining the Nth vector conversion model and the Nth accuracy rate of each initial scene in the Nth vector conversion model; S105, obtaining an Nth degree of adaptation between each initial scene and the Nth vector conversion model according to the Nth number and the Nth accuracy rate corresponding to each initial scene; S106, for any initial scene, determining an adaptation result corresponding to the current initial scene according to a comparison result of the Nth adaptation degree corresponding to the current initial scene with a preset degree threshold, wherein the adaptation result includes adaptation, non-adaptation and pending; S107, if the adaptation result corresponding to the current initial scene is adaptation, the current initial scene is determined as the first scene, and the Nth vector conversion model is determined as the target vector conversion model corresponding to the current first scene; S108, if the adaptation result corresponding to the current initial scene is not suitable, the current initial scene is determined as the second scene; S109, if the adaptation result corresponding to the current initial scene is to be determined, the current initial scene is determined as the third scene; S110, traversing all initial scenes, obtaining all first scenes and a target vector conversion model corresponding to each first scene, all second scenes, all third scenes, and the number of scenes corresponding to the third scenes; S111, acquiring a target description text sample set corresponding to each second scene according to the preset scene and the description vector sample corresponding to each description text sample; S113, training the initial vector conversion model according to the target description text sample set corresponding to each second scene and the description vector sample corresponding to each description text sample, to obtain a target vector conversion model corresponding to each second scene; S114, if the number of scenes is greater than 0, update the preset sampling ratio corresponding to each third scene and determine the updated preset sampling ratio as the target sampling ratio, determine each third scene as the initial scene, update N=N+1, and repeat step S103 until the updated number of scenes is equal to 0, and obtain the target vector conversion model corresponding to each preset scene.
3. The SQL statement conversion method based on a large language model according to claim 2 is characterized in that: The preset sampling ratio corresponding to each preset scene includes a training sample sampling ratio and a verification sample sampling ratio. S103 includes the following steps: S1031, for any initial scene, according to the sampling ratio of training samples corresponding to the current initial scene, a number of description text samples are sampled from all description text samples corresponding to the current initial scene as training samples, and the Nth number of description text samples corresponding to the current initial scene as training samples is obtained; S1032, according to the sampling ratio of the verification samples corresponding to the current initial scene, sampling from all the description text samples corresponding to the current initial scene to obtain a plurality of description text samples as verification samples; S1033, traversing all initial scenes, obtaining a number of description text samples as training samples and a number of description text samples as verification samples corresponding to each initial scene; S1034, determining a set of a plurality of description text samples corresponding to all initial scenes as training samples as an Nth description text training sample set; S1035: Determine a set of several description text samples corresponding to all initial scenes as verification samples as the Nth description text verification sample set.
4. The SQL statement conversion method based on a large language model according to claim 2 is characterized in that: S105 includes the following steps: S1051, normalizing the Nth number corresponding to each initial scene to obtain the normalized Nth number corresponding to each initial scene; S1052, input the normalized Nth number and Nth accuracy corresponding to each initial scene into a preset adaptation degree calculation model, and obtain the Nth adaptation degree of each initial scene and the Nth vector conversion model, wherein the Nth adaptation degree is inversely proportional to the corresponding normalized Nth number, and the Nth adaptation degree is directly proportional to the corresponding Nth accuracy.
5. The SQL statement conversion method based on a large language model according to claim 2, characterized in that: The preset degree threshold includes a first degree threshold and a second degree threshold, and S106 includes the following steps: S1061, if the Nth adaptation degree corresponding to the current initial scene is less than or equal to the first degree threshold, determining that the adaptation result corresponding to the current initial scene is unmatched; S1062: If the Nth adaptation degree corresponding to the current initial scene is greater than or equal to the second degree threshold, determining that the adaptation result corresponding to the current initial scene is not suitable; S1063: If the Nth adaptation degree corresponding to the current initial scene is less than the second degree threshold and greater than the first degree threshold, it is determined that the adaptation result corresponding to the current initial scene is to be determined.
6. The SQL statement conversion method based on a large language model according to claim 1, characterized in that: S4 includes the following steps: S41, calculating the similarity between the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence; S42, determining the similarity between the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence as the target matching degree between the initial sentence and each preset table.
7. The SQL statement conversion method based on a large language model according to claim 1, characterized in that: S5 includes the following steps: S51, comparing the target matching degree between the initial statement and each preset table to obtain the maximum target matching degree; S52, determining the preset table corresponding to the maximum target matching degree as the target table corresponding to the initial sentence.
8. The SQL statement conversion method based on a large language model according to claim 1, characterized in that: S6 includes the following steps: S61, obtaining a preset input template; S62, inputting the sentence vector and the target table into the preset input template to obtain target input data; S63: Input the target input data into the preset large language model to obtain an SQL statement corresponding to the initial statement.
9. A non-transitory computer-readable storage medium, wherein at least one instruction or at least one program is stored in the non-transitory computer-readable storage medium, characterized in that: The at least one instruction or the at least one program is loaded and executed by the processor to implement the SQL statement conversion method based on a large language model as described in any one of claims 1-8.
10. An electronic device, characterized in that: The invention comprises a processor and the non-transitory computer-readable storage medium as claimed in claim 9.